star_line.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. #include "star_line.h"
  2. #include "keeloq_common.h"
  3. #include "../subghz_keystore.h"
  4. #include <m-array.h>
  5. #include "../blocks/const.h"
  6. #include "../blocks/decoder.h"
  7. #include "../blocks/encoder.h"
  8. #include "../blocks/generic.h"
  9. #include "../blocks/math.h"
  10. #define TAG "SubGhzProtocolStarLine"
  11. static const SubGhzBlockConst subghz_protocol_star_line_const = {
  12. .te_short = 250,
  13. .te_long = 500,
  14. .te_delta = 120,
  15. .min_count_bit_for_found = 64,
  16. };
  17. struct SubGhzProtocolDecoderStarLine {
  18. SubGhzProtocolDecoderBase base;
  19. SubGhzBlockDecoder decoder;
  20. SubGhzBlockGeneric generic;
  21. uint16_t header_count;
  22. SubGhzKeystore* keystore;
  23. const char* manufacture_name;
  24. };
  25. struct SubGhzProtocolEncoderStarLine {
  26. SubGhzProtocolEncoderBase base;
  27. SubGhzProtocolBlockEncoder encoder;
  28. SubGhzBlockGeneric generic;
  29. };
  30. typedef enum {
  31. StarLineDecoderStepReset = 0,
  32. StarLineDecoderStepCheckPreambula,
  33. StarLineDecoderStepSaveDuration,
  34. StarLineDecoderStepCheckDuration,
  35. } StarLineDecoderStep;
  36. const SubGhzProtocolDecoder subghz_protocol_star_line_decoder = {
  37. .alloc = subghz_protocol_decoder_star_line_alloc,
  38. .free = subghz_protocol_decoder_star_line_free,
  39. .feed = subghz_protocol_decoder_star_line_feed,
  40. .reset = subghz_protocol_decoder_star_line_reset,
  41. .get_hash_data = subghz_protocol_decoder_star_line_get_hash_data,
  42. .serialize = subghz_protocol_decoder_star_line_serialize,
  43. .deserialize = subghz_protocol_decoder_star_line_deserialize,
  44. .get_string = subghz_protocol_decoder_star_line_get_string,
  45. };
  46. const SubGhzProtocolEncoder subghz_protocol_star_line_encoder = {
  47. .alloc = NULL,
  48. .free = NULL,
  49. .deserialize = NULL,
  50. .stop = NULL,
  51. .yield = NULL,
  52. };
  53. const SubGhzProtocol subghz_protocol_star_line = {
  54. .name = SUBGHZ_PROTOCOL_STAR_LINE_NAME,
  55. .type = SubGhzProtocolTypeDynamic,
  56. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
  57. .decoder = &subghz_protocol_star_line_decoder,
  58. .encoder = &subghz_protocol_star_line_encoder,
  59. };
  60. void* subghz_protocol_decoder_star_line_alloc(SubGhzEnvironment* environment) {
  61. SubGhzProtocolDecoderStarLine* instance = malloc(sizeof(SubGhzProtocolDecoderStarLine));
  62. instance->base.protocol = &subghz_protocol_star_line;
  63. instance->generic.protocol_name = instance->base.protocol->name;
  64. instance->keystore = subghz_environment_get_keystore(environment);
  65. return instance;
  66. }
  67. void subghz_protocol_decoder_star_line_free(void* context) {
  68. furi_assert(context);
  69. SubGhzProtocolDecoderStarLine* instance = context;
  70. free(instance);
  71. }
  72. void subghz_protocol_decoder_star_line_reset(void* context) {
  73. furi_assert(context);
  74. SubGhzProtocolDecoderStarLine* instance = context;
  75. instance->decoder.parser_step = StarLineDecoderStepReset;
  76. }
  77. void subghz_protocol_decoder_star_line_feed(void* context, bool level, uint32_t duration) {
  78. furi_assert(context);
  79. SubGhzProtocolDecoderStarLine* instance = context;
  80. switch(instance->decoder.parser_step) {
  81. case StarLineDecoderStepReset:
  82. if(level) {
  83. if(DURATION_DIFF(duration, subghz_protocol_star_line_const.te_long * 2) <
  84. subghz_protocol_star_line_const.te_delta * 2) {
  85. instance->decoder.parser_step = StarLineDecoderStepCheckPreambula;
  86. instance->header_count++;
  87. } else if(instance->header_count > 4) {
  88. instance->decoder.decode_data = 0;
  89. instance->decoder.decode_count_bit = 0;
  90. instance->decoder.te_last = duration;
  91. instance->decoder.parser_step = StarLineDecoderStepCheckDuration;
  92. }
  93. } else {
  94. instance->header_count = 0;
  95. }
  96. break;
  97. case StarLineDecoderStepCheckPreambula:
  98. if((!level) && (DURATION_DIFF(duration, subghz_protocol_star_line_const.te_long * 2) <
  99. subghz_protocol_star_line_const.te_delta * 2)) {
  100. //Found Preambula
  101. instance->decoder.parser_step = StarLineDecoderStepReset;
  102. } else {
  103. instance->header_count = 0;
  104. instance->decoder.parser_step = StarLineDecoderStepReset;
  105. }
  106. break;
  107. case StarLineDecoderStepSaveDuration:
  108. if(level) {
  109. if(duration >= (subghz_protocol_star_line_const.te_long +
  110. subghz_protocol_star_line_const.te_delta)) {
  111. instance->decoder.parser_step = StarLineDecoderStepReset;
  112. if(instance->decoder.decode_count_bit >=
  113. subghz_protocol_star_line_const.min_count_bit_for_found) {
  114. if(instance->generic.data != instance->decoder.decode_data) {
  115. instance->generic.data = instance->decoder.decode_data;
  116. instance->generic.data_count_bit = instance->decoder.decode_count_bit;
  117. if(instance->base.callback)
  118. instance->base.callback(&instance->base, instance->base.context);
  119. }
  120. }
  121. instance->decoder.decode_data = 0;
  122. instance->decoder.decode_count_bit = 0;
  123. instance->header_count = 0;
  124. break;
  125. } else {
  126. instance->decoder.te_last = duration;
  127. instance->decoder.parser_step = StarLineDecoderStepCheckDuration;
  128. }
  129. } else {
  130. instance->decoder.parser_step = StarLineDecoderStepReset;
  131. }
  132. break;
  133. case StarLineDecoderStepCheckDuration:
  134. if(!level) {
  135. if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_star_line_const.te_short) <
  136. subghz_protocol_star_line_const.te_delta) &&
  137. (DURATION_DIFF(duration, subghz_protocol_star_line_const.te_short) <
  138. subghz_protocol_star_line_const.te_delta)) {
  139. subghz_protocol_blocks_add_bit(&instance->decoder, 0);
  140. instance->decoder.parser_step = StarLineDecoderStepSaveDuration;
  141. } else if(
  142. (DURATION_DIFF(instance->decoder.te_last, subghz_protocol_star_line_const.te_long) <
  143. subghz_protocol_star_line_const.te_delta) &&
  144. (DURATION_DIFF(duration, subghz_protocol_star_line_const.te_long) <
  145. subghz_protocol_star_line_const.te_delta)) {
  146. subghz_protocol_blocks_add_bit(&instance->decoder, 1);
  147. instance->decoder.parser_step = StarLineDecoderStepSaveDuration;
  148. } else {
  149. instance->decoder.parser_step = StarLineDecoderStepReset;
  150. }
  151. } else {
  152. instance->decoder.parser_step = StarLineDecoderStepReset;
  153. }
  154. break;
  155. }
  156. }
  157. /**
  158. * Validation of decrypt data.
  159. * @param instance Pointer to a SubGhzBlockGeneric instance
  160. * @param decrypt Decrypd data
  161. * @param btn Button number, 4 bit
  162. * @param end_serial decrement the last 10 bits of the serial number
  163. * @return true On success
  164. */
  165. static inline bool subghz_protocol_star_line_check_decrypt(
  166. SubGhzBlockGeneric* instance,
  167. uint32_t decrypt,
  168. uint8_t btn,
  169. uint32_t end_serial) {
  170. furi_assert(instance);
  171. if((decrypt >> 24 == btn) && ((((uint16_t)(decrypt >> 16)) & 0x00FF) == end_serial)) {
  172. instance->cnt = decrypt & 0x0000FFFF;
  173. return true;
  174. }
  175. return false;
  176. }
  177. /**
  178. * Checking the accepted code against the database manafacture key
  179. * @param instance Pointer to a SubGhzBlockGeneric* instance
  180. * @param fix Fix part of the parcel
  181. * @param hop Hop encrypted part of the parcel
  182. * @param keystore Pointer to a SubGhzKeystore* instance
  183. * @param manufacture_name
  184. * @return true on successful search
  185. */
  186. static uint8_t subghz_protocol_star_line_check_remote_controller_selector(
  187. SubGhzBlockGeneric* instance,
  188. uint32_t fix,
  189. uint32_t hop,
  190. SubGhzKeystore* keystore,
  191. const char** manufacture_name) {
  192. uint16_t end_serial = (uint16_t)(fix & 0xFF);
  193. uint8_t btn = (uint8_t)(fix >> 24);
  194. uint32_t decrypt = 0;
  195. uint64_t man_normal_learning;
  196. for
  197. M_EACH(manufacture_code, *subghz_keystore_get_data(keystore), SubGhzKeyArray_t) {
  198. switch(manufacture_code->type) {
  199. case KEELOQ_LEARNING_SIMPLE:
  200. //Simple Learning
  201. decrypt = subghz_protocol_keeloq_common_decrypt(hop, manufacture_code->key);
  202. if(subghz_protocol_star_line_check_decrypt(instance, decrypt, btn, end_serial)) {
  203. *manufacture_name = furi_string_get_cstr(manufacture_code->name);
  204. return 1;
  205. }
  206. break;
  207. case KEELOQ_LEARNING_NORMAL:
  208. // Normal_Learning
  209. // https://phreakerclub.com/forum/showpost.php?p=43557&postcount=37
  210. man_normal_learning =
  211. subghz_protocol_keeloq_common_normal_learning(fix, manufacture_code->key);
  212. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man_normal_learning);
  213. if(subghz_protocol_star_line_check_decrypt(instance, decrypt, btn, end_serial)) {
  214. *manufacture_name = furi_string_get_cstr(manufacture_code->name);
  215. return 1;
  216. }
  217. break;
  218. case KEELOQ_LEARNING_UNKNOWN:
  219. // Simple Learning
  220. decrypt = subghz_protocol_keeloq_common_decrypt(hop, manufacture_code->key);
  221. if(subghz_protocol_star_line_check_decrypt(instance, decrypt, btn, end_serial)) {
  222. *manufacture_name = furi_string_get_cstr(manufacture_code->name);
  223. return 1;
  224. }
  225. // Check for mirrored man
  226. uint64_t man_rev = 0;
  227. uint64_t man_rev_byte = 0;
  228. for(uint8_t i = 0; i < 64; i += 8) {
  229. man_rev_byte = (uint8_t)(manufacture_code->key >> i);
  230. man_rev = man_rev | man_rev_byte << (56 - i);
  231. }
  232. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man_rev);
  233. if(subghz_protocol_star_line_check_decrypt(instance, decrypt, btn, end_serial)) {
  234. *manufacture_name = furi_string_get_cstr(manufacture_code->name);
  235. return 1;
  236. }
  237. //###########################
  238. // Normal_Learning
  239. // https://phreakerclub.com/forum/showpost.php?p=43557&postcount=37
  240. man_normal_learning =
  241. subghz_protocol_keeloq_common_normal_learning(fix, manufacture_code->key);
  242. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man_normal_learning);
  243. if(subghz_protocol_star_line_check_decrypt(instance, decrypt, btn, end_serial)) {
  244. *manufacture_name = furi_string_get_cstr(manufacture_code->name);
  245. return 1;
  246. }
  247. man_normal_learning = subghz_protocol_keeloq_common_normal_learning(fix, man_rev);
  248. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man_normal_learning);
  249. if(subghz_protocol_star_line_check_decrypt(instance, decrypt, btn, end_serial)) {
  250. *manufacture_name = furi_string_get_cstr(manufacture_code->name);
  251. return 1;
  252. }
  253. break;
  254. }
  255. }
  256. *manufacture_name = "Unknown";
  257. instance->cnt = 0;
  258. return 0;
  259. }
  260. /**
  261. * Analysis of received data
  262. * @param instance Pointer to a SubGhzBlockGeneric* instance
  263. * @param keystore Pointer to a SubGhzKeystore* instance
  264. * @param manufacture_name
  265. */
  266. static void subghz_protocol_star_line_check_remote_controller(
  267. SubGhzBlockGeneric* instance,
  268. SubGhzKeystore* keystore,
  269. const char** manufacture_name) {
  270. uint64_t key = subghz_protocol_blocks_reverse_key(instance->data, instance->data_count_bit);
  271. uint32_t key_fix = key >> 32;
  272. uint32_t key_hop = key & 0x00000000ffffffff;
  273. subghz_protocol_star_line_check_remote_controller_selector(
  274. instance, key_fix, key_hop, keystore, manufacture_name);
  275. instance->serial = key_fix & 0x00FFFFFF;
  276. instance->btn = key_fix >> 24;
  277. }
  278. uint8_t subghz_protocol_decoder_star_line_get_hash_data(void* context) {
  279. furi_assert(context);
  280. SubGhzProtocolDecoderStarLine* instance = context;
  281. return subghz_protocol_blocks_get_hash_data(
  282. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  283. }
  284. bool subghz_protocol_decoder_star_line_serialize(
  285. void* context,
  286. FlipperFormat* flipper_format,
  287. SubGhzRadioPreset* preset) {
  288. furi_assert(context);
  289. SubGhzProtocolDecoderStarLine* instance = context;
  290. subghz_protocol_star_line_check_remote_controller(
  291. &instance->generic, instance->keystore, &instance->manufacture_name);
  292. bool res = subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
  293. if(res && !flipper_format_write_string_cstr(
  294. flipper_format, "Manufacture", instance->manufacture_name)) {
  295. FURI_LOG_E(TAG, "Unable to add manufacture name");
  296. res = false;
  297. }
  298. if(res && instance->generic.data_count_bit !=
  299. subghz_protocol_star_line_const.min_count_bit_for_found) {
  300. FURI_LOG_E(TAG, "Wrong number of bits in key");
  301. res = false;
  302. }
  303. return res;
  304. }
  305. bool subghz_protocol_decoder_star_line_deserialize(void* context, FlipperFormat* flipper_format) {
  306. furi_assert(context);
  307. SubGhzProtocolDecoderStarLine* instance = context;
  308. bool res = false;
  309. do {
  310. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  311. FURI_LOG_E(TAG, "Deserialize error");
  312. break;
  313. }
  314. res = true;
  315. } while(false);
  316. return res;
  317. }
  318. void subghz_protocol_decoder_star_line_get_string(void* context, FuriString* output) {
  319. furi_assert(context);
  320. SubGhzProtocolDecoderStarLine* instance = context;
  321. subghz_protocol_star_line_check_remote_controller(
  322. &instance->generic, instance->keystore, &instance->manufacture_name);
  323. uint32_t code_found_hi = instance->generic.data >> 32;
  324. uint32_t code_found_lo = instance->generic.data & 0x00000000ffffffff;
  325. uint64_t code_found_reverse = subghz_protocol_blocks_reverse_key(
  326. instance->generic.data, instance->generic.data_count_bit);
  327. uint32_t code_found_reverse_hi = code_found_reverse >> 32;
  328. uint32_t code_found_reverse_lo = code_found_reverse & 0x00000000ffffffff;
  329. furi_string_cat_printf(
  330. output,
  331. "%s %dbit\r\n"
  332. "Key:%08lX%08lX\r\n"
  333. "Fix:0x%08lX Cnt:%04lX\r\n"
  334. "Hop:0x%08lX Btn:%02X\r\n"
  335. "MF:%s\r\n"
  336. "Sn:0x%07lX \r\n",
  337. instance->generic.protocol_name,
  338. instance->generic.data_count_bit,
  339. code_found_hi,
  340. code_found_lo,
  341. code_found_reverse_hi,
  342. instance->generic.cnt,
  343. code_found_reverse_lo,
  344. instance->generic.btn,
  345. instance->manufacture_name,
  346. instance->generic.serial);
  347. }