Skip to content

Commit 7609185

Browse files
Thalleycfriedt
authored andcommitted
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 <[email protected]>
1 parent 23f3aa1 commit 7609185

File tree

2 files changed

+96
-78
lines changed
  • samples/bluetooth/iso_connected_benchmark/src
  • subsys/bluetooth/host

2 files changed

+96
-78
lines changed

samples/bluetooth/iso_connected_benchmark/src/main.c

Lines changed: 93 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static enum benchmark_role role;
4747
static struct bt_conn *default_conn;
4848
static struct k_work_delayable iso_send_work;
4949
static struct bt_iso_chan iso_chans[CONFIG_BT_ISO_MAX_CHAN];
50-
static uint8_t cis_create_count = DEFAULT_CIS_COUNT;
50+
static struct bt_iso_chan *cis[CONFIG_BT_ISO_MAX_CHAN];
5151
static bool advertiser_found;
5252
static bt_addr_le_t adv_addr;
5353
static uint32_t last_received_counter;
@@ -56,6 +56,7 @@ static struct iso_recv_stats stats_overall;
5656
static int64_t iso_conn_start_time;
5757
static size_t total_iso_conn_count;
5858
static uint32_t iso_send_count;
59+
static struct bt_iso_cig *cig;
5960

6061
NET_BUF_POOL_FIXED_DEFINE(tx_pool, 1, CONFIG_BT_ISO_TX_MTU, NULL);
6162
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);
6869
static K_SEM_DEFINE(sem_disconnected, 0, 1);
6970

7071
static struct bt_iso_chan_io_qos iso_tx_qos = {
71-
.interval = DEFAULT_CIS_INTERVAL_US, /* in microseconds */
72-
.latency = DEFAULT_CIS_LATENCY_MS, /* milliseconds */
7372
.sdu = DEFAULT_CIS_SDU_SIZE, /* bytes */
7473
.rtn = DEFAULT_CIS_RTN,
7574
.phy = DEFAULT_CIS_PHY,
7675
};
7776

7877
static struct bt_iso_chan_io_qos iso_rx_qos = {
79-
.interval = DEFAULT_CIS_INTERVAL_US, /* in microseconds */
80-
.latency = DEFAULT_CIS_LATENCY_MS, /* milliseconds */
8178
.sdu = DEFAULT_CIS_SDU_SIZE, /* bytes */
8279
.rtn = DEFAULT_CIS_RTN,
8380
.phy = DEFAULT_CIS_PHY,
8481
};
8582

8683
static struct bt_iso_chan_qos iso_qos = {
84+
.tx = &iso_tx_qos,
85+
.rx = &iso_rx_qos,
86+
};
87+
88+
static struct bt_iso_cig_create_param cig_create_param = {
89+
.interval = DEFAULT_CIS_INTERVAL_US, /* in microseconds */
90+
.latency = DEFAULT_CIS_LATENCY_MS, /* milliseconds */
8791
.sca = BT_GAP_SCA_UNKNOWN,
8892
.packing = DEFAULT_CIS_PACKING,
8993
.framing = DEFAULT_CIS_FRAMING,
90-
.tx = &iso_tx_qos,
91-
.rx = &iso_rx_qos,
94+
.cis_channels = cis,
95+
.num_cis = DEFAULT_CIS_COUNT
9296
};
9397

9498
static enum benchmark_role device_role_select(void)
@@ -146,9 +150,9 @@ static void iso_timer_timeout(struct k_work *work)
146150
* calls `bt_iso_chan_send` but the controller only sending a single
147151
* ISO packet.
148152
*/
149-
k_work_reschedule(&iso_send_work, K_USEC(iso_tx_qos.interval - 100));
153+
k_work_reschedule(&iso_send_work, K_USEC(cig_create_param.interval - 100));
150154

