2021-11-26 13:53:51 +00:00
|
|
|
#include <furi.h>
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal.h>
|
2021-11-26 13:53:51 +00:00
|
|
|
#include <gui/gui.h>
|
|
|
|
#include <input/input.h>
|
|
|
|
#include <lib/toolbox/args.h>
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal_usb_hid.h>
|
2021-11-26 13:53:51 +00:00
|
|
|
#include <storage/storage.h>
|
2023-03-20 17:22:03 +00:00
|
|
|
#include "ducky_script.h"
|
|
|
|
#include "ducky_script_i.h"
|
[FL-2183] [FL-2209] Dolphin Deeds, Level up, assets generation, refactoring (#965)
* Desktop: cleanup headers
* Get loader pubsub via record
* [FL-2183] Dolphin refactoring 2022.01
* Restruct animations assets structure
* Rename assets
* Cleanup headers
* Update Recording animation
* Add BadBattery animation
* Provide loader's pubsub via record
* Fix load/unload animations
* Scripts: add flipper format support, initial dolphin packager rework. Assets: internal and external dolphin.
* Sync internal meta.txt and manifest.txt
* Reorder, rename dolphin assets
* Split essential generated assets
* Add ReadMe for dolphin assets
* Separate essential blocking animations
* Scripts: full dolphin validation before packaging
* Assets, Scripts: dolphin external resources packer
* Github: update codeowners
* Scripts: proper slots handling in dolphin animation meta
* Scripts: correct frames enumeration and fix compiled assets.
* [FL-2209] Add Dolphin Deeds points and many more
* Remove excess frame_rate
* Change dolphin assets directory
* Scripts: add internal resource support to dolphin compiler
* Scripts: add internal assets generation, renaming
* Scripts: correct assert, renaming
* Code cleanup, documentation, fixes
* Update Levelup animations
* Rename essential -> blocking
* Fix Unlocked hint
* Scripts: rewrite Templite compiller, replace regexps with token parser, split block types into code and variable blocks. Update dolphin templates.
* Documentation: add key combos description and use information
* Scripts: cleanup templit, more debug info and add dev comment
Co-authored-by: あく <alleteam@gmail.com>
2022-01-29 09:20:41 +00:00
|
|
|
#include <dolphin/dolphin.h>
|
2021-11-26 13:53:51 +00:00
|
|
|
|
|
|
|
#define TAG "BadUSB"
|
|
|
|
#define WORKER_TAG TAG "Worker"
|
|
|
|
|
2023-02-08 15:01:00 +00:00
|
|
|
#define BADUSB_ASCII_TO_KEY(script, x) \
|
|
|
|
(((uint8_t)x < 128) ? (script->layout[(uint8_t)x]) : HID_KEYBOARD_NONE)
|
|
|
|
|
2021-11-26 13:53:51 +00:00
|
|
|
typedef enum {
|
2022-03-16 07:31:47 +00:00
|
|
|
WorkerEvtToggle = (1 << 0),
|
|
|
|
WorkerEvtEnd = (1 << 1),
|
|
|
|
WorkerEvtConnect = (1 << 2),
|
|
|
|
WorkerEvtDisconnect = (1 << 3),
|
2021-11-26 13:53:51 +00:00
|
|
|
} WorkerEvtFlags;
|
|
|
|
|
2022-03-23 13:35:25 +00:00
|
|
|
static const char ducky_cmd_id[] = {"ID"};
|
2023-03-20 17:22:03 +00:00
|
|
|
|
|
|
|
static const uint8_t numpad_keys[10] = {
|
|
|
|
HID_KEYPAD_0,
|
|
|
|
HID_KEYPAD_1,
|
|
|
|
HID_KEYPAD_2,
|
|
|
|
HID_KEYPAD_3,
|
|
|
|
HID_KEYPAD_4,
|
|
|
|
HID_KEYPAD_5,
|
|
|
|
HID_KEYPAD_6,
|
|
|
|
HID_KEYPAD_7,
|
|
|
|
HID_KEYPAD_8,
|
|
|
|
HID_KEYPAD_9,
|
|
|
|
};
|
2022-01-02 17:34:39 +00:00
|
|
|
|
2023-03-09 17:42:34 +00:00
|
|
|
uint32_t ducky_get_command_len(const char* line) {
|
2021-11-26 13:53:51 +00:00
|
|
|
uint32_t len = strlen(line);
|
|
|
|
for(uint32_t i = 0; i < len; i++) {
|
|
|
|
if(line[i] == ' ') return i;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-09 17:42:34 +00:00
|
|
|
bool ducky_is_line_end(const char chr) {
|
2022-03-16 07:31:47 +00:00
|
|
|
return ((chr == ' ') || (chr == '\0') || (chr == '\r') || (chr == '\n'));
|
|
|
|
}
|
|
|
|
|
2023-03-09 17:42:34 +00:00
|
|
|
uint16_t ducky_get_keycode(BadUsbScript* bad_usb, const char* param, bool accept_chars) {
|
2023-03-20 17:22:03 +00:00
|
|
|
uint16_t keycode = ducky_get_keycode_by_name(param);
|
|
|
|
if(keycode != HID_KEYBOARD_NONE) {
|
|
|
|
return keycode;
|
2021-11-26 13:53:51 +00:00
|
|
|
}
|
2023-03-20 17:22:03 +00:00
|
|
|
|
2021-11-26 13:53:51 +00:00
|
|
|
if((accept_chars) && (strlen(param) > 0)) {
|
2023-02-08 15:01:00 +00:00
|
|
|
return (BADUSB_ASCII_TO_KEY(bad_usb, param[0]) & 0xFF);
|
2021-11-26 13:53:51 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-20 17:22:03 +00:00
|
|
|
bool ducky_get_number(const char* param, uint32_t* val) {
|
|
|
|
uint32_t value = 0;
|
|
|
|
if(sscanf(param, "%lu", &value) == 1) {
|
|
|
|
*val = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ducky_numlock_on() {
|
|
|
|
if((furi_hal_hid_get_led_state() & HID_KB_LED_NUM) == 0) {
|
|
|
|
furi_hal_hid_kb_press(HID_KEYBOARD_LOCK_NUM_LOCK);
|
|
|
|
furi_hal_hid_kb_release(HID_KEYBOARD_LOCK_NUM_LOCK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool ducky_numpad_press(const char num) {
|
|
|
|
if((num < '0') || (num > '9')) return false;
|
|
|
|
|
|
|
|
uint16_t key = numpad_keys[num - '0'];
|
|
|
|
furi_hal_hid_kb_press(key);
|
|
|
|
furi_hal_hid_kb_release(key);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ducky_altchar(const char* charcode) {
|
|
|
|
uint8_t i = 0;
|
|
|
|
bool state = false;
|
|
|
|
|
|
|
|
furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT);
|
|
|
|
|
|
|
|
while(!ducky_is_line_end(charcode[i])) {
|
|
|
|
state = ducky_numpad_press(charcode[i]);
|
|
|
|
if(state == false) break;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
furi_hal_hid_kb_release(KEY_MOD_LEFT_ALT);
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ducky_altstring(const char* param) {
|
|
|
|
uint32_t i = 0;
|
|
|
|
bool state = false;
|
|
|
|
|
|
|
|
while(param[i] != '\0') {
|
|
|
|
if((param[i] < ' ') || (param[i] > '~')) {
|
|
|
|
i++;
|
|
|
|
continue; // Skip non-printable chars
|
|
|
|
}
|
|
|
|
|
|
|
|
char temp_str[4];
|
|
|
|
snprintf(temp_str, 4, "%u", param[i]);
|
|
|
|
|
|
|
|
state = ducky_altchar(temp_str);
|
|
|
|
if(state == false) break;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t ducky_error(BadUsbScript* bad_usb, const char* text, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, text);
|
|
|
|
|
|
|
|
vsnprintf(bad_usb->st.error, sizeof(bad_usb->st.error), text, args);
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
return SCRIPT_STATE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ducky_string(BadUsbScript* bad_usb, const char* param) {
|
|
|
|
uint32_t i = 0;
|
|
|
|
|
|
|
|
while(param[i] != '\0') {
|
|
|
|
if(param[i] != '\n') {
|
|
|
|
uint16_t keycode = BADUSB_ASCII_TO_KEY(bad_usb, param[i]);
|
|
|
|
if(keycode != HID_KEYBOARD_NONE) {
|
|
|
|
furi_hal_hid_kb_press(keycode);
|
|
|
|
furi_hal_hid_kb_release(keycode);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
furi_hal_hid_kb_press(HID_KEYBOARD_RETURN);
|
|
|
|
furi_hal_hid_kb_release(HID_KEYBOARD_RETURN);
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
bad_usb->stringdelay = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ducky_string_next(BadUsbScript* bad_usb) {
|
|
|
|
if(bad_usb->string_print_pos >= furi_string_size(bad_usb->string_print)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
char print_char = furi_string_get_char(bad_usb->string_print, bad_usb->string_print_pos);
|
|
|
|
|
|
|
|
if(print_char != '\n') {
|
|
|
|
uint16_t keycode = BADUSB_ASCII_TO_KEY(bad_usb, print_char);
|
|
|
|
if(keycode != HID_KEYBOARD_NONE) {
|
|
|
|
furi_hal_hid_kb_press(keycode);
|
|
|
|
furi_hal_hid_kb_release(keycode);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
furi_hal_hid_kb_press(HID_KEYBOARD_RETURN);
|
|
|
|
furi_hal_hid_kb_release(HID_KEYBOARD_RETURN);
|
|
|
|
}
|
|
|
|
|
|
|
|
bad_usb->string_print_pos++;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t ducky_parse_line(BadUsbScript* bad_usb, FuriString* line) {
|
2022-10-05 15:15:23 +00:00
|
|
|
uint32_t line_len = furi_string_size(line);
|
|
|
|
const char* line_tmp = furi_string_get_cstr(line);
|
2021-11-26 13:53:51 +00:00
|
|
|
|
2022-11-12 11:55:42 +00:00
|
|
|
if(line_len == 0) {
|
|
|
|
return SCRIPT_STATE_NEXT_LINE; // Skip empty lines
|
2021-11-26 13:53:51 +00:00
|
|
|
}
|
2022-03-23 13:35:25 +00:00
|
|
|
FURI_LOG_D(WORKER_TAG, "line:%s", line_tmp);
|
2023-03-20 17:22:03 +00:00
|
|
|
|
2023-03-09 17:42:34 +00:00
|
|
|
// Ducky Lang Functions
|
2023-03-20 17:22:03 +00:00
|
|
|
int32_t cmd_result = ducky_execute_cmd(bad_usb, line_tmp);
|
|
|
|
if(cmd_result != SCRIPT_STATE_CMD_UNKNOWN) {
|
|
|
|
return cmd_result;
|
2023-03-09 17:42:34 +00:00
|
|
|
}
|
2023-03-20 17:22:03 +00:00
|
|
|
|
2023-03-09 17:42:34 +00:00
|
|
|
// Special keys + modifiers
|
|
|
|
uint16_t key = ducky_get_keycode(bad_usb, line_tmp, false);
|
|
|
|
if(key == HID_KEYBOARD_NONE) {
|
2023-03-20 17:22:03 +00:00
|
|
|
return ducky_error(bad_usb, "No keycode defined for %s", line_tmp);
|
2023-03-09 17:42:34 +00:00
|
|
|
}
|
|
|
|
if((key & 0xFF00) != 0) {
|
|
|
|
// It's a modifier key
|
2022-10-08 17:56:56 +00:00
|
|
|
line_tmp = &line_tmp[ducky_get_command_len(line_tmp) + 1];
|
2023-03-09 17:42:34 +00:00
|
|
|
key |= ducky_get_keycode(bad_usb, line_tmp, true);
|
2021-11-26 13:53:51 +00:00
|
|
|
}
|
2023-03-09 17:42:34 +00:00
|
|
|
furi_hal_hid_kb_press(key);
|
|
|
|
furi_hal_hid_kb_release(key);
|
2023-03-20 17:22:03 +00:00
|
|
|
return 0;
|
2021-11-26 13:53:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 13:35:25 +00:00
|
|
|
static bool ducky_set_usb_id(BadUsbScript* bad_usb, const char* line) {
|
|
|
|
if(sscanf(line, "%lX:%lX", &bad_usb->hid_cfg.vid, &bad_usb->hid_cfg.pid) == 2) {
|
|
|
|
bad_usb->hid_cfg.manuf[0] = '\0';
|
|
|
|
bad_usb->hid_cfg.product[0] = '\0';
|
|
|
|
|
|
|
|
uint8_t id_len = ducky_get_command_len(line);
|
|
|
|
if(!ducky_is_line_end(line[id_len + 1])) {
|
|
|
|
sscanf(
|
|
|
|
&line[id_len + 1],
|
|
|
|
"%31[^\r\n:]:%31[^\r\n]",
|
|
|
|
bad_usb->hid_cfg.manuf,
|
|
|
|
bad_usb->hid_cfg.product);
|
|
|
|
}
|
|
|
|
FURI_LOG_D(
|
|
|
|
WORKER_TAG,
|
2022-10-07 13:35:15 +00:00
|
|
|
"set id: %04lX:%04lX mfr:%s product:%s",
|
2022-03-23 13:35:25 +00:00
|
|
|
bad_usb->hid_cfg.vid,
|
|
|
|
bad_usb->hid_cfg.pid,
|
|
|
|
bad_usb->hid_cfg.manuf,
|
|
|
|
bad_usb->hid_cfg.product);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-26 13:53:51 +00:00
|
|
|
static bool ducky_script_preload(BadUsbScript* bad_usb, File* script_file) {
|
|
|
|
uint8_t ret = 0;
|
|
|
|
uint32_t line_len = 0;
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_reset(bad_usb->line);
|
2022-03-23 13:35:25 +00:00
|
|
|
|
2021-11-26 13:53:51 +00:00
|
|
|
do {
|
|
|
|
ret = storage_file_read(script_file, bad_usb->file_buf, FILE_BUFFER_LEN);
|
|
|
|
for(uint16_t i = 0; i < ret; i++) {
|
|
|
|
if(bad_usb->file_buf[i] == '\n' && line_len > 0) {
|
|
|
|
bad_usb->st.line_nb++;
|
|
|
|
line_len = 0;
|
|
|
|
} else {
|
2022-03-23 13:35:25 +00:00
|
|
|
if(bad_usb->st.line_nb == 0) { // Save first line
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_push_back(bad_usb->line, bad_usb->file_buf[i]);
|
2022-03-23 13:35:25 +00:00
|
|
|
}
|
2021-11-26 13:53:51 +00:00
|
|
|
line_len++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(storage_file_eof(script_file)) {
|
|
|
|
if(line_len > 0) {
|
|
|
|
bad_usb->st.line_nb++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while(ret > 0);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
const char* line_tmp = furi_string_get_cstr(bad_usb->line);
|
2022-03-23 13:35:25 +00:00
|
|
|
bool id_set = false; // Looking for ID command at first line
|
|
|
|
if(strncmp(line_tmp, ducky_cmd_id, strlen(ducky_cmd_id)) == 0) {
|
|
|
|
id_set = ducky_set_usb_id(bad_usb, &line_tmp[strlen(ducky_cmd_id) + 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(id_set) {
|
2022-03-24 17:23:40 +00:00
|
|
|
furi_check(furi_hal_usb_set_config(&usb_hid, &bad_usb->hid_cfg));
|
2022-03-23 13:35:25 +00:00
|
|
|
} else {
|
2022-03-24 17:23:40 +00:00
|
|
|
furi_check(furi_hal_usb_set_config(&usb_hid, NULL));
|
2022-03-23 13:35:25 +00:00
|
|
|
}
|
|
|
|
|
2021-11-26 13:53:51 +00:00
|
|
|
storage_file_seek(script_file, 0, true);
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_reset(bad_usb->line);
|
2021-11-26 13:53:51 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t ducky_script_execute_next(BadUsbScript* bad_usb, File* script_file) {
|
|
|
|
int32_t delay_val = 0;
|
|
|
|
|
|
|
|
if(bad_usb->repeat_cnt > 0) {
|
|
|
|
bad_usb->repeat_cnt--;
|
2023-03-20 17:22:03 +00:00
|
|
|
delay_val = ducky_parse_line(bad_usb, bad_usb->line_prev);
|
2022-03-16 07:31:47 +00:00
|
|
|
if(delay_val == SCRIPT_STATE_NEXT_LINE) { // Empty line
|
|
|
|
return 0;
|
|
|
|
} else if(delay_val < 0) { // Script error
|
2021-11-26 13:53:51 +00:00
|
|
|
bad_usb->st.error_line = bad_usb->st.line_cur - 1;
|
2022-12-26 12:13:30 +00:00
|
|
|
FURI_LOG_E(WORKER_TAG, "Unknown command at line %u", bad_usb->st.line_cur - 1U);
|
2022-03-16 07:31:47 +00:00
|
|
|
return SCRIPT_STATE_ERROR;
|
2021-11-26 13:53:51 +00:00
|
|
|
} else {
|
|
|
|
return (delay_val + bad_usb->defdelay);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_set(bad_usb->line_prev, bad_usb->line);
|
|
|
|
furi_string_reset(bad_usb->line);
|
2021-11-26 13:53:51 +00:00
|
|
|
|
|
|
|
while(1) {
|
|
|
|
if(bad_usb->buf_len == 0) {
|
|
|
|
bad_usb->buf_len = storage_file_read(script_file, bad_usb->file_buf, FILE_BUFFER_LEN);
|
|
|
|
if(storage_file_eof(script_file)) {
|
|
|
|
if((bad_usb->buf_len < FILE_BUFFER_LEN) && (bad_usb->file_end == false)) {
|
|
|
|
bad_usb->file_buf[bad_usb->buf_len] = '\n';
|
|
|
|
bad_usb->buf_len++;
|
|
|
|
bad_usb->file_end = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bad_usb->buf_start = 0;
|
2022-03-16 07:31:47 +00:00
|
|
|
if(bad_usb->buf_len == 0) return SCRIPT_STATE_END;
|
2021-11-26 13:53:51 +00:00
|
|
|
}
|
|
|
|
for(uint8_t i = bad_usb->buf_start; i < (bad_usb->buf_start + bad_usb->buf_len); i++) {
|
2022-10-05 15:15:23 +00:00
|
|
|
if(bad_usb->file_buf[i] == '\n' && furi_string_size(bad_usb->line) > 0) {
|
2021-11-26 13:53:51 +00:00
|
|
|
bad_usb->st.line_cur++;
|
|
|
|
bad_usb->buf_len = bad_usb->buf_len + bad_usb->buf_start - (i + 1);
|
|
|
|
bad_usb->buf_start = i + 1;
|
2022-11-12 11:55:42 +00:00
|
|
|
furi_string_trim(bad_usb->line);
|
2023-03-20 17:22:03 +00:00
|
|
|
delay_val = ducky_parse_line(bad_usb, bad_usb->line);
|
2022-11-12 11:55:42 +00:00
|
|
|
if(delay_val == SCRIPT_STATE_NEXT_LINE) { // Empty line
|
|
|
|
return 0;
|
2023-03-20 17:22:03 +00:00
|
|
|
} else if(delay_val == SCRIPT_STATE_STRING_START) { // Print string with delays
|
|
|
|
return delay_val;
|
2022-11-12 11:55:42 +00:00
|
|
|
} else if(delay_val < 0) {
|
2021-11-26 13:53:51 +00:00
|
|
|
bad_usb->st.error_line = bad_usb->st.line_cur;
|
2022-10-07 13:35:15 +00:00
|
|
|
FURI_LOG_E(WORKER_TAG, "Unknown command at line %u", bad_usb->st.line_cur);
|
2022-03-16 07:31:47 +00:00
|
|
|
return SCRIPT_STATE_ERROR;
|
2021-11-26 13:53:51 +00:00
|
|
|
} else {
|
|
|
|
return (delay_val + bad_usb->defdelay);
|
|
|
|
}
|
|
|
|
} else {
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_push_back(bad_usb->line, bad_usb->file_buf[i]);
|
2021-11-26 13:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
bad_usb->buf_len = 0;
|
2022-03-16 07:31:47 +00:00
|
|
|
if(bad_usb->file_end) return SCRIPT_STATE_END;
|
2021-11-26 13:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bad_usb_hid_state_callback(bool state, void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
BadUsbScript* bad_usb = context;
|
|
|
|
|
2023-03-20 17:22:03 +00:00
|
|
|
if(state == true) {
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(bad_usb->thread), WorkerEvtConnect);
|
2023-03-20 17:22:03 +00:00
|
|
|
} else {
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(bad_usb->thread), WorkerEvtDisconnect);
|
2023-03-20 17:22:03 +00:00
|
|
|
}
|
2021-11-26 13:53:51 +00:00
|
|
|
}
|
|
|
|
|
2023-02-25 17:34:48 +00:00
|
|
|
static uint32_t bad_usb_flags_get(uint32_t flags_mask, uint32_t timeout) {
|
|
|
|
uint32_t flags = furi_thread_flags_get();
|
|
|
|
furi_check((flags & FuriFlagError) == 0);
|
|
|
|
if(flags == 0) {
|
|
|
|
flags = furi_thread_flags_wait(flags_mask, FuriFlagWaitAny, timeout);
|
2023-02-26 08:08:05 +00:00
|
|
|
furi_check(((flags & FuriFlagError) == 0) || (flags == (unsigned)FuriFlagErrorTimeout));
|
2023-02-25 17:34:48 +00:00
|
|
|
} else {
|
|
|
|
uint32_t state = furi_thread_flags_clear(flags);
|
|
|
|
furi_check((state & FuriFlagError) == 0);
|
|
|
|
}
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2021-11-26 13:53:51 +00:00
|
|
|
static int32_t bad_usb_worker(void* context) {
|
|
|
|
BadUsbScript* bad_usb = context;
|
|
|
|
|
|
|
|
BadUsbWorkerState worker_state = BadUsbStateInit;
|
|
|
|
int32_t delay_val = 0;
|
|
|
|
|
|
|
|
FURI_LOG_I(WORKER_TAG, "Init");
|
2022-07-26 12:21:51 +00:00
|
|
|
File* script_file = storage_file_alloc(furi_record_open(RECORD_STORAGE));
|
2022-10-05 15:15:23 +00:00
|
|
|
bad_usb->line = furi_string_alloc();
|
|
|
|
bad_usb->line_prev = furi_string_alloc();
|
2023-03-20 17:22:03 +00:00
|
|
|
bad_usb->string_print = furi_string_alloc();
|
2021-11-26 13:53:51 +00:00
|
|
|
|
|
|
|
furi_hal_hid_set_state_callback(bad_usb_hid_state_callback, bad_usb);
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
if(worker_state == BadUsbStateInit) { // State: initialization
|
|
|
|
if(storage_file_open(
|
|
|
|
script_file,
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_get_cstr(bad_usb->file_path),
|
2021-11-26 13:53:51 +00:00
|
|
|
FSAM_READ,
|
|
|
|
FSOM_OPEN_EXISTING)) {
|
|
|
|
if((ducky_script_preload(bad_usb, script_file)) && (bad_usb->st.line_nb > 0)) {
|
|
|
|
if(furi_hal_hid_is_connected()) {
|
|
|
|
worker_state = BadUsbStateIdle; // Ready to run
|
|
|
|
} else {
|
|
|
|
worker_state = BadUsbStateNotConnected; // USB not connected
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
worker_state = BadUsbStateScriptError; // Script preload error
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
FURI_LOG_E(WORKER_TAG, "File open error");
|
|
|
|
worker_state = BadUsbStateFileError; // File open error
|
|
|
|
}
|
|
|
|
bad_usb->st.state = worker_state;
|
|
|
|
|
|
|
|
} else if(worker_state == BadUsbStateNotConnected) { // State: USB not connected
|
2023-02-25 17:34:48 +00:00
|
|
|
uint32_t flags = bad_usb_flags_get(
|
|
|
|
WorkerEvtEnd | WorkerEvtConnect | WorkerEvtToggle, FuriWaitForever);
|
|
|
|
|
2021-11-26 13:53:51 +00:00
|
|
|
if(flags & WorkerEvtEnd) {
|
|
|
|
break;
|
|
|
|
} else if(flags & WorkerEvtConnect) {
|
|
|
|
worker_state = BadUsbStateIdle; // Ready to run
|
2022-11-03 05:21:44 +00:00
|
|
|
} else if(flags & WorkerEvtToggle) {
|
|
|
|
worker_state = BadUsbStateWillRun; // Will run when USB is connected
|
2021-11-26 13:53:51 +00:00
|
|
|
}
|
|
|
|
bad_usb->st.state = worker_state;
|
|
|
|
|
|
|
|
} else if(worker_state == BadUsbStateIdle) { // State: ready to start
|
2023-02-25 17:34:48 +00:00
|
|
|
uint32_t flags = bad_usb_flags_get(
|
|
|
|
WorkerEvtEnd | WorkerEvtToggle | WorkerEvtDisconnect, FuriWaitForever);
|
|
|
|
|
2021-11-26 13:53:51 +00:00
|
|
|
if(flags & WorkerEvtEnd) {
|
|
|
|
break;
|
|
|
|
} else if(flags & WorkerEvtToggle) { // Start executing script
|
[FL-2183] [FL-2209] Dolphin Deeds, Level up, assets generation, refactoring (#965)
* Desktop: cleanup headers
* Get loader pubsub via record
* [FL-2183] Dolphin refactoring 2022.01
* Restruct animations assets structure
* Rename assets
* Cleanup headers
* Update Recording animation
* Add BadBattery animation
* Provide loader's pubsub via record
* Fix load/unload animations
* Scripts: add flipper format support, initial dolphin packager rework. Assets: internal and external dolphin.
* Sync internal meta.txt and manifest.txt
* Reorder, rename dolphin assets
* Split essential generated assets
* Add ReadMe for dolphin assets
* Separate essential blocking animations
* Scripts: full dolphin validation before packaging
* Assets, Scripts: dolphin external resources packer
* Github: update codeowners
* Scripts: proper slots handling in dolphin animation meta
* Scripts: correct frames enumeration and fix compiled assets.
* [FL-2209] Add Dolphin Deeds points and many more
* Remove excess frame_rate
* Change dolphin assets directory
* Scripts: add internal resource support to dolphin compiler
* Scripts: add internal assets generation, renaming
* Scripts: correct assert, renaming
* Code cleanup, documentation, fixes
* Update Levelup animations
* Rename essential -> blocking
* Fix Unlocked hint
* Scripts: rewrite Templite compiller, replace regexps with token parser, split block types into code and variable blocks. Update dolphin templates.
* Documentation: add key combos description and use information
* Scripts: cleanup templit, more debug info and add dev comment
Co-authored-by: あく <alleteam@gmail.com>
2022-01-29 09:20:41 +00:00
|
|
|
DOLPHIN_DEED(DolphinDeedBadUsbPlayScript);
|
2021-11-26 13:53:51 +00:00
|
|
|
delay_val = 0;
|
|
|
|
bad_usb->buf_len = 0;
|
|
|
|
bad_usb->st.line_cur = 0;
|
|
|
|
bad_usb->defdelay = 0;
|
2023-02-25 17:34:48 +00:00
|
|
|
bad_usb->stringdelay = 0;
|
2021-11-26 13:53:51 +00:00
|
|
|
bad_usb->repeat_cnt = 0;
|
2023-03-20 17:22:03 +00:00
|
|
|
bad_usb->key_hold_nb = 0;
|
2021-11-26 13:53:51 +00:00
|
|
|
bad_usb->file_end = false;
|
|
|
|
storage_file_seek(script_file, 0, true);
|
|
|
|
worker_state = BadUsbStateRunning;
|
|
|
|
} else if(flags & WorkerEvtDisconnect) {
|
|
|
|
worker_state = BadUsbStateNotConnected; // USB disconnected
|
|
|
|
}
|
|
|
|
bad_usb->st.state = worker_state;
|
2022-11-03 05:21:44 +00:00
|
|
|
|
|
|
|
} else if(worker_state == BadUsbStateWillRun) { // State: start on connection
|
2023-02-25 17:34:48 +00:00
|
|
|
uint32_t flags = bad_usb_flags_get(
|
|
|
|
WorkerEvtEnd | WorkerEvtConnect | WorkerEvtToggle, FuriWaitForever);
|
|
|
|
|
2022-11-03 05:21:44 +00:00
|
|
|
if(flags & WorkerEvtEnd) {
|
|
|
|
break;
|
|
|
|
} else if(flags & WorkerEvtConnect) { // Start executing script
|
|
|
|
DOLPHIN_DEED(DolphinDeedBadUsbPlayScript);
|
|
|
|
delay_val = 0;
|
|
|
|
bad_usb->buf_len = 0;
|
|
|
|
bad_usb->st.line_cur = 0;
|
|
|
|
bad_usb->defdelay = 0;
|
2023-02-25 17:34:48 +00:00
|
|
|
bad_usb->stringdelay = 0;
|
2022-11-03 05:21:44 +00:00
|
|
|
bad_usb->repeat_cnt = 0;
|
|
|
|
bad_usb->file_end = false;
|
|
|
|
storage_file_seek(script_file, 0, true);
|
|
|
|
// extra time for PC to recognize Flipper as keyboard
|
2023-02-25 17:34:48 +00:00
|
|
|
flags = furi_thread_flags_wait(
|
|
|
|
WorkerEvtEnd | WorkerEvtDisconnect | WorkerEvtToggle,
|
|
|
|
FuriFlagWaitAny | FuriFlagNoClear,
|
|
|
|
1500);
|
2023-02-26 08:08:05 +00:00
|
|
|
if(flags == (unsigned)FuriFlagErrorTimeout) {
|
2023-02-25 17:34:48 +00:00
|
|
|
// If nothing happened - start script execution
|
|
|
|
worker_state = BadUsbStateRunning;
|
|
|
|
} else if(flags & WorkerEvtToggle) {
|
|
|
|
worker_state = BadUsbStateIdle;
|
|
|
|
furi_thread_flags_clear(WorkerEvtToggle);
|
|
|
|
}
|
2022-11-03 05:21:44 +00:00
|
|
|
} else if(flags & WorkerEvtToggle) { // Cancel scheduled execution
|
|
|
|
worker_state = BadUsbStateNotConnected;
|
|
|
|
}
|
|
|
|
bad_usb->st.state = worker_state;
|
2021-11-26 13:53:51 +00:00
|
|
|
|
|
|
|
} else if(worker_state == BadUsbStateRunning) { // State: running
|
|
|
|
uint16_t delay_cur = (delay_val > 1000) ? (1000) : (delay_val);
|
2022-06-20 14:54:48 +00:00
|
|
|
uint32_t flags = furi_thread_flags_wait(
|
2022-07-20 10:56:33 +00:00
|
|
|
WorkerEvtEnd | WorkerEvtToggle | WorkerEvtDisconnect, FuriFlagWaitAny, delay_cur);
|
2023-02-25 17:34:48 +00:00
|
|
|
|
2021-11-26 13:53:51 +00:00
|
|
|
delay_val -= delay_cur;
|
2022-07-20 10:56:33 +00:00
|
|
|
if(!(flags & FuriFlagError)) {
|
2021-11-26 13:53:51 +00:00
|
|
|
if(flags & WorkerEvtEnd) {
|
|
|
|
break;
|
|
|
|
} else if(flags & WorkerEvtToggle) {
|
|
|
|
worker_state = BadUsbStateIdle; // Stop executing script
|
|
|
|
furi_hal_hid_kb_release_all();
|
|
|
|
} else if(flags & WorkerEvtDisconnect) {
|
|
|
|
worker_state = BadUsbStateNotConnected; // USB disconnected
|
|
|
|
furi_hal_hid_kb_release_all();
|
|
|
|
}
|
|
|
|
bad_usb->st.state = worker_state;
|
|
|
|
continue;
|
2022-12-26 12:13:30 +00:00
|
|
|
} else if(
|
|
|
|
(flags == (unsigned)FuriFlagErrorTimeout) ||
|
|
|
|
(flags == (unsigned)FuriFlagErrorResource)) {
|
2021-11-26 13:53:51 +00:00
|
|
|
if(delay_val > 0) {
|
|
|
|
bad_usb->st.delay_remain--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bad_usb->st.state = BadUsbStateRunning;
|
|
|
|
delay_val = ducky_script_execute_next(bad_usb, script_file);
|
2022-03-16 07:31:47 +00:00
|
|
|
if(delay_val == SCRIPT_STATE_ERROR) { // Script error
|
2021-11-26 13:53:51 +00:00
|
|
|
delay_val = 0;
|
|
|
|
worker_state = BadUsbStateScriptError;
|
|
|
|
bad_usb->st.state = worker_state;
|
2023-03-20 17:22:03 +00:00
|
|
|
furi_hal_hid_kb_release_all();
|
2022-03-16 07:31:47 +00:00
|
|
|
} else if(delay_val == SCRIPT_STATE_END) { // End of script
|
2021-11-26 13:53:51 +00:00
|
|
|
delay_val = 0;
|
|
|
|
worker_state = BadUsbStateIdle;
|
|
|
|
bad_usb->st.state = BadUsbStateDone;
|
|
|
|
furi_hal_hid_kb_release_all();
|
|
|
|
continue;
|
2023-03-20 17:22:03 +00:00
|
|
|
} else if(delay_val == SCRIPT_STATE_STRING_START) { // Start printing string with delays
|
|
|
|
delay_val = bad_usb->defdelay;
|
|
|
|
bad_usb->string_print_pos = 0;
|
|
|
|
worker_state = BadUsbStateStringDelay;
|
2021-11-26 13:53:51 +00:00
|
|
|
} else if(delay_val > 1000) {
|
|
|
|
bad_usb->st.state = BadUsbStateDelay; // Show long delays
|
|
|
|
bad_usb->st.delay_remain = delay_val / 1000;
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check((flags & FuriFlagError) == 0);
|
2021-11-26 13:53:51 +00:00
|
|
|
}
|
2023-03-20 17:22:03 +00:00
|
|
|
} else if(worker_state == BadUsbStateStringDelay) { // State: print string with delays
|
|
|
|
uint32_t flags = furi_thread_flags_wait(
|
|
|
|
WorkerEvtEnd | WorkerEvtToggle | WorkerEvtDisconnect,
|
|
|
|
FuriFlagWaitAny,
|
|
|
|
bad_usb->stringdelay);
|
2021-11-26 13:53:51 +00:00
|
|
|
|
2023-03-20 17:22:03 +00:00
|
|
|
if(!(flags & FuriFlagError)) {
|
|
|
|
if(flags & WorkerEvtEnd) {
|
|
|
|
break;
|
|
|
|
} else if(flags & WorkerEvtToggle) {
|
|
|
|
worker_state = BadUsbStateIdle; // Stop executing script
|
|
|
|
furi_hal_hid_kb_release_all();
|
|
|
|
} else if(flags & WorkerEvtDisconnect) {
|
|
|
|
worker_state = BadUsbStateNotConnected; // USB disconnected
|
|
|
|
furi_hal_hid_kb_release_all();
|
|
|
|
}
|
|
|
|
bad_usb->st.state = worker_state;
|
|
|
|
continue;
|
|
|
|
} else if(
|
|
|
|
(flags == (unsigned)FuriFlagErrorTimeout) ||
|
|
|
|
(flags == (unsigned)FuriFlagErrorResource)) {
|
|
|
|
bool string_end = ducky_string_next(bad_usb);
|
|
|
|
if(string_end) {
|
|
|
|
bad_usb->stringdelay = 0;
|
|
|
|
worker_state = BadUsbStateRunning;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
furi_check((flags & FuriFlagError) == 0);
|
|
|
|
}
|
2021-11-26 13:53:51 +00:00
|
|
|
} else if(
|
|
|
|
(worker_state == BadUsbStateFileError) ||
|
|
|
|
(worker_state == BadUsbStateScriptError)) { // State: error
|
2023-02-25 17:34:48 +00:00
|
|
|
uint32_t flags =
|
|
|
|
bad_usb_flags_get(WorkerEvtEnd, FuriWaitForever); // Waiting for exit command
|
|
|
|
|
2021-11-26 13:53:51 +00:00
|
|
|
if(flags & WorkerEvtEnd) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
furi_hal_hid_set_state_callback(NULL, NULL);
|
|
|
|
|
|
|
|
storage_file_close(script_file);
|
|
|
|
storage_file_free(script_file);
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(bad_usb->line);
|
|
|
|
furi_string_free(bad_usb->line_prev);
|
2023-03-20 17:22:03 +00:00
|
|
|
furi_string_free(bad_usb->string_print);
|
2021-11-26 13:53:51 +00:00
|
|
|
|
|
|
|
FURI_LOG_I(WORKER_TAG, "End");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-02-08 15:01:00 +00:00
|
|
|
static void bad_usb_script_set_default_keyboard_layout(BadUsbScript* bad_usb) {
|
|
|
|
furi_assert(bad_usb);
|
|
|
|
memset(bad_usb->layout, HID_KEYBOARD_NONE, sizeof(bad_usb->layout));
|
|
|
|
memcpy(bad_usb->layout, hid_asciimap, MIN(sizeof(hid_asciimap), sizeof(bad_usb->layout)));
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
BadUsbScript* bad_usb_script_open(FuriString* file_path) {
|
2021-11-26 13:53:51 +00:00
|
|
|
furi_assert(file_path);
|
|
|
|
|
2022-12-26 12:13:30 +00:00
|
|
|
BadUsbScript* bad_usb = malloc(sizeof(BadUsbScript));
|
2022-10-05 15:15:23 +00:00
|
|
|
bad_usb->file_path = furi_string_alloc();
|
|
|
|
furi_string_set(bad_usb->file_path, file_path);
|
2023-02-08 15:01:00 +00:00
|
|
|
bad_usb_script_set_default_keyboard_layout(bad_usb);
|
2021-11-26 13:53:51 +00:00
|
|
|
|
|
|
|
bad_usb->st.state = BadUsbStateInit;
|
2022-10-12 16:21:54 +00:00
|
|
|
bad_usb->st.error[0] = '\0';
|
2021-11-26 13:53:51 +00:00
|
|
|
|
2022-11-23 12:49:17 +00:00
|
|
|
bad_usb->thread = furi_thread_alloc_ex("BadUsbWorker", 2048, bad_usb_worker, bad_usb);
|
2021-11-26 13:53:51 +00:00
|
|
|
furi_thread_start(bad_usb->thread);
|
|
|
|
return bad_usb;
|
2022-12-26 12:13:30 +00:00
|
|
|
} //-V773
|
2021-11-26 13:53:51 +00:00
|
|
|
|
|
|
|
void bad_usb_script_close(BadUsbScript* bad_usb) {
|
|
|
|
furi_assert(bad_usb);
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(bad_usb->thread), WorkerEvtEnd);
|
2021-11-26 13:53:51 +00:00
|
|
|
furi_thread_join(bad_usb->thread);
|
|
|
|
furi_thread_free(bad_usb->thread);
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(bad_usb->file_path);
|
2021-11-26 13:53:51 +00:00
|
|
|
free(bad_usb);
|
|
|
|
}
|
|
|
|
|
2023-02-08 15:01:00 +00:00
|
|
|
void bad_usb_script_set_keyboard_layout(BadUsbScript* bad_usb, FuriString* layout_path) {
|
|
|
|
furi_assert(bad_usb);
|
|
|
|
|
|
|
|
if((bad_usb->st.state == BadUsbStateRunning) || (bad_usb->st.state == BadUsbStateDelay)) {
|
|
|
|
// do not update keyboard layout while a script is running
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
File* layout_file = storage_file_alloc(furi_record_open(RECORD_STORAGE));
|
2023-02-08 15:26:45 +00:00
|
|
|
if(!furi_string_empty(layout_path)) { //-V1051
|
2023-02-08 15:01:00 +00:00
|
|
|
if(storage_file_open(
|
|
|
|
layout_file, furi_string_get_cstr(layout_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
|
|
|
|
uint16_t layout[128];
|
|
|
|
if(storage_file_read(layout_file, layout, sizeof(layout)) == sizeof(layout)) {
|
|
|
|
memcpy(bad_usb->layout, layout, sizeof(layout));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
storage_file_close(layout_file);
|
|
|
|
} else {
|
|
|
|
bad_usb_script_set_default_keyboard_layout(bad_usb);
|
|
|
|
}
|
|
|
|
storage_file_free(layout_file);
|
|
|
|
}
|
|
|
|
|
2021-11-26 13:53:51 +00:00
|
|
|
void bad_usb_script_toggle(BadUsbScript* bad_usb) {
|
|
|
|
furi_assert(bad_usb);
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(bad_usb->thread), WorkerEvtToggle);
|
2021-11-26 13:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BadUsbState* bad_usb_script_get_state(BadUsbScript* bad_usb) {
|
|
|
|
furi_assert(bad_usb);
|
|
|
|
return &(bad_usb->st);
|
|
|
|
}
|