1 /** @file
2  *  @brief Internal header for Bluetooth BAP.
3  */
4 
5 /*
6  * Copyright (c) 2019 Bose Corporation
7  * Copyright (c) 2021-2023 Nordic Semiconductor ASA
8  *
9  * SPDX-License-Identifier: Apache-2.0
10  */
11 
12 #include <stdbool.h>
13 #include <stdint.h>
14 
15 #include <zephyr/bluetooth/audio/audio.h>
16 #include <zephyr/bluetooth/audio/bap.h>
17 #include <zephyr/bluetooth/addr.h>
18 #include <zephyr/bluetooth/conn.h>
19 #include <zephyr/bluetooth/iso.h>
20 #include <zephyr/sys/util_macro.h>
21 #include <zephyr/types.h>
22 
23 #define BT_BAP_BASS_SCAN_STATE_NOT_SCANNING   0x00
24 #define BT_BAP_BASS_SCAN_STATE_SCANNING       0x01
25 
26 #define BT_BAP_BASS_OP_SCAN_STOP              0x00
27 #define BT_BAP_BASS_OP_SCAN_START             0x01
28 #define BT_BAP_BASS_OP_ADD_SRC                0x02
29 #define BT_BAP_BASS_OP_MOD_SRC                0x03
30 #define BT_BAP_BASS_OP_BROADCAST_CODE         0x04
31 #define BT_BAP_BASS_OP_REM_SRC                0x05
32 
33 #define BT_BAP_BASS_SCAN_STATE_IDLE           0x00
34 #define BT_BAP_BASS_SCAN_STATE_SCANNING       0x01
35 #define BT_BAP_BASS_SCAN_STATE_FAILED         0x02
36 #define BT_BAP_BASS_SCAN_STATE_SYNCED         0x03
37 
38 #define BT_BAP_BASS_PA_REQ_NO_SYNC            0x00
39 #define BT_BAP_BASS_PA_REQ_SYNC_PAST          0x01
40 #define BT_BAP_BASS_PA_REQ_SYNC               0x02
41 
42 #define BT_BAP_BASS_VALID_OPCODE(opcode) ((opcode) <= BT_BAP_BASS_OP_REM_SRC)
43 
44 struct bt_bap_bass_cp_scan_stop {
45 	uint8_t opcode;
46 } __packed;
47 
48 struct bt_bap_bass_cp_scan_start {
49 	uint8_t opcode;
50 } __packed;
51 
52 struct bt_bap_bass_cp_subgroup {
53 	uint32_t bis_sync;
54 	uint8_t metadata_len;
55 	uint8_t metadata[0];
56 } __packed;
57 
58 struct bt_bap_bass_cp_add_src {
59 	uint8_t opcode;
60 	bt_addr_le_t addr;
61 	uint8_t adv_sid;
62 	uint8_t broadcast_id[BT_AUDIO_BROADCAST_ID_SIZE];
63 	uint8_t pa_sync;
64 	uint16_t pa_interval;
65 	uint8_t num_subgroups;
66 	struct bt_bap_bass_cp_subgroup subgroups[0];
67 } __packed;
68 
69 struct bt_bap_bass_cp_mod_src {
70 	uint8_t opcode;
71 	uint8_t src_id;
72 	uint8_t pa_sync;
73 	uint16_t pa_interval;
74 	uint8_t num_subgroups;
75 	struct bt_bap_bass_cp_subgroup subgroups[0];
76 } __packed;
77 
78 struct bt_bap_bass_cp_broadcase_code {
79 	uint8_t opcode;
80 	uint8_t src_id;
81 	uint8_t broadcast_code[BT_ISO_BROADCAST_CODE_SIZE];
82 } __packed;
83 
84 struct bt_bap_bass_cp_rem_src {
85 	uint8_t opcode;
86 	uint8_t src_id;
87 } __packed;
88 
89 union bt_bap_bass_cp {
90 	uint8_t opcode;
91 	struct bt_bap_bass_cp_scan_stop scan_stop;
92 	struct bt_bap_bass_cp_scan_start scan_start;
93 	struct bt_bap_bass_cp_add_src add_src;
94 	struct bt_bap_bass_cp_mod_src mod_src;
95 	struct bt_bap_bass_cp_broadcase_code broadcast_code;
96 	struct bt_bap_bass_cp_rem_src rem_src;
97 };
98 
bt_bap_pa_state_str(uint8_t state)99 static inline const char *bt_bap_pa_state_str(uint8_t state)
100 {
101 	switch (state) {
102 	case BT_BAP_PA_STATE_NOT_SYNCED:
103 		return "Not synchronized to PA";
104 	case BT_BAP_PA_STATE_INFO_REQ:
105 		return "SyncInfo Request";
106 	case BT_BAP_PA_STATE_SYNCED:
107 		return "Synchronized to PA";
108 	case BT_BAP_PA_STATE_FAILED:
109 		return "Failed to synchronize to PA";
110 	case BT_BAP_PA_STATE_NO_PAST:
111 		return "No PAST";
112 	default:
113 		return "unknown state";
114 	}
115 }
116 
bt_bap_big_enc_state_str(uint8_t state)117 static inline const char *bt_bap_big_enc_state_str(uint8_t state)
118 {
119 	switch (state) {
120 	case BT_BAP_BIG_ENC_STATE_NO_ENC:
121 		return "Not encrypted";
122 	case BT_BAP_BIG_ENC_STATE_BCODE_REQ:
123 		return "Broadcast_Code required";
124 	case BT_BAP_BIG_ENC_STATE_DEC:
125 		return "Decrypting";
126 	case BT_BAP_BIG_ENC_STATE_BAD_CODE:
127 		return "Bad_Code (incorrect encryption key)";
128 	default:
129 		return "unknown state";
130 	}
131 }
132 
valid_bis_syncs(uint32_t bis_sync)133 static inline bool valid_bis_syncs(uint32_t bis_sync)
134 {
135 	if (bis_sync == BT_BAP_BIS_SYNC_NO_PREF) {
136 		return true;
137 	}
138 
139 	if (bis_sync > BIT_MASK(BT_ISO_MAX_GROUP_ISO_COUNT)) {
140 		return false;
141 	}
142 
143 	return true;
144 }
145