firefly.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. #include "firefly.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 "SubGhzProtocolFirefly"
  8. #define DIP_PATTERN "%c%c%c%c%c%c%c%c%c%c"
  9. #define DATA_TO_DIP(dip) \
  10. (dip & 0x0200 ? '1' : '0'), (dip & 0x0100 ? '1' : '0'), (dip & 0x0080 ? '1' : '0'), \
  11. (dip & 0x0040 ? '1' : '0'), (dip & 0x0020 ? '1' : '0'), (dip & 0x0010 ? '1' : '0'), \
  12. (dip & 0x0008 ? '1' : '0'), (dip & 0x0004 ? '1' : '0'), (dip & 0x0002 ? '1' : '0'), \
  13. (dip & 0x0001 ? '1' : '0')
  14. static const SubGhzBlockConst subghz_protocol_firefly_const = {
  15. .te_short = 500,
  16. .te_long = 1500,
  17. .te_delta = 150,
  18. .min_count_bit_for_found = 10,
  19. };
  20. struct SubGhzProtocolDecoderFirefly {
  21. SubGhzProtocolDecoderBase base;
  22. SubGhzBlockDecoder decoder;
  23. SubGhzBlockGeneric generic;
  24. };
  25. struct SubGhzProtocolEncoderFirefly {
  26. SubGhzProtocolEncoderBase base;
  27. SubGhzProtocolBlockEncoder encoder;
  28. SubGhzBlockGeneric generic;
  29. };
  30. typedef enum {
  31. FireflyDecoderStepReset = 0,
  32. FireflyDecoderStepSaveDuration,
  33. FireflyDecoderStepCheckDuration,
  34. } FireflyDecoderStep;
  35. const SubGhzProtocolDecoder subghz_protocol_firefly_decoder = {
  36. .alloc = subghz_protocol_decoder_firefly_alloc,
  37. .free = subghz_protocol_decoder_firefly_free,
  38. .feed = subghz_protocol_decoder_firefly_feed,
  39. .reset = subghz_protocol_decoder_firefly_reset,
  40. .get_hash_data = subghz_protocol_decoder_firefly_get_hash_data,
  41. .serialize = subghz_protocol_decoder_firefly_serialize,
  42. .deserialize = subghz_protocol_decoder_firefly_deserialize,
  43. .get_string = subghz_protocol_decoder_firefly_get_string,
  44. };
  45. const SubGhzProtocolEncoder subghz_protocol_firefly_encoder = {
  46. .alloc = subghz_protocol_encoder_firefly_alloc,
  47. .free = subghz_protocol_encoder_firefly_free,
  48. .deserialize = subghz_protocol_encoder_firefly_deserialize,
  49. .stop = subghz_protocol_encoder_firefly_stop,
  50. .yield = subghz_protocol_encoder_firefly_yield,
  51. };
  52. const SubGhzProtocol subghz_protocol_firefly = {
  53. .name = SUBGHZ_PROTOCOL_FIREFLY_NAME,
  54. .type = SubGhzProtocolTypeStatic,
  55. .flag = SubGhzProtocolFlag_315 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
  56. SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
  57. .decoder = &subghz_protocol_firefly_decoder,
  58. .encoder = &subghz_protocol_firefly_encoder,
  59. };
  60. void* subghz_protocol_encoder_firefly_alloc(SubGhzEnvironment* environment) {
  61. SubGhzProtocolEncoderFirefly* instance = malloc(sizeof(SubGhzProtocolEncoderFirefly));
  62. instance->base.protocol = &subghz_protocol_firefly;
  63. instance->generic.protocol_name = instance->base.protocol->name;
  64. instance->encoder.repeat = 10;
  65. instance->encoder.size_upload = 28; //max 10bit*2 + 2 (start, stop)
  66. instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
  67. instance->encoder.is_runing = false;
  68. return instance;
  69. }
  70. void subghz_protocol_encoder_firefly_free(void* context) {
  71. furi_assert(context);
  72. SubGhzProtocolEncoderFirefly* instance = context;
  73. free(instance->encoder.upload);
  74. free(instance);
  75. }
  76. /**
  77. * Generating an upload from data.
  78. * @param instance Pointer to a SubGhzProtocolEncoderFirefly instance
  79. * @return true On success
  80. */
  81. static bool subghz_protocol_encoder_firefly_get_upload(SubGhzProtocolEncoderFirefly* instance) {
  82. furi_assert(instance);
  83. size_t index = 0;
  84. size_t size_upload = (instance->generic.data_count_bit * 2);
  85. if(size_upload > instance->encoder.size_upload) {
  86. FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
  87. return false;
  88. } else {
  89. instance->encoder.size_upload = size_upload;
  90. }
  91. //Send key data
  92. for(uint8_t i = instance->generic.data_count_bit; i > 1; i--) {
  93. if(bit_read(instance->generic.data, i - 1)) {
  94. //send bit 1
  95. instance->encoder.upload[index++] =
  96. level_duration_make(true, (uint32_t)subghz_protocol_firefly_const.te_short * 3);
  97. instance->encoder.upload[index++] =
  98. level_duration_make(false, (uint32_t)subghz_protocol_firefly_const.te_short);
  99. } else {
  100. //send bit 0
  101. instance->encoder.upload[index++] =
  102. level_duration_make(true, (uint32_t)subghz_protocol_firefly_const.te_short);
  103. instance->encoder.upload[index++] =
  104. level_duration_make(false, (uint32_t)subghz_protocol_firefly_const.te_short * 3);
  105. }
  106. }
  107. //Send end bit
  108. if(bit_read(instance->generic.data, 0)) {
  109. //send bit 1
  110. instance->encoder.upload[index++] =
  111. level_duration_make(true, (uint32_t)subghz_protocol_firefly_const.te_short * 3);
  112. //Send PT_GUARD
  113. instance->encoder.upload[index++] =
  114. level_duration_make(false, (uint32_t)subghz_protocol_firefly_const.te_short * 42);
  115. } else {
  116. //send bit 0
  117. instance->encoder.upload[index++] =
  118. level_duration_make(true, (uint32_t)subghz_protocol_firefly_const.te_short);
  119. //Send PT_GUARD
  120. instance->encoder.upload[index++] =
  121. level_duration_make(false, (uint32_t)subghz_protocol_firefly_const.te_short * 44);
  122. }
  123. return true;
  124. }
  125. bool subghz_protocol_encoder_firefly_deserialize(void* context, FlipperFormat* flipper_format) {
  126. furi_assert(context);
  127. SubGhzProtocolEncoderFirefly* instance = context;
  128. bool res = false;
  129. do {
  130. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  131. FURI_LOG_E(TAG, "Deserialize error");
  132. break;
  133. }
  134. //optional parameter parameter
  135. flipper_format_read_uint32(
  136. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  137. subghz_protocol_encoder_firefly_get_upload(instance);
  138. instance->encoder.is_runing = true;
  139. res = true;
  140. } while(false);
  141. return res;
  142. }
  143. void subghz_protocol_encoder_firefly_stop(void* context) {
  144. SubGhzProtocolEncoderFirefly* instance = context;
  145. instance->encoder.is_runing = false;
  146. }
  147. LevelDuration subghz_protocol_encoder_firefly_yield(void* context) {
  148. SubGhzProtocolEncoderFirefly* instance = context;
  149. if(instance->encoder.repeat == 0 || !instance->encoder.is_runing) {
  150. instance->encoder.is_runing = false;
  151. return level_duration_reset();
  152. }
  153. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  154. if(++instance->encoder.front == instance->encoder.size_upload) {
  155. instance->encoder.repeat--;
  156. instance->encoder.front = 0;
  157. }
  158. return ret;
  159. }
  160. void* subghz_protocol_decoder_firefly_alloc(SubGhzEnvironment* environment) {
  161. SubGhzProtocolDecoderFirefly* instance = malloc(sizeof(SubGhzProtocolDecoderFirefly));
  162. instance->base.protocol = &subghz_protocol_firefly;
  163. instance->generic.protocol_name = instance->base.protocol->name;
  164. return instance;
  165. }
  166. void subghz_protocol_decoder_firefly_free(void* context) {
  167. furi_assert(context);
  168. SubGhzProtocolDecoderFirefly* instance = context;
  169. free(instance);
  170. }
  171. void subghz_protocol_decoder_firefly_reset(void* context) {
  172. furi_assert(context);
  173. SubGhzProtocolDecoderFirefly* instance = context;
  174. instance->decoder.parser_step = FireflyDecoderStepReset;
  175. }
  176. void subghz_protocol_decoder_firefly_feed(void* context, bool level, uint32_t duration) {
  177. furi_assert(context);
  178. SubGhzProtocolDecoderFirefly* instance = context;
  179. switch(instance->decoder.parser_step) {
  180. case FireflyDecoderStepReset:
  181. if((!level) && (DURATION_DIFF(duration, subghz_protocol_firefly_const.te_short * 42) <
  182. subghz_protocol_firefly_const.te_delta * 20)) {
  183. //Found header Firefly
  184. instance->decoder.decode_data = 0;
  185. instance->decoder.decode_count_bit = 0;
  186. instance->decoder.parser_step = FireflyDecoderStepSaveDuration;
  187. }
  188. break;
  189. case FireflyDecoderStepSaveDuration:
  190. if(level) {
  191. instance->decoder.te_last = duration;
  192. instance->decoder.parser_step = FireflyDecoderStepCheckDuration;
  193. } else {
  194. instance->decoder.parser_step = FireflyDecoderStepReset;
  195. }
  196. break;
  197. case FireflyDecoderStepCheckDuration:
  198. if(!level) { //save interval
  199. if(duration >= (subghz_protocol_firefly_const.te_short * 5)) {
  200. instance->decoder.parser_step = FireflyDecoderStepReset;
  201. //checking that the duration matches the guardtime
  202. if((DURATION_DIFF(duration, subghz_protocol_firefly_const.te_short * 42) >
  203. subghz_protocol_firefly_const.te_delta * 20)) {
  204. break;
  205. }
  206. if(DURATION_DIFF(
  207. instance->decoder.te_last, subghz_protocol_firefly_const.te_short) <
  208. subghz_protocol_firefly_const.te_delta) {
  209. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  210. } else if(
  211. DURATION_DIFF(
  212. instance->decoder.te_last, subghz_protocol_firefly_const.te_long) <
  213. subghz_protocol_firefly_const.te_delta) {
  214. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  215. }
  216. if(instance->decoder.decode_count_bit ==
  217. subghz_protocol_firefly_const.min_count_bit_for_found) {
  218. instance->generic.serial = 0x0;
  219. instance->generic.btn = 0x0;
  220. instance->generic.data = instance->decoder.decode_data;
  221. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  222. if(instance->base.callback)
  223. instance->base.callback(&instance->base, instance->base.context);
  224. }
  225. break;
  226. }
  227. if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_firefly_const.te_short) <
  228. subghz_protocol_firefly_const.te_delta) &&
  229. (DURATION_DIFF(duration, subghz_protocol_firefly_const.te_long) <
  230. subghz_protocol_firefly_const.te_delta)) {
  231. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  232. instance->decoder.parser_step = FireflyDecoderStepSaveDuration;
  233. } else if(
  234. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_firefly_const.te_long) <
  235. subghz_protocol_firefly_const.te_delta) &&
  236. (DURATION_DIFF(duration, subghz_protocol_firefly_const.te_short) <
  237. subghz_protocol_firefly_const.te_delta)) {
  238. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  239. instance->decoder.parser_step = FireflyDecoderStepSaveDuration;
  240. } else {
  241. instance->decoder.parser_step = FireflyDecoderStepReset;
  242. }
  243. } else {
  244. instance->decoder.parser_step = FireflyDecoderStepReset;
  245. }
  246. break;
  247. }
  248. }
  249. uint8_t subghz_protocol_decoder_firefly_get_hash_data(void* context) {
  250. furi_assert(context);
  251. SubGhzProtocolDecoderFirefly* instance = context;
  252. return subghz_protocol_blocks_get_hash_data(
  253. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  254. }
  255. bool subghz_protocol_decoder_firefly_serialize(
  256. void* context,
  257. FlipperFormat* flipper_format,
  258. uint32_t frequency,
  259. FuriHalSubGhzPreset preset) {
  260. furi_assert(context);
  261. SubGhzProtocolDecoderFirefly* instance = context;
  262. return subghz_block_generic_serialize(&instance->generic, flipper_format, frequency, preset);
  263. }
  264. bool subghz_protocol_decoder_firefly_deserialize(void* context, FlipperFormat* flipper_format) {
  265. furi_assert(context);
  266. SubGhzProtocolDecoderFirefly* instance = context;
  267. return subghz_block_generic_deserialize(&instance->generic, flipper_format);
  268. }
  269. void subghz_protocol_decoder_firefly_get_string(void* context, string_t output) {
  270. furi_assert(context);
  271. SubGhzProtocolDecoderFirefly* instance = context;
  272. uint32_t code_found_lo = instance->generic.data & 0x00000000ffffffff;
  273. uint64_t code_found_reverse = subghz_protocol_blocks_reverse_key(
  274. instance->generic.data, instance->generic.data_count_bit);
  275. uint32_t code_found_reverse_lo = code_found_reverse & 0x00000000ffffffff;
  276. string_cat_printf(
  277. output,
  278. "%s %dbit\r\n"
  279. "Key:0x%08lX\r\n"
  280. "Yek:0x%08lX\r\n"
  281. "DIP:" DIP_PATTERN "\r\n",
  282. instance->generic.protocol_name,
  283. instance->generic.data_count_bit,
  284. code_found_lo,
  285. code_found_reverse_lo,
  286. DATA_TO_DIP(code_found_lo));
  287. }