diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 793f386dbe3..c9b7dadee38 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -37,6 +37,8 @@ target_sources(app PRIVATE src/behaviors/behavior_reset.c) target_sources_ifdef(CONFIG_ZMK_EXT_POWER app PRIVATE src/behaviors/behavior_ext_power.c) if ((NOT CONFIG_ZMK_SPLIT) OR CONFIG_ZMK_SPLIT_ROLE_CENTRAL) target_sources(app PRIVATE src/hid.c) + target_sources_ifdef(CONFIG_ZMK_USB app PRIVATE src/usb_hid.c) + target_sources(app PRIVATE src/behaviors/behavior_key_press.c) target_sources_ifdef(CONFIG_ZMK_BEHAVIOR_KEY_TOGGLE app PRIVATE src/behaviors/behavior_key_toggle.c) target_sources(app PRIVATE src/behaviors/behavior_hold_tap.c) @@ -65,6 +67,7 @@ if ((NOT CONFIG_ZMK_SPLIT) OR CONFIG_ZMK_SPLIT_ROLE_CENTRAL) target_sources(app PRIVATE src/events/layer_state_changed.c) target_sources(app PRIVATE src/events/modifiers_state_changed.c) target_sources(app PRIVATE src/events/keycode_state_changed.c) + target_sources(app PRIVATE src/hid_indicators.c) if (CONFIG_ZMK_BLE) target_sources(app PRIVATE src/events/ble_active_profile_changed.c) @@ -80,11 +83,12 @@ target_sources_ifdef(CONFIG_ZMK_BACKLIGHT app PRIVATE src/behaviors/behavior_bac target_sources_ifdef(CONFIG_ZMK_BATTERY_REPORTING app PRIVATE src/events/battery_state_changed.c) target_sources_ifdef(CONFIG_ZMK_BATTERY_REPORTING app PRIVATE src/battery.c) +target_sources(app PRIVATE src/events/hid_indicators_changed.c) + target_sources_ifdef(CONFIG_ZMK_SPLIT app PRIVATE src/events/split_peripheral_status_changed.c) add_subdirectory(src/split) target_sources_ifdef(CONFIG_USB_DEVICE_STACK app PRIVATE src/usb.c) -target_sources_ifdef(CONFIG_ZMK_USB app PRIVATE src/usb_hid.c) target_sources_ifdef(CONFIG_ZMK_RGB_UNDERGLOW app PRIVATE src/rgb_underglow.c) target_sources_ifdef(CONFIG_ZMK_BACKLIGHT app PRIVATE src/backlight.c) target_sources(app PRIVATE src/workqueue.c) diff --git a/app/include/zmk/ble.h b/app/include/zmk/ble.h index 435fde49654..b89cdfcba0a 100644 --- a/app/include/zmk/ble.h +++ b/app/include/zmk/ble.h @@ -26,6 +26,7 @@ int zmk_ble_prof_prev(); int zmk_ble_prof_select(uint8_t index); int zmk_ble_active_profile_index(); +int zmk_ble_profile_index(const bt_addr_le_t *addr); bt_addr_le_t *zmk_ble_active_profile_addr(); bool zmk_ble_active_profile_is_open(); bool zmk_ble_active_profile_is_connected(); diff --git a/app/include/zmk/events/hid_indicators_changed.h b/app/include/zmk/events/hid_indicators_changed.h new file mode 100644 index 00000000000..2c3ba088269 --- /dev/null +++ b/app/include/zmk/events/hid_indicators_changed.h @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2022 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include +#include + +struct zmk_hid_indicators_changed { + zmk_hid_indicators indicators; +}; + +ZMK_EVENT_DECLARE(zmk_hid_indicators_changed); diff --git a/app/include/zmk/hid.h b/app/include/zmk/hid.h index ab42adaa13e..9e02ad382b0 100644 --- a/app/include/zmk/hid.h +++ b/app/include/zmk/hid.h @@ -17,11 +17,15 @@ #define COLLECTION_REPORT 0x03 +#define HID_REPORT_ID_KEYBOARD 0x01 +#define HID_REPORT_ID_LEDS 0x01 +#define HID_REPORT_ID_CONSUMER 0x02 + static const uint8_t zmk_hid_report_desc[] = { HID_USAGE_PAGE(HID_USAGE_GEN_DESKTOP), HID_USAGE(HID_USAGE_GD_KEYBOARD), HID_COLLECTION(HID_COLLECTION_APPLICATION), - HID_REPORT_ID(0x01), + HID_REPORT_ID(HID_REPORT_ID_KEYBOARD), HID_USAGE_PAGE(HID_USAGE_KEY), HID_USAGE_MIN8(HID_USAGE_KEY_KEYBOARD_LEFTCONTROL), HID_USAGE_MAX8(HID_USAGE_KEY_KEYBOARD_RIGHT_GUI), @@ -39,6 +43,20 @@ static const uint8_t zmk_hid_report_desc[] = { /* INPUT (Cnst,Var,Abs) */ HID_INPUT(0x03), + HID_USAGE_PAGE(HID_USAGE_LED), + HID_USAGE_MIN8(HID_USAGE_LED_NUM_LOCK), + HID_USAGE_MAX8(HID_USAGE_LED_KANA), + HID_REPORT_SIZE(0x01), + HID_REPORT_COUNT(0x05), + /* OUTPUT (Data,Var,Abs) */ + HID_OUTPUT(0x02), + + HID_USAGE_PAGE(HID_USAGE_LED), + HID_REPORT_SIZE(0x03), + HID_REPORT_COUNT(0x01), + /* OUTPUT (Cnst,Var,Abs) */ + HID_OUTPUT(0x03), + HID_USAGE_PAGE(HID_USAGE_KEY), #if IS_ENABLED(CONFIG_ZMK_HID_REPORT_TYPE_NKRO) @@ -67,7 +85,7 @@ static const uint8_t zmk_hid_report_desc[] = { HID_USAGE_PAGE(HID_USAGE_CONSUMER), HID_USAGE(HID_USAGE_CONSUMER_CONSUMER_CONTROL), HID_COLLECTION(HID_COLLECTION_APPLICATION), - HID_REPORT_ID(0x02), + HID_REPORT_ID(HID_REPORT_ID_CONSUMER), HID_USAGE_PAGE(HID_USAGE_CONSUMER), #if IS_ENABLED(CONFIG_ZMK_HID_CONSUMER_REPORT_USAGES_BASIC) @@ -113,6 +131,15 @@ struct zmk_hid_keyboard_report { struct zmk_hid_keyboard_report_body body; } __packed; +struct zmk_hid_led_report_body { + uint8_t leds; +} __packed; + +struct zmk_hid_led_report { + uint8_t report_id; + struct zmk_hid_led_report_body body; +} __packed; + struct zmk_hid_consumer_report_body { #if IS_ENABLED(CONFIG_ZMK_HID_CONSUMER_REPORT_USAGES_BASIC) uint8_t keys[CONFIG_ZMK_HID_CONSUMER_REPORT_SIZE]; diff --git a/app/include/zmk/hid_indicators.h b/app/include/zmk/hid_indicators.h new file mode 100644 index 00000000000..69cee13dba2 --- /dev/null +++ b/app/include/zmk/hid_indicators.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2022 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include +#include +#include + +zmk_hid_indicators zmk_hid_indicators_get_current_profile(void); +zmk_hid_indicators zmk_hid_indicators_get_profile(struct zmk_endpoint_instance endpoint); +void zmk_hid_indicators_set_profile(zmk_hid_indicators indicators, + struct zmk_endpoint_instance endpoint); + +void zmk_hid_indicators_process_report(struct zmk_hid_led_report_body *report, + struct zmk_endpoint_instance endpoint); diff --git a/app/include/zmk/hid_indicators_types.h b/app/include/zmk/hid_indicators_types.h new file mode 100644 index 00000000000..aa1504f6bdb --- /dev/null +++ b/app/include/zmk/hid_indicators_types.h @@ -0,0 +1,9 @@ +/* + * Copyright (c) 2022 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +typedef uint8_t zmk_hid_indicators; diff --git a/app/include/zmk/split/bluetooth/central.h b/app/include/zmk/split/bluetooth/central.h index 443d9b1b489..3cce6471770 100644 --- a/app/include/zmk/split/bluetooth/central.h +++ b/app/include/zmk/split/bluetooth/central.h @@ -3,6 +3,9 @@ #include #include +#include int zmk_split_bt_invoke_behavior(uint8_t source, struct zmk_behavior_binding *binding, - struct zmk_behavior_binding_event event, bool state); \ No newline at end of file + struct zmk_behavior_binding_event event, bool state); + +int zmk_split_bt_update_hid_indicator(zmk_hid_indicators indicators); diff --git a/app/src/ble.c b/app/src/ble.c index 483bc9d79c1..7c4d5df0489 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -224,6 +224,15 @@ int zmk_ble_clear_bonds() { int zmk_ble_active_profile_index() { return active_profile; } +int zmk_ble_profile_index(const bt_addr_le_t *addr) { + for (int i = 0; i < ZMK_BLE_PROFILE_COUNT; i++) { + if (bt_addr_le_cmp(addr, &profiles[i].peer) == 0) { + return i; + } + } + return -ENODEV; +} + #if IS_ENABLED(CONFIG_SETTINGS) static void ble_save_profile_work(struct k_work *work) { settings_save_one("ble/active_profile", &active_profile, sizeof(active_profile)); diff --git a/app/src/endpoints.c b/app/src/endpoints.c index 138e790fe72..db6e210811a 100644 --- a/app/src/endpoints.c +++ b/app/src/endpoints.c @@ -116,9 +116,7 @@ int zmk_endpoints_toggle_transport(void) { return zmk_endpoints_select_transport(new_transport); } -struct zmk_endpoint_instance zmk_endpoints_selected(void) { - return current_instance; -} +struct zmk_endpoint_instance zmk_endpoints_selected(void) { return current_instance; } static int send_keyboard_report(void) { struct zmk_hid_keyboard_report *keyboard_report = zmk_hid_get_keyboard_report(); diff --git a/app/src/events/hid_indicators_changed.c b/app/src/events/hid_indicators_changed.c new file mode 100644 index 00000000000..ded368354d0 --- /dev/null +++ b/app/src/events/hid_indicators_changed.c @@ -0,0 +1,10 @@ +/* + * Copyright (c) 2022 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include +#include + +ZMK_EVENT_IMPL(zmk_hid_indicators_changed); diff --git a/app/src/hid.c b/app/src/hid.c index 2a6b5d39da2..77398625402 100644 --- a/app/src/hid.c +++ b/app/src/hid.c @@ -12,9 +12,10 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include static struct zmk_hid_keyboard_report keyboard_report = { - .report_id = 1, .body = {.modifiers = 0, ._reserved = 0, .keys = {0}}}; + .report_id = HID_REPORT_ID_KEYBOARD, .body = {.modifiers = 0, ._reserved = 0, .keys = {0}}}; -static struct zmk_hid_consumer_report consumer_report = {.report_id = 2, .body = {.keys = {0}}}; +static struct zmk_hid_consumer_report consumer_report = {.report_id = HID_REPORT_ID_CONSUMER, + .body = {.keys = {0}}}; // Keep track of how often a modifier was pressed. // Only release the modifier if the count is 0. diff --git a/app/src/hid_indicators.c b/app/src/hid_indicators.c new file mode 100644 index 00000000000..ff854479c4c --- /dev/null +++ b/app/src/hid_indicators.c @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2022 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include +#include + +#include +#include +#include +#include +#include +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +static zmk_hid_indicators hid_indicators[ZMK_ENDPOINT_COUNT]; + +zmk_hid_indicators zmk_hid_indicators_get_current_profile(void) { + return zmk_hid_indicators_get_profile(zmk_endpoints_selected()); +} + +zmk_hid_indicators zmk_hid_indicators_get_profile(struct zmk_endpoint_instance endpoint) { + int profile = zmk_endpoint_instance_to_index(endpoint); + return hid_indicators[profile]; +} + +static void raise_led_changed_event(struct k_work *_work) { + zmk_hid_indicators indicators = zmk_hid_indicators_get_current_profile(); + + ZMK_EVENT_RAISE(new_zmk_hid_indicators_changed( + (struct zmk_hid_indicators_changed){.indicators = indicators})); + +#if IS_ENABLED(CONFIG_ZMK_SPLIT) && IS_ENABLED(CONFIG_ZMK_BLE) + zmk_split_bt_update_hid_indicator(indicators); +#endif +} + +static K_WORK_DEFINE(led_changed_work, raise_led_changed_event); + +void zmk_hid_indicators_set_profile(zmk_hid_indicators indicators, + struct zmk_endpoint_instance endpoint) { + int profile = zmk_endpoint_instance_to_index(endpoint); + + // This write is not happening on the main thread. To prevent potential data races, every + // operation involving hid_indicators must be atomic. Currently, each function either reads + // or writes only one entry at a time, so it is safe to do these operations without a lock. + hid_indicators[profile] = indicators; + + k_work_submit(&led_changed_work); +} + +void zmk_hid_indicators_process_report(struct zmk_hid_led_report_body *report, + struct zmk_endpoint_instance endpoint) { + uint8_t indicators = report->leds; + zmk_hid_indicators_set_profile(indicators, endpoint); + + LOG_DBG("Update HID indicators: endpoint=%d, indicators=%x", endpoint.transport, indicators); +} + +static int profile_listener(const zmk_event_t *eh) { + raise_led_changed_event(NULL); + return 0; +} + +static ZMK_LISTENER(profile_listener, profile_listener); +static ZMK_SUBSCRIPTION(profile_listener, zmk_endpoint_changed); diff --git a/app/src/hog.c b/app/src/hog.c index 930714b092a..7a5f949cdb7 100644 --- a/app/src/hog.c +++ b/app/src/hog.c @@ -15,8 +15,10 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include #include +#include #include #include +#include enum { HIDS_REMOTE_WAKE = BIT(0), @@ -47,12 +49,17 @@ enum { }; static struct hids_report input = { - .id = 0x01, + .id = HID_REPORT_ID_KEYBOARD, .type = HIDS_INPUT, }; +static struct hids_report led_indicators = { + .id = HID_REPORT_ID_LEDS, + .type = HIDS_OUTPUT, +}; + static struct hids_report consumer_input = { - .id = 0x02, + .id = HID_REPORT_ID_CONSUMER, .type = HIDS_INPUT, }; @@ -85,6 +92,31 @@ static ssize_t read_hids_input_report(struct bt_conn *conn, const struct bt_gatt sizeof(struct zmk_hid_keyboard_report_body)); } +static ssize_t write_hids_leds_report(struct bt_conn *conn, const struct bt_gatt_attr *attr, + const void *buf, uint16_t len, uint16_t offset, + uint8_t flags) { + if (offset != 0) { + return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET); + } + if (len != sizeof(struct zmk_hid_led_report_body)) { + return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN); + } + + struct zmk_hid_led_report_body *report = (struct zmk_hid_led_report_body *)buf; + int profile = zmk_ble_profile_index(bt_conn_get_dst(conn)); + if (profile < 0) { + return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY); + } + + struct zmk_endpoint_instance endpoint = {.transport = ZMK_TRANSPORT_BLE, + .ble = { + .profile_index = profile, + }}; + zmk_hid_indicators_process_report(report, endpoint); + + return len; +} + static ssize_t read_hids_consumer_input_report(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset) { @@ -134,6 +166,14 @@ BT_GATT_SERVICE_DEFINE( BT_GATT_CCC(input_ccc_changed, BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT), BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT_REF, BT_GATT_PERM_READ_ENCRYPT, read_hids_report_ref, NULL, &input), + + BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT, + BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE | BT_GATT_CHRC_WRITE_WITHOUT_RESP, + BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT, NULL, + write_hids_leds_report, NULL), + BT_GATT_DESCRIPTOR(BT_UUID_HIDS_REPORT_REF, BT_GATT_PERM_READ_ENCRYPT, read_hids_report_ref, + NULL, &led_indicators), + BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_REPORT, BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY, BT_GATT_PERM_READ_ENCRYPT, read_hids_consumer_input_report, NULL, NULL), BT_GATT_CCC(input_ccc_changed, BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT), @@ -224,7 +264,7 @@ void send_consumer_report_callback(struct k_work *work) { } struct bt_gatt_notify_params notify_params = { - .attr = &hog_svc.attrs[10], + .attr = &hog_svc.attrs[12], .data = &report, .len = sizeof(report), }; diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c index 860e89a5f3e..2a8a9f51234 100644 --- a/app/src/split/bluetooth/central.c +++ b/app/src/split/bluetooth/central.c @@ -27,6 +27,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include #include #include +#include static int start_scanning(void); @@ -46,6 +47,7 @@ struct peripheral_slot { struct bt_gatt_subscribe_params sensor_subscribe_params; struct bt_gatt_discover_params sub_discover_params; uint16_t run_behavior_handle; + uint16_t update_hid_indicators; uint8_t position_state[POSITION_STATE_DATA_LEN]; uint8_t changed_positions[POSITION_STATE_DATA_LEN]; }; @@ -131,6 +133,7 @@ int release_peripheral_slot(int index) { // Clean up previously discovered handles; slot->subscribe_params.value_handle = 0; slot->run_behavior_handle = 0; + slot->update_hid_indicators = 0; return 0; } @@ -329,13 +332,21 @@ static uint8_t split_central_chrc_discovery_func(struct bt_conn *conn, slot->discover_params.uuid = NULL; slot->discover_params.start_handle = attr->handle + 2; slot->run_behavior_handle = bt_gatt_attr_value_handle(attr); + } else if (!bt_uuid_cmp(((struct bt_gatt_chrc *)attr->user_data)->uuid, + BT_UUID_DECLARE_128(ZMK_SPLIT_BT_UPDATE_HID_INDICATORS_UUID))) { + LOG_DBG("Found update HID indicators handle"); + slot->update_hid_indicators = bt_gatt_attr_value_handle(attr); } - bool subscribed = slot->run_behavior_handle && slot->subscribe_params.value_handle; + bool subscribed = slot->run_behavior_handle && slot->subscribe_params.value_handle && + slot->sensor_subscribe_params.value_handle; #if ZMK_KEYMAP_HAS_SENSORS subscribed = subscribed && slot->sensor_subscribe_params.value_handle; #endif /* ZMK_KEYMAP_HAS_SENSORS */ + bool subscribed = (slot->run_behavior_handle && slot->subscribe_params.value_handle && + slot->update_hid_indicators); + return subscribed ? BT_GATT_ITER_STOP : BT_GATT_ITER_CONTINUE; } @@ -685,6 +696,32 @@ int zmk_split_bt_invoke_behavior(uint8_t source, struct zmk_behavior_binding *bi return split_bt_invoke_behavior_payload(wrapper); } +static zmk_hid_indicators hid_indicators = 0; + +static void split_central_update_indicators_callback(struct k_work *work) { + zmk_hid_indicators indicators = hid_indicators; + for (int i = 0; i < ZMK_SPLIT_BLE_PERIPHERAL_COUNT; i++) { + if (peripherals[i].state != PERIPHERAL_SLOT_STATE_CONNECTED) { + continue; + } + + int err = bt_gatt_write_without_response(peripherals[i].conn, + peripherals[i].update_hid_indicators, &indicators, + sizeof(indicators), true); + + if (err) { + LOG_ERR("Failed to write HID indicator characteristic (err %d)", err); + } + } +} + +static K_WORK_DEFINE(split_central_update_indicators, split_central_update_indicators_callback); + +int zmk_split_bt_update_hid_indicator(zmk_hid_indicators indicators) { + hid_indicators = indicators; + return k_work_submit_to_queue(&split_central_split_run_q, &split_central_update_indicators); +} + int zmk_split_bt_central_init(const struct device *_arg) { k_work_queue_start(&split_central_split_run_q, split_central_split_run_q_stack, K_THREAD_STACK_SIZEOF(split_central_split_run_q_stack), diff --git a/app/src/split/bluetooth/service.c b/app/src/split/bluetooth/service.c index 620df53e11c..0fe28b444ed 100644 --- a/app/src/split/bluetooth/service.c +++ b/app/src/split/bluetooth/service.c @@ -23,6 +23,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include #include #include +#include #if ZMK_KEYMAP_HAS_SENSORS static struct sensor_event last_sensor_event; @@ -105,6 +106,29 @@ static void split_svc_pos_state_ccc(const struct bt_gatt_attr *attr, uint16_t va LOG_DBG("value %d", value); } +static zmk_hid_indicators hid_indicators = 0; + +static void split_svc_update_indicators_callback(struct k_work *work) { + ZMK_EVENT_RAISE(new_zmk_hid_indicators_changed( + (struct zmk_hid_indicators_changed){.indicators = hid_indicators})); +} + +static K_WORK_DEFINE(split_svc_update_indicators_work, split_svc_update_indicators_callback); + +static ssize_t split_svc_update_indicators(struct bt_conn *conn, const struct bt_gatt_attr *attr, + const void *buf, uint16_t len, uint16_t offset, + uint8_t flags) { + if (offset + len > sizeof(zmk_hid_indicators)) { + return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET); + } + + memcpy((uint8_t *)&hid_indicators + offset, buf, len); + + k_work_submit(&split_svc_update_indicators_work); + + return len; +} + BT_GATT_SERVICE_DEFINE( split_svc, BT_GATT_PRIMARY_SERVICE(BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID)), BT_GATT_CHARACTERISTIC(BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID), @@ -114,6 +138,9 @@ BT_GATT_SERVICE_DEFINE( BT_GATT_CHARACTERISTIC(BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_RUN_BEHAVIOR_UUID), BT_GATT_CHRC_WRITE_WITHOUT_RESP, BT_GATT_PERM_WRITE_ENCRYPT, NULL, split_svc_run_behavior, &behavior_run_payload), + BT_GATT_CHARACTERISTIC(BT_UUID_DECLARE_128(ZMK_SPLIT_BT_UPDATE_HID_INDICATORS_UUID), + BT_GATT_CHRC_WRITE_WITHOUT_RESP, BT_GATT_PERM_WRITE_ENCRYPT, NULL, + split_svc_update_indicators, NULL), BT_GATT_DESCRIPTOR(BT_UUID_NUM_OF_DIGITALS, BT_GATT_PERM_READ, split_svc_num_of_positions, NULL, &num_of_positions), #if ZMK_KEYMAP_HAS_SENSORS diff --git a/app/src/usb_hid.c b/app/src/usb_hid.c index f46c70a0a92..3855311d792 100644 --- a/app/src/usb_hid.c +++ b/app/src/usb_hid.c @@ -13,6 +13,7 @@ #include #include #include +#include #include LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); @@ -23,8 +24,45 @@ static K_SEM_DEFINE(hid_sem, 1, 1); static void in_ready_cb(const struct device *dev) { k_sem_give(&hid_sem); } +#define HID_GET_REPORT_TYPE_MASK 0xff00 +#define HID_GET_REPORT_ID_MASK 0x00ff + +#define HID_REPORT_TYPE_INPUT 0x100 +#define HID_REPORT_TYPE_OUTPUT 0x200 +#define HID_REPORT_TYPE_FEATURE 0x300 + +static int set_report_cb(const struct device *dev, struct usb_setup_packet *setup, int32_t *len, + uint8_t **data) { + if ((setup->wValue & HID_GET_REPORT_TYPE_MASK) != HID_REPORT_TYPE_OUTPUT) { + LOG_ERR("Unsupported report type %d requested", + (setup->wValue & HID_GET_REPORT_TYPE_MASK) >> 8); + return -ENOTSUP; + } + + switch (setup->wValue & HID_GET_REPORT_ID_MASK) { + case HID_REPORT_ID_LEDS: + if (*len != sizeof(struct zmk_hid_led_report)) { + LOG_ERR("LED set report is malformed: length=%d", *len); + return -EINVAL; + } else { + struct zmk_hid_led_report *report = (struct zmk_hid_led_report *)*data; + struct zmk_endpoint_instance endpoint = { + .transport = ZMK_TRANSPORT_USB, + }; + zmk_hid_indicators_process_report(&report->body, endpoint); + } + break; + default: + LOG_ERR("Invalid report ID %d requested", setup->wValue & HID_GET_REPORT_ID_MASK); + return -EINVAL; + } + + return 0; +} + static const struct hid_ops ops = { .int_in_ready = in_ready_cb, + .set_report = set_report_cb, }; int zmk_usb_hid_send_report(const uint8_t *report, size_t len) {