lfrfid_cli.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <stdarg.h>
  4. #include <cli/cli.h>
  5. #include <lib/toolbox/args.h>
  6. #include <lib/lfrfid/lfrfid_worker.h>
  7. #include <storage/storage.h>
  8. #include <toolbox/stream/file_stream.h>
  9. #include <toolbox/varint.h>
  10. #include <toolbox/protocols/protocol_dict.h>
  11. #include <lfrfid/protocols/lfrfid_protocols.h>
  12. #include <lfrfid/lfrfid_raw_file.h>
  13. #include <toolbox/pulse_protocols/pulse_glue.h>
  14. static void lfrfid_cli(Cli* cli, FuriString* args, void* context);
  15. // app cli function
  16. void lfrfid_on_system_start() {
  17. Cli* cli = furi_record_open(RECORD_CLI);
  18. cli_add_command(cli, "rfid", CliCommandFlagDefault, lfrfid_cli, NULL);
  19. furi_record_close(RECORD_CLI);
  20. }
  21. static void lfrfid_cli_print_usage() {
  22. printf("Usage:\r\n");
  23. printf("rfid read <optional: normal | indala>\r\n");
  24. printf("rfid <write | emulate> <key_type> <key_data>\r\n");
  25. printf("rfid raw_read <ask | psk> <filename>\r\n");
  26. printf("rfid raw_emulate <filename>\r\n");
  27. };
  28. typedef struct {
  29. ProtocolId protocol;
  30. FuriEventFlag* event;
  31. } LFRFIDCliReadContext;
  32. static void lfrfid_cli_read_callback(LFRFIDWorkerReadResult result, ProtocolId proto, void* ctx) {
  33. furi_assert(ctx);
  34. LFRFIDCliReadContext* context = ctx;
  35. if(result == LFRFIDWorkerReadDone) {
  36. context->protocol = proto;
  37. FURI_SW_MEMBARRIER();
  38. }
  39. furi_event_flag_set(context->event, 1 << result);
  40. }
  41. static void lfrfid_cli_read(Cli* cli, FuriString* args) {
  42. FuriString* type_string;
  43. type_string = furi_string_alloc();
  44. LFRFIDWorkerReadType type = LFRFIDWorkerReadTypeAuto;
  45. if(args_read_string_and_trim(args, type_string)) {
  46. if(furi_string_cmp_str(type_string, "normal") == 0 ||
  47. furi_string_cmp_str(type_string, "ask") == 0) {
  48. // ask
  49. type = LFRFIDWorkerReadTypeASKOnly;
  50. } else if(
  51. furi_string_cmp_str(type_string, "indala") == 0 ||
  52. furi_string_cmp_str(type_string, "psk") == 0) {
  53. // psk
  54. type = LFRFIDWorkerReadTypePSKOnly;
  55. } else {
  56. lfrfid_cli_print_usage();
  57. furi_string_free(type_string);
  58. return;
  59. }
  60. }
  61. furi_string_free(type_string);
  62. ProtocolDict* dict = protocol_dict_alloc(lfrfid_protocols, LFRFIDProtocolMax);
  63. LFRFIDWorker* worker = lfrfid_worker_alloc(dict);
  64. LFRFIDCliReadContext context;
  65. context.protocol = PROTOCOL_NO;
  66. context.event = furi_event_flag_alloc();
  67. lfrfid_worker_start_thread(worker);
  68. printf("Reading RFID...\r\nPress Ctrl+C to abort\r\n");
  69. const uint32_t available_flags = (1 << LFRFIDWorkerReadDone);
  70. lfrfid_worker_read_start(worker, type, lfrfid_cli_read_callback, &context);
  71. while(true) {
  72. uint32_t flags =
  73. furi_event_flag_wait(context.event, available_flags, FuriFlagWaitAny, 100);
  74. if(flags != FuriFlagErrorTimeout) {
  75. if(FURI_BIT(flags, LFRFIDWorkerReadDone)) {
  76. break;
  77. }
  78. }
  79. if(cli_cmd_interrupt_received(cli)) break;
  80. }
  81. lfrfid_worker_stop(worker);
  82. lfrfid_worker_stop_thread(worker);
  83. lfrfid_worker_free(worker);
  84. if(context.protocol != PROTOCOL_NO) {
  85. printf("%s ", protocol_dict_get_name(dict, context.protocol));
  86. size_t size = protocol_dict_get_data_size(dict, context.protocol);
  87. uint8_t* data = malloc(size);
  88. protocol_dict_get_data(dict, context.protocol, data, size);
  89. for(size_t i = 0; i < size; i++) {
  90. printf("%02X", data[i]);
  91. }
  92. printf("\r\n");
  93. free(data);
  94. FuriString* info;
  95. info = furi_string_alloc();
  96. protocol_dict_render_data(dict, info, context.protocol);
  97. if(!furi_string_empty(info)) {
  98. printf("%s\r\n", furi_string_get_cstr(info));
  99. }
  100. furi_string_free(info);
  101. }
  102. printf("Reading stopped\r\n");
  103. protocol_dict_free(dict);
  104. furi_event_flag_free(context.event);
  105. }
  106. static bool lfrfid_cli_parse_args(FuriString* args, ProtocolDict* dict, ProtocolId* protocol) {
  107. bool result = false;
  108. FuriString *protocol_name, *data_text;
  109. protocol_name = furi_string_alloc();
  110. data_text = furi_string_alloc();
  111. size_t data_size = protocol_dict_get_max_data_size(dict);
  112. uint8_t* data = malloc(data_size);
  113. do {
  114. // load args
  115. if(!args_read_string_and_trim(args, protocol_name) ||
  116. !args_read_string_and_trim(args, data_text)) {
  117. lfrfid_cli_print_usage();
  118. break;
  119. }
  120. // check protocol arg
  121. *protocol = protocol_dict_get_protocol_by_name(dict, furi_string_get_cstr(protocol_name));
  122. if(*protocol == PROTOCOL_NO) {
  123. printf(
  124. "Unknown protocol: %s\r\n"
  125. "Available protocols:\r\n",
  126. furi_string_get_cstr(protocol_name));
  127. for(ProtocolId i = 0; i < LFRFIDProtocolMax; i++) {
  128. printf(
  129. "\t%s, %d bytes long\r\n",
  130. protocol_dict_get_name(dict, i),
  131. protocol_dict_get_data_size(dict, i));
  132. }
  133. break;
  134. }
  135. data_size = protocol_dict_get_data_size(dict, *protocol);
  136. // check data arg
  137. if(!args_read_hex_bytes(data_text, data, data_size)) {
  138. printf(
  139. "%s data needs to be %d bytes long\r\n",
  140. protocol_dict_get_name(dict, *protocol),
  141. data_size);
  142. break;
  143. }
  144. // load data to protocol
  145. protocol_dict_set_data(dict, *protocol, data, data_size);
  146. result = true;
  147. } while(false);
  148. free(data);
  149. furi_string_free(protocol_name);
  150. furi_string_free(data_text);
  151. return result;
  152. }
  153. static void lfrfid_cli_write_callback(LFRFIDWorkerWriteResult result, void* ctx) {
  154. furi_assert(ctx);
  155. FuriEventFlag* events = ctx;
  156. furi_event_flag_set(events, 1 << result);
  157. }
  158. static void lfrfid_cli_write(Cli* cli, FuriString* args) {
  159. ProtocolDict* dict = protocol_dict_alloc(lfrfid_protocols, LFRFIDProtocolMax);
  160. ProtocolId protocol;
  161. if(!lfrfid_cli_parse_args(args, dict, &protocol)) {
  162. protocol_dict_free(dict);
  163. return;
  164. }
  165. LFRFIDWorker* worker = lfrfid_worker_alloc(dict);
  166. FuriEventFlag* event = furi_event_flag_alloc();
  167. lfrfid_worker_start_thread(worker);
  168. lfrfid_worker_write_start(worker, protocol, lfrfid_cli_write_callback, event);
  169. printf("Writing RFID...\r\nPress Ctrl+C to abort\r\n");
  170. const uint32_t available_flags = (1 << LFRFIDWorkerWriteOK) |
  171. (1 << LFRFIDWorkerWriteProtocolCannotBeWritten) |
  172. (1 << LFRFIDWorkerWriteFobCannotBeWritten);
  173. while(!cli_cmd_interrupt_received(cli)) {
  174. uint32_t flags = furi_event_flag_wait(event, available_flags, FuriFlagWaitAny, 100);
  175. if(flags != FuriFlagErrorTimeout) {
  176. if(FURI_BIT(flags, LFRFIDWorkerWriteOK)) {
  177. printf("Written!\r\n");
  178. break;
  179. }
  180. if(FURI_BIT(flags, LFRFIDWorkerWriteProtocolCannotBeWritten)) {
  181. printf("This protocol cannot be written.\r\n");
  182. break;
  183. }
  184. if(FURI_BIT(flags, LFRFIDWorkerWriteFobCannotBeWritten)) {
  185. printf("Seems this fob cannot be written.\r\n");
  186. }
  187. }
  188. }
  189. printf("Writing stopped\r\n");
  190. lfrfid_worker_stop(worker);
  191. lfrfid_worker_stop_thread(worker);
  192. lfrfid_worker_free(worker);
  193. protocol_dict_free(dict);
  194. furi_event_flag_free(event);
  195. }
  196. static void lfrfid_cli_emulate(Cli* cli, FuriString* args) {
  197. ProtocolDict* dict = protocol_dict_alloc(lfrfid_protocols, LFRFIDProtocolMax);
  198. ProtocolId protocol;
  199. if(!lfrfid_cli_parse_args(args, dict, &protocol)) {
  200. protocol_dict_free(dict);
  201. return;
  202. }
  203. LFRFIDWorker* worker = lfrfid_worker_alloc(dict);
  204. lfrfid_worker_start_thread(worker);
  205. lfrfid_worker_emulate_start(worker, protocol);
  206. printf("Emulating RFID...\r\nPress Ctrl+C to abort\r\n");
  207. while(!cli_cmd_interrupt_received(cli)) {
  208. furi_delay_ms(100);
  209. }
  210. printf("Emulation stopped\r\n");
  211. lfrfid_worker_stop(worker);
  212. lfrfid_worker_stop_thread(worker);
  213. lfrfid_worker_free(worker);
  214. protocol_dict_free(dict);
  215. }
  216. static void lfrfid_cli_raw_analyze(Cli* cli, FuriString* args) {
  217. UNUSED(cli);
  218. FuriString *filepath, *info_string;
  219. filepath = furi_string_alloc();
  220. info_string = furi_string_alloc();
  221. Storage* storage = furi_record_open(RECORD_STORAGE);
  222. LFRFIDRawFile* file = lfrfid_raw_file_alloc(storage);
  223. do {
  224. float frequency = 0;
  225. float duty_cycle = 0;
  226. if(!args_read_probably_quoted_string_and_trim(args, filepath)) {
  227. lfrfid_cli_print_usage();
  228. break;
  229. }
  230. if(!lfrfid_raw_file_open_read(file, furi_string_get_cstr(filepath))) {
  231. printf("Failed to open file\r\n");
  232. break;
  233. }
  234. if(!lfrfid_raw_file_read_header(file, &frequency, &duty_cycle)) {
  235. printf("Invalid header\r\n");
  236. break;
  237. }
  238. bool file_end = false;
  239. uint32_t total_warns = 0;
  240. uint32_t total_duration = 0;
  241. uint32_t total_pulse = 0;
  242. ProtocolId total_protocol = PROTOCOL_NO;
  243. ProtocolDict* dict = protocol_dict_alloc(lfrfid_protocols, LFRFIDProtocolMax);
  244. protocol_dict_decoders_start(dict);
  245. while(!file_end) {
  246. uint32_t pulse = 0;
  247. uint32_t duration = 0;
  248. if(lfrfid_raw_file_read_pair(file, &duration, &pulse, &file_end)) {
  249. bool warn = false;
  250. if(pulse > duration || pulse <= 0 || duration <= 0) {
  251. total_warns += 1;
  252. warn = true;
  253. }
  254. furi_string_printf(info_string, "[%ld %ld]", pulse, duration);
  255. printf("%-16s", furi_string_get_cstr(info_string));
  256. furi_string_printf(info_string, "[%ld %ld]", pulse, duration - pulse);
  257. printf("%-16s", furi_string_get_cstr(info_string));
  258. if(warn) {
  259. printf(" <<----");
  260. }
  261. if(total_protocol == PROTOCOL_NO) {
  262. total_protocol = protocol_dict_decoders_feed(dict, true, pulse);
  263. if(total_protocol == PROTOCOL_NO) {
  264. total_protocol =
  265. protocol_dict_decoders_feed(dict, false, duration - pulse);
  266. }
  267. if(total_protocol != PROTOCOL_NO) {
  268. printf(" <FOUND %s>", protocol_dict_get_name(dict, total_protocol));
  269. }
  270. }
  271. printf("\r\n");
  272. total_pulse += pulse;
  273. total_duration += duration;
  274. if(total_protocol != PROTOCOL_NO) {
  275. break;
  276. }
  277. } else {
  278. printf("Failed to read pair\r\n");
  279. break;
  280. }
  281. }
  282. printf(" Frequency: %f\r\n", (double)frequency);
  283. printf(" Duty Cycle: %f\r\n", (double)duty_cycle);
  284. printf(" Warns: %ld\r\n", total_warns);
  285. printf(" Pulse sum: %ld\r\n", total_pulse);
  286. printf("Duration sum: %ld\r\n", total_duration);
  287. printf(" Average: %f\r\n", (double)((float)total_pulse / (float)total_duration));
  288. printf(" Protocol: ");
  289. if(total_protocol != PROTOCOL_NO) {
  290. size_t data_size = protocol_dict_get_data_size(dict, total_protocol);
  291. uint8_t* data = malloc(data_size);
  292. protocol_dict_get_data(dict, total_protocol, data, data_size);
  293. printf("%s [", protocol_dict_get_name(dict, total_protocol));
  294. for(size_t i = 0; i < data_size; i++) {
  295. printf("%02X", data[i]);
  296. if(i < data_size - 1) {
  297. printf(" ");
  298. }
  299. }
  300. printf("]\r\n");
  301. protocol_dict_render_data(dict, info_string, total_protocol);
  302. printf("%s\r\n", furi_string_get_cstr(info_string));
  303. free(data);
  304. } else {
  305. printf("not found\r\n");
  306. }
  307. protocol_dict_free(dict);
  308. } while(false);
  309. furi_string_free(filepath);
  310. furi_string_free(info_string);
  311. lfrfid_raw_file_free(file);
  312. furi_record_close(RECORD_STORAGE);
  313. }
  314. static void lfrfid_cli_raw_read_callback(LFRFIDWorkerReadRawResult result, void* context) {
  315. furi_assert(context);
  316. FuriEventFlag* event = context;
  317. furi_event_flag_set(event, 1 << result);
  318. }
  319. static void lfrfid_cli_raw_read(Cli* cli, FuriString* args) {
  320. UNUSED(cli);
  321. FuriString *filepath, *type_string;
  322. filepath = furi_string_alloc();
  323. type_string = furi_string_alloc();
  324. LFRFIDWorkerReadType type = LFRFIDWorkerReadTypeAuto;
  325. do {
  326. if(args_read_string_and_trim(args, type_string)) {
  327. if(furi_string_cmp_str(type_string, "normal") == 0 ||
  328. furi_string_cmp_str(type_string, "ask") == 0) {
  329. // ask
  330. type = LFRFIDWorkerReadTypeASKOnly;
  331. } else if(
  332. furi_string_cmp_str(type_string, "indala") == 0 ||
  333. furi_string_cmp_str(type_string, "psk") == 0) {
  334. // psk
  335. type = LFRFIDWorkerReadTypePSKOnly;
  336. } else {
  337. lfrfid_cli_print_usage();
  338. break;
  339. }
  340. }
  341. if(!args_read_probably_quoted_string_and_trim(args, filepath)) {
  342. lfrfid_cli_print_usage();
  343. break;
  344. }
  345. ProtocolDict* dict = protocol_dict_alloc(lfrfid_protocols, LFRFIDProtocolMax);
  346. LFRFIDWorker* worker = lfrfid_worker_alloc(dict);
  347. FuriEventFlag* event = furi_event_flag_alloc();
  348. lfrfid_worker_start_thread(worker);
  349. bool overrun = false;
  350. const uint32_t available_flags = (1 << LFRFIDWorkerReadRawFileError) |
  351. (1 << LFRFIDWorkerReadRawOverrun);
  352. lfrfid_worker_read_raw_start(
  353. worker, furi_string_get_cstr(filepath), type, lfrfid_cli_raw_read_callback, event);
  354. while(true) {
  355. uint32_t flags = furi_event_flag_wait(event, available_flags, FuriFlagWaitAny, 100);
  356. if(flags != FuriFlagErrorTimeout) {
  357. if(FURI_BIT(flags, LFRFIDWorkerReadRawFileError)) {
  358. printf("File is not RFID raw file\r\n");
  359. break;
  360. }
  361. if(FURI_BIT(flags, LFRFIDWorkerReadRawOverrun)) {
  362. if(!overrun) {
  363. printf("Overrun\r\n");
  364. overrun = true;
  365. }
  366. }
  367. }
  368. if(cli_cmd_interrupt_received(cli)) break;
  369. }
  370. if(overrun) {
  371. printf("An overrun occurred during read\r\n");
  372. }
  373. lfrfid_worker_stop(worker);
  374. lfrfid_worker_stop_thread(worker);
  375. lfrfid_worker_free(worker);
  376. protocol_dict_free(dict);
  377. furi_event_flag_free(event);
  378. } while(false);
  379. furi_string_free(filepath);
  380. furi_string_free(type_string);
  381. }
  382. static void lfrfid_cli_raw_emulate_callback(LFRFIDWorkerEmulateRawResult result, void* context) {
  383. furi_assert(context);
  384. FuriEventFlag* event = context;
  385. furi_event_flag_set(event, 1 << result);
  386. }
  387. static void lfrfid_cli_raw_emulate(Cli* cli, FuriString* args) {
  388. UNUSED(cli);
  389. FuriString* filepath;
  390. filepath = furi_string_alloc();
  391. Storage* storage = furi_record_open(RECORD_STORAGE);
  392. do {
  393. if(!args_read_probably_quoted_string_and_trim(args, filepath)) {
  394. lfrfid_cli_print_usage();
  395. break;
  396. }
  397. if(!storage_file_exists(storage, furi_string_get_cstr(filepath))) {
  398. printf("File not found: \"%s\"\r\n", furi_string_get_cstr(filepath));
  399. break;
  400. }
  401. ProtocolDict* dict = protocol_dict_alloc(lfrfid_protocols, LFRFIDProtocolMax);
  402. LFRFIDWorker* worker = lfrfid_worker_alloc(dict);
  403. FuriEventFlag* event = furi_event_flag_alloc();
  404. lfrfid_worker_start_thread(worker);
  405. bool overrun = false;
  406. const uint32_t available_flags = (1 << LFRFIDWorkerEmulateRawFileError) |
  407. (1 << LFRFIDWorkerEmulateRawOverrun);
  408. lfrfid_worker_emulate_raw_start(
  409. worker, furi_string_get_cstr(filepath), lfrfid_cli_raw_emulate_callback, event);
  410. while(true) {
  411. uint32_t flags = furi_event_flag_wait(event, available_flags, FuriFlagWaitAny, 100);
  412. if(flags != FuriFlagErrorTimeout) {
  413. if(FURI_BIT(flags, LFRFIDWorkerEmulateRawFileError)) {
  414. printf("File is not RFID raw file\r\n");
  415. break;
  416. }
  417. if(FURI_BIT(flags, LFRFIDWorkerEmulateRawOverrun)) {
  418. if(!overrun) {
  419. printf("Overrun\r\n");
  420. overrun = true;
  421. }
  422. }
  423. }
  424. if(cli_cmd_interrupt_received(cli)) break;
  425. }
  426. if(overrun) {
  427. printf("An overrun occurred during emulation\r\n");
  428. }
  429. lfrfid_worker_stop(worker);
  430. lfrfid_worker_stop_thread(worker);
  431. lfrfid_worker_free(worker);
  432. protocol_dict_free(dict);
  433. furi_event_flag_free(event);
  434. } while(false);
  435. furi_record_close(RECORD_STORAGE);
  436. furi_string_free(filepath);
  437. }
  438. static void lfrfid_cli(Cli* cli, FuriString* args, void* context) {
  439. UNUSED(context);
  440. FuriString* cmd;
  441. cmd = furi_string_alloc();
  442. if(!args_read_string_and_trim(args, cmd)) {
  443. furi_string_free(cmd);
  444. lfrfid_cli_print_usage();
  445. return;
  446. }
  447. if(furi_string_cmp_str(cmd, "read") == 0) {
  448. lfrfid_cli_read(cli, args);
  449. } else if(furi_string_cmp_str(cmd, "write") == 0) {
  450. lfrfid_cli_write(cli, args);
  451. } else if(furi_string_cmp_str(cmd, "emulate") == 0) {
  452. lfrfid_cli_emulate(cli, args);
  453. } else if(furi_string_cmp_str(cmd, "raw_read") == 0) {
  454. lfrfid_cli_raw_read(cli, args);
  455. } else if(furi_string_cmp_str(cmd, "raw_emulate") == 0) {
  456. lfrfid_cli_raw_emulate(cli, args);
  457. } else if(furi_string_cmp_str(cmd, "raw_analyze") == 0) {
  458. lfrfid_cli_raw_analyze(cli, args);
  459. } else {
  460. lfrfid_cli_print_usage();
  461. }
  462. furi_string_free(cmd);
  463. }