firefly.c 13 KB

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