From 9a6668375bbb3b0679f3bab4d34bb2b2772168dc Mon Sep 17 00:00:00 2001 From: Alessandro Bortolin Date: Sun, 27 Nov 2022 18:11:43 +0100 Subject: [PATCH] feat: LED indicators on peripheral side --- app/include/zmk/split/bluetooth/central.h | 12 +++++- app/include/zmk/split/bluetooth/uuid.h | 1 + app/src/hid_indicators.c | 10 ++++- app/src/split/Kconfig | 6 +++ app/src/split/bluetooth/central.c | 47 ++++++++++++++++++++++- app/src/split/bluetooth/service.c | 38 ++++++++++++++++++ 6 files changed, 110 insertions(+), 4 deletions(-) diff --git a/app/include/zmk/split/bluetooth/central.h b/app/include/zmk/split/bluetooth/central.h index 443d9b1b4894..d38b51e88cdc 100644 --- a/app/include/zmk/split/bluetooth/central.h +++ b/app/include/zmk/split/bluetooth/central.h @@ -4,5 +4,15 @@ #include #include +#if IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS) +#include +#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS) + 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); + +#if IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS) + +int zmk_split_bt_update_hid_indicator(zmk_hid_indicators indicators); + +#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS) diff --git a/app/include/zmk/split/bluetooth/uuid.h b/app/include/zmk/split/bluetooth/uuid.h index c38131dd83e2..dccdfc804c56 100644 --- a/app/include/zmk/split/bluetooth/uuid.h +++ b/app/include/zmk/split/bluetooth/uuid.h @@ -17,3 +17,4 @@ #define ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID ZMK_BT_SPLIT_UUID(0x00000001) #define ZMK_SPLIT_BT_CHAR_RUN_BEHAVIOR_UUID ZMK_BT_SPLIT_UUID(0x00000002) #define ZMK_SPLIT_BT_CHAR_SENSOR_STATE_UUID ZMK_BT_SPLIT_UUID(0x00000003) +#define ZMK_SPLIT_BT_UPDATE_HID_INDICATORS_UUID ZMK_BT_SPLIT_UUID(0x00000004) diff --git a/app/src/hid_indicators.c b/app/src/hid_indicators.c index db769146a2ed..45cac96a04cd 100644 --- a/app/src/hid_indicators.c +++ b/app/src/hid_indicators.c @@ -28,8 +28,14 @@ zmk_hid_indicators zmk_hid_indicators_get_profile(struct zmk_endpoint_instance e } static void raise_led_changed_event(struct k_work *_work) { - ZMK_EVENT_RAISE(new_zmk_hid_indicators_changed((struct zmk_hid_indicators_changed){ - .indicators = zmk_hid_indicators_get_current_profile()})); + 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_PERIPHERAL_HID_INDICATORS) && IS_ENABLED(CONFIG_ZMK_SPLIT_BLE) + zmk_split_bt_update_hid_indicator(indicators); +#endif } static K_WORK_DEFINE(led_changed_work, raise_led_changed_event); diff --git a/app/src/split/Kconfig b/app/src/split/Kconfig index dbe5f092644d..1dc59eb20a9e 100644 --- a/app/src/split/Kconfig +++ b/app/src/split/Kconfig @@ -20,6 +20,12 @@ config ZMK_SPLIT_BLE endchoice +config ZMK_SPLIT_PERIPHERAL_HID_INDICATORS + bool "Peripheral HID Indicators" + depends on HID_INDICATORS + help + Enable propogating the HID (LED) Indicator state to the split peripheral(s). + #ZMK_SPLIT endif diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c index 860e89a5f3ef..4228a3e7a5f0 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,9 +332,14 @@ 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->update_hid_indicators); #if ZMK_KEYMAP_HAS_SENSORS subscribed = subscribed && slot->sensor_subscribe_params.value_handle; #endif /* ZMK_KEYMAP_HAS_SENSORS */ @@ -685,6 +693,43 @@ int zmk_split_bt_invoke_behavior(uint8_t source, struct zmk_behavior_binding *bi return split_bt_invoke_behavior_payload(wrapper); } +#if IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS) + +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; + } + + if (peripherals[i].update_hid_indicators == 0) { + // It appears that sometimes the peripheral is considered connected + // before the GATT characteristics have been discovered. If this is + // the case, the update_hid_indicators handle will not yet be set. + 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); +} + +#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_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 620df53e11ca..f9d8bab0de49 100644 --- a/app/src/split/bluetooth/service.c +++ b/app/src/split/bluetooth/service.c @@ -21,6 +21,11 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include #include #include + +#if IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS) +#include +#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS) + #include #include @@ -105,6 +110,34 @@ static void split_svc_pos_state_ccc(const struct bt_gatt_attr *attr, uint16_t va LOG_DBG("value %d", value); } +#if IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS) + +static zmk_hid_indicators hid_indicators = 0; + +static void split_svc_update_indicators_callback(struct k_work *work) { + LOG_DBG("Raising HID indicators changed event: %x", hid_indicators); + 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; +} + +#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS) + 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), @@ -122,6 +155,11 @@ BT_GATT_SERVICE_DEFINE( split_svc_sensor_state, NULL, &last_sensor_event), BT_GATT_CCC(split_svc_sensor_state_ccc, BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT), #endif /* ZMK_KEYMAP_HAS_SENSORS */ +#if IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS) + 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), +#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS) ); K_THREAD_STACK_DEFINE(service_q_stack, CONFIG_ZMK_SPLIT_BLE_PERIPHERAL_STACK_SIZE);