irda_common_decoder.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. /* check if short protocol version can be decoded */
  67. if (timings->min_split_time && !level && (timing > timings->min_split_time)) {
  68. for (int i = 1; decoder->protocol->databit_len[i] && (i < COUNT_OF(decoder->protocol->databit_len)); ++i) {
  69. if (decoder->protocol->databit_len[i] == decoder->databit_cnt) {
  70. return IrdaStatusReady;
  71. }
  72. }
  73. }
  74. status = decoder->protocol->decode(decoder, level, timing);
  75. furi_assert(status == IrdaStatusError || status == IrdaStatusOk);
  76. if (status == IrdaStatusError) {
  77. break;
  78. }
  79. decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
  80. /* check if largest protocol version can be decoded */
  81. if (level && (decoder->protocol->databit_len[0] == decoder->databit_cnt)) {
  82. status = IrdaStatusReady;
  83. break;
  84. }
  85. }
  86. return status;
  87. }
  88. /* Pulse Distance-Width Modulation */
  89. IrdaStatus irda_common_decode_pdwm(IrdaCommonDecoder* decoder, bool level, uint32_t timing) {
  90. furi_assert(decoder);
  91. IrdaStatus status = IrdaStatusOk;
  92. uint32_t bit_tolerance = decoder->protocol->timings.bit_tolerance;
  93. uint16_t bit1_mark = decoder->protocol->timings.bit1_mark;
  94. uint16_t bit1_space = decoder->protocol->timings.bit1_space;
  95. uint16_t bit0_mark = decoder->protocol->timings.bit0_mark;
  96. uint16_t bit0_space = decoder->protocol->timings.bit0_space;
  97. bool analyze_timing = level ^ (bit1_mark == bit0_mark);
  98. uint16_t bit1 = level ? bit1_mark : bit1_space;
  99. uint16_t bit0 = level ? bit0_mark : bit0_space;
  100. uint16_t no_info_timing = (bit1_mark == bit0_mark) ? bit1_mark : bit1_space;
  101. if (analyze_timing) {
  102. if (MATCH_TIMING(timing, bit1, bit_tolerance)) {
  103. accumulate_lsb(decoder, 1);
  104. } else if (MATCH_TIMING(timing, bit0, bit_tolerance)) {
  105. accumulate_lsb(decoder, 0);
  106. } else {
  107. status = IrdaStatusError;
  108. }
  109. } else {
  110. if (!MATCH_TIMING(timing, no_info_timing, bit_tolerance)) {
  111. status = IrdaStatusError;
  112. }
  113. }
  114. return status;
  115. }
  116. /* level switch detection goes in middle of time-quant */
  117. IrdaStatus irda_common_decode_manchester(IrdaCommonDecoder* decoder, bool level, uint32_t timing) {
  118. furi_assert(decoder);
  119. uint16_t bit = decoder->protocol->timings.bit1_mark;
  120. uint16_t tolerance = decoder->protocol->timings.bit_tolerance;
  121. bool* switch_detect = &decoder->switch_detect;
  122. furi_assert((*switch_detect == true) || (*switch_detect == false));
  123. bool single_timing = MATCH_TIMING(timing, bit, tolerance);
  124. bool double_timing = MATCH_TIMING(timing, 2*bit, tolerance);
  125. if(!single_timing && !double_timing) {
  126. return IrdaStatusError;
  127. }
  128. if (decoder->protocol->manchester_start_from_space && (decoder->databit_cnt == 0)) {
  129. *switch_detect = 1; /* fake as we were previously in the middle of time-quant */
  130. accumulate_lsb(decoder, 0);
  131. }
  132. if (*switch_detect == 0) {
  133. if (double_timing) {
  134. return IrdaStatusError;
  135. }
  136. /* only single timing - level switch required in the middle of time-quant */
  137. *switch_detect = 1;
  138. } else {
  139. /* double timing means we're in the middle of time-quant again */
  140. if (single_timing)
  141. *switch_detect = 0;
  142. }
  143. if (*switch_detect) {
  144. accumulate_lsb(decoder, level);
  145. }
  146. return IrdaStatusOk;
  147. }
  148. IrdaMessage* irda_common_decoder_check_ready(IrdaCommonDecoder* decoder) {
  149. IrdaMessage* message = NULL;
  150. if (decoder->protocol->interpret(decoder)) {
  151. decoder->databit_cnt = 0;
  152. message = &decoder->message;
  153. if (decoder->protocol->decode_repeat) {
  154. decoder->state = IrdaCommonDecoderStateProcessRepeat;
  155. } else {
  156. decoder->state = IrdaCommonDecoderStateWaitPreamble;
  157. }
  158. }
  159. return message;
  160. }
  161. IrdaMessage* irda_common_decode(IrdaCommonDecoder* decoder, bool level, uint32_t duration) {
  162. furi_assert(decoder);
  163. IrdaMessage* message = 0;
  164. IrdaStatus status = IrdaStatusError;
  165. if (decoder->level == level) {
  166. irda_common_decoder_reset(decoder);
  167. }
  168. decoder->level = level; // start with low level (Space timing)
  169. decoder->timings[decoder->timings_cnt] = duration;
  170. decoder->timings_cnt++;
  171. furi_check(decoder->timings_cnt <= sizeof(decoder->timings));
  172. while(1) {
  173. switch (decoder->state) {
  174. case IrdaCommonDecoderStateWaitPreamble:
  175. if (irda_check_preamble(decoder)) {
  176. decoder->state = IrdaCommonDecoderStateDecode;
  177. decoder->databit_cnt = 0;
  178. decoder->switch_detect = false;
  179. continue;
  180. }
  181. break;
  182. case IrdaCommonDecoderStateDecode:
  183. status = irda_common_decode_bits(decoder);
  184. if (status == IrdaStatusReady) {
  185. message = irda_common_decoder_check_ready(decoder);
  186. if (message) {
  187. continue;
  188. } else if (decoder->protocol->databit_len[0] == decoder->databit_cnt) {
  189. /* error: can't decode largest protocol - begin decoding from start */
  190. decoder->state = IrdaCommonDecoderStateWaitPreamble;
  191. }
  192. } else if (status == IrdaStatusError) {
  193. irda_common_decoder_reset_state(decoder);
  194. continue;
  195. }
  196. break;
  197. case IrdaCommonDecoderStateProcessRepeat:
  198. status = decoder->protocol->decode_repeat(decoder);
  199. if (status == IrdaStatusError) {
  200. irda_common_decoder_reset_state(decoder);
  201. continue;
  202. } else if (status == IrdaStatusReady) {
  203. decoder->message.repeat = true;
  204. message = &decoder->message;
  205. }
  206. break;
  207. }
  208. break;
  209. }
  210. return message;
  211. }
  212. void* irda_common_decoder_alloc(const IrdaCommonProtocolSpec* protocol) {
  213. furi_assert(protocol);
  214. /* protocol->databit_len[0] has to contain biggest value of bits that can be decoded */
  215. for (int i = 1; i < COUNT_OF(protocol->databit_len); ++i) {
  216. furi_assert(protocol->databit_len[i] <= protocol->databit_len[0]);
  217. }
  218. uint32_t alloc_size = sizeof(IrdaCommonDecoder)
  219. + protocol->databit_len[0] / 8
  220. + !!(protocol->databit_len[0] % 8);
  221. IrdaCommonDecoder* decoder = furi_alloc(alloc_size);
  222. memset(decoder, 0, alloc_size);
  223. decoder->protocol = protocol;
  224. decoder->level = true;
  225. return decoder;
  226. }
  227. void irda_common_decoder_free(IrdaCommonDecoder* decoder) {
  228. furi_assert(decoder);
  229. free(decoder);
  230. }
  231. void irda_common_decoder_reset_state(IrdaCommonDecoder* decoder) {
  232. decoder->state = IrdaCommonDecoderStateWaitPreamble;
  233. decoder->databit_cnt = 0;
  234. decoder->switch_detect = false;
  235. decoder->message.protocol = IrdaProtocolUnknown;
  236. if (decoder->protocol->timings.preamble_mark == 0) {
  237. if (decoder->timings_cnt > 0) {
  238. decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
  239. }
  240. }
  241. }
  242. void irda_common_decoder_reset(IrdaCommonDecoder* decoder) {
  243. furi_assert(decoder);
  244. irda_common_decoder_reset_state(decoder);
  245. decoder->timings_cnt = 0;
  246. }