faac_slh.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include "faac_slh.h"
  2. #include "../blocks/const.h"
  3. #include "../blocks/decoder.h"
  4. #include "../blocks/encoder.h"
  5. #include "../blocks/generic.h"
  6. #include "../blocks/math.h"
  7. #define TAG "SubGhzProtocolFaacSHL"
  8. static const SubGhzBlockConst subghz_protocol_faac_slh_const = {
  9. .te_short = 255,
  10. .te_long = 595,
  11. .te_delta = 100,
  12. .min_count_bit_for_found = 64,
  13. };
  14. struct SubGhzProtocolDecoderFaacSLH {
  15. SubGhzProtocolDecoderBase base;
  16. SubGhzBlockDecoder decoder;
  17. SubGhzBlockGeneric generic;
  18. };
  19. struct SubGhzProtocolEncoderFaacSLH {
  20. SubGhzProtocolEncoderBase base;
  21. SubGhzProtocolBlockEncoder encoder;
  22. SubGhzBlockGeneric generic;
  23. };
  24. typedef enum {
  25. FaacSLHDecoderStepReset = 0,
  26. FaacSLHDecoderStepFoundPreambula,
  27. FaacSLHDecoderStepSaveDuration,
  28. FaacSLHDecoderStepCheckDuration,
  29. } FaacSLHDecoderStep;
  30. const SubGhzProtocolDecoder subghz_protocol_faac_slh_decoder = {
  31. .alloc = subghz_protocol_decoder_faac_slh_alloc,
  32. .free = subghz_protocol_decoder_faac_slh_free,
  33. .feed = subghz_protocol_decoder_faac_slh_feed,
  34. .reset = subghz_protocol_decoder_faac_slh_reset,
  35. .get_hash_data = subghz_protocol_decoder_faac_slh_get_hash_data,
  36. .serialize = subghz_protocol_decoder_faac_slh_serialize,
  37. .deserialize = subghz_protocol_decoder_faac_slh_deserialize,
  38. .get_string = subghz_protocol_decoder_faac_slh_get_string,
  39. };
  40. const SubGhzProtocolEncoder subghz_protocol_faac_slh_encoder = {
  41. .alloc = NULL,
  42. .free = NULL,
  43. .deserialize = NULL,
  44. .stop = NULL,
  45. .yield = NULL,
  46. };
  47. const SubGhzProtocol subghz_protocol_faac_slh = {
  48. .name = SUBGHZ_PROTOCOL_FAAC_SLH_NAME,
  49. .type = SubGhzProtocolTypeDynamic,
  50. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_868 | SubGhzProtocolFlag_AM |
  51. SubGhzProtocolFlag_Decodable,
  52. .decoder = &subghz_protocol_faac_slh_decoder,
  53. .encoder = &subghz_protocol_faac_slh_encoder,
  54. };
  55. void* subghz_protocol_decoder_faac_slh_alloc(SubGhzEnvironment* environment) {
  56. UNUSED(environment);
  57. SubGhzProtocolDecoderFaacSLH* instance = malloc(sizeof(SubGhzProtocolDecoderFaacSLH));
  58. instance->base.protocol = &subghz_protocol_faac_slh;
  59. instance->generic.protocol_name = instance->base.protocol->name;
  60. return instance;
  61. }
  62. void subghz_protocol_decoder_faac_slh_free(void* context) {
  63. furi_assert(context);
  64. SubGhzProtocolDecoderFaacSLH* instance = context;
  65. free(instance);
  66. }
  67. void subghz_protocol_decoder_faac_slh_reset(void* context) {
  68. furi_assert(context);
  69. SubGhzProtocolDecoderFaacSLH* instance = context;
  70. instance->decoder.parser_step = FaacSLHDecoderStepReset;
  71. }
  72. void subghz_protocol_decoder_faac_slh_feed(void* context, bool level, uint32_t duration) {
  73. furi_assert(context);
  74. SubGhzProtocolDecoderFaacSLH* instance = context;
  75. switch(instance->decoder.parser_step) {
  76. case FaacSLHDecoderStepReset:
  77. if((level) && (DURATION_DIFF(duration, subghz_protocol_faac_slh_const.te_long * 2) <
  78. subghz_protocol_faac_slh_const.te_delta * 3)) {
  79. instance->decoder.parser_step = FaacSLHDecoderStepFoundPreambula;
  80. }
  81. break;
  82. case FaacSLHDecoderStepFoundPreambula:
  83. if((!level) && (DURATION_DIFF(duration, subghz_protocol_faac_slh_const.te_long * 2) <
  84. subghz_protocol_faac_slh_const.te_delta * 3)) {
  85. //Found Preambula
  86. instance->decoder.parser_step = FaacSLHDecoderStepSaveDuration;
  87. instance->decoder.decode_data = 0;
  88. instance->decoder.decode_count_bit = 0;
  89. } else {
  90. instance->decoder.parser_step = FaacSLHDecoderStepReset;
  91. }
  92. break;
  93. case FaacSLHDecoderStepSaveDuration:
  94. if(level) {
  95. if(duration >= ((uint32_t)subghz_protocol_faac_slh_const.te_short * 3 +
  96. subghz_protocol_faac_slh_const.te_delta)) {
  97. instance->decoder.parser_step = FaacSLHDecoderStepFoundPreambula;
  98. if(instance->decoder.decode_count_bit ==
  99. subghz_protocol_faac_slh_const.min_count_bit_for_found) {
  100. instance->generic.data = instance->decoder.decode_data;
  101. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  102. if(instance->base.callback)
  103. instance->base.callback(&instance->base, instance->base.context);
  104. }
  105. instance->decoder.decode_data = 0;
  106. instance->decoder.decode_count_bit = 0;
  107. break;
  108. } else {
  109. instance->decoder.te_last = duration;
  110. instance->decoder.parser_step = FaacSLHDecoderStepCheckDuration;
  111. }
  112. } else {
  113. instance->decoder.parser_step = FaacSLHDecoderStepReset;
  114. }
  115. break;
  116. case FaacSLHDecoderStepCheckDuration:
  117. if(!level) {
  118. if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_faac_slh_const.te_short) <
  119. subghz_protocol_faac_slh_const.te_delta) &&
  120. (DURATION_DIFF(duration, subghz_protocol_faac_slh_const.te_long) <
  121. subghz_protocol_faac_slh_const.te_delta)) {
  122. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  123. instance->decoder.parser_step = FaacSLHDecoderStepSaveDuration;
  124. } else if(
  125. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_faac_slh_const.te_long) <
  126. subghz_protocol_faac_slh_const.te_delta) &&
  127. (DURATION_DIFF(duration, subghz_protocol_faac_slh_const.te_short) <
  128. subghz_protocol_faac_slh_const.te_delta)) {
  129. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  130. instance->decoder.parser_step = FaacSLHDecoderStepSaveDuration;
  131. } else {
  132. instance->decoder.parser_step = FaacSLHDecoderStepReset;
  133. }
  134. } else {
  135. instance->decoder.parser_step = FaacSLHDecoderStepReset;
  136. }
  137. break;
  138. }
  139. }
  140. /**
  141. * Analysis of received data
  142. * @param instance Pointer to a SubGhzBlockGeneric* instance
  143. */
  144. static void subghz_protocol_faac_slh_check_remote_controller(SubGhzBlockGeneric* instance) {
  145. uint64_t code_found_reverse =
  146. subghz_protocol_blocks_reverse_key(instance->data, instance->data_count_bit);
  147. uint32_t code_fix = code_found_reverse & 0xFFFFFFFF;
  148. instance->serial = code_fix & 0xFFFFFFF;
  149. instance->btn = (code_fix >> 28) & 0x0F;
  150. }
  151. uint8_t subghz_protocol_decoder_faac_slh_get_hash_data(void* context) {
  152. furi_assert(context);
  153. SubGhzProtocolDecoderFaacSLH* instance = context;
  154. return subghz_protocol_blocks_get_hash_data(
  155. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  156. }
  157. bool subghz_protocol_decoder_faac_slh_serialize(
  158. void* context,
  159. FlipperFormat* flipper_format,
  160. SubGhzRadioPreset* preset) {
  161. furi_assert(context);
  162. SubGhzProtocolDecoderFaacSLH* instance = context;
  163. return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  164. }
  165. bool subghz_protocol_decoder_faac_slh_deserialize(void* context, FlipperFormat* flipper_format) {
  166. furi_assert(context);
  167. SubGhzProtocolDecoderFaacSLH* instance = context;
  168. bool ret = false;
  169. do {
  170. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  171. break;
  172. }
  173. if(instance->generic.data_count_bit !=
  174. subghz_protocol_faac_slh_const.min_count_bit_for_found) {
  175. FURI_LOG_E(TAG, "Wrong number of bits in key");
  176. break;
  177. }
  178. ret = true;
  179. } while(false);
  180. return ret;
  181. }
  182. void subghz_protocol_decoder_faac_slh_get_string(void* context, FuriString* output) {
  183. furi_assert(context);
  184. SubGhzProtocolDecoderFaacSLH* instance = context;
  185. subghz_protocol_faac_slh_check_remote_controller(&instance->generic);
  186. uint64_t code_found_reverse = subghz_protocol_blocks_reverse_key(
  187. instance->generic.data, instance->generic.data_count_bit);
  188. uint32_t code_fix = code_found_reverse & 0xFFFFFFFF;
  189. uint32_t code_hop = (code_found_reverse >> 32) & 0xFFFFFFFF;
  190. furi_string_cat_printf(
  191. output,
  192. "%s %dbit\r\n"
  193. "Key:%lX%08lX\r\n"
  194. "Fix:%08lX \r\n"
  195. "Hop:%08lX \r\n"
  196. "Sn:%07lX Btn:%X\r\n",
  197. instance->generic.protocol_name,
  198. instance->generic.data_count_bit,
  199. (uint32_t)(instance->generic.data >> 32),
  200. (uint32_t)instance->generic.data,
  201. code_fix,
  202. code_hop,
  203. instance->generic.serial,
  204. instance->generic.btn);
  205. }