summaryrefslogtreecommitdiff
path: root/app/src
diff options
context:
space:
mode:
authorPeter Johanson <peter@peterjohanson.com>2021-10-17 02:27:10 +0000
committerPete Johanson <peter@peterjohanson.com>2021-11-04 22:31:56 -0400
commit0b0b49d62666cfec2157900fd0dbf62460aa33f9 (patch)
tree85b6f0cc7f962e6317c6d3fcb7c058d92b148205 /app/src
parent73fd4b9230bec4333fc3ecc74e455af78016f0e2 (diff)
fix(split): Properly rediscover peripherals.
* Handle disconnects of peripherals, and properly clean up and resume discovering for when the peripheral re-appears.
Diffstat (limited to 'app/src')
-rw-r--r--app/src/split/bluetooth/central.c88
1 files changed, 51 insertions, 37 deletions
diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c
index a56b0b8..9a7f01b 100644
--- a/app/src/split/bluetooth/central.c
+++ b/app/src/split/bluetooth/central.c
@@ -29,9 +29,10 @@ static int start_scan(void);
static struct bt_conn *default_conn;
-static struct bt_uuid_128 uuid = BT_UUID_INIT_128(ZMK_SPLIT_BT_SERVICE_UUID);
+static const struct bt_uuid_128 split_service_uuid = BT_UUID_INIT_128(ZMK_SPLIT_BT_SERVICE_UUID);
static struct bt_gatt_discover_params discover_params;
static struct bt_gatt_subscribe_params subscribe_params;
+static struct bt_gatt_discover_params sub_discover_params;
K_MSGQ_DEFINE(peripheral_event_msgq, sizeof(struct zmk_position_state_changed),
CONFIG_ZMK_SPLIT_BLE_CENTRAL_POSITION_QUEUE_SIZE, 4);
@@ -83,15 +84,12 @@ static uint8_t split_central_notify_func(struct bt_conn *conn,
return BT_GATT_ITER_CONTINUE;
}
-static int split_central_subscribe(struct bt_conn *conn) {
+static void split_central_subscribe(struct bt_conn *conn) {
int err = bt_gatt_subscribe(conn, &subscribe_params);
switch (err) {
case -EALREADY:
LOG_DBG("[ALREADY SUBSCRIBED]");
break;
- // break;
- // bt_gatt_unsubscribe(conn, &subscribe_params);
- // return split_central_subscribe(conn);
case 0:
LOG_DBG("[SUBSCRIBED]");
break;
@@ -99,54 +97,67 @@ static int split_central_subscribe(struct bt_conn *conn) {
LOG_ERR("Subscribe failed (err %d)", err);
break;
}
-
- return 0;
}
-static uint8_t split_central_discovery_func(struct bt_conn *conn, const struct bt_gatt_attr *attr,
- struct bt_gatt_discover_params *params) {
- int err;
-
+static uint8_t split_central_chrc_discovery_func(struct bt_conn *conn,
+ const struct bt_gatt_attr *attr,
+ struct bt_gatt_discover_params *params) {
if (!attr) {
LOG_DBG("Discover complete");
- (void)memset(params, 0, sizeof(*params));
+ return BT_GATT_ITER_STOP;
+ }
+
+ if (!attr->user_data) {
+ LOG_ERR("Required user data not passed to discovery");
return BT_GATT_ITER_STOP;
}
LOG_DBG("[ATTRIBUTE] handle %u", attr->handle);
- if (!bt_uuid_cmp(discover_params.uuid, BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID))) {
- memcpy(&uuid, BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID), sizeof(uuid));
- discover_params.uuid = &uuid.uuid;
- discover_params.start_handle = attr->handle + 1;
+ if (!bt_uuid_cmp(((struct bt_gatt_chrc *)attr->user_data)->uuid,
+ BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID))) {
+ LOG_DBG("Found position state characteristic");
+ discover_params.uuid = NULL;
+ discover_params.start_handle = attr->handle + 2;
discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC;
- err = bt_gatt_discover(conn, &discover_params);
- if (err) {
- LOG_ERR("Discover failed (err %d)", err);
- }
- } else if (!bt_uuid_cmp(discover_params.uuid,
- BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID))) {
- memcpy(&uuid, BT_UUID_GATT_CCC, sizeof(uuid));
- discover_params.uuid = &uuid.uuid;
- discover_params.start_handle = attr->handle + 2;
- discover_params.type = BT_GATT_DISCOVER_DESCRIPTOR;
+ subscribe_params.disc_params = &sub_discover_params;
+ subscribe_params.end_handle = discover_params.end_handle;
subscribe_params.value_handle = bt_gatt_attr_value_handle(attr);
-
- err = bt_gatt_discover(conn, &discover_params);
- if (err) {
- LOG_ERR("Discover failed (err %d)", err);
- }
- } else {
subscribe_params.notify = split_central_notify_func;
subscribe_params.value = BT_GATT_CCC_NOTIFY;
- subscribe_params.ccc_handle = attr->handle;
-
split_central_subscribe(conn);
+ }
+
+ return subscribe_params.value_handle ? BT_GATT_ITER_STOP : BT_GATT_ITER_CONTINUE;
+}
+static uint8_t split_central_service_discovery_func(struct bt_conn *conn,
+ const struct bt_gatt_attr *attr,
+ struct bt_gatt_discover_params *params) {
+ if (!attr) {
+ LOG_DBG("Discover complete");
+ (void)memset(params, 0, sizeof(*params));
return BT_GATT_ITER_STOP;
}
+ LOG_DBG("[ATTRIBUTE] handle %u", attr->handle);
+
+ if (bt_uuid_cmp(discover_params.uuid, BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID))) {
+ LOG_DBG("Found other service");
+ return BT_GATT_ITER_CONTINUE;
+ }
+
+ LOG_DBG("Found split service");
+ discover_params.uuid = NULL;
+ discover_params.func = split_central_chrc_discovery_func;
+ discover_params.start_handle = attr->handle + 1;
+ discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC;
+
+ int err = bt_gatt_discover(conn, &discover_params);
+ if (err) {
+ LOG_ERR("Failed to start discovering split service characteristics (err %d)", err);
+ }
return BT_GATT_ITER_STOP;
}
@@ -161,9 +172,9 @@ static void split_central_process_connection(struct bt_conn *conn) {
return;
}
- if (conn == default_conn && !subscribe_params.value) {
- discover_params.uuid = &uuid.uuid;
- discover_params.func = split_central_discovery_func;
+ if (conn == default_conn && !subscribe_params.value_handle) {
+ discover_params.uuid = &split_service_uuid.uuid;
+ discover_params.func = split_central_service_discovery_func;
discover_params.start_handle = 0x0001;
discover_params.end_handle = 0xffff;
discover_params.type = BT_GATT_DISCOVER_PRIMARY;
@@ -318,6 +329,9 @@ static void split_central_disconnected(struct bt_conn *conn, uint8_t reason) {
bt_conn_unref(default_conn);
default_conn = NULL;
+ // Clean up previously discovered handles;
+ subscribe_params.value_handle = 0;
+
start_scan();
}