From 574ee4ee5f96da37ab4e6d083c15bedb1f04e799 Mon Sep 17 00:00:00 2001 From: ReFil <31960031+ReFil@users.noreply.github.com> Date: Tue, 19 Dec 2023 12:23:06 +0000 Subject: [PATCH] feat(split): Use split data transfer for hid indicators --- app/src/hid_indicators.c | 24 ++++++++++++++++---- app/src/split/bluetooth/service.c | 37 ------------------------------- 2 files changed, 20 insertions(+), 41 deletions(-) diff --git a/app/src/hid_indicators.c b/app/src/hid_indicators.c index 1b489068956..817b238e11e 100644 --- a/app/src/hid_indicators.c +++ b/app/src/hid_indicators.c @@ -13,6 +13,8 @@ #include #include #include +#include +#include LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); @@ -27,14 +29,23 @@ zmk_hid_indicators_t zmk_hid_indicators_get_profile(struct zmk_endpoint_instance return hid_indicators[profile]; } +static void zmk_hid_indicators_send_state(struct k_work *work) { + zmk_hid_indicators_t indicators = zmk_hid_indicators_get_current_profile(); + int err = zmk_split_central_send_data(DATA_TAG_HID_INDICATORS_STATE, + sizeof(zmk_hid_indicators_t), (uint8_t *)&indicators); + if (err) { + LOG_ERR("HID indicators send failed (err %d)", err); + } +} + +K_WORK_DEFINE(hid_indicators_send_state_work, zmk_hid_indicators_send_state); + static void raise_led_changed_event(struct k_work *_work) { const zmk_hid_indicators_t indicators = zmk_hid_indicators_get_current_profile(); raise_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 + k_work_submit_to_queue(zmk_workqueue_lowprio_work_q(), &hid_indicators_send_state_work); } static K_WORK_DEFINE(led_changed_work, raise_led_changed_event); @@ -60,9 +71,14 @@ void zmk_hid_indicators_process_report(struct zmk_hid_led_report_body *report, } static int profile_listener(const zmk_event_t *eh) { - raise_led_changed_event(NULL); + if (as_zmk_endpoint_changed(eh)) { + raise_led_changed_event(NULL); + } else if (as_zmk_split_peripheral_status_changed(eh)) { + k_work_submit_to_queue(zmk_workqueue_lowprio_work_q(), &hid_indicators_send_state_work); + } return 0; } static ZMK_LISTENER(profile_listener, profile_listener); static ZMK_SUBSCRIPTION(profile_listener, zmk_endpoint_changed); +static ZMK_SUBSCRIPTION(profile_listener, zmk_split_peripheral_status_changed); diff --git a/app/src/split/bluetooth/service.c b/app/src/split/bluetooth/service.c index 5798dc7af91..461ee185b94 100644 --- a/app/src/split/bluetooth/service.c +++ b/app/src/split/bluetooth/service.c @@ -22,10 +22,6 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include #include -#if IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS) -#include -#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS) - #include #include #include @@ -138,34 +134,6 @@ 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_t hid_indicators = 0; - -static void split_svc_update_indicators_callback(struct k_work *work) { - LOG_DBG("Raising HID indicators changed event: %x", hid_indicators); - raise_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_t)) { - 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), @@ -186,11 +154,6 @@ 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);