From cf970efb98c5af97955bfffbcebb3b065b16edc4 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Fri, 28 Aug 2020 14:15:16 -0400 Subject: feat(bluetooth): Proper bond management, identity support for non-splits * Add `bt` behavior that can be used to perform certain actions, such as next/prev identity, reset identity, etc. NOTE: Multiple identities is only supported for non-split shields, due to missing Zephyr identity functionality for dual central/peripheral devices. * Proper bond reset tied to action, that honors peripheral bonds, so folks can reset and pair to other hosts, without breaking bonds between splt halves. --- app/src/split/bluetooth/central.c | 49 ++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 16 deletions(-) (limited to 'app/src/split') diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c index 9e67228..df8f34e 100644 --- a/app/src/split/bluetooth/central.c +++ b/app/src/split/bluetooth/central.c @@ -10,12 +10,14 @@ #include #include #include +#include #include #include LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); +#include #include #include #include @@ -71,6 +73,18 @@ static u8_t split_central_notify_func(struct bt_conn *conn, return BT_GATT_ITER_CONTINUE; } +static int split_central_subscribe(struct bt_conn *conn) +{ + int err = bt_gatt_subscribe(conn, &subscribe_params); + if (err && err != -EALREADY) { + LOG_ERR("Subscribe failed (err %d)", err); + } else { + LOG_DBG("[SUBSCRIBED]"); + } + + return 0; +} + static u8_t split_central_discovery_func(struct bt_conn *conn, const struct bt_gatt_attr *attr, struct bt_gatt_discover_params *params) @@ -112,12 +126,7 @@ static u8_t split_central_discovery_func(struct bt_conn *conn, subscribe_params.value = BT_GATT_CCC_NOTIFY; subscribe_params.ccc_handle = attr->handle; - err = bt_gatt_subscribe(conn, &subscribe_params); - if (err && err != -EALREADY) { - LOG_ERR("Subscribe failed (err %d)", err); - } else { - LOG_DBG("[SUBSCRIBED]"); - } + split_central_subscribe(conn); return BT_GATT_ITER_STOP; } @@ -137,16 +146,20 @@ static void split_central_process_connection(struct bt_conn *conn) { } if (conn == default_conn) { - discover_params.uuid = &uuid.uuid; - discover_params.func = split_central_discovery_func; - discover_params.start_handle = 0x0001; - discover_params.end_handle = 0xffff; - discover_params.type = BT_GATT_DISCOVER_PRIMARY; + if (subscribe_params.value) { + split_central_subscribe(conn); + } else { + discover_params.uuid = &uuid.uuid; + discover_params.func = split_central_discovery_func; + discover_params.start_handle = 0x0001; + discover_params.end_handle = 0xffff; + discover_params.type = BT_GATT_DISCOVER_PRIMARY; - err = bt_gatt_discover(default_conn, &discover_params); - if (err) { - LOG_ERR("Discover failed(err %d)", err); - return; + err = bt_gatt_discover(default_conn, &discover_params); + if (err) { + LOG_ERR("Discover failed(err %d)", err); + return; + } } } @@ -194,6 +207,8 @@ static bool split_central_eir_found(struct bt_data *data, void *user_data) LOG_DBG("Found the split service"); + zmk_ble_set_peripheral_addr(addr); + err = bt_le_scan_stop(); if (err) { LOG_ERR("Stop LE scan failed (err %d)", err); @@ -206,10 +221,11 @@ static bool split_central_eir_found(struct bt_data *data, void *user_data) split_central_process_connection(default_conn); } else { param = BT_LE_CONN_PARAM(0x0006, 0x0006, 30, 400); + err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, param, &default_conn); if (err) { - LOG_ERR("Create conn failed (err %d)", err); + LOG_ERR("Create conn failed (err %d) (create conn? 0x%04x)", err, BT_HCI_OP_LE_CREATE_CONN); start_scan(); } @@ -263,6 +279,7 @@ static void split_central_connected(struct bt_conn *conn, u8_t conn_err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); + if (conn_err) { LOG_ERR("Failed to connect to %s (%u)", addr, conn_err); -- cgit v1.2.3