phoenix_v2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #include "phoenix_v2.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 "SubGhzProtocolPhoenix_V2"
  8. //transmission only static mode
  9. static const SubGhzBlockConst subghz_protocol_phoenix_v2_const = {
  10. .te_short = 427,
  11. .te_long = 853,
  12. .te_delta = 100,
  13. .min_count_bit_for_found = 52,
  14. };
  15. struct SubGhzProtocolDecoderPhoenix_V2 {
  16. SubGhzProtocolDecoderBase base;
  17. SubGhzBlockDecoder decoder;
  18. SubGhzBlockGeneric generic;
  19. };
  20. struct SubGhzProtocolEncoderPhoenix_V2 {
  21. SubGhzProtocolEncoderBase base;
  22. SubGhzProtocolBlockEncoder encoder;
  23. SubGhzBlockGeneric generic;
  24. };
  25. typedef enum {
  26. Phoenix_V2DecoderStepReset = 0,
  27. Phoenix_V2DecoderStepFoundStartBit,
  28. Phoenix_V2DecoderStepSaveDuration,
  29. Phoenix_V2DecoderStepCheckDuration,
  30. } Phoenix_V2DecoderStep;
  31. const SubGhzProtocolDecoder subghz_protocol_phoenix_v2_decoder = {
  32. .alloc = subghz_protocol_decoder_phoenix_v2_alloc,
  33. .free = subghz_protocol_decoder_phoenix_v2_free,
  34. .feed = subghz_protocol_decoder_phoenix_v2_feed,
  35. .reset = subghz_protocol_decoder_phoenix_v2_reset,
  36. .get_hash_data = subghz_protocol_decoder_phoenix_v2_get_hash_data,
  37. .serialize = subghz_protocol_decoder_phoenix_v2_serialize,
  38. .deserialize = subghz_protocol_decoder_phoenix_v2_deserialize,
  39. .get_string = subghz_protocol_decoder_phoenix_v2_get_string,
  40. };
  41. const SubGhzProtocolEncoder subghz_protocol_phoenix_v2_encoder = {
  42. .alloc = subghz_protocol_encoder_phoenix_v2_alloc,
  43. .free = subghz_protocol_encoder_phoenix_v2_free,
  44. .deserialize = subghz_protocol_encoder_phoenix_v2_deserialize,
  45. .stop = subghz_protocol_encoder_phoenix_v2_stop,
  46. .yield = subghz_protocol_encoder_phoenix_v2_yield,
  47. };
  48. const SubGhzProtocol subghz_protocol_phoenix_v2 = {
  49. .name = SUBGHZ_PROTOCOL_PHOENIX_V2_NAME,
  50. .type = SubGhzProtocolTypeStatic,
  51. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
  52. SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
  53. .decoder = &subghz_protocol_phoenix_v2_decoder,
  54. .encoder = &subghz_protocol_phoenix_v2_encoder,
  55. };
  56. void* subghz_protocol_encoder_phoenix_v2_alloc(SubGhzEnvironment* environment) {
  57. UNUSED(environment);
  58. SubGhzProtocolEncoderPhoenix_V2* instance = malloc(sizeof(SubGhzProtocolEncoderPhoenix_V2));
  59. instance->base.protocol = &subghz_protocol_phoenix_v2;
  60. instance->generic.protocol_name = instance->base.protocol->name;
  61. instance->encoder.repeat = 10;
  62. instance->encoder.size_upload = 128;
  63. instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
  64. instance->encoder.is_running = false;
  65. return instance;
  66. }
  67. void subghz_protocol_encoder_phoenix_v2_free(void* context) {
  68. furi_assert(context);
  69. SubGhzProtocolEncoderPhoenix_V2* instance = context;
  70. free(instance->encoder.upload);
  71. free(instance);
  72. }
  73. /**
  74. * Generating an upload from data.
  75. * @param instance Pointer to a SubGhzProtocolEncoderPhoenix_V2 instance
  76. * @return true On success
  77. */
  78. static bool
  79. subghz_protocol_encoder_phoenix_v2_get_upload(SubGhzProtocolEncoderPhoenix_V2* instance) {
  80. furi_assert(instance);
  81. size_t index = 0;
  82. size_t size_upload = (instance->generic.data_count_bit * 2) + 2;
  83. if(size_upload > instance->encoder.size_upload) {
  84. FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
  85. return false;
  86. } else {
  87. instance->encoder.size_upload = size_upload;
  88. }
  89. //Send header
  90. instance->encoder.upload[index++] =
  91. level_duration_make(false, (uint32_t)subghz_protocol_phoenix_v2_const.te_short * 60);
  92. //Send start bit
  93. instance->encoder.upload[index++] =
  94. level_duration_make(true, (uint32_t)subghz_protocol_phoenix_v2_const.te_short * 6);
  95. //Send key data
  96. for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
  97. if(!bit_read(instance->generic.data, i - 1)) {
  98. //send bit 1
  99. instance->encoder.upload[index++] =
  100. level_duration_make(false, (uint32_t)subghz_protocol_phoenix_v2_const.te_long);
  101. instance->encoder.upload[index++] =
  102. level_duration_make(true, (uint32_t)subghz_protocol_phoenix_v2_const.te_short);
  103. } else {
  104. //send bit 0
  105. instance->encoder.upload[index++] =
  106. level_duration_make(false, (uint32_t)subghz_protocol_phoenix_v2_const.te_short);
  107. instance->encoder.upload[index++] =
  108. level_duration_make(true, (uint32_t)subghz_protocol_phoenix_v2_const.te_long);
  109. }
  110. }
  111. return true;
  112. }
  113. bool subghz_protocol_encoder_phoenix_v2_deserialize(void* context, FlipperFormat* flipper_format) {
  114. furi_assert(context);
  115. SubGhzProtocolEncoderPhoenix_V2* instance = context;
  116. bool res = false;
  117. do {
  118. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  119. FURI_LOG_E(TAG, "Deserialize error");
  120. break;
  121. }
  122. if(instance->generic.data_count_bit !=
  123. subghz_protocol_phoenix_v2_const.min_count_bit_for_found) {
  124. FURI_LOG_E(TAG, "Wrong number of bits in key");
  125. break;
  126. }
  127. //optional parameter parameter
  128. flipper_format_read_uint32(
  129. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  130. if(!subghz_protocol_encoder_phoenix_v2_get_upload(instance)) break;
  131. instance->encoder.is_running = true;
  132. res = true;
  133. } while(false);
  134. return res;
  135. }
  136. void subghz_protocol_encoder_phoenix_v2_stop(void* context) {
  137. SubGhzProtocolEncoderPhoenix_V2* instance = context;
  138. instance->encoder.is_running = false;
  139. }
  140. LevelDuration subghz_protocol_encoder_phoenix_v2_yield(void* context) {
  141. SubGhzProtocolEncoderPhoenix_V2* instance = context;
  142. if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
  143. instance->encoder.is_running = false;
  144. return level_duration_reset();
  145. }
  146. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  147. if(++instance->encoder.front == instance->encoder.size_upload) {
  148. instance->encoder.repeat--;
  149. instance->encoder.front = 0;
  150. }
  151. return ret;
  152. }
  153. void* subghz_protocol_decoder_phoenix_v2_alloc(SubGhzEnvironment* environment) {
  154. UNUSED(environment);
  155. SubGhzProtocolDecoderPhoenix_V2* instance = malloc(sizeof(SubGhzProtocolDecoderPhoenix_V2));
  156. instance->base.protocol = &subghz_protocol_phoenix_v2;
  157. instance->generic.protocol_name = instance->base.protocol->name;
  158. return instance;
  159. }
  160. void subghz_protocol_decoder_phoenix_v2_free(void* context) {
  161. furi_assert(context);
  162. SubGhzProtocolDecoderPhoenix_V2* instance = context;
  163. free(instance);
  164. }
  165. void subghz_protocol_decoder_phoenix_v2_reset(void* context) {
  166. furi_assert(context);
  167. SubGhzProtocolDecoderPhoenix_V2* instance = context;
  168. instance->decoder.parser_step = Phoenix_V2DecoderStepReset;
  169. }
  170. void subghz_protocol_decoder_phoenix_v2_feed(void* context, bool level, uint32_t duration) {
  171. furi_assert(context);
  172. SubGhzProtocolDecoderPhoenix_V2* instance = context;
  173. switch(instance->decoder.parser_step) {
  174. case Phoenix_V2DecoderStepReset:
  175. if((!level) && (DURATION_DIFF(duration, subghz_protocol_phoenix_v2_const.te_short * 60) <
  176. subghz_protocol_phoenix_v2_const.te_delta * 30)) {
  177. //Found Preambula
  178. instance->decoder.parser_step = Phoenix_V2DecoderStepFoundStartBit;
  179. }
  180. break;
  181. case Phoenix_V2DecoderStepFoundStartBit:
  182. if(level && ((DURATION_DIFF(duration, (subghz_protocol_phoenix_v2_const.te_short * 6)) <
  183. subghz_protocol_phoenix_v2_const.te_delta * 4))) {
  184. //Found start bit
  185. instance->decoder.parser_step = Phoenix_V2DecoderStepSaveDuration;
  186. instance->decoder.decode_data = 0;
  187. instance->decoder.decode_count_bit = 0;
  188. } else {
  189. instance->decoder.parser_step = Phoenix_V2DecoderStepReset;
  190. }
  191. break;
  192. case Phoenix_V2DecoderStepSaveDuration:
  193. if(!level) {
  194. if(duration >= ((uint32_t)subghz_protocol_phoenix_v2_const.te_short * 10 +
  195. subghz_protocol_phoenix_v2_const.te_delta)) {
  196. instance->decoder.parser_step = Phoenix_V2DecoderStepFoundStartBit;
  197. if(instance->decoder.decode_count_bit ==
  198. subghz_protocol_phoenix_v2_const.min_count_bit_for_found) {
  199. instance->generic.data = instance->decoder.decode_data;
  200. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  201. if(instance->base.callback)
  202. instance->base.callback(&instance->base, instance->base.context);
  203. }
  204. instance->decoder.decode_data = 0;
  205. instance->decoder.decode_count_bit = 0;
  206. break;
  207. } else {
  208. instance->decoder.te_last = duration;
  209. instance->decoder.parser_step = Phoenix_V2DecoderStepCheckDuration;
  210. }
  211. }
  212. break;
  213. case Phoenix_V2DecoderStepCheckDuration:
  214. if(level) {
  215. if((DURATION_DIFF(
  216. instance->decoder.te_last, subghz_protocol_phoenix_v2_const.te_short) <
  217. subghz_protocol_phoenix_v2_const.te_delta) &&
  218. (DURATION_DIFF(duration, subghz_protocol_phoenix_v2_const.te_long) <
  219. subghz_protocol_phoenix_v2_const.te_delta * 3)) {
  220. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  221. instance->decoder.parser_step = Phoenix_V2DecoderStepSaveDuration;
  222. } else if(
  223. (DURATION_DIFF(
  224. instance->decoder.te_last, subghz_protocol_phoenix_v2_const.te_long) <
  225. subghz_protocol_phoenix_v2_const.te_delta * 3) &&
  226. (DURATION_DIFF(duration, subghz_protocol_phoenix_v2_const.te_short) <
  227. subghz_protocol_phoenix_v2_const.te_delta)) {
  228. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  229. instance->decoder.parser_step = Phoenix_V2DecoderStepSaveDuration;
  230. } else {
  231. instance->decoder.parser_step = Phoenix_V2DecoderStepReset;
  232. }
  233. } else {
  234. instance->decoder.parser_step = Phoenix_V2DecoderStepReset;
  235. }
  236. break;
  237. }
  238. }
  239. /**
  240. * Analysis of received data
  241. * @param instance Pointer to a SubGhzBlockGeneric* instance
  242. */
  243. static void subghz_protocol_phoenix_v2_check_remote_controller(SubGhzBlockGeneric* instance) {
  244. uint64_t data_rev =
  245. subghz_protocol_blocks_reverse_key(instance->data, instance->data_count_bit + 4);
  246. instance->serial = data_rev & 0xFFFFFFFF;
  247. instance->cnt = (data_rev >> 40) & 0xFFFF;
  248. instance->btn = (data_rev >> 32) & 0xF;
  249. }
  250. uint8_t subghz_protocol_decoder_phoenix_v2_get_hash_data(void* context) {
  251. furi_assert(context);
  252. SubGhzProtocolDecoderPhoenix_V2* instance = context;
  253. return subghz_protocol_blocks_get_hash_data(
  254. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  255. }
  256. bool subghz_protocol_decoder_phoenix_v2_serialize(
  257. void* context,
  258. FlipperFormat* flipper_format,
  259. SubGhzRadioPreset* preset) {
  260. furi_assert(context);
  261. SubGhzProtocolDecoderPhoenix_V2* instance = context;
  262. return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  263. }
  264. bool subghz_protocol_decoder_phoenix_v2_deserialize(void* context, FlipperFormat* flipper_format) {
  265. furi_assert(context);
  266. SubGhzProtocolDecoderPhoenix_V2* instance = context;
  267. bool ret = false;
  268. do {
  269. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  270. break;
  271. }
  272. if(instance->generic.data_count_bit !=
  273. subghz_protocol_phoenix_v2_const.min_count_bit_for_found) {
  274. FURI_LOG_E(TAG, "Wrong number of bits in key");
  275. break;
  276. }
  277. ret = true;
  278. } while(false);
  279. return ret;
  280. }
  281. void subghz_protocol_decoder_phoenix_v2_get_string(void* context, FuriString* output) {
  282. furi_assert(context);
  283. SubGhzProtocolDecoderPhoenix_V2* instance = context;
  284. subghz_protocol_phoenix_v2_check_remote_controller(&instance->generic);
  285. furi_string_cat_printf(
  286. output,
  287. "%s %dbit\r\n"
  288. "Key:%02lX%08lX\r\n"
  289. "Sn:0x%07lX \r\n"
  290. "Btn:%X\r\n",
  291. instance->generic.protocol_name,
  292. instance->generic.data_count_bit,
  293. (uint32_t)(instance->generic.data >> 32) & 0xFFFFFFFF,
  294. (uint32_t)(instance->generic.data & 0xFFFFFFFF),
  295. instance->generic.serial,
  296. instance->generic.btn);
  297. }