Skip to content

Commit

Permalink
feat(split): Use split data transfer for hid indicators
Browse files Browse the repository at this point in the history
  • Loading branch information
ReFil committed Mar 21, 2024
1 parent 3f316a1 commit 57bc9f3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 41 deletions.
24 changes: 20 additions & 4 deletions app/src/hid_indicators.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <zmk/events/hid_indicators_changed.h>
#include <zmk/events/endpoint_changed.h>
#include <zmk/split/bluetooth/central.h>
#include <zmk/events/split_peripheral_status_changed.h>
#include <zmk/workqueue.h>

LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

Expand All @@ -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);
Expand All @@ -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);
37 changes: 0 additions & 37 deletions app/src/split/bluetooth/service.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/split/bluetooth/uuid.h>
#include <zmk/split/bluetooth/service.h>

#if IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)
#include <zmk/events/hid_indicators_changed.h>
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)

#include <zmk/events/sensor_event.h>
#include <zmk/sensors.h>
#include <zmk/events/split_data_xfer_event.h>
Expand Down Expand Up @@ -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),
Expand All @@ -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);
Expand Down

0 comments on commit 57bc9f3

Please sign in to comment.