irda_common_decoder.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include "furi/check.h"
  2. #include "irda.h"
  3. #include "irda_common_i.h"
  4. #include <stdbool.h>
  5. #include <furi.h>
  6. #include "irda_i.h"
  7. static void irda_common_decoder_reset_state(IrdaCommonDecoder* common_decoder);
  8. static bool irda_check_preamble(IrdaCommonDecoder* decoder) {
  9. furi_assert(decoder);
  10. bool result = false;
  11. bool start_level = (decoder->level + decoder->timings_cnt + 1) % 2;
  12. // align to start at Mark timing
  13. if (!start_level) {
  14. if (decoder->timings_cnt > 0) {
  15. --decoder->timings_cnt;
  16. shift_left_array(decoder->timings, decoder->timings_cnt, 1);
  17. }
  18. }
  19. if (decoder->protocol->timings.preamble_mark == 0) {
  20. return true;
  21. }
  22. while ((!result) && (decoder->timings_cnt >= 2)) {
  23. float preamble_tolerance = decoder->protocol->timings.preamble_tolerance;
  24. uint16_t preamble_mark = decoder->protocol->timings.preamble_mark;
  25. uint16_t preamble_space = decoder->protocol->timings.preamble_space;
  26. if ((MATCH_PREAMBLE_TIMING(decoder->timings[0], preamble_mark, preamble_tolerance))
  27. && (MATCH_PREAMBLE_TIMING(decoder->timings[1], preamble_space, preamble_tolerance))) {
  28. result = true;
  29. }
  30. decoder->timings_cnt -= 2;
  31. shift_left_array(decoder->timings, decoder->timings_cnt, 2);
  32. }
  33. return result;
  34. }
  35. /* Pulse Distance-Width Modulation */
  36. IrdaStatus irda_common_decode_pdwm(IrdaCommonDecoder* decoder) {
  37. furi_assert(decoder);
  38. uint32_t* timings = decoder->timings;
  39. uint16_t index = 0;
  40. uint8_t shift = 0;
  41. IrdaStatus status = IrdaStatusError;
  42. uint32_t bit_tolerance = decoder->protocol->timings.bit_tolerance;
  43. uint16_t bit1_mark = decoder->protocol->timings.bit1_mark;
  44. uint16_t bit1_space = decoder->protocol->timings.bit1_space;
  45. uint16_t bit0_mark = decoder->protocol->timings.bit0_mark;
  46. uint16_t bit0_space = decoder->protocol->timings.bit0_space;
  47. while (1) {
  48. // Stop bit
  49. if ((decoder->databit_cnt == decoder->protocol->databit_len) && (decoder->timings_cnt == 1)) {
  50. if (MATCH_BIT_TIMING(timings[0], bit1_mark, bit_tolerance)) {
  51. decoder->timings_cnt = 0;
  52. status = IrdaStatusReady;
  53. } else {
  54. status = IrdaStatusError;
  55. }
  56. break;
  57. }
  58. if (decoder->timings_cnt >= 2) {
  59. index = decoder->databit_cnt / 8;
  60. shift = decoder->databit_cnt % 8; // LSB first
  61. if (!shift)
  62. decoder->data[index] = 0;
  63. if (MATCH_BIT_TIMING(timings[0], bit1_mark, bit_tolerance)
  64. && MATCH_BIT_TIMING(timings[1], bit1_space, bit_tolerance)) {
  65. decoder->data[index] |= (0x1 << shift); // add 1
  66. } else if (MATCH_BIT_TIMING(timings[0], bit0_mark, bit_tolerance)
  67. && MATCH_BIT_TIMING(timings[1], bit0_space, bit_tolerance)) {
  68. (void) decoder->data[index]; // add 0
  69. } else {
  70. status = IrdaStatusError;
  71. break;
  72. }
  73. ++decoder->databit_cnt;
  74. decoder->timings_cnt -= 2;
  75. shift_left_array(decoder->timings, decoder->timings_cnt, 2);
  76. } else {
  77. status = IrdaStatusOk;
  78. break;
  79. }
  80. }
  81. return status;
  82. }
  83. /* level switch detection goes in middle of time-quant */
  84. IrdaStatus irda_common_decode_manchester(IrdaCommonDecoder* decoder) {
  85. furi_assert(decoder);
  86. IrdaStatus status = IrdaStatusOk;
  87. uint16_t bit = decoder->protocol->timings.bit1_mark;
  88. uint16_t tolerance = decoder->protocol->timings.bit_tolerance;
  89. while (decoder->timings_cnt) {
  90. uint32_t timing = decoder->timings[0];
  91. bool* switch_detect = &decoder->switch_detect;
  92. furi_assert((*switch_detect == true) || (*switch_detect == false));
  93. bool single_timing = MATCH_BIT_TIMING(timing, bit, tolerance);
  94. bool double_timing = MATCH_BIT_TIMING(timing, 2*bit, tolerance);
  95. if(!single_timing && !double_timing) {
  96. status = IrdaStatusError;
  97. break;
  98. }
  99. if ((decoder->protocol->manchester_start_from_space) && (decoder->databit_cnt == 0)) {
  100. *switch_detect = 1; /* fake as we were previously in the middle of time-quant */
  101. decoder->data[0] = 0; /* first captured timing should be Mark */
  102. ++decoder->databit_cnt;
  103. }
  104. if (*switch_detect == 0) {
  105. if (double_timing) {
  106. status = IrdaStatusError;
  107. break;
  108. }
  109. /* only single timing - level switch required in the middle of time-quant */
  110. *switch_detect = 1;
  111. } else {
  112. /* double timing means we in the middle of time-quant again */
  113. if (single_timing)
  114. *switch_detect = 0;
  115. }
  116. --decoder->timings_cnt;
  117. shift_left_array(decoder->timings, decoder->timings_cnt, 1);
  118. status = IrdaStatusOk;
  119. bool level = (decoder->level + decoder->timings_cnt) % 2;
  120. if (decoder->databit_cnt < decoder->protocol->databit_len) {
  121. if (*switch_detect) {
  122. uint8_t index = decoder->databit_cnt / 8;
  123. uint8_t shift = decoder->databit_cnt % 8; // LSB first
  124. if (!shift)
  125. decoder->data[index] = 0;
  126. decoder->data[index] |= (level << shift);
  127. ++decoder->databit_cnt;
  128. }
  129. if (decoder->databit_cnt == decoder->protocol->databit_len) {
  130. if (level) {
  131. status = IrdaStatusReady;
  132. break;
  133. }
  134. }
  135. } else {
  136. furi_assert(level);
  137. /* cover case: sequence should be stopped after last bit was received */
  138. if (single_timing) {
  139. status = IrdaStatusReady;
  140. break;
  141. } else {
  142. status = IrdaStatusError;
  143. }
  144. }
  145. }
  146. return status;
  147. }
  148. IrdaMessage* irda_common_decode(IrdaCommonDecoder* decoder, bool level, uint32_t duration) {
  149. furi_assert(decoder);
  150. IrdaMessage* message = 0;
  151. IrdaStatus status = IrdaStatusError;
  152. if (decoder->level == level) {
  153. decoder->timings_cnt = 0;
  154. }
  155. decoder->level = level; // start with low level (Space timing)
  156. decoder->timings[decoder->timings_cnt] = duration;
  157. decoder->timings_cnt++;
  158. furi_check(decoder->timings_cnt <= sizeof(decoder->timings));
  159. while(1) {
  160. switch (decoder->state) {
  161. case IrdaCommonDecoderStateWaitPreamble:
  162. if (irda_check_preamble(decoder)) {
  163. decoder->state = IrdaCommonDecoderStateDecode;
  164. decoder->databit_cnt = 0;
  165. decoder->switch_detect = false;
  166. continue;
  167. }
  168. break;
  169. case IrdaCommonDecoderStateDecode:
  170. status = decoder->protocol->decode(decoder);
  171. if (status == IrdaStatusReady) {
  172. if (decoder->protocol->interpret(decoder)) {
  173. message = &decoder->message;
  174. decoder->state = IrdaCommonDecoderStateProcessRepeat;
  175. } else {
  176. decoder->state = IrdaCommonDecoderStateWaitPreamble;
  177. }
  178. } else if (status == IrdaStatusError) {
  179. irda_common_decoder_reset_state(decoder);
  180. continue;
  181. }
  182. break;
  183. case IrdaCommonDecoderStateProcessRepeat:
  184. if (!decoder->protocol->decode_repeat) {
  185. decoder->state = IrdaCommonDecoderStateWaitPreamble;
  186. continue;
  187. }
  188. status = decoder->protocol->decode_repeat(decoder);
  189. if (status == IrdaStatusError) {
  190. irda_common_decoder_reset_state(decoder);
  191. continue;
  192. } else if (status == IrdaStatusReady) {
  193. decoder->message.repeat = true;
  194. message = &decoder->message;
  195. }
  196. break;
  197. }
  198. break;
  199. }
  200. return message;
  201. }
  202. void* irda_common_decoder_alloc(const IrdaCommonProtocolSpec* protocol) {
  203. furi_assert(protocol);
  204. uint32_t alloc_size = sizeof(IrdaCommonDecoder)
  205. + protocol->databit_len / 8
  206. + !!(protocol->databit_len % 8);
  207. IrdaCommonDecoder* decoder = furi_alloc(alloc_size);
  208. memset(decoder, 0, alloc_size);
  209. decoder->protocol = protocol;
  210. decoder->level = true;
  211. return decoder;
  212. }
  213. void irda_common_decoder_set_context(void* decoder, void* context) {
  214. IrdaCommonDecoder* common_decoder = decoder;
  215. common_decoder->context = context;
  216. }
  217. void irda_common_decoder_free(void* decoder) {
  218. furi_assert(decoder);
  219. free(decoder);
  220. }
  221. void irda_common_decoder_reset_state(IrdaCommonDecoder* common_decoder) {
  222. common_decoder->state = IrdaCommonDecoderStateWaitPreamble;
  223. common_decoder->databit_cnt = 0;
  224. common_decoder->switch_detect = false;
  225. common_decoder->message.protocol = IrdaProtocolUnknown;
  226. if ((common_decoder->protocol->timings.preamble_mark == 0) && (common_decoder->timings_cnt > 0)) {
  227. --common_decoder->timings_cnt;
  228. shift_left_array(common_decoder->timings, common_decoder->timings_cnt, 1);
  229. }
  230. }
  231. void irda_common_decoder_reset(void* decoder) {
  232. furi_assert(decoder);
  233. IrdaCommonDecoder* common_decoder = decoder;
  234. irda_common_decoder_reset_state(common_decoder);
  235. common_decoder->timings_cnt = 0;
  236. }