Skip to content

Commit 33e3b02

Browse files
committed
feat: Split physical layout selection sync.
* Ensure the split peripherals have the same selected physical layout on connection and change.
1 parent f992352 commit 33e3b02

File tree

5 files changed

+141
-3
lines changed

5 files changed

+141
-3
lines changed

app/include/zmk/physical_layouts.h

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88

99
#include <zephyr/kernel.h>
1010
#include <zmk/matrix_transform.h>
11+
#include <zmk/event_manager.h>
12+
13+
struct zmk_physical_layout_selection_changed {
14+
uint8_t selection;
15+
};
16+
17+
ZMK_EVENT_DECLARE(zmk_physical_layout_selection_changed);
1118

1219
struct zmk_key_physical_attrs {
1320
int16_t width;

app/include/zmk/split/bluetooth/uuid.h

+1
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@
1818
#define ZMK_SPLIT_BT_CHAR_RUN_BEHAVIOR_UUID ZMK_BT_SPLIT_UUID(0x00000002)
1919
#define ZMK_SPLIT_BT_CHAR_SENSOR_STATE_UUID ZMK_BT_SPLIT_UUID(0x00000003)
2020
#define ZMK_SPLIT_BT_UPDATE_HID_INDICATORS_UUID ZMK_BT_SPLIT_UUID(0x00000004)
21+
#define ZMK_SPLIT_BT_SELECT_PHYS_LAYOUT_UUID ZMK_BT_SPLIT_UUID(0x00000005)

app/src/physical_layouts.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
2222
#include <zmk/event_manager.h>
2323
#include <zmk/events/position_state_changed.h>
2424

25+
ZMK_EVENT_IMPL(zmk_physical_layout_selection_changed);
26+
2527
#define DT_DRV_COMPAT zmk_physical_layout
2628

2729
#define USE_PHY_LAYOUTS \
@@ -247,7 +249,14 @@ int zmk_physical_layouts_select(uint8_t index) {
247249
return -EINVAL;
248250
}
249251

250-
return zmk_physical_layouts_select_layout(layouts[index]);
252+
int ret = zmk_physical_layouts_select_layout(layouts[index]);
253+
254+
if (ret >= 0) {
255+
raise_zmk_physical_layout_selection_changed(
256+
(struct zmk_physical_layout_selection_changed){.selection = index});
257+
}
258+
259+
return ret;
251260
}
252261

253262
int zmk_physical_layouts_get_selected(void) {

app/src/split/bluetooth/central.c

+81-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
3030
#include <zmk/events/sensor_event.h>
3131
#include <zmk/events/battery_state_changed.h>
3232
#include <zmk/hid_indicators_types.h>
33+
#include <zmk/physical_layouts.h>
3334

3435
static int start_scanning(void);
3536

@@ -56,6 +57,7 @@ struct peripheral_slot {
5657
#if IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)
5758
uint16_t update_hid_indicators;
5859
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)
60+
uint16_t selected_physical_layout_handle;
5961
uint8_t position_state[POSITION_STATE_DATA_LEN];
6062
uint8_t changed_positions[POSITION_STATE_DATA_LEN];
6163
};
@@ -141,6 +143,7 @@ int release_peripheral_slot(int index) {
141143
// Clean up previously discovered handles;
142144
slot->subscribe_params.value_handle = 0;
143145
slot->run_behavior_handle = 0;
146+
slot->selected_physical_layout_handle = 0;
144147
#if IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)
145148
slot->update_hid_indicators = 0;
146149
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)
@@ -392,6 +395,46 @@ static int split_central_subscribe(struct bt_conn *conn, struct bt_gatt_subscrib
392395
return err;
393396
}
394397

