Skip to content

Commit

Permalink
split into 2 sync options
Browse files Browse the repository at this point in the history
  • Loading branch information
angweekiat committed Sep 8, 2024
1 parent bc5dc5b commit fad0f80
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 85 deletions.
3 changes: 2 additions & 1 deletion app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ target_sources_ifdef(CONFIG_ZMK_BATTERY_REPORTING app PRIVATE src/battery.c)
target_sources_ifdef(CONFIG_ZMK_HID_INDICATORS app PRIVATE src/events/hid_indicators_changed.c)

target_sources_ifdef(CONFIG_ZMK_SPLIT app PRIVATE src/events/split_peripheral_status_changed.c)
target_sources_ifdef(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING app PRIVATE src/events/sync_activity_event.c)
target_sources_ifdef(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_PERIODIC app PRIVATE src/events/sync_activity_event.c)
target_sources_ifdef(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_ON_EVENT app PRIVATE src/events/sync_activity_event.c)
add_subdirectory(src/split)

target_sources_ifdef(CONFIG_USB_DEVICE_STACK app PRIVATE src/usb.c)
Expand Down
6 changes: 4 additions & 2 deletions app/include/zmk/split/bluetooth/central.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ int zmk_split_get_peripheral_battery_level(uint8_t source, uint8_t *level);

#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_CENTRAL_BATTERY_LEVEL_FETCHING)

#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)
#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_PERIODIC) || \
IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_ON_EVENT)

int zmk_split_bt_queue_sync_activity(int32_t inactive_duration);

#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_PERIODIC) ||
// IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_ON_EVENT)
66 changes: 39 additions & 27 deletions app/src/activity.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/events/sensor_event.h>
#include <zmk/events/sync_activity_event.h>

#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)
#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_PERIODIC) || \
IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_ON_EVENT)
#include <zmk/split/bluetooth/central.h>
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_PERIODIC) ||
// IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_ON_EVENT)

#include <zmk/pm.h>

Expand All @@ -42,14 +44,20 @@ bool is_usb_power_present(void) {
static enum zmk_activity_state activity_state;

static uint32_t activity_last_uptime;
#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING) && \
IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL)

#if IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL)

#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_PERIODIC)
static uint32_t last_periodic_sync_time;
#if CONFIG_ZMK_SPLIT_SYNC_EVENT_MIN_INTERVAL_MS > 0
#define PERIODIC_SYNC_INTERVAL_MS CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_PERIODIC_INTERVAL_MS
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_PERIODIC)

#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_ON_EVENT)
static uint32_t last_event_sync_time;
#endif // CONFIG_ZMK_SPLIT_SYNC_EVENT_MIN_INTERVAL_MS > 0
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING) &&
// IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
#define EVENT_SYNC_MIN_INTERVAL_MS CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_EVENT_MIN_INTERVAL
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_ON_EVENT)

#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL)

#define MAX_IDLE_MS CONFIG_ZMK_IDLE_TIMEOUT

Expand All @@ -75,15 +83,15 @@ enum zmk_activity_state zmk_activity_get_state(void) { return activity_state; }
int activity_event_listener(const zmk_event_t *eh) {
activity_last_uptime = k_uptime_get();

#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING) && \
IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) && CONFIG_ZMK_SPLIT_SYNC_EVENT_MIN_INTERVAL_MS > 0
if (activity_last_uptime - last_event_sync_time > CONFIG_ZMK_SPLIT_SYNC_EVENT_MIN_INTERVAL_MS) {
#if IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) && \
IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_ON_EVENT)
if (activity_last_uptime - last_event_sync_time > EVENT_SYNC_MIN_INTERVAL_MS) {
LOG_DBG("Refresh %d", activity_last_uptime - last_event_sync_time);
last_event_sync_time = activity_last_uptime;
zmk_split_bt_queue_sync_activity(0);
}
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING) &&
// IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) && CONFIG_ZMK_SPLIT_SYNC_EVENT_MIN_INTERVAL_MS >
// 0
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) &&
// IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_ON_EVENT)

return set_state(ZMK_ACTIVITY_ACTIVE);
}
Expand All @@ -109,14 +117,14 @@ void activity_work_handler(struct k_work *work) {
set_state(ZMK_ACTIVITY_IDLE);
}

