kiss.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. }
  100. // TNC needs the ability to RX KISS frame, decode it, schedule it, and then send out via APRS or Arrow-Net
  101. // TNC needs the ability to switch to RX mode,
  102. void kiss_init(uint8_t tnc_number, kiss_cb_t tx_cb, kiss_cb_t rx_cb)
  103. {
  104. // TODO: make frame buffer dynamic
  105. rx_buffer.max_len = FRAME_BUFFER_SIZE;
  106. tx_buffer.max_len = FRAME_BUFFER_SIZE;
  107. // set tnc number
  108. kiss_settings.tnc_number = tnc_number;
  109. // set callback functions
  110. kiss_settings.rx_callback = rx_cb;
  111. kiss_settings.tx_callback = tx_cb;
  112. }
  113. // Decode KISS-TNC data from SPP Interface and combine until there is
  114. // is a valid packet. Pass valid packet / frame to AX.25 engine.
  115. // Data Flow:
  116. // (1) SPP interface hands over data array which is runs a state machine to decode the KISS Frame
  117. // (2) Decoded information is uploaded thrown into a buffer
  118. // (3) When frame is complete the state machine will copy or indicate to another task to dump the buffer
  119. // If the frame is not completed, the state machine will wait until more data is provided by the SPP interface.
  120. // If the frame is corrupted or the buffer fills, the frame will be dumped and the error must be logged.
  121. KISS_STATE_t kiss_state = FRAME_END;
  122. void kiss_receive(uint8_t* data, uint16_t len)
  123. {
  124. uint32_t i;
  125. uint8_t chr;
  126. for (i=0; i<len; i++)
  127. {
  128. chr = data[i];
  129. switch(kiss_state) {
  130. case ESC_MODE: {
  131. if (chr == KISS_TFEND)
  132. {
  133. kiss_append_buffer(KISS_FEND); // append FEND to frame
  134. kiss_state = FRAME_ASS; // return to assembly
  135. }
  136. else if (chr == KISS_TFESC)
  137. {
  138. kiss_append_buffer(KISS_FESC); // append FESC to frame
  139. kiss_state = FRAME_ASS; // return to assembly
  140. }
  141. else if (chr == KISS_FEND)
  142. {
  143. kiss_process_frame(); // process frame
  144. kiss_state = FRAME_END; // end frame assembly
  145. }
  146. else
  147. {
  148. // log error
  149. kiss_append_buffer(chr); // append chr to frame
  150. kiss_state = FRAME_ASS; // return to assembly
  151. }
  152. break;
  153. }
  154. case FRAME_END: {
  155. if (chr != KISS_FEND)
  156. {
  157. kiss_append_buffer(chr); // append chr to frame
  158. kiss_state = FRAME_ASS; // return to assembly
  159. }
  160. else
  161. {
  162. kiss_state = FRAME_END; // stay in frame end
  163. }
  164. break;
  165. }
  166. case FRAME_ASS: {
  167. if (chr == KISS_FESC)
  168. {
  169. kiss_state = ESC_MODE;
  170. }
  171. else if (chr == KISS_FEND)
  172. {
  173. kiss_process_frame(); // process frame
  174. kiss_state = FRAME_END; // end frame assembly
  175. }
  176. else
  177. {
  178. kiss_append_buffer(chr); // append chr to frame
  179. kiss_state = FRAME_ASS; // continue frame assembly
  180. }
  181. break;
  182. default: {
  183. ESP_LOGE(TNC_TAG, "TNC FSM Error");
  184. }
  185. }
  186. }
  187. }
  188. }