phoenix_v2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. SubGhzProtocolStatus
  114. subghz_protocol_encoder_phoenix_v2_deserialize(void* context, FlipperFormat* flipper_format) {
  115. furi_assert(context);
  116. SubGhzProtocolEncoderPhoenix_V2* instance = context;
  117. SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
  118. do {
  119. ret = subghz_block_generic_deserialize_check_count_bit(
  120. &instance->generic,
  121. flipper_format,
  122. subghz_protocol_phoenix_v2_const.min_count_bit_for_found);
  123. if(ret != SubGhzProtocolStatusOk) {
  124. break;
  125. }
  126. //optional parameter parameter
  127. flipper_format_read_uint32(
  128. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  129. if(!subghz_protocol_encoder_phoenix_v2_get_upload(instance)) {
  130. ret = SubGhzProtocolStatusErrorEncoderGetUpload;
  131. break;
  132. }
  133. instance->encoder.is_running = true;
  134. } while(false);
  135. return ret;
  136. }
  137. void subghz_protocol_encoder_phoenix_v2_stop(void* context) {
  138. SubGhzProtocolEncoderPhoenix_V2* instance = context;
  139. instance->encoder.is_running = false;
  140. }
  141. LevelDuration subghz_protocol_encoder_phoenix_v2_yield(void* context) {
  142. SubGhzProtocolEncoderPhoenix_V2* instance = context;
  143. if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
  144. instance->encoder.is_running = false;
  145. return level_duration_reset();
  146. }
  147. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  148. if(++instance->encoder.front == instance->encoder.size_upload) {
  149. instance->encoder.repeat--;
  150. instance->encoder.front = 0;
  151. }
  152. return ret;
  153. }
  154. void* subghz_protocol_decoder_phoenix_v2_alloc(SubGhzEnvironment* environment) {
  155. UNUSED(environment);
  156. SubGhzProtocolDecoderPhoenix_V2* instance = malloc(sizeof(SubGhzProtocolDecoderPhoenix_V2));
  157. instance->base.protocol = &subghz_protocol_phoenix_v2;
  158. instance->generic.protocol_name = instance->base.protocol->name;
  159. return instance;
  160. }
  161. void subghz_protocol_decoder_phoenix_v2_free(void* context) {
  162. furi_assert(context);
  163. SubGhzProtocolDecoderPhoenix_V2* instance = context;
  164. free(instance);
  165. }
  166. void subghz_protocol_decoder_phoenix_v2_reset(void* context) {
  167. furi_assert(context);
  168. SubGhzProtocolDecoderPhoenix_V2* instance = context;
  169. instance->decoder.parser_step = Phoenix_V2DecoderStepReset;
  170. }
  171. void subghz_protocol_decoder_phoenix_v2_feed(void* context, bool level, uint32_t duration) {
  172. furi_assert(context);
  173. SubGhzProtocolDecoderPhoenix_V2* instance = context;
  174. switch(instance->decoder.parser_step) {
  175. case Phoenix_V2DecoderStepReset:
  176. if((!level) && (DURATION_DIFF(duration, subghz_protocol_phoenix_v2_const.te_short * 60) <
  177. subghz_protocol_phoenix_v2_const.te_delta * 30)) {
  178. //Found Preambula
  179. instance->decoder.parser_step = Phoenix_V2DecoderStepFoundStartBit;
  180. }
  181. break;
  182. case Phoenix_V2DecoderStepFoundStartBit:
  183. if(level && ((DURATION_DIFF(duration, (subghz_protocol_phoenix_v2_const.te_short * 6)) <
  184. subghz_protocol_phoenix_v2_const.te_delta * 4))) {
  185. //Found start bit
  186. instance->decoder.parser_step = Phoenix_V2DecoderStepSaveDuration;
  187. instance->decoder.decode_data = 0;
  188. instance->decoder.decode_count_bit = 0;
  189. } else {
  190. instance->decoder.parser_step = Phoenix_V2DecoderStepReset;
  191. }
  192. break;
  193. case Phoenix_V2DecoderStepSaveDuration:
  194. if(!level) {
  195. if(duration >= ((uint32_t)subghz_protocol_phoenix_v2_const.te_short * 10 +
  196. subghz_protocol_phoenix_v2_const.te_delta)) {
  197. instance->decoder.parser_step = Phoenix_V2DecoderStepFoundStartBit;
  198. if(instance->decoder.decode_count_bit ==
  199. subghz_protocol_phoenix_v2_const.min_count_bit_for_found) {
  200. instance->generic.data = instance->decoder.decode_data;
  201. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  202. if(instance->base.callback)
  203. instance->base.callback(&instance->base, instance->base.context);
  204. }
  205. instance->decoder.decode_data = 0;
  206. instance->decoder.decode_count_bit = 0;
  207. break;
  208. } else {
  209. instance->decoder.te_last = duration;
  210. instance->decoder.parser_step = Phoenix_V2DecoderStepCheckDuration;
  211. }
  212. }
  213. break;
  214. case Phoenix_V2DecoderStepCheckDuration:
  215. if(level) {
  216. if((DURATION_DIFF(
  217. instance->decoder.te_last, subghz_protocol_phoenix_v2_const.te_short) <
  218. subghz_protocol_phoenix_v2_const.te_delta) &&
  219. (DURATION_DIFF(duration, subghz_protocol_phoenix_v2_const.te_long) <
  220. subghz_protocol_phoenix_v2_const.te_delta * 3)) {
  221. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  222. instance->decoder.parser_step = Phoenix_V2DecoderStepSaveDuration;
  223. } else if(
  224. (DURATION_DIFF(
  225. instance->decoder.te_last, subghz_protocol_phoenix_v2_const.te_long) <
  226. subghz_protocol_phoenix_v2_const.te_delta * 3) &&
  227. (DURATION_DIFF(duration, subghz_protocol_phoenix_v2_const.te_short) <
  228. subghz_protocol_phoenix_v2_const.te_delta)) {
  229. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  230. instance->decoder.parser_step = Phoenix_V2DecoderStepSaveDuration;
  231. } else {
  232. instance->decoder.parser_step = Phoenix_V2DecoderStepReset;
  233. }
  234. } else {
  235. instance->decoder.parser_step = Phoenix_V2DecoderStepReset;
  236. }
  237. break;
  238. }
  239. }
  240. /**
  241. * Analysis of received data
  242. * @param instance Pointer to a SubGhzBlockGeneric* instance
  243. */
  244. static void subghz_protocol_phoenix_v2_check_remote_controller(SubGhzBlockGeneric* instance) {
  245. uint64_t data_rev =
  246. subghz_protocol_blocks_reverse_key(instance->data, instance->data_count_bit + 4);
  247. instance->serial = data_rev & 0xFFFFFFFF;
  248. instance->cnt = (data_rev >> 40) & 0xFFFF;
  249. instance->btn = (data_rev >> 32) & 0xF;
  250. }
  251. uint8_t subghz_protocol_decoder_phoenix_v2_get_hash_data(void* context) {
  252. furi_assert(context);
  253. SubGhzProtocolDecoderPhoenix_V2* instance = context;
  254. return subghz_protocol_blocks_get_hash_data(
  255. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  256. }
  257. SubGhzProtocolStatus subghz_protocol_decoder_phoenix_v2_serialize(
  258. void* context,
  259. FlipperFormat* flipper_format,
  260. SubGhzRadioPreset* preset) {
  261. furi_assert(context);
  262. SubGhzProtocolDecoderPhoenix_V2* instance = context;
  263. return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  264. }
  265. SubGhzProtocolStatus
  266. subghz_protocol_decoder_phoenix_v2_deserialize(void* context, FlipperFormat* flipper_format) {
  267. furi_assert(context);
  268. SubGhzProtocolDecoderPhoenix_V2* instance = context;
  269. return subghz_block_generic_deserialize_check_count_bit(
  270. &instance->generic,
  271. flipper_format,
  272. subghz_protocol_phoenix_v2_const.min_count_bit_for_found);
  273. }
  274. void subghz_protocol_decoder_phoenix_v2_get_string(void* context, FuriString* output) {
  275. furi_assert(context);
  276. SubGhzProtocolDecoderPhoenix_V2* instance = context;
  277. subghz_protocol_phoenix_v2_check_remote_controller(&instance->generic);
  278. furi_string_cat_printf(
  279. output,
  280. "%s %dbit\r\n"
  281. "Key:%02lX%08lX\r\n"
  282. "Sn:0x%07lX \r\n"
  283. "Btn:%X\r\n",
  284. instance->generic.protocol_name,
  285. instance->generic.data_count_bit,
  286. (uint32_t)(instance->generic.data >> 32) & 0xFFFFFFFF,
  287. (uint32_t)(instance->generic.data & 0xFFFFFFFF),
  288. instance->generic.serial,
  289. instance->generic.btn);
  290. }