From 5047340a789aa9ffc9bb6f87c55b7132e8067536 Mon Sep 17 00:00:00 2001 From: ReFil <31960031+ReFil@users.noreply.github.com> Date: Wed, 22 Nov 2023 14:43:56 +0000 Subject: [PATCH] feat(split): Add peripheral is bonded/connected to central For easy use by displays and indicators it would be helpful to be able to see if any peripheral at an index is connected and bonded --- app/include/zmk/ble.h | 1 + app/include/zmk/split/bluetooth/central.h | 4 +++- app/src/ble.c | 8 ++++++++ app/src/split/bluetooth/central.c | 16 ++++++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/app/include/zmk/ble.h b/app/include/zmk/ble.h index 773323c1..f84fc19a 100644 --- a/app/include/zmk/ble.h +++ b/app/include/zmk/ble.h @@ -38,4 +38,5 @@ int zmk_ble_unpair_all(void); #if IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) int zmk_ble_put_peripheral_addr(const bt_addr_le_t *addr); +bt_addr_le_t *zmk_ble_get_peripheral_addr(uint8_t index); #endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) */ diff --git a/app/include/zmk/split/bluetooth/central.h b/app/include/zmk/split/bluetooth/central.h index 5e9e09ff..7c2c3f94 100644 --- a/app/include/zmk/split/bluetooth/central.h +++ b/app/include/zmk/split/bluetooth/central.h @@ -21,4 +21,6 @@ int zmk_split_bt_update_hid_indicator(zmk_hid_indicators_t indicators); int zmk_split_get_peripheral_battery_level(uint8_t source, uint8_t *level); -#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_CENTRAL_BATTERY_LEVEL_FETCHING) \ No newline at end of file +#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_CENTRAL_BATTERY_LEVEL_FETCHING) +bool zmk_split_bt_central_peripheral_is_connected(uint8_t index); +bool zmk_split_bt_central_peripheral_is_bonded(uint8_t index); diff --git a/app/src/ble.c b/app/src/ble.c index 7e1ae7d4..ef099910 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -357,6 +357,14 @@ int zmk_ble_put_peripheral_addr(const bt_addr_le_t *addr) { return -ENOMEM; } +bt_addr_le_t *zmk_ble_get_peripheral_addr(uint8_t index) { + if (index < ZMK_SPLIT_BLE_PERIPHERAL_COUNT) { + return &peripheral_addrs[index]; + } + // Peripheral index out of range + return (bt_addr_le_t *)BT_ADDR_LE_NONE; +} + #endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) */ #if IS_ENABLED(CONFIG_SETTINGS) diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c index ee21a12f..62c6df16 100644 --- a/app/src/split/bluetooth/central.c +++ b/app/src/split/bluetooth/central.c @@ -743,6 +743,22 @@ static struct bt_conn_cb conn_callbacks = { .disconnected = split_central_disconnected, }; +bool zmk_split_bt_central_peripheral_is_connected(uint8_t index) { + // If index is out of range always not connected + if (index >= ZMK_SPLIT_BLE_PERIPHERAL_COUNT) + return false; + else + return (peripherals[index].state == PERIPHERAL_SLOT_STATE_CONNECTED); +} + +bool zmk_split_bt_central_peripheral_is_bonded(uint8_t index) { + // If index is out of range always not bonded + if (index >= ZMK_SPLIT_BLE_PERIPHERAL_COUNT) + return false; + else + return (bt_addr_le_cmp(zmk_ble_get_peripheral_addr(index), BT_ADDR_LE_ANY) == 0); +} + K_THREAD_STACK_DEFINE(split_central_split_run_q_stack, CONFIG_ZMK_SPLIT_BLE_CENTRAL_SPLIT_RUN_STACK_SIZE);