irda_common_decoder.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #include "furi/check.h"
  2. #include "furi/common_defines.h"
  3. #include "irda.h"
  4. #include "irda_common_i.h"
  5. #include <stdbool.h>
  6. #include <furi.h>
  7. #include "irda_i.h"
  8. #include <stdint.h>
  9. static void irda_common_decoder_reset_state(IrdaCommonDecoder* decoder);
  10. static inline size_t consume_samples(uint32_t* array, size_t len, size_t shift) {
  11. furi_assert(len >= shift);
  12. len -= shift;
  13. for (int i = 0; i < len; ++i)
  14. array[i] = array[i + shift];
  15. return len;
  16. }
  17. static inline void accumulate_lsb(IrdaCommonDecoder* decoder, bool bit) {
  18. uint16_t index = decoder->databit_cnt / 8;
  19. uint8_t shift = decoder->databit_cnt % 8; // LSB first
  20. if (!shift)
  21. decoder->data[index] = 0;
  22. if (bit) {
  23. decoder->data[index] |= (0x1 << shift); // add 1
  24. } else {
  25. (void) decoder->data[index]; // add 0
  26. }
  27. ++decoder->databit_cnt;
  28. }
  29. static bool irda_check_preamble(IrdaCommonDecoder* decoder) {
  30. furi_assert(decoder);
  31. bool result = false;
  32. bool start_level = (decoder->level + decoder->timings_cnt + 1) % 2;
  33. if (decoder->timings_cnt == 0)
  34. return false;
  35. // align to start at Mark timing
  36. if (!start_level) {
  37. decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
  38. }
  39. if (decoder->protocol->timings.preamble_mark == 0) {
  40. return true;
  41. }
  42. while ((!result) && (decoder->timings_cnt >= 2)) {
  43. float preamble_tolerance = decoder->protocol->timings.preamble_tolerance;
  44. uint16_t preamble_mark = decoder->protocol->timings.preamble_mark;
  45. uint16_t preamble_space = decoder->protocol->timings.preamble_space;
  46. if ((MATCH_TIMING(decoder->timings[0], preamble_mark, preamble_tolerance))
  47. && (MATCH_TIMING(decoder->timings[1], preamble_space, preamble_tolerance))) {
  48. result = true;
  49. }
  50. decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 2);
  51. }
  52. return result;
  53. }
  54. /**
  55. * decoder->protocol->databit_len[0] contains biggest amount of bits, for this protocol.
  56. * decoder->protocol->databit_len[1...] contains lesser values, but which can be decoded
  57. * for some protocol modifications.
  58. */
  59. static IrdaStatus irda_common_decode_bits(IrdaCommonDecoder* decoder) {
  60. furi_assert(decoder);
  61. IrdaStatus status = IrdaStatusOk;
  62. const IrdaTimings* timings = &decoder->protocol->timings;
  63. while (decoder->timings_cnt && (status == IrdaStatusOk)) {
  64. bool level = (decoder->level + decoder->timings_cnt + 1) % 2;
  65. uint32_t timing = decoder->timings[0];
  66. if (timings->min_split_time && !level) {
  67. if (timing > timings->min_split_time) {
  68. /* long low timing - check if we're ready for any of protocol modification */
  69. for (int i = 0; decoder->protocol->databit_len[i] && (i < COUNT_OF(decoder->protocol->databit_len)); ++i) {
  70. if (decoder->protocol->databit_len[i] == decoder->databit_cnt) {
  71. return IrdaStatusReady;
  72. }
  73. }
  74. } else if (decoder->protocol->databit_len[0] == decoder->databit_cnt) {
  75. /* short low timing for longest protocol - this is signal is longer than we expected */
  76. return IrdaStatusError;
  77. }
  78. }
  79. status = decoder->protocol->decode(decoder, level, timing);
  80. furi_check(decoder->databit_cnt <= decoder->protocol->databit_len[0]);
  81. furi_assert(status == IrdaStatusError || status == IrdaStatusOk);
  82. if (status == IrdaStatusError) {
  83. break;
  84. }
  85. decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
  86. /* check if largest protocol version can be decoded */
  87. if (level && (decoder->protocol->databit_len[0] == decoder->databit_cnt) && !timings->min_split_time) {
  88. status = IrdaStatusReady;
  89. break;
  90. }
  91. }
  92. return status;
  93. }
  94. /* Pulse Distance-Width Modulation */
  95. IrdaStatus irda_common_decode_pdwm(IrdaCommonDecoder* decoder, bool level, uint32_t timing) {
  96. furi_assert(decoder);
  97. IrdaStatus status = IrdaStatusOk;
  98. uint32_t bit_tolerance = decoder->protocol->timings.bit_tolerance;
  99. uint16_t bit1_mark = decoder->protocol->timings.bit1_mark;
  100. uint16_t bit1_space = decoder->protocol->timings.bit1_space;
  101. uint16_t bit0_mark = decoder->protocol->timings.bit0_mark;
  102. uint16_t bit0_space = decoder->protocol->timings.bit0_space;
  103. bool analyze_timing = level ^ (bit1_mark == bit0_mark);
  104. uint16_t bit1 = level ? bit1_mark : bit1_space;
  105. uint16_t bit0 = level ? bit0_mark : bit0_space;
  106. uint16_t no_info_timing = (bit1_mark == bit0_mark) ? bit1_mark : bit1_space;
  107. if (analyze_timing) {
  108. if (MATCH_TIMING(timing, bit1, bit_tolerance)) {
  109. accumulate_lsb(decoder, 1);
  110. } else if (MATCH_TIMING(timing, bit0, bit_tolerance)) {
  111. accumulate_lsb(decoder, 0);
  112. } else {
  113. status = IrdaStatusError;
  114. }
  115. } else {
  116. if (!MATCH_TIMING(timing, no_info_timing, bit_tolerance)) {
  117. status = IrdaStatusError;
  118. }
  119. }
  120. return status;
  121. }
  122. /* level switch detection goes in middle of time-quant */
  123. IrdaStatus irda_common_decode_manchester(IrdaCommonDecoder* decoder, bool level, uint32_t timing) {
  124. furi_assert(decoder);
  125. uint16_t bit = decoder->protocol->timings.bit1_mark;
  126. uint16_t tolerance = decoder->protocol->timings.bit_tolerance;
  127. bool* switch_detect = &decoder->switch_detect;
  128. furi_assert((*switch_detect == true) || (*switch_detect == false));
  129. bool single_timing = MATCH_TIMING(timing, bit, tolerance);
  130. bool double_timing = MATCH_TIMING(timing, 2*bit, tolerance);
  131. if(!single_timing && !double_timing) {
  132. return IrdaStatusError;
  133. }
  134. if (decoder->protocol->manchester_start_from_space && (decoder->databit_cnt == 0)) {
  135. *switch_detect = 1; /* fake as we were previously in the middle of time-quant */
  136. accumulate_lsb(decoder, 0);
  137. }
  138. if (*switch_detect == 0) {
  139. if (double_timing) {
  140. return IrdaStatusError;
  141. }
  142. /* only single timing - level switch required in the middle of time-quant */
  143. *switch_detect = 1;
  144. } else {
  145. /* double timing means we're in the middle of time-quant again */
  146. if (single_timing)
  147. *switch_detect = 0;
  148. }
  149. if (*switch_detect) {
  150. if (decoder->protocol->databit_len[0] == decoder->databit_cnt) {
  151. return IrdaStatusError;
  152. }
  153. accumulate_lsb(decoder, level);
  154. }
  155. return IrdaStatusOk;
  156. }
  157. IrdaMessage* irda_common_decoder_check_ready(IrdaCommonDecoder* decoder) {
  158. IrdaMessage* message = NULL;
  159. bool found_length = false;
  160. for (int i = 0; decoder->protocol->databit_len[i] && (i < COUNT_OF(decoder->protocol->databit_len)); ++i) {
  161. if (decoder->protocol->databit_len[i] == decoder->databit_cnt) {
  162. found_length = true;
  163. break;
  164. }
  165. }
  166. if (found_length && decoder->protocol->interpret(decoder)) {
  167. decoder->databit_cnt = 0;
  168. message = &decoder->message;
  169. if (decoder->protocol->decode_repeat) {
  170. decoder->state = IrdaCommonDecoderStateProcessRepeat;
  171. } else {
  172. decoder->state = IrdaCommonDecoderStateWaitPreamble;
  173. }
  174. }
  175. return message;
  176. }
  177. IrdaMessage* irda_common_decode(IrdaCommonDecoder* decoder, bool level, uint32_t duration) {
  178. furi_assert(decoder);
  179. IrdaMessage* message = 0;
  180. IrdaStatus status = IrdaStatusError;
  181. if (decoder->level == level) {
  182. irda_common_decoder_reset(decoder);
  183. }
  184. decoder->level = level; // start with low level (Space timing)
  185. decoder->timings[decoder->timings_cnt] = duration;
  186. decoder->timings_cnt++;
  187. furi_check(decoder->timings_cnt <= sizeof(decoder->timings));
  188. while(1) {
  189. switch (decoder->state) {
  190. case IrdaCommonDecoderStateWaitPreamble:
  191. if (irda_check_preamble(decoder)) {
  192. decoder->state = IrdaCommonDecoderStateDecode;
  193. decoder->databit_cnt = 0;
  194. decoder->switch_detect = false;
  195. continue;
  196. }
  197. break;
  198. case IrdaCommonDecoderStateDecode:
  199. status = irda_common_decode_bits(decoder);
  200. if (status == IrdaStatusReady) {
  201. message = irda_common_decoder_check_ready(decoder);
  202. if (message) {
  203. continue;
  204. } else if (decoder->protocol->databit_len[0] == decoder->databit_cnt) {
  205. /* error: can't decode largest protocol - begin decoding from start */
  206. decoder->state = IrdaCommonDecoderStateWaitPreamble;
  207. }
  208. } else if (status == IrdaStatusError) {
  209. irda_common_decoder_reset_state(decoder);
  210. continue;
  211. }
  212. break;
  213. case IrdaCommonDecoderStateProcessRepeat:
  214. status = decoder->protocol->decode_repeat(decoder);
  215. if (status == IrdaStatusError) {
  216. irda_common_decoder_reset_state(decoder);
  217. continue;
  218. } else if (status == IrdaStatusReady) {
  219. decoder->message.repeat = true;
  220. message = &decoder->message;
  221. }
  222. break;
  223. }
  224. break;
  225. }
  226. return message;
  227. }
  228. void* irda_common_decoder_alloc(const IrdaCommonProtocolSpec* protocol) {
  229. furi_assert(protocol);
  230. /* protocol->databit_len[0] has to contain biggest value of bits that can be decoded */
  231. for (int i = 1; i < COUNT_OF(protocol->databit_len); ++i) {
  232. furi_assert(protocol->databit_len[i] <= protocol->databit_len[0]);
  233. }
  234. uint32_t alloc_size = sizeof(IrdaCommonDecoder)
  235. + protocol->databit_len[0] / 8
  236. + !!(protocol->databit_len[0] % 8);
  237. IrdaCommonDecoder* decoder = furi_alloc(alloc_size);
  238. decoder->protocol = protocol;
  239. decoder->level = true;
  240. return decoder;
  241. }
  242. void irda_common_decoder_free(IrdaCommonDecoder* decoder) {
  243. furi_assert(decoder);
  244. free(decoder);
  245. }
  246. void irda_common_decoder_reset_state(IrdaCommonDecoder* decoder) {
  247. decoder->state = IrdaCommonDecoderStateWaitPreamble;
  248. decoder->databit_cnt = 0;
  249. decoder->switch_detect = false;
  250. decoder->message.protocol = IrdaProtocolUnknown;
  251. if (decoder->protocol->timings.preamble_mark == 0) {
  252. if (decoder->timings_cnt > 0) {
  253. decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
  254. }
  255. }
  256. }
  257. void irda_common_decoder_reset(IrdaCommonDecoder* decoder) {
  258. furi_assert(decoder);
  259. irda_common_decoder_reset_state(decoder);
  260. decoder->timings_cnt = 0;
  261. }