secplus_v2.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. #include "secplus_v2.h"
  2. #include <lib/toolbox/manchester_decoder.h>
  3. #include <lib/toolbox/manchester_encoder.h>
  4. #include "../blocks/const.h"
  5. #include "../blocks/decoder.h"
  6. #include "../blocks/encoder.h"
  7. #include "../blocks/generic.h"
  8. #include "../blocks/math.h"
  9. /*
  10. * Help
  11. * https://github.com/argilo/secplus
  12. * https://github.com/merbanan/rtl_433/blob/master/src/devices/secplus_v2.c
  13. */
  14. #define TAG "SubGhzProtocoSecPlus_v2"
  15. #define SECPLUS_V2_HEADER 0x3C0000000000
  16. #define SECPLUS_V2_HEADER_MASK 0xFFFF3C0000000000
  17. #define SECPLUS_V2_PACKET_1 0x000000000000
  18. #define SECPLUS_V2_PACKET_2 0x010000000000
  19. #define SECPLUS_V2_PACKET_MASK 0x30000000000
  20. static const SubGhzBlockConst subghz_protocol_secplus_v2_const = {
  21. .te_short = 250,
  22. .te_long = 500,
  23. .te_delta = 110,
  24. .min_count_bit_for_found = 62,
  25. };
  26. struct SubGhzProtocolDecoderSecPlus_v2 {
  27. SubGhzProtocolDecoderBase base;
  28. SubGhzBlockDecoder decoder;
  29. SubGhzBlockGeneric generic;
  30. ManchesterState manchester_saved_state;
  31. uint64_t secplus_packet_1;
  32. };
  33. struct SubGhzProtocolEncoderSecPlus_v2 {
  34. SubGhzProtocolEncoderBase base;
  35. SubGhzProtocolBlockEncoder encoder;
  36. SubGhzBlockGeneric generic;
  37. uint64_t secplus_packet_1;
  38. };
  39. typedef enum {
  40. SecPlus_v2DecoderStepReset = 0,
  41. SecPlus_v2DecoderStepDecoderData,
  42. } SecPlus_v2DecoderStep;
  43. const SubGhzProtocolDecoder subghz_protocol_secplus_v2_decoder = {
  44. .alloc = subghz_protocol_decoder_secplus_v2_alloc,
  45. .free = subghz_protocol_decoder_secplus_v2_free,
  46. .feed = subghz_protocol_decoder_secplus_v2_feed,
  47. .reset = subghz_protocol_decoder_secplus_v2_reset,
  48. .get_hash_data = subghz_protocol_decoder_secplus_v2_get_hash_data,
  49. .serialize = subghz_protocol_decoder_secplus_v2_serialize,
  50. .deserialize = subghz_protocol_decoder_secplus_v2_deserialize,
  51. .get_string = subghz_protocol_decoder_secplus_v2_get_string,
  52. };
  53. const SubGhzProtocolEncoder subghz_protocol_secplus_v2_encoder = {
  54. .alloc = subghz_protocol_encoder_secplus_v2_alloc,
  55. .free = subghz_protocol_encoder_secplus_v2_free,
  56. .deserialize = subghz_protocol_encoder_secplus_v2_deserialize,
  57. .stop = subghz_protocol_encoder_secplus_v2_stop,
  58. .yield = subghz_protocol_encoder_secplus_v2_yield,
  59. };
  60. const SubGhzProtocol subghz_protocol_secplus_v2 = {
  61. .name = SUBGHZ_PROTOCOL_SECPLUS_V2_NAME,
  62. .type = SubGhzProtocolTypeDynamic,
  63. .flag = SubGhzProtocolFlag_315 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
  64. SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Send,
  65. .decoder = &subghz_protocol_secplus_v2_decoder,
  66. .encoder = &subghz_protocol_secplus_v2_encoder,
  67. };
  68. void* subghz_protocol_encoder_secplus_v2_alloc(SubGhzEnvironment* environment) {
  69. UNUSED(environment);
  70. SubGhzProtocolEncoderSecPlus_v2* instance = malloc(sizeof(SubGhzProtocolEncoderSecPlus_v2));
  71. instance->base.protocol = &subghz_protocol_secplus_v2;
  72. instance->generic.protocol_name = instance->base.protocol->name;
  73. instance->encoder.repeat = 10;
  74. instance->encoder.size_upload = 256;
  75. instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
  76. instance->encoder.is_running = false;
  77. return instance;
  78. }
  79. void subghz_protocol_encoder_secplus_v2_free(void* context) {
  80. furi_assert(context);
  81. SubGhzProtocolEncoderSecPlus_v2* instance = context;
  82. free(instance->encoder.upload);
  83. free(instance);
  84. }
  85. static bool subghz_protocol_secplus_v2_mix_invet(uint8_t invert, uint16_t p[]) {
  86. // selectively invert buffers
  87. switch(invert) {
  88. case 0x00: // 0b0000 (True, True, False),
  89. p[0] = ~p[0] & 0x03FF;
  90. p[1] = ~p[1] & 0x03FF;
  91. break;
  92. case 0x01: // 0b0001 (False, True, False),
  93. p[1] = ~p[1] & 0x03FF;
  94. break;
  95. case 0x02: // 0b0010 (False, False, True),
  96. p[2] = ~p[2] & 0x03FF;
  97. break;
  98. case 0x04: // 0b0100 (True, True, True),
  99. p[0] = ~p[0] & 0x03FF;
  100. p[1] = ~p[1] & 0x03FF;
  101. p[2] = ~p[2] & 0x03FF;
  102. break;
  103. case 0x05: // 0b0101 (True, False, True),
  104. case 0x0a: // 0b1010 (True, False, True),
  105. p[0] = ~p[0] & 0x03FF;
  106. p[2] = ~p[2] & 0x03FF;
  107. break;
  108. case 0x06: // 0b0110 (False, True, True),
  109. p[1] = ~p[1] & 0x03FF;
  110. p[2] = ~p[2] & 0x03FF;
  111. break;
  112. case 0x08: // 0b1000 (True, False, False),
  113. p[0] = ~p[0] & 0x03FF;
  114. break;
  115. case 0x09: // 0b1001 (False, False, False),
  116. break;
  117. default:
  118. FURI_LOG_E(TAG, "Invert FAIL");
  119. return false;
  120. }
  121. return true;
  122. }
  123. static bool subghz_protocol_secplus_v2_mix_order_decode(uint8_t order, uint16_t p[]) {
  124. uint16_t a = p[0], b = p[1], c = p[2];
  125. // selectively reorder buffers
  126. switch(order) {
  127. case 0x06: // 0b0110 2, 1, 0],
  128. case 0x09: // 0b1001 2, 1, 0],
  129. p[2] = a;
  130. p[1] = b;
  131. p[0] = c;
  132. break;
  133. case 0x08: // 0b1000 1, 2, 0],
  134. case 0x04: // 0b0100 1, 2, 0],
  135. p[1] = a;
  136. p[2] = b;
  137. p[0] = c;
  138. break;
  139. case 0x01: // 0b0001 2, 0, 1],
  140. p[2] = a;
  141. p[0] = b;
  142. p[1] = c;
  143. break;
  144. case 0x00: // 0b0000 0, 2, 1],
  145. p[0] = a;
  146. p[2] = b;
  147. p[1] = c;
  148. break;
  149. case 0x05: // 0b0101 1, 0, 2],
  150. p[1] = a;
  151. p[0] = b;
  152. p[2] = c;
  153. break;
  154. case 0x02: // 0b0010 0, 1, 2],
  155. case 0x0A: // 0b1010 0, 1, 2],
  156. p[0] = a;
  157. p[1] = b;
  158. p[2] = c;
  159. break;
  160. default:
  161. FURI_LOG_E(TAG, "Order FAIL");
  162. return false;
  163. }
  164. return true;
  165. }
  166. static bool subghz_protocol_secplus_v2_mix_order_encode(uint8_t order, uint16_t p[]) {
  167. uint16_t a, b, c;
  168. // selectively reorder buffers
  169. switch(order) {
  170. case 0x06: // 0b0110 2, 1, 0],
  171. case 0x09: // 0b1001 2, 1, 0],
  172. a = p[2];
  173. b = p[1];
  174. c = p[0];
  175. break;
  176. case 0x08: // 0b1000 1, 2, 0],
  177. case 0x04: // 0b0100 1, 2, 0],
  178. a = p[1];
  179. b = p[2];
  180. c = p[0];
  181. break;
  182. case 0x01: // 0b0001 2, 0, 1],
  183. a = p[2];
  184. b = p[0];
  185. c = p[1];
  186. break;
  187. case 0x00: // 0b0000 0, 2, 1],
  188. a = p[0];
  189. b = p[2];
  190. c = p[1];
  191. break;
  192. case 0x05: // 0b0101 1, 0, 2],
  193. a = p[1];
  194. b = p[0];
  195. c = p[2];
  196. break;
  197. case 0x02: // 0b0010 0, 1, 2],
  198. case 0x0A: // 0b1010 0, 1, 2],
  199. a = p[0];
  200. b = p[1];
  201. c = p[2];
  202. break;
  203. default:
  204. FURI_LOG_E(TAG, "Order FAIL");
  205. return false;
  206. }
  207. p[0] = a;
  208. p[1] = b;
  209. p[2] = c;
  210. return true;
  211. }
  212. /**
  213. * Security+ 2.0 half-message decoding
  214. * @param data data
  215. * @param roll_array[] return roll_array part
  216. * @param fixed[] return fixed part
  217. * @return true On success
  218. */
  219. static bool
  220. subghz_protocol_secplus_v2_decode_half(uint64_t data, uint8_t roll_array[], uint32_t* fixed) {
  221. uint8_t order = (data >> 34) & 0x0f;
  222. uint8_t invert = (data >> 30) & 0x0f;
  223. uint16_t p[3] = {0};
  224. for(int i = 29; i >= 0; i -= 3) {
  225. p[0] = p[0] << 1 | bit_read(data, i);
  226. p[1] = p[1] << 1 | bit_read(data, i - 1);
  227. p[2] = p[2] << 1 | bit_read(data, i - 2);
  228. }
  229. if(!subghz_protocol_secplus_v2_mix_invet(invert, p)) return false;
  230. if(!subghz_protocol_secplus_v2_mix_order_decode(order, p)) return false;
  231. data = order << 4 | invert;
  232. int k = 0;
  233. for(int i = 6; i >= 0; i -= 2) {
  234. roll_array[k++] = (data >> i) & 0x03;
  235. if(roll_array[k] == 3) {
  236. FURI_LOG_E(TAG, "Roll_Array FAIL");
  237. return false;
  238. }
  239. }
  240. for(int i = 8; i >= 0; i -= 2) {
  241. roll_array[k++] = (p[2] >> i) & 0x03;
  242. if(roll_array[k] == 3) {
  243. FURI_LOG_E(TAG, "Roll_Array FAIL");
  244. return false;
  245. }
  246. }
  247. fixed[0] = p[0] << 10 | p[1];
  248. return true;
  249. }
  250. /**
  251. * Analysis of received data
  252. * @param instance Pointer to a SubGhzBlockGeneric* instance
  253. * @param packet_1 first part of the message
  254. */
  255. static void
  256. subghz_protocol_secplus_v2_remote_controller(SubGhzBlockGeneric* instance, uint64_t packet_1) {
  257. uint32_t fixed_1[1];
  258. uint8_t roll_1[9] = {0};
  259. uint32_t fixed_2[1];
  260. uint8_t roll_2[9] = {0};
  261. uint8_t rolling_digits[18] = {0};
  262. if(subghz_protocol_secplus_v2_decode_half(packet_1, roll_1, fixed_1) &&
  263. subghz_protocol_secplus_v2_decode_half(instance->data, roll_2, fixed_2)) {
  264. rolling_digits[0] = roll_2[8];
  265. rolling_digits[1] = roll_1[8];
  266. rolling_digits[2] = roll_2[4];
  267. rolling_digits[3] = roll_2[5];
  268. rolling_digits[4] = roll_2[6];
  269. rolling_digits[5] = roll_2[7];
  270. rolling_digits[6] = roll_1[4];
  271. rolling_digits[7] = roll_1[5];
  272. rolling_digits[8] = roll_1[6];
  273. rolling_digits[9] = roll_1[7];
  274. rolling_digits[10] = roll_2[0];
  275. rolling_digits[11] = roll_2[1];
  276. rolling_digits[12] = roll_2[2];
  277. rolling_digits[13] = roll_2[3];
  278. rolling_digits[14] = roll_1[0];
  279. rolling_digits[15] = roll_1[1];
  280. rolling_digits[16] = roll_1[2];
  281. rolling_digits[17] = roll_1[3];
  282. uint32_t rolling = 0;
  283. for(int i = 0; i < 18; i++) {
  284. rolling = (rolling * 3) + rolling_digits[i];
  285. }
  286. // Max value = 2^28 (268435456)
  287. if(rolling >= 0x10000000) {
  288. FURI_LOG_E(TAG, "Rolling FAIL");
  289. instance->cnt = 0;
  290. instance->btn = 0;
  291. instance->serial = 0;
  292. } else {
  293. instance->cnt = subghz_protocol_blocks_reverse_key(rolling, 28);
  294. instance->btn = fixed_1[0] >> 12;
  295. instance->serial = fixed_1[0] << 20 | fixed_2[0];
  296. }
  297. } else {
  298. instance->cnt = 0;
  299. instance->btn = 0;
  300. instance->serial = 0;
  301. }
  302. }
  303. /**
  304. * Security+ 2.0 half-message encoding
  305. * @param roll_array[] roll_array part
  306. * @param fixed[] fixed part
  307. * @return return data
  308. */
  309. static uint64_t subghz_protocol_secplus_v2_encode_half(uint8_t roll_array[], uint32_t fixed) {
  310. uint64_t data = 0;
  311. uint16_t p[3] = {(fixed >> 10) & 0x3FF, fixed & 0x3FF, 0};
  312. uint8_t order = roll_array[0] << 2 | roll_array[1];
  313. uint8_t invert = roll_array[2] << 2 | roll_array[3];
  314. p[2] = (uint16_t)roll_array[4] << 8 | roll_array[5] << 6 | roll_array[6] << 4 |
  315. roll_array[7] << 2 | roll_array[8];
  316. if(!subghz_protocol_secplus_v2_mix_order_encode(order, p)) return 0;
  317. if(!subghz_protocol_secplus_v2_mix_invet(invert, p)) return 0;
  318. for(int i = 0; i < 10; i++) {
  319. data <<= 3;
  320. data |= bit_read(p[0], 9 - i) << 2 | bit_read(p[1], 9 - i) << 1 | bit_read(p[2], 9 - i);
  321. }
  322. data |= ((uint64_t)order) << 34 | ((uint64_t)invert) << 30;
  323. return data;
  324. }
  325. /**
  326. * Security+ 2.0 message encoding
  327. * @param instance SubGhzProtocolEncoderSecPlus_v2*
  328. */
  329. static void subghz_protocol_secplus_v2_encode(SubGhzProtocolEncoderSecPlus_v2* instance) {
  330. uint32_t fixed_1[1] = {instance->generic.btn << 12 | instance->generic.serial >> 20};
  331. uint32_t fixed_2[1] = {instance->generic.serial & 0xFFFFF};
  332. uint8_t rolling_digits[18] = {0};
  333. uint8_t roll_1[9] = {0};
  334. uint8_t roll_2[9] = {0};
  335. instance->generic.cnt++;
  336. //ToDo it is not known what value the counter starts
  337. if(instance->generic.cnt > 0xFFFFFFF) instance->generic.cnt = 0xE500000;
  338. uint32_t rolling = subghz_protocol_blocks_reverse_key(instance->generic.cnt, 28);
  339. for(int8_t i = 17; i > -1; i--) {
  340. rolling_digits[i] = rolling % 3;
  341. rolling /= 3;
  342. }
  343. roll_2[8] = rolling_digits[0];
  344. roll_1[8] = rolling_digits[1];
  345. roll_2[4] = rolling_digits[2];
  346. roll_2[5] = rolling_digits[3];
  347. roll_2[6] = rolling_digits[4];
  348. roll_2[7] = rolling_digits[5];
  349. roll_1[4] = rolling_digits[6];
  350. roll_1[5] = rolling_digits[7];
  351. roll_1[6] = rolling_digits[8];
  352. roll_1[7] = rolling_digits[9];
  353. roll_2[0] = rolling_digits[10];
  354. roll_2[1] = rolling_digits[11];
  355. roll_2[2] = rolling_digits[12];
  356. roll_2[3] = rolling_digits[13];
  357. roll_1[0] = rolling_digits[14];
  358. roll_1[1] = rolling_digits[15];
  359. roll_1[2] = rolling_digits[16];
  360. roll_1[3] = rolling_digits[17];
  361. instance->secplus_packet_1 = SECPLUS_V2_HEADER | SECPLUS_V2_PACKET_1 |
  362. subghz_protocol_secplus_v2_encode_half(roll_1, fixed_1[0]);
  363. instance->generic.data = SECPLUS_V2_HEADER | SECPLUS_V2_PACKET_2 |
  364. subghz_protocol_secplus_v2_encode_half(roll_2, fixed_2[0]);
  365. }
  366. static LevelDuration
  367. subghz_protocol_encoder_secplus_v2_add_duration_to_upload(ManchesterEncoderResult result) {
  368. LevelDuration data = {.duration = 0, .level = 0};
  369. switch(result) {
  370. case ManchesterEncoderResultShortLow:
  371. data.duration = subghz_protocol_secplus_v2_const.te_short;
  372. data.level = false;
  373. break;
  374. case ManchesterEncoderResultLongLow:
  375. data.duration = subghz_protocol_secplus_v2_const.te_long;
  376. data.level = false;
  377. break;
  378. case ManchesterEncoderResultLongHigh:
  379. data.duration = subghz_protocol_secplus_v2_const.te_long;
  380. data.level = true;
  381. break;
  382. case ManchesterEncoderResultShortHigh:
  383. data.duration = subghz_protocol_secplus_v2_const.te_short;
  384. data.level = true;
  385. break;
  386. default:
  387. furi_crash("SubGhz: ManchesterEncoderResult is incorrect.");
  388. break;
  389. }
  390. return level_duration_make(data.level, data.duration);
  391. }
  392. /**
  393. * Generating an upload from data.
  394. * @param instance Pointer to a SubGhzProtocolEncoderSecPlus_v2 instance
  395. */
  396. static void
  397. subghz_protocol_encoder_secplus_v2_get_upload(SubGhzProtocolEncoderSecPlus_v2* instance) {
  398. furi_assert(instance);
  399. size_t index = 0;
  400. ManchesterEncoderState enc_state;
  401. manchester_encoder_reset(&enc_state);
  402. ManchesterEncoderResult result;
  403. //Send data packet 1
  404. for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
  405. if(!manchester_encoder_advance(
  406. &enc_state, bit_read(instance->secplus_packet_1, i - 1), &result)) {
  407. instance->encoder.upload[index++] =
  408. subghz_protocol_encoder_secplus_v2_add_duration_to_upload(result);
  409. manchester_encoder_advance(
  410. &enc_state, bit_read(instance->secplus_packet_1, i - 1), &result);
  411. }
  412. instance->encoder.upload[index++] =
  413. subghz_protocol_encoder_secplus_v2_add_duration_to_upload(result);
  414. }
  415. instance->encoder.upload[index] = subghz_protocol_encoder_secplus_v2_add_duration_to_upload(
  416. manchester_encoder_finish(&enc_state));
  417. if(level_duration_get_level(instance->encoder.upload[index])) {
  418. index++;
  419. }
  420. instance->encoder.upload[index++] =
  421. level_duration_make(false, (uint32_t)subghz_protocol_secplus_v2_const.te_long * 136);
  422. //Send data packet 2
  423. manchester_encoder_reset(&enc_state);
  424. for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
  425. if(!manchester_encoder_advance(
  426. &enc_state, bit_read(instance->generic.data, i - 1), &result)) {
  427. instance->encoder.upload[index++] =
  428. subghz_protocol_encoder_secplus_v2_add_duration_to_upload(result);
  429. manchester_encoder_advance(
  430. &enc_state, bit_read(instance->generic.data, i - 1), &result);
  431. }
  432. instance->encoder.upload[index++] =
  433. subghz_protocol_encoder_secplus_v2_add_duration_to_upload(result);
  434. }
  435. instance->encoder.upload[index] = subghz_protocol_encoder_secplus_v2_add_duration_to_upload(
  436. manchester_encoder_finish(&enc_state));
  437. if(level_duration_get_level(instance->encoder.upload[index])) {
  438. index++;
  439. }
  440. instance->encoder.upload[index++] =
  441. level_duration_make(false, (uint32_t)subghz_protocol_secplus_v2_const.te_long * 136);
  442. instance->encoder.size_upload = index;
  443. }
  444. bool subghz_protocol_encoder_secplus_v2_deserialize(void* context, FlipperFormat* flipper_format) {
  445. furi_assert(context);
  446. SubGhzProtocolEncoderSecPlus_v2* instance = context;
  447. bool res = false;
  448. do {
  449. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  450. FURI_LOG_E(TAG, "Deserialize error");
  451. break;
  452. }
  453. if(instance->generic.data_count_bit !=
  454. subghz_protocol_secplus_v2_const.min_count_bit_for_found) {
  455. FURI_LOG_E(TAG, "Wrong number of bits in key");
  456. break;
  457. }
  458. uint8_t key_data[sizeof(uint64_t)] = {0};
  459. if(!flipper_format_read_hex(
  460. flipper_format, "Secplus_packet_1", key_data, sizeof(uint64_t))) {
  461. FURI_LOG_E(TAG, "Secplus_packet_1");
  462. break;
  463. }
  464. for(uint8_t i = 0; i < sizeof(uint64_t); i++) {
  465. instance->secplus_packet_1 = instance->secplus_packet_1 << 8 | key_data[i];
  466. }
  467. subghz_protocol_secplus_v2_remote_controller(
  468. &instance->generic, instance->secplus_packet_1);
  469. subghz_protocol_secplus_v2_encode(instance);
  470. //optional parameter parameter
  471. flipper_format_read_uint32(
  472. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  473. subghz_protocol_encoder_secplus_v2_get_upload(instance);
  474. //update data
  475. for(size_t i = 0; i < sizeof(uint64_t); i++) {
  476. key_data[sizeof(uint64_t) - i - 1] = (instance->generic.data >> i * 8) & 0xFF;
  477. }
  478. if(!flipper_format_update_hex(flipper_format, "Key", key_data, sizeof(uint64_t))) {
  479. FURI_LOG_E(TAG, "Unable to add Key");
  480. break;
  481. }
  482. for(size_t i = 0; i < sizeof(uint64_t); i++) {
  483. key_data[sizeof(uint64_t) - i - 1] = (instance->secplus_packet_1 >> i * 8) & 0xFF;
  484. }
  485. if(!flipper_format_update_hex(
  486. flipper_format, "Secplus_packet_1", key_data, sizeof(uint64_t))) {
  487. FURI_LOG_E(TAG, "Unable to add Secplus_packet_1");
  488. break;
  489. }
  490. instance->encoder.is_running = true;
  491. res = true;
  492. } while(false);
  493. return res;
  494. }
  495. void subghz_protocol_encoder_secplus_v2_stop(void* context) {
  496. SubGhzProtocolEncoderSecPlus_v2* instance = context;
  497. instance->encoder.is_running = false;
  498. }
  499. LevelDuration subghz_protocol_encoder_secplus_v2_yield(void* context) {
  500. SubGhzProtocolEncoderSecPlus_v2* instance = context;
  501. if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
  502. instance->encoder.is_running = false;
  503. return level_duration_reset();
  504. }
  505. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  506. if(++instance->encoder.front == instance->encoder.size_upload) {
  507. instance->encoder.repeat--;
  508. instance->encoder.front = 0;
  509. }
  510. return ret;
  511. }
  512. bool subghz_protocol_secplus_v2_create_data(
  513. void* context,
  514. FlipperFormat* flipper_format,
  515. uint32_t serial,
  516. uint8_t btn,
  517. uint32_t cnt,
  518. SubGhzRadioPreset* preset) {
  519. furi_assert(context);
  520. SubGhzProtocolEncoderSecPlus_v2* instance = context;
  521. instance->generic.serial = serial;
  522. instance->generic.cnt = cnt;
  523. instance->generic.btn = btn;
  524. instance->generic.data_count_bit =
  525. (uint8_t)subghz_protocol_secplus_v2_const.min_count_bit_for_found;
  526. subghz_protocol_secplus_v2_encode(instance);
  527. bool res = subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  528. uint8_t key_data[sizeof(uint64_t)] = {0};
  529. for(size_t i = 0; i < sizeof(uint64_t); i++) {
  530. key_data[sizeof(uint64_t) - i - 1] = (instance->secplus_packet_1 >> i * 8) & 0xFF;
  531. }
  532. if(res &&
  533. !flipper_format_write_hex(flipper_format, "Secplus_packet_1", key_data, sizeof(uint64_t))) {
  534. FURI_LOG_E(TAG, "Unable to add Secplus_packet_1");
  535. res = false;
  536. }
  537. return res;
  538. }
  539. void* subghz_protocol_decoder_secplus_v2_alloc(SubGhzEnvironment* environment) {
  540. UNUSED(environment);
  541. SubGhzProtocolDecoderSecPlus_v2* instance = malloc(sizeof(SubGhzProtocolDecoderSecPlus_v2));
  542. instance->base.protocol = &subghz_protocol_secplus_v2;
  543. instance->generic.protocol_name = instance->base.protocol->name;
  544. return instance;
  545. }
  546. void subghz_protocol_decoder_secplus_v2_free(void* context) {
  547. furi_assert(context);
  548. SubGhzProtocolDecoderSecPlus_v2* instance = context;
  549. free(instance);
  550. }
  551. void subghz_protocol_decoder_secplus_v2_reset(void* context) {
  552. furi_assert(context);
  553. // SubGhzProtocolDecoderSecPlus_v2* instance = context;
  554. // does not reset the decoder because you need to get 2 parts of the package
  555. }
  556. static bool subghz_protocol_secplus_v2_check_packet(SubGhzProtocolDecoderSecPlus_v2* instance) {
  557. if((instance->decoder.decode_data & SECPLUS_V2_HEADER_MASK) == SECPLUS_V2_HEADER) {
  558. if((instance->decoder.decode_data & SECPLUS_V2_PACKET_MASK) == SECPLUS_V2_PACKET_1) {
  559. instance->secplus_packet_1 = instance->decoder.decode_data;
  560. } else if(
  561. ((instance->decoder.decode_data & SECPLUS_V2_PACKET_MASK) == SECPLUS_V2_PACKET_2) &&
  562. (instance->secplus_packet_1)) {
  563. return true;
  564. }
  565. }
  566. return false;
  567. }
  568. void subghz_protocol_decoder_secplus_v2_feed(void* context, bool level, uint32_t duration) {
  569. furi_assert(context);
  570. SubGhzProtocolDecoderSecPlus_v2* instance = context;
  571. ManchesterEvent event = ManchesterEventReset;
  572. switch(instance->decoder.parser_step) {
  573. case SecPlus_v2DecoderStepReset:
  574. if((!level) && (DURATION_DIFF(duration, subghz_protocol_secplus_v2_const.te_long * 130) <
  575. subghz_protocol_secplus_v2_const.te_delta * 100)) {
  576. //Found header Security+ 2.0
  577. instance->decoder.parser_step = SecPlus_v2DecoderStepDecoderData;
  578. instance->decoder.decode_data = 0;
  579. instance->decoder.decode_count_bit = 0;
  580. instance->secplus_packet_1 = 0;
  581. manchester_advance(
  582. instance->manchester_saved_state,
  583. ManchesterEventReset,
  584. &instance->manchester_saved_state,
  585. NULL);
  586. manchester_advance(
  587. instance->manchester_saved_state,
  588. ManchesterEventLongHigh,
  589. &instance->manchester_saved_state,
  590. NULL);
  591. manchester_advance(
  592. instance->manchester_saved_state,
  593. ManchesterEventShortLow,
  594. &instance->manchester_saved_state,
  595. NULL);
  596. }
  597. break;
  598. case SecPlus_v2DecoderStepDecoderData:
  599. if(!level) {
  600. if(DURATION_DIFF(duration, subghz_protocol_secplus_v2_const.te_short) <
  601. subghz_protocol_secplus_v2_const.te_delta) {
  602. event = ManchesterEventShortLow;
  603. } else if(
  604. DURATION_DIFF(duration, subghz_protocol_secplus_v2_const.te_long) <
  605. subghz_protocol_secplus_v2_const.te_delta) {
  606. event = ManchesterEventLongLow;
  607. } else if(
  608. duration >= (uint32_t)(subghz_protocol_secplus_v2_const.te_long * 2 +
  609. subghz_protocol_secplus_v2_const.te_delta)) {
  610. if(instance->decoder.decode_count_bit ==
  611. subghz_protocol_secplus_v2_const.min_count_bit_for_found) {
  612. instance->generic.data = instance->decoder.decode_data;
  613. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  614. if(subghz_protocol_secplus_v2_check_packet(instance)) {
  615. if(instance->base.callback)
  616. instance->base.callback(&instance->base, instance->base.context);
  617. instance->decoder.parser_step = SecPlus_v2DecoderStepReset;
  618. }
  619. }
  620. instance->decoder.decode_data = 0;
  621. instance->decoder.decode_count_bit = 0;
  622. manchester_advance(
  623. instance->manchester_saved_state,
  624. ManchesterEventReset,
  625. &instance->manchester_saved_state,
  626. NULL);
  627. manchester_advance(
  628. instance->manchester_saved_state,
  629. ManchesterEventLongHigh,
  630. &instance->manchester_saved_state,
  631. NULL);
  632. manchester_advance(
  633. instance->manchester_saved_state,
  634. ManchesterEventShortLow,
  635. &instance->manchester_saved_state,
  636. NULL);
  637. } else {
  638. instance->decoder.parser_step = SecPlus_v2DecoderStepReset;
  639. }
  640. } else {
  641. if(DURATION_DIFF(duration, subghz_protocol_secplus_v2_const.te_short) <
  642. subghz_protocol_secplus_v2_const.te_delta) {
  643. event = ManchesterEventShortHigh;
  644. } else if(
  645. DURATION_DIFF(duration, subghz_protocol_secplus_v2_const.te_long) <
  646. subghz_protocol_secplus_v2_const.te_delta) {
  647. event = ManchesterEventLongHigh;
  648. } else {
  649. instance->decoder.parser_step = SecPlus_v2DecoderStepReset;
  650. }
  651. }
  652. if(event != ManchesterEventReset) {
  653. bool data;
  654. bool data_ok = manchester_advance(
  655. instance->manchester_saved_state, event, &instance->manchester_saved_state, &data);
  656. if(data_ok) {
  657. instance->decoder.decode_data = (instance->decoder.decode_data << 1) | data;
  658. instance->decoder.decode_count_bit++;
  659. }
  660. }
  661. break;
  662. }
  663. }
  664. uint8_t subghz_protocol_decoder_secplus_v2_get_hash_data(void* context) {
  665. furi_assert(context);
  666. SubGhzProtocolDecoderSecPlus_v2* instance = context;
  667. return subghz_protocol_blocks_get_hash_data(
  668. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  669. }
  670. bool subghz_protocol_decoder_secplus_v2_serialize(
  671. void* context,
  672. FlipperFormat* flipper_format,
  673. SubGhzRadioPreset* preset) {
  674. furi_assert(context);
  675. SubGhzProtocolDecoderSecPlus_v2* instance = context;
  676. bool res = subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  677. uint8_t key_data[sizeof(uint64_t)] = {0};
  678. for(size_t i = 0; i < sizeof(uint64_t); i++) {
  679. key_data[sizeof(uint64_t) - i - 1] = (instance->secplus_packet_1 >> i * 8) & 0xFF;
  680. }
  681. if(res &&
  682. !flipper_format_write_hex(flipper_format, "Secplus_packet_1", key_data, sizeof(uint64_t))) {
  683. FURI_LOG_E(TAG, "Unable to add Secplus_packet_1");
  684. res = false;
  685. }
  686. return res;
  687. }
  688. bool subghz_protocol_decoder_secplus_v2_deserialize(void* context, FlipperFormat* flipper_format) {
  689. furi_assert(context);
  690. SubGhzProtocolDecoderSecPlus_v2* instance = context;
  691. bool res = false;
  692. do {
  693. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  694. FURI_LOG_E(TAG, "Deserialize error");
  695. break;
  696. }
  697. if(instance->generic.data_count_bit !=
  698. subghz_protocol_secplus_v2_const.min_count_bit_for_found) {
  699. FURI_LOG_E(TAG, "Wrong number of bits in key");
  700. break;
  701. }
  702. if(!flipper_format_rewind(flipper_format)) {
  703. FURI_LOG_E(TAG, "Rewind error");
  704. break;
  705. }
  706. uint8_t key_data[sizeof(uint64_t)] = {0};
  707. if(!flipper_format_read_hex(
  708. flipper_format, "Secplus_packet_1", key_data, sizeof(uint64_t))) {
  709. FURI_LOG_E(TAG, "Missing Secplus_packet_1");
  710. break;
  711. }
  712. for(uint8_t i = 0; i < sizeof(uint64_t); i++) {
  713. instance->secplus_packet_1 = instance->secplus_packet_1 << 8 | key_data[i];
  714. }
  715. res = true;
  716. } while(false);
  717. return res;
  718. }
  719. void subghz_protocol_decoder_secplus_v2_get_string(void* context, FuriString* output) {
  720. furi_assert(context);
  721. SubGhzProtocolDecoderSecPlus_v2* instance = context;
  722. subghz_protocol_secplus_v2_remote_controller(&instance->generic, instance->secplus_packet_1);
  723. furi_string_cat_printf(
  724. output,
  725. "%s %db\r\n"
  726. "Pk1:0x%lX%08lX\r\n"
  727. "Pk2:0x%lX%08lX\r\n"
  728. "Sn:0x%08lX Btn:0x%01X\r\n"
  729. "Cnt:0x%03lX\r\n",
  730. instance->generic.protocol_name,
  731. instance->generic.data_count_bit,
  732. (uint32_t)(instance->secplus_packet_1 >> 32),
  733. (uint32_t)instance->secplus_packet_1,
  734. (uint32_t)(instance->generic.data >> 32),
  735. (uint32_t)instance->generic.data,
  736. instance->generic.serial,
  737. instance->generic.btn,
  738. instance->generic.cnt);
  739. }