kiss.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * tnc_kiss.c
  3. *
  4. * Created on: Feb 26, 2019
  5. * Author: curiousmuch
  6. */
  7. // basic set of functions at this moment which decodes KISS
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "freertos/ringbuf.h"
  11. #include "esp_log.h"
  12. #include <stdio.h>
  13. #include "cc1200.h"
  14. #include "kiss.h"
  15. #include "bt_spp.h"
  16. #include "fcs_calc.h"
  17. kiss_buffer_t rx_buffer;
  18. kiss_buffer_t tx_buffer;
  19. kiss_settings_t kiss_settings;
  20. void static kiss_clear_tx_buffer(void)
  21. {
  22. ESP_LOGD(TNC_TAG, "TNC buffer cleared.");
  23. tx_buffer.index = 0;
  24. tx_buffer.status = FRAME_INCOMPLETE;
  25. }
  26. void static kiss_append_tx_buffer(uint8_t chr)
  27. {
  28. ESP_LOGD(TNC_TAG, "Appending %x to KISS TX buffer.", chr);
  29. // ERROR - Packet Buffer Overflow
  30. if (tx_buffer.index >= tx_buffer.max_len)
  31. {
  32. ESP_LOGE(TNC_TAG, "TNC TX buffer overflow");
  33. kiss_clear_tx_buffer();
  34. }
  35. tx_buffer.buf[(tx_buffer.index)] = chr;
  36. tx_buffer.index = tx_buffer.index + 1;
  37. }
  38. void kiss_transmit(uint8_t type, uint8_t *data, uint32_t len)
  39. {
  40. uint32_t i;
  41. // TODO: Cancel TX to BLE TX Buffer if overflow is detected
  42. // TODO: Can we eliminate TX buffer and use BLE Buffer only?????
  43. kiss_clear_tx_buffer();
  44. kiss_append_tx_buffer(KISS_FEND);
  45. kiss_append_tx_buffer(type);
  46. // cycle
  47. for (i=0; i<(len); i++)
  48. {
  49. if (data[i] == KISS_FEND)
  50. {
  51. kiss_append_tx_buffer(KISS_FESC);
  52. kiss_append_tx_buffer(KISS_TFEND);
  53. }
  54. else if (data[i] == KISS_FESC)
  55. {
  56. kiss_append_tx_buffer(KISS_FESC);
  57. kiss_append_tx_buffer(KISS_TFESC);
  58. }
  59. else
  60. {
  61. kiss_append_tx_buffer(data[i]);
  62. }
  63. }
  64. kiss_append_tx_buffer(KISS_FEND);
  65. // activate callback function
  66. kiss_settings.tx_callback(tx_buffer.buf, tx_buffer.index);
  67. }
  68. void kiss_clear_buffer(void)
  69. {
  70. ESP_LOGD(TNC_TAG, "TNC buffer cleared.");
  71. rx_buffer.index = 0;
  72. }
  73. void kiss_append_buffer(uint8_t chr)
  74. {
  75. ESP_LOGD(TNC_TAG, "Appending %x to TNC buffer.", chr);
  76. // ERROR - Packet Buffer Overflow
  77. if (rx_buffer.index >= rx_buffer.max_len)
  78. {
  79. ESP_LOGE(TNC_TAG, "TNC buffer overflow");
  80. kiss_clear_buffer();
  81. }
  82. rx_buffer.buf[(rx_buffer.index)] = chr;
  83. rx_buffer.index = rx_buffer.index + 1;
  84. }
  85. //extern RingbufHandle_t radio_tx_buf;
  86. // Frame Format
  87. // [ Command Byte ][ Data ]
  88. void kiss_process_frame(void)
  89. {
  90. // minimum kiss frame is 2 bytes
  91. if (rx_buffer.index < 1)
  92. return;
  93. uint8_t data_byte, tnc_number;
  94. data_byte = rx_buffer.buf[0];
  95. tnc_number = (data_byte >> 4) & 0x0F;
  96. // confirm packet is for this device
  97. if (tnc_number == kiss_settings.tnc_number)
  98. kiss_settings.rx_callback(rx_buffer.buf, rx_buffer.index);
  99. kiss_clear_buffer();
  100. }
  101. // TNC needs the ability to RX KISS frame, decode it, schedule it, and then send out via APRS or Arrow-Net
  102. // TNC needs the ability to switch to RX mode,
  103. void kiss_init(uint8_t tnc_number, kiss_cb_t tx_cb, kiss_cb_t rx_cb)
  104. {
  105. // TODO: make frame buffer dynamic
  106. rx_buffer.max_len = FRAME_BUFFER_SIZE;
  107. tx_buffer.max_len = FRAME_BUFFER_SIZE;
  108. // set tnc number
  109. kiss_settings.tnc_number = tnc_number;
  110. // set callback functions
  111. kiss_settings.rx_callback = rx_cb;
  112. kiss_settings.tx_callback = tx_cb;
  113. }
  114. // Decode KISS-TNC data from SPP Interface and combine until there is
  115. // is a valid packet. Pass valid packet / frame to AX.25 engine.
  116. // Data Flow:
  117. // (1) SPP interface hands over data array which is runs a state machine to decode the KISS Frame
  118. // (2) Decoded information is uploaded thrown into a buffer
  119. // (3) When frame is complete the state machine will copy or indicate to another task to dump the buffer
  120. // If the frame is not completed, the state machine will wait until more data is provided by the SPP interface.
  121. // If the frame is corrupted or the buffer fills, the frame will be dumped and the error must be logged.
  122. KISS_STATE_t kiss_state = FRAME_END;
  123. void kiss_receive(uint8_t* data, uint16_t len)
  124. {
  125. uint32_t i;
  126. uint8_t chr;
  127. for (i=0; i<len; i++)
  128. {
  129. chr = data[i];
  130. switch(kiss_state) {
  131. case ESC_MODE: {
  132. if (chr == KISS_TFEND)
  133. {
  134. kiss_append_buffer(KISS_FEND); // append FEND to frame
  135. kiss_state = FRAME_ASS; // return to assembly
  136. }
  137. else if (chr == KISS_TFESC)
  138. {
  139. kiss_append_buffer(KISS_FESC); // append FESC to frame
  140. kiss_state = FRAME_ASS; // return to assembly
  141. }
  142. else if (chr == KISS_FEND)
  143. {
  144. kiss_process_frame(); // process frame
  145. kiss_state = FRAME_END; // end frame assembly
  146. }
  147. else
  148. {
  149. // log error
  150. kiss_append_buffer(chr); // append chr to frame
  151. kiss_state = FRAME_ASS; // return to assembly
  152. }
  153. break;
  154. }
  155. case FRAME_END: {
  156. if (chr != KISS_FEND)
  157. {
  158. kiss_append_buffer(chr); // append chr to frame
  159. kiss_state = FRAME_ASS; // return to assembly
  160. }
  161. else
  162. {
  163. kiss_state = FRAME_END; // stay in frame end
  164. }
  165. break;
  166. }
  167. case FRAME_ASS: {
  168. if (chr == KISS_FESC)
  169. {
  170. kiss_state = ESC_MODE;
  171. }
  172. else if (chr == KISS_FEND)
  173. {
  174. kiss_process_frame(); // process frame
  175. kiss_state = FRAME_END; // end frame assembly
  176. }
  177. else
  178. {
  179. kiss_append_buffer(chr); // append chr to frame
  180. kiss_state = FRAME_ASS; // continue frame assembly
  181. }
  182. break;
  183. default: {
  184. ESP_LOGE(TNC_TAG, "TNC FSM Error");
  185. }
  186. }
  187. }
  188. }
  189. }