#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING) && \
IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
if (current - last_periodic_sync_time > CONFIG_ZMK_SPLIT_SYNC_PERIODIC_INTERVAL_MS) {
#if IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) && \
IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_PERIODIC)
if (current - last_periodic_sync_time > PERIODIC_SYNC_INTERVAL_MS) {
last_periodic_sync_time = current;
zmk_split_bt_queue_sync_activity(inactive_time);
}
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING) &&
// IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) &&
// IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_PERIODIC)
}

K_WORK_DEFINE(activity_work, activity_work_handler);
Expand All @@ -136,19 +144,22 @@ ZMK_LISTENER(activity, activity_event_listener);
ZMK_SUBSCRIPTION(activity, zmk_position_state_changed);
ZMK_SUBSCRIPTION(activity, zmk_sensor_event);

#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING) && \
!IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
#if !IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) && \
(IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_PERIODIC) || \
IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_ON_EVENT))

int sync_activity_event_listener(const zmk_event_t *eh) {
int32_t current = k_uptime_get();

struct zmk_sync_activity_event *ev = as_zmk_sync_activity_event(eh);
if (ev == NULL) {
LOG_ERR("Invalid event type");
return -ENOTSUP;
}
int32_t central_activity_last_uptime = k_uptime_get() - ev->central_inactive_duration;
activity_last_uptime = central_activity_last_uptime;
int32_t new_inactive_time = k_uptime_get() - activity_last_uptime;

if (activity_state == ZMK_ACTIVITY_IDLE && new_inactive_time < MAX_IDLE_MS) {
activity_last_uptime = current - ev->central_inactive_duration;

if (activity_state == ZMK_ACTIVITY_IDLE && ev->central_inactive_duration < MAX_IDLE_MS) {
LOG_DBG("Syncing state to active to match central device.");
return set_state(ZMK_ACTIVITY_ACTIVE);
}
Expand All @@ -157,7 +168,8 @@ int sync_activity_event_listener(const zmk_event_t *eh) {

ZMK_LISTENER(sync_activity, sync_activity_event_listener);
ZMK_SUBSCRIPTION(sync_activity, zmk_sync_activity_event);
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING) &&
// !IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
#endif // !IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) &&
// (IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_PERIODIC) ||
// IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_ON_EVENT))

SYS_INIT(activity_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
23 changes: 13 additions & 10 deletions app/src/split/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,27 @@ config ZMK_SPLIT_PERIPHERAL_HID_INDICATORS
help
Enable propagating the HID (LED) Indicator state to the split peripheral(s).

config ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING
bool "Sync last activity timing across all devices"
config ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_PERIODIC
bool "Sync last activity timing across all devices periodically"
default n
help
Sync central device last activity timing to the split peripheral(s).
Sync central device last activity timing to the split peripheral(s) with a periodic interval.
Does not help to wake up peripheral devices that have gone to deep sleep.

if ZMK_SPLIT_ROLE_CENTRAL && ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING
config ZMK_SPLIT_SYNC_PERIODIC_INTERVAL_MS
config ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_PERIODIC_INTERVAL_MS
int "Last activity time periodic sync interval in milliseconds"
default 30000

config ZMK_SPLIT_SYNC_EVENT_MIN_INTERVAL_MS
int "Sync timings on events (key/sensors presses) as well, 0 to disable. Represents minimum interval in milliseconds"
default 0
config ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_ON_EVENT
bool "Sync last activity timing across all devices upon central activity event"
default n
help
Sync central device last activity timing to the split peripheral(s) when an event (key press/sensor)
is detected. Does not help to wake up peripheral devices that have gone to deep sleep.

#ZMK_SPLIT_ROLE_CENTRAL && ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING
endif
config ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_EVENT_MIN_INTERVAL
int "Sync timings on events (key/sensors presses) as well, 0 to disable. Represents minimum interval in milliseconds"
default 1000

#ZMK_SPLIT
endif
Expand Down
38 changes: 22 additions & 16 deletions app/src/split/bluetooth/central.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ static int start_scanning(void);

#define POSITION_STATE_DATA_LEN 16

#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_PERIODIC) || \
IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_ON_EVENT)
#define SYNC_LAST_ACTIVITY_TIMING 1
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_PERIODIC) ||
// IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_ON_EVENT)

enum peripheral_slot_state {
PERIPHERAL_SLOT_STATE_OPEN,
PERIPHERAL_SLOT_STATE_CONNECTING,
Expand All @@ -50,9 +56,9 @@ struct peripheral_slot {
struct bt_gatt_subscribe_params sensor_subscribe_params;
struct bt_gatt_discover_params sub_discover_params;
uint16_t run_behavior_handle;
#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)
#if IS_ENABLED(SYNC_LAST_ACTIVITY_TIMING)
uint16_t sync_activity_handle;
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)
#endif // IS_ENABLED(SYNC_LAST_ACTIVITY_TIMING)
#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_CENTRAL_BATTERY_LEVEL_FETCHING)
struct bt_gatt_subscribe_params batt_lvl_subscribe_params;
struct bt_gatt_read_params batt_lvl_read_params;
Expand All @@ -71,10 +77,10 @@ static bool is_scanning = false;

static const struct bt_uuid_128 split_service_uuid = BT_UUID_INIT_128(ZMK_SPLIT_BT_SERVICE_UUID);

#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)
#if IS_ENABLED(SYNC_LAST_ACTIVITY_TIMING)
static int32_t activity_inactive_duration;
static void split_central_sync_activity_with_delay();
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)
#endif // IS_ENABLED(SYNC_LAST_ACTIVITY_TIMING)

K_MSGQ_DEFINE(peripheral_event_msgq, sizeof(struct zmk_position_state_changed),
CONFIG_ZMK_SPLIT_BLE_CENTRAL_POSITION_QUEUE_SIZE, 4);
Expand Down Expand Up @@ -155,9 +161,9 @@ int release_peripheral_slot(int index) {
#if IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)
slot->update_hid_indicators = 0;
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)
#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)
#if IS_ENABLED(SYNC_LAST_ACTIVITY_TIMING)
slot->sync_activity_handle = 0;
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)
#endif // IS_ENABLED(SYNC_LAST_ACTIVITY_TIMING)

