secplus_v2.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. #include "secplus_v2.h"
  2. #include <lib/toolbox/manchester_decoder.h>
  3. #include "../blocks/const.h"
  4. #include "../blocks/decoder.h"
  5. #include "../blocks/encoder.h"
  6. #include "../blocks/generic.h"
  7. #include "../blocks/math.h"
  8. /*
  9. * Help
  10. * https://github.com/argilo/secplus
  11. * https://github.com/merbanan/rtl_433/blob/master/src/devices/secplus_v2.c
  12. */
  13. #define TAG "SubGhzProtocoSecPlus_v2"
  14. #define SECPLUS_V2_HEADER 0x3C0000000000
  15. #define SECPLUS_V2_HEADER_MASK 0xFFFF3C0000000000
  16. #define SECPLUS_V2_PACKET_1 0x000000000000
  17. #define SECPLUS_V2_PACKET_2 0x010000000000
  18. #define SECPLUS_V2_PACKET_MASK 0x30000000000
  19. static const SubGhzBlockConst subghz_protocol_secplus_v2_const = {
  20. .te_short = 250,
  21. .te_long = 500,
  22. .te_delta = 110,
  23. .min_count_bit_for_found = 62,
  24. };
  25. struct SubGhzProtocolDecoderSecPlus_v2 {
  26. SubGhzProtocolDecoderBase base;
  27. SubGhzBlockDecoder decoder;
  28. SubGhzBlockGeneric generic;
  29. ManchesterState manchester_saved_state;
  30. uint64_t secplus_packet_1;
  31. };
  32. struct SubGhzProtocolEncoderSecPlus_v2 {
  33. SubGhzProtocolEncoderBase base;
  34. SubGhzProtocolBlockEncoder encoder;
  35. SubGhzBlockGeneric generic;
  36. };
  37. typedef enum {
  38. SecPlus_v2DecoderStepReset = 0,
  39. SecPlus_v2DecoderStepDecoderData,
  40. } SecPlus_v2DecoderStep;
  41. const SubGhzProtocolDecoder subghz_protocol_secplus_v2_decoder = {
  42. .alloc = subghz_protocol_decoder_secplus_v2_alloc,
  43. .free = subghz_protocol_decoder_secplus_v2_free,
  44. .feed = subghz_protocol_decoder_secplus_v2_feed,
  45. .reset = subghz_protocol_decoder_secplus_v2_reset,
  46. .get_hash_data = subghz_protocol_decoder_secplus_v2_get_hash_data,
  47. .serialize = subghz_protocol_decoder_secplus_v2_serialize,
  48. .deserialize = subghz_protocol_decoder_secplus_v2_deserialize,
  49. .get_string = subghz_protocol_decoder_secplus_v2_get_string,
  50. };
  51. const SubGhzProtocolEncoder subghz_protocol_secplus_v2_encoder = {
  52. .alloc = NULL,
  53. .free = NULL,
  54. .deserialize = NULL,
  55. .stop = NULL,
  56. .yield = NULL,
  57. };
  58. const SubGhzProtocol subghz_protocol_secplus_v2 = {
  59. .name = SUBGHZ_PROTOCOL_SECPLUS_V2_NAME,
  60. .type = SubGhzProtocolTypeDynamic,
  61. .flag = SubGhzProtocolFlag_315 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
  62. .decoder = &subghz_protocol_secplus_v2_decoder,
  63. .encoder = &subghz_protocol_secplus_v2_encoder,
  64. };
  65. void* subghz_protocol_decoder_secplus_v2_alloc(SubGhzEnvironment* environment) {
  66. UNUSED(environment);
  67. SubGhzProtocolDecoderSecPlus_v2* instance = malloc(sizeof(SubGhzProtocolDecoderSecPlus_v2));
  68. instance->base.protocol = &subghz_protocol_secplus_v2;
  69. instance->generic.protocol_name = instance->base.protocol->name;
  70. return instance;
  71. }
  72. void subghz_protocol_decoder_secplus_v2_free(void* context) {
  73. furi_assert(context);
  74. SubGhzProtocolDecoderSecPlus_v2* instance = context;
  75. free(instance);
  76. }
  77. void subghz_protocol_decoder_secplus_v2_reset(void* context) {
  78. furi_assert(context);
  79. // SubGhzProtocolDecoderSecPlus_v2* instance = context;
  80. // does not reset the decoder because you need to get 2 parts of the package
  81. }
  82. static bool subghz_protocol_secplus_v2_check_packet(SubGhzProtocolDecoderSecPlus_v2* instance) {
  83. if((instance->decoder.decode_data & SECPLUS_V2_HEADER_MASK) == SECPLUS_V2_HEADER) {
  84. if((instance->decoder.decode_data & SECPLUS_V2_PACKET_MASK) == SECPLUS_V2_PACKET_1) {
  85. instance->secplus_packet_1 = instance->decoder.decode_data;
  86. } else if(
  87. ((instance->decoder.decode_data & SECPLUS_V2_PACKET_MASK) == SECPLUS_V2_PACKET_2) &&
  88. (instance->secplus_packet_1)) {
  89. return true;
  90. }
  91. }
  92. return false;
  93. }
  94. void subghz_protocol_decoder_secplus_v2_feed(void* context, bool level, uint32_t duration) {
  95. furi_assert(context);
  96. SubGhzProtocolDecoderSecPlus_v2* instance = context;
  97. ManchesterEvent event = ManchesterEventReset;
  98. switch(instance->decoder.parser_step) {
  99. case SecPlus_v2DecoderStepReset:
  100. if((!level) && (DURATION_DIFF(duration, subghz_protocol_secplus_v2_const.te_long * 130) <
  101. subghz_protocol_secplus_v2_const.te_delta * 100)) {
  102. //Found header Security+ 2.0
  103. instance->decoder.parser_step = SecPlus_v2DecoderStepDecoderData;
  104. instance->decoder.decode_data = 0;
  105. instance->decoder.decode_count_bit = 0;
  106. instance->secplus_packet_1 = 0;
  107. manchester_advance(
  108. instance->manchester_saved_state,
  109. ManchesterEventReset,
  110. &instance->manchester_saved_state,
  111. NULL);
  112. manchester_advance(
  113. instance->manchester_saved_state,
  114. ManchesterEventLongHigh,
  115. &instance->manchester_saved_state,
  116. NULL);
  117. manchester_advance(
  118. instance->manchester_saved_state,
  119. ManchesterEventShortLow,
  120. &instance->manchester_saved_state,
  121. NULL);
  122. }
  123. break;
  124. case SecPlus_v2DecoderStepDecoderData:
  125. if(!level) {
  126. if(DURATION_DIFF(duration, subghz_protocol_secplus_v2_const.te_short) <
  127. subghz_protocol_secplus_v2_const.te_delta) {
  128. event = ManchesterEventShortLow;
  129. } else if(
  130. DURATION_DIFF(duration, subghz_protocol_secplus_v2_const.te_long) <
  131. subghz_protocol_secplus_v2_const.te_delta) {
  132. event = ManchesterEventLongLow;
  133. } else if(
  134. duration >= (uint32_t)(subghz_protocol_secplus_v2_const.te_long * 2 +
  135. subghz_protocol_secplus_v2_const.te_delta)) {
  136. if(instance->decoder.decode_count_bit >=
  137. subghz_protocol_secplus_v2_const.min_count_bit_for_found) {
  138. instance->generic.data = instance->decoder.decode_data;
  139. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  140. if(subghz_protocol_secplus_v2_check_packet(instance)) {
  141. if(instance->base.callback)
  142. instance->base.callback(&instance->base, instance->base.context);
  143. instance->decoder.parser_step = SecPlus_v2DecoderStepReset;
  144. }
  145. }
  146. instance->decoder.decode_data = 0;
  147. instance->decoder.decode_count_bit = 0;
  148. manchester_advance(
  149. instance->manchester_saved_state,
  150. ManchesterEventReset,
  151. &instance->manchester_saved_state,
  152. NULL);
  153. manchester_advance(
  154. instance->manchester_saved_state,
  155. ManchesterEventLongHigh,
  156. &instance->manchester_saved_state,
  157. NULL);
  158. manchester_advance(
  159. instance->manchester_saved_state,
  160. ManchesterEventShortLow,
  161. &instance->manchester_saved_state,
  162. NULL);
  163. } else {
  164. instance->decoder.parser_step = SecPlus_v2DecoderStepReset;
  165. }
  166. } else {
  167. if(DURATION_DIFF(duration, subghz_protocol_secplus_v2_const.te_short) <
  168. subghz_protocol_secplus_v2_const.te_delta) {
  169. event = ManchesterEventShortHigh;
  170. } else if(
  171. DURATION_DIFF(duration, subghz_protocol_secplus_v2_const.te_long) <
  172. subghz_protocol_secplus_v2_const.te_delta) {
  173. event = ManchesterEventLongHigh;
  174. } else {
  175. instance->decoder.parser_step = SecPlus_v2DecoderStepReset;
  176. }
  177. }
  178. if(event != ManchesterEventReset) {
  179. bool data;
  180. bool data_ok = manchester_advance(
  181. instance->manchester_saved_state, event, &instance->manchester_saved_state, &data);
  182. if(data_ok) {
  183. instance->decoder.decode_data = (instance->decoder.decode_data << 1) | data;
  184. instance->decoder.decode_count_bit++;
  185. }
  186. }
  187. break;
  188. }
  189. }
  190. /**
  191. * Security+ 2.0 half-message decoding
  192. * @param data data
  193. * @param roll_array[] return roll_array part
  194. * @param fixed[] return fixed part
  195. * @return true On success
  196. */
  197. static bool
  198. subghz_protocol_secplus_v2_decode_half(uint64_t data, uint8_t roll_array[], uint32_t* fixed) {
  199. uint8_t order = (data >> 34) & 0x0f;
  200. uint8_t invert = (data >> 30) & 0x0f;
  201. uint16_t p[3] = {0};
  202. for(int i = 29; i >= 0; i -= 3) {
  203. p[0] = p[0] << 1 | bit_read(data, i);
  204. p[1] = p[1] << 1 | bit_read(data, i - 1);
  205. p[2] = p[2] << 1 | bit_read(data, i - 2);
  206. }
  207. // selectively invert buffers
  208. switch(invert) {
  209. case 0x00: // 0b0000 (True, True, False),
  210. p[0] = ~p[0] & 0x03FF;
  211. p[1] = ~p[1] & 0x03FF;
  212. break;
  213. case 0x01: // 0b0001 (False, True, False),
  214. p[1] = ~p[1] & 0x03FF;
  215. break;
  216. case 0x02: // 0b0010 (False, False, True),
  217. p[2] = ~p[2] & 0x03FF;
  218. break;
  219. case 0x04: // 0b0100 (True, True, True),
  220. p[0] = ~p[0] & 0x03FF;
  221. p[1] = ~p[1] & 0x03FF;
  222. p[2] = ~p[2] & 0x03FF;
  223. break;
  224. case 0x05: // 0b0101 (True, False, True),
  225. case 0x0a: // 0b1010 (True, False, True),
  226. p[0] = ~p[0] & 0x03FF;
  227. p[2] = ~p[2] & 0x03FF;
  228. break;
  229. case 0x06: // 0b0110 (False, True, True),
  230. p[1] = ~p[1] & 0x03FF;
  231. p[2] = ~p[2] & 0x03FF;
  232. break;
  233. case 0x08: // 0b1000 (True, False, False),
  234. p[0] = ~p[0] & 0x03FF;
  235. break;
  236. case 0x09: // 0b1001 (False, False, False),
  237. break;
  238. default:
  239. FURI_LOG_E(TAG, "Invert FAIL");
  240. return false;
  241. }
  242. uint16_t a = p[0], b = p[1], c = p[2];
  243. // selectively reorder buffers
  244. switch(order) {
  245. case 0x06: // 0b0110 2, 1, 0],
  246. case 0x09: // 0b1001 2, 1, 0],
  247. p[2] = a;
  248. p[1] = b;
  249. p[0] = c;
  250. break;
  251. case 0x08: // 0b1000 1, 2, 0],
  252. case 0x04: // 0b0100 1, 2, 0],
  253. p[1] = a;
  254. p[2] = b;
  255. p[0] = c;
  256. break;
  257. case 0x01: // 0b0001 2, 0, 1],
  258. p[2] = a;
  259. p[0] = b;
  260. p[1] = c;
  261. break;
  262. case 0x00: // 0b0000 0, 2, 1],
  263. p[0] = a;
  264. p[2] = b;
  265. p[1] = c;
  266. break;
  267. case 0x05: // 0b0101 1, 0, 2],
  268. p[1] = a;
  269. p[0] = b;
  270. p[2] = c;
  271. break;
  272. case 0x02: // 0b0010 0, 1, 2],
  273. case 0x0A: // 0b1010 0, 1, 2],
  274. p[0] = a;
  275. p[1] = b;
  276. p[2] = c;
  277. break;
  278. default:
  279. FURI_LOG_E(TAG, "Order FAIL");
  280. return false;
  281. }
  282. data = order << 4 | invert;
  283. int k = 0;
  284. for(int i = 6; i >= 0; i -= 2) {
  285. roll_array[k++] = (data >> i) & 0x03;
  286. if(roll_array[k] == 3) {
  287. FURI_LOG_E(TAG, "Roll_Array FAIL");
  288. return false;
  289. }
  290. }
  291. for(int i = 8; i >= 0; i -= 2) {
  292. roll_array[k++] = (p[2] >> i) & 0x03;
  293. if(roll_array[k] == 3) {
  294. FURI_LOG_E(TAG, "Roll_Array FAIL");
  295. return false;
  296. }
  297. }
  298. fixed[0] = p[0] << 10 | p[1];
  299. return true;
  300. }
  301. /**
  302. * Analysis of received data
  303. * @param instance Pointer to a SubGhzBlockGeneric* instance
  304. * @param packet_1 first part of the message
  305. */
  306. static void
  307. subghz_protocol_secplus_v2_remote_controller(SubGhzBlockGeneric* instance, uint64_t packet_1) {
  308. uint32_t fixed_1[1];
  309. uint8_t roll_1[9] = {0};
  310. uint32_t fixed_2[1];
  311. uint8_t roll_2[9] = {0};
  312. uint8_t rolling_digits[18] = {0};
  313. if(subghz_protocol_secplus_v2_decode_half(packet_1, roll_1, fixed_1) &&
  314. subghz_protocol_secplus_v2_decode_half(instance->data, roll_2, fixed_2)) {
  315. rolling_digits[0] = roll_2[8];
  316. rolling_digits[1] = roll_1[8];
  317. rolling_digits[2] = roll_2[4];
  318. rolling_digits[3] = roll_2[5];
  319. rolling_digits[4] = roll_2[6];
  320. rolling_digits[5] = roll_2[7];
  321. rolling_digits[6] = roll_1[4];
  322. rolling_digits[7] = roll_1[5];
  323. rolling_digits[8] = roll_1[6];
  324. rolling_digits[9] = roll_1[7];
  325. rolling_digits[10] = roll_2[0];
  326. rolling_digits[11] = roll_2[1];
  327. rolling_digits[12] = roll_2[2];
  328. rolling_digits[13] = roll_2[3];
  329. rolling_digits[14] = roll_1[0];
  330. rolling_digits[15] = roll_1[1];
  331. rolling_digits[16] = roll_1[2];
  332. rolling_digits[17] = roll_1[3];
  333. uint32_t rolling = 0;
  334. for(int i = 0; i < 18; i++) {
  335. rolling = (rolling * 3) + rolling_digits[i];
  336. }
  337. // Max value = 2^28 (268435456)
  338. if(rolling >= 0x10000000) {
  339. FURI_LOG_E(TAG, "Rolling FAIL");
  340. instance->cnt = 0;
  341. instance->btn = 0;
  342. instance->serial = 0;
  343. } else {
  344. instance->cnt = subghz_protocol_blocks_reverse_key(rolling, 28);
  345. instance->btn = fixed_1[0] >> 12;
  346. instance->serial = fixed_1[0] << 20 | fixed_2[0];
  347. }
  348. } else {
  349. instance->cnt = 0;
  350. instance->btn = 0;
  351. instance->serial = 0;
  352. }
  353. }
  354. uint8_t subghz_protocol_decoder_secplus_v2_get_hash_data(void* context) {
  355. furi_assert(context);
  356. SubGhzProtocolDecoderSecPlus_v2* instance = context;
  357. return subghz_protocol_blocks_get_hash_data(
  358. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  359. }
  360. bool subghz_protocol_decoder_secplus_v2_serialize(
  361. void* context,
  362. FlipperFormat* flipper_format,
  363. uint32_t frequency,
  364. FuriHalSubGhzPreset preset) {
  365. furi_assert(context);
  366. SubGhzProtocolDecoderSecPlus_v2* instance = context;
  367. bool res =
  368. subghz_block_generic_serialize(&instance->generic, flipper_format, frequency, preset);
  369. uint8_t key_data[sizeof(uint64_t)] = {0};
  370. for(size_t i = 0; i < sizeof(uint64_t); i++) {
  371. key_data[sizeof(uint64_t) - i - 1] = (instance->secplus_packet_1 >> i * 8) & 0xFF;
  372. }
  373. if(res &&
  374. !flipper_format_write_hex(flipper_format, "Secplus_packet_1", key_data, sizeof(uint64_t))) {
  375. FURI_LOG_E(TAG, "Unable to add Secplus_packet_1");
  376. res = false;
  377. }
  378. return res;
  379. }
  380. bool subghz_protocol_decoder_secplus_v2_deserialize(void* context, FlipperFormat* flipper_format) {
  381. furi_assert(context);
  382. SubGhzProtocolDecoderSecPlus_v2* instance = context;
  383. bool res = false;
  384. do {
  385. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  386. FURI_LOG_E(TAG, "Deserialize error");
  387. break;
  388. }
  389. if(!flipper_format_rewind(flipper_format)) {
  390. FURI_LOG_E(TAG, "Rewind error");
  391. break;
  392. }
  393. uint8_t key_data[sizeof(uint64_t)] = {0};
  394. if(!flipper_format_read_hex(
  395. flipper_format, "Secplus_packet_1", key_data, sizeof(uint64_t))) {
  396. FURI_LOG_E(TAG, "Missing Secplus_packet_1");
  397. break;
  398. }
  399. for(uint8_t i = 0; i < sizeof(uint64_t); i++) {
  400. instance->secplus_packet_1 = instance->secplus_packet_1 << 8 | key_data[i];
  401. }
  402. res = true;
  403. } while(false);
  404. return res;
  405. }
  406. void subghz_protocol_decoder_secplus_v2_get_string(void* context, string_t output) {
  407. furi_assert(context);
  408. SubGhzProtocolDecoderSecPlus_v2* instance = context;
  409. subghz_protocol_secplus_v2_remote_controller(&instance->generic, instance->secplus_packet_1);
  410. string_cat_printf(
  411. output,
  412. "%s %db\r\n"
  413. "Pk1:0x%lX%08lX\r\n"
  414. "Pk2:0x%lX%08lX\r\n"
  415. "Sn:0x%08lX Btn:0x%01X\r\n"
  416. "Cnt:0x%03X\r\n",
  417. instance->generic.protocol_name,
  418. instance->generic.data_count_bit,
  419. (uint32_t)(instance->secplus_packet_1 >> 32),
  420. (uint32_t)instance->secplus_packet_1,
  421. (uint32_t)(instance->generic.data >> 32),
  422. (uint32_t)instance->generic.data,
  423. instance->generic.serial,
  424. instance->generic.btn,
  425. instance->generic.cnt);
  426. }