Skip to content

Commit

Permalink
feat(split): add peripheral changed event on central
Browse files Browse the repository at this point in the history
Add a new event for when any peripheral connects or disconnects from the central
  • Loading branch information
ReFil committed Jan 7, 2025
1 parent d0016b3 commit dff7aaf
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
8 changes: 7 additions & 1 deletion app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ 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)
if (CONFIG_ZMK_SPLIT)
if(CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
target_sources(app PRIVATE src/events/split_central_peripheral_status_changed.c)
else()
target_sources(app PRIVATE src/events/split_peripheral_status_changed.c)
endif()
endif()
add_subdirectory(src/split)

target_sources_ifdef(CONFIG_USB_DEVICE_STACK app PRIVATE src/usb.c)
Expand Down
18 changes: 18 additions & 0 deletions app/include/zmk/events/split_central_peripheral_status_changed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2024 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#pragma once

#include <zephyr/kernel.h>
#include <zmk/event_manager.h>
#include <zmk/split/bluetooth/central.h>

struct zmk_split_central_peripheral_status_changed {
enum zmk_split_bt_central_peripheral_source_state state;
uint8_t slot;
};

ZMK_EVENT_DECLARE(zmk_split_central_peripheral_status_changed);
6 changes: 6 additions & 0 deletions app/include/zmk/split/bluetooth/central.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
#include <zephyr/bluetooth/addr.h>
#include <zmk/behavior.h>

enum zmk_split_bt_central_peripheral_source_state {
PERIPHERAL_SLOT_STATE_OPEN,
PERIPHERAL_SLOT_STATE_CONNECTING,
PERIPHERAL_SLOT_STATE_CONNECTED,
};

#if IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)
#include <zmk/hid_indicators_types.h>
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS)
Expand Down
10 changes: 10 additions & 0 deletions app/src/events/split_central_peripheral_status_changed.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright (c) 2024 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/

#include <zephyr/kernel.h>
#include <zmk/events/split_central_peripheral_status_changed.h>

ZMK_EVENT_IMPL(zmk_split_central_peripheral_status_changed);
19 changes: 12 additions & 7 deletions app/src/split/bluetooth/central.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/sensors.h>
#include <zmk/split/bluetooth/uuid.h>
#include <zmk/split/bluetooth/service.h>
#include <zmk/split/bluetooth/central.h>
#include <zmk/event_manager.h>
#include <zmk/events/position_state_changed.h>
#include <zmk/events/sensor_event.h>
#include <zmk/events/battery_state_changed.h>
#include <zmk/events/split_central_peripheral_status_changed.h>
#include <zmk/pointing/input_split.h>
#include <zmk/hid_indicators_types.h>
#include <zmk/physical_layouts.h>
Expand All @@ -37,14 +39,8 @@ static int start_scanning(void);

#define POSITION_STATE_DATA_LEN 16

enum peripheral_slot_state {
PERIPHERAL_SLOT_STATE_OPEN,
PERIPHERAL_SLOT_STATE_CONNECTING,
PERIPHERAL_SLOT_STATE_CONNECTED,
};

struct peripheral_slot {
enum peripheral_slot_state state;
enum zmk_split_bt_central_peripheral_source_state state;
struct bt_conn *conn;
struct bt_gatt_discover_params discover_params;
struct bt_gatt_subscribe_params subscribe_params;
Expand Down Expand Up @@ -965,6 +961,10 @@ static void split_central_connected(struct bt_conn *conn, uint8_t conn_err) {

confirm_peripheral_slot_conn(conn);
split_central_process_connection(conn);
raise_zmk_split_central_peripheral_status_changed(
(struct zmk_split_central_peripheral_status_changed){
.state = peripherals[peripheral_slot_index_for_conn(conn)].state,
.slot = peripheral_slot_index_for_conn(conn)});
}

static void split_central_disconnected(struct bt_conn *conn, uint8_t reason) {
Expand All @@ -988,6 +988,11 @@ static void split_central_disconnected(struct bt_conn *conn, uint8_t reason) {

err = release_peripheral_slot_for_conn(conn);

raise_zmk_split_central_peripheral_status_changed(
(struct zmk_split_central_peripheral_status_changed){
.state = peripherals[peripheral_slot_index_for_conn(conn)].state,
.slot = peripheral_slot_index_for_conn(conn)});

if (err < 0) {
return;
}
Expand Down

0 comments on commit dff7aaf

Please sign in to comment.