mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-04-24 03:29:57 -07:00
Refactor BadKB app structure
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
#include "bad_kb_app.h"
|
||||
#include "bad_kb_app_i.h"
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include <storage/storage.h>
|
||||
#include <lib/toolbox/path.h>
|
||||
#include <xtreme/xtreme.h>
|
||||
#include <lib/flipper_format/flipper_format.h>
|
||||
|
||||
#include <bt/bt_service/bt_i.h>
|
||||
#include <bt/bt_service/bt.h>
|
||||
#include "helpers/ducky_script_i.h"
|
||||
|
||||
// Adjusts to serial MAC +2 in app init
|
||||
uint8_t BAD_KB_BOUND_MAC[GAP_MAC_ADDR_SIZE] = {0};
|
||||
|
||||
static bool bad_kb_app_custom_event_callback(void* context, uint32_t event) {
|
||||
furi_assert(context);
|
||||
@@ -114,6 +116,165 @@ void bad_kb_app_show_loading_popup(BadKbApp* app, bool show) {
|
||||
}
|
||||
}
|
||||
|
||||
int32_t bad_kb_conn_apply(BadKbApp* app) {
|
||||
if(app->is_bt) {
|
||||
bt_timeout = bt_hid_delays[LevelRssi39_0];
|
||||
bt_disconnect(app->bt);
|
||||
furi_delay_ms(200);
|
||||
bt_keys_storage_set_storage_path(app->bt, BAD_KB_KEYS_PATH);
|
||||
|
||||
// Setup new config
|
||||
BadKbConfig* cfg = app->set_bt_id ? &app->id_config : &app->config;
|
||||
memcpy(&app->cur_ble_cfg, &cfg->ble, sizeof(cfg->ble));
|
||||
app->cur_ble_cfg.bonding = app->bt_remember;
|
||||
if(app->bt_remember) {
|
||||
app->cur_ble_cfg.pairing = GapPairingPinCodeVerifyYesNo;
|
||||
} else {
|
||||
app->cur_ble_cfg.pairing = GapPairingNone;
|
||||
memcpy(app->cur_ble_cfg.mac, BAD_KB_BOUND_MAC, sizeof(BAD_KB_BOUND_MAC));
|
||||
}
|
||||
|
||||
// Set profile
|
||||
app->ble_hid = bt_profile_start(app->bt, ble_profile_hid, &app->cur_ble_cfg);
|
||||
furi_check(app->ble_hid);
|
||||
|
||||
// Advertise even if BT is off in settings
|
||||
furi_hal_bt_start_advertising();
|
||||
|
||||
app->conn_mode = BadKbConnModeBt;
|
||||
|
||||
} else {
|
||||
// Unlock RPC connections
|
||||
furi_hal_usb_unlock();
|
||||
|
||||
// Context will apply with set_config only if pointer address is different, so we use a copy
|
||||
FuriHalUsbHidConfig* cur_usb_cfg = malloc(sizeof(FuriHalUsbHidConfig));
|
||||
|
||||
// Setup new config
|
||||
BadKbConfig* cfg = app->set_usb_id ? &app->id_config : &app->config;
|
||||
memcpy(cur_usb_cfg, &cfg->usb, sizeof(cfg->usb));
|
||||
|
||||
// Set profile
|
||||
furi_check(furi_hal_usb_set_config(&usb_hid, cur_usb_cfg));
|
||||
if(app->cur_usb_cfg) free(app->cur_usb_cfg);
|
||||
app->cur_usb_cfg = cur_usb_cfg;
|
||||
|
||||
app->conn_mode = BadKbConnModeUsb;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void bad_kb_conn_reset(BadKbApp* app) {
|
||||
if(app->conn_mode == BadKbConnModeBt) {
|
||||
bt_disconnect(app->bt);
|
||||
furi_delay_ms(200);
|
||||
bt_keys_storage_set_default_path(app->bt);
|
||||
furi_check(bt_profile_restore_default(app->bt));
|
||||
} else if(app->conn_mode == BadKbConnModeUsb) {
|
||||
// TODO: maybe also restore USB context?
|
||||
furi_check(furi_hal_usb_set_config(app->prev_usb_mode, NULL));
|
||||
}
|
||||
|
||||
app->conn_mode = BadKbConnModeNone;
|
||||
}
|
||||
|
||||
void bad_kb_config_adjust(BadKbConfig* cfg) {
|
||||
// Avoid empty name
|
||||
if(cfg->ble.name[0] == '\0') {
|
||||
snprintf(
|
||||
cfg->ble.name, sizeof(cfg->ble.name), "Control %s", furi_hal_version_get_name_ptr());
|
||||
}
|
||||
|
||||
const uint8_t* normal_mac = furi_hal_version_get_ble_mac();
|
||||
uint8_t empty_mac[sizeof(cfg->ble.mac)] = {0};
|
||||
uint8_t default_mac[sizeof(cfg->ble.mac)] = {0x6c, 0x7a, 0xd8, 0xac, 0x57, 0x72}; //furi_hal_bt
|
||||
if(memcmp(cfg->ble.mac, empty_mac, sizeof(cfg->ble.mac)) == 0 ||
|
||||
memcmp(cfg->ble.mac, normal_mac, sizeof(cfg->ble.mac)) == 0 ||
|
||||
memcmp(cfg->ble.mac, default_mac, sizeof(cfg->ble.mac)) == 0) {
|
||||
memcpy(cfg->ble.mac, normal_mac, sizeof(cfg->ble.mac));
|
||||
cfg->ble.mac[2]++;
|
||||
}
|
||||
|
||||
// Use defaults if vid or pid are unset
|
||||
if(cfg->usb.vid == 0) cfg->usb.vid = HID_VID_DEFAULT;
|
||||
if(cfg->usb.pid == 0) cfg->usb.pid = HID_PID_DEFAULT;
|
||||
}
|
||||
|
||||
void bad_kb_config_refresh(BadKbApp* app) {
|
||||
bt_set_status_changed_callback(app->bt, NULL, NULL);
|
||||
furi_hal_hid_set_state_callback(NULL, NULL);
|
||||
if(app->bad_kb_script) {
|
||||
furi_thread_flags_set(furi_thread_get_id(app->bad_kb_script->thread), WorkerEvtDisconnect);
|
||||
}
|
||||
if(app->conn_init_thread) {
|
||||
furi_thread_join(app->conn_init_thread);
|
||||
}
|
||||
|
||||
bool apply = false;
|
||||
if(app->is_bt) {
|
||||
BadKbConfig* cfg = app->set_bt_id ? &app->id_config : &app->config;
|
||||
bad_kb_config_adjust(cfg);
|
||||
|
||||
if(app->conn_mode != BadKbConnModeBt) {
|
||||
apply = true;
|
||||
bad_kb_conn_reset(app);
|
||||
} else {
|
||||
BleProfileHidParams* cur = &app->cur_ble_cfg;
|
||||
apply = apply || cfg->ble.bonding != app->bt_remember;
|
||||
apply = apply || strncmp(cfg->ble.name, cur->name, sizeof(cfg->ble.name));
|
||||
apply = apply || memcmp(cfg->ble.mac, cur->mac, sizeof(cfg->ble.mac));
|
||||
}
|
||||
} else {
|
||||
BadKbConfig* cfg = app->set_usb_id ? &app->id_config : &app->config;
|
||||
bad_kb_config_adjust(cfg);
|
||||
|
||||
if(app->conn_mode != BadKbConnModeUsb) {
|
||||
apply = true;
|
||||
bad_kb_conn_reset(app);
|
||||
} else {
|
||||
FuriHalUsbHidConfig* cur = app->cur_usb_cfg;
|
||||
apply = apply || cfg->usb.vid != cur->vid;
|
||||
apply = apply || cfg->usb.pid != cur->pid;
|
||||
apply = apply || strncmp(cfg->usb.manuf, cur->manuf, sizeof(cur->manuf));
|
||||
apply = apply || strncmp(cfg->usb.product, cur->product, sizeof(cur->product));
|
||||
}
|
||||
}
|
||||
|
||||
if(apply) {
|
||||
bad_kb_conn_apply(app);
|
||||
}
|
||||
|
||||
if(app->bad_kb_script) {
|
||||
BadKbScript* script = app->bad_kb_script;
|
||||
script->st.is_bt = app->is_bt;
|
||||
script->bt = app->is_bt ? app->bt : NULL;
|
||||
bool connected;
|
||||
if(app->is_bt) {
|
||||
bt_set_status_changed_callback(app->bt, bad_kb_bt_hid_state_callback, script);
|
||||
connected = furi_hal_bt_is_connected();
|
||||
} else {
|
||||
furi_hal_hid_set_state_callback(bad_kb_usb_hid_state_callback, script);
|
||||
connected = furi_hal_hid_is_connected();
|
||||
}
|
||||
if(connected) {
|
||||
furi_thread_flags_set(furi_thread_get_id(script->thread), WorkerEvtConnect);
|
||||
}
|
||||
}
|
||||
|
||||
// Reload config page
|
||||
scene_manager_next_scene(app->scene_manager, BadKbSceneConfig);
|
||||
scene_manager_previous_scene(app->scene_manager);
|
||||
|
||||
// Update settings
|
||||
if(xtreme_settings.bad_bt != app->is_bt ||
|
||||
xtreme_settings.bad_bt_remember != app->bt_remember) {
|
||||
xtreme_settings.bad_bt = app->is_bt;
|
||||
xtreme_settings.bad_bt_remember = app->bt_remember;
|
||||
xtreme_settings_save();
|
||||
}
|
||||
}
|
||||
|
||||
BadKbApp* bad_kb_app_alloc(char* arg) {
|
||||
BadKbApp* app = malloc(sizeof(BadKbApp));
|
||||
|
||||
@@ -263,8 +424,8 @@ void bad_kb_app_free(BadKbApp* app) {
|
||||
free(app);
|
||||
}
|
||||
|
||||
int32_t bad_kb_app(char* p) {
|
||||
BadKbApp* bad_kb_app = bad_kb_app_alloc(p);
|
||||
int32_t bad_kb_app(void* p) {
|
||||
BadKbApp* bad_kb_app = bad_kb_app_alloc((char*)p);
|
||||
|
||||
view_dispatcher_run(bad_kb_app->view_dispatcher);
|
||||
|
||||
|
||||
@@ -1,30 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "scenes/bad_kb_scene.h"
|
||||
#include "helpers/ducky_script.h"
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <gui/gui.h>
|
||||
#include <assets_icons.h>
|
||||
#include <gui/scene_manager.h>
|
||||
#include <dialogs/dialogs.h>
|
||||
#include <notification/notification_messages.h>
|
||||
typedef struct BadKbApp BadKbApp;
|
||||
|
||||
#define BAD_KB_APP_SCRIPT_EXTENSION ".txt"
|
||||
#define BAD_KB_APP_LAYOUT_EXTENSION ".kl"
|
||||
|
||||
typedef enum BadKbCustomEvent {
|
||||
BadKbAppCustomEventTextInputDone,
|
||||
BadKbAppCustomEventByteInputDone,
|
||||
BadKbCustomEventErrorBack
|
||||
} BadKbCustomEvent;
|
||||
|
||||
typedef enum {
|
||||
BadKbAppViewWidget,
|
||||
BadKbAppViewWork,
|
||||
BadKbAppViewVarItemList,
|
||||
BadKbAppViewByteInput,
|
||||
BadKbAppViewTextInput,
|
||||
BadKbAppViewLoading,
|
||||
} BadKbAppView;
|
||||
|
||||
void bad_kb_app_show_loading_popup(BadKbApp* app, bool show);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
111
applications/main/bad_kb/bad_kb_app_i.h
Normal file
111
applications/main/bad_kb/bad_kb_app_i.h
Normal file
@@ -0,0 +1,111 @@
|
||||
#pragma once
|
||||
|
||||
#include "bad_kb_app.h"
|
||||
#include "scenes/bad_kb_scene.h"
|
||||
#include "helpers/ducky_script.h"
|
||||
#include "helpers/ble_hid.h"
|
||||
#include "bad_kb_paths.h"
|
||||
|
||||
#include <gui/gui.h>
|
||||
#include <assets_icons.h>
|
||||
#include <gui/view_dispatcher.h>
|
||||
#include <gui/scene_manager.h>
|
||||
#include <gui/modules/submenu.h>
|
||||
#include <dialogs/dialogs.h>
|
||||
#include <notification/notification_messages.h>
|
||||
#include <gui/modules/variable_item_list.h>
|
||||
#include <gui/modules/text_input.h>
|
||||
#include <gui/modules/byte_input.h>
|
||||
#include <gui/modules/loading.h>
|
||||
#include <gui/modules/widget.h>
|
||||
#include "views/bad_kb_view.h"
|
||||
#include <furi_hal_usb.h>
|
||||
|
||||
#define BAD_KB_APP_SCRIPT_EXTENSION ".txt"
|
||||
#define BAD_KB_APP_LAYOUT_EXTENSION ".kl"
|
||||
|
||||
extern uint8_t BAD_KB_BOUND_MAC[GAP_MAC_ADDR_SIZE]; // For remember mode
|
||||
|
||||
typedef enum BadKbCustomEvent {
|
||||
BadKbAppCustomEventTextInputDone,
|
||||
BadKbAppCustomEventByteInputDone,
|
||||
BadKbCustomEventErrorBack
|
||||
} BadKbCustomEvent;
|
||||
|
||||
typedef enum {
|
||||
BadKbAppErrorNoFiles,
|
||||
} BadKbAppError;
|
||||
|
||||
typedef struct {
|
||||
BleProfileHidParams ble;
|
||||
FuriHalUsbHidConfig usb;
|
||||
} BadKbConfig;
|
||||
|
||||
typedef enum {
|
||||
BadKbConnModeNone,
|
||||
BadKbConnModeUsb,
|
||||
BadKbConnModeBt,
|
||||
} BadKbConnMode;
|
||||
|
||||
struct BadKbApp {
|
||||
Gui* gui;
|
||||
ViewDispatcher* view_dispatcher;
|
||||
SceneManager* scene_manager;
|
||||
NotificationApp* notifications;
|
||||
DialogsApp* dialogs;
|
||||
Widget* widget;
|
||||
VariableItemList* var_item_list;
|
||||
TextInput* text_input;
|
||||
ByteInput* byte_input;
|
||||
Loading* loading;
|
||||
|
||||
char bt_name_buf[FURI_HAL_BT_ADV_NAME_LENGTH];
|
||||
uint8_t bt_mac_buf[GAP_MAC_ADDR_SIZE];
|
||||
char usb_name_buf[HID_MANUF_PRODUCT_NAME_LEN];
|
||||
uint16_t usb_vidpid_buf[2];
|
||||
|
||||
BadKbAppError error;
|
||||
FuriString* file_path;
|
||||
FuriString* keyboard_layout;
|
||||
BadKb* bad_kb_view;
|
||||
BadKbScript* bad_kb_script;
|
||||
|
||||
Bt* bt;
|
||||
bool is_bt;
|
||||
bool bt_remember;
|
||||
BadKbConfig config; // User options
|
||||
BadKbConfig id_config; // ID and BT_ID values
|
||||
|
||||
bool set_bt_id;
|
||||
bool set_usb_id;
|
||||
bool has_bt_id;
|
||||
bool has_usb_id;
|
||||
|
||||
FuriHalBleProfileBase* ble_hid;
|
||||
FuriHalUsbInterface* prev_usb_mode;
|
||||
|
||||
BleProfileHidParams cur_ble_cfg;
|
||||
FuriHalUsbHidConfig* cur_usb_cfg;
|
||||
|
||||
BadKbConnMode conn_mode;
|
||||
FuriThread* conn_init_thread;
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
BadKbAppViewWidget,
|
||||
BadKbAppViewWork,
|
||||
BadKbAppViewVarItemList,
|
||||
BadKbAppViewByteInput,
|
||||
BadKbAppViewTextInput,
|
||||
BadKbAppViewLoading,
|
||||
} BadKbAppView;
|
||||
|
||||
void bad_kb_app_show_loading_popup(BadKbApp* app, bool show);
|
||||
|
||||
int32_t bad_kb_conn_apply(BadKbApp* app);
|
||||
|
||||
void bad_kb_conn_reset(BadKbApp* app);
|
||||
|
||||
void bad_kb_config_refresh(BadKbApp* app);
|
||||
|
||||
void bad_kb_config_adjust(BadKbConfig* cfg);
|
||||
@@ -1,21 +1,16 @@
|
||||
#include "../bad_kb_app_i.h"
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include <gui/gui.h>
|
||||
#include <input/input.h>
|
||||
#include <lib/toolbox/args.h>
|
||||
#include "ble_hid.h"
|
||||
#include <furi_hal_usb_hid.h>
|
||||
#include <bt/bt_service/bt.h>
|
||||
#include "ble_hid.h"
|
||||
#include <storage/storage.h>
|
||||
#include "ducky_script.h"
|
||||
#include "ducky_script_i.h"
|
||||
#include <dolphin/dolphin.h>
|
||||
#include <toolbox/hex.h>
|
||||
#include <xtreme/xtreme.h>
|
||||
#include "../scenes/bad_kb_scene.h"
|
||||
|
||||
// Adjusts to serial MAC +2 in app init
|
||||
uint8_t BAD_KB_BOUND_MAC[GAP_MAC_ADDR_SIZE] = {0};
|
||||
|
||||
#define TAG "BadKb"
|
||||
#define WORKER_TAG TAG "Worker"
|
||||
@@ -61,14 +56,6 @@ static inline void update_bt_timeout(Bt* bt) {
|
||||
}
|
||||
}
|
||||
|
||||
typedef enum {
|
||||
WorkerEvtStartStop = (1 << 0),
|
||||
WorkerEvtPauseResume = (1 << 1),
|
||||
WorkerEvtEnd = (1 << 2),
|
||||
WorkerEvtConnect = (1 << 3),
|
||||
WorkerEvtDisconnect = (1 << 4),
|
||||
} WorkerEvtFlags;
|
||||
|
||||
static const char ducky_cmd_id[] = {"ID"};
|
||||
static const char ducky_cmd_bt_id[] = {"BT_ID"};
|
||||
|
||||
@@ -476,7 +463,7 @@ static int32_t ducky_script_execute_next(BadKbScript* bad_kb, File* script_file)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void bad_kb_bt_hid_state_callback(BtStatus status, void* context) {
|
||||
void bad_kb_bt_hid_state_callback(BtStatus status, void* context) {
|
||||
furi_assert(context);
|
||||
BadKbScript* bad_kb = context;
|
||||
bool state = (status == BtStatusConnected);
|
||||
@@ -492,7 +479,7 @@ static void bad_kb_bt_hid_state_callback(BtStatus status, void* context) {
|
||||
}
|
||||
}
|
||||
|
||||
static void bad_kb_usb_hid_state_callback(bool state, void* context) {
|
||||
void bad_kb_usb_hid_state_callback(bool state, void* context) {
|
||||
furi_assert(context);
|
||||
BadKbScript* bad_kb = context;
|
||||
|
||||
@@ -516,165 +503,6 @@ static uint32_t bad_kb_flags_get(uint32_t flags_mask, uint32_t timeout) {
|
||||
return flags;
|
||||
}
|
||||
|
||||
int32_t bad_kb_conn_apply(BadKbApp* app) {
|
||||
if(app->is_bt) {
|
||||
bt_timeout = bt_hid_delays[LevelRssi39_0];
|
||||
bt_disconnect(app->bt);
|
||||
furi_delay_ms(200);
|
||||
bt_keys_storage_set_storage_path(app->bt, BAD_KB_KEYS_PATH);
|
||||
|
||||
// Setup new config
|
||||
BadKbConfig* cfg = app->set_bt_id ? &app->id_config : &app->config;
|
||||
memcpy(&app->cur_ble_cfg, &cfg->ble, sizeof(cfg->ble));
|
||||
app->cur_ble_cfg.bonding = app->bt_remember;
|
||||
if(app->bt_remember) {
|
||||
app->cur_ble_cfg.pairing = GapPairingPinCodeVerifyYesNo;
|
||||
} else {
|
||||
app->cur_ble_cfg.pairing = GapPairingNone;
|
||||
memcpy(app->cur_ble_cfg.mac, BAD_KB_BOUND_MAC, sizeof(BAD_KB_BOUND_MAC));
|
||||
}
|
||||
|
||||
// Set profile
|
||||
app->ble_hid = bt_profile_start(app->bt, ble_profile_hid, &app->cur_ble_cfg);
|
||||
furi_check(app->ble_hid);
|
||||
|
||||
// Advertise even if BT is off in settings
|
||||
furi_hal_bt_start_advertising();
|
||||
|
||||
app->conn_mode = BadKbConnModeBt;
|
||||
|
||||
} else {
|
||||
// Unlock RPC connections
|
||||
furi_hal_usb_unlock();
|
||||
|
||||
// Context will apply with set_config only if pointer address is different, so we use a copy
|
||||
FuriHalUsbHidConfig* cur_usb_cfg = malloc(sizeof(FuriHalUsbHidConfig));
|
||||
|
||||
// Setup new config
|
||||
BadKbConfig* cfg = app->set_usb_id ? &app->id_config : &app->config;
|
||||
memcpy(cur_usb_cfg, &cfg->usb, sizeof(cfg->usb));
|
||||
|
||||
// Set profile
|
||||
furi_check(furi_hal_usb_set_config(&usb_hid, cur_usb_cfg));
|
||||
if(app->cur_usb_cfg) free(app->cur_usb_cfg);
|
||||
app->cur_usb_cfg = cur_usb_cfg;
|
||||
|
||||
app->conn_mode = BadKbConnModeUsb;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void bad_kb_conn_reset(BadKbApp* app) {
|
||||
if(app->conn_mode == BadKbConnModeBt) {
|
||||
bt_disconnect(app->bt);
|
||||
furi_delay_ms(200);
|
||||
bt_keys_storage_set_default_path(app->bt);
|
||||
furi_check(bt_profile_restore_default(app->bt));
|
||||
} else if(app->conn_mode == BadKbConnModeUsb) {
|
||||
// TODO: maybe also restore USB context?
|
||||
furi_check(furi_hal_usb_set_config(app->prev_usb_mode, NULL));
|
||||
}
|
||||
|
||||
app->conn_mode = BadKbConnModeNone;
|
||||
}
|
||||
|
||||
void bad_kb_config_adjust(BadKbConfig* cfg) {
|
||||
// Avoid empty name
|
||||
if(cfg->ble.name[0] == '\0') {
|
||||
snprintf(
|
||||
cfg->ble.name, sizeof(cfg->ble.name), "Control %s", furi_hal_version_get_name_ptr());
|
||||
}
|
||||
|
||||
const uint8_t* normal_mac = furi_hal_version_get_ble_mac();
|
||||
uint8_t empty_mac[sizeof(cfg->ble.mac)] = {0};
|
||||
uint8_t default_mac[sizeof(cfg->ble.mac)] = {0x6c, 0x7a, 0xd8, 0xac, 0x57, 0x72}; //furi_hal_bt
|
||||
if(memcmp(cfg->ble.mac, empty_mac, sizeof(cfg->ble.mac)) == 0 ||
|
||||
memcmp(cfg->ble.mac, normal_mac, sizeof(cfg->ble.mac)) == 0 ||
|
||||
memcmp(cfg->ble.mac, default_mac, sizeof(cfg->ble.mac)) == 0) {
|
||||
memcpy(cfg->ble.mac, normal_mac, sizeof(cfg->ble.mac));
|
||||
cfg->ble.mac[2]++;
|
||||
}
|
||||
|
||||
// Use defaults if vid or pid are unset
|
||||
if(cfg->usb.vid == 0) cfg->usb.vid = HID_VID_DEFAULT;
|
||||
if(cfg->usb.pid == 0) cfg->usb.pid = HID_PID_DEFAULT;
|
||||
}
|
||||
|
||||
void bad_kb_config_refresh(BadKbApp* app) {
|
||||
bt_set_status_changed_callback(app->bt, NULL, NULL);
|
||||
furi_hal_hid_set_state_callback(NULL, NULL);
|
||||
if(app->bad_kb_script) {
|
||||
furi_thread_flags_set(furi_thread_get_id(app->bad_kb_script->thread), WorkerEvtDisconnect);
|
||||
}
|
||||
if(app->conn_init_thread) {
|
||||
furi_thread_join(app->conn_init_thread);
|
||||
}
|
||||
|
||||
bool apply = false;
|
||||
if(app->is_bt) {
|
||||
BadKbConfig* cfg = app->set_bt_id ? &app->id_config : &app->config;
|
||||
bad_kb_config_adjust(cfg);
|
||||
|
||||
if(app->conn_mode != BadKbConnModeBt) {
|
||||
apply = true;
|
||||
bad_kb_conn_reset(app);
|
||||
} else {
|
||||
BleProfileHidParams* cur = &app->cur_ble_cfg;
|
||||
apply = apply || cfg->ble.bonding != app->bt_remember;
|
||||
apply = apply || strncmp(cfg->ble.name, cur->name, sizeof(cfg->ble.name));
|
||||
apply = apply || memcmp(cfg->ble.mac, cur->mac, sizeof(cfg->ble.mac));
|
||||
}
|
||||
} else {
|
||||
BadKbConfig* cfg = app->set_usb_id ? &app->id_config : &app->config;
|
||||
bad_kb_config_adjust(cfg);
|
||||
|
||||
if(app->conn_mode != BadKbConnModeUsb) {
|
||||
apply = true;
|
||||
bad_kb_conn_reset(app);
|
||||
} else {
|
||||
FuriHalUsbHidConfig* cur = app->cur_usb_cfg;
|
||||
apply = apply || cfg->usb.vid != cur->vid;
|
||||
apply = apply || cfg->usb.pid != cur->pid;
|
||||
apply = apply || strncmp(cfg->usb.manuf, cur->manuf, sizeof(cur->manuf));
|
||||
apply = apply || strncmp(cfg->usb.product, cur->product, sizeof(cur->product));
|
||||
}
|
||||
}
|
||||
|
||||
if(apply) {
|
||||
bad_kb_conn_apply(app);
|
||||
}
|
||||
|
||||
if(app->bad_kb_script) {
|
||||
BadKbScript* script = app->bad_kb_script;
|
||||
script->st.is_bt = app->is_bt;
|
||||
script->bt = app->is_bt ? app->bt : NULL;
|
||||
bool connected;
|
||||
if(app->is_bt) {
|
||||
bt_set_status_changed_callback(app->bt, bad_kb_bt_hid_state_callback, script);
|
||||
connected = furi_hal_bt_is_connected();
|
||||
} else {
|
||||
furi_hal_hid_set_state_callback(bad_kb_usb_hid_state_callback, script);
|
||||
connected = furi_hal_hid_is_connected();
|
||||
}
|
||||
if(connected) {
|
||||
furi_thread_flags_set(furi_thread_get_id(script->thread), WorkerEvtConnect);
|
||||
}
|
||||
}
|
||||
|
||||
// Reload config page
|
||||
scene_manager_next_scene(app->scene_manager, BadKbSceneConfig);
|
||||
scene_manager_previous_scene(app->scene_manager);
|
||||
|
||||
// Update settings
|
||||
if(xtreme_settings.bad_bt != app->is_bt ||
|
||||
xtreme_settings.bad_bt_remember != app->bt_remember) {
|
||||
xtreme_settings.bad_bt = app->is_bt;
|
||||
xtreme_settings.bad_bt_remember = app->bt_remember;
|
||||
xtreme_settings_save();
|
||||
}
|
||||
}
|
||||
|
||||
static int32_t bad_kb_worker(void* context) {
|
||||
BadKbScript* bad_kb = context;
|
||||
|
||||
|
||||
@@ -6,19 +6,9 @@ extern "C" {
|
||||
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include <bt/bt_service/bt_i.h>
|
||||
#include <bt/bt_service/bt.h>
|
||||
|
||||
#include <gui/view_dispatcher.h>
|
||||
#include <gui/modules/widget.h>
|
||||
#include <gui/modules/variable_item_list.h>
|
||||
#include <gui/modules/text_input.h>
|
||||
#include <gui/modules/byte_input.h>
|
||||
#include <gui/modules/loading.h>
|
||||
#include "../views/bad_kb_view.h"
|
||||
#include "../bad_kb_paths.h"
|
||||
#include "ble_hid.h"
|
||||
|
||||
#define FILE_BUFFER_LEN 16
|
||||
#include "../bad_kb_app.h"
|
||||
|
||||
typedef enum {
|
||||
LevelRssi122_100,
|
||||
@@ -34,6 +24,14 @@ extern const uint8_t bt_hid_delays[LevelRssiNum];
|
||||
|
||||
extern uint8_t bt_timeout;
|
||||
|
||||
typedef enum {
|
||||
WorkerEvtStartStop = (1 << 0),
|
||||
WorkerEvtPauseResume = (1 << 1),
|
||||
WorkerEvtEnd = (1 << 2),
|
||||
WorkerEvtConnect = (1 << 3),
|
||||
WorkerEvtDisconnect = (1 << 4),
|
||||
} WorkerEvtFlags;
|
||||
|
||||
typedef enum {
|
||||
BadKbStateInit,
|
||||
BadKbStateNotConnected,
|
||||
@@ -49,7 +47,7 @@ typedef enum {
|
||||
BadKbStateFileError,
|
||||
} BadKbWorkerState;
|
||||
|
||||
struct BadKbState {
|
||||
typedef struct {
|
||||
BadKbWorkerState state;
|
||||
bool is_bt;
|
||||
uint32_t pin;
|
||||
@@ -59,36 +57,9 @@ struct BadKbState {
|
||||
size_t error_line;
|
||||
char error[64];
|
||||
uint32_t elapsed;
|
||||
};
|
||||
} BadKbState;
|
||||
|
||||
typedef struct BadKbApp BadKbApp;
|
||||
|
||||
typedef struct {
|
||||
FuriThread* thread;
|
||||
BadKbState st;
|
||||
|
||||
FuriString* file_path;
|
||||
FuriString* keyboard_layout;
|
||||
uint8_t file_buf[FILE_BUFFER_LEN + 1];
|
||||
uint8_t buf_start;
|
||||
uint8_t buf_len;
|
||||
bool file_end;
|
||||
|
||||
uint32_t defdelay;
|
||||
uint32_t stringdelay;
|
||||
uint16_t layout[128];
|
||||
|
||||
FuriString* line;
|
||||
FuriString* line_prev;
|
||||
uint32_t repeat_cnt;
|
||||
uint8_t key_hold_nb;
|
||||
|
||||
FuriString* string_print;
|
||||
size_t string_print_pos;
|
||||
|
||||
Bt* bt;
|
||||
BadKbApp* app;
|
||||
} BadKbScript;
|
||||
typedef struct BadKbScript BadKbScript;
|
||||
|
||||
BadKbScript* bad_kb_script_open(FuriString* file_path, Bt* bt, BadKbApp* app);
|
||||
|
||||
@@ -106,73 +77,9 @@ void bad_kb_script_pause_resume(BadKbScript* bad_kb);
|
||||
|
||||
BadKbState* bad_kb_script_get_state(BadKbScript* bad_kb);
|
||||
|
||||
extern uint8_t BAD_KB_BOUND_MAC[GAP_MAC_ADDR_SIZE]; // For remember mode
|
||||
void bad_kb_bt_hid_state_callback(BtStatus status, void* context);
|
||||
|
||||
typedef enum {
|
||||
BadKbAppErrorNoFiles,
|
||||
} BadKbAppError;
|
||||
|
||||
typedef struct {
|
||||
BleProfileHidParams ble;
|
||||
FuriHalUsbHidConfig usb;
|
||||
} BadKbConfig;
|
||||
|
||||
typedef enum {
|
||||
BadKbConnModeNone,
|
||||
BadKbConnModeUsb,
|
||||
BadKbConnModeBt,
|
||||
} BadKbConnMode;
|
||||
|
||||
struct BadKbApp {
|
||||
Gui* gui;
|
||||
ViewDispatcher* view_dispatcher;
|
||||
SceneManager* scene_manager;
|
||||
NotificationApp* notifications;
|
||||
DialogsApp* dialogs;
|
||||
Widget* widget;
|
||||
VariableItemList* var_item_list;
|
||||
TextInput* text_input;
|
||||
ByteInput* byte_input;
|
||||
Loading* loading;
|
||||
char usb_name_buf[HID_MANUF_PRODUCT_NAME_LEN];
|
||||
uint16_t usb_vidpid_buf[2];
|
||||
char bt_name_buf[FURI_HAL_BT_ADV_NAME_LENGTH];
|
||||
uint8_t bt_mac_buf[GAP_MAC_ADDR_SIZE];
|
||||
|
||||
BadKbAppError error;
|
||||
FuriString* file_path;
|
||||
FuriString* keyboard_layout;
|
||||
BadKb* bad_kb_view;
|
||||
BadKbScript* bad_kb_script;
|
||||
|
||||
Bt* bt;
|
||||
bool is_bt;
|
||||
bool bt_remember;
|
||||
BadKbConfig config; // User options
|
||||
BadKbConfig id_config; // ID and BT_ID values
|
||||
|
||||
bool set_usb_id;
|
||||
bool set_bt_id;
|
||||
bool has_usb_id;
|
||||
bool has_bt_id;
|
||||
|
||||
FuriHalBleProfileBase* ble_hid;
|
||||
FuriHalUsbInterface* prev_usb_mode;
|
||||
|
||||
BleProfileHidParams cur_ble_cfg;
|
||||
FuriHalUsbHidConfig* cur_usb_cfg;
|
||||
|
||||
BadKbConnMode conn_mode;
|
||||
FuriThread* conn_init_thread;
|
||||
};
|
||||
|
||||
int32_t bad_kb_conn_apply(BadKbApp* app);
|
||||
|
||||
void bad_kb_conn_reset(BadKbApp* app);
|
||||
|
||||
void bad_kb_config_refresh(BadKbApp* app);
|
||||
|
||||
void bad_kb_config_adjust(BadKbConfig* cfg);
|
||||
void bad_kb_usb_hid_state_callback(bool state, void* context);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "../bad_kb_app_i.h"
|
||||
#include <furi_hal.h>
|
||||
#include "ble_hid.h"
|
||||
#include <furi_hal_usb_hid.h>
|
||||
#include "ble_hid.h"
|
||||
#include "ducky_script.h"
|
||||
#include "ducky_script_i.h"
|
||||
|
||||
|
||||
@@ -15,6 +15,35 @@ extern "C" {
|
||||
#define SCRIPT_STATE_STRING_START (-5)
|
||||
#define SCRIPT_STATE_WAIT_FOR_BTN (-6)
|
||||
|
||||
#define FILE_BUFFER_LEN 16
|
||||
|
||||
struct BadKbScript {
|
||||
FuriThread* thread;
|
||||
BadKbState st;
|
||||
|
||||
FuriString* file_path;
|
||||
FuriString* keyboard_layout;
|
||||
uint8_t file_buf[FILE_BUFFER_LEN + 1];
|
||||
uint8_t buf_start;
|
||||
uint8_t buf_len;
|
||||
bool file_end;
|
||||
|
||||
uint32_t defdelay;
|
||||
uint32_t stringdelay;
|
||||
uint16_t layout[128];
|
||||
|
||||
FuriString* line;
|
||||
FuriString* line_prev;
|
||||
uint32_t repeat_cnt;
|
||||
uint8_t key_hold_nb;
|
||||
|
||||
FuriString* string_print;
|
||||
size_t string_print_pos;
|
||||
|
||||
Bt* bt;
|
||||
BadKbApp* app;
|
||||
};
|
||||
|
||||
uint16_t ducky_get_keycode(BadKbScript* bad_kb, const char* param, bool accept_chars);
|
||||
|
||||
uint32_t ducky_get_command_len(const char* line);
|
||||
|
||||
0
applications/main/bad_kb/resources/badkb/assets/layouts/sk-SK.kl
Normal file → Executable file
0
applications/main/bad_kb/resources/badkb/assets/layouts/sk-SK.kl
Normal file → Executable file
@@ -1,5 +1,4 @@
|
||||
#include "../bad_kb_app.h"
|
||||
#include "../helpers/ducky_script.h"
|
||||
#include "../bad_kb_app_i.h"
|
||||
#include "furi_hal_power.h"
|
||||
#include "furi_hal_usb.h"
|
||||
#include <xtreme/xtreme.h>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "../bad_kb_app.h"
|
||||
#include "../bad_kb_app_i.h"
|
||||
|
||||
void bad_kb_scene_config_bt_mac_byte_input_callback(void* context) {
|
||||
BadKbApp* bad_kb = context;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "../bad_kb_app.h"
|
||||
#include "../bad_kb_app_i.h"
|
||||
|
||||
static void bad_kb_scene_config_bt_name_text_input_callback(void* context) {
|
||||
BadKbApp* bad_kb = context;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "../bad_kb_app.h"
|
||||
#include "../bad_kb_app_i.h"
|
||||
#include "furi_hal_power.h"
|
||||
#include "furi_hal_usb.h"
|
||||
#include <storage/storage.h>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "../bad_kb_app.h"
|
||||
#include "../bad_kb_app_i.h"
|
||||
|
||||
static void bad_kb_scene_config_usb_name_text_input_callback(void* context) {
|
||||
BadKbApp* bad_kb = context;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "../bad_kb_app.h"
|
||||
#include "../bad_kb_app_i.h"
|
||||
|
||||
void bad_kb_scene_config_usb_vidpid_byte_input_callback(void* context) {
|
||||
BadKbApp* bad_kb = context;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "../bad_kb_app.h"
|
||||
#include "../bad_kb_app_i.h"
|
||||
|
||||
static void
|
||||
bad_kb_scene_error_event_callback(GuiButtonType result, InputType type, void* context) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "../bad_kb_app.h"
|
||||
#include "../bad_kb_app_i.h"
|
||||
#include <furi_hal_power.h>
|
||||
#include <furi_hal_usb.h>
|
||||
#include <storage/storage.h>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "../helpers/ducky_script.h"
|
||||
#include "../bad_kb_app.h"
|
||||
#include "../bad_kb_app_i.h"
|
||||
#include "../views/bad_kb_view.h"
|
||||
#include <furi_hal.h>
|
||||
#include "toolbox/path.h"
|
||||
|
||||
@@ -1,13 +1,20 @@
|
||||
#include "../bad_kb_app_i.h"
|
||||
#include "bad_kb_view.h"
|
||||
#include "../helpers/ducky_script.h"
|
||||
#include "../bad_kb_app.h"
|
||||
#include <toolbox/path.h>
|
||||
#include <gui/elements.h>
|
||||
#include <assets_icons.h>
|
||||
#include <xtreme/xtreme.h>
|
||||
#include <bt/bt_service/bt_i.h>
|
||||
|
||||
#define MAX_NAME_LEN 64
|
||||
|
||||
struct BadKb {
|
||||
View* view;
|
||||
BadKbButtonCallback callback;
|
||||
void* context;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
char file_name[MAX_NAME_LEN];
|
||||
char layout[MAX_NAME_LEN];
|
||||
@@ -44,6 +51,8 @@ static void bad_kb_draw_callback(Canvas* canvas, void* _model) {
|
||||
canvas_draw_str(
|
||||
canvas, 2, 8 + canvas_current_font_height(canvas), furi_string_get_cstr(disp_str));
|
||||
|
||||
furi_string_reset(disp_str);
|
||||
|
||||
canvas_draw_icon(canvas, 22, 24, &I_UsbTree_48x22);
|
||||
|
||||
if((state == BadKbStateIdle) || (state == BadKbStateDone) ||
|
||||
|
||||
@@ -1,17 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <gui/view.h>
|
||||
#include "../helpers/ducky_script.h"
|
||||
|
||||
typedef struct BadKb BadKb;
|
||||
typedef void (*BadKbButtonCallback)(InputKey key, void* context);
|
||||
|
||||
typedef struct {
|
||||
View* view;
|
||||
BadKbButtonCallback callback;
|
||||
void* context;
|
||||
} BadKb;
|
||||
|
||||
typedef struct BadKbState BadKbState;
|
||||
|
||||
BadKb* bad_kb_alloc();
|
||||
|
||||
void bad_kb_free(BadKb* bad_kb);
|
||||
|
||||
Reference in New Issue
Block a user