Skip to content

Commit

Permalink
refactor(split): Clean up split GATT discovery.
Browse files Browse the repository at this point in the history
* Use Zephyr auto CCC discovery instead of doing it ourselves.
* Split service versus characteristic discovery into dedicated
  steps in the flow.
* Fix for not searching properly when connecting to a peripheral
  a second time.
  • Loading branch information
petejohanson committed Jan 12, 2021
1 parent 7af2e6e commit 58a4233
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 48 deletions.
1 change: 1 addition & 0 deletions app/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ config ZMK_SPLIT_BLE_ROLE_CENTRAL
bool "Central"
select BT_CENTRAL
select BT_GATT_CLIENT
select BT_GATT_AUTO_DISCOVER_CCC

if !ZMK_SPLIT_BLE_ROLE_CENTRAL

Expand Down
105 changes: 57 additions & 48 deletions app/src/split/bluetooth/central.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,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 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;
static uint16_t run_behavior_handle;

static uint8_t split_central_notify_func(struct bt_conn *conn,
Expand Down Expand Up @@ -76,81 +77,85 @@ 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;
default:
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;
}

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 (attr->user_data &&
!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 if (!bt_uuid_cmp(discover_params.uuid,
BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_RUN_BEHAVIOR_UUID))) {
run_behavior_handle = bt_gatt_attr_value_handle(attr);
} 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);
} else if (attr->user_data &&
!bt_uuid_cmp(((struct bt_gatt_chrc *)attr->user_data)->uuid,
BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_RUN_BEHAVIOR_UUID))) {
LOG_DBG("Found run behavior handle");
run_behavior_handle = bt_gatt_attr_value_handle(attr);
}

memcpy(&uuid, BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_RUN_BEHAVIOR_UUID), sizeof(uuid));
discover_params.uuid = &uuid.uuid;
discover_params.start_handle = attr->handle + 1;
discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC;
bool subscribed = (run_behavior_handle && subscribe_params.value_handle);

err = bt_gatt_discover(conn, &discover_params);
if (err) {
LOG_ERR("Discover failed (err %d)", err);
}
return subscribed ? 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) {
int err;

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;

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;
}

Expand All @@ -165,9 +170,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;
Expand Down Expand Up @@ -322,6 +327,10 @@ 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;
run_behavior_handle = 0;

start_scan();
}

Expand Down

0 comments on commit 58a4233

Please sign in to comment.