Skip to content

Commit 09b3331

Browse files
committed
feat(split): Increase split interval during idle
1 parent 6227866 commit 09b3331

File tree

4 files changed

+124
-1
lines changed

4 files changed

+124
-1
lines changed

app/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ if (CONFIG_ZMK_SPLIT_BLE AND (NOT CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL))
7676
endif()
7777
if (CONFIG_ZMK_SPLIT_BLE AND CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL)
7878
target_sources(app PRIVATE src/split/bluetooth/central.c)
79+
target_sources_ifdef(CONFIG_ZMK_BLE_PERIPHERAL_IDLE app PRIVATE src/split/bluetooth/central_listener.c)
7980
endif()
8081
target_sources_ifdef(CONFIG_USB_DEVICE_STACK app PRIVATE src/usb.c)
8182
target_sources_ifdef(CONFIG_ZMK_USB app PRIVATE src/usb_hid.c)

app/Kconfig

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,40 @@ config ZMK_BLE_SPLIT_CENTRAL_SPLIT_RUN_QUEUE_SIZE
202202
int "Max number of behavior run events to queue to send to the peripheral(s)"
203203
default 5
204204

205+
config ZMK_BLE_PERIPHERAL_INT
206+
int "Peripheral connection interval in 1.25ms units"
207+
default 6
208+
209+
config ZMK_BLE_PERIPHERAL_LATENCY
210+
int "Peripheral latency in Connection Intervals"
211+
default 30
212+
213+
config ZMK_BLE_PERIPHERAL_TIMEOUT
214+
int "Peripheral supervision timeout in 10ms units"
215+
default 400
216+
217+
config ZMK_BLE_PERIPHERAL_IDLE
218+
bool "Set slower split peripheral BLE params on idle to save power"
219+
depends on ZMK_SPLIT_BLE_ROLE_CENTRAL
220+
default y
221+
222+
if ZMK_BLE_PERIPHERAL_IDLE
223+
224+
config ZMK_BLE_PERIPHERAL_IDLE_INT
225+
int "Peripheral idle connection interval in 1.25ms units"
226+
default 18
227+
228+
config ZMK_BLE_PERIPHERAL_IDLE_LATENCY
229+
int "Peripheral idle latency in Connection Intervals"
230+
default 10
231+
232+
config ZMK_BLE_PERIPHERAL_IDLE_TIMEOUT
233+
int "Peripheral idle supervision timeout in 10ms units"
234+
default 400
235+
236+
# ZMK_BLE_PERIPHERAL_IDLE
237+
endif
238+
205239
endif
206240

207241
if !ZMK_SPLIT_BLE_ROLE_CENTRAL

app/src/split/bluetooth/central.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,9 @@ static bool split_central_eir_found(struct bt_data *data, void *user_data) {
393393
LOG_ERR("Update phy conn failed (err %d)", err);
394394
}
395395
} else {
396-
param = BT_LE_CONN_PARAM(0x0006, 0x0006, 30, 400);
396+
param = BT_LE_CONN_PARAM(
397+
CONFIG_ZMK_BLE_PERIPHERAL_INT, CONFIG_ZMK_BLE_PERIPHERAL_INT,
398+
CONFIG_ZMK_BLE_PERIPHERAL_LATENCY, CONFIG_ZMK_BLE_PERIPHERAL_TIMEOUT);
397399

398400
LOG_DBG("Initiating new connnection");
399401

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2022 The ZMK Contributors
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
7+
#include <logging/log.h>
8+
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
9+
10+
#include <bluetooth/bluetooth.h>
11+
#include <bluetooth/conn.h>
12+
13+
#include <zmk/event_manager.h>
14+
#include <zmk/events/activity_state_changed.h>
15+
16+
static void set_sleep_params(struct bt_conn *conn, void *data) {
17+
struct bt_conn_info info;
18+
19+
bt_conn_get_info(conn, &info);
20+
21+
if (info.role == BT_CONN_ROLE_CENTRAL) {
22+
int err =
23+
bt_conn_le_param_update(conn, BT_LE_CONN_PARAM(CONFIG_ZMK_BLE_PERIPHERAL_IDLE_INT,
24+
CONFIG_ZMK_BLE_PERIPHERAL_IDLE_INT,
25+
CONFIG_ZMK_BLE_PERIPHERAL_IDLE_LATENCY,
26+
CONFIG_ZMK_BLE_PERIPHERAL_IDLE_TIMEOUT));
27+
28+
if (err) {
29+
LOG_DBG("Failed to sleep split connection: %d", err);
30+
}
31+
}
32+
}
33+
34+
static void set_wake_params(struct bt_conn *conn, void *data) {
35+
struct bt_conn_info info;
36+
37+
bt_conn_get_info(conn, &info);
38+
39+
if (info.role == BT_CONN_ROLE_CENTRAL) {
40+
int err = bt_conn_le_param_update(
41+
conn,
42+
BT_LE_CONN_PARAM(CONFIG_ZMK_BLE_PERIPHERAL_INT, CONFIG_ZMK_BLE_PERIPHERAL_INT,
43+
CONFIG_ZMK_BLE_PERIPHERAL_LATENCY, CONFIG_ZMK_BLE_PERIPHERAL_TIMEOUT));
44+
45+
if (err) {
46+
LOG_DBG("Failed to wake up split connection: %d", err);
47+
}
48+
}
49+
}
50+
51+
static void sleep_all() {
52+
LOG_DBG("Setting idle connection parameters on peripherals");
53+
54+
bt_conn_foreach(BT_CONN_TYPE_LE, set_sleep_params, NULL);
55+
}
56+
57+
static void wake_all() {
58+
LOG_DBG("Waking up from idle connection parameters on peripherals");
59+
60+
bt_conn_foreach(BT_CONN_TYPE_LE, set_wake_params, NULL);
61+
}
62+
63+
int central_event_handler(const zmk_event_t *eh) {
64+
struct zmk_activity_state_changed *ev = as_zmk_activity_state_changed(eh);
65+
if (ev == NULL) {
66+
return -ENOTSUP;
67+
}
68+
69+
switch (ev->state) {
70+
case ZMK_ACTIVITY_ACTIVE:
71+
wake_all();
72+
break;
73+
case ZMK_ACTIVITY_IDLE:
74+
sleep_all();
75+
break;
76+
case ZMK_ACTIVITY_SLEEP:
77+
break;
78+
default:
79+
LOG_WRN("Unhandled activity state: %d", ev->state);
80+
return -EINVAL;
81+
}
82+
return 0;
83+
}
84+
85+
ZMK_LISTENER(central, central_event_handler);
86+
ZMK_SUBSCRIPTION(central, zmk_activity_state_changed);

0 commit comments

Comments
 (0)