aprs_decoder.c 5.3 KB

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