secplus_v2.c 28 KB

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