alutech_at_4n.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. #include "alutech_at_4n.h"
  2. #include "../blocks/const.h"
  3. #include "../blocks/decoder.h"
  4. #include "../blocks/encoder.h"
  5. #include "../blocks/generic.h"
  6. #include "../blocks/math.h"
  7. #define TAG "SubGhzProtocoAlutech_at_4n"
  8. #define SUBGHZ_NO_ALUTECH_AT_4N_RAINBOW_TABLE 0xFFFFFFFF
  9. static const SubGhzBlockConst subghz_protocol_alutech_at_4n_const = {
  10. .te_short = 400,
  11. .te_long = 800,
  12. .te_delta = 140,
  13. .min_count_bit_for_found = 72,
  14. };
  15. struct SubGhzProtocolDecoderAlutech_at_4n {
  16. SubGhzProtocolDecoderBase base;
  17. SubGhzBlockDecoder decoder;
  18. SubGhzBlockGeneric generic;
  19. uint64_t data;
  20. uint32_t crc;
  21. uint16_t header_count;
  22. const char* alutech_at_4n_rainbow_table_file_name;
  23. };
  24. struct SubGhzProtocolEncoderAlutech_at_4n {
  25. SubGhzProtocolEncoderBase base;
  26. SubGhzProtocolBlockEncoder encoder;
  27. SubGhzBlockGeneric generic;
  28. };
  29. typedef enum {
  30. Alutech_at_4nDecoderStepReset = 0,
  31. Alutech_at_4nDecoderStepCheckPreambula,
  32. Alutech_at_4nDecoderStepSaveDuration,
  33. Alutech_at_4nDecoderStepCheckDuration,
  34. } Alutech_at_4nDecoderStep;
  35. const SubGhzProtocolDecoder subghz_protocol_alutech_at_4n_decoder = {
  36. .alloc = subghz_protocol_decoder_alutech_at_4n_alloc,
  37. .free = subghz_protocol_decoder_alutech_at_4n_free,
  38. .feed = subghz_protocol_decoder_alutech_at_4n_feed,
  39. .reset = subghz_protocol_decoder_alutech_at_4n_reset,
  40. .get_hash_data = subghz_protocol_decoder_alutech_at_4n_get_hash_data,
  41. .serialize = subghz_protocol_decoder_alutech_at_4n_serialize,
  42. .deserialize = subghz_protocol_decoder_alutech_at_4n_deserialize,
  43. .get_string = subghz_protocol_decoder_alutech_at_4n_get_string,
  44. };
  45. const SubGhzProtocolEncoder subghz_protocol_alutech_at_4n_encoder = {
  46. .alloc = NULL,
  47. .free = NULL,
  48. .deserialize = NULL,
  49. .stop = NULL,
  50. .yield = NULL,
  51. };
  52. const SubGhzProtocol subghz_protocol_alutech_at_4n = {
  53. .name = SUBGHZ_PROTOCOL_ALUTECH_AT_4N_NAME,
  54. .type = SubGhzProtocolTypeDynamic,
  55. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
  56. .decoder = &subghz_protocol_alutech_at_4n_decoder,
  57. .encoder = &subghz_protocol_alutech_at_4n_encoder,
  58. };
  59. /**
  60. * Read bytes from rainbow table
  61. * @param file_name Full path to rainbow table the file
  62. * @param number_alutech_at_4n_magic_data number in the array
  63. * @return alutech_at_4n_magic_data
  64. */
  65. static uint32_t subghz_protocol_alutech_at_4n_get_magic_data_in_file(
  66. const char* file_name,
  67. uint8_t number_alutech_at_4n_magic_data) {
  68. if(!strcmp(file_name, "")) return SUBGHZ_NO_ALUTECH_AT_4N_RAINBOW_TABLE;
  69. uint8_t buffer[sizeof(uint32_t)] = {0};
  70. uint32_t address = number_alutech_at_4n_magic_data * sizeof(uint32_t);
  71. uint32_t alutech_at_4n_magic_data = 0;
  72. if(subghz_keystore_raw_get_data(file_name, address, buffer, sizeof(uint32_t))) {
  73. for(size_t i = 0; i < sizeof(uint32_t); i++) {
  74. alutech_at_4n_magic_data = (alutech_at_4n_magic_data << 8) | buffer[i];
  75. }
  76. } else {
  77. alutech_at_4n_magic_data = SUBGHZ_NO_ALUTECH_AT_4N_RAINBOW_TABLE;
  78. }
  79. return alutech_at_4n_magic_data;
  80. }
  81. static uint8_t subghz_protocol_alutech_at_4n_crc(uint64_t data) {
  82. uint8_t* p = (uint8_t*)&data;
  83. uint8_t crc = 0xff;
  84. for(uint8_t y = 0; y < 8; y++) {
  85. crc = crc ^ p[y];
  86. for(uint8_t i = 0; i < 8; i++) {
  87. if((crc & 0x80) != 0) {
  88. crc <<= 1;
  89. crc ^= 0x31;
  90. } else {
  91. crc <<= 1;
  92. }
  93. }
  94. }
  95. return crc;
  96. }
  97. static uint8_t subghz_protocol_alutech_at_4n_decrypt_data_crc(uint8_t data) {
  98. uint8_t crc = data;
  99. for(uint8_t i = 0; i < 8; i++) {
  100. if((crc & 0x80) != 0) {
  101. crc <<= 1;
  102. crc ^= 0x31;
  103. } else {
  104. crc <<= 1;
  105. }
  106. }
  107. return ~crc;
  108. }
  109. static uint64_t subghz_protocol_alutech_at_4n_decrypt(uint64_t data, const char* file_name) {
  110. uint8_t* p = (uint8_t*)&data;
  111. uint32_t data1 = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
  112. uint32_t data2 = p[4] << 24 | p[5] << 16 | p[6] << 8 | p[7];
  113. uint32_t data3 = 0;
  114. uint32_t magic_data[] = {
  115. subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 0),
  116. subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 1),
  117. subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 2),
  118. subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 3),
  119. subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 4),
  120. subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 5)};
  121. uint32_t i = magic_data[0];
  122. do {
  123. data2 = data2 -
  124. ((magic_data[1] + (data1 << 4)) ^ ((magic_data[2] + (data1 >> 5)) ^ (data1 + i)));
  125. data3 = data2 + i;
  126. i += magic_data[3];
  127. data1 =
  128. data1 - ((magic_data[4] + (data2 << 4)) ^ ((magic_data[5] + (data2 >> 5)) ^ data3));
  129. } while(i != 0);
  130. p[0] = (uint8_t)(data1 >> 24);
  131. p[1] = (uint8_t)(data1 >> 16);
  132. p[3] = (uint8_t)data1;
  133. p[4] = (uint8_t)(data2 >> 24);
  134. p[5] = (uint8_t)(data2 >> 16);
  135. p[2] = (uint8_t)(data1 >> 8);
  136. p[6] = (uint8_t)(data2 >> 8);
  137. p[7] = (uint8_t)data2;
  138. return data;
  139. }
  140. // static uint64_t subghz_protocol_alutech_at_4n_encrypt(uint64_t data, const char* file_name) {
  141. // uint8_t* p = (uint8_t*)&data;
  142. // uint32_t data1 = 0;
  143. // uint32_t data2 = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
  144. // uint32_t data3 = p[4] << 24 | p[5] << 16 | p[6] << 8 | p[7];
  145. // uint32_t magic_data[] = {
  146. // subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 6),
  147. // subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 4),
  148. // subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 5),
  149. // subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 1),
  150. // subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 2),
  151. // subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 0)};
  152. // do {
  153. // data1 = data1 + magic_data[0];
  154. // data2 = data2 + ((magic_data[1] + (data3 << 4)) ^
  155. // ((magic_data[2] + (data3 >> 5)) ^ (data1 + data3)));
  156. // data3 = data3 + ((magic_data[3] + (data2 << 4)) ^
  157. // ((magic_data[4] + (data2 >> 5)) ^ (data1 + data2)));
  158. // } while(data1 != magic_data[5]);
  159. // p[0] = (uint8_t)(data2 >> 24);
  160. // p[1] = (uint8_t)(data2 >> 16);
  161. // p[3] = (uint8_t)data2;
  162. // p[4] = (uint8_t)(data3 >> 24);
  163. // p[5] = (uint8_t)(data3 >> 16);
  164. // p[2] = (uint8_t)(data2 >> 8);
  165. // p[6] = (uint8_t)(data3 >> 8);
  166. // p[7] = (uint8_t)data3;
  167. // return data;
  168. // }
  169. void* subghz_protocol_decoder_alutech_at_4n_alloc(SubGhzEnvironment* environment) {
  170. SubGhzProtocolDecoderAlutech_at_4n* instance =
  171. malloc(sizeof(SubGhzProtocolDecoderAlutech_at_4n));
  172. instance->base.protocol = &subghz_protocol_alutech_at_4n;
  173. instance->generic.protocol_name = instance->base.protocol->name;
  174. instance->alutech_at_4n_rainbow_table_file_name =
  175. subghz_environment_get_alutech_at_4n_rainbow_table_file_name(environment);
  176. if(instance->alutech_at_4n_rainbow_table_file_name) {
  177. FURI_LOG_I(
  178. TAG, "Loading rainbow table from %s", instance->alutech_at_4n_rainbow_table_file_name);
  179. }
  180. return instance;
  181. }
  182. void subghz_protocol_decoder_alutech_at_4n_free(void* context) {
  183. furi_assert(context);
  184. SubGhzProtocolDecoderAlutech_at_4n* instance = context;
  185. instance->alutech_at_4n_rainbow_table_file_name = NULL;
  186. free(instance);
  187. }
  188. void subghz_protocol_decoder_alutech_at_4n_reset(void* context) {
  189. furi_assert(context);
  190. SubGhzProtocolDecoderAlutech_at_4n* instance = context;
  191. instance->decoder.parser_step = Alutech_at_4nDecoderStepReset;
  192. }
  193. void subghz_protocol_decoder_alutech_at_4n_feed(void* context, bool level, uint32_t duration) {
  194. furi_assert(context);
  195. SubGhzProtocolDecoderAlutech_at_4n* instance = context;
  196. switch(instance->decoder.parser_step) {
  197. case Alutech_at_4nDecoderStepReset:
  198. if((level) && DURATION_DIFF(duration, subghz_protocol_alutech_at_4n_const.te_short) <
  199. subghz_protocol_alutech_at_4n_const.te_delta) {
  200. instance->decoder.parser_step = Alutech_at_4nDecoderStepCheckPreambula;
  201. instance->header_count++;
  202. }
  203. break;
  204. case Alutech_at_4nDecoderStepCheckPreambula:
  205. if((!level) && (DURATION_DIFF(duration, subghz_protocol_alutech_at_4n_const.te_short) <
  206. subghz_protocol_alutech_at_4n_const.te_delta)) {
  207. instance->decoder.parser_step = Alutech_at_4nDecoderStepReset;
  208. break;
  209. }
  210. if((instance->header_count > 2) &&
  211. (DURATION_DIFF(duration, subghz_protocol_alutech_at_4n_const.te_short * 10) <
  212. subghz_protocol_alutech_at_4n_const.te_delta * 10)) {
  213. // Found header
  214. instance->decoder.parser_step = Alutech_at_4nDecoderStepSaveDuration;
  215. instance->decoder.decode_data = 0;
  216. instance->data = 0;
  217. instance->decoder.decode_count_bit = 0;
  218. } else {
  219. instance->decoder.parser_step = Alutech_at_4nDecoderStepReset;
  220. instance->header_count = 0;
  221. }
  222. break;
  223. case Alutech_at_4nDecoderStepSaveDuration:
  224. if(level) {
  225. instance->decoder.te_last = duration;
  226. instance->decoder.parser_step = Alutech_at_4nDecoderStepCheckDuration;
  227. }
  228. break;
  229. case Alutech_at_4nDecoderStepCheckDuration:
  230. if(!level) {
  231. if(duration >= ((uint32_t)subghz_protocol_alutech_at_4n_const.te_short * 2 +
  232. subghz_protocol_alutech_at_4n_const.te_delta)) {
  233. //add last bit
  234. if(DURATION_DIFF(
  235. instance->decoder.te_last, subghz_protocol_alutech_at_4n_const.te_short) <
  236. subghz_protocol_alutech_at_4n_const.te_delta) {
  237. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  238. } else if(
  239. DURATION_DIFF(
  240. instance->decoder.te_last, subghz_protocol_alutech_at_4n_const.te_long) <
  241. subghz_protocol_alutech_at_4n_const.te_delta * 2) {
  242. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  243. }
  244. // Found end TX
  245. instance->decoder.parser_step = Alutech_at_4nDecoderStepReset;
  246. if(instance->decoder.decode_count_bit ==
  247. subghz_protocol_alutech_at_4n_const.min_count_bit_for_found) {
  248. if(instance->generic.data != instance->data) {
  249. instance->generic.data = instance->data;
  250. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  251. instance->crc = instance->decoder.decode_data;
  252. if(instance->base.callback)
  253. instance->base.callback(&instance->base, instance->base.context);
  254. }
  255. instance->decoder.decode_data = 0;
  256. instance->data = 0;
  257. instance->decoder.decode_count_bit = 0;
  258. instance->header_count = 0;
  259. }
  260. break;
  261. } else if(
  262. (DURATION_DIFF(
  263. instance->decoder.te_last, subghz_protocol_alutech_at_4n_const.te_short) <
  264. subghz_protocol_alutech_at_4n_const.te_delta) &&
  265. (DURATION_DIFF(duration, subghz_protocol_alutech_at_4n_const.te_long) <
  266. subghz_protocol_alutech_at_4n_const.te_delta * 2)) {
  267. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  268. if(instance->decoder.decode_count_bit == 64) {
  269. instance->data = instance->decoder.decode_data;
  270. instance->decoder.decode_data = 0;
  271. }
  272. instance->decoder.parser_step = Alutech_at_4nDecoderStepSaveDuration;
  273. } else if(
  274. (DURATION_DIFF(
  275. instance->decoder.te_last, subghz_protocol_alutech_at_4n_const.te_long) <
  276. subghz_protocol_alutech_at_4n_const.te_delta * 2) &&
  277. (DURATION_DIFF(duration, subghz_protocol_alutech_at_4n_const.te_short) <
  278. subghz_protocol_alutech_at_4n_const.te_delta)) {
  279. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  280. if(instance->decoder.decode_count_bit == 64) {
  281. instance->data = instance->decoder.decode_data;
  282. instance->decoder.decode_data = 0;
  283. }
  284. instance->decoder.parser_step = Alutech_at_4nDecoderStepSaveDuration;
  285. } else {
  286. instance->decoder.parser_step = Alutech_at_4nDecoderStepReset;
  287. instance->header_count = 0;
  288. }
  289. } else {
  290. instance->decoder.parser_step = Alutech_at_4nDecoderStepReset;
  291. instance->header_count = 0;
  292. }
  293. break;
  294. }
  295. }
  296. /**
  297. * Analysis of received data
  298. * @param instance Pointer to a SubGhzBlockGeneric* instance
  299. * @param file_name Full path to rainbow table the file
  300. */
  301. static void subghz_protocol_alutech_at_4n_remote_controller(
  302. SubGhzBlockGeneric* instance,
  303. uint8_t crc,
  304. const char* file_name) {
  305. /**
  306. * Message format 72bit LSB first
  307. * data crc
  308. * XXXXXXXXXXXXXXXX CC
  309. *
  310. * For analysis, you need to turn the package MSB
  311. * in decoded messages format
  312. *
  313. * crc1 serial cnt key
  314. * cc SSSSSSSS XXxx BB
  315. *
  316. * crc1 is calculated from the lower part of cnt
  317. * key 1=0xff, 2=0x11, 3=0x22, 4=0x33, 5=0x44
  318. *
  319. */
  320. bool status = false;
  321. uint64_t data = subghz_protocol_blocks_reverse_key(instance->data, 64);
  322. crc = subghz_protocol_blocks_reverse_key(crc, 8);
  323. if(crc == subghz_protocol_alutech_at_4n_crc(data)) {
  324. data = subghz_protocol_alutech_at_4n_decrypt(data, file_name);
  325. status = true;
  326. }
  327. if(status && ((uint8_t)(data >> 56) ==
  328. subghz_protocol_alutech_at_4n_decrypt_data_crc((uint8_t)((data >> 8) & 0xFF)))) {
  329. instance->btn = (uint8_t)data & 0xFF;
  330. instance->cnt = (uint16_t)(data >> 8) & 0xFFFF;
  331. instance->serial = (uint32_t)(data >> 24) & 0xFFFFFFFF;
  332. }
  333. if(!status) {
  334. instance->btn = 0;
  335. instance->cnt = 0;
  336. instance->serial = 0;
  337. }
  338. }
  339. uint8_t subghz_protocol_decoder_alutech_at_4n_get_hash_data(void* context) {
  340. furi_assert(context);
  341. SubGhzProtocolDecoderAlutech_at_4n* instance = context;
  342. return (uint8_t)instance->crc;
  343. }
  344. bool subghz_protocol_decoder_alutech_at_4n_serialize(
  345. void* context,
  346. FlipperFormat* flipper_format,
  347. SubGhzRadioPreset* preset) {
  348. furi_assert(context);
  349. SubGhzProtocolDecoderAlutech_at_4n* instance = context;
  350. bool res = subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  351. if(res && !flipper_format_write_uint32(flipper_format, "CRC", &instance->crc, 1)) {
  352. FURI_LOG_E(TAG, "Unable to add CRC");
  353. res = false;
  354. }
  355. return res;
  356. return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  357. }
  358. bool subghz_protocol_decoder_alutech_at_4n_deserialize(
  359. void* context,
  360. FlipperFormat* flipper_format) {
  361. furi_assert(context);
  362. SubGhzProtocolDecoderAlutech_at_4n* instance = context;
  363. bool ret = false;
  364. do {
  365. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  366. break;
  367. }
  368. if(instance->generic.data_count_bit !=
  369. subghz_protocol_alutech_at_4n_const.min_count_bit_for_found) {
  370. FURI_LOG_E(TAG, "Wrong number of bits in key");
  371. break;
  372. }
  373. if(!flipper_format_rewind(flipper_format)) {
  374. FURI_LOG_E(TAG, "Rewind error");
  375. break;
  376. }
  377. if(!flipper_format_read_uint32(flipper_format, "CRC", (uint32_t*)&instance->crc, 1)) {
  378. FURI_LOG_E(TAG, "Missing CRC");
  379. break;
  380. }
  381. ret = true;
  382. } while(false);
  383. return ret;
  384. }
  385. void subghz_protocol_decoder_alutech_at_4n_get_string(void* context, FuriString* output) {
  386. furi_assert(context);
  387. SubGhzProtocolDecoderAlutech_at_4n* instance = context;
  388. subghz_protocol_alutech_at_4n_remote_controller(
  389. &instance->generic, instance->crc, instance->alutech_at_4n_rainbow_table_file_name);
  390. uint32_t code_found_hi = instance->generic.data >> 32;
  391. uint32_t code_found_lo = instance->generic.data & 0x00000000ffffffff;
  392. furi_string_cat_printf(
  393. output,
  394. "%s %d\r\n"
  395. "Key:0x%08lX%08lX%02X\r\n"
  396. "Sn:0x%08lX Btn:0x%01X\r\n"
  397. "Cnt:0x%03lX\r\n",
  398. instance->generic.protocol_name,
  399. instance->generic.data_count_bit,
  400. code_found_hi,
  401. code_found_lo,
  402. (uint8_t)instance->crc,
  403. instance->generic.serial,
  404. instance->generic.btn,
  405. instance->generic.cnt);
  406. }