Skip to content

Commit 3d2bd01

Browse files
committed
fix(split): Raise release events on disconnect.
* When a peripheral disconnects from a centraly, raise position events to release any active positions from that peripheral.
1 parent 53bec71 commit 3d2bd01

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

app/src/split/bluetooth/central.c

+26-10
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ static const struct bt_uuid_128 split_service_uuid = BT_UUID_INIT_128(ZMK_SPLIT_
5454
K_MSGQ_DEFINE(peripheral_event_msgq, sizeof(struct zmk_position_state_changed),
5555
CONFIG_ZMK_SPLIT_BLE_CENTRAL_POSITION_QUEUE_SIZE, 4);
5656

57+
void peripheral_event_work_callback(struct k_work *work) {
58+
struct zmk_position_state_changed ev;
59+
while (k_msgq_get(&peripheral_event_msgq, &ev, K_NO_WAIT) == 0) {
60+
LOG_DBG("Trigger key position state change for %d", ev.position);
61+
ZMK_EVENT_RAISE(new_zmk_position_state_changed(ev));
62+
}
63+
}
64+
65+
K_WORK_DEFINE(peripheral_event_work, peripheral_event_work_callback);
66+
5767
int peripheral_slot_index_for_conn(struct bt_conn *conn) {
5868
for (int i = 0; i < ZMK_BLE_SPLIT_PERIPHERAL_COUNT; i++) {
5969
if (peripherals[i].conn == conn) {
@@ -92,6 +102,22 @@ int release_peripheral_slot(int index) {
92102
}
93103
slot->state = PERIPHERAL_SLOT_STATE_OPEN;
94104

105+
// Raise events releasing any active positions from this peripheral
106+
for (int i = 0; i < POSITION_STATE_DATA_LEN; i++) {
107+
for (int j = 0; j < 8; j++) {
108+
if (slot->position_state[i] & BIT(j)) {
109+
uint32_t position = (i * 8) + j;
110+
struct zmk_position_state_changed ev = {.source = index,
111+
.position = position,
112+
.state = false,
113+
.timestamp = k_uptime_get()};
114+
115+
k_msgq_put(&peripheral_event_msgq, &ev, K_NO_WAIT);
116+
k_work_submit(&peripheral_event_work);
117+
}
118+
}
119+
}
120+
95121
for (int i = 0; i < POSITION_STATE_DATA_LEN; i++) {
96122
slot->position_state[i] = 0U;
97123
slot->changed_positions[i] = 0U;
@@ -136,16 +162,6 @@ int confirm_peripheral_slot_conn(struct bt_conn *conn) {
136162
return 0;
137163
}
138164

139-
void peripheral_event_work_callback(struct k_work *work) {
140-
struct zmk_position_state_changed ev;
141-
while (k_msgq_get(&peripheral_event_msgq, &ev, K_NO_WAIT) == 0) {
142-
LOG_DBG("Trigger key position state change for %d", ev.position);
143-
ZMK_EVENT_RAISE(new_zmk_position_state_changed(ev));
144-
}
145-
}
146-
147-
K_WORK_DEFINE(peripheral_event_work, peripheral_event_work_callback);
148-
149165
static uint8_t split_central_notify_func(struct bt_conn *conn,
150166
struct bt_gatt_subscribe_params *params, const void *data,
151167
uint16_t length) {

0 commit comments

Comments
 (0)