star_line.c 15 KB

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