Bluetooth: sample: Update iso_connected_benchmark to use new ISO API

Update the sample to use the new ISO API.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
This commit is contained in:
Emil Gydesen 2021-08-20 10:44:24 +02:00 committed by Christopher Friedt
commit 76091850c5
2 changed files with 100 additions and 82 deletions

View file

@ -47,7 +47,7 @@ static enum benchmark_role role;
static struct bt_conn *default_conn;
static struct k_work_delayable iso_send_work;
static struct bt_iso_chan iso_chans[CONFIG_BT_ISO_MAX_CHAN];
static uint8_t cis_create_count = DEFAULT_CIS_COUNT;
static struct bt_iso_chan *cis[CONFIG_BT_ISO_MAX_CHAN];
static bool advertiser_found;
static bt_addr_le_t adv_addr;
static uint32_t last_received_counter;
@ -56,6 +56,7 @@ static struct iso_recv_stats stats_overall;
static int64_t iso_conn_start_time;
static size_t total_iso_conn_count;
static uint32_t iso_send_count;
static struct bt_iso_cig *cig;
NET_BUF_POOL_FIXED_DEFINE(tx_pool, 1, CONFIG_BT_ISO_TX_MTU, NULL);
static uint8_t iso_data[CONFIG_BT_ISO_TX_MTU - BT_ISO_CHAN_SEND_RESERVE];
@ -68,27 +69,30 @@ static K_SEM_DEFINE(sem_connected, 0, 1);
static K_SEM_DEFINE(sem_disconnected, 0, 1);
static struct bt_iso_chan_io_qos iso_tx_qos = {
.interval = DEFAULT_CIS_INTERVAL_US, /* in microseconds */
.latency = DEFAULT_CIS_LATENCY_MS, /* milliseconds */
.sdu = DEFAULT_CIS_SDU_SIZE, /* bytes */
.rtn = DEFAULT_CIS_RTN,
.phy = DEFAULT_CIS_PHY,
};
static struct bt_iso_chan_io_qos iso_rx_qos = {
.interval = DEFAULT_CIS_INTERVAL_US, /* in microseconds */
.latency = DEFAULT_CIS_LATENCY_MS, /* milliseconds */
.sdu = DEFAULT_CIS_SDU_SIZE, /* bytes */
.rtn = DEFAULT_CIS_RTN,
.phy = DEFAULT_CIS_PHY,
};
static struct bt_iso_chan_qos iso_qos = {
.tx = &iso_tx_qos,
.rx = &iso_rx_qos,
};
static struct bt_iso_cig_create_param cig_create_param = {
.interval = DEFAULT_CIS_INTERVAL_US, /* in microseconds */
.latency = DEFAULT_CIS_LATENCY_MS, /* milliseconds */
.sca = BT_GAP_SCA_UNKNOWN,
.packing = DEFAULT_CIS_PACKING,
.framing = DEFAULT_CIS_FRAMING,
.tx = &iso_tx_qos,
.rx = &iso_rx_qos,
.cis_channels = cis,
.num_cis = DEFAULT_CIS_COUNT
};
static enum benchmark_role device_role_select(void)
@ -146,9 +150,9 @@ static void iso_timer_timeout(struct k_work *work)
* calls `bt_iso_chan_send` but the controller only sending a single
* ISO packet.
*/
k_work_reschedule(&iso_send_work, K_USEC(iso_tx_qos.interval - 100));
k_work_reschedule(&iso_send_work, K_USEC(cig_create_param.interval - 100));
for (int i = 0; i < cis_create_count; i++) {
for (int i = 0; i < cig_create_param.num_cis; i++) {
buf = net_buf_alloc(&tx_pool, K_FOREVER);
if (buf == NULL) {
LOG_ERR("Could not allocate buffer");
@ -276,7 +280,7 @@ static int iso_accept(struct bt_conn *conn, struct bt_iso_chan **chan)
if (iso_chans[i].state == BT_ISO_DISCONNECTED) {
LOG_INF("Returning instance %d", i);
*chan = &iso_chans[i];
cis_create_count++;
cig_create_param.num_cis++;
k_sem_give(&sem_iso_accept);
return 0;
@ -458,14 +462,14 @@ static int parse_rtn_arg(struct bt_iso_chan_io_qos *qos)
return (int)rtn;
}
static int parse_interval_arg(struct bt_iso_chan_io_qos *qos)
static int parse_interval_arg(void)
{
char buffer[9];
size_t char_count;
uint64_t interval;
printk("Set interval (us) (current %u, default %u)\n",
qos->interval, DEFAULT_CIS_INTERVAL_US);
cig_create_param.interval, DEFAULT_CIS_INTERVAL_US);
char_count = get_chars(buffer, sizeof(buffer) - 1);
if (char_count == 0) {
@ -482,14 +486,14 @@ static int parse_interval_arg(struct bt_iso_chan_io_qos *qos)
return (int)interval;
}
static int parse_latency_arg(struct bt_iso_chan_io_qos *qos)
static int parse_latency_arg(void)
{
char buffer[6];
size_t char_count;
uint64_t latency;
printk("Set latency (ms) (current %u, default %u)\n",
qos->latency, DEFAULT_CIS_LATENCY_MS);
cig_create_param.latency, DEFAULT_CIS_LATENCY_MS);
char_count = get_chars(buffer, sizeof(buffer) - 1);
if (char_count == 0) {
@ -563,7 +567,7 @@ static int parse_cis_count_arg(void)
uint64_t cis_count;
printk("Set CIS count (current %u, default %u)\n",
cis_create_count, DEFAULT_CIS_COUNT);
cig_create_param.num_cis, DEFAULT_CIS_COUNT);
char_count = get_chars(buffer, sizeof(buffer) - 1);
if (char_count == 0) {
@ -579,11 +583,39 @@ static int parse_cis_count_arg(void)
return (int)cis_count;
}
static int parse_args(struct bt_iso_chan_io_qos *qos)
static int parse_cig_args(void)
{
int rtn;
int interval;
int latency;
int cis_count;
printk("Follow the prompts. Press enter to use default values.\n");
cis_count = parse_cis_count_arg();
if (cis_count < 0) {
return -EINVAL;
}
interval = parse_interval_arg();
if (interval < 0) {
return -EINVAL;
}
latency = parse_latency_arg();
if (latency < 0) {
return -EINVAL;
}
cig_create_param.interval = interval;
cig_create_param.latency = latency;
cig_create_param.num_cis = cis_count;
return 0;
}
static int parse_cis_args(struct bt_iso_chan_io_qos *qos)
{
int rtn;
int phy;
int sdu;
@ -594,16 +626,6 @@ static int parse_args(struct bt_iso_chan_io_qos *qos)
return -EINVAL;
}
interval = parse_interval_arg(qos);
if (interval < 0) {
return -EINVAL;
}
latency = parse_latency_arg(qos);
if (latency < 0) {
return -EINVAL;
}
phy = parse_phy_arg(qos);
if (phy < 0) {
return -EINVAL;
@ -615,8 +637,6 @@ static int parse_args(struct bt_iso_chan_io_qos *qos)
}
qos->rtn = rtn;
qos->interval = interval;
qos->latency = latency;
qos->phy = phy;
qos->sdu = sdu;
@ -628,10 +648,26 @@ static int change_central_settings(void)
char c;
int err;
printk("Change CIG settings (y/N)? (Current settings: cis_count=%u, "
"interval=%u, latency=%u)\n",
cig_create_param.num_cis, cig_create_param.interval,
cig_create_param.latency);
c = tolower(console_getchar());
if (c == 'y') {
err = parse_cig_args();
if (err != 0) {
return err;
}
printk("New settings: cis_count=%u, inteval=%u, latency=%u\n",
cig_create_param.num_cis, cig_create_param.interval,
cig_create_param.latency);
}
printk("Change TX settings (y/N)? (Current settings: rtn=%u, "
"interval=%u, latency=%u, phy=%u, sdu=%u)\n",
iso_tx_qos.rtn, iso_tx_qos.interval, iso_tx_qos.latency,
iso_tx_qos.phy, iso_tx_qos.sdu);
"phy=%u, sdu=%u)\n",
iso_tx_qos.rtn, iso_tx_qos.phy, iso_tx_qos.sdu);
c = tolower(console_getchar());
if (c == 'y') {
@ -643,22 +679,19 @@ static int change_central_settings(void)
} else {
iso_qos.tx = &iso_tx_qos;
err = parse_args(&iso_tx_qos);
err = parse_cis_args(&iso_tx_qos);
if (err != 0) {
return err;
}
printk("New settings: rtn=%u, interval=%u, latency=%u, "
"phy=%u, sdu=%u\n",
iso_tx_qos.rtn, iso_tx_qos.interval, iso_tx_qos.latency,
iso_tx_qos.phy, iso_tx_qos.sdu);
printk("New settings: rtn=%u, phy=%u, sdu=%u\n",
iso_tx_qos.rtn, iso_tx_qos.phy, iso_tx_qos.sdu);
}
}
printk("Change RX settings (y/N)? (Current settings: rtn=%u, "
"interval=%u, latency=%u, phy=%u, sdu=%u)\n",
iso_rx_qos.rtn, iso_rx_qos.interval, iso_rx_qos.latency,
iso_rx_qos.phy, iso_rx_qos.sdu);
"phy=%u, sdu=%u)\n",
iso_rx_qos.rtn, iso_rx_qos.phy, iso_rx_qos.sdu);
c = tolower(console_getchar());
if (c == 'y') {
@ -678,15 +711,13 @@ static int change_central_settings(void)
c = tolower(console_getchar());
if (c == 'n') {
err = parse_args(&iso_rx_qos);
err = parse_cis_args(&iso_rx_qos);
if (err != 0) {
return err;
}
printk("New settings: rtn=%u, interval=%u, "
"latency=%u, phy=%u, sdu=%u\n",
iso_rx_qos.rtn, iso_rx_qos.interval,
iso_rx_qos.latency, iso_rx_qos.phy,
printk("New settings: rtn=%u, phy=%u, sdu=%u\n",
iso_rx_qos.rtn, iso_rx_qos.phy,
iso_rx_qos.sdu);
} else {
(void)memcpy(&iso_rx_qos, &iso_tx_qos,
@ -695,20 +726,6 @@ static int change_central_settings(void)
}
}
printk("Change CIS count (y/N)? (Current: %u)\n", cis_create_count);
c = tolower(console_getchar());
if (c == 'y') {
int cis_count = parse_cis_count_arg();
if (cis_count < 0) {
return -EINVAL;
}
cis_create_count = cis_count;
printk("New CIS count: %u\n", cis_create_count);
}
return 0;
}
@ -755,36 +772,36 @@ static int central_create_connection(void)
return 0;
}
static int central_create_cis(void)
static int central_create_cig(void)
{
struct bt_conn *conn_pointers[CONFIG_BT_ISO_MAX_CHAN];
struct bt_iso_chan *chan_pointers[CONFIG_BT_ISO_MAX_CHAN];
struct bt_iso_connect_param connect_param[CONFIG_BT_ISO_MAX_CHAN];
int err;
iso_conn_start_time = 0;
for (int i = 0; i < cis_create_count; i++) {
conn_pointers[i] = default_conn;
chan_pointers[i] = &iso_chans[i];
}
LOG_INF("Creating CIG");
LOG_INF("Binding ISO");
err = bt_iso_chan_bind(conn_pointers, cis_create_count, chan_pointers);
err = bt_iso_cig_create(&cig_create_param, &cig);
if (err != 0) {
LOG_ERR("Failed to bind iso to connection: %d", err);
LOG_ERR("Failed to create CIG: %d", err);
return err;
}
LOG_INF("Connecting ISO channels");
err = bt_iso_chan_connect(chan_pointers, cis_create_count);
for (int i = 0; i < cig_create_param.num_cis; i++) {
connect_param[i].conn = default_conn;
connect_param[i].iso = &iso_chans[i];
}
err = bt_iso_chan_connect(connect_param, cig_create_param.num_cis);
if (err != 0) {
LOG_ERR("Failed to connect iso: %d", err);
return err;
}
total_iso_conn_count++;
for (int i = 0; i < cis_create_count; i++) {
for (int i = 0; i < cig_create_param.num_cis; i++) {
err = k_sem_take(&sem_iso_connected, K_FOREVER);
if (err != 0) {
LOG_ERR("failed to take sem_iso_connected: %d", err);
@ -813,7 +830,7 @@ static int cleanup(void)
err = k_sem_take(&sem_disconnected, K_NO_WAIT);
if (err != 0) {
for (int i = 0; i < cis_create_count; i++) {
for (int i = 0; i < cig_create_param.num_cis; i++) {
err = k_sem_take(&sem_iso_disconnected, K_NO_WAIT);
if (err == 0) {
err = bt_iso_chan_disconnect(&iso_chans[i]);
@ -864,9 +881,9 @@ static int run_central(void)
return err;
}
err = central_create_cis();
err = central_create_cig();
if (err != 0) {
LOG_ERR("Failed to create CISes: %d", err);
LOG_ERR("Failed to create CIG or connect CISes: %d", err);
return err;
}
@ -881,7 +898,7 @@ static int run_central(void)
bt_conn_unref(default_conn);
for (int i = 0; i < cis_create_count; i++) {
for (int i = 0; i < cig_create_param.num_cis; i++) {
err = k_sem_take(&sem_iso_disconnected, K_FOREVER);
if (err != 0) {
LOG_ERR("failed to take sem_iso_disconnected: %d", err);
@ -901,7 +918,7 @@ static int run_peripheral(void)
static bool initialized;
/* Reset */
cis_create_count = 0;
cig_create_param.num_cis = 0;
iso_conn_start_time = 0;
last_received_counter = 0;
memset(&stats_current_conn, 0, sizeof(stats_current_conn));
@ -944,7 +961,7 @@ static int run_peripheral(void)
return err;
}
for (int i = 0; i < cis_create_count; i++) {
for (int i = 0; i < cig_create_param.num_cis; i++) {
err = k_sem_take(&sem_iso_connected, K_FOREVER);
if (err != 0) {
LOG_ERR("failed to take sem_iso_connected: %d", err);
@ -965,7 +982,7 @@ static int run_peripheral(void)
bt_conn_unref(default_conn);
for (int i = 0; i < cis_create_count; i++) {
for (int i = 0; i < cig_create_param.num_cis; i++) {
err = k_sem_take(&sem_iso_disconnected, K_FOREVER);
if (err != 0) {
LOG_ERR("failed to take sem_iso_disconnected: %d", err);
@ -1005,6 +1022,7 @@ void main(void)
for (int i = 0; i < ARRAY_SIZE(iso_chans); i++) {
iso_chans->ops = &iso_ops;
iso_chans->qos = &iso_qos;
cis[i] = &iso_chans[i];
}
/* Init data */

View file

@ -1295,9 +1295,9 @@ static void le_ecred_reconf_req(struct bt_l2cap *l2cap, uint8_t ident,
goto response;
}
while (chan_count-- >= 0) {
BT_L2CAP_LE_CHAN(chans[chan_count])->tx.mtu = mtu;
BT_L2CAP_LE_CHAN(chans[chan_count])->tx.mps = mps;
for (int i = 0; i < chan_count; i++) {
BT_L2CAP_LE_CHAN(chans[i])->tx.mtu = mtu;
BT_L2CAP_LE_CHAN(chans[i])->tx.mps = mps;
}
BT_DBG("mtu %u mps %u", mtu, mps);