receiver.c 3.7 KB

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