irda_common_decoder.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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) array[i] = array[i + shift];
  14. return len;
  15. }
  16. static inline void accumulate_lsb(IrdaCommonDecoder* decoder, bool bit) {
  17. uint16_t index = decoder->databit_cnt / 8;
  18. uint8_t shift = decoder->databit_cnt % 8; // LSB first
  19. if(!shift) decoder->data[index] = 0;
  20. if(bit) {
  21. decoder->data[index] |= (0x1 << shift); // add 1
  22. } else {
  23. (void)decoder->data[index]; // add 0
  24. }
  25. ++decoder->databit_cnt;
  26. }
  27. static bool irda_check_preamble(IrdaCommonDecoder* decoder) {
  28. furi_assert(decoder);
  29. bool result = false;
  30. bool start_level = (decoder->level + decoder->timings_cnt + 1) % 2;
  31. if(decoder->timings_cnt == 0) return false;
  32. // align to start at Mark timing
  33. if(!start_level) {
  34. decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
  35. }
  36. if(decoder->protocol->timings.preamble_mark == 0) {
  37. return true;
  38. }
  39. while((!result) && (decoder->timings_cnt >= 2)) {
  40. float preamble_tolerance = decoder->protocol->timings.preamble_tolerance;
  41. uint16_t preamble_mark = decoder->protocol->timings.preamble_mark;
  42. uint16_t preamble_space = decoder->protocol->timings.preamble_space;
  43. if((MATCH_TIMING(decoder->timings[0], preamble_mark, preamble_tolerance)) &&
  44. (MATCH_TIMING(decoder->timings[1], preamble_space, preamble_tolerance))) {
  45. result = true;
  46. }
  47. decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 2);
  48. }
  49. return result;
  50. }
  51. /**
  52. * decoder->protocol->databit_len[0] contains biggest amount of bits, for this protocol.
  53. * decoder->protocol->databit_len[1...] contains lesser values, but which can be decoded
  54. * for some protocol modifications.
  55. */
  56. static IrdaStatus irda_common_decode_bits(IrdaCommonDecoder* decoder) {
  57. furi_assert(decoder);
  58. IrdaStatus status = IrdaStatusOk;
  59. const IrdaTimings* timings = &decoder->protocol->timings;
  60. while(decoder->timings_cnt && (status == IrdaStatusOk)) {
  61. bool level = (decoder->level + decoder->timings_cnt + 1) % 2;
  62. uint32_t timing = decoder->timings[0];
  63. if(timings->min_split_time && !level) {
  64. if(timing > timings->min_split_time) {
  65. /* long low timing - check if we're ready for any of protocol modification */
  66. for(int i = 0; decoder->protocol->databit_len[i] &&
  67. (i < COUNT_OF(decoder->protocol->databit_len));
  68. ++i) {
  69. if(decoder->protocol->databit_len[i] == decoder->databit_cnt) {
  70. return IrdaStatusReady;
  71. }
  72. }
  73. } else if(decoder->protocol->databit_len[0] == decoder->databit_cnt) {
  74. /* short low timing for longest protocol - this is signal is longer than we expected */
  75. return IrdaStatusError;
  76. }
  77. }
  78. status = decoder->protocol->decode(decoder, level, timing);
  79. furi_check(decoder->databit_cnt <= decoder->protocol->databit_len[0]);
  80. furi_assert(status == IrdaStatusError || status == IrdaStatusOk);
  81. if(status == IrdaStatusError) {
  82. break;
  83. }
  84. decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
  85. /* check if largest protocol version can be decoded */
  86. if(level && (decoder->protocol->databit_len[0] == decoder->databit_cnt) &&
  87. !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) *switch_detect = 0;
  147. }
  148. if(*switch_detect) {
  149. if(decoder->protocol->databit_len[0] == decoder->databit_cnt) {
  150. return IrdaStatusError;
  151. }
  152. accumulate_lsb(decoder, level);
  153. }
  154. return IrdaStatusOk;
  155. }
  156. IrdaMessage* irda_common_decoder_check_ready(IrdaCommonDecoder* decoder) {
  157. IrdaMessage* message = NULL;
  158. bool found_length = false;
  159. for(int i = 0;
  160. decoder->protocol->databit_len[i] && (i < COUNT_OF(decoder->protocol->databit_len));
  161. ++i) {
  162. if(decoder->protocol->databit_len[i] == decoder->databit_cnt) {
  163. found_length = true;
  164. break;
  165. }
  166. }
  167. if(found_length && decoder->protocol->interpret(decoder)) {
  168. decoder->databit_cnt = 0;
  169. message = &decoder->message;
  170. if(decoder->protocol->decode_repeat) {
  171. decoder->state = IrdaCommonDecoderStateProcessRepeat;
  172. } else {
  173. decoder->state = IrdaCommonDecoderStateWaitPreamble;
  174. }
  175. }
  176. return message;
  177. }
  178. IrdaMessage* irda_common_decode(IrdaCommonDecoder* decoder, bool level, uint32_t duration) {
  179. furi_assert(decoder);
  180. IrdaMessage* message = 0;
  181. IrdaStatus status = IrdaStatusError;
  182. if(decoder->level == level) {
  183. irda_common_decoder_reset(decoder);
  184. }
  185. decoder->level = level; // start with low level (Space timing)
  186. decoder->timings[decoder->timings_cnt] = duration;
  187. decoder->timings_cnt++;
  188. furi_check(decoder->timings_cnt <= sizeof(decoder->timings));
  189. while(1) {
  190. switch(decoder->state) {
  191. case IrdaCommonDecoderStateWaitPreamble:
  192. if(irda_check_preamble(decoder)) {
  193. decoder->state = IrdaCommonDecoderStateDecode;
  194. decoder->databit_cnt = 0;
  195. decoder->switch_detect = false;
  196. continue;
  197. }
  198. break;
  199. case IrdaCommonDecoderStateDecode:
  200. status = irda_common_decode_bits(decoder);
  201. if(status == IrdaStatusReady) {
  202. message = irda_common_decoder_check_ready(decoder);
  203. if(message) {
  204. continue;
  205. } else if(decoder->protocol->databit_len[0] == decoder->databit_cnt) {
  206. /* error: can't decode largest protocol - begin decoding from start */
  207. decoder->state = IrdaCommonDecoderStateWaitPreamble;
  208. }
  209. } else if(status == IrdaStatusError) {
  210. irda_common_decoder_reset_state(decoder);
  211. continue;
  212. }
  213. break;
  214. case IrdaCommonDecoderStateProcessRepeat:
  215. status = decoder->protocol->decode_repeat(decoder);
  216. if(status == IrdaStatusError) {
  217. irda_common_decoder_reset_state(decoder);
  218. continue;
  219. } else if(status == IrdaStatusReady) {
  220. decoder->message.repeat = true;
  221. message = &decoder->message;
  222. }
  223. break;
  224. }
  225. break;
  226. }
  227. return message;
  228. }
  229. void* irda_common_decoder_alloc(const IrdaCommonProtocolSpec* protocol) {
  230. furi_assert(protocol);
  231. /* protocol->databit_len[0] has to contain biggest value of bits that can be decoded */
  232. for(int i = 1; i < COUNT_OF(protocol->databit_len); ++i) {
  233. furi_assert(protocol->databit_len[i] <= protocol->databit_len[0]);
  234. }
  235. uint32_t alloc_size = sizeof(IrdaCommonDecoder) + protocol->databit_len[0] / 8 +
  236. !!(protocol->databit_len[0] % 8);
  237. IrdaCommonDecoder* decoder = malloc(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. }