bad_usb_script.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <gui/gui.h>
  4. #include <input/input.h>
  5. #include <lib/toolbox/args.h>
  6. #include <furi_hal_usb_hid.h>
  7. #include <storage/storage.h>
  8. #include "bad_usb_script.h"
  9. #include <dolphin/dolphin.h>
  10. #define TAG "BadUSB"
  11. #define WORKER_TAG TAG "Worker"
  12. #define FILE_BUFFER_LEN 16
  13. typedef enum {
  14. WorkerEvtReserved = (1 << 0),
  15. WorkerEvtToggle = (1 << 1),
  16. WorkerEvtEnd = (1 << 2),
  17. WorkerEvtConnect = (1 << 3),
  18. WorkerEvtDisconnect = (1 << 4),
  19. } WorkerEvtFlags;
  20. struct BadUsbScript {
  21. BadUsbState st;
  22. string_t file_path;
  23. uint32_t defdelay;
  24. FuriThread* thread;
  25. uint8_t file_buf[FILE_BUFFER_LEN + 1];
  26. uint8_t buf_start;
  27. uint8_t buf_len;
  28. bool file_end;
  29. string_t line;
  30. string_t line_prev;
  31. uint32_t repeat_cnt;
  32. };
  33. typedef struct {
  34. char* name;
  35. uint16_t keycode;
  36. } DuckyKey;
  37. static const DuckyKey ducky_keys[] = {
  38. {"CTRL-ALT", KEY_MOD_LEFT_CTRL | KEY_MOD_LEFT_ALT},
  39. {"CTRL-SHIFT", KEY_MOD_LEFT_CTRL | KEY_MOD_LEFT_SHIFT},
  40. {"ALT-SHIFT", KEY_MOD_LEFT_ALT | KEY_MOD_LEFT_SHIFT},
  41. {"ALT-GUI", KEY_MOD_LEFT_ALT | KEY_MOD_LEFT_GUI},
  42. {"CTRL", KEY_MOD_LEFT_CTRL},
  43. {"CONTROL", KEY_MOD_LEFT_CTRL},
  44. {"SHIFT", KEY_MOD_LEFT_SHIFT},
  45. {"ALT", KEY_MOD_LEFT_ALT},
  46. {"GUI", KEY_MOD_LEFT_GUI},
  47. {"WINDOWS", KEY_MOD_LEFT_GUI},
  48. {"DOWNARROW", KEY_DOWN_ARROW},
  49. {"DOWN", KEY_DOWN_ARROW},
  50. {"LEFTARROW", KEY_LEFT_ARROW},
  51. {"LEFT", KEY_LEFT_ARROW},
  52. {"RIGHTARROW", KEY_RIGHT_ARROW},
  53. {"RIGHT", KEY_RIGHT_ARROW},
  54. {"UPARROW", KEY_UP_ARROW},
  55. {"UP", KEY_UP_ARROW},
  56. {"ENTER", KEY_ENTER},
  57. {"BREAK", KEY_PAUSE},
  58. {"PAUSE", KEY_PAUSE},
  59. {"CAPSLOCK", KEY_CAPS_LOCK},
  60. {"DELETE", KEY_DELETE},
  61. {"BACKSPACE", KEY_BACKSPACE},
  62. {"END", KEY_END},
  63. {"ESC", KEY_ESC},
  64. {"ESCAPE", KEY_ESC},
  65. {"HOME", KEY_HOME},
  66. {"INSERT", KEY_INSERT},
  67. {"NUMLOCK", KEY_NUM_LOCK},
  68. {"PAGEUP", KEY_PAGE_UP},
  69. {"PAGEDOWN", KEY_PAGE_DOWN},
  70. {"PRINTSCREEN", KEY_PRINT},
  71. {"SCROLLOCK", KEY_SCROLL_LOCK},
  72. {"SPACE", KEY_SPACE},
  73. {"TAB", KEY_TAB},
  74. {"MENU", KEY_APPLICATION},
  75. {"APP", KEY_APPLICATION},
  76. {"F1", KEY_F1},
  77. {"F2", KEY_F2},
  78. {"F3", KEY_F3},
  79. {"F4", KEY_F4},
  80. {"F5", KEY_F5},
  81. {"F6", KEY_F6},
  82. {"F7", KEY_F7},
  83. {"F8", KEY_F8},
  84. {"F9", KEY_F9},
  85. {"F10", KEY_F10},
  86. {"F11", KEY_F11},
  87. {"F12", KEY_F12},
  88. };
  89. static const char ducky_cmd_comment[] = {"REM"};
  90. static const char ducky_cmd_delay[] = {"DELAY "};
  91. static const char ducky_cmd_string[] = {"STRING "};
  92. static const char ducky_cmd_defdelay_1[] = {"DEFAULT_DELAY "};
  93. static const char ducky_cmd_defdelay_2[] = {"DEFAULTDELAY "};
  94. static const char ducky_cmd_repeat[] = {"REPEAT "};
  95. static const char ducky_cmd_altchar[] = {"ALTCHAR "};
  96. static const char ducky_cmd_altstr_1[] = {"ALTSTRING "};
  97. static const char ducky_cmd_altstr_2[] = {"ALTCODE "};
  98. static const uint8_t numpad_keys[10] = {
  99. KEYPAD_0,
  100. KEYPAD_1,
  101. KEYPAD_2,
  102. KEYPAD_3,
  103. KEYPAD_4,
  104. KEYPAD_5,
  105. KEYPAD_6,
  106. KEYPAD_7,
  107. KEYPAD_8,
  108. KEYPAD_9,
  109. };
  110. static bool ducky_get_number(const char* param, uint32_t* val) {
  111. uint32_t value = 0;
  112. if(sscanf(param, "%lu", &value) == 1) {
  113. *val = value;
  114. return true;
  115. }
  116. return false;
  117. }
  118. static uint32_t ducky_get_command_len(const char* line) {
  119. uint32_t len = strlen(line);
  120. for(uint32_t i = 0; i < len; i++) {
  121. if(line[i] == ' ') return i;
  122. }
  123. return 0;
  124. }
  125. static void ducky_numlock_on() {
  126. if((furi_hal_hid_get_led_state() & HID_KB_LED_NUM) == 0) {
  127. furi_hal_hid_kb_press(KEY_NUM_LOCK);
  128. furi_hal_hid_kb_release(KEY_NUM_LOCK);
  129. }
  130. }
  131. static bool ducky_numpad_press(const char num) {
  132. if((num < '0') || (num > '9')) return false;
  133. uint16_t key = numpad_keys[num - '0'];
  134. furi_hal_hid_kb_press(key);
  135. furi_hal_hid_kb_release(key);
  136. return true;
  137. }
  138. static bool ducky_altchar(const char* charcode) {
  139. uint8_t i = 0;
  140. bool state = false;
  141. //TODO: numlock
  142. FURI_LOG_I(WORKER_TAG, "char %s", charcode);
  143. furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT);
  144. while((charcode[i] != ' ') && (charcode[i] != '\n') && (charcode[i] != '\0')) {
  145. state = ducky_numpad_press(charcode[i]);
  146. if(state == false) break;
  147. i++;
  148. }
  149. furi_hal_hid_kb_release(KEY_MOD_LEFT_ALT);
  150. return state;
  151. }
  152. static bool ducky_altstring(const char* param) {
  153. uint32_t i = 0;
  154. bool state = false;
  155. while(param[i] != '\0') {
  156. if((param[i] < ' ') || (param[i] > '~')) {
  157. i++;
  158. continue; // Skip non-printable chars
  159. }
  160. char temp_str[4];
  161. snprintf(temp_str, 4, "%u", param[i]);
  162. state = ducky_altchar(temp_str);
  163. if(state == false) break;
  164. i++;
  165. }
  166. return state;
  167. }
  168. static bool ducky_string(const char* param) {
  169. uint32_t i = 0;
  170. while(param[i] != '\0') {
  171. furi_hal_hid_kb_press(HID_ASCII_TO_KEY(param[i]));
  172. furi_hal_hid_kb_release(HID_ASCII_TO_KEY(param[i]));
  173. i++;
  174. }
  175. return true;
  176. }
  177. static uint16_t ducky_get_keycode(const char* param, bool accept_chars) {
  178. for(uint8_t i = 0; i < (sizeof(ducky_keys) / sizeof(ducky_keys[0])); i++) {
  179. uint8_t key_cmd_len = strlen(ducky_keys[i].name);
  180. if((strncmp(param, ducky_keys[i].name, key_cmd_len) == 0) &&
  181. ((param[key_cmd_len] == ' ') || (param[key_cmd_len] == '\n') ||
  182. (param[key_cmd_len] == '\0'))) {
  183. return ducky_keys[i].keycode;
  184. }
  185. }
  186. if((accept_chars) && (strlen(param) > 0)) {
  187. return (HID_ASCII_TO_KEY(param[0]) & 0xFF);
  188. }
  189. return 0;
  190. }
  191. static int32_t ducky_parse_line(BadUsbScript* bad_usb, string_t line) {
  192. uint32_t line_len = string_size(line);
  193. const char* line_tmp = string_get_cstr(line);
  194. bool state = false;
  195. for(uint32_t i = 0; i < line_len; i++) {
  196. if((line_tmp[i] != ' ') && (line_tmp[i] != '\t') && (line_tmp[i] != '\n')) {
  197. line_tmp = &line_tmp[i];
  198. break; // Skip spaces and tabs
  199. }
  200. if(i == line_len - 1) return 0; // Skip empty lines
  201. }
  202. FURI_LOG_I(WORKER_TAG, "line:%s", line_tmp);
  203. // General commands
  204. if(strncmp(line_tmp, ducky_cmd_comment, strlen(ducky_cmd_comment)) == 0) {
  205. // REM - comment line
  206. return (0);
  207. } else if(strncmp(line_tmp, ducky_cmd_delay, strlen(ducky_cmd_delay)) == 0) {
  208. // DELAY
  209. line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
  210. uint32_t delay_val = 0;
  211. state = ducky_get_number(line_tmp, &delay_val);
  212. if((state) && (delay_val > 0)) {
  213. return (int32_t)delay_val;
  214. }
  215. return (-1);
  216. } else if(
  217. (strncmp(line_tmp, ducky_cmd_defdelay_1, strlen(ducky_cmd_defdelay_1)) == 0) ||
  218. (strncmp(line_tmp, ducky_cmd_defdelay_2, strlen(ducky_cmd_defdelay_2)) == 0)) {
  219. // DEFAULT_DELAY
  220. line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
  221. state = ducky_get_number(line_tmp, &bad_usb->defdelay);
  222. return (state) ? (0) : (-1);
  223. } else if(strncmp(line_tmp, ducky_cmd_string, strlen(ducky_cmd_string)) == 0) {
  224. // STRING
  225. line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
  226. state = ducky_string(line_tmp);
  227. return (state) ? (0) : (-1);
  228. } else if(strncmp(line_tmp, ducky_cmd_altchar, strlen(ducky_cmd_altchar)) == 0) {
  229. // ALTCHAR
  230. line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
  231. ducky_numlock_on();
  232. state = ducky_altchar(line_tmp);
  233. return (state) ? (0) : (-1);
  234. } else if(
  235. (strncmp(line_tmp, ducky_cmd_altstr_1, strlen(ducky_cmd_altstr_1)) == 0) ||
  236. (strncmp(line_tmp, ducky_cmd_altstr_2, strlen(ducky_cmd_altstr_2)) == 0)) {
  237. // ALTSTRING
  238. line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
  239. ducky_numlock_on();
  240. state = ducky_altstring(line_tmp);
  241. return (state) ? (0) : (-1);
  242. } else if(strncmp(line_tmp, ducky_cmd_repeat, strlen(ducky_cmd_repeat)) == 0) {
  243. // REPEAT
  244. line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
  245. state = ducky_get_number(line_tmp, &bad_usb->repeat_cnt);
  246. return (state) ? (0) : (-1);
  247. } else {
  248. // Special keys + modifiers
  249. uint16_t key = ducky_get_keycode(line_tmp, false);
  250. if(key == KEY_NONE) return (-1);
  251. if((key & 0xFF00) != 0) {
  252. // It's a modifier key
  253. line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
  254. key |= ducky_get_keycode(line_tmp, true);
  255. }
  256. furi_hal_hid_kb_press(key);
  257. furi_hal_hid_kb_release(key);
  258. return (0);
  259. }
  260. return (-1);
  261. }
  262. static bool ducky_script_preload(BadUsbScript* bad_usb, File* script_file) {
  263. uint8_t ret = 0;
  264. uint32_t line_len = 0;
  265. do {
  266. ret = storage_file_read(script_file, bad_usb->file_buf, FILE_BUFFER_LEN);
  267. for(uint16_t i = 0; i < ret; i++) {
  268. if(bad_usb->file_buf[i] == '\n' && line_len > 0) {
  269. bad_usb->st.line_nb++;
  270. line_len = 0;
  271. } else {
  272. line_len++;
  273. }
  274. }
  275. if(storage_file_eof(script_file)) {
  276. if(line_len > 0) {
  277. bad_usb->st.line_nb++;
  278. break;
  279. }
  280. }
  281. } while(ret > 0);
  282. storage_file_seek(script_file, 0, true);
  283. return true;
  284. }
  285. static int32_t ducky_script_execute_next(BadUsbScript* bad_usb, File* script_file) {
  286. int32_t delay_val = 0;
  287. if(bad_usb->repeat_cnt > 0) {
  288. bad_usb->repeat_cnt--;
  289. delay_val = ducky_parse_line(bad_usb, bad_usb->line_prev);
  290. if(delay_val < 0) {
  291. bad_usb->st.error_line = bad_usb->st.line_cur - 1;
  292. FURI_LOG_E(WORKER_TAG, "Unknown command at line %lu", bad_usb->st.line_cur - 1);
  293. return (-1);
  294. } else {
  295. return (delay_val + bad_usb->defdelay);
  296. }
  297. }
  298. string_set(bad_usb->line_prev, bad_usb->line);
  299. string_reset(bad_usb->line);
  300. while(1) {
  301. if(bad_usb->buf_len == 0) {
  302. bad_usb->buf_len = storage_file_read(script_file, bad_usb->file_buf, FILE_BUFFER_LEN);
  303. if(storage_file_eof(script_file)) {
  304. if((bad_usb->buf_len < FILE_BUFFER_LEN) && (bad_usb->file_end == false)) {
  305. bad_usb->file_buf[bad_usb->buf_len] = '\n';
  306. bad_usb->buf_len++;
  307. bad_usb->file_end = true;
  308. }
  309. }
  310. bad_usb->buf_start = 0;
  311. if(bad_usb->buf_len == 0) return (-2);
  312. }
  313. for(uint8_t i = bad_usb->buf_start; i < (bad_usb->buf_start + bad_usb->buf_len); i++) {
  314. if(bad_usb->file_buf[i] == '\n' && string_size(bad_usb->line) > 0) {
  315. bad_usb->st.line_cur++;
  316. bad_usb->buf_len = bad_usb->buf_len + bad_usb->buf_start - (i + 1);
  317. bad_usb->buf_start = i + 1;
  318. delay_val = ducky_parse_line(bad_usb, bad_usb->line);
  319. if(delay_val < 0) {
  320. bad_usb->st.error_line = bad_usb->st.line_cur;
  321. FURI_LOG_E(WORKER_TAG, "Unknown command at line %lu", bad_usb->st.line_cur);
  322. return (-1);
  323. } else {
  324. return (delay_val + bad_usb->defdelay);
  325. }
  326. } else {
  327. string_push_back(bad_usb->line, bad_usb->file_buf[i]);
  328. }
  329. }
  330. bad_usb->buf_len = 0;
  331. if(bad_usb->file_end) return (-2);
  332. }
  333. return 0;
  334. }
  335. static void bad_usb_hid_state_callback(bool state, void* context) {
  336. furi_assert(context);
  337. BadUsbScript* bad_usb = context;
  338. if(state == true)
  339. osThreadFlagsSet(furi_thread_get_thread_id(bad_usb->thread), WorkerEvtConnect);
  340. else
  341. osThreadFlagsSet(furi_thread_get_thread_id(bad_usb->thread), WorkerEvtDisconnect);
  342. }
  343. static int32_t bad_usb_worker(void* context) {
  344. BadUsbScript* bad_usb = context;
  345. BadUsbWorkerState worker_state = BadUsbStateInit;
  346. int32_t delay_val = 0;
  347. FURI_LOG_I(WORKER_TAG, "Init");
  348. File* script_file = storage_file_alloc(furi_record_open("storage"));
  349. string_init(bad_usb->line);
  350. string_init(bad_usb->line_prev);
  351. furi_hal_hid_set_state_callback(bad_usb_hid_state_callback, bad_usb);
  352. while(1) {
  353. if(worker_state == BadUsbStateInit) { // State: initialization
  354. if(storage_file_open(
  355. script_file,
  356. string_get_cstr(bad_usb->file_path),
  357. FSAM_READ,
  358. FSOM_OPEN_EXISTING)) {
  359. if((ducky_script_preload(bad_usb, script_file)) && (bad_usb->st.line_nb > 0)) {
  360. if(furi_hal_hid_is_connected()) {
  361. worker_state = BadUsbStateIdle; // Ready to run
  362. } else {
  363. worker_state = BadUsbStateNotConnected; // USB not connected
  364. }
  365. } else {
  366. worker_state = BadUsbStateScriptError; // Script preload error
  367. }
  368. } else {
  369. FURI_LOG_E(WORKER_TAG, "File open error");
  370. worker_state = BadUsbStateFileError; // File open error
  371. }
  372. bad_usb->st.state = worker_state;
  373. } else if(worker_state == BadUsbStateNotConnected) { // State: USB not connected
  374. uint32_t flags =
  375. osThreadFlagsWait(WorkerEvtEnd | WorkerEvtConnect, osFlagsWaitAny, osWaitForever);
  376. furi_check((flags & osFlagsError) == 0);
  377. if(flags & WorkerEvtEnd) {
  378. break;
  379. } else if(flags & WorkerEvtConnect) {
  380. worker_state = BadUsbStateIdle; // Ready to run
  381. }
  382. bad_usb->st.state = worker_state;
  383. } else if(worker_state == BadUsbStateIdle) { // State: ready to start
  384. uint32_t flags = osThreadFlagsWait(
  385. WorkerEvtEnd | WorkerEvtToggle | WorkerEvtDisconnect,
  386. osFlagsWaitAny,
  387. osWaitForever);
  388. furi_check((flags & osFlagsError) == 0);
  389. if(flags & WorkerEvtEnd) {
  390. break;
  391. } else if(flags & WorkerEvtToggle) { // Start executing script
  392. DOLPHIN_DEED(DolphinDeedBadUsbPlayScript);
  393. delay_val = 0;
  394. bad_usb->buf_len = 0;
  395. bad_usb->st.line_cur = 0;
  396. bad_usb->defdelay = 0;
  397. bad_usb->repeat_cnt = 0;
  398. bad_usb->file_end = false;
  399. storage_file_seek(script_file, 0, true);
  400. worker_state = BadUsbStateRunning;
  401. } else if(flags & WorkerEvtDisconnect) {
  402. worker_state = BadUsbStateNotConnected; // USB disconnected
  403. }
  404. bad_usb->st.state = worker_state;
  405. } else if(worker_state == BadUsbStateRunning) { // State: running
  406. uint16_t delay_cur = (delay_val > 1000) ? (1000) : (delay_val);
  407. uint32_t flags = osThreadFlagsWait(
  408. WorkerEvtEnd | WorkerEvtToggle | WorkerEvtDisconnect, osFlagsWaitAny, delay_cur);
  409. delay_val -= delay_cur;
  410. if(!(flags & osFlagsError)) {
  411. if(flags & WorkerEvtEnd) {
  412. break;
  413. } else if(flags & WorkerEvtToggle) {
  414. worker_state = BadUsbStateIdle; // Stop executing script
  415. furi_hal_hid_kb_release_all();
  416. } else if(flags & WorkerEvtDisconnect) {
  417. worker_state = BadUsbStateNotConnected; // USB disconnected
  418. furi_hal_hid_kb_release_all();
  419. }
  420. bad_usb->st.state = worker_state;
  421. continue;
  422. } else if((flags == osFlagsErrorTimeout) || (flags == osFlagsErrorResource)) {
  423. if(delay_val > 0) {
  424. bad_usb->st.delay_remain--;
  425. continue;
  426. }
  427. bad_usb->st.state = BadUsbStateRunning;
  428. delay_val = ducky_script_execute_next(bad_usb, script_file);
  429. if(delay_val == -1) { // Script error
  430. delay_val = 0;
  431. worker_state = BadUsbStateScriptError;
  432. bad_usb->st.state = worker_state;
  433. } else if(delay_val == -2) { // End of script
  434. delay_val = 0;
  435. worker_state = BadUsbStateIdle;
  436. bad_usb->st.state = BadUsbStateDone;
  437. furi_hal_hid_kb_release_all();
  438. continue;
  439. } else if(delay_val > 1000) {
  440. bad_usb->st.state = BadUsbStateDelay; // Show long delays
  441. bad_usb->st.delay_remain = delay_val / 1000;
  442. }
  443. } else {
  444. furi_check((flags & osFlagsError) == 0);
  445. }
  446. } else if(
  447. (worker_state == BadUsbStateFileError) ||
  448. (worker_state == BadUsbStateScriptError)) { // State: error
  449. uint32_t flags = osThreadFlagsWait(
  450. WorkerEvtEnd, osFlagsWaitAny, osWaitForever); // Waiting for exit command
  451. furi_check((flags & osFlagsError) == 0);
  452. if(flags & WorkerEvtEnd) {
  453. break;
  454. }
  455. }
  456. }
  457. furi_hal_hid_set_state_callback(NULL, NULL);
  458. storage_file_close(script_file);
  459. storage_file_free(script_file);
  460. string_clear(bad_usb->line);
  461. string_clear(bad_usb->line_prev);
  462. FURI_LOG_I(WORKER_TAG, "End");
  463. return 0;
  464. }
  465. BadUsbScript* bad_usb_script_open(string_t file_path) {
  466. furi_assert(file_path);
  467. BadUsbScript* bad_usb = furi_alloc(sizeof(BadUsbScript));
  468. string_init(bad_usb->file_path);
  469. string_set(bad_usb->file_path, file_path);
  470. bad_usb->st.state = BadUsbStateInit;
  471. bad_usb->thread = furi_thread_alloc();
  472. furi_thread_set_name(bad_usb->thread, "BadUsbWorker");
  473. furi_thread_set_stack_size(bad_usb->thread, 2048);
  474. furi_thread_set_context(bad_usb->thread, bad_usb);
  475. furi_thread_set_callback(bad_usb->thread, bad_usb_worker);
  476. furi_thread_start(bad_usb->thread);
  477. return bad_usb;
  478. }
  479. void bad_usb_script_close(BadUsbScript* bad_usb) {
  480. furi_assert(bad_usb);
  481. osThreadFlagsSet(furi_thread_get_thread_id(bad_usb->thread), WorkerEvtEnd);
  482. furi_thread_join(bad_usb->thread);
  483. furi_thread_free(bad_usb->thread);
  484. string_clear(bad_usb->file_path);
  485. free(bad_usb);
  486. }
  487. void bad_usb_script_toggle(BadUsbScript* bad_usb) {
  488. furi_assert(bad_usb);
  489. osThreadFlagsSet(furi_thread_get_thread_id(bad_usb->thread), WorkerEvtToggle);
  490. }
  491. BadUsbState* bad_usb_script_get_state(BadUsbScript* bad_usb) {
  492. furi_assert(bad_usb);
  493. return &(bad_usb->st);
  494. }