return 0;
}
Expand Down Expand Up @@ -506,13 +512,13 @@ static uint8_t split_central_chrc_discovery_func(struct bt_conn *conn,
slot->batt_lvl_read_params.single.offset = 0;
bt_gatt_read(conn, &slot->batt_lvl_read_params);
#endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_CENTRAL_BATTERY_LEVEL_FETCHING) */
#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)
#if IS_ENABLED(SYNC_LAST_ACTIVITY_TIMING)
} else if (!bt_uuid_cmp(chrc_uuid, BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_SYNC_ACTIVITY_UUID))) {
LOG_DBG("Found sync activity handle");
slot->discover_params.uuid = NULL;
slot->discover_params.start_handle = attr->handle + 2;
slot->sync_activity_handle = bt_gatt_attr_value_handle(attr);
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)
#endif // IS_ENABLED(SYNC_LAST_ACTIVITY_TIMING)
}

bool subscribed = slot->run_behavior_handle && slot->subscribe_params.value_handle &&
Expand All @@ -525,9 +531,9 @@ static uint8_t split_central_chrc_discovery_func(struct bt_conn *conn,
#if IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)
subscribed = subscribed && slot->update_hid_indicators;
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)
#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)
#if IS_ENABLED(SYNC_LAST_ACTIVITY_TIMING)
subscribed = subscribed && slot->sync_activity_handle;
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)
#endif // IS_ENABLED(SYNC_LAST_ACTIVITY_TIMING)
#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_CENTRAL_BATTERY_LEVEL_FETCHING)
subscribed = subscribed && slot->batt_lvl_subscribe_params.value_handle;
#endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_CENTRAL_BATTERY_LEVEL_FETCHING) */
Expand Down Expand Up @@ -766,9 +772,11 @@ static void split_central_connected(struct bt_conn *conn, uint8_t conn_err) {
confirm_peripheral_slot_conn(conn);
split_central_process_connection(conn);

#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)
#if IS_ENABLED(SYNC_LAST_ACTIVITY_TIMING)
// Bluetooth discovery is done only after connection, so a delay is
/// added here to compensate for that before syncing the activity time
split_central_sync_activity_with_delay();
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)
#endif // IS_ENABLED(SYNC_LAST_ACTIVITY_TIMING)
}

