kiss.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 "tnc_kiss.h"
  14. // TODO: remove and use call back function for TNC
  15. #include "cc1200.h"
  16. #include "fcs_calc.h"
  17. buffer_handle_t buffer_handle;
  18. tnc_settings_t tnc_settings;
  19. void kiss_clear_buffer(void)
  20. {
  21. ESP_LOGD(TNC_TAG, "TNC buffer cleared.");
  22. buffer_handle.index = 0;
  23. }
  24. void kiss_append_buffer(uint8_t chr)
  25. {
  26. ESP_LOGD(TNC_TAG, "Appending %x to TNC buffer.", chr);
  27. // ERROR - Packet Buffer Overflow
  28. if (buffer_handle.index >= buffer_handle.max_len)
  29. {
  30. ESP_LOGE(TNC_TAG, "TNC buffer overflow");
  31. kiss_clear_buffer();
  32. }
  33. buffer_handle.buf[(buffer_handle.index)] = chr;
  34. buffer_handle.index = buffer_handle.index + 1;
  35. }
  36. extern RingbufHandle_t radio_tx_buf;
  37. // Frame Format
  38. // [ Command Byte ][ Data ]
  39. void kiss_process_frame(void)
  40. {
  41. uint8_t data_byte, tnc_number;
  42. data_byte = buffer_handle.buf[0];
  43. tnc_number = (data_byte >> 4) & 0x0F; // TODO: Have KISS code respect TNC number
  44. switch( data_byte ) {
  45. case KISS_DATAFRAME: {
  46. ESP_LOGI(TNC_TAG, "Received Data Frame - Length %d", (buffer_handle.index));
  47. // unblock AX.25 code to process code
  48. uint32_t fcs = fcs_calc(&buffer_handle.buf[1], buffer_handle.index-1);
  49. buffer_handle.buf[buffer_handle.index] = fcs & 0xFF;
  50. buffer_handle.buf[buffer_handle.index+1] = fcs>>8 & 0xFF;
  51. xRingbufferSend(radio_tx_buf, &buffer_handle.buf[1], ((buffer_handle.index+1)*sizeof(uint8_t)), 10/portTICK_PERIOD_MS);
  52. ESP_LOG_BUFFER_HEXDUMP(TNC_TAG, &buffer_handle.buf[1], buffer_handle.index+1, ESP_LOG_INFO);
  53. //cc1200_radio_APRSTXPacket(buffer_handle.buf, (buffer_handle.index+2), tnc_settings.tx_delay, tnc_settings.tx_tail);
  54. break;
  55. }
  56. case KISS_CMD_TXDELAY: {
  57. ESP_LOGI(TNC_TAG, "Updated TX Delay");
  58. tnc_settings.tx_delay = buffer_handle.buf[1];
  59. break;
  60. }
  61. case KISS_CMD_P: {
  62. ESP_LOGI(TNC_TAG, "Update Persistence");
  63. tnc_settings.persistence = buffer_handle.buf[1];
  64. break;
  65. }
  66. case KISS_CMD_SLOTTIME: {
  67. ESP_LOGI(TNC_TAG, "Updated Slottime");
  68. tnc_settings.slot_time = buffer_handle.buf[1];
  69. break;
  70. }
  71. case KISS_CMD_TXTAIL: {
  72. ESP_LOGI(TNC_TAG, "Updated TX Tail");
  73. tnc_settings.tx_tail= buffer_handle.buf[1];
  74. break;
  75. }
  76. case KISS_CMD_FULLDUPLEX: {
  77. ESP_LOGI(TNC_TAG, "Updated Full/Half Duplex Setting");
  78. tnc_settings.full_duplex = buffer_handle.buf[1];
  79. break;
  80. }
  81. // case KISS_CMD_SETHARDWARE: {
  82. // break;
  83. // }
  84. // case KISS_CMD_RETURN: {
  85. // tnc_settings.tx_delay = buffer_handle.buff_ptr[1];
  86. // break;
  87. // }
  88. default: {
  89. // error
  90. break;
  91. }
  92. }
  93. kiss_clear_buffer();
  94. return;
  95. }
  96. void tnc_set_paramters(tnc_settings_t s)
  97. {
  98. tnc_settings.tx_delay = s.tx_delay;
  99. tnc_settings.persistence = s.persistence;
  100. tnc_settings.slot_time = s.slot_time;
  101. tnc_settings.tx_tail = s.tx_tail;
  102. tnc_settings.full_duplex = s.full_duplex;
  103. // tnc_settings.tnc_number = s.tnc_number;
  104. }
  105. // TNC needs the ability to RX KISS frame, decode it, schedule it, and then send out via APRS or Arrow-Net
  106. // TNC needs the ability to switch to RX mode,
  107. void tnc_init(void)
  108. {
  109. buffer_handle.max_len = FRAME_BUFFER_SIZE; // TODO: make frame buffer dynamic
  110. // tnc_settings.max_frame_size = FRAME_BUFFER_SIZE;
  111. // TODO: Store / retrieve default TNC settings from flash?
  112. // set default TNC settings
  113. tnc_settings.tx_delay = 5;
  114. tnc_settings.persistence = 63;
  115. tnc_settings.slot_time = 10;
  116. tnc_settings.tx_tail = 1;
  117. tnc_settings.full_duplex = 0;
  118. // setup TNC task with queue
  119. }
  120. // Decode KISS-TNC data from SPP Interface and combine until there is
  121. // is a valid packet. Pass valid packet / frame to AX.25 engine.
  122. // Data Flow:
  123. // (1) SPP interface hands over data array which is runs a state machine to decode the KISS Frame
  124. // (2) Decoded information is uploaded thrown into a buffer
  125. // (3) When frame is complete the state machine will copy or indicate to another task to dump the buffer
  126. // If the frame is not completed, the state machine will wait until more data is provided by the SPP interface.
  127. // If the frame is corrupted or the buffer fills, the frame will be dumped and the error must be logged.
  128. KISS_STATE_t kiss_state = FRAME_END;
  129. void tnc_receive(uint8_t* data, uint16_t len)
  130. {
  131. uint32_t i;
  132. uint8_t chr;
  133. for (i=0; i<len; i++)
  134. {
  135. chr = data[i];
  136. switch(kiss_state) {
  137. case ESC_MODE: {
  138. if (chr == KISS_TFEND)
  139. {
  140. kiss_append_buffer(KISS_FEND); // append FEND to frame
  141. kiss_state = FRAME_ASS; // return to assembly
  142. }
  143. else if (chr == KISS_TFESC)
  144. {
  145. kiss_append_buffer(KISS_FESC); // append FESC to frame
  146. kiss_state = FRAME_ASS; // return to assembly
  147. }
  148. else if (chr == KISS_FEND)
  149. {
  150. kiss_process_frame(); // process frame
  151. kiss_state = FRAME_END; // end frame assembly
  152. }
  153. else
  154. {
  155. // log error
  156. kiss_append_buffer(chr); // append chr to frame
  157. kiss_state = FRAME_ASS; // return to assembly
  158. }
  159. break;
  160. }
  161. case FRAME_END: {
  162. if (chr != KISS_FEND)
  163. {
  164. kiss_append_buffer(chr); // append chr to frame
  165. kiss_state = FRAME_ASS; // return to assembly
  166. }
  167. else
  168. {
  169. kiss_state = FRAME_END; // stay in frame end
  170. }
  171. break;
  172. }
  173. case FRAME_ASS: {
  174. if (chr == KISS_FESC)
  175. {
  176. kiss_state = ESC_MODE;
  177. }
  178. else if (chr == KISS_FEND)
  179. {
  180. kiss_process_frame(); // process frame
  181. kiss_state = FRAME_END; // end frame assembly
  182. }
  183. else
  184. {
  185. kiss_append_buffer(chr); // append chr to frame
  186. kiss_state = FRAME_ASS; // continue frame assembly
  187. }
  188. break;
  189. default: {
  190. ESP_LOGE(TNC_TAG, "TNC FSM Error");
  191. }
  192. }
  193. }
  194. }
  195. }