secplus_v2.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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_runing = 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. uint8_t key_data[sizeof(uint64_t)] = {0};
  454. if(!flipper_format_read_hex(
  455. flipper_format, "Secplus_packet_1", key_data, sizeof(uint64_t))) {
  456. FURI_LOG_E(TAG, "Secplus_packet_1");
  457. break;
  458. }
  459. for(uint8_t i = 0; i < sizeof(uint64_t); i++) {
  460. instance->secplus_packet_1 = instance->secplus_packet_1 << 8 | key_data[i];
  461. }
  462. subghz_protocol_secplus_v2_remote_controller(
  463. &instance->generic, instance->secplus_packet_1);
  464. subghz_protocol_secplus_v2_encode(instance);
  465. //optional parameter parameter
  466. flipper_format_read_uint32(
  467. flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
  468. subghz_protocol_encoder_secplus_v2_get_upload(instance);
  469. //update data
  470. for(size_t i = 0; i < sizeof(uint64_t); i++) {
  471. key_data[sizeof(uint64_t) - i - 1] = (instance->generic.data >> i * 8) & 0xFF;
  472. }
  473. if(!flipper_format_update_hex(flipper_format, "Key", key_data, sizeof(uint64_t))) {
  474. FURI_LOG_E(TAG, "Unable to add Key");
  475. break;
  476. }
  477. for(size_t i = 0; i < sizeof(uint64_t); i++) {
  478. key_data[sizeof(uint64_t) - i - 1] = (instance->secplus_packet_1 >> i * 8) & 0xFF;
  479. }
  480. if(!flipper_format_update_hex(
  481. flipper_format, "Secplus_packet_1", key_data, sizeof(uint64_t))) {
  482. FURI_LOG_E(TAG, "Unable to add Secplus_packet_1");
  483. break;
  484. }
  485. instance->encoder.is_runing = true;
  486. res = true;
  487. } while(false);
  488. return res;
  489. }
  490. void subghz_protocol_encoder_secplus_v2_stop(void* context) {
  491. SubGhzProtocolEncoderSecPlus_v2* instance = context;
  492. instance->encoder.is_runing = false;
  493. }
  494. LevelDuration subghz_protocol_encoder_secplus_v2_yield(void* context) {
  495. SubGhzProtocolEncoderSecPlus_v2* instance = context;
  496. if(instance->encoder.repeat == 0 || !instance->encoder.is_runing) {
  497. instance->encoder.is_runing = false;
  498. return level_duration_reset();
  499. }
  500. LevelDuration ret = instance->encoder.upload[instance->encoder.front];
  501. if(++instance->encoder.front == instance->encoder.size_upload) {
  502. instance->encoder.repeat--;
  503. instance->encoder.front = 0;
  504. }
  505. return ret;
  506. }
  507. bool subghz_protocol_secplus_v2_create_data(
  508. void* context,
  509. FlipperFormat* flipper_format,
  510. uint32_t serial,
  511. uint8_t btn,
  512. uint32_t cnt,
  513. uint32_t frequency,
  514. FuriHalSubGhzPreset preset) {
  515. furi_assert(context);
  516. SubGhzProtocolEncoderSecPlus_v2* instance = context;
  517. instance->generic.serial = serial;
  518. instance->generic.cnt = cnt;
  519. instance->generic.btn = btn;
  520. instance->generic.data_count_bit =
  521. (uint8_t)subghz_protocol_secplus_v2_const.min_count_bit_for_found;
  522. subghz_protocol_secplus_v2_encode(instance);
  523. bool res =
  524. subghz_block_generic_serialize(&instance->generic, flipper_format, frequency, preset);
  525. uint8_t key_data[sizeof(uint64_t)] = {0};
  526. for(size_t i = 0; i < sizeof(uint64_t); i++) {
  527. key_data[sizeof(uint64_t) - i - 1] = (instance->secplus_packet_1 >> i * 8) & 0xFF;
  528. }
  529. if(res &&
  530. !flipper_format_write_hex(flipper_format, "Secplus_packet_1", key_data, sizeof(uint64_t))) {
  531. FURI_LOG_E(TAG, "Unable to add Secplus_packet_1");
  532. res = false;
  533. }
  534. return res;
  535. }
  536. void* subghz_protocol_decoder_secplus_v2_alloc(SubGhzEnvironment* environment) {
  537. UNUSED(environment);
  538. SubGhzProtocolDecoderSecPlus_v2* instance = malloc(sizeof(SubGhzProtocolDecoderSecPlus_v2));
  539. instance->base.protocol = &subghz_protocol_secplus_v2;
  540. instance->generic.protocol_name = instance->base.protocol->name;
  541. return instance;
  542. }
  543. void subghz_protocol_decoder_secplus_v2_free(void* context) {
  544. furi_assert(context);
  545. SubGhzProtocolDecoderSecPlus_v2* instance = context;
  546. free(instance);
  547. }
  548. void subghz_protocol_decoder_secplus_v2_reset(void* context) {
  549. furi_assert(context);
  550. // SubGhzProtocolDecoderSecPlus_v2* instance = context;
  551. // does not reset the decoder because you need to get 2 parts of the package
  552. }
  553. static bool subghz_protocol_secplus_v2_check_packet(SubGhzProtocolDecoderSecPlus_v2* instance) {
  554. if((instance->decoder.decode_data & SECPLUS_V2_HEADER_MASK) == SECPLUS_V2_HEADER) {
  555. if((instance->decoder.decode_data & SECPLUS_V2_PACKET_MASK) == SECPLUS_V2_PACKET_1) {
  556. instance->secplus_packet_1 = instance->decoder.decode_data;
  557. } else if(
  558. ((instance->decoder.decode_data & SECPLUS_V2_PACKET_MASK) == SECPLUS_V2_PACKET_2) &&
  559. (instance->secplus_packet_1)) {
  560. return true;
  561. }
  562. }
  563. return false;
  564. }
  565. void subghz_protocol_decoder_secplus_v2_feed(void* context, bool level, uint32_t duration) {
  566. furi_assert(context);
  567. SubGhzProtocolDecoderSecPlus_v2* instance = context;
  568. ManchesterEvent event = ManchesterEventReset;
  569. switch(instance->decoder.parser_step) {
  570. case SecPlus_v2DecoderStepReset:
  571. if((!level) && (DURATION_DIFF(duration, subghz_protocol_secplus_v2_const.te_long * 130) <
  572. subghz_protocol_secplus_v2_const.te_delta * 100)) {
  573. //Found header Security+ 2.0
  574. instance->decoder.parser_step = SecPlus_v2DecoderStepDecoderData;
  575. instance->decoder.decode_data = 0;
  576. instance->decoder.decode_count_bit = 0;
  577. instance->secplus_packet_1 = 0;
  578. manchester_advance(
  579. instance->manchester_saved_state,
  580. ManchesterEventReset,
  581. &instance->manchester_saved_state,
  582. NULL);
  583. manchester_advance(
  584. instance->manchester_saved_state,
  585. ManchesterEventLongHigh,
  586. &instance->manchester_saved_state,
  587. NULL);
  588. manchester_advance(
  589. instance->manchester_saved_state,
  590. ManchesterEventShortLow,
  591. &instance->manchester_saved_state,
  592. NULL);
  593. }
  594. break;
  595. case SecPlus_v2DecoderStepDecoderData:
  596. if(!level) {
  597. if(DURATION_DIFF(duration, subghz_protocol_secplus_v2_const.te_short) <
  598. subghz_protocol_secplus_v2_const.te_delta) {
  599. event = ManchesterEventShortLow;
  600. } else if(
  601. DURATION_DIFF(duration, subghz_protocol_secplus_v2_const.te_long) <
  602. subghz_protocol_secplus_v2_const.te_delta) {
  603. event = ManchesterEventLongLow;
  604. } else if(
  605. duration >= (uint32_t)(subghz_protocol_secplus_v2_const.te_long * 2 +
  606. subghz_protocol_secplus_v2_const.te_delta)) {
  607. if(instance->decoder.decode_count_bit >=
  608. subghz_protocol_secplus_v2_const.min_count_bit_for_found) {
  609. instance->generic.data = instance->decoder.decode_data;
  610. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  611. if(subghz_protocol_secplus_v2_check_packet(instance)) {
  612. if(instance->base.callback)
  613. instance->base.callback(&instance->base, instance->base.context);
  614. instance->decoder.parser_step = SecPlus_v2DecoderStepReset;
  615. }
  616. }
  617. instance->decoder.decode_data = 0;
  618. instance->decoder.decode_count_bit = 0;
  619. manchester_advance(
  620. instance->manchester_saved_state,
  621. ManchesterEventReset,
  622. &instance->manchester_saved_state,
  623. NULL);
  624. manchester_advance(
  625. instance->manchester_saved_state,
  626. ManchesterEventLongHigh,
  627. &instance->manchester_saved_state,
  628. NULL);
  629. manchester_advance(
  630. instance->manchester_saved_state,
  631. ManchesterEventShortLow,
  632. &instance->manchester_saved_state,
  633. NULL);
  634. } else {
  635. instance->decoder.parser_step = SecPlus_v2DecoderStepReset;
  636. }
  637. } else {
  638. if(DURATION_DIFF(duration, subghz_protocol_secplus_v2_const.te_short) <
  639. subghz_protocol_secplus_v2_const.te_delta) {
  640. event = ManchesterEventShortHigh;
  641. } else if(
  642. DURATION_DIFF(duration, subghz_protocol_secplus_v2_const.te_long) <
  643. subghz_protocol_secplus_v2_const.te_delta) {
  644. event = ManchesterEventLongHigh;
  645. } else {
  646. instance->decoder.parser_step = SecPlus_v2DecoderStepReset;
  647. }
  648. }
  649. if(event != ManchesterEventReset) {
  650. bool data;
  651. bool data_ok = manchester_advance(
  652. instance->manchester_saved_state, event, &instance->manchester_saved_state, &data);
  653. if(data_ok) {
  654. instance->decoder.decode_data = (instance->decoder.decode_data << 1) | data;
  655. instance->decoder.decode_count_bit++;
  656. }
  657. }
  658. break;
  659. }
  660. }
  661. uint8_t subghz_protocol_decoder_secplus_v2_get_hash_data(void* context) {
  662. furi_assert(context);
  663. SubGhzProtocolDecoderSecPlus_v2* instance = context;
  664. return subghz_protocol_blocks_get_hash_data(
  665. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  666. }
  667. bool subghz_protocol_decoder_secplus_v2_serialize(
  668. void* context,
  669. FlipperFormat* flipper_format,
  670. uint32_t frequency,
  671. FuriHalSubGhzPreset preset) {
  672. furi_assert(context);
  673. SubGhzProtocolDecoderSecPlus_v2* instance = context;
  674. bool res =
  675. subghz_block_generic_serialize(&instance->generic, flipper_format, frequency, preset);
  676. uint8_t key_data[sizeof(uint64_t)] = {0};
  677. for(size_t i = 0; i < sizeof(uint64_t); i++) {
  678. key_data[sizeof(uint64_t) - i - 1] = (instance->secplus_packet_1 >> i * 8) & 0xFF;
  679. }
  680. if(res &&
  681. !flipper_format_write_hex(flipper_format, "Secplus_packet_1", key_data, sizeof(uint64_t))) {
  682. FURI_LOG_E(TAG, "Unable to add Secplus_packet_1");
  683. res = false;
  684. }
  685. return res;
  686. }
  687. bool subghz_protocol_decoder_secplus_v2_deserialize(void* context, FlipperFormat* flipper_format) {
  688. furi_assert(context);
  689. SubGhzProtocolDecoderSecPlus_v2* instance = context;
  690. bool res = false;
  691. do {
  692. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  693. FURI_LOG_E(TAG, "Deserialize error");
  694. break;
  695. }
  696. if(!flipper_format_rewind(flipper_format)) {
  697. FURI_LOG_E(TAG, "Rewind error");
  698. break;
  699. }
  700. uint8_t key_data[sizeof(uint64_t)] = {0};
  701. if(!flipper_format_read_hex(
  702. flipper_format, "Secplus_packet_1", key_data, sizeof(uint64_t))) {
  703. FURI_LOG_E(TAG, "Missing Secplus_packet_1");
  704. break;
  705. }
  706. for(uint8_t i = 0; i < sizeof(uint64_t); i++) {
  707. instance->secplus_packet_1 = instance->secplus_packet_1 << 8 | key_data[i];
  708. }
  709. res = true;
  710. } while(false);
  711. return res;
  712. }
  713. void subghz_protocol_decoder_secplus_v2_get_string(void* context, string_t output) {
  714. furi_assert(context);
  715. SubGhzProtocolDecoderSecPlus_v2* instance = context;
  716. subghz_protocol_secplus_v2_remote_controller(&instance->generic, instance->secplus_packet_1);
  717. string_cat_printf(
  718. output,
  719. "%s %db\r\n"
  720. "Pk1:0x%lX%08lX\r\n"
  721. "Pk2:0x%lX%08lX\r\n"
  722. "Sn:0x%08lX Btn:0x%01X\r\n"
  723. "Cnt:0x%03X\r\n",
  724. instance->generic.protocol_name,
  725. instance->generic.data_count_bit,
  726. (uint32_t)(instance->secplus_packet_1 >> 32),
  727. (uint32_t)instance->secplus_packet_1,
  728. (uint32_t)(instance->generic.data >> 32),
  729. (uint32_t)instance->generic.data,
  730. instance->generic.serial,
  731. instance->generic.btn,
  732. instance->generic.cnt);
  733. }