Skip to content

Commit

Permalink
Testing hid without LED descr
Browse files Browse the repository at this point in the history
  • Loading branch information
xMasterX committed Sep 19, 2023
1 parent 70b8823 commit f7f9250
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 84 deletions.
92 changes: 14 additions & 78 deletions firmware/targets/f7/ble_glue/services/hid_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ typedef enum {
HidSvcGattCharacteristicReportMap,
HidSvcGattCharacteristicInfo,
HidSvcGattCharacteristicCtrlPoint,
HidSvcGattCharacteristicLed,
HidSvcGattCharacteristicCount,
} HidSvcGattCharacteristicId;

Expand Down Expand Up @@ -54,22 +53,6 @@ static bool
return false;
}

// LED Descriptor params for BadBT

static uint8_t led_desc_context_buf[2] = {HID_SVC_REPORT_COUNT + 1, 2};

static FlipperGattCharacteristicDescriptorParams hid_svc_char_descr_led = {
.uuid_type = UUID_TYPE_16,
.uuid.Char_UUID_16 = REPORT_REFERENCE_DESCRIPTOR_UUID,
.max_length = HID_SVC_REPORT_REF_LEN,
.data_callback.fn = hid_svc_char_desc_data_callback,
.data_callback.context = led_desc_context_buf,
.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 FlipperGattCharacteristicParams hid_svc_chars[HidSvcGattCharacteristicCount] = {
[HidSvcGattCharacteristicProtocolMode] =
{.name = "Protocol Mode",
Expand Down Expand Up @@ -113,21 +96,6 @@ static const FlipperGattCharacteristicParams hid_svc_chars[HidSvcGattCharacteris
.security_permissions = ATTR_PERMISSION_NONE,
.gatt_evt_mask = GATT_NOTIFY_ATTRIBUTE_WRITE,
.is_variable = CHAR_VALUE_LEN_CONSTANT},
[HidSvcGattCharacteristicLed] =
{
.name =
"HID LED State", // LED Characteristic and descriptor for BadBT to get numlock state for altchars
.data_prop_type = FlipperGattCharacteristicDataFixed,
.data.fixed.length = 1,
.uuid.Char_UUID_16 = REPORT_CHAR_UUID,
.uuid_type = UUID_TYPE_16,
.char_properties = CHAR_PROP_READ | CHAR_PROP_WRITE_WITHOUT_RESP | CHAR_PROP_WRITE,
.security_permissions = ATTR_PERMISSION_NONE,
.gatt_evt_mask = GATT_NOTIFY_ATTRIBUTE_WRITE |
GATT_NOTIFY_WRITE_REQ_AND_WAIT_FOR_APPL_RESP,
.is_variable = CHAR_VALUE_LEN_CONSTANT,
.descriptor_params = &hid_svc_char_descr_led,
},
};

static const FlipperGattCharacteristicDescriptorParams hid_svc_char_descr_template = {
Expand Down Expand Up @@ -160,9 +128,6 @@ typedef struct {
FlipperGattCharacteristicInstance input_report_chars[HID_SVC_INPUT_REPORT_COUNT];
FlipperGattCharacteristicInstance output_report_chars[HID_SVC_OUTPUT_REPORT_COUNT];
FlipperGattCharacteristicInstance feature_report_chars[HID_SVC_FEATURE_REPORT_COUNT];
// led state
HidLedStateEventCallback led_state_event_callback;
void* led_state_ctx;
} HIDSvc;

static HIDSvc* hid_svc = NULL;
Expand All @@ -179,34 +144,6 @@ static SVCCTL_EvtAckStatus_t hid_svc_event_handler(void* event) {
} else if(blecore_evt->ecode == ACI_GATT_SERVER_CONFIRMATION_VSEVT_CODE) {
// Process notification confirmation
ret = SVCCTL_EvtAckFlowEnable;
} else if(blecore_evt->ecode == ACI_GATT_WRITE_PERMIT_REQ_VSEVT_CODE) {
// LED Characteristic and descriptor for BadBT to get numlock state for altchars
//
// Process write request
aci_gatt_write_permit_req_event_rp0* req =
(aci_gatt_write_permit_req_event_rp0*)blecore_evt->data;

furi_check(hid_svc->led_state_event_callback && hid_svc->led_state_ctx);

// this check is likely to be incorrect, it will actually work in our case
// but we need to investigate gatt api to see what is the rules
// that specify attibute handle value from char handle (or the reverse)
if(req->Attribute_Handle == (hid_svc->chars[HidSvcGattCharacteristicLed].handle + 1)) {
hid_svc->led_state_event_callback(req->Data[0], hid_svc->led_state_ctx);
aci_gatt_write_resp(
req->Connection_Handle,
req->Attribute_Handle,
0x00, /* write_status = 0 (no error))*/
0x00, /* err_code */
req->Data_Length,
req->Data);
aci_gatt_write_char_value(
req->Connection_Handle,
hid_svc->chars[HidSvcGattCharacteristicLed].handle,
req->Data_Length,
req->Data);
ret = SVCCTL_EvtAckFlowEnable;
}
}
}
return ret;
Expand All @@ -227,17 +164,19 @@ void hid_svc_start() {
PRIMARY_SERVICE,
2 + /* protocol mode */
(4 * HID_SVC_INPUT_REPORT_COUNT) + (3 * HID_SVC_OUTPUT_REPORT_COUNT) +
(3 * HID_SVC_FEATURE_REPORT_COUNT) + 1 + 2 + 2 + 2 +
4, /* Service + Report Map + HID Information + HID Control Point + LED state */
(3 * HID_SVC_FEATURE_REPORT_COUNT) + 1 + 2 + 2 +
2, /* Service + Report Map + HID Information + HID Control Point */
&hid_svc->svc_handle);
if(status) {
FURI_LOG_E(TAG, "Failed to add HID service: %d", status);
}

for(size_t i = 0; i < HidSvcGattCharacteristicCount; i++) {
flipper_gatt_characteristic_init(
hid_svc->svc_handle, &hid_svc_chars[i], &hid_svc->chars[i]);
}
// Maintain previously defined characteristic order
flipper_gatt_characteristic_init(
hid_svc->svc_handle,
&hid_svc_chars[HidSvcGattCharacteristicProtocolMode],
&hid_svc->chars[HidSvcGattCharacteristicProtocolMode]);

uint8_t protocol_mode = 1;
flipper_gatt_characteristic_update(
hid_svc->svc_handle,
Expand Down Expand Up @@ -279,6 +218,12 @@ void hid_svc_start() {
&hid_report_chars[report_type_idx].chars[report_idx]);
}
}

// Setup remaining characteristics
for(size_t i = HidSvcGattCharacteristicReportMap; i < HidSvcGattCharacteristicCount; i++) {
flipper_gatt_characteristic_init(
hid_svc->svc_handle, &hid_svc_chars[i], &hid_svc->chars[i]);
}
}

