aprs_decoder.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * aprs_decoder.c
  3. *
  4. * Created on: Aug 25, 2019
  5. * Author: curiousmuch
  6. */
  7. #include "aprs_decoder.h"
  8. /* Public Variables */
  9. decoder_varibles_t v;
  10. /* Private Functions */
  11. uint32_t fcs_calc(uint8_t *data, uint32_t len)
  12. {
  13. uint32_t crc, aprs_polynomial, i, j, byte_lsb, crc_lsb;
  14. crc = 0xFFFF;
  15. aprs_polynomial = 0x8408;
  16. for (i=0;i<len;i++)
  17. {
  18. for (j=0;j<8;j++)
  19. {
  20. byte_lsb = (data[i] >> j) & 0x01;
  21. crc_lsb = crc & 0x0001;
  22. if (crc_lsb != byte_lsb)
  23. crc = (crc >> 1) ^ aprs_polynomial;
  24. else
  25. crc = crc >> 1;
  26. }
  27. }
  28. crc = crc ^ 0xFFFF;
  29. return crc & 0xFFFF;
  30. }
  31. void frame_buffer_init(uint8_t *buf, uint32_t len)
  32. {
  33. v.frame_buffer = buf;
  34. v.frame_buffer_len = len;
  35. }
  36. void aprs_decoder_reset(void)
  37. {
  38. v.decoder_state = FLAG_SEARCH;
  39. v.frame_buffer_index = 0;
  40. //v.frame_max_len = APRS_MAX_FRAME;
  41. v.flag_buffer = 0;
  42. v.flag_buffer_index = 0;
  43. v.byte_buffer = 0;
  44. v.byte_buffer_index = 0;
  45. v.current_nrzi_bit = 0;
  46. v.previous_nrzi_bit = 0;
  47. v.current_bit = 0;
  48. v.one_count = 0;
  49. v.skip_bit_flag = 0;
  50. v.packet_rx = 0;
  51. }
  52. uint8_t get_rx_status(void)
  53. {
  54. if (v.packet_rx)
  55. return 1;
  56. else
  57. return 0;
  58. }
  59. uint8_t flag_found(void)
  60. {
  61. if (v.flag_buffer == 0x7E)
  62. {
  63. return 1;
  64. }
  65. else
  66. {
  67. return 0;
  68. }
  69. }
  70. decoder_output_t ax25_decoder_feed_bit(uint8_t nrzi_bit)
  71. {
  72. v.current_nrzi_bit = nrzi_bit;
  73. // decoder NRZI
  74. if (v.previous_nrzi_bit == v.current_nrzi_bit)
  75. {
  76. v.current_bit = 1;
  77. v.one_count += 1;
  78. }
  79. else
  80. {
  81. v.current_bit = 0;
  82. v.one_count = 0;
  83. }
  84. v.previous_nrzi_bit = v.current_nrzi_bit;
  85. // load bit into flag buffer
  86. v.flag_buffer = (v.flag_buffer >> 1) + (v.current_bit*0x80);
  87. switch(v.decoder_state)
  88. {
  89. case FLAG_SEARCH:
  90. if (flag_found())
  91. {
  92. // set state variable
  93. v.decoder_state = FLAG_FOUND;
  94. v.packet_rx = 1;
  95. // re-initialize buffer indexes
  96. v.flag_buffer_index = 0;
  97. v.frame_buffer_index = 0;
  98. }
  99. else
  100. {
  101. v.decoder_state = FLAG_SEARCH;
  102. v.packet_rx = 0;
  103. }
  104. break;
  105. case FLAG_FOUND:
  106. if (v.flag_buffer_index == 7) // check every 8 bits for flag again
  107. {
  108. if (flag_found()) // if flag is found, payload hasn't started
  109. {
  110. v.decoder_state = FLAG_FOUND;
  111. v.flag_buffer_index = 0;
  112. }
  113. else
  114. {
  115. v.decoder_state = FRAME_START;
  116. // load current bits in byte buffer and remove 0 stuffing
  117. uint8_t i, bit;
  118. v.skip_bit_flag = 0;
  119. v.byte_buffer = 0;
  120. v.byte_buffer_index = 0;
  121. v.one_count = 0;
  122. for (i=0;i<7;i++)
  123. {
  124. bit = (v.flag_buffer << i) & 0x80; // load bit
  125. // count ones for to remove bit stuffing
  126. if (bit)
  127. v.one_count += 1;
  128. else
  129. v.one_count = 0;
  130. // skip bit or store in v.byte_buffer
  131. if (v.skip_bit_flag)
  132. {
  133. v.skip_bit_flag = 0;
  134. // if "0" is not stuffed packet is invalid
  135. if (bit != 0)
  136. {
  137. v.decoder_state = ABORT;
  138. break;
  139. }
  140. }
  141. else
  142. {
  143. v.byte_buffer |= (bit) ? (0x80>>i): 0;
  144. v.byte_buffer_index += 1;
  145. }
  146. if (v.one_count == 5)
  147. v.skip_bit_flag = 1;
  148. }
  149. // check if byte buffer is full
  150. if (v.byte_buffer_index == 7)
  151. {
  152. v.frame_buffer[v.frame_buffer_index] = v.byte_buffer;
  153. v.byte_buffer = 0;
  154. v.byte_buffer_index = 0;
  155. v.frame_buffer_index += 1;
  156. }
  157. break;
  158. }
  159. }
  160. else
  161. {
  162. v.flag_buffer_index += 1;
  163. }
  164. break;
  165. case FRAME_START:
  166. // skip stuffed "0"
  167. if (v.skip_bit_flag == 1)
  168. {
  169. v.skip_bit_flag = 0;
  170. // bit will only not be stuffed properly if the HDLC flag is being received
  171. // indicating the end of the frame or the packet has become corrupted.
  172. if (v.current_bit != 0)
  173. {
  174. v.decoder_state = FRAME_BREAK;
  175. break;
  176. }
  177. }
  178. else
  179. {
  180. // load bit
  181. v.byte_buffer |= v.current_bit*(0x01<<v.byte_buffer_index);
  182. v.byte_buffer_index += 1;
  183. // check if byte buffer is full
  184. if (v.byte_buffer_index > 7)
  185. {
  186. v.frame_buffer[v.frame_buffer_index] = v.byte_buffer;
  187. v.byte_buffer = 0;
  188. v.byte_buffer_index = 0;
  189. v.frame_buffer_index += 1;
  190. // check for overflow
  191. if (v.frame_buffer_index >= v.frame_buffer_len)
  192. {
  193. v.decoder_state = ABORT;
  194. break;
  195. }
  196. }
  197. }
  198. if(v.one_count == 5)
  199. v.skip_bit_flag = 1;
  200. break;
  201. default:
  202. // we're in trouble.
  203. break;
  204. }
  205. if (v.decoder_state == FRAME_BREAK)
  206. {
  207. // state
  208. v.decoder_state = FLAG_SEARCH;
  209. // calculate CRC
  210. volatile uint32_t fcs, frame_fcs;
  211. if (v.frame_buffer_index < 10) // TODO: Determine minimum APRS packet
  212. return ERROR_PACKET_FORMAT;
  213. v.frame_len = v.frame_buffer_index - 2;
  214. fcs = fcs_calc(v.frame_buffer, v.frame_len);
  215. frame_fcs = (v.frame_buffer[v.frame_len+1]<<8) |
  216. v.frame_buffer[v.frame_len];
  217. if (fcs == frame_fcs)
  218. return FRAME_DECODED;
  219. else
  220. return ERROR_FCS_MISMATCH;
  221. }
  222. if (v.decoder_state == ABORT)
  223. {
  224. // re-initialize state machine
  225. aprs_decoder_init();
  226. }
  227. return NORMAL;
  228. }