mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-04-24 03:29:57 -07:00
BadKB BLE refactor
This commit is contained in:
@@ -30,12 +30,6 @@ static void bad_kb_app_tick_event_callback(void* context) {
|
||||
static void bad_kb_load_settings(BadKbApp* app) {
|
||||
furi_string_reset(app->keyboard_layout);
|
||||
BadKbConfig* cfg = &app->config;
|
||||
strcpy(cfg->bt_name, "");
|
||||
memcpy(cfg->bt_mac, BAD_KB_EMPTY_MAC, BAD_KB_MAC_LEN);
|
||||
strcpy(cfg->usb_cfg.manuf, "");
|
||||
strcpy(cfg->usb_cfg.product, "");
|
||||
cfg->usb_cfg.vid = 0;
|
||||
cfg->usb_cfg.pid = 0;
|
||||
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
FlipperFormat* file = flipper_format_file_alloc(storage);
|
||||
@@ -45,29 +39,30 @@ static void bad_kb_load_settings(BadKbApp* app) {
|
||||
furi_string_reset(app->keyboard_layout);
|
||||
}
|
||||
if(flipper_format_read_string(file, "Bt_Name", tmp_str) && !furi_string_empty(tmp_str)) {
|
||||
strlcpy(cfg->bt_name, furi_string_get_cstr(tmp_str), BAD_KB_NAME_LEN);
|
||||
strlcpy(cfg->ble.name, furi_string_get_cstr(tmp_str), sizeof(cfg->ble.name));
|
||||
} else {
|
||||
strcpy(cfg->bt_name, "");
|
||||
strcpy(cfg->ble.name, "");
|
||||
}
|
||||
if(!flipper_format_read_hex(file, "Bt_Mac", (uint8_t*)&cfg->bt_mac, BAD_KB_MAC_LEN)) {
|
||||
memcpy(cfg->bt_mac, BAD_KB_EMPTY_MAC, BAD_KB_MAC_LEN);
|
||||
if(!flipper_format_read_hex(
|
||||
file, "Bt_Mac", (uint8_t*)&cfg->ble.mac, sizeof(cfg->ble.mac))) {
|
||||
memset(cfg->ble.mac, 0, sizeof(cfg->ble.mac));
|
||||
}
|
||||
if(flipper_format_read_string(file, "Usb_Manuf", tmp_str) && !furi_string_empty(tmp_str)) {
|
||||
strlcpy(cfg->usb_cfg.manuf, furi_string_get_cstr(tmp_str), BAD_KB_USB_LEN);
|
||||
strlcpy(cfg->usb.manuf, furi_string_get_cstr(tmp_str), sizeof(cfg->usb.manuf));
|
||||
} else {
|
||||
strcpy(cfg->usb_cfg.manuf, "");
|
||||
strcpy(cfg->usb.manuf, "");
|
||||
}
|
||||
if(flipper_format_read_string(file, "Usb_Product", tmp_str) &&
|
||||
!furi_string_empty(tmp_str)) {
|
||||
strlcpy(cfg->usb_cfg.product, furi_string_get_cstr(tmp_str), BAD_KB_USB_LEN);
|
||||
strlcpy(cfg->usb.product, furi_string_get_cstr(tmp_str), sizeof(cfg->usb.product));
|
||||
} else {
|
||||
strcpy(cfg->usb_cfg.product, "");
|
||||
strcpy(cfg->usb.product, "");
|
||||
}
|
||||
if(!flipper_format_read_uint32(file, "Usb_Vid", &cfg->usb_cfg.vid, 1)) {
|
||||
cfg->usb_cfg.vid = 0;
|
||||
if(!flipper_format_read_uint32(file, "Usb_Vid", &cfg->usb.vid, 1)) {
|
||||
cfg->usb.vid = 0;
|
||||
}
|
||||
if(!flipper_format_read_uint32(file, "Usb_Pid", &cfg->usb_cfg.pid, 1)) {
|
||||
cfg->usb_cfg.pid = 0;
|
||||
if(!flipper_format_read_uint32(file, "Usb_Pid", &cfg->usb.pid, 1)) {
|
||||
cfg->usb.pid = 0;
|
||||
}
|
||||
furi_string_free(tmp_str);
|
||||
flipper_format_file_close(file);
|
||||
@@ -96,12 +91,12 @@ static void bad_kb_save_settings(BadKbApp* app) {
|
||||
FlipperFormat* file = flipper_format_file_alloc(storage);
|
||||
if(flipper_format_file_open_always(file, BAD_KB_SETTINGS_PATH)) {
|
||||
flipper_format_write_string(file, "Keyboard_Layout", app->keyboard_layout);
|
||||
flipper_format_write_string_cstr(file, "Bt_Name", cfg->bt_name);
|
||||
flipper_format_write_hex(file, "Bt_Mac", (uint8_t*)&cfg->bt_mac, BAD_KB_MAC_LEN);
|
||||
flipper_format_write_string_cstr(file, "Usb_Manuf", cfg->usb_cfg.manuf);
|
||||
flipper_format_write_string_cstr(file, "Usb_Product", cfg->usb_cfg.product);
|
||||
flipper_format_write_uint32(file, "Usb_Vid", &cfg->usb_cfg.vid, 1);
|
||||
flipper_format_write_uint32(file, "Usb_Pid", &cfg->usb_cfg.pid, 1);
|
||||
flipper_format_write_string_cstr(file, "Bt_Name", cfg->ble.name);
|
||||
flipper_format_write_hex(file, "Bt_Mac", (uint8_t*)&cfg->ble.mac, sizeof(cfg->ble.mac));
|
||||
flipper_format_write_string_cstr(file, "Usb_Manuf", cfg->usb.manuf);
|
||||
flipper_format_write_string_cstr(file, "Usb_Product", cfg->usb.product);
|
||||
flipper_format_write_uint32(file, "Usb_Vid", &cfg->usb.vid, 1);
|
||||
flipper_format_write_uint32(file, "Usb_Pid", &cfg->usb.pid, 1);
|
||||
flipper_format_file_close(file);
|
||||
}
|
||||
flipper_format_free(file);
|
||||
@@ -158,13 +153,9 @@ BadKbApp* bad_kb_app_alloc(char* arg) {
|
||||
|
||||
// Save prev config
|
||||
app->prev_usb_mode = furi_hal_usb_get_config();
|
||||
FuriHalBtProfile kbd = FuriHalBtProfileHidKeyboard;
|
||||
app->prev_bt_mode = furi_hal_bt_get_profile_pairing_method(kbd);
|
||||
memcpy(app->prev_bt_mac, furi_hal_bt_get_profile_mac_addr(kbd), BAD_KB_MAC_LEN);
|
||||
strlcpy(app->prev_bt_name, furi_hal_bt_get_profile_adv_name(kbd), BAD_KB_NAME_LEN);
|
||||
|
||||
// Adjust BT remember MAC to be serial MAC +2
|
||||
memcpy(BAD_KB_BOUND_MAC, furi_hal_version_get_ble_mac(), BAD_KB_MAC_LEN);
|
||||
memcpy(BAD_KB_BOUND_MAC, furi_hal_version_get_ble_mac(), sizeof(BAD_KB_BOUND_MAC));
|
||||
BAD_KB_BOUND_MAC[2] += 2;
|
||||
|
||||
// Custom Widget
|
||||
@@ -256,7 +247,7 @@ void bad_kb_app_free(BadKbApp* app) {
|
||||
app->conn_init_thread = NULL;
|
||||
}
|
||||
bad_kb_conn_reset(app);
|
||||
if(app->hid_cfg) free(app->hid_cfg);
|
||||
if(app->cur_usb_cfg) free(app->cur_usb_cfg);
|
||||
|
||||
// Close records
|
||||
furi_record_close(RECORD_GUI);
|
||||
|
||||
416
applications/main/bad_kb/helpers/ble_hid.c
Normal file
416
applications/main/bad_kb/helpers/ble_hid.c
Normal file
@@ -0,0 +1,416 @@
|
||||
#include "ble_hid.h"
|
||||
|
||||
#include <furi_hal_usb_hid.h>
|
||||
#include <services/dev_info_service.h>
|
||||
#include <services/battery_service.h>
|
||||
#include "ble_hid_svc.h"
|
||||
|
||||
#include <furi.h>
|
||||
#include <usb_hid.h>
|
||||
#include <ble/ble.h>
|
||||
|
||||
#define HID_INFO_BASE_USB_SPECIFICATION (0x0101)
|
||||
#define HID_INFO_COUNTRY_CODE (0x00)
|
||||
#define BLE_PROFILE_HID_INFO_FLAG_REMOTE_WAKE_MSK (0x01)
|
||||
#define BLE_PROFILE_HID_INFO_FLAG_NORMALLY_CONNECTABLE_MSK (0x02)
|
||||
|
||||
#define BLE_PROFILE_HID_KB_MAX_KEYS (6)
|
||||
#define BLE_PROFILE_CONSUMER_MAX_KEYS (1)
|
||||
|
||||
// Report ids cant be 0
|
||||
enum HidReportId {
|
||||
ReportIdKeyboard = 1,
|
||||
ReportIdMouse = 2,
|
||||
ReportIdConsumer = 3,
|
||||
};
|
||||
// Report numbers corresponded to the report id with an offset of 1
|
||||
enum HidInputNumber {
|
||||
ReportNumberKeyboard = 0,
|
||||
ReportNumberMouse = 1,
|
||||
ReportNumberConsumer = 2,
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
uint8_t mods;
|
||||
uint8_t reserved;
|
||||
uint8_t key[BLE_PROFILE_HID_KB_MAX_KEYS];
|
||||
} FURI_PACKED FuriHalBtHidKbReport;
|
||||
|
||||
typedef struct {
|
||||
uint8_t btn;
|
||||
int8_t x;
|
||||
int8_t y;
|
||||
int8_t wheel;
|
||||
} FURI_PACKED FuriHalBtHidMouseReport;
|
||||
|
||||
typedef struct {
|
||||
uint16_t key[BLE_PROFILE_CONSUMER_MAX_KEYS];
|
||||
} FURI_PACKED FuriHalBtHidConsumerReport;
|
||||
|
||||
// keyboard+mouse+consumer hid report
|
||||
static const uint8_t ble_profile_hid_report_map_data[] = {
|
||||
// Keyboard Report
|
||||
HID_USAGE_PAGE(HID_PAGE_DESKTOP),
|
||||
HID_USAGE(HID_DESKTOP_KEYBOARD),
|
||||
HID_COLLECTION(HID_APPLICATION_COLLECTION),
|
||||
HID_REPORT_ID(ReportIdKeyboard),
|
||||
HID_USAGE_PAGE(HID_DESKTOP_KEYPAD),
|
||||
HID_USAGE_MINIMUM(HID_KEYBOARD_L_CTRL),
|
||||
HID_USAGE_MAXIMUM(HID_KEYBOARD_R_GUI),
|
||||
HID_LOGICAL_MINIMUM(0),
|
||||
HID_LOGICAL_MAXIMUM(1),
|
||||
HID_REPORT_SIZE(1),
|
||||
HID_REPORT_COUNT(8),
|
||||
HID_INPUT(HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
|
||||
HID_REPORT_COUNT(1),
|
||||
HID_REPORT_SIZE(8),
|
||||
HID_INPUT(HID_IOF_CONSTANT | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
|
||||
HID_USAGE_PAGE(HID_PAGE_LED),
|
||||
HID_REPORT_COUNT(8),
|
||||
HID_REPORT_SIZE(1),
|
||||
HID_USAGE_MINIMUM(1),
|
||||
HID_USAGE_MAXIMUM(8),
|
||||
HID_OUTPUT(HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
|
||||
HID_REPORT_COUNT(BLE_PROFILE_HID_KB_MAX_KEYS),
|
||||
HID_REPORT_SIZE(8),
|
||||
HID_LOGICAL_MINIMUM(0),
|
||||
HID_LOGICAL_MAXIMUM(101),
|
||||
HID_USAGE_PAGE(HID_DESKTOP_KEYPAD),
|
||||
HID_USAGE_MINIMUM(0),
|
||||
HID_USAGE_MAXIMUM(101),
|
||||
HID_INPUT(HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE),
|
||||
HID_END_COLLECTION,
|
||||
// Mouse Report
|
||||
HID_USAGE_PAGE(HID_PAGE_DESKTOP),
|
||||
HID_USAGE(HID_DESKTOP_MOUSE),
|
||||
HID_COLLECTION(HID_APPLICATION_COLLECTION),
|
||||
HID_USAGE(HID_DESKTOP_POINTER),
|
||||
HID_COLLECTION(HID_PHYSICAL_COLLECTION),
|
||||
HID_REPORT_ID(ReportIdMouse),
|
||||
HID_USAGE_PAGE(HID_PAGE_BUTTON),
|
||||
HID_USAGE_MINIMUM(1),
|
||||
HID_USAGE_MAXIMUM(3),
|
||||
HID_LOGICAL_MINIMUM(0),
|
||||
HID_LOGICAL_MAXIMUM(1),
|
||||
HID_REPORT_COUNT(3),
|
||||
HID_REPORT_SIZE(1),
|
||||
HID_INPUT(HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
|
||||
HID_REPORT_SIZE(1),
|
||||
HID_REPORT_COUNT(5),
|
||||
HID_INPUT(HID_IOF_CONSTANT | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
|
||||
HID_USAGE_PAGE(HID_PAGE_DESKTOP),
|
||||
HID_USAGE(HID_DESKTOP_X),
|
||||
HID_USAGE(HID_DESKTOP_Y),
|
||||
HID_USAGE(HID_DESKTOP_WHEEL),
|
||||
HID_LOGICAL_MINIMUM(-127),
|
||||
HID_LOGICAL_MAXIMUM(127),
|
||||
HID_REPORT_SIZE(8),
|
||||
HID_REPORT_COUNT(3),
|
||||
HID_INPUT(HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE),
|
||||
HID_END_COLLECTION,
|
||||
HID_END_COLLECTION,
|
||||
// Consumer Report
|
||||
HID_USAGE_PAGE(HID_PAGE_CONSUMER),
|
||||
HID_USAGE(HID_CONSUMER_CONTROL),
|
||||
HID_COLLECTION(HID_APPLICATION_COLLECTION),
|
||||
HID_REPORT_ID(ReportIdConsumer),
|
||||
HID_LOGICAL_MINIMUM(0),
|
||||
HID_RI_LOGICAL_MAXIMUM(16, 0x3FF),
|
||||
HID_USAGE_MINIMUM(0),
|
||||
HID_RI_USAGE_MAXIMUM(16, 0x3FF),
|
||||
HID_REPORT_COUNT(BLE_PROFILE_CONSUMER_MAX_KEYS),
|
||||
HID_REPORT_SIZE(16),
|
||||
HID_INPUT(HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE),
|
||||
HID_END_COLLECTION,
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
FuriHalBleProfileBase base;
|
||||
|
||||
FuriHalBtHidKbReport* kb_report;
|
||||
FuriHalBtHidMouseReport* mouse_report;
|
||||
FuriHalBtHidConsumerReport* consumer_report;
|
||||
|
||||
BleServiceBattery* battery_svc;
|
||||
BleServiceDevInfo* dev_info_svc;
|
||||
BleServiceHid* hid_svc;
|
||||
} BleProfileHid;
|
||||
_Static_assert(offsetof(BleProfileHid, base) == 0, "Wrong layout");
|
||||
|
||||
static FuriHalBleProfileBase* ble_profile_hid_start(FuriHalBleProfileParams profile_params) {
|
||||
UNUSED(profile_params);
|
||||
|
||||
BleProfileHid* profile = malloc(sizeof(BleProfileHid));
|
||||
|
||||
profile->base.config = ble_profile_hid;
|
||||
|
||||
profile->battery_svc = ble_svc_battery_start(true);
|
||||
profile->dev_info_svc = ble_svc_dev_info_start();
|
||||
profile->hid_svc = ble_svc_hid_start();
|
||||
|
||||
// Configure HID Keyboard
|
||||
profile->kb_report = malloc(sizeof(FuriHalBtHidKbReport));
|
||||
profile->mouse_report = malloc(sizeof(FuriHalBtHidMouseReport));
|
||||
profile->consumer_report = malloc(sizeof(FuriHalBtHidConsumerReport));
|
||||
|
||||
// Configure Report Map characteristic
|
||||
ble_svc_hid_update_report_map(
|
||||
profile->hid_svc,
|
||||
ble_profile_hid_report_map_data,
|
||||
sizeof(ble_profile_hid_report_map_data));
|
||||
// Configure HID Information characteristic
|
||||
uint8_t hid_info_val[4] = {
|
||||
HID_INFO_BASE_USB_SPECIFICATION & 0x00ff,
|
||||
(HID_INFO_BASE_USB_SPECIFICATION & 0xff00) >> 8,
|
||||
HID_INFO_COUNTRY_CODE,
|
||||
BLE_PROFILE_HID_INFO_FLAG_REMOTE_WAKE_MSK |
|
||||
BLE_PROFILE_HID_INFO_FLAG_NORMALLY_CONNECTABLE_MSK,
|
||||
};
|
||||
ble_svc_hid_update_info(profile->hid_svc, hid_info_val);
|
||||
|
||||
return &profile->base;
|
||||
}
|
||||
|
||||
static void ble_profile_hid_stop(FuriHalBleProfileBase* profile) {
|
||||
furi_check(profile);
|
||||
furi_check(profile->config == ble_profile_hid);
|
||||
|
||||
BleProfileHid* hid_profile = (BleProfileHid*)profile;
|
||||
ble_svc_battery_stop(hid_profile->battery_svc);
|
||||
ble_svc_dev_info_stop(hid_profile->dev_info_svc);
|
||||
ble_svc_hid_stop(hid_profile->hid_svc);
|
||||
|
||||
free(hid_profile->kb_report);
|
||||
free(hid_profile->mouse_report);
|
||||
free(hid_profile->consumer_report);
|
||||
}
|
||||
|
||||
bool ble_profile_hid_kb_press(FuriHalBleProfileBase* profile, uint16_t button) {
|
||||
furi_check(profile);
|
||||
furi_check(profile->config == ble_profile_hid);
|
||||
|
||||
BleProfileHid* hid_profile = (BleProfileHid*)profile;
|
||||
FuriHalBtHidKbReport* kb_report = hid_profile->kb_report;
|
||||
for(uint8_t i = 0; i < BLE_PROFILE_HID_KB_MAX_KEYS; i++) {
|
||||
if(kb_report->key[i] == 0) {
|
||||
kb_report->key[i] = button & 0xFF;
|
||||
break;
|
||||
}
|
||||
}
|
||||
kb_report->mods |= (button >> 8);
|
||||
return ble_svc_hid_update_input_report(
|
||||
hid_profile->hid_svc,
|
||||
ReportNumberKeyboard,
|
||||
(uint8_t*)kb_report,
|
||||
sizeof(FuriHalBtHidKbReport));
|
||||
}
|
||||
|
||||
bool ble_profile_hid_kb_release(FuriHalBleProfileBase* profile, uint16_t button) {
|
||||
furi_check(profile);
|
||||
furi_check(profile->config == ble_profile_hid);
|
||||
|
||||
BleProfileHid* hid_profile = (BleProfileHid*)profile;
|
||||
|
||||
FuriHalBtHidKbReport* kb_report = hid_profile->kb_report;
|
||||
for(uint8_t i = 0; i < BLE_PROFILE_HID_KB_MAX_KEYS; i++) {
|
||||
if(kb_report->key[i] == (button & 0xFF)) {
|
||||
kb_report->key[i] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
kb_report->mods &= ~(button >> 8);
|
||||
return ble_svc_hid_update_input_report(
|
||||
hid_profile->hid_svc,
|
||||
ReportNumberKeyboard,
|
||||
(uint8_t*)kb_report,
|
||||
sizeof(FuriHalBtHidKbReport));
|
||||
}
|
||||
|
||||
bool ble_profile_hid_kb_release_all(FuriHalBleProfileBase* profile) {
|
||||
furi_check(profile);
|
||||
furi_check(profile->config == ble_profile_hid);
|
||||
|
||||
BleProfileHid* hid_profile = (BleProfileHid*)profile;
|
||||
FuriHalBtHidKbReport* kb_report = hid_profile->kb_report;
|
||||
for(uint8_t i = 0; i < BLE_PROFILE_HID_KB_MAX_KEYS; i++) {
|
||||
kb_report->key[i] = 0;
|
||||
}
|
||||
kb_report->mods = 0;
|
||||
return ble_svc_hid_update_input_report(
|
||||
hid_profile->hid_svc,
|
||||
ReportNumberKeyboard,
|
||||
(uint8_t*)kb_report,
|
||||
sizeof(FuriHalBtHidKbReport));
|
||||
}
|
||||
|
||||
bool ble_profile_hid_consumer_key_press(FuriHalBleProfileBase* profile, uint16_t button) {
|
||||
furi_check(profile);
|
||||
furi_check(profile->config == ble_profile_hid);
|
||||
|
||||
BleProfileHid* hid_profile = (BleProfileHid*)profile;
|
||||
FuriHalBtHidConsumerReport* consumer_report = hid_profile->consumer_report;
|
||||
for(uint8_t i = 0; i < BLE_PROFILE_CONSUMER_MAX_KEYS; i++) { //-V1008
|
||||
if(consumer_report->key[i] == 0) {
|
||||
consumer_report->key[i] = button;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ble_svc_hid_update_input_report(
|
||||
hid_profile->hid_svc,
|
||||
ReportNumberConsumer,
|
||||
(uint8_t*)consumer_report,
|
||||
sizeof(FuriHalBtHidConsumerReport));
|
||||
}
|
||||
|
||||
bool ble_profile_hid_consumer_key_release(FuriHalBleProfileBase* profile, uint16_t button) {
|
||||
furi_check(profile);
|
||||
furi_check(profile->config == ble_profile_hid);
|
||||
|
||||
BleProfileHid* hid_profile = (BleProfileHid*)profile;
|
||||
FuriHalBtHidConsumerReport* consumer_report = hid_profile->consumer_report;
|
||||
for(uint8_t i = 0; i < BLE_PROFILE_CONSUMER_MAX_KEYS; i++) { //-V1008
|
||||
if(consumer_report->key[i] == button) {
|
||||
consumer_report->key[i] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ble_svc_hid_update_input_report(
|
||||
hid_profile->hid_svc,
|
||||
ReportNumberConsumer,
|
||||
(uint8_t*)consumer_report,
|
||||
sizeof(FuriHalBtHidConsumerReport));
|
||||
}
|
||||
|
||||
bool ble_profile_hid_consumer_key_release_all(FuriHalBleProfileBase* profile) {
|
||||
furi_check(profile);
|
||||
furi_check(profile->config == ble_profile_hid);
|
||||
|
||||
BleProfileHid* hid_profile = (BleProfileHid*)profile;
|
||||
FuriHalBtHidConsumerReport* consumer_report = hid_profile->consumer_report;
|
||||
for(uint8_t i = 0; i < BLE_PROFILE_CONSUMER_MAX_KEYS; i++) { //-V1008
|
||||
consumer_report->key[i] = 0;
|
||||
}
|
||||
return ble_svc_hid_update_input_report(
|
||||
hid_profile->hid_svc,
|
||||
ReportNumberConsumer,
|
||||
(uint8_t*)consumer_report,
|
||||
sizeof(FuriHalBtHidConsumerReport));
|
||||
}
|
||||
|
||||
bool ble_profile_hid_mouse_move(FuriHalBleProfileBase* profile, int8_t dx, int8_t dy) {
|
||||
furi_check(profile);
|
||||
furi_check(profile->config == ble_profile_hid);
|
||||
|
||||
BleProfileHid* hid_profile = (BleProfileHid*)profile;
|
||||
FuriHalBtHidMouseReport* mouse_report = hid_profile->mouse_report;
|
||||
mouse_report->x = dx;
|
||||
mouse_report->y = dy;
|
||||
bool state = ble_svc_hid_update_input_report(
|
||||
hid_profile->hid_svc,
|
||||
ReportNumberMouse,
|
||||
(uint8_t*)mouse_report,
|
||||
sizeof(FuriHalBtHidMouseReport));
|
||||
mouse_report->x = 0;
|
||||
mouse_report->y = 0;
|
||||
return state;
|
||||
}
|
||||
|
||||
bool ble_profile_hid_mouse_press(FuriHalBleProfileBase* profile, uint8_t button) {
|
||||
furi_check(profile);
|
||||
furi_check(profile->config == ble_profile_hid);
|
||||
|
||||
BleProfileHid* hid_profile = (BleProfileHid*)profile;
|
||||
FuriHalBtHidMouseReport* mouse_report = hid_profile->mouse_report;
|
||||
mouse_report->btn |= button;
|
||||
return ble_svc_hid_update_input_report(
|
||||
hid_profile->hid_svc,
|
||||
ReportNumberMouse,
|
||||
(uint8_t*)mouse_report,
|
||||
sizeof(FuriHalBtHidMouseReport));
|
||||
}
|
||||
|
||||
bool ble_profile_hid_mouse_release(FuriHalBleProfileBase* profile, uint8_t button) {
|
||||
furi_check(profile);
|
||||
furi_check(profile->config == ble_profile_hid);
|
||||
|
||||
BleProfileHid* hid_profile = (BleProfileHid*)profile;
|
||||
FuriHalBtHidMouseReport* mouse_report = hid_profile->mouse_report;
|
||||
mouse_report->btn &= ~button;
|
||||
return ble_svc_hid_update_input_report(
|
||||
hid_profile->hid_svc,
|
||||
ReportNumberMouse,
|
||||
(uint8_t*)mouse_report,
|
||||
sizeof(FuriHalBtHidMouseReport));
|
||||
}
|
||||
|
||||
bool ble_profile_hid_mouse_release_all(FuriHalBleProfileBase* profile) {
|
||||
furi_check(profile);
|
||||
furi_check(profile->config == ble_profile_hid);
|
||||
|
||||
BleProfileHid* hid_profile = (BleProfileHid*)profile;
|
||||
FuriHalBtHidMouseReport* mouse_report = hid_profile->mouse_report;
|
||||
mouse_report->btn = 0;
|
||||
return ble_svc_hid_update_input_report(
|
||||
hid_profile->hid_svc,
|
||||
ReportNumberMouse,
|
||||
(uint8_t*)mouse_report,
|
||||
sizeof(FuriHalBtHidMouseReport));
|
||||
}
|
||||
|
||||
bool ble_profile_hid_mouse_scroll(FuriHalBleProfileBase* profile, int8_t delta) {
|
||||
furi_check(profile);
|
||||
furi_check(profile->config == ble_profile_hid);
|
||||
|
||||
BleProfileHid* hid_profile = (BleProfileHid*)profile;
|
||||
FuriHalBtHidMouseReport* mouse_report = hid_profile->mouse_report;
|
||||
mouse_report->wheel = delta;
|
||||
bool state = ble_svc_hid_update_input_report(
|
||||
hid_profile->hid_svc,
|
||||
ReportNumberMouse,
|
||||
(uint8_t*)mouse_report,
|
||||
sizeof(FuriHalBtHidMouseReport));
|
||||
mouse_report->wheel = 0;
|
||||
return state;
|
||||
}
|
||||
|
||||
static GapConfig template_config = {
|
||||
.adv_service_uuid = HUMAN_INTERFACE_DEVICE_SERVICE_UUID,
|
||||
.appearance_char = GAP_APPEARANCE_KEYBOARD,
|
||||
.bonding_mode = true,
|
||||
.pairing_method = GapPairingPinCodeVerifyYesNo,
|
||||
.conn_param =
|
||||
{
|
||||
.conn_int_min = 0x18, // 30 ms
|
||||
.conn_int_max = 0x24, // 45 ms
|
||||
.slave_latency = 0,
|
||||
.supervisor_timeout = 0,
|
||||
},
|
||||
};
|
||||
|
||||
static void ble_profile_hid_get_config(GapConfig* config, FuriHalBleProfileParams profile_params) {
|
||||
BleProfileHidParams* hid_profile_params = profile_params;
|
||||
|
||||
furi_check(config);
|
||||
memcpy(config, &template_config, sizeof(GapConfig));
|
||||
|
||||
// Set mac address
|
||||
memcpy(config->mac_address, hid_profile_params->mac, sizeof(config->mac_address));
|
||||
|
||||
// Set advertise name
|
||||
config->adv_name[0] = furi_hal_version_get_ble_local_device_name_ptr()[0];
|
||||
strlcpy(config->adv_name + 1, hid_profile_params->name, sizeof(config->adv_name) - 1);
|
||||
|
||||
// Set bonding mode
|
||||
config->bonding_mode = hid_profile_params->bonding;
|
||||
|
||||
// Set pairing method
|
||||
config->pairing_method = hid_profile_params->pairing;
|
||||
}
|
||||
|
||||
static const FuriHalBleProfileTemplate profile_callbacks = {
|
||||
.start = ble_profile_hid_start,
|
||||
.stop = ble_profile_hid_stop,
|
||||
.get_gap_config = ble_profile_hid_get_config,
|
||||
};
|
||||
|
||||
const FuriHalBleProfileTemplate* ble_profile_hid = &profile_callbacks;
|
||||
107
applications/main/bad_kb/helpers/ble_hid.h
Normal file
107
applications/main/bad_kb/helpers/ble_hid.h
Normal file
@@ -0,0 +1,107 @@
|
||||
#pragma once
|
||||
|
||||
#include <furi_ble/profile_interface.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Optional arguments to pass along with profile template as
|
||||
* FuriHalBleProfileParams for tuning profile behavior
|
||||
**/
|
||||
typedef struct {
|
||||
char name[FURI_HAL_BT_ADV_NAME_LENGTH]; /**< Full device name */
|
||||
uint8_t mac[GAP_MAC_ADDR_SIZE]; /**< Full device address */
|
||||
bool bonding; /**< Save paired devices */
|
||||
GapPairing pairing; /**< Pairing security method */
|
||||
} BleProfileHidParams;
|
||||
|
||||
/** Hid Keyboard Profile descriptor */
|
||||
extern const FuriHalBleProfileTemplate* ble_profile_hid;
|
||||
|
||||
/** Press keyboard button
|
||||
*
|
||||
* @param profile profile instance
|
||||
* @param button button code from HID specification
|
||||
*
|
||||
* @return true on success
|
||||
*/
|
||||
bool ble_profile_hid_kb_press(FuriHalBleProfileBase* profile, uint16_t button);
|
||||
|
||||
/** Release keyboard button
|
||||
*
|
||||
* @param profile profile instance
|
||||
* @param button button code from HID specification
|
||||
*
|
||||
* @return true on success
|
||||
*/
|
||||
bool ble_profile_hid_kb_release(FuriHalBleProfileBase* profile, uint16_t button);
|
||||
|
||||
/** Release all keyboard buttons
|
||||
*
|
||||
* @param profile profile instance
|
||||
* @return true on success
|
||||
*/
|
||||
bool ble_profile_hid_kb_release_all(FuriHalBleProfileBase* profile);
|
||||
|
||||
/** Set the following consumer key to pressed state and send HID report
|
||||
*
|
||||
* @param profile profile instance
|
||||
* @param button key code
|
||||
*/
|
||||
bool ble_profile_hid_consumer_key_press(FuriHalBleProfileBase* profile, uint16_t button);
|
||||
|
||||
/** Set the following consumer key to released state and send HID report
|
||||
*
|
||||
* @param profile profile instance
|
||||
* @param button key code
|
||||
*/
|
||||
bool ble_profile_hid_consumer_key_release(FuriHalBleProfileBase* profile, uint16_t button);
|
||||
|
||||
/** Set consumer key to released state and send HID report
|
||||
*
|
||||
* @param profile profile instance
|
||||
* @param button key code
|
||||
*/
|
||||
bool ble_profile_hid_consumer_key_release_all(FuriHalBleProfileBase* profile);
|
||||
|
||||
/** Set mouse movement and send HID report
|
||||
*
|
||||
* @param profile profile instance
|
||||
* @param dx x coordinate delta
|
||||
* @param dy y coordinate delta
|
||||
*/
|
||||
bool ble_profile_hid_mouse_move(FuriHalBleProfileBase* profile, int8_t dx, int8_t dy);
|
||||
|
||||
/** Set mouse button to pressed state and send HID report
|
||||
*
|
||||
* @param profile profile instance
|
||||
* @param button key code
|
||||
*/
|
||||
bool ble_profile_hid_mouse_press(FuriHalBleProfileBase* profile, uint8_t button);
|
||||
|
||||
/** Set mouse button to released state and send HID report
|
||||
*
|
||||
* @param profile profile instance
|
||||
* @param button key code
|
||||
*/
|
||||
bool ble_profile_hid_mouse_release(FuriHalBleProfileBase* profile, uint8_t button);
|
||||
|
||||
/** Set mouse button to released state and send HID report
|
||||
*
|
||||
* @param profile profile instance
|
||||
* @param button key code
|
||||
*/
|
||||
bool ble_profile_hid_mouse_release_all(FuriHalBleProfileBase* profile);
|
||||
|
||||
/** Set mouse wheel position and send HID report
|
||||
*
|
||||
* @param profile profile instance
|
||||
* @param delta number of scroll steps
|
||||
*/
|
||||
bool ble_profile_hid_mouse_scroll(FuriHalBleProfileBase* profile, int8_t delta);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
320
applications/main/bad_kb/helpers/ble_hid_svc.c
Normal file
320
applications/main/bad_kb/helpers/ble_hid_svc.c
Normal file
@@ -0,0 +1,320 @@
|
||||
#include "ble_hid_svc.h"
|
||||
#include "app_common.h"
|
||||
#include <ble/ble.h>
|
||||
#include <furi_ble/event_dispatcher.h>
|
||||
#include <furi_ble/gatt.h>
|
||||
|
||||
#include <furi.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define TAG "BleHid"
|
||||
|
||||
#define BLE_SVC_HID_REPORT_MAP_MAX_LEN (255)
|
||||
#define BLE_SVC_HID_REPORT_MAX_LEN (255)
|
||||
#define BLE_SVC_HID_REPORT_REF_LEN (2)
|
||||
#define BLE_SVC_HID_INFO_LEN (4)
|
||||
#define BLE_SVC_HID_CONTROL_POINT_LEN (1)
|
||||
|
||||
#define BLE_SVC_HID_INPUT_REPORT_COUNT (3)
|
||||
#define BLE_SVC_HID_OUTPUT_REPORT_COUNT (0)
|
||||
#define BLE_SVC_HID_FEATURE_REPORT_COUNT (0)
|
||||
#define BLE_SVC_HID_REPORT_COUNT \
|
||||
(BLE_SVC_HID_INPUT_REPORT_COUNT + BLE_SVC_HID_OUTPUT_REPORT_COUNT + \
|
||||
BLE_SVC_HID_FEATURE_REPORT_COUNT)
|
||||
|
||||
typedef enum {
|
||||
HidSvcGattCharacteristicProtocolMode = 0,
|
||||
HidSvcGattCharacteristicReportMap,
|
||||
HidSvcGattCharacteristicInfo,
|
||||
HidSvcGattCharacteristicCtrlPoint,
|
||||
HidSvcGattCharacteristicCount,
|
||||
} HidSvcGattCharacteristicId;
|
||||
|
||||
typedef struct {
|
||||
uint8_t report_idx;
|
||||
uint8_t report_type;
|
||||
} HidSvcReportId;
|
||||
|
||||
static_assert(sizeof(HidSvcReportId) == sizeof(uint16_t), "HidSvcReportId must be 2 bytes");
|
||||
|
||||
static const Service_UUID_t ble_svc_hid_uuid = {
|
||||
.Service_UUID_16 = HUMAN_INTERFACE_DEVICE_SERVICE_UUID,
|
||||
};
|
||||
|
||||
static bool ble_svc_hid_char_desc_data_callback(
|
||||
const void* context,
|
||||
const uint8_t** data,
|
||||
uint16_t* data_len) {
|
||||
const HidSvcReportId* report_id = context;
|
||||
*data_len = sizeof(HidSvcReportId);
|
||||
if(data) {
|
||||
*data = (const uint8_t*)report_id;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
const void* data_ptr;
|
||||
uint16_t data_len;
|
||||
} HidSvcDataWrapper;
|
||||
|
||||
static bool ble_svc_hid_report_data_callback(
|
||||
const void* context,
|
||||
const uint8_t** data,
|
||||
uint16_t* data_len) {
|
||||
const HidSvcDataWrapper* report_data = context;
|
||||
if(data) {
|
||||
*data = report_data->data_ptr;
|
||||
*data_len = report_data->data_len;
|
||||
} else {
|
||||
*data_len = BLE_SVC_HID_REPORT_MAP_MAX_LEN;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static const BleGattCharacteristicParams ble_svc_hid_chars[HidSvcGattCharacteristicCount] = {
|
||||
[HidSvcGattCharacteristicProtocolMode] =
|
||||
{.name = "Protocol Mode",
|
||||
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
||||
.data.fixed.length = 1,
|
||||
.uuid.Char_UUID_16 = PROTOCOL_MODE_CHAR_UUID,
|
||||
.uuid_type = UUID_TYPE_16,
|
||||
.char_properties = CHAR_PROP_READ | CHAR_PROP_WRITE_WITHOUT_RESP,
|
||||
.security_permissions = ATTR_PERMISSION_NONE,
|
||||
.gatt_evt_mask = GATT_NOTIFY_ATTRIBUTE_WRITE,
|
||||
.is_variable = CHAR_VALUE_LEN_CONSTANT},
|
||||
[HidSvcGattCharacteristicReportMap] =
|
||||
{.name = "Report Map",
|
||||
.data_prop_type = FlipperGattCharacteristicDataCallback,
|
||||
.data.callback.fn = ble_svc_hid_report_data_callback,
|
||||
.data.callback.context = NULL,
|
||||
.uuid.Char_UUID_16 = REPORT_MAP_CHAR_UUID,
|
||||
.uuid_type = UUID_TYPE_16,
|
||||
.char_properties = CHAR_PROP_READ,
|
||||
.security_permissions = ATTR_PERMISSION_NONE,
|
||||
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
|
||||
.is_variable = CHAR_VALUE_LEN_VARIABLE},
|
||||
[HidSvcGattCharacteristicInfo] =
|
||||
{.name = "HID Information",
|
||||
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
||||
.data.fixed.length = BLE_SVC_HID_INFO_LEN,
|
||||
.data.fixed.ptr = NULL,
|
||||
.uuid.Char_UUID_16 = HID_INFORMATION_CHAR_UUID,
|
||||
.uuid_type = UUID_TYPE_16,
|
||||
.char_properties = CHAR_PROP_READ,
|
||||
.security_permissions = ATTR_PERMISSION_NONE,
|
||||
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
|
||||
.is_variable = CHAR_VALUE_LEN_CONSTANT},
|
||||
[HidSvcGattCharacteristicCtrlPoint] =
|
||||
{.name = "HID Control Point",
|
||||
.data_prop_type = FlipperGattCharacteristicDataFixed,
|
||||
.data.fixed.length = BLE_SVC_HID_CONTROL_POINT_LEN,
|
||||
.uuid.Char_UUID_16 = HID_CONTROL_POINT_CHAR_UUID,
|
||||
.uuid_type = UUID_TYPE_16,
|
||||
.char_properties = CHAR_PROP_WRITE_WITHOUT_RESP,
|
||||
.security_permissions = ATTR_PERMISSION_NONE,
|
||||
.gatt_evt_mask = GATT_NOTIFY_ATTRIBUTE_WRITE,
|
||||
.is_variable = CHAR_VALUE_LEN_CONSTANT},
|
||||
};
|
||||
|
||||
static const BleGattCharacteristicDescriptorParams ble_svc_hid_char_descr_template = {
|
||||
.uuid_type = UUID_TYPE_16,
|
||||
.uuid.Char_UUID_16 = REPORT_REFERENCE_DESCRIPTOR_UUID,
|
||||
.max_length = BLE_SVC_HID_REPORT_REF_LEN,
|
||||
.data_callback.fn = ble_svc_hid_char_desc_data_callback,
|
||||
.security_permissions = ATTR_PERMISSION_NONE,
|
||||
.access_permissions = ATTR_ACCESS_READ_WRITE,
|
||||
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
|
||||
.is_variable = CHAR_VALUE_LEN_CONSTANT,
|
||||
};
|
||||
|
||||
static const BleGattCharacteristicParams ble_svc_hid_report_template = {
|
||||
.name = "Report",
|
||||
.data_prop_type = FlipperGattCharacteristicDataCallback,
|
||||
.data.callback.fn = ble_svc_hid_report_data_callback,
|
||||
.data.callback.context = NULL,
|
||||
.uuid.Char_UUID_16 = REPORT_CHAR_UUID,
|
||||
.uuid_type = UUID_TYPE_16,
|
||||
.char_properties = CHAR_PROP_READ | CHAR_PROP_NOTIFY,
|
||||
.security_permissions = ATTR_PERMISSION_NONE,
|
||||
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
|
||||
.is_variable = CHAR_VALUE_LEN_VARIABLE,
|
||||
};
|
||||
|
||||
struct BleServiceHid {
|
||||
uint16_t svc_handle;
|
||||
BleGattCharacteristicInstance chars[HidSvcGattCharacteristicCount];
|
||||
BleGattCharacteristicInstance input_report_chars[BLE_SVC_HID_INPUT_REPORT_COUNT];
|
||||
BleGattCharacteristicInstance output_report_chars[BLE_SVC_HID_OUTPUT_REPORT_COUNT];
|
||||
BleGattCharacteristicInstance feature_report_chars[BLE_SVC_HID_FEATURE_REPORT_COUNT];
|
||||
GapSvcEventHandler* event_handler;
|
||||
};
|
||||
|
||||
static BleEventAckStatus ble_svc_hid_event_handler(void* event, void* context) {
|
||||
UNUSED(context);
|
||||
|
||||
BleEventAckStatus ret = BleEventNotAck;
|
||||
hci_event_pckt* event_pckt = (hci_event_pckt*)(((hci_uart_pckt*)event)->data);
|
||||
evt_blecore_aci* blecore_evt = (evt_blecore_aci*)event_pckt->data;
|
||||
// aci_gatt_attribute_modified_event_rp0* attribute_modified;
|
||||
if(event_pckt->evt == HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE) {
|
||||
if(blecore_evt->ecode == ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE) {
|
||||
// Process modification events
|
||||
ret = BleEventAckFlowEnable;
|
||||
} else if(blecore_evt->ecode == ACI_GATT_SERVER_CONFIRMATION_VSEVT_CODE) {
|
||||
// Process notification confirmation
|
||||
ret = BleEventAckFlowEnable;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
BleServiceHid* ble_svc_hid_start() {
|
||||
BleServiceHid* hid_svc = malloc(sizeof(BleServiceHid));
|
||||
|
||||
// Register event handler
|
||||
hid_svc->event_handler =
|
||||
ble_event_dispatcher_register_svc_handler(ble_svc_hid_event_handler, hid_svc);
|
||||
/**
|
||||
* Add Human Interface Device Service
|
||||
*/
|
||||
if(!ble_gatt_service_add(
|
||||
UUID_TYPE_16,
|
||||
&ble_svc_hid_uuid,
|
||||
PRIMARY_SERVICE,
|
||||
2 + /* protocol mode */
|
||||
(4 * BLE_SVC_HID_INPUT_REPORT_COUNT) + (3 * BLE_SVC_HID_OUTPUT_REPORT_COUNT) +
|
||||
(3 * BLE_SVC_HID_FEATURE_REPORT_COUNT) + 1 + 2 + 2 +
|
||||
2, /* Service + Report Map + HID Information + HID Control Point */
|
||||
&hid_svc->svc_handle)) {
|
||||
free(hid_svc);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Maintain previously defined characteristic order
|
||||
ble_gatt_characteristic_init(
|
||||
hid_svc->svc_handle,
|
||||
&ble_svc_hid_chars[HidSvcGattCharacteristicProtocolMode],
|
||||
&hid_svc->chars[HidSvcGattCharacteristicProtocolMode]);
|
||||
|
||||
uint8_t protocol_mode = 1;
|
||||
ble_gatt_characteristic_update(
|
||||
hid_svc->svc_handle,
|
||||
&hid_svc->chars[HidSvcGattCharacteristicProtocolMode],
|
||||
&protocol_mode);
|
||||
|
||||
// reports
|
||||
BleGattCharacteristicDescriptorParams ble_svc_hid_char_descr;
|
||||
BleGattCharacteristicParams report_char;
|
||||
HidSvcReportId report_id;
|
||||
|
||||
memcpy(
|
||||
&ble_svc_hid_char_descr, &ble_svc_hid_char_descr_template, sizeof(ble_svc_hid_char_descr));
|
||||
memcpy(&report_char, &ble_svc_hid_report_template, sizeof(report_char));
|
||||
|
||||
ble_svc_hid_char_descr.data_callback.context = &report_id;
|
||||
report_char.descriptor_params = &ble_svc_hid_char_descr;
|
||||
|
||||
typedef struct {
|
||||
uint8_t report_type;
|
||||
uint8_t report_count;
|
||||
BleGattCharacteristicInstance* chars;
|
||||
} HidSvcReportCharProps;
|
||||
|
||||
HidSvcReportCharProps hid_report_chars[] = {
|
||||
{0x01, BLE_SVC_HID_INPUT_REPORT_COUNT, hid_svc->input_report_chars},
|
||||
{0x02, BLE_SVC_HID_OUTPUT_REPORT_COUNT, hid_svc->output_report_chars},
|
||||
{0x03, BLE_SVC_HID_FEATURE_REPORT_COUNT, hid_svc->feature_report_chars},
|
||||
};
|
||||
|
||||
for(size_t report_type_idx = 0; report_type_idx < COUNT_OF(hid_report_chars);
|
||||
report_type_idx++) {
|
||||
report_id.report_type = hid_report_chars[report_type_idx].report_type;
|
||||
for(size_t report_idx = 0; report_idx < hid_report_chars[report_type_idx].report_count;
|
||||
report_idx++) {
|
||||
report_id.report_idx = report_idx + 1;
|
||||
ble_gatt_characteristic_init(
|
||||
hid_svc->svc_handle,
|
||||
&report_char,
|
||||
&hid_report_chars[report_type_idx].chars[report_idx]);
|
||||
}
|
||||
}
|
||||
|
||||
// Setup remaining characteristics
|
||||
for(size_t i = HidSvcGattCharacteristicReportMap; i < HidSvcGattCharacteristicCount; i++) {
|
||||
ble_gatt_characteristic_init(
|
||||
hid_svc->svc_handle, &ble_svc_hid_chars[i], &hid_svc->chars[i]);
|
||||
}
|
||||
|
||||
return hid_svc;
|
||||
}
|
||||
|
||||
bool ble_svc_hid_update_report_map(BleServiceHid* hid_svc, const uint8_t* data, uint16_t len) {
|
||||
furi_assert(data);
|
||||
furi_assert(hid_svc);
|
||||
|
||||
HidSvcDataWrapper report_data = {
|
||||
.data_ptr = data,
|
||||
.data_len = len,
|
||||
};
|
||||
return ble_gatt_characteristic_update(
|
||||
hid_svc->svc_handle, &hid_svc->chars[HidSvcGattCharacteristicReportMap], &report_data);
|
||||
}
|
||||
|
||||
bool ble_svc_hid_update_input_report(
|
||||
BleServiceHid* hid_svc,
|
||||
uint8_t input_report_num,
|
||||
uint8_t* data,
|
||||
uint16_t len) {
|
||||
furi_assert(data);
|
||||
furi_assert(hid_svc);
|
||||
furi_assert(input_report_num < BLE_SVC_HID_INPUT_REPORT_COUNT);
|
||||
|
||||
HidSvcDataWrapper report_data = {
|
||||
.data_ptr = data,
|
||||
.data_len = len,
|
||||
};
|
||||
return ble_gatt_characteristic_update(
|
||||
hid_svc->svc_handle, &hid_svc->input_report_chars[input_report_num], &report_data);
|
||||
}
|
||||
|
||||
bool ble_svc_hid_update_info(BleServiceHid* hid_svc, uint8_t* data) {
|
||||
furi_assert(data);
|
||||
furi_assert(hid_svc);
|
||||
|
||||
return ble_gatt_characteristic_update(
|
||||
hid_svc->svc_handle, &hid_svc->chars[HidSvcGattCharacteristicInfo], &data);
|
||||
}
|
||||
|
||||
void ble_svc_hid_stop(BleServiceHid* hid_svc) {
|
||||
furi_assert(hid_svc);
|
||||
ble_event_dispatcher_unregister_svc_handler(hid_svc->event_handler);
|
||||
// Delete characteristics
|
||||
for(size_t i = 0; i < HidSvcGattCharacteristicCount; i++) {
|
||||
ble_gatt_characteristic_delete(hid_svc->svc_handle, &hid_svc->chars[i]);
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
uint8_t report_count;
|
||||
BleGattCharacteristicInstance* chars;
|
||||
} HidSvcReportCharProps;
|
||||
|
||||
HidSvcReportCharProps hid_report_chars[] = {
|
||||
{BLE_SVC_HID_INPUT_REPORT_COUNT, hid_svc->input_report_chars},
|
||||
{BLE_SVC_HID_OUTPUT_REPORT_COUNT, hid_svc->output_report_chars},
|
||||
{BLE_SVC_HID_FEATURE_REPORT_COUNT, hid_svc->feature_report_chars},
|
||||
};
|
||||
|
||||
for(size_t report_type_idx = 0; report_type_idx < COUNT_OF(hid_report_chars);
|
||||
report_type_idx++) {
|
||||
for(size_t report_idx = 0; report_idx < hid_report_chars[report_type_idx].report_count;
|
||||
report_idx++) {
|
||||
ble_gatt_characteristic_delete(
|
||||
hid_svc->svc_handle, &hid_report_chars[report_type_idx].chars[report_idx]);
|
||||
}
|
||||
}
|
||||
|
||||
// Delete service
|
||||
ble_gatt_service_delete(hid_svc->svc_handle);
|
||||
free(hid_svc);
|
||||
}
|
||||
29
applications/main/bad_kb/helpers/ble_hid_svc.h
Normal file
29
applications/main/bad_kb/helpers/ble_hid_svc.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct BleServiceHid BleServiceHid;
|
||||
|
||||
BleServiceHid* ble_svc_hid_start();
|
||||
|
||||
void ble_svc_hid_stop(BleServiceHid* service);
|
||||
|
||||
bool ble_svc_hid_update_report_map(BleServiceHid* service, const uint8_t* data, uint16_t len);
|
||||
|
||||
bool ble_svc_hid_update_input_report(
|
||||
BleServiceHid* service,
|
||||
uint8_t input_report_num,
|
||||
uint8_t* data,
|
||||
uint16_t len);
|
||||
|
||||
// Expects data to be of length BLE_SVC_HID_INFO_LEN (4 bytes)
|
||||
bool ble_svc_hid_update_info(BleServiceHid* service, uint8_t* data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <gui/gui.h>
|
||||
#include <input/input.h>
|
||||
#include <lib/toolbox/args.h>
|
||||
#include <furi_hal_bt_hid.h>
|
||||
#include "ble_hid.h"
|
||||
#include <furi_hal_usb_hid.h>
|
||||
#include <bt/bt_service/bt.h>
|
||||
#include <storage/storage.h>
|
||||
@@ -14,10 +14,8 @@
|
||||
#include <xtreme/xtreme.h>
|
||||
#include "../scenes/bad_kb_scene.h"
|
||||
|
||||
const uint8_t BAD_KB_EMPTY_MAC[BAD_KB_MAC_LEN] = FURI_HAL_BT_EMPTY_MAC_ADDR;
|
||||
|
||||
// Adjusts to serial MAC +2 in app init
|
||||
uint8_t BAD_KB_BOUND_MAC[BAD_KB_MAC_LEN] = FURI_HAL_BT_EMPTY_MAC_ADDR;
|
||||
uint8_t BAD_KB_BOUND_MAC[GAP_MAC_ADDR_SIZE] = {0};
|
||||
|
||||
#define TAG "BadKb"
|
||||
#define WORKER_TAG TAG "Worker"
|
||||
@@ -27,11 +25,11 @@ uint8_t BAD_KB_BOUND_MAC[BAD_KB_MAC_LEN] = FURI_HAL_BT_EMPTY_MAC_ADDR;
|
||||
|
||||
// Delays for waiting between HID key press and key release
|
||||
const uint8_t bt_hid_delays[LevelRssiNum] = {
|
||||
45, // LevelRssi122_100
|
||||
38, // LevelRssi99_80
|
||||
30, // LevelRssi79_60
|
||||
26, // LevelRssi59_40
|
||||
21, // LevelRssi39_0
|
||||
60, // LevelRssi122_100
|
||||
55, // LevelRssi99_80
|
||||
50, // LevelRssi79_60
|
||||
47, // LevelRssi59_40
|
||||
34, // LevelRssi39_0
|
||||
};
|
||||
|
||||
uint8_t bt_timeout = 0;
|
||||
@@ -120,12 +118,17 @@ bool ducky_get_number(const char* param, uint32_t* val) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t furi_hal_bt_hid_get_led_state() {
|
||||
// FIXME
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ducky_numlock_on(BadKbScript* bad_kb) {
|
||||
if(bad_kb->bt) {
|
||||
if((furi_hal_bt_hid_get_led_state() & HID_KB_LED_NUM) == 0) {
|
||||
furi_hal_bt_hid_kb_press(HID_KEYBOARD_LOCK_NUM_LOCK);
|
||||
ble_profile_hid_kb_press(bad_kb->app->ble_hid, HID_KEYBOARD_LOCK_NUM_LOCK);
|
||||
furi_delay_ms(bt_timeout);
|
||||
furi_hal_bt_hid_kb_release(HID_KEYBOARD_LOCK_NUM_LOCK);
|
||||
ble_profile_hid_kb_release(bad_kb->app->ble_hid, HID_KEYBOARD_LOCK_NUM_LOCK);
|
||||
}
|
||||
} else {
|
||||
if((furi_hal_hid_get_led_state() & HID_KB_LED_NUM) == 0) {
|
||||
@@ -140,9 +143,9 @@ bool ducky_numpad_press(BadKbScript* bad_kb, const char num) {
|
||||
|
||||
uint16_t key = numpad_keys[num - '0'];
|
||||
if(bad_kb->bt) {
|
||||
furi_hal_bt_hid_kb_press(key);
|
||||
ble_profile_hid_kb_press(bad_kb->app->ble_hid, key);
|
||||
furi_delay_ms(bt_timeout);
|
||||
furi_hal_bt_hid_kb_release(key);
|
||||
ble_profile_hid_kb_release(bad_kb->app->ble_hid, key);
|
||||
} else {
|
||||
furi_hal_hid_kb_press(key);
|
||||
furi_hal_hid_kb_release(key);
|
||||
@@ -156,7 +159,7 @@ bool ducky_altchar(BadKbScript* bad_kb, const char* charcode) {
|
||||
bool state = false;
|
||||
|
||||
if(bad_kb->bt) {
|
||||
furi_hal_bt_hid_kb_press(KEY_MOD_LEFT_ALT);
|
||||
ble_profile_hid_kb_press(bad_kb->app->ble_hid, KEY_MOD_LEFT_ALT);
|
||||
} else {
|
||||
furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT);
|
||||
}
|
||||
@@ -168,7 +171,7 @@ bool ducky_altchar(BadKbScript* bad_kb, const char* charcode) {
|
||||
}
|
||||
|
||||
if(bad_kb->bt) {
|
||||
furi_hal_bt_hid_kb_release(KEY_MOD_LEFT_ALT);
|
||||
ble_profile_hid_kb_release(bad_kb->app->ble_hid, KEY_MOD_LEFT_ALT);
|
||||
} else {
|
||||
furi_hal_hid_kb_release(KEY_MOD_LEFT_ALT);
|
||||
}
|
||||
@@ -213,9 +216,9 @@ bool ducky_string(BadKbScript* bad_kb, const char* param) {
|
||||
uint16_t keycode = BADKB_ASCII_TO_KEY(bad_kb, param[i]);
|
||||
if(keycode != HID_KEYBOARD_NONE) {
|
||||
if(bad_kb->bt) {
|
||||
furi_hal_bt_hid_kb_press(keycode);
|
||||
ble_profile_hid_kb_press(bad_kb->app->ble_hid, keycode);
|
||||
furi_delay_ms(bt_timeout);
|
||||
furi_hal_bt_hid_kb_release(keycode);
|
||||
ble_profile_hid_kb_release(bad_kb->app->ble_hid, keycode);
|
||||
} else {
|
||||
furi_hal_hid_kb_press(keycode);
|
||||
furi_hal_hid_kb_release(keycode);
|
||||
@@ -223,9 +226,9 @@ bool ducky_string(BadKbScript* bad_kb, const char* param) {
|
||||
}
|
||||
} else {
|
||||
if(bad_kb->bt) {
|
||||
furi_hal_bt_hid_kb_press(HID_KEYBOARD_RETURN);
|
||||
ble_profile_hid_kb_press(bad_kb->app->ble_hid, HID_KEYBOARD_RETURN);
|
||||
furi_delay_ms(bt_timeout);
|
||||
furi_hal_bt_hid_kb_release(HID_KEYBOARD_RETURN);
|
||||
ble_profile_hid_kb_release(bad_kb->app->ble_hid, HID_KEYBOARD_RETURN);
|
||||
} else {
|
||||
furi_hal_hid_kb_press(HID_KEYBOARD_RETURN);
|
||||
furi_hal_hid_kb_release(HID_KEYBOARD_RETURN);
|
||||
@@ -248,9 +251,9 @@ static bool ducky_string_next(BadKbScript* bad_kb) {
|
||||
uint16_t keycode = BADKB_ASCII_TO_KEY(bad_kb, print_char);
|
||||
if(keycode != HID_KEYBOARD_NONE) {
|
||||
if(bad_kb->bt) {
|
||||
furi_hal_bt_hid_kb_press(keycode);
|
||||
ble_profile_hid_kb_press(bad_kb->app->ble_hid, keycode);
|
||||
furi_delay_ms(bt_timeout);
|
||||
furi_hal_bt_hid_kb_release(keycode);
|
||||
ble_profile_hid_kb_release(bad_kb->app->ble_hid, keycode);
|
||||
} else {
|
||||
furi_hal_hid_kb_press(keycode);
|
||||
furi_hal_hid_kb_release(keycode);
|
||||
@@ -258,9 +261,9 @@ static bool ducky_string_next(BadKbScript* bad_kb) {
|
||||
}
|
||||
} else {
|
||||
if(bad_kb->bt) {
|
||||
furi_hal_bt_hid_kb_press(HID_KEYBOARD_RETURN);
|
||||
ble_profile_hid_kb_press(bad_kb->app->ble_hid, HID_KEYBOARD_RETURN);
|
||||
furi_delay_ms(bt_timeout);
|
||||
furi_hal_bt_hid_kb_release(HID_KEYBOARD_RETURN);
|
||||
ble_profile_hid_kb_release(bad_kb->app->ble_hid, HID_KEYBOARD_RETURN);
|
||||
} else {
|
||||
furi_hal_hid_kb_press(HID_KEYBOARD_RETURN);
|
||||
furi_hal_hid_kb_release(HID_KEYBOARD_RETURN);
|
||||
@@ -302,9 +305,9 @@ static int32_t ducky_parse_line(BadKbScript* bad_kb, FuriString* line) {
|
||||
}
|
||||
}
|
||||
if(bad_kb->bt) {
|
||||
furi_hal_bt_hid_kb_press(key);
|
||||
ble_profile_hid_kb_press(bad_kb->app->ble_hid, key);
|
||||
furi_delay_ms(bt_timeout);
|
||||
furi_hal_bt_hid_kb_release(key);
|
||||
ble_profile_hid_kb_release(bad_kb->app->ble_hid, key);
|
||||
} else {
|
||||
furi_hal_hid_kb_press(key);
|
||||
furi_hal_hid_kb_release(key);
|
||||
@@ -313,7 +316,7 @@ static int32_t ducky_parse_line(BadKbScript* bad_kb, FuriString* line) {
|
||||
}
|
||||
|
||||
static bool ducky_set_usb_id(BadKbScript* bad_kb, const char* line) {
|
||||
FuriHalUsbHidConfig* cfg = &bad_kb->app->id_config.usb_cfg;
|
||||
FuriHalUsbHidConfig* cfg = &bad_kb->app->id_config.usb;
|
||||
|
||||
if(sscanf(line, "%lX:%lX", &cfg->vid, &cfg->pid) == 2) {
|
||||
cfg->manuf[0] = '\0';
|
||||
@@ -339,20 +342,20 @@ static bool ducky_set_bt_id(BadKbScript* bad_kb, const char* line) {
|
||||
BadKbConfig* cfg = &bad_kb->app->id_config;
|
||||
|
||||
size_t line_len = strlen(line);
|
||||
size_t mac_len = BAD_KB_MAC_LEN * 3; // 2 text chars + separator per byte
|
||||
size_t mac_len = sizeof(cfg->ble.mac) * 3; // 2 text chars + separator per byte
|
||||
if(line_len < mac_len + 1) return false; // MAC + at least 1 char for name
|
||||
|
||||
for(size_t i = 0; i < BAD_KB_MAC_LEN; i++) {
|
||||
for(size_t i = 0; i < sizeof(cfg->ble.mac); i++) {
|
||||
char a = line[i * 3];
|
||||
char b = line[i * 3 + 1];
|
||||
if((a < 'A' && a > 'F') || (a < '0' && a > '9') || (b < 'A' && b > 'F') ||
|
||||
(b < '0' && b > '9') || !hex_char_to_uint8(a, b, &cfg->bt_mac[i])) {
|
||||
(b < '0' && b > '9') || !hex_char_to_uint8(a, b, &cfg->ble.mac[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
furi_hal_bt_reverse_mac_addr(cfg->bt_mac);
|
||||
furi_hal_bt_reverse_mac_addr(cfg->ble.mac);
|
||||
|
||||
strlcpy(cfg->bt_name, line + mac_len, BAD_KB_NAME_LEN);
|
||||
strlcpy(cfg->ble.name, line + mac_len, sizeof(cfg->ble.name));
|
||||
FURI_LOG_D(WORKER_TAG, "set bt id: %s", line);
|
||||
return true;
|
||||
}
|
||||
@@ -515,37 +518,29 @@ static uint32_t bad_kb_flags_get(uint32_t flags_mask, uint32_t timeout) {
|
||||
|
||||
int32_t bad_kb_conn_apply(BadKbApp* app) {
|
||||
if(app->is_bt) {
|
||||
// Shorthands so this bs is readable
|
||||
BadKbConfig* cfg = app->set_bt_id ? &app->id_config : &app->config;
|
||||
FuriHalBtProfile kbd = FuriHalBtProfileHidKeyboard;
|
||||
|
||||
// Setup new config
|
||||
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);
|
||||
furi_hal_bt_set_profile_adv_name(kbd, cfg->bt_name);
|
||||
|
||||
// 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) {
|
||||
furi_hal_bt_set_profile_mac_addr(kbd, BAD_KB_BOUND_MAC);
|
||||
furi_hal_bt_set_profile_pairing_method(kbd, GapPairingPinCodeVerifyYesNo);
|
||||
app->cur_ble_cfg.pairing = GapPairingPinCodeVerifyYesNo;
|
||||
} else {
|
||||
furi_hal_bt_set_profile_mac_addr(kbd, cfg->bt_mac);
|
||||
furi_hal_bt_set_profile_pairing_method(kbd, GapPairingNone);
|
||||
app->cur_ble_cfg.pairing = GapPairingNone;
|
||||
memcpy(app->cur_ble_cfg.mac, BAD_KB_BOUND_MAC, sizeof(BAD_KB_BOUND_MAC));
|
||||
}
|
||||
|
||||
// Set profile, restart BT, adjust defaults
|
||||
furi_check(bt_set_profile(app->bt, BtProfileHidKeyboard));
|
||||
// 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();
|
||||
|
||||
// Toggle key callback after since BT restart resets it
|
||||
if(app->bt_remember) {
|
||||
bt_enable_peer_key_update(app->bt);
|
||||
} else {
|
||||
bt_disable_peer_key_update(app->bt);
|
||||
}
|
||||
|
||||
app->conn_mode = BadKbConnModeBt;
|
||||
|
||||
} else {
|
||||
@@ -553,14 +548,16 @@ int32_t bad_kb_conn_apply(BadKbApp* app) {
|
||||
furi_hal_usb_unlock();
|
||||
|
||||
// Context will apply with set_config only if pointer address is different, so we use a copy
|
||||
FuriHalUsbHidConfig* hid_cfg = malloc(sizeof(FuriHalUsbHidConfig));
|
||||
memcpy(
|
||||
hid_cfg,
|
||||
app->set_usb_id ? &app->id_config.usb_cfg : &app->config.usb_cfg,
|
||||
sizeof(FuriHalUsbHidConfig));
|
||||
furi_check(furi_hal_usb_set_config(&usb_hid, hid_cfg));
|
||||
if(app->hid_cfg) free(app->hid_cfg);
|
||||
app->hid_cfg = hid_cfg;
|
||||
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;
|
||||
}
|
||||
@@ -570,17 +567,10 @@ int32_t bad_kb_conn_apply(BadKbApp* app) {
|
||||
|
||||
void bad_kb_conn_reset(BadKbApp* app) {
|
||||
if(app->conn_mode == BadKbConnModeBt) {
|
||||
// TODO: maybe also restore BT profile?
|
||||
bt_disconnect(app->bt);
|
||||
furi_delay_ms(200);
|
||||
bt_keys_storage_set_default_path(app->bt);
|
||||
FuriHalBtProfile kbd = FuriHalBtProfileHidKeyboard;
|
||||
furi_hal_bt_set_profile_mac_addr(kbd, app->prev_bt_mac);
|
||||
furi_hal_bt_set_profile_adv_name(kbd, app->prev_bt_name);
|
||||
furi_hal_bt_set_profile_pairing_method(kbd, app->prev_bt_mode);
|
||||
furi_check(bt_set_profile(app->bt, BtProfileSerial));
|
||||
bt_enable_peer_key_update(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));
|
||||
@@ -591,24 +581,24 @@ void bad_kb_conn_reset(BadKbApp* app) {
|
||||
|
||||
void bad_kb_config_adjust(BadKbConfig* cfg) {
|
||||
// Avoid empty name
|
||||
if(strcmp(cfg->bt_name, "") == 0) {
|
||||
snprintf(cfg->bt_name, BAD_KB_NAME_LEN, "Control %s", furi_hal_version_get_name_ptr());
|
||||
if(cfg->ble.name[0] == '\0') {
|
||||
snprintf(
|
||||
cfg->ble.name, sizeof(cfg->ble.name), "Control %s", furi_hal_version_get_name_ptr());
|
||||
}
|
||||
|
||||
// MAC is adjusted by furi_hal_bt, adjust here too so it matches after applying
|
||||
const uint8_t* normal_mac = furi_hal_version_get_ble_mac();
|
||||
uint8_t empty_mac[BAD_KB_MAC_LEN] = FURI_HAL_BT_EMPTY_MAC_ADDR;
|
||||
uint8_t default_mac[BAD_KB_MAC_LEN] = FURI_HAL_BT_DEFAULT_MAC_ADDR;
|
||||
if(memcmp(cfg->bt_mac, empty_mac, BAD_KB_MAC_LEN) == 0 ||
|
||||
memcmp(cfg->bt_mac, normal_mac, BAD_KB_MAC_LEN) == 0 ||
|
||||
memcmp(cfg->bt_mac, default_mac, BAD_KB_MAC_LEN) == 0) {
|
||||
memcpy(cfg->bt_mac, normal_mac, BAD_KB_MAC_LEN);
|
||||
cfg->bt_mac[2]++;
|
||||
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_cfg.vid == 0) cfg->usb_cfg.vid = HID_VID_DEFAULT;
|
||||
if(cfg->usb_cfg.pid == 0) cfg->usb_cfg.pid = HID_PID_DEFAULT;
|
||||
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) {
|
||||
@@ -630,14 +620,10 @@ void bad_kb_config_refresh(BadKbApp* app) {
|
||||
apply = true;
|
||||
bad_kb_conn_reset(app);
|
||||
} else {
|
||||
apply = apply || strncmp(
|
||||
cfg->bt_name,
|
||||
furi_hal_bt_get_profile_adv_name(FuriHalBtProfileHidKeyboard),
|
||||
BAD_KB_NAME_LEN);
|
||||
apply = apply || memcmp(
|
||||
app->bt_remember ? BAD_KB_BOUND_MAC : cfg->bt_mac,
|
||||
furi_hal_bt_get_profile_mac_addr(FuriHalBtProfileHidKeyboard),
|
||||
BAD_KB_MAC_LEN);
|
||||
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;
|
||||
@@ -647,17 +633,11 @@ void bad_kb_config_refresh(BadKbApp* app) {
|
||||
apply = true;
|
||||
bad_kb_conn_reset(app);
|
||||
} else {
|
||||
void* ctx;
|
||||
if(furi_hal_usb_get_config() == &usb_hid &&
|
||||
(ctx = furi_hal_usb_get_config_context()) != NULL) {
|
||||
FuriHalUsbHidConfig* cur = ctx;
|
||||
apply = apply || cfg->usb_cfg.vid != cur->vid;
|
||||
apply = apply || cfg->usb_cfg.pid != cur->pid;
|
||||
apply = apply || strncmp(cfg->usb_cfg.manuf, cur->manuf, sizeof(cur->manuf));
|
||||
apply = apply || strncmp(cfg->usb_cfg.product, cur->product, sizeof(cur->product));
|
||||
} else {
|
||||
apply = true;
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -838,14 +818,14 @@ static int32_t bad_kb_worker(void* context) {
|
||||
} else if(flags & WorkerEvtStartStop) {
|
||||
worker_state = BadKbStateIdle; // Stop executing script
|
||||
if(bad_kb->bt) {
|
||||
furi_hal_bt_hid_kb_release_all();
|
||||
ble_profile_hid_kb_release_all(bad_kb->app->ble_hid);
|
||||
} else {
|
||||
furi_hal_hid_kb_release_all();
|
||||
}
|
||||
} else if(flags & WorkerEvtDisconnect) {
|
||||
worker_state = BadKbStateNotConnected; // Disconnected
|
||||
if(bad_kb->bt) {
|
||||
furi_hal_bt_hid_kb_release_all();
|
||||
ble_profile_hid_kb_release_all(bad_kb->app->ble_hid);
|
||||
} else {
|
||||
furi_hal_hid_kb_release_all();
|
||||
}
|
||||
@@ -871,7 +851,7 @@ static int32_t bad_kb_worker(void* context) {
|
||||
worker_state = BadKbStateScriptError;
|
||||
bad_kb->st.state = worker_state;
|
||||
if(bad_kb->bt) {
|
||||
furi_hal_bt_hid_kb_release_all();
|
||||
ble_profile_hid_kb_release_all(bad_kb->app->ble_hid);
|
||||
} else {
|
||||
furi_hal_hid_kb_release_all();
|
||||
}
|
||||
@@ -880,7 +860,7 @@ static int32_t bad_kb_worker(void* context) {
|
||||
worker_state = BadKbStateIdle;
|
||||
bad_kb->st.state = BadKbStateDone;
|
||||
if(bad_kb->bt) {
|
||||
furi_hal_bt_hid_kb_release_all();
|
||||
ble_profile_hid_kb_release_all(bad_kb->app->ble_hid);
|
||||
} else {
|
||||
furi_hal_hid_kb_release_all();
|
||||
}
|
||||
@@ -917,7 +897,7 @@ static int32_t bad_kb_worker(void* context) {
|
||||
} else if(flags & WorkerEvtDisconnect) {
|
||||
worker_state = BadKbStateNotConnected; // Disconnected
|
||||
if(bad_kb->bt) {
|
||||
furi_hal_bt_hid_kb_release_all();
|
||||
ble_profile_hid_kb_release_all(bad_kb->app->ble_hid);
|
||||
} else {
|
||||
furi_hal_hid_kb_release_all();
|
||||
}
|
||||
@@ -940,7 +920,7 @@ static int32_t bad_kb_worker(void* context) {
|
||||
worker_state = BadKbStateIdle; // Stop executing script
|
||||
bad_kb->st.state = worker_state;
|
||||
if(bad_kb->bt) {
|
||||
furi_hal_bt_hid_kb_release_all();
|
||||
ble_profile_hid_kb_release_all(bad_kb->app->ble_hid);
|
||||
} else {
|
||||
furi_hal_hid_kb_release_all();
|
||||
}
|
||||
@@ -948,7 +928,7 @@ static int32_t bad_kb_worker(void* context) {
|
||||
worker_state = BadKbStateNotConnected; // Disconnected
|
||||
bad_kb->st.state = worker_state;
|
||||
if(bad_kb->bt) {
|
||||
furi_hal_bt_hid_kb_release_all();
|
||||
ble_profile_hid_kb_release_all(bad_kb->app->ble_hid);
|
||||
} else {
|
||||
furi_hal_hid_kb_release_all();
|
||||
}
|
||||
@@ -983,14 +963,14 @@ static int32_t bad_kb_worker(void* context) {
|
||||
} else if(flags & WorkerEvtStartStop) {
|
||||
worker_state = BadKbStateIdle; // Stop executing script
|
||||
if(bad_kb->bt) {
|
||||
furi_hal_bt_hid_kb_release_all();
|
||||
ble_profile_hid_kb_release_all(bad_kb->app->ble_hid);
|
||||
} else {
|
||||
furi_hal_hid_kb_release_all();
|
||||
}
|
||||
} else if(flags & WorkerEvtDisconnect) {
|
||||
worker_state = BadKbStateNotConnected; // Disconnected
|
||||
if(bad_kb->bt) {
|
||||
furi_hal_bt_hid_kb_release_all();
|
||||
ble_profile_hid_kb_release_all(bad_kb->app->ble_hid);
|
||||
} else {
|
||||
furi_hal_hid_kb_release_all();
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ extern "C" {
|
||||
#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
|
||||
|
||||
@@ -105,21 +106,15 @@ void bad_kb_script_pause_resume(BadKbScript* bad_kb);
|
||||
|
||||
BadKbState* bad_kb_script_get_state(BadKbScript* bad_kb);
|
||||
|
||||
#define BAD_KB_NAME_LEN FURI_HAL_BT_ADV_NAME_LENGTH
|
||||
#define BAD_KB_MAC_LEN GAP_MAC_ADDR_SIZE
|
||||
#define BAD_KB_USB_LEN HID_MANUF_PRODUCT_NAME_LEN
|
||||
|
||||
extern const uint8_t BAD_KB_EMPTY_MAC[BAD_KB_MAC_LEN];
|
||||
extern uint8_t BAD_KB_BOUND_MAC[BAD_KB_MAC_LEN]; // For remember mode
|
||||
extern uint8_t BAD_KB_BOUND_MAC[GAP_MAC_ADDR_SIZE]; // For remember mode
|
||||
|
||||
typedef enum {
|
||||
BadKbAppErrorNoFiles,
|
||||
} BadKbAppError;
|
||||
|
||||
typedef struct {
|
||||
char bt_name[BAD_KB_NAME_LEN];
|
||||
uint8_t bt_mac[BAD_KB_MAC_LEN];
|
||||
FuriHalUsbHidConfig usb_cfg;
|
||||
BleProfileHidParams ble;
|
||||
FuriHalUsbHidConfig usb;
|
||||
} BadKbConfig;
|
||||
|
||||
typedef enum {
|
||||
@@ -139,10 +134,10 @@ struct BadKbApp {
|
||||
TextInput* text_input;
|
||||
ByteInput* byte_input;
|
||||
Loading* loading;
|
||||
char usb_name_buf[BAD_KB_USB_LEN];
|
||||
char usb_name_buf[HID_MANUF_PRODUCT_NAME_LEN];
|
||||
uint16_t usb_vidpid_buf[2];
|
||||
char bt_name_buf[BAD_KB_NAME_LEN];
|
||||
uint8_t bt_mac_buf[BAD_KB_MAC_LEN];
|
||||
char bt_name_buf[FURI_HAL_BT_ADV_NAME_LENGTH];
|
||||
uint8_t bt_mac_buf[GAP_MAC_ADDR_SIZE];
|
||||
|
||||
BadKbAppError error;
|
||||
FuriString* file_path;
|
||||
@@ -161,12 +156,12 @@ struct BadKbApp {
|
||||
bool has_usb_id;
|
||||
bool has_bt_id;
|
||||
|
||||
GapPairing prev_bt_mode;
|
||||
char prev_bt_name[BAD_KB_NAME_LEN];
|
||||
uint8_t prev_bt_mac[BAD_KB_MAC_LEN];
|
||||
FuriHalBleProfileBase* ble_hid;
|
||||
FuriHalUsbInterface* prev_usb_mode;
|
||||
|
||||
FuriHalUsbHidConfig* hid_cfg;
|
||||
BleProfileHidParams cur_ble_cfg;
|
||||
FuriHalUsbHidConfig* cur_usb_cfg;
|
||||
|
||||
BadKbConnMode conn_mode;
|
||||
FuriThread* conn_init_thread;
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include <furi_hal.h>
|
||||
#include <furi_hal_bt_hid.h>
|
||||
#include "ble_hid.h"
|
||||
#include <furi_hal_usb_hid.h>
|
||||
#include "ducky_script.h"
|
||||
#include "ducky_script_i.h"
|
||||
@@ -83,10 +83,11 @@ static int32_t ducky_fnc_sysrq(BadKbScript* bad_kb, const char* line, int32_t pa
|
||||
line = &line[ducky_get_command_len(line) + 1];
|
||||
uint16_t key = ducky_get_keycode(bad_kb, line, true);
|
||||
if(bad_kb->bt) {
|
||||
furi_hal_bt_hid_kb_press(KEY_MOD_LEFT_ALT | HID_KEYBOARD_PRINT_SCREEN);
|
||||
furi_hal_bt_hid_kb_press(key);
|
||||
ble_profile_hid_kb_press(
|
||||
bad_kb->app->ble_hid, KEY_MOD_LEFT_ALT | HID_KEYBOARD_PRINT_SCREEN);
|
||||
ble_profile_hid_kb_press(bad_kb->app->ble_hid, key);
|
||||
furi_delay_ms(bt_timeout);
|
||||
furi_hal_bt_hid_kb_release_all();
|
||||
ble_profile_hid_kb_release_all(bad_kb->app->ble_hid);
|
||||
} else {
|
||||
furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT | HID_KEYBOARD_PRINT_SCREEN);
|
||||
furi_hal_hid_kb_press(key);
|
||||
@@ -132,7 +133,7 @@ static int32_t ducky_fnc_hold(BadKbScript* bad_kb, const char* line, int32_t par
|
||||
return ducky_error(bad_kb, "Too many keys are hold");
|
||||
}
|
||||
if(bad_kb->bt) {
|
||||
furi_hal_bt_hid_kb_press(key);
|
||||
ble_profile_hid_kb_press(bad_kb->app->ble_hid, key);
|
||||
} else {
|
||||
furi_hal_hid_kb_press(key);
|
||||
}
|
||||
@@ -152,7 +153,7 @@ static int32_t ducky_fnc_release(BadKbScript* bad_kb, const char* line, int32_t
|
||||
}
|
||||
bad_kb->key_hold_nb--;
|
||||
if(bad_kb->bt) {
|
||||
furi_hal_bt_hid_kb_release(key);
|
||||
ble_profile_hid_kb_release(bad_kb->app->ble_hid, key);
|
||||
} else {
|
||||
furi_hal_hid_kb_release(key);
|
||||
}
|
||||
|
||||
@@ -143,7 +143,7 @@ bool bad_kb_scene_config_on_event(void* context, SceneManagerEvent event) {
|
||||
scene_manager_next_scene(bad_kb->scene_manager, BadKbSceneConfigBtMac);
|
||||
break;
|
||||
case VarItemListIndexBtRandomizeMac:
|
||||
furi_hal_random_fill_buf(bad_kb->config.bt_mac, BAD_KB_MAC_LEN);
|
||||
furi_hal_random_fill_buf(bad_kb->config.ble.mac, sizeof(bad_kb->config.ble.mac));
|
||||
bad_kb_config_refresh(bad_kb);
|
||||
break;
|
||||
default:
|
||||
@@ -167,8 +167,8 @@ bool bad_kb_scene_config_on_event(void* context, SceneManagerEvent event) {
|
||||
case VarItemListIndexUsbRandomizeVidPid:
|
||||
furi_hal_random_fill_buf(
|
||||
(void*)bad_kb->usb_vidpid_buf, sizeof(bad_kb->usb_vidpid_buf));
|
||||
bad_kb->config.usb_cfg.vid = bad_kb->usb_vidpid_buf[0];
|
||||
bad_kb->config.usb_cfg.pid = bad_kb->usb_vidpid_buf[1];
|
||||
bad_kb->config.usb.vid = bad_kb->usb_vidpid_buf[0];
|
||||
bad_kb->config.usb.pid = bad_kb->usb_vidpid_buf[1];
|
||||
bad_kb_config_refresh(bad_kb);
|
||||
break;
|
||||
default:
|
||||
|
||||
@@ -10,7 +10,7 @@ void bad_kb_scene_config_bt_mac_on_enter(void* context) {
|
||||
BadKbApp* bad_kb = context;
|
||||
ByteInput* byte_input = bad_kb->byte_input;
|
||||
|
||||
memmove(bad_kb->bt_mac_buf, bad_kb->config.bt_mac, BAD_KB_MAC_LEN);
|
||||
memcpy(bad_kb->bt_mac_buf, bad_kb->config.ble.mac, sizeof(bad_kb->bt_mac_buf));
|
||||
furi_hal_bt_reverse_mac_addr(bad_kb->bt_mac_buf);
|
||||
byte_input_set_header_text(byte_input, "Set BT MAC address");
|
||||
|
||||
@@ -20,7 +20,7 @@ void bad_kb_scene_config_bt_mac_on_enter(void* context) {
|
||||
NULL,
|
||||
bad_kb,
|
||||
bad_kb->bt_mac_buf,
|
||||
BAD_KB_MAC_LEN);
|
||||
sizeof(bad_kb->bt_mac_buf));
|
||||
|
||||
view_dispatcher_switch_to_view(bad_kb->view_dispatcher, BadKbAppViewByteInput);
|
||||
}
|
||||
@@ -33,7 +33,7 @@ bool bad_kb_scene_config_bt_mac_on_event(void* context, SceneManagerEvent event)
|
||||
consumed = true;
|
||||
if(event.event == BadKbAppCustomEventByteInputDone) {
|
||||
furi_hal_bt_reverse_mac_addr(bad_kb->bt_mac_buf);
|
||||
memmove(bad_kb->config.bt_mac, bad_kb->bt_mac_buf, BAD_KB_MAC_LEN);
|
||||
memcpy(bad_kb->config.ble.mac, bad_kb->bt_mac_buf, sizeof(bad_kb->config.ble.mac));
|
||||
bad_kb_config_refresh(bad_kb);
|
||||
}
|
||||
scene_manager_previous_scene(bad_kb->scene_manager);
|
||||
|
||||
@@ -10,7 +10,7 @@ void bad_kb_scene_config_bt_name_on_enter(void* context) {
|
||||
BadKbApp* bad_kb = context;
|
||||
TextInput* text_input = bad_kb->text_input;
|
||||
|
||||
strlcpy(bad_kb->bt_name_buf, bad_kb->config.bt_name, BAD_KB_NAME_LEN);
|
||||
strlcpy(bad_kb->bt_name_buf, bad_kb->config.ble.name, sizeof(bad_kb->bt_name_buf));
|
||||
text_input_set_header_text(text_input, "Set BT device name");
|
||||
|
||||
text_input_set_result_callback(
|
||||
@@ -18,7 +18,7 @@ void bad_kb_scene_config_bt_name_on_enter(void* context) {
|
||||
bad_kb_scene_config_bt_name_text_input_callback,
|
||||
bad_kb,
|
||||
bad_kb->bt_name_buf,
|
||||
BAD_KB_NAME_LEN,
|
||||
sizeof(bad_kb->bt_name_buf),
|
||||
true);
|
||||
|
||||
view_dispatcher_switch_to_view(bad_kb->view_dispatcher, BadKbAppViewTextInput);
|
||||
@@ -31,7 +31,7 @@ bool bad_kb_scene_config_bt_name_on_event(void* context, SceneManagerEvent event
|
||||
if(event.type == SceneManagerEventTypeCustom) {
|
||||
consumed = true;
|
||||
if(event.event == BadKbAppCustomEventTextInputDone) {
|
||||
strlcpy(bad_kb->config.bt_name, bad_kb->bt_name_buf, BAD_KB_NAME_LEN);
|
||||
strlcpy(bad_kb->config.ble.name, bad_kb->bt_name_buf, sizeof(bad_kb->config.ble.name));
|
||||
bad_kb_config_refresh(bad_kb);
|
||||
}
|
||||
scene_manager_previous_scene(bad_kb->scene_manager);
|
||||
|
||||
@@ -11,10 +11,10 @@ void bad_kb_scene_config_usb_name_on_enter(void* context) {
|
||||
TextInput* text_input = bad_kb->text_input;
|
||||
|
||||
if(scene_manager_get_scene_state(bad_kb->scene_manager, BadKbSceneConfigUsbName)) {
|
||||
strlcpy(bad_kb->usb_name_buf, bad_kb->config.usb_cfg.manuf, BAD_KB_USB_LEN);
|
||||
strlcpy(bad_kb->usb_name_buf, bad_kb->config.usb.manuf, sizeof(bad_kb->usb_name_buf));
|
||||
text_input_set_header_text(text_input, "Set USB manufacturer name");
|
||||
} else {
|
||||
strlcpy(bad_kb->usb_name_buf, bad_kb->config.usb_cfg.product, BAD_KB_USB_LEN);
|
||||
strlcpy(bad_kb->usb_name_buf, bad_kb->config.usb.product, sizeof(bad_kb->usb_name_buf));
|
||||
text_input_set_header_text(text_input, "Set USB product name");
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ void bad_kb_scene_config_usb_name_on_enter(void* context) {
|
||||
bad_kb_scene_config_usb_name_text_input_callback,
|
||||
bad_kb,
|
||||
bad_kb->usb_name_buf,
|
||||
BAD_KB_USB_LEN,
|
||||
sizeof(bad_kb->usb_name_buf),
|
||||
true);
|
||||
|
||||
view_dispatcher_switch_to_view(bad_kb->view_dispatcher, BadKbAppViewTextInput);
|
||||
@@ -37,9 +37,15 @@ bool bad_kb_scene_config_usb_name_on_event(void* context, SceneManagerEvent even
|
||||
consumed = true;
|
||||
if(event.event == BadKbAppCustomEventTextInputDone) {
|
||||
if(scene_manager_get_scene_state(bad_kb->scene_manager, BadKbSceneConfigUsbName)) {
|
||||
strlcpy(bad_kb->config.usb_cfg.manuf, bad_kb->usb_name_buf, BAD_KB_USB_LEN);
|
||||
strlcpy(
|
||||
bad_kb->config.usb.manuf,
|
||||
bad_kb->usb_name_buf,
|
||||
sizeof(bad_kb->config.usb.product));
|
||||
} else {
|
||||
strlcpy(bad_kb->config.usb_cfg.product, bad_kb->usb_name_buf, BAD_KB_USB_LEN);
|
||||
strlcpy(
|
||||
bad_kb->config.usb.product,
|
||||
bad_kb->usb_name_buf,
|
||||
sizeof(bad_kb->config.usb.product));
|
||||
}
|
||||
bad_kb_config_refresh(bad_kb);
|
||||
}
|
||||
|
||||
@@ -10,8 +10,8 @@ void bad_kb_scene_config_usb_vidpid_on_enter(void* context) {
|
||||
BadKbApp* bad_kb = context;
|
||||
ByteInput* byte_input = bad_kb->byte_input;
|
||||
|
||||
bad_kb->usb_vidpid_buf[0] = __REVSH(bad_kb->config.usb_cfg.vid);
|
||||
bad_kb->usb_vidpid_buf[1] = __REVSH(bad_kb->config.usb_cfg.pid);
|
||||
bad_kb->usb_vidpid_buf[0] = __REVSH(bad_kb->config.usb.vid);
|
||||
bad_kb->usb_vidpid_buf[1] = __REVSH(bad_kb->config.usb.pid);
|
||||
byte_input_set_header_text(byte_input, "Set USB VID:PID");
|
||||
|
||||
byte_input_set_result_callback(
|
||||
@@ -32,8 +32,8 @@ bool bad_kb_scene_config_usb_vidpid_on_event(void* context, SceneManagerEvent ev
|
||||
if(event.type == SceneManagerEventTypeCustom) {
|
||||
consumed = true;
|
||||
if(event.event == BadKbAppCustomEventByteInputDone) {
|
||||
bad_kb->config.usb_cfg.vid = __REVSH(bad_kb->usb_vidpid_buf[0]);
|
||||
bad_kb->config.usb_cfg.pid = __REVSH(bad_kb->usb_vidpid_buf[1]);
|
||||
bad_kb->config.usb.vid = __REVSH(bad_kb->usb_vidpid_buf[0]);
|
||||
bad_kb->config.usb.pid = __REVSH(bad_kb->usb_vidpid_buf[1]);
|
||||
bad_kb_config_refresh(bad_kb);
|
||||
}
|
||||
scene_manager_previous_scene(bad_kb->scene_manager);
|
||||
|
||||
@@ -779,7 +779,7 @@ Function,+,bt_keys_storage_set_storage_path,void,"Bt*, const char*"
|
||||
Function,-,bt_open_rpc_connection,void,Bt*
|
||||
Function,+,bt_profile_restore_default,_Bool,Bt*
|
||||
Function,+,bt_profile_start,FuriHalBleProfileBase*,"Bt*, const FuriHalBleProfileTemplate*, FuriHalBleProfileParams"
|
||||
Function,-,bt_remote_rssi,_Bool,"Bt*, uint8_t*"
|
||||
Function,+,bt_remote_rssi,_Bool,"Bt*, uint8_t*"
|
||||
Function,+,bt_set_status_changed_callback,void,"Bt*, BtStatusChangedCallback, void*"
|
||||
Function,-,bt_settings_load,_Bool,BtSettings*
|
||||
Function,+,bt_settings_save,_Bool,BtSettings*
|
||||
|
||||
|
Reference in New Issue
Block a user