static void split_central_disconnected(struct bt_conn *conn, uint8_t reason) {
Expand Down Expand Up @@ -922,7 +930,7 @@ int zmk_split_bt_update_hid_indicator(zmk_hid_indicators_t indicators) {

#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)

#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)
#if IS_ENABLED(SYNC_LAST_ACTIVITY_TIMING)

static void split_central_sync_activity_callback(struct k_work *work) {
for (int i = 0; i < ZMK_SPLIT_BLE_PERIPHERAL_COUNT; i++) {
Expand Down Expand Up @@ -951,8 +959,6 @@ K_TIMER_DEFINE(split_central_sync_activity_delay_timer,
split_central_sync_activity_delay_timer_callback, NULL);

static void split_central_sync_activity_with_delay() {
// Bluetooth discovery is done only after connection, so a delay is added here to compensate
// for that, before syncing the sleep timers
k_timer_start(&split_central_sync_activity_delay_timer, K_SECONDS(1), K_SECONDS(1));
}

Expand All @@ -961,7 +967,7 @@ int zmk_split_bt_queue_sync_activity(int32_t inactive_duration) {
return k_work_submit_to_queue(&split_central_split_run_q, &split_central_sync_activity);
}

#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)
#endif // IS_ENABLED(SYNC_LAST_ACTIVITY_TIMING)

static int finish_init() {
return IS_ENABLED(CONFIG_ZMK_BLE_CLEAR_BONDS_ON_START) ? 0 : start_scanning();
Expand Down
61 changes: 32 additions & 29 deletions app/src/split/bluetooth/service.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#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)
#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)

#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_PERIODIC) || \
IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_ON_EVENT)
#include <zmk/events/sync_activity_event.h>
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)
#define SYNC_LAST_ACTIVITY_TIMING 1
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_PERIODIC) ||
// IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING_ON_EVENT)

#include <zmk/events/sensor_event.h>
#include <zmk/sensors.h>
Expand Down Expand Up @@ -142,30 +146,6 @@ static ssize_t split_svc_update_indicators(struct bt_conn *conn, const struct bt

#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)

#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)
static int32_t central_inactive_duration;

static void split_svc_sync_activity_callback(struct k_work *work) {
raise_zmk_sync_activity_event(
(struct zmk_sync_activity_event){.central_inactive_duration = central_inactive_duration});
}

static K_WORK_DEFINE(split_svc_sync_activity_work, split_svc_sync_activity_callback);

static ssize_t split_svc_sync_activity(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(int32_t)) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
}

memcpy((uint8_t *)&central_inactive_duration + offset, buf, len);
k_work_submit(&split_svc_sync_activity_work);

return len;
}
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)

static uint8_t selected_phys_layout = 0;

static void split_svc_select_phys_layout_callback(struct k_work *work) {
Expand Down Expand Up @@ -202,6 +182,30 @@ static ssize_t split_svc_get_selected_phys_layout(struct bt_conn *conn,
return bt_gatt_attr_read(conn, attrs, buf, len, offset, &selected, sizeof(selected));
}

#if IS_ENABLED(SYNC_LAST_ACTIVITY_TIMING)
static int32_t central_inactive_duration;

static void split_svc_sync_activity_callback(struct k_work *work) {
raise_zmk_sync_activity_event(
(struct zmk_sync_activity_event){.central_inactive_duration = central_inactive_duration});
}

static K_WORK_DEFINE(split_svc_sync_activity_work, split_svc_sync_activity_callback);

static ssize_t split_svc_sync_activity(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(int32_t)) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
}

memcpy((uint8_t *)&central_inactive_duration + offset, buf, len);
k_work_submit(&split_svc_sync_activity_work);

return len;
}
#endif // IS_ENABLED(SYNC_LAST_ACTIVITY_TIMING)

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 @@ -228,12 +232,11 @@ BT_GATT_SERVICE_DEFINE(
BT_GATT_CHRC_WRITE | BT_GATT_CHRC_READ,
BT_GATT_PERM_WRITE_ENCRYPT | BT_GATT_PERM_READ_ENCRYPT,
split_svc_get_selected_phys_layout, split_svc_select_phys_layout, NULL),

#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)
#if IS_ENABLED(SYNC_LAST_ACTIVITY_TIMING)
BT_GATT_CHARACTERISTIC(BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_SYNC_ACTIVITY_UUID),
BT_GATT_CHRC_WRITE_WITHOUT_RESP, BT_GATT_PERM_WRITE_ENCRYPT, NULL,
split_svc_sync_activity, NULL),
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)
#endif // IS_ENABLED(SYNC_LAST_ACTIVITY_TIMING)

);

Expand Down

0 comments on commit fad0f80

Please sign in to comment.