star_line.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. static inline bool subghz_protocol_star_line_check_decrypt(
  159. SubGhzBlockGeneric* instance,
  160. uint32_t decrypt,
  161. uint8_t btn,
  162. uint32_t end_serial) {
  163. furi_assert(instance);
  164. if((decrypt >> 24 == btn) && ((((uint16_t)(decrypt >> 16)) & 0x00FF) == end_serial)) {
  165. instance->cnt = decrypt & 0x0000FFFF;
  166. return true;
  167. }
  168. return false;
  169. }
  170. /** Checking the accepted code against the database manafacture key
  171. *
  172. * @param instance SubGhzProtocolStarLine instance
  173. * @param fix fix part of the parcel
  174. * @param hop hop encrypted part of the parcel
  175. * @return true on successful search
  176. */
  177. static uint8_t subghz_protocol_star_line_check_remote_controller_selector(
  178. SubGhzBlockGeneric* instance,
  179. uint32_t fix,
  180. uint32_t hop,
  181. SubGhzKeystore* keystore,
  182. const char** manufacture_name) {
  183. uint16_t end_serial = (uint16_t)(fix & 0xFF);
  184. uint8_t btn = (uint8_t)(fix >> 24);
  185. uint32_t decrypt = 0;
  186. uint64_t man_normal_learning;
  187. for
  188. M_EACH(manufacture_code, *subghz_keystore_get_data(keystore), SubGhzKeyArray_t) {
  189. switch(manufacture_code->type) {
  190. case KEELOQ_LEARNING_SIMPLE:
  191. //Simple Learning
  192. decrypt = subghz_protocol_keeloq_common_decrypt(hop, manufacture_code->key);
  193. if(subghz_protocol_star_line_check_decrypt(instance, decrypt, btn, end_serial)) {
  194. *manufacture_name = string_get_cstr(manufacture_code->name);
  195. return 1;
  196. }
  197. break;
  198. case KEELOQ_LEARNING_NORMAL:
  199. // Normal_Learning
  200. // https://phreakerclub.com/forum/showpost.php?p=43557&postcount=37
  201. man_normal_learning =
  202. subghz_protocol_keeloq_common_normal_learning(fix, manufacture_code->key);
  203. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man_normal_learning);
  204. if(subghz_protocol_star_line_check_decrypt(instance, decrypt, btn, end_serial)) {
  205. *manufacture_name = string_get_cstr(manufacture_code->name);
  206. return 1;
  207. }
  208. break;
  209. case KEELOQ_LEARNING_UNKNOWN:
  210. // Simple Learning
  211. decrypt = subghz_protocol_keeloq_common_decrypt(hop, manufacture_code->key);
  212. if(subghz_protocol_star_line_check_decrypt(instance, decrypt, btn, end_serial)) {
  213. *manufacture_name = string_get_cstr(manufacture_code->name);
  214. return 1;
  215. }
  216. // Check for mirrored man
  217. uint64_t man_rev = 0;
  218. uint64_t man_rev_byte = 0;
  219. for(uint8_t i = 0; i < 64; i += 8) {
  220. man_rev_byte = (uint8_t)(manufacture_code->key >> i);
  221. man_rev = man_rev | man_rev_byte << (56 - i);
  222. }
  223. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man_rev);
  224. if(subghz_protocol_star_line_check_decrypt(instance, decrypt, btn, end_serial)) {
  225. *manufacture_name = string_get_cstr(manufacture_code->name);
  226. return 1;
  227. }
  228. //###########################
  229. // Normal_Learning
  230. // https://phreakerclub.com/forum/showpost.php?p=43557&postcount=37
  231. man_normal_learning =
  232. subghz_protocol_keeloq_common_normal_learning(fix, manufacture_code->key);
  233. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man_normal_learning);
  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. man_normal_learning = subghz_protocol_keeloq_common_normal_learning(fix, man_rev);
  239. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man_normal_learning);
  240. if(subghz_protocol_star_line_check_decrypt(instance, decrypt, btn, end_serial)) {
  241. *manufacture_name = string_get_cstr(manufacture_code->name);
  242. return 1;
  243. }
  244. break;
  245. }
  246. }
  247. *manufacture_name = "Unknown";
  248. instance->cnt = 0;
  249. return 0;
  250. }
  251. /** Analysis of received data
  252. *
  253. * @param instance SubGhzProtocolStarLine instance
  254. */
  255. static void subghz_protocol_star_line_check_remote_controller(
  256. SubGhzBlockGeneric* instance,
  257. SubGhzKeystore* keystore,
  258. const char** manufacture_name) {
  259. uint64_t key = subghz_protocol_blocks_reverse_key(instance->data, instance->data_count_bit);
  260. uint32_t key_fix = key >> 32;
  261. uint32_t key_hop = key & 0x00000000ffffffff;
  262. subghz_protocol_star_line_check_remote_controller_selector(
  263. instance, key_fix, key_hop, keystore, manufacture_name);
  264. instance->serial = key_fix & 0x00FFFFFF;
  265. instance->btn = key_fix >> 24;
  266. }
  267. uint8_t subghz_protocol_decoder_star_line_get_hash_data(void* context) {
  268. furi_assert(context);
  269. SubGhzProtocolDecoderStarLine* instance = context;
  270. return subghz_protocol_blocks_get_hash_data(
  271. &instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
  272. }
  273. bool subghz_protocol_decoder_star_line_serialize(
  274. void* context,
  275. FlipperFormat* flipper_format,
  276. uint32_t frequency,
  277. FuriHalSubGhzPreset preset) {
  278. furi_assert(context);
  279. SubGhzProtocolDecoderStarLine* instance = context;
  280. subghz_protocol_star_line_check_remote_controller(
  281. &instance->generic, instance->keystore, &instance->manufacture_name);
  282. bool res =
  283. subghz_block_generic_serialize(&instance->generic, flipper_format, frequency, preset);
  284. if(res && !flipper_format_write_string_cstr(
  285. flipper_format, "Manufacture", instance->manufacture_name)) {
  286. FURI_LOG_E(TAG, "Unable to add manufacture name");
  287. res = false;
  288. }
  289. return res;
  290. }
  291. bool subghz_protocol_decoder_star_line_deserialize(void* context, FlipperFormat* flipper_format) {
  292. furi_assert(context);
  293. SubGhzProtocolDecoderStarLine* instance = context;
  294. bool res = false;
  295. do {
  296. if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
  297. FURI_LOG_E(TAG, "Deserialize error");
  298. break;
  299. }
  300. res = true;
  301. } while(false);
  302. return res;
  303. }
  304. void subghz_protocol_decoder_star_line_get_string(void* context, string_t output) {
  305. furi_assert(context);
  306. SubGhzProtocolDecoderStarLine* instance = context;
  307. subghz_protocol_star_line_check_remote_controller(
  308. &instance->generic, instance->keystore, &instance->manufacture_name);
  309. uint32_t code_found_hi = instance->generic.data >> 32;
  310. uint32_t code_found_lo = instance->generic.data & 0x00000000ffffffff;
  311. uint64_t code_found_reverse = subghz_protocol_blocks_reverse_key(
  312. instance->generic.data, instance->generic.data_count_bit);
  313. uint32_t code_found_reverse_hi = code_found_reverse >> 32;
  314. uint32_t code_found_reverse_lo = code_found_reverse & 0x00000000ffffffff;
  315. string_cat_printf(
  316. output,
  317. "%s %dbit\r\n"
  318. "Key:%08lX%08lX\r\n"
  319. "Fix:0x%08lX Cnt:%04X\r\n"
  320. "Hop:0x%08lX Btn:%02lX\r\n"
  321. "MF:%s\r\n"
  322. "Sn:0x%07lX \r\n",
  323. instance->generic.protocol_name,
  324. instance->generic.data_count_bit,
  325. code_found_hi,
  326. code_found_lo,
  327. code_found_reverse_hi,
  328. instance->generic.cnt,
  329. code_found_reverse_lo,
  330. instance->generic.btn,
  331. instance->manufacture_name,
  332. instance->generic.serial);
  333. }