2022-01-05 16:10:18 +00:00
|
|
|
#include "furi_hal_bt_hid.h"
|
2022-07-08 12:36:34 +00:00
|
|
|
#include "furi_hal_usb_hid.h"
|
|
|
|
#include "usb_hid.h"
|
2021-12-08 11:28:01 +00:00
|
|
|
#include "dev_info_service.h"
|
|
|
|
#include "battery_service.h"
|
|
|
|
#include "hid_service.h"
|
|
|
|
|
|
|
|
#include <furi.h>
|
|
|
|
|
|
|
|
#define FURI_HAL_BT_INFO_BASE_USB_SPECIFICATION (0x0101)
|
|
|
|
#define FURI_HAL_BT_INFO_COUNTRY_CODE (0x00)
|
|
|
|
#define FURI_HAL_BT_HID_INFO_FLAG_REMOTE_WAKE_MSK (0x01)
|
|
|
|
#define FURI_HAL_BT_HID_INFO_FLAG_NORMALLY_CONNECTABLE_MSK (0x02)
|
|
|
|
|
2022-07-08 12:36:34 +00:00
|
|
|
#define FURI_HAL_BT_HID_KB_MAX_KEYS 6
|
|
|
|
#define FURI_HAL_BT_HID_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,
|
|
|
|
};
|
2021-12-08 11:28:01 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint8_t mods;
|
|
|
|
uint8_t reserved;
|
2022-07-08 12:36:34 +00:00
|
|
|
uint8_t key[FURI_HAL_BT_HID_KB_MAX_KEYS];
|
|
|
|
} __attribute__((__packed__)) FuriHalBtHidKbReport;
|
2021-12-08 11:28:01 +00:00
|
|
|
|
2021-12-15 17:39:06 +00:00
|
|
|
typedef struct {
|
2022-07-08 12:36:34 +00:00
|
|
|
uint8_t btn;
|
|
|
|
int8_t x;
|
|
|
|
int8_t y;
|
|
|
|
int8_t wheel;
|
|
|
|
} __attribute__((__packed__)) FuriHalBtHidMouseReport;
|
2021-12-08 11:28:01 +00:00
|
|
|
|
2022-07-08 12:36:34 +00:00
|
|
|
typedef struct {
|
|
|
|
uint16_t key[FURI_HAL_BT_HID_CONSUMER_MAX_KEYS];
|
|
|
|
} __attribute__((__packed__)) FuriHalBtHidConsumerReport;
|
|
|
|
|
|
|
|
// keyboard+mouse+consumer hid report
|
|
|
|
static const uint8_t furi_hal_bt_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(FURI_HAL_BT_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(FURI_HAL_BT_HID_CONSUMER_MAX_KEYS),
|
|
|
|
HID_REPORT_SIZE(16),
|
|
|
|
HID_INPUT(HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE),
|
|
|
|
HID_END_COLLECTION,
|
|
|
|
};
|
2021-12-08 11:28:01 +00:00
|
|
|
FuriHalBtHidKbReport* kb_report = NULL;
|
2022-07-08 12:36:34 +00:00
|
|
|
FuriHalBtHidMouseReport* mouse_report = NULL;
|
|
|
|
FuriHalBtHidConsumerReport* consumer_report = NULL;
|
2021-12-08 11:28:01 +00:00
|
|
|
|
|
|
|
void furi_hal_bt_hid_start() {
|
|
|
|
// Start device info
|
|
|
|
if(!dev_info_svc_is_started()) {
|
|
|
|
dev_info_svc_start();
|
|
|
|
}
|
|
|
|
// Start battery service
|
|
|
|
if(!battery_svc_is_started()) {
|
|
|
|
battery_svc_start();
|
|
|
|
}
|
|
|
|
// Start HID service
|
|
|
|
if(!hid_svc_is_started()) {
|
|
|
|
hid_svc_start();
|
|
|
|
}
|
|
|
|
// Configure HID Keyboard
|
2022-02-18 19:53:46 +00:00
|
|
|
kb_report = malloc(sizeof(FuriHalBtHidKbReport));
|
2022-07-08 12:36:34 +00:00
|
|
|
mouse_report = malloc(sizeof(FuriHalBtHidMouseReport));
|
|
|
|
consumer_report = malloc(sizeof(FuriHalBtHidConsumerReport));
|
2021-12-08 11:28:01 +00:00
|
|
|
// Configure Report Map characteristic
|
2022-01-05 16:10:18 +00:00
|
|
|
hid_svc_update_report_map(
|
|
|
|
furi_hal_bt_hid_report_map_data, sizeof(furi_hal_bt_hid_report_map_data));
|
2021-12-08 11:28:01 +00:00
|
|
|
// Configure HID Information characteristic
|
|
|
|
uint8_t hid_info_val[4] = {
|
|
|
|
FURI_HAL_BT_INFO_BASE_USB_SPECIFICATION & 0x00ff,
|
|
|
|
(FURI_HAL_BT_INFO_BASE_USB_SPECIFICATION & 0xff00) >> 8,
|
|
|
|
FURI_HAL_BT_INFO_COUNTRY_CODE,
|
2022-01-05 16:10:18 +00:00
|
|
|
FURI_HAL_BT_HID_INFO_FLAG_REMOTE_WAKE_MSK |
|
|
|
|
FURI_HAL_BT_HID_INFO_FLAG_NORMALLY_CONNECTABLE_MSK,
|
2021-12-08 11:28:01 +00:00
|
|
|
};
|
|
|
|
hid_svc_update_info(hid_info_val, sizeof(hid_info_val));
|
|
|
|
}
|
|
|
|
|
|
|
|
void furi_hal_bt_hid_stop() {
|
|
|
|
furi_assert(kb_report);
|
2022-07-08 12:36:34 +00:00
|
|
|
furi_assert(mouse_report);
|
|
|
|
furi_assert(consumer_report);
|
2021-12-08 11:28:01 +00:00
|
|
|
// Stop all services
|
|
|
|
if(dev_info_svc_is_started()) {
|
|
|
|
dev_info_svc_stop();
|
|
|
|
}
|
|
|
|
if(battery_svc_is_started()) {
|
|
|
|
battery_svc_stop();
|
|
|
|
}
|
|
|
|
if(hid_svc_is_started()) {
|
|
|
|
hid_svc_stop();
|
|
|
|
}
|
|
|
|
free(kb_report);
|
2022-07-08 12:36:34 +00:00
|
|
|
free(mouse_report);
|
|
|
|
free(consumer_report);
|
2021-12-08 11:28:01 +00:00
|
|
|
kb_report = NULL;
|
2022-07-08 12:36:34 +00:00
|
|
|
mouse_report = NULL;
|
|
|
|
consumer_report = NULL;
|
2021-12-08 11:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool furi_hal_bt_hid_kb_press(uint16_t button) {
|
|
|
|
furi_assert(kb_report);
|
2022-07-08 12:36:34 +00:00
|
|
|
for(uint8_t i = 0; i < FURI_HAL_BT_HID_KB_MAX_KEYS; i++) {
|
2022-01-05 16:10:18 +00:00
|
|
|
if(kb_report->key[i] == 0) {
|
2021-12-08 11:28:01 +00:00
|
|
|
kb_report->key[i] = button & 0xFF;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
kb_report->mods |= (button >> 8);
|
2022-07-08 12:36:34 +00:00
|
|
|
return hid_svc_update_input_report(
|
|
|
|
ReportNumberKeyboard, (uint8_t*)kb_report, sizeof(FuriHalBtHidKbReport));
|
2021-12-08 11:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool furi_hal_bt_hid_kb_release(uint16_t button) {
|
|
|
|
furi_assert(kb_report);
|
2022-07-08 12:36:34 +00:00
|
|
|
for(uint8_t i = 0; i < FURI_HAL_BT_HID_KB_MAX_KEYS; i++) {
|
2022-01-05 16:10:18 +00:00
|
|
|
if(kb_report->key[i] == (button & 0xFF)) {
|
2021-12-08 11:28:01 +00:00
|
|
|
kb_report->key[i] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
kb_report->mods &= ~(button >> 8);
|
2022-07-08 12:36:34 +00:00
|
|
|
return hid_svc_update_input_report(
|
|
|
|
ReportNumberKeyboard, (uint8_t*)kb_report, sizeof(FuriHalBtHidKbReport));
|
2021-12-08 11:28:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool furi_hal_bt_hid_kb_release_all() {
|
|
|
|
furi_assert(kb_report);
|
2022-07-08 12:36:34 +00:00
|
|
|
for(uint8_t i = 0; i < FURI_HAL_BT_HID_KB_MAX_KEYS; i++) {
|
|
|
|
kb_report->key[i] = 0;
|
|
|
|
}
|
|
|
|
kb_report->mods = 0;
|
|
|
|
return hid_svc_update_input_report(
|
|
|
|
ReportNumberKeyboard, (uint8_t*)kb_report, sizeof(FuriHalBtHidKbReport));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool furi_hal_bt_hid_consumer_key_press(uint16_t button) {
|
|
|
|
furi_assert(consumer_report);
|
2022-12-26 12:13:30 +00:00
|
|
|
for(uint8_t i = 0; i < FURI_HAL_BT_HID_CONSUMER_MAX_KEYS; i++) { //-V1008
|
2022-07-08 12:36:34 +00:00
|
|
|
if(consumer_report->key[i] == 0) {
|
|
|
|
consumer_report->key[i] = button;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hid_svc_update_input_report(
|
|
|
|
ReportNumberConsumer, (uint8_t*)consumer_report, sizeof(FuriHalBtHidConsumerReport));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool furi_hal_bt_hid_consumer_key_release(uint16_t button) {
|
|
|
|
furi_assert(consumer_report);
|
2022-12-26 12:13:30 +00:00
|
|
|
for(uint8_t i = 0; i < FURI_HAL_BT_HID_CONSUMER_MAX_KEYS; i++) { //-V1008
|
2022-07-08 12:36:34 +00:00
|
|
|
if(consumer_report->key[i] == button) {
|
|
|
|
consumer_report->key[i] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hid_svc_update_input_report(
|
|
|
|
ReportNumberConsumer, (uint8_t*)consumer_report, sizeof(FuriHalBtHidConsumerReport));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool furi_hal_bt_hid_consumer_key_release_all() {
|
|
|
|
furi_assert(consumer_report);
|
2022-12-26 12:13:30 +00:00
|
|
|
for(uint8_t i = 0; i < FURI_HAL_BT_HID_CONSUMER_MAX_KEYS; i++) { //-V1008
|
2022-07-08 12:36:34 +00:00
|
|
|
consumer_report->key[i] = 0;
|
|
|
|
}
|
|
|
|
return hid_svc_update_input_report(
|
|
|
|
ReportNumberConsumer, (uint8_t*)consumer_report, sizeof(FuriHalBtHidConsumerReport));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool furi_hal_bt_hid_mouse_move(int8_t dx, int8_t dy) {
|
|
|
|
furi_assert(mouse_report);
|
|
|
|
mouse_report->x = dx;
|
|
|
|
mouse_report->y = dy;
|
|
|
|
bool state = hid_svc_update_input_report(
|
|
|
|
ReportNumberMouse, (uint8_t*)mouse_report, sizeof(FuriHalBtHidMouseReport));
|
|
|
|
mouse_report->x = 0;
|
|
|
|
mouse_report->y = 0;
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool furi_hal_bt_hid_mouse_press(uint8_t button) {
|
|
|
|
furi_assert(mouse_report);
|
|
|
|
mouse_report->btn |= button;
|
|
|
|
return hid_svc_update_input_report(
|
|
|
|
ReportNumberMouse, (uint8_t*)mouse_report, sizeof(FuriHalBtHidMouseReport));
|
2021-12-08 11:28:01 +00:00
|
|
|
}
|
2021-12-15 17:39:06 +00:00
|
|
|
|
2022-07-08 12:36:34 +00:00
|
|
|
bool furi_hal_bt_hid_mouse_release(uint8_t button) {
|
|
|
|
furi_assert(mouse_report);
|
|
|
|
mouse_report->btn &= ~button;
|
|
|
|
return hid_svc_update_input_report(
|
|
|
|
ReportNumberMouse, (uint8_t*)mouse_report, sizeof(FuriHalBtHidMouseReport));
|
2021-12-15 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 12:36:34 +00:00
|
|
|
bool furi_hal_bt_hid_mouse_release_all() {
|
|
|
|
furi_assert(mouse_report);
|
|
|
|
mouse_report->btn = 0;
|
|
|
|
return hid_svc_update_input_report(
|
|
|
|
ReportNumberMouse, (uint8_t*)mouse_report, sizeof(FuriHalBtHidMouseReport));
|
2021-12-15 17:39:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 12:36:34 +00:00
|
|
|
bool furi_hal_bt_hid_mouse_scroll(int8_t delta) {
|
|
|
|
furi_assert(mouse_report);
|
|
|
|
mouse_report->wheel = delta;
|
|
|
|
bool state = hid_svc_update_input_report(
|
|
|
|
ReportNumberMouse, (uint8_t*)mouse_report, sizeof(FuriHalBtHidMouseReport));
|
|
|
|
mouse_report->wheel = 0;
|
|
|
|
return state;
|
2021-12-15 17:39:06 +00:00
|
|
|
}
|