subghz_protocol_star_line.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #include "subghz_protocol_star_line.h"
  2. #include "subghz_protocol_keeloq_common.h"
  3. #include "../subghz_keystore.h"
  4. #include <furi.h>
  5. #include <m-string.h>
  6. #include <m-array.h>
  7. struct SubGhzProtocolStarLine {
  8. SubGhzProtocolCommon common;
  9. SubGhzKeystore* keystore;
  10. const char* manufacture_name;
  11. };
  12. typedef enum {
  13. StarLineDecoderStepReset = 0,
  14. StarLineDecoderStepCheckPreambula,
  15. StarLineDecoderStepSaveDuration,
  16. StarLineDecoderStepCheckDuration,
  17. } StarLineDecoderStep;
  18. SubGhzProtocolStarLine* subghz_protocol_star_line_alloc(SubGhzKeystore* keystore) {
  19. SubGhzProtocolStarLine* instance = furi_alloc(sizeof(SubGhzProtocolStarLine));
  20. instance->keystore = keystore;
  21. instance->common.name = "Star Line";
  22. instance->common.code_min_count_bit_for_found = 64;
  23. instance->common.te_short = 250;
  24. instance->common.te_long = 500;
  25. instance->common.te_delta = 120;
  26. instance->common.type_protocol = SubGhzProtocolCommonTypeDynamic;
  27. instance->common.to_string = (SubGhzProtocolCommonToStr)subghz_protocol_star_line_to_str;
  28. instance->common.to_load_protocol =
  29. (SubGhzProtocolCommonLoadFromRAW)subghz_decoder_star_line_to_load_protocol;
  30. return instance;
  31. }
  32. void subghz_protocol_star_line_free(SubGhzProtocolStarLine* instance) {
  33. furi_assert(instance);
  34. free(instance);
  35. }
  36. const char* subghz_protocol_star_line_find_and_get_manufacture_name(void* context) {
  37. SubGhzProtocolStarLine* instance = context;
  38. subghz_protocol_star_line_check_remote_controller(instance);
  39. return instance->manufacture_name;
  40. }
  41. const char* subghz_protocol_star_line_get_manufacture_name(void* context) {
  42. SubGhzProtocolStarLine* instance = context;
  43. return instance->manufacture_name;
  44. }
  45. /** Send bit
  46. *
  47. * @param instance - SubGhzProtocolStarLine instance
  48. * @param bit - bit
  49. */
  50. void subghz_protocol_star_line_send_bit(SubGhzProtocolStarLine* instance, uint8_t bit) {
  51. if(bit) {
  52. //send bit 1
  53. SUBGHZ_TX_PIN_HIGH();
  54. delay_us(instance->common.te_long);
  55. SUBGHZ_TX_PIN_LOW();
  56. delay_us(instance->common.te_long);
  57. } else {
  58. //send bit 0
  59. SUBGHZ_TX_PIN_HIGH();
  60. delay_us(instance->common.te_short);
  61. SUBGHZ_TX_PIN_LOW();
  62. delay_us(instance->common.te_short);
  63. }
  64. }
  65. void subghz_protocol_star_line_send_key(
  66. SubGhzProtocolStarLine* instance,
  67. uint64_t key,
  68. uint8_t bit,
  69. uint8_t repeat) {
  70. while(repeat--) {
  71. //Send header
  72. for(uint8_t i = 0; i < 6; i++) {
  73. SUBGHZ_TX_PIN_HIGH();
  74. delay_us(instance->common.te_long * 2);
  75. SUBGHZ_TX_PIN_LOW();
  76. delay_us(instance->common.te_long * 2);
  77. }
  78. //Send Start bit ??????????
  79. //Send key data
  80. for(uint8_t i = bit; i > 0; i--) {
  81. subghz_protocol_star_line_send_bit(instance, bit_read(key, i - 1));
  82. }
  83. //Send Stop bit ??????????
  84. }
  85. }
  86. void subghz_protocol_star_line_reset(SubGhzProtocolStarLine* instance) {
  87. instance->common.parser_step = StarLineDecoderStepReset;
  88. }
  89. /** Checking the accepted code against the database manafacture key
  90. *
  91. * @param instance SubGhzProtocolStarLine instance
  92. * @param fix fix part of the parcel
  93. * @param hop hop encrypted part of the parcel
  94. * @return true on successful search
  95. */
  96. uint8_t subghz_protocol_star_line_check_remote_controller_selector(
  97. SubGhzProtocolStarLine* instance,
  98. uint32_t fix,
  99. uint32_t hop) {
  100. uint16_t end_serial = (uint16_t)(fix & 0xFF);
  101. uint8_t btn = (uint8_t)(fix >> 24);
  102. uint32_t decrypt = 0;
  103. uint64_t man_normal_learning;
  104. for
  105. M_EACH(manufacture_code, *subghz_keystore_get_data(instance->keystore), SubGhzKeyArray_t) {
  106. switch(manufacture_code->type) {
  107. case KEELOQ_LEARNING_SIMPLE:
  108. //Simple Learning
  109. decrypt = subghz_protocol_keeloq_common_decrypt(hop, manufacture_code->key);
  110. if((decrypt >> 24 == btn) &&
  111. ((((uint16_t)(decrypt >> 16)) & 0x00FF) == end_serial)) {
  112. instance->manufacture_name = string_get_cstr(manufacture_code->name);
  113. instance->common.cnt = decrypt & 0x0000FFFF;
  114. return 1;
  115. }
  116. break;
  117. case KEELOQ_LEARNING_NORMAL:
  118. // Normal_Learning
  119. // https://phreakerclub.com/forum/showpost.php?p=43557&postcount=37
  120. man_normal_learning =
  121. subghz_protocol_keeloq_common_normal_learning(fix, manufacture_code->key);
  122. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man_normal_learning);
  123. if((decrypt >> 24 == btn) &&
  124. ((((uint16_t)(decrypt >> 16)) & 0x00FF) == end_serial)) {
  125. instance->manufacture_name = string_get_cstr(manufacture_code->name);
  126. instance->common.cnt = decrypt & 0x0000FFFF;
  127. return 1;
  128. }
  129. break;
  130. case KEELOQ_LEARNING_UNKNOWN:
  131. // Simple Learning
  132. decrypt = subghz_protocol_keeloq_common_decrypt(hop, manufacture_code->key);
  133. if((decrypt >> 24 == btn) &&
  134. ((((uint16_t)(decrypt >> 16)) & 0x00FF) == end_serial)) {
  135. instance->manufacture_name = string_get_cstr(manufacture_code->name);
  136. instance->common.cnt = decrypt & 0x0000FFFF;
  137. return 1;
  138. }
  139. // Check for mirrored man
  140. uint64_t man_rev = 0;
  141. uint64_t man_rev_byte = 0;
  142. for(uint8_t i = 0; i < 64; i += 8) {
  143. man_rev_byte = (uint8_t)(manufacture_code->key >> i);
  144. man_rev = man_rev | man_rev_byte << (56 - i);
  145. }
  146. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man_rev);
  147. if((decrypt >> 24 == btn) &&
  148. ((((uint16_t)(decrypt >> 16)) & 0x00FF) == end_serial)) {
  149. instance->manufacture_name = string_get_cstr(manufacture_code->name);
  150. instance->common.cnt = decrypt & 0x0000FFFF;
  151. return 1;
  152. }
  153. //###########################
  154. // Normal_Learning
  155. // https://phreakerclub.com/forum/showpost.php?p=43557&postcount=37
  156. man_normal_learning =
  157. subghz_protocol_keeloq_common_normal_learning(fix, manufacture_code->key);
  158. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man_normal_learning);
  159. if((decrypt >> 24 == btn) &&
  160. ((((uint16_t)(decrypt >> 16)) & 0x00FF) == end_serial)) {
  161. instance->manufacture_name = string_get_cstr(manufacture_code->name);
  162. instance->common.cnt = decrypt & 0x0000FFFF;
  163. return 1;
  164. }
  165. // Check for mirrored man
  166. man_rev = 0;
  167. man_rev_byte = 0;
  168. for(uint8_t i = 0; i < 64; i += 8) {
  169. man_rev_byte = (uint8_t)(manufacture_code->key >> i);
  170. man_rev = man_rev | man_rev_byte << (56 - i);
  171. }
  172. man_normal_learning = subghz_protocol_keeloq_common_normal_learning(fix, man_rev);
  173. decrypt = subghz_protocol_keeloq_common_decrypt(hop, man_normal_learning);
  174. if((decrypt >> 24 == btn) &&
  175. ((((uint16_t)(decrypt >> 16)) & 0x00FF) == end_serial)) {
  176. instance->manufacture_name = string_get_cstr(manufacture_code->name);
  177. instance->common.cnt = decrypt & 0x0000FFFF;
  178. return 1;
  179. }
  180. break;
  181. }
  182. }
  183. instance->manufacture_name = "Unknown";
  184. instance->common.cnt = 0;
  185. return 0;
  186. }
  187. /** Analysis of received data
  188. *
  189. * @param instance SubGhzProtocolStarLine instance
  190. */
  191. void subghz_protocol_star_line_check_remote_controller(SubGhzProtocolStarLine* instance) {
  192. uint64_t key = subghz_protocol_common_reverse_key(
  193. instance->common.code_last_found, instance->common.code_last_count_bit);
  194. uint32_t key_fix = key >> 32;
  195. uint32_t key_hop = key & 0x00000000ffffffff;
  196. subghz_protocol_star_line_check_remote_controller_selector(instance, key_fix, key_hop);
  197. instance->common.serial = key_fix & 0x00FFFFFF;
  198. instance->common.btn = key_fix >> 24;
  199. }
  200. void subghz_protocol_star_line_parse(
  201. SubGhzProtocolStarLine* instance,
  202. bool level,
  203. uint32_t duration) {
  204. switch(instance->common.parser_step) {
  205. case StarLineDecoderStepReset:
  206. if(level) {
  207. if(DURATION_DIFF(duration, instance->common.te_long * 2) <
  208. instance->common.te_delta * 2) {
  209. instance->common.parser_step = StarLineDecoderStepCheckPreambula;
  210. instance->common.header_count++;
  211. } else if(instance->common.header_count > 4) {
  212. instance->common.code_found = 0;
  213. instance->common.code_count_bit = 0;
  214. instance->common.te_last = duration;
  215. instance->common.parser_step = StarLineDecoderStepCheckDuration;
  216. }
  217. } else {
  218. instance->common.header_count = 0;
  219. }
  220. break;
  221. case StarLineDecoderStepCheckPreambula:
  222. if((!level) && (DURATION_DIFF(duration, instance->common.te_long * 2) <
  223. instance->common.te_delta * 2)) {
  224. //Found Preambula
  225. instance->common.parser_step = StarLineDecoderStepReset;
  226. } else {
  227. instance->common.header_count = 0;
  228. instance->common.parser_step = StarLineDecoderStepReset;
  229. }
  230. break;
  231. case StarLineDecoderStepSaveDuration:
  232. if(level) {
  233. if(duration >= (instance->common.te_long + instance->common.te_delta)) {
  234. instance->common.parser_step = StarLineDecoderStepReset;
  235. if(instance->common.code_count_bit >=
  236. instance->common.code_min_count_bit_for_found) {
  237. if(instance->common.code_last_found != instance->common.code_found) {
  238. instance->common.code_last_found = instance->common.code_found;
  239. instance->common.code_last_count_bit = instance->common.code_count_bit;
  240. if(instance->common.callback)
  241. instance->common.callback(
  242. (SubGhzProtocolCommon*)instance, instance->common.context);
  243. }
  244. }
  245. instance->common.code_found = 0;
  246. instance->common.code_count_bit = 0;
  247. instance->common.header_count = 0;
  248. break;
  249. } else {
  250. instance->common.te_last = duration;
  251. instance->common.parser_step = StarLineDecoderStepCheckDuration;
  252. }
  253. } else {
  254. instance->common.parser_step = StarLineDecoderStepReset;
  255. }
  256. break;
  257. case StarLineDecoderStepCheckDuration:
  258. if(!level) {
  259. if((DURATION_DIFF(instance->common.te_last, instance->common.te_short) <
  260. instance->common.te_delta) &&
  261. (DURATION_DIFF(duration, instance->common.te_short) < instance->common.te_delta)) {
  262. subghz_protocol_common_add_bit(&instance->common, 0);
  263. instance->common.parser_step = StarLineDecoderStepSaveDuration;
  264. } else if(
  265. (DURATION_DIFF(instance->common.te_last, instance->common.te_long) <
  266. instance->common.te_delta) &&
  267. (DURATION_DIFF(duration, instance->common.te_long) < instance->common.te_delta)) {
  268. subghz_protocol_common_add_bit(&instance->common, 1);
  269. instance->common.parser_step = StarLineDecoderStepSaveDuration;
  270. } else {
  271. instance->common.parser_step = StarLineDecoderStepReset;
  272. }
  273. } else {
  274. instance->common.parser_step = StarLineDecoderStepReset;
  275. }
  276. break;
  277. }
  278. }
  279. void subghz_protocol_star_line_to_str(SubGhzProtocolStarLine* instance, string_t output) {
  280. subghz_protocol_star_line_check_remote_controller(instance);
  281. uint32_t code_found_hi = instance->common.code_last_found >> 32;
  282. uint32_t code_found_lo = instance->common.code_last_found & 0x00000000ffffffff;
  283. uint64_t code_found_reverse = subghz_protocol_common_reverse_key(
  284. instance->common.code_last_found, instance->common.code_last_count_bit);
  285. uint32_t code_found_reverse_hi = code_found_reverse >> 32;
  286. uint32_t code_found_reverse_lo = code_found_reverse & 0x00000000ffffffff;
  287. string_cat_printf(
  288. output,
  289. "%s %dbit\r\n"
  290. "Key:%08lX%08lX\r\n"
  291. "Fix:0x%08lX Cnt:%04X\r\n"
  292. "Hop:0x%08lX Btn:%02lX\r\n"
  293. "MF:%s\r\n"
  294. "Sn:0x%07lX \r\n",
  295. instance->common.name,
  296. instance->common.code_last_count_bit,
  297. code_found_hi,
  298. code_found_lo,
  299. code_found_reverse_hi,
  300. instance->common.cnt,
  301. code_found_reverse_lo,
  302. instance->common.btn,
  303. instance->manufacture_name,
  304. instance->common.serial);
  305. }
  306. void subghz_decoder_star_line_to_load_protocol(SubGhzProtocolStarLine* instance, void* context) {
  307. furi_assert(context);
  308. furi_assert(instance);
  309. SubGhzProtocolCommonLoad* data = context;
  310. instance->common.code_last_found = data->code_found;
  311. instance->common.code_last_count_bit = data->code_count_bit;
  312. subghz_protocol_star_line_check_remote_controller(instance);
  313. }