aprs_decoder.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * aprs_decoder.c
  3. *
  4. * Created on: Aug 25, 2019
  5. * Author: curiousmuch
  6. */
  7. typedef enum {
  8. NORMAL,
  9. FRAME_DECODED,
  10. ERROR_FCS_MISMATCH,
  11. ERROR_PACKET_FORMAT,
  12. ERROR_BUFFER_OVERFLOW
  13. } decoder_output_t;
  14. typedef enum {
  15. NONE,
  16. BUFFER_OVERFLOW,
  17. BIT_STUFFING_FAILURE,
  18. FCS_MISMATCH,
  19. } decoder_error_t;
  20. typedef enum {
  21. FLAG_SEARCH,
  22. FLAG_FOUND,
  23. FRAME_START,
  24. PACKET_END,
  25. FRAME_END,
  26. ABORT,
  27. } decoder_state_t;
  28. typedef struct {
  29. decoder_state_t decoder_state;
  30. uint8_t *frame_buffer;
  31. uint8_t frame_buffer_index;
  32. uint8_t frame_buffer_len;
  33. uint8_t flag_buffer;
  34. uint8_t flag_buffer_index;
  35. uint8_t byte_buffer;
  36. uint8_t current_nrzi_bit;
  37. uint8_t previous_nrzi_bit;
  38. uint8_t current_bit;
  39. uint8_t one_count;
  40. uint32_t byte_buffer_index;
  41. uint8_t skip_bit_flag;
  42. } decoder_varibles_t;
  43. decoder_varibles_t v;
  44. #define APRS_MAX_FRAME 256
  45. void frame_buffer_init(uint8_t *buf, uint8_t len)
  46. {
  47. v.frame_buffer = buf;
  48. v.frame_buffer_len = len;
  49. }
  50. void aprs_decode_init(void)
  51. {
  52. v.decoder_state = FLAG_SEARCH;
  53. v.frame_buffer_index = 0;
  54. //v.frame_max_len = APRS_MAX_FRAME;
  55. v.flag_buffer = 0;
  56. v.flag_buffer_index = 0;
  57. v.byte_buffer_index = 0;
  58. v.byte_buffer = 0;
  59. v.current_nrzi_bit = 0;
  60. v.previous_nrzi_bit = 0;
  61. v.current_bit = 0;
  62. v.one_count = 0;
  63. v.skip_bit_flag = 0;
  64. }
  65. uint8_t flag_found(void)
  66. {
  67. if (v.flag_buffer == 0x7E)
  68. return 1;
  69. else
  70. return 0;
  71. }
  72. decoder_output_t aprs_decode_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. v.flag_buffer = (v.flag_buffer >> 1) + (v.current_bit*0x80);
  89. switch(v.decoder_state)
  90. {
  91. case FLAG_SEARCH:
  92. if (flag_found())
  93. {
  94. // set state variable
  95. v.decoder_state = FLAG_FOUND;
  96. // re-initialize buffer indexes
  97. v.flag_buffer_index = 0;
  98. v.frame_buffer_index = 0;
  99. }
  100. else
  101. {
  102. v.decoder_state = FLAG_SEARCH;
  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_index = 0;
  120. v.one_count = 0;
  121. for (i=0;i<7;i++)
  122. {
  123. bit = (v.flag_buffer << i) & 0x80; // load bit
  124. // count ones for to remove bit stuffing
  125. if (bit)
  126. v.one_count =+ 1;
  127. else
  128. v.one_count = 0;
  129. // skip bit or store in v.byte_buffer
  130. if (v.skip_bit_flag)
  131. {
  132. v.skip_bit_flag = 0;
  133. // if "0" is not stuffed packet is invalid
  134. if (bit != 0)
  135. v.decoder_state = ABORT;
  136. }
  137. else
  138. {
  139. v.byte_buffer |= bit*(0x8>>i);
  140. v.byte_buffer_index =+ 1;
  141. }
  142. if (v.one_count == 5)
  143. v.skip_bit_flag = 1;
  144. }
  145. // check if byte buffer is full
  146. if (v.byte_buffer_index == 7)
  147. {
  148. v.frame_buffer[v.frame_buffer_index] = v.byte_buffer;
  149. v.byte_buffer = 0;
  150. v.byte_buffer_index = 0;
  151. v.frame_buffer_index += 1;
  152. }
  153. break;
  154. }
  155. }
  156. v.flag_buffer_index =+ 1;
  157. break;
  158. case FRAME_START:
  159. // skip stuffed "0"
  160. if (v.skip_bit_flag == 1)
  161. {
  162. v.skip_bit_flag = 0;
  163. // bit will only not be stuffed properly if the HDLC flag is being received
  164. // indicating the end of the frame or the packet has become corrupted.
  165. if (v.current_bit != 0)
  166. {
  167. v.decoder_state = PACKET_END;
  168. break;
  169. }
  170. }
  171. else
  172. {
  173. // load bit
  174. v.byte_buffer |= v.current_bit*(0x8>>v.byte_buffer_index);
  175. v.byte_buffer_index =+ 1;
  176. // check if byte buffer is full
  177. if (v.byte_buffer_index == 7)
  178. {
  179. v.frame_buffer[v.frame_buffer_index] = v.byte_buffer;
  180. v.byte_buffer = 0;
  181. v.byte_buffer_index = 0;
  182. v.frame_buffer_index += 1;
  183. // check for overflow
  184. if (v.frame_buffer_index == v.frame_buffer_len)
  185. {
  186. v.decoder_state = ABORT;
  187. break;
  188. }
  189. }
  190. }
  191. if(v.one_count == 5)
  192. v.skip_bit_flag = 1;
  193. break;
  194. default:
  195. // we're in trouble.
  196. break;
  197. }
  198. if (v.decoder_state == FRAME_END)
  199. {
  200. // calculate CRC
  201. // re-initialize state machine
  202. return FRAME_DECODED;
  203. }
  204. if (v.decoder_state == ABORT)
  205. {
  206. // re-initialize state machine
  207. aprs_decoder_init();
  208. }
  209. return NORMAL;
  210. }