From fe91cc6625ac6655abed4e8b4453ed7436bc34f9 Mon Sep 17 00:00:00 2001 From: snoyer Date: Sat, 19 Jul 2025 14:51:53 +0100 Subject: [PATCH] refactor(ble): add functions to check if profile is open/connected by address (#2993) Helper functions for BLE profile statuses. --- app/include/zmk/ble.h | 3 +++ app/src/ble.c | 18 +++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/include/zmk/ble.h b/app/include/zmk/ble.h index 4e978b14b..92b2107d8 100644 --- a/app/include/zmk/ble.h +++ b/app/include/zmk/ble.h @@ -33,6 +33,9 @@ bt_addr_le_t *zmk_ble_profile_address(uint8_t index); bt_addr_le_t *zmk_ble_active_profile_addr(void); struct bt_conn *zmk_ble_active_profile_conn(void); +bool zmk_ble_profile_is_connected(uint8_t index); +bool zmk_ble_profile_is_open(uint8_t index); + bool zmk_ble_active_profile_is_open(void); bool zmk_ble_active_profile_is_connected(void); char *zmk_ble_active_profile_name(void); diff --git a/app/src/ble.c b/app/src/ble.c index c749c8e5a..2611eee5e 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -95,8 +95,13 @@ static void raise_profile_changed_event_callback(struct k_work *work) { K_WORK_DEFINE(raise_profile_changed_event_work, raise_profile_changed_event_callback); -bool zmk_ble_active_profile_is_open(void) { - return !bt_addr_le_cmp(&profiles[active_profile].peer, BT_ADDR_LE_ANY); +bool zmk_ble_active_profile_is_open(void) { return zmk_ble_profile_is_open(active_profile); } + +bool zmk_ble_profile_is_open(uint8_t index) { + if (index >= ZMK_BLE_PROFILE_COUNT) { + return false; + } + return !bt_addr_le_cmp(&profiles[index].peer, BT_ADDR_LE_ANY); } void set_profile_address(uint8_t index, const bt_addr_le_t *addr) { @@ -115,9 +120,16 @@ void set_profile_address(uint8_t index, const bt_addr_le_t *addr) { } bool zmk_ble_active_profile_is_connected(void) { + return zmk_ble_profile_is_connected(active_profile); +} + +bool zmk_ble_profile_is_connected(uint8_t index) { + if (index >= ZMK_BLE_PROFILE_COUNT) { + return false; + } struct bt_conn *conn; struct bt_conn_info info; - bt_addr_le_t *addr = zmk_ble_active_profile_addr(); + bt_addr_le_t *addr = &profiles[index].peer; if (!bt_addr_le_cmp(addr, BT_ADDR_LE_ANY)) { return false; } else if ((conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, addr)) == NULL) {