bad_usb_script.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. FURI_LOG_I(WORKER_TAG, "char %s", charcode);
  142. furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT);
  143. while((charcode[i] != ' ') && (charcode[i] != '\n') && (charcode[i] != '\0')) {
  144. state = ducky_numpad_press(charcode[i]);
  145. if(state == false) break;
  146. i++;
  147. }
  148. furi_hal_hid_kb_release(KEY_MOD_LEFT_ALT);
  149. return state;
  150. }
  151. static bool ducky_altstring(const char* param) {
  152. uint32_t i = 0;
  153. bool state = false;
  154. while(param[i] != '\0') {
  155. if((param[i] < ' ') || (param[i] > '~')) {
  156. i++;
  157. continue; // Skip non-printable chars
  158. }
  159. char temp_str[4];
  160. snprintf(temp_str, 4, "%u", param[i]);
  161. state = ducky_altchar(temp_str);
  162. if(state == false) break;
  163. i++;
  164. }
  165. return state;
  166. }
  167. static bool ducky_string(const char* param) {
  168. uint32_t i = 0;
  169. while(param[i] != '\0') {
  170. furi_hal_hid_kb_press(HID_ASCII_TO_KEY(param[i]));
  171. furi_hal_hid_kb_release(HID_ASCII_TO_KEY(param[i]));
  172. i++;
  173. }
  174. return true;
  175. }
  176. static uint16_t ducky_get_keycode(const char* param, bool accept_chars) {
  177. for(uint8_t i = 0; i < (sizeof(ducky_keys) / sizeof(ducky_keys[0])); i++) {
  178. uint8_t key_cmd_len = strlen(ducky_keys[i].name);
  179. if((strncmp(param, ducky_keys[i].name, key_cmd_len) == 0) &&
  180. ((param[key_cmd_len] == ' ') || (param[key_cmd_len] == '\n') ||
  181. (param[key_cmd_len] == '\0'))) {
  182. return ducky_keys[i].keycode;
  183. }
  184. }
  185. if((accept_chars) && (strlen(param) > 0)) {
  186. return (HID_ASCII_TO_KEY(param[0]) & 0xFF);
  187. }
  188. return 0;
  189. }
  190. static int32_t ducky_parse_line(BadUsbScript* bad_usb, string_t line) {
  191. uint32_t line_len = string_size(line);
  192. const char* line_tmp = string_get_cstr(line);
  193. bool state = false;
  194. for(uint32_t i = 0; i < line_len; i++) {
  195. if((line_tmp[i] != ' ') && (line_tmp[i] != '\t') && (line_tmp[i] != '\n')) {
  196. line_tmp = &line_tmp[i];
  197. break; // Skip spaces and tabs
  198. }
  199. if(i == line_len - 1) return 0; // Skip empty lines
  200. }
  201. FURI_LOG_I(WORKER_TAG, "line:%s", line_tmp);
  202. // General commands
  203. if(strncmp(line_tmp, ducky_cmd_comment, strlen(ducky_cmd_comment)) == 0) {
  204. // REM - comment line
  205. return (0);
  206. } else if(strncmp(line_tmp, ducky_cmd_delay, strlen(ducky_cmd_delay)) == 0) {
  207. // DELAY
  208. line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
  209. uint32_t delay_val = 0;
  210. state = ducky_get_number(line_tmp, &delay_val);
  211. if((state) && (delay_val > 0)) {
  212. return (int32_t)delay_val;
  213. }
  214. return (-1);
  215. } else if(
  216. (strncmp(line_tmp, ducky_cmd_defdelay_1, strlen(ducky_cmd_defdelay_1)) == 0) ||
  217. (strncmp(line_tmp, ducky_cmd_defdelay_2, strlen(ducky_cmd_defdelay_2)) == 0)) {
  218. // DEFAULT_DELAY
  219. line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
  220. state = ducky_get_number(line_tmp, &bad_usb->defdelay);
  221. return (state) ? (0) : (-1);
  222. } else if(strncmp(line_tmp, ducky_cmd_string, strlen(ducky_cmd_string)) == 0) {
  223. // STRING
  224. line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
  225. state = ducky_string(line_tmp);
  226. return (state) ? (0) : (-1);
  227. } else if(strncmp(line_tmp, ducky_cmd_altchar, strlen(ducky_cmd_altchar)) == 0) {
  228. // ALTCHAR
  229. line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
  230. ducky_numlock_on();
  231. state = ducky_altchar(line_tmp);
  232. return (state) ? (0) : (-1);
  233. } else if(
  234. (strncmp(line_tmp, ducky_cmd_altstr_1, strlen(ducky_cmd_altstr_1)) == 0) ||
  235. (strncmp(line_tmp, ducky_cmd_altstr_2, strlen(ducky_cmd_altstr_2)) == 0)) {
  236. // ALTSTRING
  237. line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
  238. ducky_numlock_on();
  239. state = ducky_altstring(line_tmp);
  240. return (state) ? (0) : (-1);
  241. } else if(strncmp(line_tmp, ducky_cmd_repeat, strlen(ducky_cmd_repeat)) == 0) {
  242. // REPEAT
  243. line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
  244. state = ducky_get_number(line_tmp, &bad_usb->repeat_cnt);
  245. return (state) ? (0) : (-1);
  246. } else {
  247. // Special keys + modifiers
  248. uint16_t key = ducky_get_keycode(line_tmp, false);
  249. if(key == KEY_NONE) return (-1);
  250. if((key & 0xFF00) != 0) {
  251. // It's a modifier key
  252. line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
  253. key |= ducky_get_keycode(line_tmp, true);
  254. }
  255. furi_hal_hid_kb_press(key);
  256. furi_hal_hid_kb_release(key);
  257. return (0);
  258. }
  259. return (-1);
  260. }
  261. static bool ducky_script_preload(BadUsbScript* bad_usb, File* script_file) {
  262. uint8_t ret = 0;
  263. uint32_t line_len = 0;
  264. do {
  265. ret = storage_file_read(script_file, bad_usb->file_buf, FILE_BUFFER_LEN);
  266. for(uint16_t i = 0; i < ret; i++) {
  267. if(bad_usb->file_buf[i] == '\n' && line_len > 0) {
  268. bad_usb->st.line_nb++;
  269. line_len = 0;
  270. } else {
  271. line_len++;
  272. }
  273. }
  274. if(storage_file_eof(script_file)) {
  275. if(line_len > 0) {
  276. bad_usb->st.line_nb++;
  277. break;
  278. }
  279. }
  280. } while(ret > 0);
  281. storage_file_seek(script_file, 0, true);
  282. return true;
  283. }
  284. static int32_t ducky_script_execute_next(BadUsbScript* bad_usb, File* script_file) {
  285. int32_t delay_val = 0;
  286. if(bad_usb->repeat_cnt > 0) {
  287. bad_usb->repeat_cnt--;
  288. delay_val = ducky_parse_line(bad_usb, bad_usb->line_prev);
  289. if(delay_val < 0) {
  290. bad_usb->st.error_line = bad_usb->st.line_cur - 1;
  291. FURI_LOG_E(WORKER_TAG, "Unknown command at line %lu", bad_usb->st.line_cur - 1);
  292. return (-1);
  293. } else {
  294. return (delay_val + bad_usb->defdelay);
  295. }
  296. }
  297. string_set(bad_usb->line_prev, bad_usb->line);
  298. string_reset(bad_usb->line);
  299. while(1) {
  300. if(bad_usb->buf_len == 0) {
  301. bad_usb->buf_len = storage_file_read(script_file, bad_usb->file_buf, FILE_BUFFER_LEN);
  302. if(storage_file_eof(script_file)) {
  303. if((bad_usb->buf_len < FILE_BUFFER_LEN) && (bad_usb->file_end == false)) {
  304. bad_usb->file_buf[bad_usb->buf_len] = '\n';
  305. bad_usb->buf_len++;
  306. bad_usb->file_end = true;
  307. }
  308. }
  309. bad_usb->buf_start = 0;
  310. if(bad_usb->buf_len == 0) return (-2);
  311. }
  312. for(uint8_t i = bad_usb->buf_start; i < (bad_usb->buf_start + bad_usb->buf_len); i++) {
  313. if(bad_usb->file_buf[i] == '\n' && string_size(bad_usb->line) > 0) {
  314. bad_usb->st.line_cur++;
  315. bad_usb->buf_len = bad_usb->buf_len + bad_usb->buf_start - (i + 1);
  316. bad_usb->buf_start = i + 1;
  317. delay_val = ducky_parse_line(bad_usb, bad_usb->line);
  318. if(delay_val < 0) {
  319. bad_usb->st.error_line = bad_usb->st.line_cur;
  320. FURI_LOG_E(WORKER_TAG, "Unknown command at line %lu", bad_usb->st.line_cur);
  321. return (-1);
  322. } else {
  323. return (delay_val + bad_usb->defdelay);
  324. }
  325. } else {
  326. string_push_back(bad_usb->line, bad_usb->file_buf[i]);
  327. }
  328. }
  329. bad_usb->buf_len = 0;
  330. if(bad_usb->file_end) return (-2);
  331. }
  332. return 0;
  333. }
  334. static void bad_usb_hid_state_callback(bool state, void* context) {
  335. furi_assert(context);
  336. BadUsbScript* bad_usb = context;
  337. if(state == true)
  338. osThreadFlagsSet(furi_thread_get_thread_id(bad_usb->thread), WorkerEvtConnect);
  339. else
  340. osThreadFlagsSet(furi_thread_get_thread_id(bad_usb->thread), WorkerEvtDisconnect);
  341. }
  342. static int32_t bad_usb_worker(void* context) {
  343. BadUsbScript* bad_usb = context;
  344. BadUsbWorkerState worker_state = BadUsbStateInit;
  345. int32_t delay_val = 0;
  346. FURI_LOG_I(WORKER_TAG, "Init");
  347. File* script_file = storage_file_alloc(furi_record_open("storage"));
  348. string_init(bad_usb->line);
  349. string_init(bad_usb->line_prev);
  350. furi_hal_hid_set_state_callback(bad_usb_hid_state_callback, bad_usb);
  351. while(1) {
  352. if(worker_state == BadUsbStateInit) { // State: initialization
  353. if(storage_file_open(
  354. script_file,
  355. string_get_cstr(bad_usb->file_path),
  356. FSAM_READ,
  357. FSOM_OPEN_EXISTING)) {
  358. if((ducky_script_preload(bad_usb, script_file)) && (bad_usb->st.line_nb > 0)) {
  359. if(furi_hal_hid_is_connected()) {
  360. worker_state = BadUsbStateIdle; // Ready to run
  361. } else {
  362. worker_state = BadUsbStateNotConnected; // USB not connected
  363. }
  364. } else {
  365. worker_state = BadUsbStateScriptError; // Script preload error
  366. }
  367. } else {
  368. FURI_LOG_E(WORKER_TAG, "File open error");
  369. worker_state = BadUsbStateFileError; // File open error
  370. }
  371. bad_usb->st.state = worker_state;
  372. } else if(worker_state == BadUsbStateNotConnected) { // State: USB not connected
  373. uint32_t flags =
  374. osThreadFlagsWait(WorkerEvtEnd | WorkerEvtConnect, osFlagsWaitAny, osWaitForever);
  375. furi_check((flags & osFlagsError) == 0);
  376. if(flags & WorkerEvtEnd) {
  377. break;
  378. } else if(flags & WorkerEvtConnect) {
  379. worker_state = BadUsbStateIdle; // Ready to run
  380. }
  381. bad_usb->st.state = worker_state;
  382. } else if(worker_state == BadUsbStateIdle) { // State: ready to start
  383. uint32_t flags = osThreadFlagsWait(
  384. WorkerEvtEnd | WorkerEvtToggle | WorkerEvtDisconnect,
  385. osFlagsWaitAny,
  386. osWaitForever);
  387. furi_check((flags & osFlagsError) == 0);
  388. if(flags & WorkerEvtEnd) {
  389. break;
  390. } else if(flags & WorkerEvtToggle) { // Start executing script
  391. DOLPHIN_DEED(DolphinDeedBadUsbPlayScript);
  392. delay_val = 0;
  393. bad_usb->buf_len = 0;
  394. bad_usb->st.line_cur = 0;
  395. bad_usb->defdelay = 0;
  396. bad_usb->repeat_cnt = 0;
  397. bad_usb->file_end = false;
  398. storage_file_seek(script_file, 0, true);
  399. worker_state = BadUsbStateRunning;
  400. } else if(flags & WorkerEvtDisconnect) {
  401. worker_state = BadUsbStateNotConnected; // USB disconnected
  402. }
  403. bad_usb->st.state = worker_state;
  404. } else if(worker_state == BadUsbStateRunning) { // State: running
  405. uint16_t delay_cur = (delay_val > 1000) ? (1000) : (delay_val);
  406. uint32_t flags = osThreadFlagsWait(
  407. WorkerEvtEnd | WorkerEvtToggle | WorkerEvtDisconnect, osFlagsWaitAny, delay_cur);
  408. delay_val -= delay_cur;
  409. if(!(flags & osFlagsError)) {
  410. if(flags & WorkerEvtEnd) {
  411. break;
  412. } else if(flags & WorkerEvtToggle) {
  413. worker_state = BadUsbStateIdle; // Stop executing script
  414. furi_hal_hid_kb_release_all();
  415. } else if(flags & WorkerEvtDisconnect) {
  416. worker_state = BadUsbStateNotConnected; // USB disconnected
  417. furi_hal_hid_kb_release_all();
  418. }
  419. bad_usb->st.state = worker_state;
  420. continue;
  421. } else if((flags == osFlagsErrorTimeout) || (flags == osFlagsErrorResource)) {
  422. if(delay_val > 0) {
  423. bad_usb->st.delay_remain--;
  424. continue;
  425. }
  426. bad_usb->st.state = BadUsbStateRunning;
  427. delay_val = ducky_script_execute_next(bad_usb, script_file);
  428. if(delay_val == -1) { // Script error
  429. delay_val = 0;
  430. worker_state = BadUsbStateScriptError;
  431. bad_usb->st.state = worker_state;
  432. } else if(delay_val == -2) { // End of script
  433. delay_val = 0;
  434. worker_state = BadUsbStateIdle;
  435. bad_usb->st.state = BadUsbStateDone;
  436. furi_hal_hid_kb_release_all();
  437. continue;
  438. } else if(delay_val > 1000) {
  439. bad_usb->st.state = BadUsbStateDelay; // Show long delays
  440. bad_usb->st.delay_remain = delay_val / 1000;
  441. }
  442. } else {
  443. furi_check((flags & osFlagsError) == 0);
  444. }
  445. } else if(
  446. (worker_state == BadUsbStateFileError) ||
  447. (worker_state == BadUsbStateScriptError)) { // State: error
  448. uint32_t flags = osThreadFlagsWait(
  449. WorkerEvtEnd, osFlagsWaitAny, osWaitForever); // Waiting for exit command
  450. furi_check((flags & osFlagsError) == 0);
  451. if(flags & WorkerEvtEnd) {
  452. break;
  453. }
  454. }
  455. }
  456. furi_hal_hid_set_state_callback(NULL, NULL);
  457. storage_file_close(script_file);
  458. storage_file_free(script_file);
  459. string_clear(bad_usb->line);
  460. string_clear(bad_usb->line_prev);
  461. FURI_LOG_I(WORKER_TAG, "End");
  462. return 0;
  463. }
  464. BadUsbScript* bad_usb_script_open(string_t file_path) {
  465. furi_assert(file_path);
  466. BadUsbScript* bad_usb = furi_alloc(sizeof(BadUsbScript));
  467. string_init(bad_usb->file_path);
  468. string_set(bad_usb->file_path, file_path);
  469. bad_usb->st.state = BadUsbStateInit;
  470. bad_usb->thread = furi_thread_alloc();
  471. furi_thread_set_name(bad_usb->thread, "BadUsbWorker");
  472. furi_thread_set_stack_size(bad_usb->thread, 2048);
  473. furi_thread_set_context(bad_usb->thread, bad_usb);
  474. furi_thread_set_callback(bad_usb->thread, bad_usb_worker);
  475. furi_thread_start(bad_usb->thread);
  476. return bad_usb;
  477. }
  478. void bad_usb_script_close(BadUsbScript* bad_usb) {
  479. furi_assert(bad_usb);
  480. osThreadFlagsSet(furi_thread_get_thread_id(bad_usb->thread), WorkerEvtEnd);
  481. furi_thread_join(bad_usb->thread);
  482. furi_thread_free(bad_usb->thread);
  483. string_clear(bad_usb->file_path);
  484. free(bad_usb);
  485. }
  486. void bad_usb_script_toggle(BadUsbScript* bad_usb) {
  487. furi_assert(bad_usb);
  488. osThreadFlagsSet(furi_thread_get_thread_id(bad_usb->thread), WorkerEvtToggle);
  489. }
  490. BadUsbState* bad_usb_script_get_state(BadUsbScript* bad_usb) {
  491. furi_assert(bad_usb);
  492. return &(bad_usb->st);
  493. }