firefly.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. UNUSED(environment);
  62. SubGhzProtocolEncoderFirefly* instance = malloc(sizeof(SubGhzProtocolEncoderFirefly));
  63. instance->base.protocol = &subghz_protocol_firefly;
  64. instance->generic.protocol_name = instance->base.protocol->name;
  65. instance->encoder.repeat = 10;
  66. instance->encoder.size_upload = 28; //max 10bit*2 + 2 (start, stop)
  67. instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
  68. instance->encoder.is_runing = false;
  69. return instance;
  70. }
  71. void subghz_protocol_encoder_firefly_free(void* context) {
  72. furi_assert(context);
  73. SubGhzProtocolEncoderFirefly* instance = context;
  74. free(instance->encoder.upload);
  75. free(instance);
  76. }
  77. /**
  78. * Generating an upload from data.
  79. * @param instance Pointer to a SubGhzProtocolEncoderFirefly instance
  80. * @return true On success
  81. */
  82. static bool subghz_protocol_encoder_firefly_get_upload(SubGhzProtocolEncoderFirefly* instance) {
  83. furi_assert(instance);
  84. size_t index = 0;
  85. size_t size_upload = (instance->generic.data_count_bit * 2);
  86. if(size_upload > instance->encoder.size_upload) {
  87. FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
  88. return false;
  89. } else {
  90. instance->encoder.size_upload = size_upload;
  91. }
  92. //Send key data
  93. for(uint8_t i = instance->generic.data_count_bit; i > 1; i--) {
  94. if(bit_read(instance->generic.data, i - 1)) {
  95. //send bit 1
  96. instance->encoder.upload[index++] =
  97. level_duration_make(true, (uint32_t)subghz_protocol_firefly_const.te_short * 3);
  98. instance->encoder.upload[index++] =
  99. level_duration_make(false, (uint32_t)subghz_protocol_firefly_const.te_short);
  100. } else {
  101. //send bit 0
  102. instance->encoder.upload[index++] =
  103. level_duration_make(true, (uint32_t)subghz_protocol_firefly_const.te_short);
  104. instance->encoder.upload[index++] =
  105. level_duration_make(false, (uint32_t)subghz_protocol_firefly_const.te_short * 3);
  106. }
  107. }
  108. //Send end bit
  109. if(bit_read(instance->generic.data, 0)) {
  110. //send bit 1
  111. instance->encoder.upload[index++] =
  112. level_duration_make(true, (uint32_t)subghz_protocol_firefly_const.te_short * 3);
  113. //Send PT_GUARD
  114. instance->encoder.upload[index++] =
  115. level_duration_make(false, (uint32_t)subghz_protocol_firefly_const.te_short * 42);
  116. } else {
  117. //send bit 0
  118. instance->encoder.upload[index++] =
  119. level_duration_make(true, (uint32_t)subghz_protocol_firefly_const.te_short);
  120. //Send PT_GUARD
  121. instance->encoder.upload[index++] =
  122. level_duration_make(false, (uint32_t)subghz_protocol_firefly_const.te_short * 44);
  123. }
  124. return true;
  125. }
  126. bool subghz_protocol_encoder_firefly_deserialize(void* context, FlipperFormat* flipper_format) {
  127. furi_assert(context);
  128. SubGhzProtocolEncoderFirefly* instance = context;
  129. bool res = false;
  130. do {
  131. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  132. FURI_LOG_E(TAG, "Deserialize error");
  133. break;
  134. }
  135. //optional parameter parameter
  136. flipper_format_read_uint32(
  137. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  138. subghz_protocol_encoder_firefly_get_upload(instance);
  139. instance->encoder.is_runing = true;
  140. res = true;
  141. } while(false);
  142. return res;
  143. }
  144. void subghz_protocol_encoder_firefly_stop(void* context) {
  145. SubGhzProtocolEncoderFirefly* instance = context;
  146. instance->encoder.is_runing = false;
  147. }
  148. LevelDuration subghz_protocol_encoder_firefly_yield(void* context) {
  149. SubGhzProtocolEncoderFirefly* instance = context;
  150. if(instance->encoder.repeat == 0 || !instance->encoder.is_runing) {
  151. instance->encoder.is_runing = false;
  152. return level_duration_reset();
  153. }
  154. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  155. if(++instance->encoder.front == instance->encoder.size_upload) {
  156. instance->encoder.repeat--;
  157. instance->encoder.front = 0;
  158. }
  159. return ret;
  160. }
  161. void* subghz_protocol_decoder_firefly_alloc(SubGhzEnvironment* environment) {
  162. UNUSED(environment);
  163. SubGhzProtocolDecoderFirefly* instance = malloc(sizeof(SubGhzProtocolDecoderFirefly));
  164. instance->base.protocol = &subghz_protocol_firefly;
  165. instance->generic.protocol_name = instance->base.protocol->name;
  166. return instance;
  167. }
  168. void subghz_protocol_decoder_firefly_free(void* context) {
  169. furi_assert(context);
  170. SubGhzProtocolDecoderFirefly* instance = context;
  171. free(instance);
  172. }
  173. void subghz_protocol_decoder_firefly_reset(void* context) {
  174. furi_assert(context);
  175. SubGhzProtocolDecoderFirefly* instance = context;
  176. instance->decoder.parser_step = FireflyDecoderStepReset;
  177. }
  178. void subghz_protocol_decoder_firefly_feed(void* context, bool level, uint32_t duration) {
  179. furi_assert(context);
  180. SubGhzProtocolDecoderFirefly* instance = context;
  181. switch(instance->decoder.parser_step) {
  182. case FireflyDecoderStepReset:
  183. if((!level) && (DURATION_DIFF(duration, subghz_protocol_firefly_const.te_short * 42) <
  184. subghz_protocol_firefly_const.te_delta * 20)) {
  185. //Found header Firefly
  186. instance->decoder.decode_data = 0;
  187. instance->decoder.decode_count_bit = 0;
  188. instance->decoder.parser_step = FireflyDecoderStepSaveDuration;
  189. }
  190. break;
  191. case FireflyDecoderStepSaveDuration:
  192. if(level) {
  193. instance->decoder.te_last = duration;
  194. instance->decoder.parser_step = FireflyDecoderStepCheckDuration;
  195. } else {
  196. instance->decoder.parser_step = FireflyDecoderStepReset;
  197. }
  198. break;
  199. case FireflyDecoderStepCheckDuration:
  200. if(!level) { //save interval
  201. if(duration >= (subghz_protocol_firefly_const.te_short * 5)) {
  202. instance->decoder.parser_step = FireflyDecoderStepReset;
  203. //checking that the duration matches the guardtime
  204. if((DURATION_DIFF(duration, subghz_protocol_firefly_const.te_short * 42) >
  205. subghz_protocol_firefly_const.te_delta * 20)) {
  206. break;
  207. }
  208. if(DURATION_DIFF(
  209. instance->decoder.te_last, subghz_protocol_firefly_const.te_short) <
  210. subghz_protocol_firefly_const.te_delta) {
  211. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  212. } else if(
  213. DURATION_DIFF(
  214. instance->decoder.te_last, subghz_protocol_firefly_const.te_long) <
  215. subghz_protocol_firefly_const.te_delta) {
  216. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  217. }
  218. if(instance->decoder.decode_count_bit ==
  219. subghz_protocol_firefly_const.min_count_bit_for_found) {
  220. instance->generic.serial = 0x0;
  221. instance->generic.btn = 0x0;
  222. instance->generic.data = instance->decoder.decode_data;
  223. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  224. if(instance->base.callback)
  225. instance->base.callback(&instance->base, instance->base.context);
  226. }
  227. break;
  228. }
  229. if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_firefly_const.te_short) <
  230. subghz_protocol_firefly_const.te_delta) &&
  231. (DURATION_DIFF(duration, subghz_protocol_firefly_const.te_long) <
  232. subghz_protocol_firefly_const.te_delta)) {
  233. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  234. instance->decoder.parser_step = FireflyDecoderStepSaveDuration;
  235. } else if(
  236. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_firefly_const.te_long) <
  237. subghz_protocol_firefly_const.te_delta) &&
  238. (DURATION_DIFF(duration, subghz_protocol_firefly_const.te_short) <
  239. subghz_protocol_firefly_const.te_delta)) {
  240. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  241. instance->decoder.parser_step = FireflyDecoderStepSaveDuration;
  242. } else {
  243. instance->decoder.parser_step = FireflyDecoderStepReset;
  244. }
  245. } else {
  246. instance->decoder.parser_step = FireflyDecoderStepReset;
  247. }
  248. break;
  249. }
  250. }
  251. uint8_t subghz_protocol_decoder_firefly_get_hash_data(void* context) {
  252. furi_assert(context);
  253. SubGhzProtocolDecoderFirefly* instance = context;
  254. return subghz_protocol_blocks_get_hash_data(
  255. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  256. }
  257. bool subghz_protocol_decoder_firefly_serialize(
  258. void* context,
  259. FlipperFormat* flipper_format,
  260. uint32_t frequency,
  261. FuriHalSubGhzPreset preset) {
  262. furi_assert(context);
  263. SubGhzProtocolDecoderFirefly* instance = context;
  264. return subghz_block_generic_serialize(&instance->generic, flipper_format, frequency, preset);
  265. }
  266. bool subghz_protocol_decoder_firefly_deserialize(void* context, FlipperFormat* flipper_format) {
  267. furi_assert(context);
  268. SubGhzProtocolDecoderFirefly* instance = context;
  269. return subghz_block_generic_deserialize(&instance->generic, flipper_format);
  270. }
  271. void subghz_protocol_decoder_firefly_get_string(void* context, string_t output) {
  272. furi_assert(context);
  273. SubGhzProtocolDecoderFirefly* instance = context;
  274. uint32_t code_found_lo = instance->generic.data & 0x00000000ffffffff;
  275. uint64_t code_found_reverse = subghz_protocol_blocks_reverse_key(
  276. instance->generic.data, instance->generic.data_count_bit);
  277. uint32_t code_found_reverse_lo = code_found_reverse & 0x00000000ffffffff;
  278. string_cat_printf(
  279. output,
  280. "%s %dbit\r\n"
  281. "Key:0x%08lX\r\n"
  282. "Yek:0x%08lX\r\n"
  283. "DIP:" DIP_PATTERN "\r\n",
  284. instance->generic.protocol_name,
  285. instance->generic.data_count_bit,
  286. code_found_lo,
  287. code_found_reverse_lo,
  288. DATA_TO_DIP(code_found_lo));
  289. }