receiver.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "receiver.h"
  2. #include "protocols/registry.h"
  3. #include <m-array.h>
  4. typedef struct {
  5. SubGhzProtocolEncoderBase* base;
  6. } SubGhzReceiverSlot;
  7. ARRAY_DEF(SubGhzReceiverSlotArray, SubGhzReceiverSlot, M_POD_OPLIST);
  8. #define M_OPL_SubGhzReceiverSlotArray_t() ARRAY_OPLIST(SubGhzReceiverSlotArray, M_POD_OPLIST)
  9. struct SubGhzReceiver {
  10. SubGhzReceiverSlotArray_t slots;
  11. SubGhzProtocolFlag filter;
  12. SubGhzReceiverCallback callback;
  13. void* context;
  14. };
  15. SubGhzReceiver* subghz_receiver_alloc_init(SubGhzEnvironment* environment) {
  16. SubGhzReceiver* instance = malloc(sizeof(SubGhzReceiver));
  17. SubGhzReceiverSlotArray_init(instance->slots);
  18. for(size_t i = 0; i < subghz_protocol_registry_count(); ++i) {
  19. const SubGhzProtocol* protocol = subghz_protocol_registry_get_by_index(i);
  20. if(protocol->decoder && protocol->decoder->alloc) {
  21. SubGhzReceiverSlot* slot = SubGhzReceiverSlotArray_push_new(instance->slots);
  22. slot->base = protocol->decoder->alloc(environment);
  23. }
  24. }
  25. instance->callback = NULL;
  26. instance->context = NULL;
  27. return instance;
  28. }
  29. void subghz_receiver_free(SubGhzReceiver* instance) {
  30. furi_assert(instance);
  31. instance->callback = NULL;
  32. instance->context = NULL;
  33. // Release allocated slots
  34. for
  35. M_EACH(slot, instance->slots, SubGhzReceiverSlotArray_t) {
  36. slot->base->protocol->decoder->free(slot->base);
  37. slot->base = NULL;
  38. }
  39. SubGhzReceiverSlotArray_clear(instance->slots);
  40. free(instance);
  41. }
  42. void subghz_receiver_decode(SubGhzReceiver* instance, bool level, uint32_t duration) {
  43. furi_assert(instance);
  44. furi_assert(instance->slots);
  45. for
  46. M_EACH(slot, instance->slots, SubGhzReceiverSlotArray_t) {
  47. if((slot->base->protocol->flag & instance->filter) == instance->filter) {
  48. slot->base->protocol->decoder->feed(slot->base, level, duration);
  49. }
  50. }
  51. }
  52. void subghz_receiver_reset(SubGhzReceiver* instance) {
  53. furi_assert(instance);
  54. furi_assert(instance->slots);
  55. for
  56. M_EACH(slot, instance->slots, SubGhzReceiverSlotArray_t) {
  57. slot->base->protocol->decoder->reset(slot->base);
  58. }
  59. }
  60. static void subghz_receiver_rx_callback(SubGhzProtocolDecoderBase* decoder_base, void* context) {
  61. SubGhzReceiver* instance = context;
  62. if(instance->callback) {
  63. instance->callback(instance, decoder_base, instance->context);
  64. }
  65. }
  66. void subghz_receiver_set_rx_callback(
  67. SubGhzReceiver* instance,
  68. SubGhzReceiverCallback callback,
  69. void* context) {
  70. furi_assert(instance);
  71. for
  72. M_EACH(slot, instance->slots, SubGhzReceiverSlotArray_t) {
  73. subghz_protocol_decoder_base_set_decoder_callback(
  74. (SubGhzProtocolDecoderBase*)slot->base, subghz_receiver_rx_callback, instance);
  75. }
  76. instance->callback = callback;
  77. instance->context = context;
  78. }
  79. void subghz_receiver_set_filter(SubGhzReceiver* instance, SubGhzProtocolFlag filter) {
  80. furi_assert(instance);
  81. instance->filter = filter;
  82. }
  83. SubGhzProtocolDecoderBase* subghz_receiver_search_decoder_base_by_name(
  84. SubGhzReceiver* instance,
  85. const char* decoder_name) {
  86. SubGhzProtocolDecoderBase* result = NULL;
  87. for
  88. M_EACH(slot, instance->slots, SubGhzReceiverSlotArray_t) {
  89. if(strcmp(slot->base->protocol->name, decoder_name) == 0) {
  90. result = (SubGhzProtocolDecoderBase*)slot->base;
  91. break;
  92. }
  93. }
  94. return result;
  95. }