398+
static int update_peripheral_selected_layout(struct peripheral_slot *slot, uint8_t layout_idx) {
399+
if (slot->state != PERIPHERAL_SLOT_STATE_CONNECTED) {
400+
return -ENOTCONN;
401+
}
402+
403+
if (slot->selected_physical_layout_handle == 0) {
404+
// It appears that sometimes the peripheral is considered connected
405+
// before the GATT characteristics have been discovered. If this is
406+
// the case, the selected_physical_layout_handle will not yet be set.
407+
return -EAGAIN;
408+
}
409+
410+
if (bt_conn_get_security(slot->conn) < BT_SECURITY_L2) {
411+
return -EAGAIN;
412+
}
413+
414+
int err = bt_gatt_write_without_response(slot->conn, slot->selected_physical_layout_handle,
415+
&layout_idx, sizeof(layout_idx), true);
416+
417+
if (err < 0) {
418+
LOG_ERR("Failed to write physical layout index to peripheral (err %d)", err);
419+
}
420+
421+
return err;
422+
}
423+
424+
static void update_peripherals_selected_physical_layout(struct k_work *_work) {
425+
uint8_t layout_idx = zmk_physical_layouts_get_selected();
426+
for (int i = 0; i < ZMK_SPLIT_BLE_PERIPHERAL_COUNT; i++) {
427+
if (peripherals[i].state != PERIPHERAL_SLOT_STATE_CONNECTED) {
428+
continue;
429+
}
430+
431+
update_peripheral_selected_layout(&peripherals[i], layout_idx);
432+
}
433+
}
434+
435+
K_WORK_DEFINE(update_peripherals_selected_layouts_work,
436+
update_peripherals_selected_physical_layout);
437+
395438
static uint8_t split_central_chrc_discovery_func(struct bt_conn *conn,
396439
const struct bt_gatt_attr *attr,
397440
struct bt_gatt_discover_params *params) {
@@ -442,6 +485,11 @@ static uint8_t split_central_chrc_discovery_func(struct bt_conn *conn,
442485
slot->discover_params.uuid = NULL;
443486
slot->discover_params.start_handle = attr->handle + 2;
444487
slot->run_behavior_handle = bt_gatt_attr_value_handle(attr);
488+
} else if (!bt_uuid_cmp(((struct bt_gatt_chrc *)attr->user_data)->uuid,
489+
BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SELECT_PHYS_LAYOUT_UUID))) {
490+
LOG_DBG("Found select physical layout handle");
491+
slot->selected_physical_layout_handle = bt_gatt_attr_value_handle(attr);
492+
k_work_submit(&update_peripherals_selected_layouts_work);
445493
#if IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)
446494
} else if (!bt_uuid_cmp(((struct bt_gatt_chrc *)attr->user_data)->uuid,
447495
BT_UUID_DECLARE_128(ZMK_SPLIT_BT_UPDATE_HID_INDICATORS_UUID))) {
@@ -467,7 +515,8 @@ static uint8_t split_central_chrc_discovery_func(struct bt_conn *conn,
467515
#endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_CENTRAL_BATTERY_LEVEL_FETCHING) */
468516
}
469517

470-
bool subscribed = slot->run_behavior_handle && slot->subscribe_params.value_handle;
518+
bool subscribed = slot->run_behavior_handle && slot->subscribe_params.value_handle &&
519+
slot->selected_physical_layout_handle;
471520

472521
#if ZMK_KEYMAP_HAS_SENSORS
473522
subscribed = subscribed && slot->sensor_subscribe_params.value_handle;
@@ -739,9 +788,30 @@ static void split_central_disconnected(struct bt_conn *conn, uint8_t reason) {
739788
start_scanning();
740789
}
741790

791+
static void split_central_security_changed(struct bt_conn *conn, bt_security_t level,
792+
enum bt_security_err err) {
793+
struct peripheral_slot *slot = peripheral_slot_for_conn(conn);
794+
if (!slot || !slot->selected_physical_layout_handle) {
795+
return;
796+
}
797+
798+
if (err > 0) {
799+
LOG_DBG("Skipping updating the physical layout for peripheral with security error");
800+
return;
801+
}
802+
803+
if (level < BT_SECURITY_L2) {
804+
LOG_DBG("Skipping updating the physical layout for peripheral with insufficient security");
805+
return;
806+
}
807+
808+
k_work_submit(&update_peripherals_selected_layouts_work);
809+
}
810+
742811
static struct bt_conn_cb conn_callbacks = {
743812
.connected = split_central_connected,
744813
.disconnected = split_central_disconnected,
814+
.security_changed = split_central_security_changed,
745815
};
746816

747817
K_THREAD_STACK_DEFINE(split_central_split_run_q_stack,
@@ -898,3 +968,13 @@ static int zmk_split_bt_central_init(void) {
898968
}
899969

900970
SYS_INIT(zmk_split_bt_central_init, APPLICATION, CONFIG_ZMK_BLE_INIT_PRIORITY);
971+
972+
static int zmk_split_bt_central_listener_cb(const zmk_event_t *eh) {
973+
if (as_zmk_physical_layout_selection_changed(eh)) {
974+
k_work_submit(&update_peripherals_selected_layouts_work);
975+
}
976+
return ZMK_EV_EVENT_BUBBLE;
977+
}
978+
979+
ZMK_LISTENER(zmk_split_bt_central, zmk_split_bt_central_listener_cb);
980+
ZMK_SUBSCRIPTION(zmk_split_bt_central, zmk_physical_layout_selection_changed);

app/src/split/bluetooth/service.c

+42-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
1919
#include <drivers/behavior.h>
2020
#include <zmk/behavior.h>
2121
#include <zmk/matrix.h>
22+
#include <zmk/physical_layouts.h>
2223
#include <zmk/split/bluetooth/uuid.h>
2324
#include <zmk/split/bluetooth/service.h>
2425

@@ -138,6 +139,42 @@ static ssize_t split_svc_update_indicators(struct bt_conn *conn, const struct bt
138139

139140
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)
140141

142+
static uint8_t selected_phys_layout = 0;
143+
144+
static void split_svc_select_phys_layout_callback(struct k_work *work) {
145+
LOG_DBG("Selecting physical layout after GATT write of %d", selected_phys_layout);
146+
zmk_physical_layouts_select(selected_phys_layout);
147+
}
148+
149+
static K_WORK_DEFINE(split_svc_select_phys_layout_work, split_svc_select_phys_layout_callback);
150+
151+
static ssize_t split_svc_select_phys_layout(struct bt_conn *conn, const struct bt_gatt_attr *attr,
152+
const void *buf, uint16_t len, uint16_t offset,
153+
uint8_t flags) {
154+
if (offset + len > sizeof(uint8_t) || len == 0) {
155+
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
156+
}
157+
158+
selected_phys_layout = *(uint8_t *)buf;
159+
160+
k_work_submit(&split_svc_select_phys_layout_work);
161+
162+
return len;
163+
}
164+
165+
static ssize_t split_svc_get_selected_phys_layout(struct bt_conn *conn,
166+
const struct bt_gatt_attr *attrs, void *buf,
167+
uint16_t len, uint16_t offset) {
168+
int selected_ret = zmk_physical_layouts_get_selected();
169+
if (selected_ret < 0) {
170+
return BT_GATT_ERR(BT_ATT_ERR_VALUE_NOT_ALLOWED);
171+
}
172+
173+
uint8_t selected = (uint8_t)selected_ret;
174+
175+
return bt_gatt_attr_read(conn, attrs, buf, len, offset, &selected, sizeof(selected));
176+
}
177+
141178
BT_GATT_SERVICE_DEFINE(
142179
split_svc, BT_GATT_PRIMARY_SERVICE(BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID)),
143180
BT_GATT_CHARACTERISTIC(BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID),
@@ -160,7 +197,11 @@ BT_GATT_SERVICE_DEFINE(
160197
BT_GATT_CHRC_WRITE_WITHOUT_RESP, BT_GATT_PERM_WRITE_ENCRYPT, NULL,
161198
split_svc_update_indicators, NULL),
162199
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)
163-
);
200+
BT_GATT_CHARACTERISTIC(BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SELECT_PHYS_LAYOUT_UUID),
201+
BT_GATT_CHRC_WRITE | BT_GATT_CHRC_READ,
202+
BT_GATT_PERM_WRITE_ENCRYPT | BT_GATT_PERM_READ_ENCRYPT,
203+
split_svc_get_selected_phys_layout, split_svc_select_phys_layout,
204+
NULL), );
164205

165206
K_THREAD_STACK_DEFINE(service_q_stack, CONFIG_ZMK_SPLIT_BLE_PERIPHERAL_STACK_SIZE);
166207

0 commit comments

Comments
 (0)