Bluetooth: CSIS: Refactor bt_csis_client_discover_sets to use member

Refactor bt_csis_client_discover_sets to use the
bt_csis_client_set_member struct instead of a bt_conn.
The bt_csis_client_set_member represents a remote server
(set member), and make it possible to avoid sending indexes
of instances around instead of bt_csis.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
This commit is contained in:
Emil Gydesen 2021-11-23 13:32:04 +01:00 committed by Anas Nashif
commit fa1be6436a
4 changed files with 49 additions and 71 deletions

View file

@ -218,25 +218,24 @@ typedef void (*bt_csis_client_discover_cb)(struct bt_csis_client_set_member *mem
* @brief Initialise the csis_client instance for a connection. This will do a * @brief Initialise the csis_client instance for a connection. This will do a
* discovery on the device and prepare the instance for following commands. * discovery on the device and prepare the instance for following commands.
* *
* @param member Pointer to a set member struct to store discovery results in * @param member Pointer to a set member struct to store discovery results in.
* *
* @return int Return 0 on success, or an errno value on error. * @return int Return 0 on success, or an errno value on error.
*/ */
int bt_csis_client_discover(struct bt_csis_client_set_member *member); int bt_csis_client_discover(struct bt_csis_client_set_member *member);
typedef void (*bt_csis_client_discover_sets_cb)(struct bt_conn *conn, typedef void (*bt_csis_client_discover_sets_cb)(struct bt_csis_client_set_member *member,
int err, uint8_t set_count, int err, uint8_t set_count);
struct bt_csis_client_set *sets);
/** /**
* @brief Reads CSIS characteristics from a device, to find more information * @brief Reads CSIS characteristics from a device, to find more information
* about the set(s) that the device is part of. * about the set(s) that the device is part of.
* *
* @param conn The connection to the device to read CSIS characteristics * @param member Pointer to a set member struct to store discovery results in.
* *
* @return int Return 0 on success, or an errno value on error. * @return int Return 0 on success, or an errno value on error.
*/ */
int bt_csis_client_discover_sets(struct bt_conn *conn); int bt_csis_client_discover_sets(struct bt_csis_client_set_member *member);
typedef void (*bt_csis_client_lock_set_cb)(int err); typedef void (*bt_csis_client_lock_set_cb)(int err);

View file

@ -68,7 +68,7 @@ static struct bt_uuid_16 uuid = BT_UUID_INIT_16(0);
static struct bt_csis_client_cb *csis_client_cbs; static struct bt_csis_client_cb *csis_client_cbs;
static struct bt_csis_client_inst client_insts[CONFIG_BT_MAX_CONN]; static struct bt_csis_client_inst client_insts[CONFIG_BT_MAX_CONN];
static int read_set_sirk(struct bt_conn *conn, uint8_t inst_idx); static int read_set_sirk(struct bt_csis *csis);
static int csis_client_read_set_size(struct bt_conn *conn, uint8_t inst_idx, static int csis_client_read_set_size(struct bt_conn *conn, uint8_t inst_idx,
bt_gatt_read_func_t cb); bt_gatt_read_func_t cb);
@ -353,34 +353,27 @@ static int csis_client_write_set_lock(struct bt_csis *inst,
return bt_gatt_write(inst->cli.conn, &write_params); return bt_gatt_write(inst->cli.conn, &write_params);
} }
static int read_set_sirk(struct bt_conn *conn, uint8_t inst_idx) static int read_set_sirk(struct bt_csis *csis)
{ {
if (inst_idx >= CONFIG_BT_CSIS_CLIENT_MAX_CSIS_INSTANCES) { if (cur_inst != NULL) {
return -EINVAL; if (cur_inst != csis) {
} else if (cur_inst != NULL) {
if (cur_inst != lookup_instance_by_index(conn, inst_idx)) {
return -EBUSY; return -EBUSY;
} }
} else { } else {
cur_inst = lookup_instance_by_index(conn, inst_idx); cur_inst = csis;
if (cur_inst == NULL) {
BT_DBG("Inst not found");
return -EINVAL;
}
} }
if (cur_inst->cli.set_sirk_handle == 0) { if (csis->cli.set_sirk_handle == 0) {
BT_DBG("Handle not set"); BT_DBG("Handle not set");
cur_inst = NULL;
return -EINVAL; return -EINVAL;
} }
read_params.func = csis_client_discover_sets_read_set_sirk_cb; read_params.func = csis_client_discover_sets_read_set_sirk_cb;
read_params.handle_count = 1; read_params.handle_count = 1;
read_params.single.handle = cur_inst->cli.set_sirk_handle; read_params.single.handle = csis->cli.set_sirk_handle;
read_params.single.offset = 0U; read_params.single.offset = 0U;
return bt_gatt_read(conn, &read_params); return bt_gatt_read(csis->cli.conn, &read_params);
} }
static int csis_client_read_set_size(struct bt_conn *conn, uint8_t inst_idx, static int csis_client_read_set_size(struct bt_conn *conn, uint8_t inst_idx,
@ -666,9 +659,8 @@ static uint8_t csis_client_discover_sets_read_rank_cb(struct bt_conn *conn,
if (cb_err != 0) { if (cb_err != 0) {
if (csis_client_cbs != NULL && csis_client_cbs->sets != NULL) { if (csis_client_cbs != NULL && csis_client_cbs->sets != NULL) {
csis_client_cbs->sets(conn, cb_err, csis_client_cbs->sets(client->set_member, cb_err,
client->inst_count, client->inst_count);
client->set_member->sets);
} }
} }
@ -709,9 +701,8 @@ static uint8_t csis_client_discover_sets_read_set_size_cb(struct bt_conn *conn,
if (cb_err != 0) { if (cb_err != 0) {
if (csis_client_cbs != NULL && csis_client_cbs->sets != NULL) { if (csis_client_cbs != NULL && csis_client_cbs->sets != NULL) {
csis_client_cbs->sets(conn, cb_err, csis_client_cbs->sets(client->set_member, cb_err,
client->inst_count, client->inst_count);
client->set_member->sets);
} }
} }
@ -797,9 +788,8 @@ static uint8_t csis_client_discover_sets_read_set_sirk_cb(struct bt_conn *conn,
if (cb_err != 0) { if (cb_err != 0) {
if (csis_client_cbs != NULL && csis_client_cbs->sets != NULL) { if (csis_client_cbs != NULL && csis_client_cbs->sets != NULL) {
csis_client_cbs->sets(conn, cb_err, csis_client_cbs->sets(client->set_member, cb_err,
client->inst_count, client->inst_count);
client->set_member->sets);
} }
} }
@ -841,12 +831,14 @@ static void discover_sets_resume(struct bt_conn *conn, uint16_t sirk_handle,
cur_inst = NULL; cur_inst = NULL;
if (next_idx < client->inst_count) { if (next_idx < client->inst_count) {
cur_inst = lookup_instance_by_index(conn, next_idx);
/* Read next */ /* Read next */
cb_err = read_set_sirk(conn, next_idx); cb_err = read_set_sirk(cur_inst);
} else if (csis_client_cbs != NULL && } else if (csis_client_cbs != NULL &&
csis_client_cbs->sets != NULL) { csis_client_cbs->sets != NULL) {
csis_client_cbs->sets(conn, 0, client->inst_count, csis_client_cbs->sets(client->set_member, 0,
client->set_member->sets); client->inst_count);
} }
return; return;
@ -854,9 +846,8 @@ static void discover_sets_resume(struct bt_conn *conn, uint16_t sirk_handle,
if (cb_err != 0) { if (cb_err != 0) {
if (csis_client_cbs != NULL && csis_client_cbs->sets != NULL) { if (csis_client_cbs != NULL && csis_client_cbs->sets != NULL) {
csis_client_cbs->sets(conn, cb_err, csis_client_cbs->sets(client->set_member, cb_err,
client->inst_count, client->inst_count);
client->set_member->sets);
} }
} else { } else {
busy = true; busy = true;
@ -1177,19 +1168,24 @@ int bt_csis_client_discover(struct bt_csis_client_set_member *member)
return err; return err;
} }
int bt_csis_client_discover_sets(struct bt_conn *conn) int bt_csis_client_discover_sets(struct bt_csis_client_set_member *member)
{ {
int err; int err;
if (conn == NULL) { CHECKIF(member == NULL) {
BT_DBG("Not connected"); BT_DBG("member is NULL");
return -ENOTCONN; return -EINVAL;
}
if (member->conn == NULL) {
BT_DBG("member->conn is NULL");
return -EINVAL;
} else if (busy) { } else if (busy) {
return -EBUSY; return -EBUSY;
} }
/* Start reading values and call CB when done */ /* Start reading values and call CB when done */
err = read_set_sirk(conn, 0); err = read_set_sirk(member->sets[0].csis);
if (err == 0) { if (err == 0) {
busy = true; busy = true;
} }

View file

@ -100,9 +100,9 @@ static void csis_discover_cb(struct bt_csis_client_set_member *member, int err,
} }
} }
static void csis_client_discover_sets_cb(struct bt_conn *conn, int err, static void csis_client_discover_sets_cb(struct bt_csis_client_set_member *member,
uint8_t set_count, int err,
struct bt_csis_client_set *sets) uint8_t set_count)
{ {
if (err != 0) { if (err != 0) {
shell_error(ctx_shell, "Discover sets failed (%d)", err); shell_error(ctx_shell, "Discover sets failed (%d)", err);
@ -110,21 +110,10 @@ static void csis_client_discover_sets_cb(struct bt_conn *conn, int err,
} }
for (uint8_t i = 0; i < set_count; i++) { for (uint8_t i = 0; i < set_count; i++) {
struct bt_csis_client_set *set = &member->sets[i];
shell_print(ctx_shell, "Set size %d (pointer: %p)", shell_print(ctx_shell, "Set size %d (pointer: %p)",
sets[i].set_size, &sets[i]); set[i].set_size, &set[i]);
}
for (size_t i = 0; i < ARRAY_SIZE(set_members); i++) {
struct bt_csis_client_set_member *member;
member = &set_members[i];
if (member->conn == conn) {
for (uint8_t j = 0; j < set_count; j++) {
(void)memcpy(&member->sets[j], &sets[j],
sizeof(sets[j]));
}
}
} }
} }
@ -302,7 +291,7 @@ static int cmd_csis_client_discover_sets(const struct shell *sh, size_t argc,
} }
} }
err = bt_csis_client_discover_sets(set_members[member_index].conn); err = bt_csis_client_discover_sets(&set_members[member_index]);
if (err != 0) { if (err != 0) {
shell_error(sh, "Fail: %d", err); shell_error(sh, "Fail: %d", err);
} }

View file

@ -47,9 +47,9 @@ static void csis_client_lock_set_cb(int err)
set_locked = true; set_locked = true;
} }
static void csis_client_discover_sets_cb(struct bt_conn *conn, int err, static void csis_client_discover_sets_cb(struct bt_csis_client_set_member *member,
uint8_t set_count, int err,
struct bt_csis_client_set *sets) uint8_t set_count)
{ {
printk("%s\n", __func__); printk("%s\n", __func__);
@ -58,13 +58,7 @@ static void csis_client_discover_sets_cb(struct bt_conn *conn, int err,
return; return;
} }
for (uint8_t i = 0; i < set_count; i++) { set = &member->sets[0];
printk("Set %u: size %d\n", i, sets[i].set_size);
memcpy(&set_members[bt_conn_index(conn)].sets[i], &sets[i],
sizeof(sets[i]));
}
set = sets;
sets_discovered = true; sets_discovered = true;
} }
@ -242,7 +236,7 @@ static void test_main(void)
WAIT_FOR(discovered); WAIT_FOR(discovered);
err = bt_csis_client_discover_sets(set_members[0].conn); err = bt_csis_client_discover_sets(&set_members[0]);
if (err != 0) { if (err != 0) {
FAIL("Failed to do CSIS client discovery sets (%d)\n", err); FAIL("Failed to do CSIS client discovery sets (%d)\n", err);
return; return;
@ -303,7 +297,7 @@ static void test_main(void)
sets_discovered = false; sets_discovered = false;
printk("Doing sets discovery on member[%u]", i); printk("Doing sets discovery on member[%u]", i);
err = bt_csis_client_discover_sets(set_members[i].conn); err = bt_csis_client_discover_sets(&set_members[i]);
if (err != 0) { if (err != 0) {
FAIL("Failed to do CSIS client discovery sets (%d)\n", err); FAIL("Failed to do CSIS client discovery sets (%d)\n", err);
return; return;