151-
for (int i = 0; i < cis_create_count; i++) {
155+
for (int i = 0; i < cig_create_param.num_cis; i++) {
152156
buf = net_buf_alloc(&tx_pool, K_FOREVER);
153157
if (buf == NULL) {
154158
LOG_ERR("Could not allocate buffer");
@@ -276,7 +280,7 @@ static int iso_accept(struct bt_conn *conn, struct bt_iso_chan **chan)
276280
if (iso_chans[i].state == BT_ISO_DISCONNECTED) {
277281
LOG_INF("Returning instance %d", i);
278282
*chan = &iso_chans[i];
279-
cis_create_count++;
283+
cig_create_param.num_cis++;
280284

281285
k_sem_give(&sem_iso_accept);
282286
return 0;
@@ -458,14 +462,14 @@ static int parse_rtn_arg(struct bt_iso_chan_io_qos *qos)
458462
return (int)rtn;
459463
}
460464

461-
static int parse_interval_arg(struct bt_iso_chan_io_qos *qos)
465+
static int parse_interval_arg(void)
462466
{
463467
char buffer[9];
464468
size_t char_count;
465469
uint64_t interval;
466470

467471
printk("Set interval (us) (current %u, default %u)\n",
468-
qos->interval, DEFAULT_CIS_INTERVAL_US);
472+
cig_create_param.interval, DEFAULT_CIS_INTERVAL_US);
469473

470474
char_count = get_chars(buffer, sizeof(buffer) - 1);
471475
if (char_count == 0) {
@@ -482,14 +486,14 @@ static int parse_interval_arg(struct bt_iso_chan_io_qos *qos)
482486
return (int)interval;
483487
}
484488

485-
static int parse_latency_arg(struct bt_iso_chan_io_qos *qos)
489+
static int parse_latency_arg(void)
486490
{
487491
char buffer[6];
488492
size_t char_count;
489493
uint64_t latency;
490494

491495
printk("Set latency (ms) (current %u, default %u)\n",
492-
qos->latency, DEFAULT_CIS_LATENCY_MS);
496+
cig_create_param.latency, DEFAULT_CIS_LATENCY_MS);
493497

494498
char_count = get_chars(buffer, sizeof(buffer) - 1);
495499
if (char_count == 0) {
@@ -563,7 +567,7 @@ static int parse_cis_count_arg(void)
563567
uint64_t cis_count;
564568

565569
printk("Set CIS count (current %u, default %u)\n",
566-
cis_create_count, DEFAULT_CIS_COUNT);
570+
cig_create_param.num_cis, DEFAULT_CIS_COUNT);
567571

568572
char_count = get_chars(buffer, sizeof(buffer) - 1);
569573
if (char_count == 0) {
@@ -579,31 +583,49 @@ static int parse_cis_count_arg(void)
579583
return (int)cis_count;
580584
}
581585

582-
static int parse_args(struct bt_iso_chan_io_qos *qos)
586+
static int parse_cig_args(void)
583587
{
584-
int rtn;
585588
int interval;
586589
int latency;
587-
int phy;
588-
int sdu;
590+
int cis_count;
589591

590592
printk("Follow the prompts. Press enter to use default values.\n");
591593

592-
rtn = parse_rtn_arg(qos);
593-
if (rtn < 0) {
594+
cis_count = parse_cis_count_arg();
595+
if (cis_count < 0) {
594596
return -EINVAL;
595597
}
596598

597-
interval = parse_interval_arg(qos);
599+
interval = parse_interval_arg();
598600
if (interval < 0) {
599601
return -EINVAL;
600602
}
601603

602-
latency = parse_latency_arg(qos);
604+
latency = parse_latency_arg();
603605
if (latency < 0) {
604606
return -EINVAL;
605607
}
606608

609+
cig_create_param.interval = interval;
610+
cig_create_param.latency = latency;
611+
cig_create_param.num_cis = cis_count;
612+
613+
return 0;
614+
}
615+
616+
static int parse_cis_args(struct bt_iso_chan_io_qos *qos)
617+
{
618+
int rtn;
619+
int phy;
620+
int sdu;
621+
622+
printk("Follow the prompts. Press enter to use default values.\n");
623+
624+
rtn = parse_rtn_arg(qos);
625+
if (rtn < 0) {
626+
return -EINVAL;
627+
}
628+
607629
phy = parse_phy_arg(qos);
608630
if (phy < 0) {
609631
return -EINVAL;
@@ -615,8 +637,6 @@ static int parse_args(struct bt_iso_chan_io_qos *qos)
615637
}
616638

617639
qos->rtn = rtn;
618-
qos->interval = interval;
619-
qos->latency = latency;
620640
qos->phy = phy;
621641
qos->sdu = sdu;
622642

@@ -628,10 +648,26 @@ static int change_central_settings(void)
628648
char c;
629649
int err;
630650

651+
printk("Change CIG settings (y/N)? (Current settings: cis_count=%u, "
652+
"interval=%u, latency=%u)\n",
653+
cig_create_param.num_cis, cig_create_param.interval,
654+
cig_create_param.latency);
655+
656+
c = tolower(console_getchar());
657+
if (c == 'y') {
658+
err = parse_cig_args();
659+
if (err != 0) {
660+
return err;
661+
}
662+
663+
printk("New settings: cis_count=%u, inteval=%u, latency=%u\n",
664+
cig_create_param.num_cis, cig_create_param.interval,
665+
cig_create_param.latency);
666+
}
667+
631668
printk("Change TX settings (y/N)? (Current settings: rtn=%u, "
632-
"interval=%u, latency=%u, phy=%u, sdu=%u)\n",
633-
iso_tx_qos.rtn, iso_tx_qos.interval, iso_tx_qos.latency,
634-
iso_tx_qos.phy, iso_tx_qos.sdu);
669+
"phy=%u, sdu=%u)\n",
670+
iso_tx_qos.rtn, iso_tx_qos.phy, iso_tx_qos.sdu);
635671

636672
c = tolower(console_getchar());
637673
if (c == 'y') {
@@ -643,22 +679,19 @@ static int change_central_settings(void)
643679
} else {
644680
iso_qos.tx = &iso_tx_qos;
645681

646-
err = parse_args(&iso_tx_qos);
682+
err = parse_cis_args(&iso_tx_qos);
647683
if (err != 0) {
648684
return err;
649685
}
650686

651-
printk("New settings: rtn=%u, interval=%u, latency=%u, "
652-
"phy=%u, sdu=%u\n",
653-
iso_tx_qos.rtn, iso_tx_qos.interval, iso_tx_qos.latency,
654-
iso_tx_qos.phy, iso_tx_qos.sdu);
687+
printk("New settings: rtn=%u, phy=%u, sdu=%u\n",
688+
iso_tx_qos.rtn, iso_tx_qos.phy, iso_tx_qos.sdu);
655689
}
656690
}
657691

658692
printk("Change RX settings (y/N)? (Current settings: rtn=%u, "
659-
"interval=%u, latency=%u, phy=%u, sdu=%u)\n",
660-
iso_rx_qos.rtn, iso_rx_qos.interval, iso_rx_qos.latency,
661-
iso_rx_qos.phy, iso_rx_qos.sdu);
693+
"phy=%u, sdu=%u)\n",
694+
iso_rx_qos.rtn, iso_rx_qos.phy, iso_rx_qos.sdu);
662695

663696
c = tolower(console_getchar());
664697
if (c == 'y') {
@@ -678,15 +711,13 @@ static int change_central_settings(void)
678711

679712
c = tolower(console_getchar());
680713
if (c == 'n') {
681-
err = parse_args(&iso_rx_qos);
714+
err = parse_cis_args(&iso_rx_qos);
682715
if (err != 0) {
683716
return err;
684717
}
685718

686-
printk("New settings: rtn=%u, interval=%u, "
687-
"latency=%u, phy=%u, sdu=%u\n",
688-
iso_rx_qos.rtn, iso_rx_qos.interval,
689-
iso_rx_qos.latency, iso_rx_qos.phy,
719+
printk("New settings: rtn=%u, phy=%u, sdu=%u\n",
720+
iso_rx_qos.rtn, iso_rx_qos.phy,
690721
iso_rx_qos.sdu);
691722
} else {
692723
(void)memcpy(&iso_rx_qos, &iso_tx_qos,
@@ -695,20 +726,6 @@ static int change_central_settings(void)
695726
}
696727
}
697728

698-
printk("Change CIS count (y/N)? (Current: %u)\n", cis_create_count);
699-
700-
c = tolower(console_getchar());
701-
if (c == 'y') {
702-
int cis_count = parse_cis_count_arg();
703-
704-
if (cis_count < 0) {
705-
return -EINVAL;
706-
}
707-
708-
cis_create_count = cis_count;
709-
printk("New CIS count: %u\n", cis_create_count);
710-
}
711-
712729
return 0;
713730
}
714731

@@ -755,36 +772,36 @@ static int central_create_connection(void)
755772
return 0;
756773
}
757774

758-
static int central_create_cis(void)
775+
static int central_create_cig(void)
759776
{
760-
761-
struct bt_conn *conn_pointers[CONFIG_BT_ISO_MAX_CHAN];
762-
struct bt_iso_chan *chan_pointers[CONFIG_BT_ISO_MAX_CHAN];
777+
struct bt_iso_connect_param connect_param[CONFIG_BT_ISO_MAX_CHAN];
763778
int err;
764779

765780
iso_conn_start_time = 0;
766781

767-
for (int i = 0; i < cis_create_count; i++) {
768-
conn_pointers[i] = default_conn;
769-
chan_pointers[i] = &iso_chans[i];
770-
}
782+
LOG_INF("Creating CIG");
771783

772-
LOG_INF("Binding ISO");
773-
err = bt_iso_chan_bind(conn_pointers, cis_create_count, chan_pointers);
784+
err = bt_iso_cig_create(&cig_create_param, &cig);
774785
if (err != 0) {
775-
LOG_ERR("Failed to bind iso to connection: %d", err);
786+
LOG_ERR("Failed to create CIG: %d", err);
776787
return err;
777788
}
778789

779790
LOG_INF("Connecting ISO channels");
780-
err = bt_iso_chan_connect(chan_pointers, cis_create_count);
791+
792+
for (int i = 0; i < cig_create_param.num_cis; i++) {
793+
connect_param[i].conn = default_conn;
794+
connect_param[i].iso = &iso_chans[i];
795+
}
796+
797+
err = bt_iso_chan_connect(connect_param, cig_create_param.num_cis);
781798
if (err != 0) {
782799
LOG_ERR("Failed to connect iso: %d", err);
783800
return err;
784801
}
785802
total_iso_conn_count++;
786803

787-
for (int i = 0; i < cis_create_count; i++) {
804+
for (int i = 0; i < cig_create_param.num_cis; i++) {
788805
err = k_sem_take(&sem_iso_connected, K_FOREVER);
789806
if (err != 0) {
790807
LOG_ERR("failed to take sem_iso_connected: %d", err);
@@ -813,7 +830,7 @@ static int cleanup(void)
813830

814831
err = k_sem_take(&sem_disconnected, K_NO_WAIT);
815832
if (err != 0) {
816-
for (int i = 0; i < cis_create_count; i++) {
833+
for (int i = 0; i < cig_create_param.num_cis; i++) {
817834
err = k_sem_take(&sem_iso_disconnected, K_NO_WAIT);
818835
if (err == 0) {
819836
err = bt_iso_chan_disconnect(&iso_chans[i]);
@@ -864,9 +881,9 @@ static int run_central(void)
864881
return err;
865882
}
866883

867-
err = central_create_cis();
884+
err = central_create_cig();
868885
if (err != 0) {
869-
LOG_ERR("Failed to create CISes: %d", err);
886+
LOG_ERR("Failed to create CIG or connect CISes: %d", err);
870887
return err;
871888
}
872889

@@ -881,7 +898,7 @@ static int run_central(void)
881898

882899
bt_conn_unref(default_conn);
883900

884-
for (int i = 0; i < cis_create_count; i++) {
901+
for (int i = 0; i < cig_create_param.num_cis; i++) {
885902
err = k_sem_take(&sem_iso_disconnected, K_FOREVER);
886903
if (err != 0) {
887904
LOG_ERR("failed to take sem_iso_disconnected: %d", err);
@@ -901,7 +918,7 @@ static int run_peripheral(void)
901918
static bool initialized;
902919

903920
/* Reset */
904-
cis_create_count = 0;
921+
cig_create_param.num_cis = 0;
905922
iso_conn_start_time = 0;
906923
last_received_counter = 0;
907924
memset(&stats_current_conn, 0, sizeof(stats_current_conn));
@@ -944,7 +961,7 @@ static int run_peripheral(void)
944961
return err;
945962
}
946963

947-
for (int i = 0; i < cis_create_count; i++) {
964+
for (int i = 0; i < cig_create_param.num_cis; i++) {
948965
err = k_sem_take(&sem_iso_connected, K_FOREVER);
949966
if (err != 0) {
950967
LOG_ERR("failed to take sem_iso_connected: %d", err);
@@ -965,7 +982,7 @@ static int run_peripheral(void)
965982

966983
bt_conn_unref(default_conn);
967984

968-
for (int i = 0; i < cis_create_count; i++) {
985+
for (int i = 0; i < cig_create_param.num_cis; i++) {
969986
err = k_sem_take(&sem_iso_disconnected, K_FOREVER);
970987
if (err != 0) {
971988
LOG_ERR("failed to take sem_iso_disconnected: %d", err);
@@ -1005,6 +1022,7 @@ void main(void)
10051022
for (int i = 0; i < ARRAY_SIZE(iso_chans); i++) {
10061023
iso_chans->ops = &iso_ops;
10071024
iso_chans->qos = &iso_qos;
1025+
cis[i] = &iso_chans[i];
10081026
}
10091027

10101028
/* Init data */

0 commit comments

Comments
 (0)