aprs_decoder.c 5.0 KB

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