forked from erikrobstad/sdk-zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbap_unicast_client.c
4051 lines (3197 loc) · 104 KB
/
bap_unicast_client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* @file
* @brief Bluetooth Unicast Client
*/
/*
* Copyright (c) 2020 Intel Corporation
* Copyright (c) 2022-2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/check.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/gatt.h>
#include <zephyr/bluetooth/hci.h>
#include <zephyr/bluetooth/audio/audio.h>
#include <zephyr/bluetooth/audio/bap.h>
#include "../host/hci_core.h"
#include "../host/conn_internal.h"
#include "../host/iso_internal.h"
#include "bap_iso.h"
#include "audio_internal.h"
#include "bap_endpoint.h"
#include "pacs_internal.h"
#include "bap_unicast_client_internal.h"
#include <zephyr/logging/log.h>
BUILD_ASSERT(CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT > 0 ||
CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT > 0,
"CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT or "
"CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT shall be non-zero");
BUILD_ASSERT(CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT == 0 ||
CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT > 1,
"CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT shall be either 0 or > 1");
BUILD_ASSERT(CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT == 0 ||
CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT > 1,
"CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT shall be either 0 or > 1");
LOG_MODULE_REGISTER(bt_bap_unicast_client, CONFIG_BT_BAP_UNICAST_CLIENT_LOG_LEVEL);
#define PAC_DIR_UNUSED(dir) ((dir) != BT_AUDIO_DIR_SINK && (dir) != BT_AUDIO_DIR_SOURCE)
struct bt_bap_unicast_client_ep {
uint16_t handle;
uint16_t cp_handle;
struct bt_gatt_subscribe_params subscribe;
struct bt_gatt_discover_params discover;
struct bt_bap_ep ep;
struct k_work_delayable ase_read_work;
/* Bool to help handle different order of CP and ASE notification when releasing */
bool release_requested;
bool cp_ntf_pending;
};
static const struct bt_uuid *snk_uuid = BT_UUID_PACS_SNK;
static const struct bt_uuid *src_uuid = BT_UUID_PACS_SRC;
static const struct bt_uuid *pacs_context_uuid = BT_UUID_PACS_SUPPORTED_CONTEXT;
static const struct bt_uuid *pacs_snk_loc_uuid = BT_UUID_PACS_SNK_LOC;
static const struct bt_uuid *pacs_src_loc_uuid = BT_UUID_PACS_SRC_LOC;
static const struct bt_uuid *pacs_avail_ctx_uuid = BT_UUID_PACS_AVAILABLE_CONTEXT;
static const struct bt_uuid *ase_snk_uuid = BT_UUID_ASCS_ASE_SNK;
static const struct bt_uuid *ase_src_uuid = BT_UUID_ASCS_ASE_SRC;
static const struct bt_uuid *cp_uuid = BT_UUID_ASCS_ASE_CP;
static struct bt_bap_unicast_group unicast_groups[UNICAST_GROUP_CNT];
static struct unicast_client {
#if CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT > 0
struct bt_bap_unicast_client_ep snks[CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT];
#endif /* CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT > 0 */
#if CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT > 0
struct bt_bap_unicast_client_ep srcs[CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT];
#endif /* CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT > 0 */
struct bt_gatt_subscribe_params cp_subscribe;
struct bt_gatt_subscribe_params snk_loc_subscribe;
struct bt_gatt_subscribe_params src_loc_subscribe;
struct bt_gatt_subscribe_params avail_ctx_subscribe;
/* TODO: We should be able to reduce the number of discover params for
* CCCD discovery, but requires additional work if it is to support an
* arbitrary number of EATT bearers, as control point and location CCCD
* discovery needs to be done in serial to avoid using the same discover
* parameters twice
*/
struct bt_gatt_discover_params loc_cc_disc;
struct bt_gatt_discover_params avail_ctx_cc_disc;
/* Discovery parameters */
enum bt_audio_dir dir;
bool busy;
union {
struct bt_gatt_read_params read_params;
struct bt_gatt_discover_params disc_params;
struct bt_gatt_write_params write_params;
};
/* The att_buf needs to use the maximum ATT attribute size as a single
* PAC record may use the full size
*/
uint8_t att_buf[BT_ATT_MAX_ATTRIBUTE_LEN];
struct net_buf_simple net_buf;
} uni_cli_insts[CONFIG_BT_MAX_CONN];
static const struct bt_bap_unicast_client_cb *unicast_client_cbs;
/* TODO: Move the functions to avoid these prototypes */
static int unicast_client_ep_set_metadata(struct bt_bap_ep *ep, void *data, uint8_t len,
struct bt_audio_codec_cfg *codec_cfg);
static int unicast_client_ep_set_codec_cfg(struct bt_bap_ep *ep, uint8_t id, uint16_t cid,
uint16_t vid, void *data, uint8_t len,
struct bt_audio_codec_cfg *codec_cfg);
static int unicast_client_ep_start(struct bt_bap_ep *ep,
struct net_buf_simple *buf);
static int unicast_client_ase_discover(struct bt_conn *conn, uint16_t start_handle);
static void unicast_client_reset(struct bt_bap_ep *ep, uint8_t reason);
static void delayed_ase_read_handler(struct k_work *work);
static void unicast_client_ep_set_status(struct bt_bap_ep *ep, struct net_buf_simple *buf);
static int unicast_client_send_start(struct bt_bap_ep *ep)
{
if (ep->receiver_ready != true || ep->dir != BT_AUDIO_DIR_SOURCE) {
LOG_DBG("Invalid ep %p %u %s",
ep, ep->receiver_ready, bt_audio_dir_str(ep->dir));
return -EINVAL;
}
struct bt_ascs_start_op *req;
struct net_buf_simple *buf;
int err;
buf = bt_bap_unicast_client_ep_create_pdu(ep->stream->conn, BT_ASCS_START_OP);
if (buf == NULL) {
LOG_DBG("Could not create PDU");
return -EBUSY;
}
req = net_buf_simple_add(buf, sizeof(*req));
req->num_ases = 1U;
err = unicast_client_ep_start(ep, buf);
if (err != 0) {
LOG_DBG("unicast_client_ep_start failed: %d",
err);
return err;
}
err = bt_bap_unicast_client_ep_send(ep->stream->conn, ep, buf);
if (err != 0) {
LOG_DBG("bt_bap_unicast_client_ep_send failed: %d", err);
return err;
}
return 0;
}
static void unicast_client_ep_idle_state(struct bt_bap_ep *ep);
static struct bt_bap_stream *audio_stream_by_ep_id(const struct bt_conn *conn,
uint8_t id)
{
#if CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT > 0 || CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT > 0
const uint8_t conn_index = bt_conn_index(conn);
#endif /* CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT > 0 || \
* CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT > 0 \
*/
#if CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT > 0
for (size_t i = 0U; i < ARRAY_SIZE(uni_cli_insts[conn_index].snks); i++) {
const struct bt_bap_unicast_client_ep *client_ep =
&uni_cli_insts[conn_index].snks[i];
if (client_ep->ep.status.id == id) {
return client_ep->ep.stream;
}
}
#endif /* CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT > 0 */
#if CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT > 0
for (size_t i = 0U; i < ARRAY_SIZE(uni_cli_insts[conn_index].srcs); i++) {
const struct bt_bap_unicast_client_ep *client_ep =
&uni_cli_insts[conn_index].srcs[i];
if (client_ep->ep.status.id == id) {
return client_ep->ep.stream;
}
}
#endif /* CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT > 0 */
return NULL;
}
static void reset_att_buf(struct unicast_client *client)
{
net_buf_simple_init_with_data(&client->net_buf, &client->att_buf, sizeof(client->att_buf));
net_buf_simple_reset(&client->net_buf);
}
#if defined(CONFIG_BT_AUDIO_RX)
static void unicast_client_ep_iso_recv(struct bt_iso_chan *chan,
const struct bt_iso_recv_info *info, struct net_buf *buf)
{
struct bt_bap_iso *iso = CONTAINER_OF(chan, struct bt_bap_iso, chan);
const struct bt_bap_stream_ops *ops;
struct bt_bap_stream *stream;
struct bt_bap_ep *ep = iso->rx.ep;
if (ep == NULL) {
/* In the case that the CIS has been setup as bidirectional, and
* only one of the directions have an ASE configured yet,
* we should only care about valid ISO packets when doing this
* check. The reason is that some controllers send HCI ISO data
* packets to the host, even if no SDU was sent on the remote
* side. This basically means that empty PDUs are sent to the
* host as HCI ISO data packets, which we should just ignore
*/
if ((info->flags & BT_ISO_FLAGS_VALID) != 0) {
LOG_ERR("iso %p not bound with ep", chan);
}
return;
}
if (ep->status.state != BT_BAP_EP_STATE_STREAMING) {
if (IS_ENABLED(CONFIG_BT_BAP_DEBUG_STREAM_DATA)) {
LOG_DBG("ep %p is not in the streaming state: %s", ep,
bt_bap_ep_state_str(ep->status.state));
}
return;
}
stream = ep->stream;
if (stream == NULL) {
LOG_ERR("No stream for ep %p", ep);
return;
}
ops = stream->ops;
if (IS_ENABLED(CONFIG_BT_BAP_DEBUG_STREAM_DATA)) {
LOG_DBG("stream %p ep %p len %zu", stream, ep, net_buf_frags_len(buf));
}
if (ops != NULL && ops->recv != NULL) {
ops->recv(stream, info, buf);
} else {
LOG_WRN("No callback for recv set");
}
}
#endif /* CONFIG_BT_AUDIO_RX */
#if defined(CONFIG_BT_AUDIO_TX)
static void unicast_client_ep_iso_sent(struct bt_iso_chan *chan)
{
struct bt_bap_iso *iso = CONTAINER_OF(chan, struct bt_bap_iso, chan);
struct bt_bap_stream *stream;
struct bt_bap_ep *ep = iso->tx.ep;
if (ep == NULL) {
LOG_ERR("iso %p not bound with ep", chan);
return;
}
stream = ep->stream;
if (stream == NULL) {
LOG_ERR("No stream for ep %p", ep);
return;
}
if (IS_ENABLED(CONFIG_BT_BAP_DEBUG_STREAM_DATA)) {
LOG_DBG("stream %p ep %p", stream, ep);
}
if (stream->ops != NULL && stream->ops->sent != NULL) {
stream->ops->sent(stream);
}
}
#endif /* CONFIG_BT_AUDIO_TX */
static void unicast_client_ep_iso_connected(struct bt_bap_ep *ep)
{
struct bt_bap_stream *stream;
if (ep->status.state != BT_BAP_EP_STATE_ENABLING) {
LOG_DBG("endpoint not in enabling state: %s",
bt_bap_ep_state_str(ep->status.state));
return;
}
stream = ep->stream;
if (stream == NULL) {
LOG_ERR("No stream for ep %p", ep);
return;
}
LOG_DBG("stream %p ep %p dir %s receiver_ready %u",
stream, ep, bt_audio_dir_str(ep->dir), ep->receiver_ready);
if (ep->receiver_ready && ep->dir == BT_AUDIO_DIR_SOURCE) {
const int err = unicast_client_send_start(ep);
if (err != 0) {
LOG_DBG("Failed to send start: %d", err);
/* TBD: Should we release the stream then? */
ep->receiver_ready = false;
}
}
}
static void unicast_client_iso_connected(struct bt_iso_chan *chan)
{
struct bt_bap_iso *iso = CONTAINER_OF(chan, struct bt_bap_iso, chan);
if (iso->rx.ep == NULL && iso->tx.ep == NULL) {
LOG_ERR("iso %p not bound with ep", chan);
return;
}
if (iso->rx.ep != NULL) {
unicast_client_ep_iso_connected(iso->rx.ep);
}
if (iso->tx.ep != NULL) {
unicast_client_ep_iso_connected(iso->tx.ep);
}
}
static void unicast_client_ep_iso_disconnected(struct bt_bap_ep *ep, uint8_t reason)
{
struct bt_bap_stream *stream;
stream = ep->stream;
if (stream == NULL) {
LOG_ERR("Stream not associated with an ep");
return;
}
LOG_DBG("stream %p ep %p reason 0x%02x", stream, ep, reason);
ep->reason = reason;
/* If we were in the idle state when we started the ISO disconnection
* then we need to call unicast_client_ep_idle_state again when
* the ISO has finalized the disconnection
*/
if (ep->status.state == BT_BAP_EP_STATE_IDLE) {
unicast_client_ep_idle_state(ep);
if (stream->conn != NULL) {
struct bt_conn_info conn_info;
int err;
err = bt_conn_get_info(stream->conn, &conn_info);
if (err != 0 || conn_info.state == BT_CONN_STATE_DISCONNECTED) {
/* Retrigger the reset of the EP if the ACL is disconnected before
* the ISO is disconnected
*/
unicast_client_reset(ep, reason);
}
}
}
}
static void unicast_client_iso_disconnected(struct bt_iso_chan *chan, uint8_t reason)
{
struct bt_bap_iso *iso = CONTAINER_OF(chan, struct bt_bap_iso, chan);
if (iso->rx.ep == NULL && iso->tx.ep == NULL) {
LOG_ERR("iso %p not bound with ep", chan);
return;
}
if (iso->rx.ep != NULL) {
unicast_client_ep_iso_disconnected(iso->rx.ep, reason);
}
if (iso->tx.ep != NULL) {
unicast_client_ep_iso_disconnected(iso->tx.ep, reason);
}
}
static struct bt_iso_chan_ops unicast_client_iso_ops = {
#if defined(CONFIG_BT_AUDIO_RX)
.recv = unicast_client_ep_iso_recv,
#endif /* CONFIG_BT_AUDIO_RX */
#if defined(CONFIG_BT_AUDIO_TX)
.sent = unicast_client_ep_iso_sent,
#endif /* CONFIG_BT_AUDIO_TX */
.connected = unicast_client_iso_connected,
.disconnected = unicast_client_iso_disconnected,
};
bool bt_bap_ep_is_unicast_client(const struct bt_bap_ep *ep)
{
for (size_t i = 0U; i < ARRAY_SIZE(uni_cli_insts); i++) {
#if CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT > 0
if (PART_OF_ARRAY(uni_cli_insts[i].snks, ep)) {
return true;
}
#endif /* CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT > 0 */
#if CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT > 0
if (PART_OF_ARRAY(uni_cli_insts[i].srcs, ep)) {
return true;
}
#endif /* CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT > 0 */
}
return false;
}
static void unicast_client_ep_init(struct bt_bap_ep *ep, uint16_t handle, uint8_t dir)
{
struct bt_bap_unicast_client_ep *client_ep;
LOG_DBG("ep %p dir %s handle 0x%04x", ep, bt_audio_dir_str(dir), handle);
client_ep = CONTAINER_OF(ep, struct bt_bap_unicast_client_ep, ep);
(void)memset(ep, 0, sizeof(*ep));
client_ep->handle = handle;
ep->status.id = 0U;
ep->dir = dir;
ep->reason = BT_HCI_ERR_SUCCESS;
k_work_init_delayable(&client_ep->ase_read_work, delayed_ase_read_handler);
}
static struct bt_bap_ep *unicast_client_ep_find(struct bt_conn *conn, uint16_t handle)
{
uint8_t index;
index = bt_conn_index(conn);
#if CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT > 0
for (size_t i = 0U; i < ARRAY_SIZE(uni_cli_insts[index].snks); i++) {
struct bt_bap_unicast_client_ep *client_ep = &uni_cli_insts[index].snks[i];
if ((handle && client_ep->handle == handle) || (!handle && client_ep->handle)) {
return &client_ep->ep;
}
}
#endif /* CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT > 0 */
#if CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT > 0
for (size_t i = 0U; i < ARRAY_SIZE(uni_cli_insts[index].srcs); i++) {
struct bt_bap_unicast_client_ep *client_ep = &uni_cli_insts[index].srcs[i];
if ((handle && client_ep->handle == handle) || (!handle && client_ep->handle)) {
return &client_ep->ep;
}
}
#endif /* CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT > 0 */
return NULL;
}
struct bt_bap_iso *bt_bap_unicast_client_new_audio_iso(void)
{
struct bt_bap_iso *bap_iso;
bap_iso = bt_bap_iso_new();
if (bap_iso == NULL) {
return NULL;
}
bt_bap_iso_init(bap_iso, &unicast_client_iso_ops);
LOG_DBG("New bap_iso %p", bap_iso);
return bap_iso;
}
static struct bt_bap_ep *unicast_client_ep_new(struct bt_conn *conn, enum bt_audio_dir dir,
uint16_t handle)
{
size_t i, size;
uint8_t index;
struct bt_bap_unicast_client_ep *cache;
index = bt_conn_index(conn);
switch (dir) {
#if CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT > 0
case BT_AUDIO_DIR_SINK:
cache = uni_cli_insts[index].snks;
size = ARRAY_SIZE(uni_cli_insts[index].snks);
break;
#endif /* CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT > 0 */
#if CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT > 0
case BT_AUDIO_DIR_SOURCE:
cache = uni_cli_insts[index].srcs;
size = ARRAY_SIZE(uni_cli_insts[index].srcs);
break;
#endif /* CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT > 0 */
default:
return NULL;
}
for (i = 0; i < size; i++) {
struct bt_bap_unicast_client_ep *client_ep = &cache[i];
if (!client_ep->handle) {
unicast_client_ep_init(&client_ep->ep, handle, dir);
return &client_ep->ep;
}
}
return NULL;
}
static struct bt_bap_ep *unicast_client_ep_get(struct bt_conn *conn, enum bt_audio_dir dir,
uint16_t handle)
{
struct bt_bap_ep *ep;
ep = unicast_client_ep_find(conn, handle);
if (ep || !handle) {
return ep;
}
return unicast_client_ep_new(conn, dir, handle);
}
static void unicast_client_ep_set_local_idle_state(struct bt_bap_ep *ep)
{
struct bt_ascs_ase_status status = {
.id = ep->status.id,
.state = BT_BAP_EP_STATE_IDLE,
};
struct net_buf_simple buf;
net_buf_simple_init_with_data(&buf, &status, sizeof(status));
unicast_client_ep_set_status(ep, &buf);
}
static void unicast_client_ep_idle_state(struct bt_bap_ep *ep)
{
struct bt_bap_unicast_client_ep *client_ep =
CONTAINER_OF(ep, struct bt_bap_unicast_client_ep, ep);
struct bt_bap_stream *stream = ep->stream;
const struct bt_bap_stream_ops *ops;
if (stream == NULL) {
return;
}
/* If CIS is connected, disconnect and wait for CIS disconnection */
if (bt_bap_stream_can_disconnect(stream)) {
int err;
LOG_DBG("Disconnecting stream");
err = bt_bap_stream_disconnect(stream);
if (err != 0) {
LOG_ERR("Failed to disconnect stream: %d", err);
}
return;
} else if (ep->iso != NULL && ep->iso->chan.state == BT_ISO_STATE_DISCONNECTING) {
/* Wait for disconnection */
return;
}
bt_bap_stream_reset(stream);
/* Notify upper layer */
if (client_ep->release_requested) {
client_ep->release_requested = false;
if (client_ep->cp_ntf_pending) {
/* In case that we get the idle state notification before the CP
* notification we trigger the CP callback now, as after this we won't be
* able to find the stream by the ASE ID
*/
client_ep->cp_ntf_pending = false;
if (unicast_client_cbs != NULL && unicast_client_cbs->release != NULL) {
unicast_client_cbs->release(stream, BT_BAP_ASCS_RSP_CODE_SUCCESS,
BT_BAP_ASCS_REASON_NONE);
}
}
}
ops = stream->ops;
if (ops != NULL && ops->released != NULL) {
ops->released(stream);
} else {
LOG_WRN("No callback for released set");
}
}
static void unicast_client_ep_qos_update(struct bt_bap_ep *ep,
const struct bt_ascs_ase_status_qos *qos)
{
struct bt_iso_chan_io_qos *iso_io_qos;
LOG_DBG("ep %p dir %s bap_iso %p", ep, bt_audio_dir_str(ep->dir), ep->iso);
if (ep->dir == BT_AUDIO_DIR_SOURCE) {
/* If the endpoint is a source, then we need to
* reset our RX parameters
*/
iso_io_qos = &ep->iso->rx.qos;
} else if (ep->dir == BT_AUDIO_DIR_SINK) {
/* If the endpoint is a sink, then we need to
* reset our TX parameters
*/
iso_io_qos = &ep->iso->tx.qos;
} else {
__ASSERT(false, "Invalid ep->dir: %u", ep->dir);
return;
}
iso_io_qos->phy = qos->phy;
iso_io_qos->sdu = sys_le16_to_cpu(qos->sdu);
iso_io_qos->rtn = qos->rtn;
}
static void unicast_client_ep_config_state(struct bt_bap_ep *ep, struct net_buf_simple *buf)
{
struct bt_bap_unicast_client_ep *client_ep =
CONTAINER_OF(ep, struct bt_bap_unicast_client_ep, ep);
struct bt_ascs_ase_status_config *cfg;
struct bt_audio_codec_qos_pref *pref;
struct bt_bap_stream *stream;
void *cc;
if (client_ep->release_requested) {
LOG_DBG("Released was requested, change local state to idle");
ep->reason = BT_HCI_ERR_LOCALHOST_TERM_CONN;
unicast_client_ep_set_local_idle_state(ep);
return;
}
if (buf->len < sizeof(*cfg)) {
LOG_ERR("Config status too short");
return;
}
stream = ep->stream;
if (stream == NULL) {
LOG_WRN("No stream active for endpoint");
return;
}
cfg = net_buf_simple_pull_mem(buf, sizeof(*cfg));
if (stream->codec_cfg == NULL) {
LOG_ERR("Stream %p does not have a codec configured", stream);
return;
} else if (stream->codec_cfg->id != cfg->codec.id) {
LOG_ERR("Codec configuration mismatched: %u, %u", stream->codec_cfg->id,
cfg->codec.id);
/* TODO: Release the stream? */
return;
}
if (buf->len < cfg->cc_len) {
LOG_ERR("Malformed ASE Config status: buf->len %u < %u cc_len", buf->len,
cfg->cc_len);
return;
}
cc = net_buf_simple_pull_mem(buf, cfg->cc_len);
pref = &stream->ep->qos_pref;
/* Convert to interval representation so they can be matched by QoS */
pref->unframed_supported = cfg->framing == BT_ASCS_QOS_FRAMING_UNFRAMED;
pref->phy = cfg->phy;
pref->rtn = cfg->rtn;
pref->latency = sys_le16_to_cpu(cfg->latency);
pref->pd_min = sys_get_le24(cfg->pd_min);
pref->pd_max = sys_get_le24(cfg->pd_max);
pref->pref_pd_min = sys_get_le24(cfg->prefer_pd_min);
pref->pref_pd_max = sys_get_le24(cfg->prefer_pd_max);
LOG_DBG("dir %s unframed_supported 0x%02x phy 0x%02x rtn %u "
"latency %u pd_min %u pd_max %u pref_pd_min %u pref_pd_max %u codec 0x%02x ",
bt_audio_dir_str(ep->dir), pref->unframed_supported, pref->phy, pref->rtn,
pref->latency, pref->pd_min, pref->pd_max, pref->pref_pd_min, pref->pref_pd_max,
stream->codec_cfg->id);
unicast_client_ep_set_codec_cfg(ep, cfg->codec.id, sys_le16_to_cpu(cfg->codec.cid),
sys_le16_to_cpu(cfg->codec.vid), cc, cfg->cc_len, NULL);
/* Notify upper layer */
if (stream->ops != NULL && stream->ops->configured != NULL) {
stream->ops->configured(stream, pref);
} else {
LOG_WRN("No callback for configured set");
}
}
static void unicast_client_ep_qos_state(struct bt_bap_ep *ep, struct net_buf_simple *buf,
uint8_t old_state)
{
struct bt_ascs_ase_status_qos *qos;
struct bt_bap_stream *stream;
if (buf->len < sizeof(*qos)) {
LOG_ERR("QoS status too short");
return;
}
stream = ep->stream;
if (stream == NULL) {
LOG_ERR("No stream active for endpoint");
return;
}
if (ep->dir == BT_AUDIO_DIR_SINK && stream->ops != NULL && stream->ops->disabled != NULL) {
/* If the old state was enabling or streaming, then the sink
* ASE has been disabled. Since the sink ASE does not have a
* disabling state, we can check if by comparing the old_state
*/
const bool disabled = old_state == BT_BAP_EP_STATE_ENABLING ||
old_state == BT_BAP_EP_STATE_STREAMING;
if (disabled) {
stream->ops->disabled(stream);
}
}
qos = net_buf_simple_pull_mem(buf, sizeof(*qos));
/* Update existing QoS configuration */
unicast_client_ep_qos_update(ep, qos);
ep->cig_id = qos->cig_id;
ep->cis_id = qos->cis_id;
(void)memcpy(&stream->qos->interval, sys_le24_to_cpu(qos->interval), sizeof(qos->interval));
stream->qos->framing = qos->framing;
stream->qos->phy = qos->phy;
stream->qos->sdu = sys_le16_to_cpu(qos->sdu);
stream->qos->rtn = qos->rtn;
stream->qos->latency = sys_le16_to_cpu(qos->latency);
(void)memcpy(&stream->qos->pd, sys_le24_to_cpu(qos->pd), sizeof(qos->pd));
LOG_DBG("dir %s cig 0x%02x cis 0x%02x codec 0x%02x interval %u "
"framing 0x%02x phy 0x%02x rtn %u latency %u pd %u",
bt_audio_dir_str(ep->dir), ep->cig_id, ep->cis_id, stream->codec_cfg->id,
stream->qos->interval, stream->qos->framing, stream->qos->phy, stream->qos->rtn,
stream->qos->latency, stream->qos->pd);
/* Disconnect ISO if connected */
if (bt_bap_stream_can_disconnect(stream)) {
const int err = bt_bap_stream_disconnect(stream);
if (err != 0) {
LOG_ERR("Failed to disconnect stream: %d", err);
}
} else {
/* We setup the data path here, as this is the earliest where
* we have the ISO <-> EP coupling completed (due to setting
* the CIS ID in the QoS procedure).
*/
bt_bap_iso_configure_data_path(ep, stream->codec_cfg);
}
/* Notify upper layer */
if (stream->ops != NULL && stream->ops->qos_set != NULL) {
stream->ops->qos_set(stream);
} else {
LOG_WRN("No callback for qos_set set");
}
}
static void unicast_client_ep_enabling_state(struct bt_bap_ep *ep, struct net_buf_simple *buf,
bool state_changed)
{
struct bt_ascs_ase_status_enable *enable;
struct bt_bap_stream *stream;
void *metadata;
if (buf->len < sizeof(*enable)) {
LOG_ERR("Enabling status too short");
return;
}
stream = ep->stream;
if (stream == NULL) {
LOG_ERR("No stream active for endpoint");
return;
}
enable = net_buf_simple_pull_mem(buf, sizeof(*enable));
if (buf->len < enable->metadata_len) {
LOG_ERR("Malformed PDU: remaining len %u expected %u", buf->len,
enable->metadata_len);
return;
}
metadata = net_buf_simple_pull_mem(buf, enable->metadata_len);
LOG_DBG("dir %s cig 0x%02x cis 0x%02x", bt_audio_dir_str(ep->dir), ep->cig_id, ep->cis_id);
unicast_client_ep_set_metadata(ep, metadata, enable->metadata_len, NULL);
/* Notify upper layer
*
* If the state did not change then only the metadata was changed
*/
if (state_changed) {
if (stream->ops != NULL && stream->ops->enabled != NULL) {
stream->ops->enabled(stream);
} else {
LOG_WRN("No callback for enabled set");
}
} else {
if (stream->ops != NULL && stream->ops->metadata_updated != NULL) {
stream->ops->metadata_updated(stream);
} else {
LOG_WRN("No callback for metadata_updated set");
}
}
}
static void unicast_client_ep_streaming_state(struct bt_bap_ep *ep, struct net_buf_simple *buf,
bool state_changed)
{
struct bt_ascs_ase_status_stream *stream_status;
struct bt_bap_stream *stream;
if (buf->len < sizeof(*stream_status)) {
LOG_ERR("Streaming status too short");
return;
}
stream = ep->stream;
if (stream == NULL) {
LOG_ERR("No stream active for endpoint");
return;
}
stream_status = net_buf_simple_pull_mem(buf, sizeof(*stream_status));
LOG_DBG("dir %s cig 0x%02x cis 0x%02x", bt_audio_dir_str(ep->dir), ep->cig_id, ep->cis_id);
/* Notify upper layer
*
* If the state did not change then only the metadata was changed
*/
if (state_changed) {
if (stream->ops != NULL && stream->ops->started != NULL) {
stream->ops->started(stream);
} else {
LOG_WRN("No callback for started set");
}
} else {
if (stream->ops != NULL && stream->ops->metadata_updated != NULL) {
stream->ops->metadata_updated(stream);
} else {
LOG_WRN("No callback for metadata_updated set");
}
}
}
static void unicast_client_ep_disabling_state(struct bt_bap_ep *ep, struct net_buf_simple *buf)
{
struct bt_ascs_ase_status_disable *disable;
struct bt_bap_stream *stream;
if (buf->len < sizeof(*disable)) {
LOG_ERR("Disabling status too short");
return;
}
stream = ep->stream;
if (stream == NULL) {
LOG_ERR("No stream active for endpoint");
return;
}
disable = net_buf_simple_pull_mem(buf, sizeof(*disable));
LOG_DBG("dir %s cig 0x%02x cis 0x%02x", bt_audio_dir_str(ep->dir), ep->cig_id, ep->cis_id);
/* Notify upper layer */
if (stream->ops != NULL && stream->ops->disabled != NULL) {
stream->ops->disabled(stream);
} else {
LOG_WRN("No callback for disabled set");
}
}
static void unicast_client_ep_releasing_state(struct bt_bap_ep *ep, struct net_buf_simple *buf)
{
struct bt_bap_stream *stream;
stream = ep->stream;
if (stream == NULL) {
LOG_ERR("No stream active for endpoint");
return;
}
LOG_DBG("dir %s", bt_audio_dir_str(ep->dir));
if (bt_bap_stream_can_disconnect(stream)) {
/* The Unicast Client shall terminate any CIS established for
* that ASE by following the Connected Isochronous Stream
* Terminate procedure defined in Volume 3, Part C,
* Section 9.3.15 in when the Unicast Client has determined
* that the ASE is in the Releasing state.
*/
const int err = bt_bap_stream_disconnect(stream);
if (err != 0) {
LOG_ERR("Failed to disconnect stream: %d", err);
}
}
}
static void unicast_client_ep_set_status(struct bt_bap_ep *ep, struct net_buf_simple *buf)
{
struct bt_ascs_ase_status *status;
struct bt_bap_unicast_client_ep *client_ep;
bool state_changed;
uint8_t old_state;
if (!ep) {
return;
}
client_ep = CONTAINER_OF(ep, struct bt_bap_unicast_client_ep, ep);
status = net_buf_simple_pull_mem(buf, sizeof(*status));
old_state = ep->status.state;
ep->status = *status;
state_changed = old_state != ep->status.state;
if (state_changed && old_state == BT_BAP_EP_STATE_STREAMING) {
/* We left the streaming state, let the upper layers know that the stream is stopped
*/
struct bt_bap_stream *stream = ep->stream;
if (stream != NULL) {
struct bt_bap_stream_ops *ops = stream->ops;
uint8_t reason = ep->reason;
if (reason == BT_HCI_ERR_SUCCESS) {
/* Default to BT_HCI_ERR_UNSPECIFIED if no other reason is set */
reason = BT_HCI_ERR_UNSPECIFIED;
} else {
/* Reset reason */
ep->reason = BT_HCI_ERR_SUCCESS;
}
if (ops != NULL && ops->stopped != NULL) {
ops->stopped(stream, reason);
} else {
LOG_WRN("No callback for stopped set");
}
}
}
LOG_DBG("ep %p handle 0x%04x id 0x%02x dir %s state %s -> %s", ep, client_ep->handle,
status->id, bt_audio_dir_str(ep->dir), bt_bap_ep_state_str(old_state),
bt_bap_ep_state_str(status->state));
switch (status->state) {
case BT_BAP_EP_STATE_IDLE:
ep->receiver_ready = false;
unicast_client_ep_idle_state(ep);
break;
case BT_BAP_EP_STATE_CODEC_CONFIGURED:
switch (old_state) {
/* Valid only if ASE_State field = 0x00 (Idle) */
case BT_BAP_EP_STATE_IDLE:
/* or 0x01 (Codec Configured) */
case BT_BAP_EP_STATE_CODEC_CONFIGURED:
/* or 0x02 (QoS Configured) */
case BT_BAP_EP_STATE_QOS_CONFIGURED:
/* or 0x06 (Releasing) */
case BT_BAP_EP_STATE_RELEASING:
break;
default:
LOG_WRN("Invalid state transition: %s -> %s",