summaryrefslogtreecommitdiff
path: root/app/src/ble.c
diff options
context:
space:
mode:
authorJoel Spadin <joelspadin@gmail.com>2020-10-24 13:39:02 -0500
committerJoel Spadin <joelspadin@gmail.com>2020-10-28 18:15:05 -0500
commitb538e605321791568aa25f0bd534ebcb30da92c2 (patch)
tree7e6ec61bf59a37255f8544bb4b73509c2c370074 /app/src/ble.c
parent8f666cecc9717b3fbbe2f5f34375b6a45b8965fd (diff)
feat(endpoints): update on BLE profile change
Added zmk_ble_active_profile_is_connected() to allow code outside ble.c to check the status of the active profile, and changed the ble_active_profile_changed event to also notify when the active profile connects or disconnects. Changed endpoint selection to to also update when the active profile changes, connects, or disconnects.
Diffstat (limited to 'app/src/ble.c')
-rw-r--r--app/src/ble.c26
1 files changed, 23 insertions, 3 deletions
diff --git a/app/src/ble.c b/app/src/ble.c
index 9090582..a79f723 100644
--- a/app/src/ble.c
+++ b/app/src/ble.c
@@ -94,6 +94,12 @@ static void raise_profile_changed_event() {
ZMK_EVENT_RAISE(ev);
}
+static void raise_profile_changed_event_callback(struct k_work *work) {
+ raise_profile_changed_event();
+}
+
+K_WORK_DEFINE(raise_profile_changed_event_work, raise_profile_changed_event_callback);
+
static bool active_profile_is_open() {
return !bt_addr_le_cmp(&profiles[active_profile].peer, BT_ADDR_LE_ANY);
}
@@ -111,7 +117,7 @@ void set_profile_address(u8_t index, const bt_addr_le_t *addr) {
raise_profile_changed_event();
}
-bool active_profile_is_connected() {
+bool zmk_ble_active_profile_is_connected() {
struct bt_conn *conn;
bt_addr_le_t *addr = zmk_ble_active_profile_addr();
if (!bt_addr_le_cmp(addr, BT_ADDR_LE_ANY)) {
@@ -163,9 +169,9 @@ int update_advertising() {
struct bt_conn *conn;
enum advertising_type desired_adv = ZMK_ADV_NONE;
- if (active_profile_is_open() || !active_profile_is_connected()) {
+ if (active_profile_is_open() || !zmk_ble_active_profile_is_connected()) {
desired_adv = ZMK_ADV_CONN;
- } else if (!active_profile_is_connected()) {
+ } else if (!zmk_ble_active_profile_is_connected()) {
desired_adv = ZMK_ADV_CONN;
// Need to fix directed advertising for privacy centrals. See
// https://github.com/zephyrproject-rtos/zephyr/pull/14984 char
@@ -327,6 +333,10 @@ static int ble_profiles_handle_set(const char *name, size_t len, settings_read_c
struct settings_handler profiles_handler = {.name = "ble", .h_set = ble_profiles_handle_set};
#endif /* IS_ENABLED(CONFIG_SETTINGS) */
+static bool is_conn_active_profile(const struct bt_conn *conn) {
+ return bt_addr_le_cmp(bt_conn_get_dst(conn), &profiles[active_profile].peer) == 0;
+}
+
static void connected(struct bt_conn *conn, u8_t err) {
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
@@ -352,6 +362,11 @@ static void connected(struct bt_conn *conn, u8_t err) {
}
update_advertising();
+
+ if (is_conn_active_profile(conn)) {
+ LOG_DBG("Active profile connected");
+ raise_profile_changed_event();
+ }
}
static void disconnected(struct bt_conn *conn, u8_t reason) {
@@ -364,6 +379,11 @@ static void disconnected(struct bt_conn *conn, u8_t reason) {
// We need to do this in a work callback, otherwise the advertising update will still see the
// connection for a profile as active, and not start advertising yet.
k_work_submit(&update_advertising_work);
+
+ if (is_conn_active_profile(conn)) {
+ LOG_DBG("Active profile disconnected");
+ k_work_submit(&raise_profile_changed_event_work);
+ }
}
static void security_changed(struct bt_conn *conn, bt_security_t level, enum bt_security_err err) {