irda.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #include "irda.h"
  2. #include "furi/check.h"
  3. #include "irda_common_i.h"
  4. #include "irda_protocol_defs_i.h"
  5. #include <stdbool.h>
  6. #include <stdint.h>
  7. #include <stdlib.h>
  8. #include <furi.h>
  9. #include "irda_i.h"
  10. #include <furi-hal-irda.h>
  11. struct IrdaDecoderHandler {
  12. void** ctx;
  13. };
  14. typedef struct {
  15. IrdaAlloc alloc;
  16. IrdaDecode decode;
  17. IrdaReset reset;
  18. IrdaFree free;
  19. } IrdaDecoders;
  20. typedef struct {
  21. IrdaEncoderReset reset;
  22. IrdaAlloc alloc;
  23. IrdaEncode encode;
  24. IrdaFree free;
  25. } IrdaEncoders;
  26. typedef struct {
  27. IrdaProtocol protocol;
  28. const char* name;
  29. IrdaDecoders decoder;
  30. IrdaEncoders encoder;
  31. uint8_t address_length;
  32. uint8_t command_length;
  33. uint32_t frequency;
  34. float duty_cycle;
  35. } IrdaProtocolImplementation;
  36. struct IrdaEncoderHandler {
  37. void* encoder;
  38. IrdaProtocol protocol;
  39. };
  40. // TODO: replace with key-value, Now we refer by enum index, which is dangerous.
  41. static const IrdaProtocolImplementation irda_protocols[] = {
  42. // #0
  43. { .protocol = IrdaProtocolNEC,
  44. .name = "NEC",
  45. .decoder = {
  46. .alloc = irda_decoder_nec_alloc,
  47. .decode = irda_decoder_nec_decode,
  48. .reset = irda_decoder_nec_reset,
  49. .free = irda_decoder_nec_free},
  50. .encoder = {
  51. .alloc = irda_encoder_nec_alloc,
  52. .encode = irda_encoder_nec_encode,
  53. .reset = irda_encoder_nec_reset,
  54. .free = irda_encoder_nec_free},
  55. .address_length = 2,
  56. .command_length = 2,
  57. .frequency = IRDA_COMMON_CARRIER_FREQUENCY,
  58. .duty_cycle = IRDA_COMMON_DUTY_CYCLE,
  59. },
  60. // #1 - have to be after NEC
  61. { .protocol = IrdaProtocolNECext,
  62. .name = "NECext",
  63. .decoder = {
  64. .alloc = irda_decoder_necext_alloc,
  65. .decode = irda_decoder_nec_decode,
  66. .reset = irda_decoder_nec_reset,
  67. .free = irda_decoder_nec_free},
  68. .encoder = {
  69. .alloc = irda_encoder_necext_alloc,
  70. .encode = irda_encoder_nec_encode,
  71. .reset = irda_encoder_necext_reset,
  72. .free = irda_encoder_nec_free},
  73. .address_length = 4,
  74. .command_length = 2,
  75. .frequency = IRDA_COMMON_CARRIER_FREQUENCY,
  76. .duty_cycle = IRDA_COMMON_DUTY_CYCLE,
  77. },
  78. // #2
  79. { .protocol = IrdaProtocolSamsung32,
  80. .name ="Samsung32",
  81. .decoder = {
  82. .alloc = irda_decoder_samsung32_alloc,
  83. .decode = irda_decoder_samsung32_decode,
  84. .reset = irda_decoder_samsung32_reset,
  85. .free = irda_decoder_samsung32_free},
  86. .encoder = {
  87. .alloc = irda_encoder_samsung32_alloc,
  88. .encode = irda_encoder_samsung32_encode,
  89. .reset = irda_encoder_samsung32_reset,
  90. .free = irda_encoder_samsung32_free},
  91. .address_length = 2,
  92. .command_length = 2,
  93. .frequency = IRDA_COMMON_CARRIER_FREQUENCY,
  94. .duty_cycle = IRDA_COMMON_DUTY_CYCLE,
  95. },
  96. // #3
  97. { .protocol = IrdaProtocolRC6,
  98. .name = "RC6",
  99. .decoder = {
  100. .alloc = irda_decoder_rc6_alloc,
  101. .decode = irda_decoder_rc6_decode,
  102. .reset = irda_decoder_rc6_reset,
  103. .free = irda_decoder_rc6_free},
  104. .encoder = {
  105. .alloc = irda_encoder_rc6_alloc,
  106. .encode = irda_encoder_rc6_encode,
  107. .reset = irda_encoder_rc6_reset,
  108. .free = irda_encoder_rc6_free},
  109. .address_length = 2,
  110. .command_length = 2,
  111. .frequency = IRDA_COMMON_CARRIER_FREQUENCY,
  112. .duty_cycle = IRDA_COMMON_DUTY_CYCLE,
  113. },
  114. };
  115. const IrdaMessage* irda_decode(IrdaDecoderHandler* handler, bool level, uint32_t duration) {
  116. furi_assert(handler);
  117. IrdaMessage* message = NULL;
  118. IrdaMessage* result = NULL;
  119. for (int i = 0; i < COUNT_OF(irda_protocols); ++i) {
  120. if (irda_protocols[i].decoder.decode) {
  121. message = irda_protocols[i].decoder.decode(handler->ctx[i], level, duration);
  122. if (!result && message) {
  123. message->protocol = irda_protocols[i].protocol;
  124. result = message;
  125. }
  126. }
  127. }
  128. return result;
  129. }
  130. IrdaDecoderHandler* irda_alloc_decoder(void) {
  131. IrdaDecoderHandler* handler = furi_alloc(sizeof(IrdaDecoderHandler));
  132. handler->ctx = furi_alloc(sizeof(void*) * COUNT_OF(irda_protocols));
  133. for (int i = 0; i < COUNT_OF(irda_protocols); ++i) {
  134. handler->ctx[i] = 0;
  135. if (irda_protocols[i].decoder.alloc)
  136. handler->ctx[i] = irda_protocols[i].decoder.alloc();
  137. }
  138. return handler;
  139. }
  140. void irda_free_decoder(IrdaDecoderHandler* handler) {
  141. furi_assert(handler);
  142. furi_assert(handler->ctx);
  143. for (int i = 0; i < COUNT_OF(irda_protocols); ++i) {
  144. if (irda_protocols[i].decoder.free)
  145. irda_protocols[i].decoder.free(handler->ctx[i]);
  146. }
  147. free(handler->ctx);
  148. free(handler);
  149. }
  150. void irda_reset_decoder(IrdaDecoderHandler* handler) {
  151. for (int i = 0; i < COUNT_OF(irda_protocols); ++i) {
  152. if (irda_protocols[i].decoder.reset)
  153. irda_protocols[i].decoder.reset(handler->ctx[i]);
  154. }
  155. }
  156. IrdaEncoderHandler* irda_alloc_encoder(void) {
  157. IrdaEncoderHandler* handler = furi_alloc(sizeof(IrdaEncoderHandler));
  158. handler->encoder = NULL;
  159. handler->protocol = IrdaProtocolUnknown;
  160. return handler;
  161. }
  162. void irda_free_encoder(IrdaEncoderHandler* handler) {
  163. furi_assert(handler);
  164. if (handler->encoder) {
  165. furi_assert(irda_is_protocol_valid(handler->protocol));
  166. furi_assert(irda_protocols[handler->protocol].encoder.free);
  167. irda_protocols[handler->protocol].encoder.free(handler->encoder);
  168. }
  169. free(handler);
  170. }
  171. void irda_reset_encoder(IrdaEncoderHandler* handler, const IrdaMessage* message) {
  172. furi_assert(handler);
  173. furi_assert(message);
  174. furi_assert(irda_is_protocol_valid(message->protocol));
  175. furi_assert(irda_protocols[message->protocol].encoder.reset);
  176. furi_assert(irda_protocols[message->protocol].encoder.alloc);
  177. /* Realloc encoder if different protocol set */
  178. if (message->protocol != handler->protocol) {
  179. if (handler->encoder != NULL) {
  180. furi_assert(handler->protocol != IrdaProtocolUnknown);
  181. irda_protocols[handler->protocol].encoder.free(handler->encoder);
  182. }
  183. handler->encoder = irda_protocols[message->protocol].encoder.alloc();
  184. handler->protocol = message->protocol;
  185. }
  186. irda_protocols[handler->protocol].encoder.reset(handler->encoder, message);
  187. }
  188. IrdaStatus irda_encode(IrdaEncoderHandler* handler, uint32_t* duration, bool* level) {
  189. furi_assert(handler);
  190. furi_assert(irda_is_protocol_valid(handler->protocol));
  191. furi_assert(irda_protocols[handler->protocol].encoder.encode);
  192. IrdaStatus status = irda_protocols[handler->protocol].encoder.encode(handler->encoder, duration, level);
  193. furi_assert(status != IrdaStatusError);
  194. return status;
  195. }
  196. bool irda_is_protocol_valid(IrdaProtocol protocol) {
  197. return (protocol >= 0) && (protocol < COUNT_OF(irda_protocols));
  198. }
  199. IrdaProtocol irda_get_protocol_by_name(const char* protocol_name) {
  200. for (int i = 0; i < COUNT_OF(irda_protocols); ++i) {
  201. if (!strcmp(irda_protocols[i].name, protocol_name))
  202. return i;
  203. }
  204. furi_assert(0);
  205. return IrdaProtocolUnknown;
  206. }
  207. const char* irda_get_protocol_name(IrdaProtocol protocol) {
  208. furi_assert(irda_is_protocol_valid(protocol));
  209. if (irda_is_protocol_valid(protocol))
  210. return irda_protocols[protocol].name;
  211. else
  212. return "Invalid";
  213. }
  214. uint8_t irda_get_protocol_address_length(IrdaProtocol protocol) {
  215. furi_assert(irda_is_protocol_valid(protocol));
  216. if (irda_is_protocol_valid(protocol))
  217. return irda_protocols[protocol].address_length;
  218. else
  219. return 0;
  220. }
  221. uint8_t irda_get_protocol_command_length(IrdaProtocol protocol) {
  222. furi_assert(irda_is_protocol_valid(protocol));
  223. if (irda_is_protocol_valid(protocol))
  224. return irda_protocols[protocol].command_length;
  225. else
  226. return 0;
  227. }
  228. uint32_t irda_get_protocol_frequency(IrdaProtocol protocol) {
  229. furi_assert(irda_is_protocol_valid(protocol));
  230. if (irda_is_protocol_valid(protocol))
  231. return irda_protocols[protocol].frequency;
  232. else
  233. return 0;
  234. }
  235. float irda_get_protocol_duty_cycle(IrdaProtocol protocol) {
  236. furi_assert(irda_is_protocol_valid(protocol));
  237. if (irda_is_protocol_valid(protocol))
  238. return irda_protocols[protocol].duty_cycle;
  239. else
  240. return 0;
  241. }