bool hid_svc_update_report_map(const uint8_t* data, uint16_t len) {
Expand Down Expand Up @@ -314,15 +259,6 @@ bool hid_svc_update_info(uint8_t* data) {
hid_svc->svc_handle, &hid_svc->chars[HidSvcGattCharacteristicInfo], &data);
}

void hid_svc_register_led_state_callback(HidLedStateEventCallback callback, void* context) {
furi_assert(hid_svc);
furi_assert(callback);
furi_assert(context);

hid_svc->led_state_event_callback = callback;
hid_svc->led_state_ctx = context;
}

bool hid_svc_is_started() {
return hid_svc != NULL;
}
Expand Down
4 changes: 0 additions & 4 deletions firmware/targets/f7/ble_glue/services/hid_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
#define HID_SVC_REPORT_COUNT \
(HID_SVC_INPUT_REPORT_COUNT + HID_SVC_OUTPUT_REPORT_COUNT + HID_SVC_FEATURE_REPORT_COUNT)

typedef uint16_t (*HidLedStateEventCallback)(uint8_t state, void* ctx);

void hid_svc_start();

void hid_svc_stop();
Expand All @@ -29,5 +27,3 @@ bool hid_svc_update_input_report(uint8_t input_report_num, uint8_t* data, uint16

// Expects data to be of length HID_SVC_INFO_LEN (4 bytes)
bool hid_svc_update_info(uint8_t* data);

void hid_svc_register_led_state_callback(HidLedStateEventCallback callback, void* context);
4 changes: 2 additions & 2 deletions firmware/targets/f7/furi_hal/furi_hal_bt_hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ void furi_hal_bt_hid_start() {
hid_svc_start();
}
// Configure HID Keyboard
hid_svc_register_led_state_callback(furi_hal_bt_hid_led_state_cb, &hid_host_led_state);
//hid_svc_register_led_state_callback(furi_hal_bt_hid_led_state_cb, &hid_host_led_state);

kb_report = malloc(sizeof(FuriHalBtHidKbReport));
mouse_report = malloc(sizeof(FuriHalBtHidMouseReport));
Expand All @@ -228,7 +228,7 @@ void furi_hal_bt_hid_stop() {
furi_assert(mouse_report);
furi_assert(consumer_report);

hid_svc_register_led_state_callback(NULL, NULL);
//hid_svc_register_led_state_callback(NULL, NULL);
// Stop all services
if(dev_info_svc_is_started()) {
dev_info_svc_stop();
Expand Down

0 comments on commit f7f9250

Please sign in to comment.