secplus_v2.c 16 KB

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