tnc_kiss.c 6.3 KB

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