diff options
author | Pete Johanson <peter@peterjohanson.com> | 2020-08-28 14:15:16 -0400 |
---|---|---|
committer | Pete Johanson <peter@peterjohanson.com> | 2020-09-13 22:33:05 -0400 |
commit | cf970efb98c5af97955bfffbcebb3b065b16edc4 (patch) | |
tree | a44d5b05e62bf7bdd00d4e4bae38617a411a89f5 /app/src/split/bluetooth/central.c | |
parent | 304603240f7ba16f67912a0031c64fb9ae4e8279 (diff) |
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.
Diffstat (limited to 'app/src/split/bluetooth/central.c')
-rw-r--r-- | app/src/split/bluetooth/central.c | 49 |
1 files changed, 33 insertions, 16 deletions
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 <bluetooth/conn.h> #include <bluetooth/uuid.h> #include <bluetooth/gatt.h> +#include <bluetooth/hci.h> #include <sys/byteorder.h> #include <logging/log.h> LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); +#include <zmk/ble.h> #include <zmk/split/bluetooth/uuid.h> #include <zmk/event-manager.h> #include <zmk/events/position-state-changed.h> @@ -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); |