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