Browse Source

fix(split): Properly rediscover peripherals.

* Handle disconnects of peripherals, and properly clean up
  and resume discovering for when the peripheral re-appears.
xmkb
Peter Johanson 3 years ago committed by Pete Johanson
parent
commit
0b0b49d626
  1. 1
      app/Kconfig
  2. 88
      app/src/split/bluetooth/central.c

1
app/Kconfig

@ -167,6 +167,7 @@ menuconfig ZMK_SPLIT_BLE_ROLE_CENTRAL
bool "Central" bool "Central"
select BT_CENTRAL select BT_CENTRAL
select BT_GATT_CLIENT select BT_GATT_CLIENT
select BT_GATT_AUTO_DISCOVER_CCC
if ZMK_SPLIT_BLE_ROLE_CENTRAL if ZMK_SPLIT_BLE_ROLE_CENTRAL

88
app/src/split/bluetooth/central.c

@ -29,9 +29,10 @@ static int start_scan(void);
static struct bt_conn *default_conn; 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_discover_params discover_params;
static struct bt_gatt_subscribe_params subscribe_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), K_MSGQ_DEFINE(peripheral_event_msgq, sizeof(struct zmk_position_state_changed),
CONFIG_ZMK_SPLIT_BLE_CENTRAL_POSITION_QUEUE_SIZE, 4); 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; 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); int err = bt_gatt_subscribe(conn, &subscribe_params);
switch (err) { switch (err) {
case -EALREADY: case -EALREADY:
LOG_DBG("[ALREADY SUBSCRIBED]"); LOG_DBG("[ALREADY SUBSCRIBED]");
break; break;
// break;
// bt_gatt_unsubscribe(conn, &subscribe_params);
// return split_central_subscribe(conn);
case 0: case 0:
LOG_DBG("[SUBSCRIBED]"); LOG_DBG("[SUBSCRIBED]");
break; break;
@ -99,54 +97,67 @@ static int split_central_subscribe(struct bt_conn *conn) {
LOG_ERR("Subscribe failed (err %d)", err); LOG_ERR("Subscribe failed (err %d)", err);
break; break;
} }
return 0;
} }
static uint8_t split_central_discovery_func(struct bt_conn *conn, const struct bt_gatt_attr *attr, static uint8_t split_central_chrc_discovery_func(struct bt_conn *conn,
struct bt_gatt_discover_params *params) { const struct bt_gatt_attr *attr,
int err; struct bt_gatt_discover_params *params) {
if (!attr) { if (!attr) {
LOG_DBG("Discover complete"); 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; return BT_GATT_ITER_STOP;
} }
LOG_DBG("[ATTRIBUTE] handle %u", attr->handle); LOG_DBG("[ATTRIBUTE] handle %u", attr->handle);
if (!bt_uuid_cmp(discover_params.uuid, BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID))) { if (!bt_uuid_cmp(((struct bt_gatt_chrc *)attr->user_data)->uuid,
memcpy(&uuid, BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID), sizeof(uuid)); BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID))) {
discover_params.uuid = &uuid.uuid; LOG_DBG("Found position state characteristic");
discover_params.start_handle = attr->handle + 1; discover_params.uuid = NULL;
discover_params.start_handle = attr->handle + 2;
discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC; discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC;
err = bt_gatt_discover(conn, &discover_params); subscribe_params.disc_params = &sub_discover_params;
if (err) { subscribe_params.end_handle = discover_params.end_handle;
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.value_handle = bt_gatt_attr_value_handle(attr); 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.notify = split_central_notify_func;
subscribe_params.value = BT_GATT_CCC_NOTIFY; subscribe_params.value = BT_GATT_CCC_NOTIFY;
subscribe_params.ccc_handle = attr->handle;
split_central_subscribe(conn); 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; 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; return BT_GATT_ITER_STOP;
} }
@ -161,9 +172,9 @@ static void split_central_process_connection(struct bt_conn *conn) {
return; return;
} }
if (conn == default_conn && !subscribe_params.value) { if (conn == default_conn && !subscribe_params.value_handle) {
discover_params.uuid = &uuid.uuid; discover_params.uuid = &split_service_uuid.uuid;
discover_params.func = split_central_discovery_func; discover_params.func = split_central_service_discovery_func;
discover_params.start_handle = 0x0001; discover_params.start_handle = 0x0001;
discover_params.end_handle = 0xffff; discover_params.end_handle = 0xffff;
discover_params.type = BT_GATT_DISCOVER_PRIMARY; 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); bt_conn_unref(default_conn);
default_conn = NULL; default_conn = NULL;
// Clean up previously discovered handles;
subscribe_params.value_handle = 0;
start_scan(); start_scan();
} }

Loading…
Cancel
Save