1 /*
2 * Copyright 2020 HIMSA II K/S - www.himsa.com. Represented by EHIMA -
3 * www.ehima.com
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 /*
19 * This file contains definitions for Basic Audio Profile / Audio Stream Control
20 * and Published Audio Capabilities definitions, structures etc.
21 */
22
23 #pragma once
24
25 #include <stdint.h>
26
27 #include <bitset>
28 #include <map>
29 #include <optional>
30 #include <string>
31 #include <variant>
32 #include <vector>
33
34 #include "bta_groups.h"
35 #include "bta_le_audio_api.h"
36 #include "bta_le_audio_uuids.h"
37 #include "btm_iso_api_types.h"
38
39 namespace le_audio {
40
41 #define UINT8_TO_VEC_UINT8(u8) \
42 std::vector<uint8_t> { u8 }
43 #define UINT16_TO_VEC_UINT8(u16) \
44 std::vector<uint8_t>((uint8_t*)&u16, (uint8_t*)&u16 + sizeof(u16))
45 #define UINT32_TO_VEC_UINT8(u32) \
46 std::vector<uint8_t>((uint8_t*)&u32, (uint8_t*)&u32 + sizeof(u32))
47
48 #define VEC_UINT8_TO_UINT8(vec) vec.data()[0]
49 #define VEC_UINT8_TO_UINT16(vec) ((vec.data()[1] << 8) + vec.data()[0])
50 #define OFF_VEC_UINT8_TO_UINT16(vec, off) \
51 ((vec.data()[1 + off] << 8) + vec.data()[0 + off])
52 #define VEC_UINT8_TO_UINT32(vec) \
53 ((vec.data()[3] << 24) + (vec.data()[2] << 16) + (vec.data()[1] << 8) + \
54 vec.data()[0])
55
56 namespace uuid {
57 /* CAP service
58 * This service is used to identify peer role (which we are not using for now)
59 * and to wrap CSIS service as this is required to understand the context of the
60 * CSIS
61 */
62 static const bluetooth::Uuid kCapServiceUuid =
63 bluetooth::Uuid::From16Bit(UUID_COMMON_AUDIO_SERVICE);
64
65 /* Assigned numbers for attributes */
66 static const bluetooth::Uuid kPublishedAudioCapabilityServiceUuid =
67 bluetooth::Uuid::From16Bit(0x1850);
68 static const bluetooth::Uuid kAudioStreamControlServiceUuid =
69 bluetooth::Uuid::From16Bit(0x184E);
70
71 static const bluetooth::Uuid kTelephonyMediaAudioServiceUuid =
72 bluetooth::Uuid::From16Bit(0x1855);
73
74 /* Published Audio Capabilities Service Characteristics */
75 static const bluetooth::Uuid kSinkPublishedAudioCapabilityCharacteristicUuid =
76 bluetooth::Uuid::From16Bit(0x2BC9);
77 static const bluetooth::Uuid kSourcePublishedAudioCapabilityCharacteristicUuid =
78 bluetooth::Uuid::From16Bit(0x2BCB);
79 static const bluetooth::Uuid kSinkAudioLocationCharacteristicUuid =
80 bluetooth::Uuid::From16Bit(0x2BCA);
81 static const bluetooth::Uuid kSourceAudioLocationCharacteristicUuid =
82 bluetooth::Uuid::From16Bit(0x2BCC);
83
84 /* Audio Stream Control Service Characteristics */
85 static const bluetooth::Uuid kAudioContextAvailabilityCharacteristicUuid =
86 bluetooth::Uuid::From16Bit(0x2BCD);
87 static const bluetooth::Uuid kAudioSupportedContextCharacteristicUuid =
88 bluetooth::Uuid::From16Bit(0x2BCE);
89
90 /* Audio Stream Control Service Characteristics */
91 static const bluetooth::Uuid kSinkAudioStreamEndpointUuid =
92 bluetooth::Uuid::From16Bit(0x2BC4);
93 static const bluetooth::Uuid kSourceAudioStreamEndpointUuid =
94 bluetooth::Uuid::From16Bit(0x2BC5);
95 static const bluetooth::Uuid
96 kAudioStreamEndpointControlPointCharacteristicUuid =
97 bluetooth::Uuid::From16Bit(0x2BC6);
98
99 /* Telephony and Media Audio Service Characteristics */
100 static const bluetooth::Uuid kTelephonyMediaAudioProfileRoleCharacteristicUuid =
101 bluetooth::Uuid::From16Bit(0x2B51);
102 } // namespace uuid
103
104 namespace codec_spec_conf {
105 /* LTV Types */
106 constexpr uint8_t kLeAudioCodecLC3TypeSamplingFreq = 0x01;
107 constexpr uint8_t kLeAudioCodecLC3TypeFrameDuration = 0x02;
108 constexpr uint8_t kLeAudioCodecLC3TypeAudioChannelAllocation = 0x03;
109 constexpr uint8_t kLeAudioCodecLC3TypeOctetPerFrame = 0x04;
110 constexpr uint8_t kLeAudioCodecLC3TypeCodecFrameBlocksPerSdu = 0x05;
111
112 /* Sampling Frequencies */
113 constexpr uint8_t kLeAudioSamplingFreq8000Hz = 0x01;
114 constexpr uint8_t kLeAudioSamplingFreq11025Hz = 0x02;
115 constexpr uint8_t kLeAudioSamplingFreq16000Hz = 0x03;
116 constexpr uint8_t kLeAudioSamplingFreq22050Hz = 0x04;
117 constexpr uint8_t kLeAudioSamplingFreq24000Hz = 0x05;
118 constexpr uint8_t kLeAudioSamplingFreq32000Hz = 0x06;
119 constexpr uint8_t kLeAudioSamplingFreq44100Hz = 0x07;
120 constexpr uint8_t kLeAudioSamplingFreq48000Hz = 0x08;
121 constexpr uint8_t kLeAudioSamplingFreq88200Hz = 0x09;
122 constexpr uint8_t kLeAudioSamplingFreq96000Hz = 0x0A;
123 constexpr uint8_t kLeAudioSamplingFreq176400Hz = 0x0B;
124 constexpr uint8_t kLeAudioSamplingFreq192000Hz = 0x0C;
125 constexpr uint8_t kLeAudioSamplingFreq384000Hz = 0x0D;
126
127 /* Frame Durations */
128 constexpr uint8_t kLeAudioCodecLC3FrameDur7500us = 0x00;
129 constexpr uint8_t kLeAudioCodecLC3FrameDur10000us = 0x01;
130
131 /* Audio Allocations */
132 constexpr uint32_t kLeAudioLocationNotAllowed = 0x00000000;
133 constexpr uint32_t kLeAudioLocationFrontLeft = 0x00000001;
134 constexpr uint32_t kLeAudioLocationFrontRight = 0x00000002;
135 constexpr uint32_t kLeAudioLocationFrontCenter = 0x00000004;
136 constexpr uint32_t kLeAudioLocationLowFreqEffects1 = 0x00000008;
137 constexpr uint32_t kLeAudioLocationBackLeft = 0x00000010;
138 constexpr uint32_t kLeAudioLocationBackRight = 0x00000020;
139 constexpr uint32_t kLeAudioLocationFrontLeftOfCenter = 0x00000040;
140 constexpr uint32_t kLeAudioLocationFrontRightOfCenter = 0x00000080;
141 constexpr uint32_t kLeAudioLocationBackCenter = 0x00000100;
142 constexpr uint32_t kLeAudioLocationLowFreqEffects2 = 0x00000200;
143 constexpr uint32_t kLeAudioLocationSideLeft = 0x00000400;
144 constexpr uint32_t kLeAudioLocationSideRight = 0x00000800;
145 constexpr uint32_t kLeAudioLocationTopFrontLeft = 0x00001000;
146 constexpr uint32_t kLeAudioLocationTopFrontRight = 0x00002000;
147 constexpr uint32_t kLeAudioLocationTopFrontCenter = 0x00004000;
148 constexpr uint32_t kLeAudioLocationTopCenter = 0x00008000;
149 constexpr uint32_t kLeAudioLocationTopBackLeft = 0x00010000;
150 constexpr uint32_t kLeAudioLocationTopBackRight = 0x00020000;
151 constexpr uint32_t kLeAudioLocationTopSideLeft = 0x00040000;
152 constexpr uint32_t kLeAudioLocationTopSideRight = 0x00080000;
153 constexpr uint32_t kLeAudioLocationTopBackCenter = 0x00100000;
154 constexpr uint32_t kLeAudioLocationBottomFrontCenter = 0x00200000;
155 constexpr uint32_t kLeAudioLocationBottomFrontLeft = 0x00400000;
156 constexpr uint32_t kLeAudioLocationBottomFrontRight = 0x00800000;
157 constexpr uint32_t kLeAudioLocationFrontLeftWide = 0x01000000;
158 constexpr uint32_t kLeAudioLocationFrontRightWide = 0x02000000;
159 constexpr uint32_t kLeAudioLocationLeftSurround = 0x04000000;
160 constexpr uint32_t kLeAudioLocationRightSurround = 0x08000000;
161
162 constexpr uint32_t kLeAudioLocationAnyLeft =
163 kLeAudioLocationFrontLeft | kLeAudioLocationBackLeft |
164 kLeAudioLocationFrontLeftOfCenter | kLeAudioLocationSideLeft |
165 kLeAudioLocationTopFrontLeft | kLeAudioLocationTopBackLeft |
166 kLeAudioLocationTopSideLeft | kLeAudioLocationBottomFrontLeft |
167 kLeAudioLocationFrontLeftWide | kLeAudioLocationLeftSurround;
168
169 constexpr uint32_t kLeAudioLocationAnyRight =
170 kLeAudioLocationFrontRight | kLeAudioLocationBackRight |
171 kLeAudioLocationFrontRightOfCenter | kLeAudioLocationSideRight |
172 kLeAudioLocationTopFrontRight | kLeAudioLocationTopBackRight |
173 kLeAudioLocationTopSideRight | kLeAudioLocationBottomFrontRight |
174 kLeAudioLocationFrontRightWide | kLeAudioLocationRightSurround;
175
176 constexpr uint32_t kLeAudioLocationStereo =
177 kLeAudioLocationFrontLeft | kLeAudioLocationFrontRight;
178
179 /* Octets Per Frame */
180 constexpr uint16_t kLeAudioCodecLC3FrameLen30 = 30;
181 constexpr uint16_t kLeAudioCodecLC3FrameLen40 = 40;
182 constexpr uint16_t kLeAudioCodecLC3FrameLen60 = 60;
183 constexpr uint16_t kLeAudioCodecLC3FrameLen80 = 80;
184 constexpr uint16_t kLeAudioCodecLC3FrameLen120 = 120;
185
186 } // namespace codec_spec_conf
187
188 constexpr uint8_t kInvalidCisId = 0xFF;
189
190 namespace codec_spec_caps {
SamplingFreqConfig2Capability(uint8_t conf)191 uint16_t constexpr SamplingFreqConfig2Capability(uint8_t conf) {
192 return (1 << (conf - 1));
193 }
194
FrameDurationConfig2Capability(uint8_t conf)195 uint8_t constexpr FrameDurationConfig2Capability(uint8_t conf) {
196 return (0x01 << (conf));
197 }
198
GetAudioChannelCounts(std::bitset<32> allocation)199 inline uint8_t GetAudioChannelCounts(std::bitset<32> allocation) {
200 /*
201 * BAP d09r07 B4.2.3 Audio_Channel_Allocation
202 * "(...) Audio_Channel_Allocation bitmap value of all zeros or the
203 * absence of the Audio_Channel_Allocation LTV structure within a
204 * Codec_Specific_Configuration field shall be interpreted as defining a
205 * single audio channel of Mono audio (a single channel of no specified
206 * Audio Location).
207 */
208 uint8_t audio_channel_counts = allocation.count() ?: 1;
209 return (0x01 << (audio_channel_counts - 1));
210 }
211
212 /* LTV Types - same values as in Codec Specific Configurations but 0x03 is
213 * named differently.
214 */
215 constexpr uint8_t kLeAudioCodecLC3TypeSamplingFreq =
216 codec_spec_conf::kLeAudioCodecLC3TypeSamplingFreq;
217 constexpr uint8_t kLeAudioCodecLC3TypeFrameDuration =
218 codec_spec_conf::kLeAudioCodecLC3TypeFrameDuration;
219 constexpr uint8_t kLeAudioCodecLC3TypeAudioChannelCounts =
220 codec_spec_conf::kLeAudioCodecLC3TypeAudioChannelAllocation;
221 constexpr uint8_t kLeAudioCodecLC3TypeOctetPerFrame =
222 codec_spec_conf::kLeAudioCodecLC3TypeOctetPerFrame;
223 constexpr uint8_t kLeAudioCodecLC3TypeMaxCodecFramesPerSdu =
224 codec_spec_conf::kLeAudioCodecLC3TypeCodecFrameBlocksPerSdu;
225
226 /* Sampling Frequencies */
227 constexpr uint16_t kLeAudioSamplingFreq8000Hz =
228 SamplingFreqConfig2Capability(codec_spec_conf::kLeAudioSamplingFreq8000Hz);
229 constexpr uint16_t kLeAudioSamplingFreq11025Hz =
230 SamplingFreqConfig2Capability(codec_spec_conf::kLeAudioSamplingFreq11025Hz);
231 constexpr uint16_t kLeAudioSamplingFreq16000Hz =
232 SamplingFreqConfig2Capability(codec_spec_conf::kLeAudioSamplingFreq16000Hz);
233 constexpr uint16_t kLeAudioSamplingFreq22050Hz =
234 SamplingFreqConfig2Capability(codec_spec_conf::kLeAudioSamplingFreq22050Hz);
235 constexpr uint16_t kLeAudioSamplingFreq24000Hz =
236 SamplingFreqConfig2Capability(codec_spec_conf::kLeAudioSamplingFreq24000Hz);
237 constexpr uint16_t kLeAudioSamplingFreq32000Hz =
238 SamplingFreqConfig2Capability(codec_spec_conf::kLeAudioSamplingFreq32000Hz);
239 constexpr uint16_t kLeAudioSamplingFreq44100Hz =
240 SamplingFreqConfig2Capability(codec_spec_conf::kLeAudioSamplingFreq44100Hz);
241 constexpr uint16_t kLeAudioSamplingFreq48000Hz =
242 SamplingFreqConfig2Capability(codec_spec_conf::kLeAudioSamplingFreq48000Hz);
243 constexpr uint16_t kLeAudioSamplingFreq88200Hz =
244 SamplingFreqConfig2Capability(codec_spec_conf::kLeAudioSamplingFreq88200Hz);
245 constexpr uint16_t kLeAudioSamplingFreq96000Hz =
246 SamplingFreqConfig2Capability(codec_spec_conf::kLeAudioSamplingFreq96000Hz);
247 constexpr uint16_t kLeAudioSamplingFreq176400Hz = SamplingFreqConfig2Capability(
248 codec_spec_conf::kLeAudioSamplingFreq176400Hz);
249 constexpr uint16_t kLeAudioSamplingFreq192000Hz = SamplingFreqConfig2Capability(
250 codec_spec_conf::kLeAudioSamplingFreq192000Hz);
251 constexpr uint16_t kLeAudioSamplingFreq384000Hz = SamplingFreqConfig2Capability(
252 codec_spec_conf::kLeAudioSamplingFreq384000Hz);
253
254 /* Frame Durations */
255 constexpr uint8_t kLeAudioCodecLC3FrameDur7500us =
256 FrameDurationConfig2Capability(
257 codec_spec_conf::kLeAudioCodecLC3FrameDur7500us);
258 constexpr uint8_t kLeAudioCodecLC3FrameDur10000us =
259 FrameDurationConfig2Capability(
260 codec_spec_conf::kLeAudioCodecLC3FrameDur10000us);
261 constexpr uint8_t kLeAudioCodecLC3FrameDurPrefer7500us = 0x10;
262 constexpr uint8_t kLeAudioCodecLC3FrameDurPrefer10000us = 0x20;
263
264 /* Audio Channel Counts */
265 /* Each bit represents support for additional channel: bit 0 - one channel,
266 * bit 1 - two, bit 3 - four channels. Multiple bits can be enabled at once.
267 */
268 constexpr uint8_t kLeAudioCodecLC3ChannelCountNone = 0x00;
269 constexpr uint8_t kLeAudioCodecLC3ChannelCountSingleChannel = 0x01;
270 constexpr uint8_t kLeAudioCodecLC3ChannelCountTwoChannel = 0x02;
271 constexpr uint8_t kLeAudioCodecLC3ChannelCountThreeChannel = 0x04;
272 constexpr uint8_t kLeAudioCodecLC3ChannelCountFourChannel = 0x08;
273 constexpr uint8_t kLeAudioCodecLC3ChannelCountFiveChannel = 0x10;
274 constexpr uint8_t kLeAudioCodecLC3ChannelCountSixChannel = 0x20;
275 constexpr uint8_t kLeAudioCodecLC3ChannelCountSevenChannel = 0x40;
276 constexpr uint8_t kLeAudioCodecLC3ChannelCountEightChannel = 0x40;
277
278 /* Octets Per Frame - same as in Codec Specific Configurations but in
279 * capabilities we get two values: min and max.
280 */
281 constexpr uint16_t kLeAudioCodecLC3FrameLen30 =
282 codec_spec_conf::kLeAudioCodecLC3FrameLen30;
283 constexpr uint16_t kLeAudioCodecLC3FrameLen40 =
284 codec_spec_conf::kLeAudioCodecLC3FrameLen40;
285 constexpr uint16_t kLeAudioCodecLC3FrameLen120 =
286 codec_spec_conf::kLeAudioCodecLC3FrameLen120;
287
288 }; // namespace codec_spec_caps
289
290 namespace types {
291 constexpr uint8_t kLeAudioCodingFormatLC3 = bluetooth::hci::kIsoCodingFormatLc3;
292 constexpr uint8_t kLeAudioCodingFormatVendorSpecific =
293 bluetooth::hci::kIsoCodingFormatVendorSpecific;
294 constexpr uint16_t kLeAudioVendorCompanyIdUndefined = 0x00;
295 constexpr uint16_t kLeAudioVendorCodecIdUndefined = 0x00;
296
297 /* Metadata types from Assigned Numbers */
298 constexpr uint8_t kLeAudioMetadataTypePreferredAudioContext = 0x01;
299 constexpr uint8_t kLeAudioMetadataTypeStreamingAudioContext = 0x02;
300 constexpr uint8_t kLeAudioMetadataTypeProgramInfo = 0x03;
301 constexpr uint8_t kLeAudioMetadataTypeLanguage = 0x04;
302 constexpr uint8_t kLeAudioMetadataTypeCcidList = 0x05;
303
304 constexpr uint8_t kLeAudioMetadataTypeLen = 1;
305 constexpr uint8_t kLeAudioMetadataLenLen = 1;
306
307 constexpr uint8_t kLeAudioMetadataStreamingAudioContextLen = 2;
308
309 /* CSIS Types */
310 constexpr uint8_t kDefaultScanDurationS = 5;
311 constexpr uint8_t kDefaultCsisSetSize = 2;
312
313 constexpr uint8_t kLeAudioDirectionSink = 0x01;
314 constexpr uint8_t kLeAudioDirectionSource = 0x02;
315
316 /* Audio stream config types */
317 constexpr uint8_t kFramingUnframedPduSupported = 0x00;
318 constexpr uint8_t kFramingUnframedPduUnsupported = 0x01;
319
320 constexpr uint8_t kTargetLatencyLower = 0x01;
321 constexpr uint8_t kTargetLatencyBalancedLatencyReliability = 0x02;
322 constexpr uint8_t kTargetLatencyHigherReliability = 0x03;
323
324 constexpr uint8_t kTargetPhy1M = 0x01;
325 constexpr uint8_t kTargetPhy2M = 0x02;
326 constexpr uint8_t kTargetPhyCoded = 0x03;
327
328 constexpr uint32_t kPresDelayNoPreference = 0x00000000;
329
330 constexpr uint16_t kMaxTransportLatencyMin = 0x0005;
331 constexpr uint16_t kMaxTransportLatencyMax = 0x0FA0;
332
333 enum class CigState : uint8_t { NONE, CREATING, CREATED, REMOVING, RECOVERING };
334
335 /* ASE states according to BAP defined state machine states */
336 enum class AseState : uint8_t {
337 BTA_LE_AUDIO_ASE_STATE_IDLE = 0x00,
338 BTA_LE_AUDIO_ASE_STATE_CODEC_CONFIGURED = 0x01,
339 BTA_LE_AUDIO_ASE_STATE_QOS_CONFIGURED = 0x02,
340 BTA_LE_AUDIO_ASE_STATE_ENABLING = 0x03,
341 BTA_LE_AUDIO_ASE_STATE_STREAMING = 0x04,
342 BTA_LE_AUDIO_ASE_STATE_DISABLING = 0x05,
343 BTA_LE_AUDIO_ASE_STATE_RELEASING = 0x06,
344 };
345
346 enum class AudioStreamDataPathState {
347 IDLE,
348 CIS_DISCONNECTING,
349 CIS_ASSIGNED,
350 CIS_PENDING,
351 CIS_ESTABLISHED,
352 DATA_PATH_ESTABLISHED,
353 };
354
355 enum class CisType {
356 CIS_TYPE_BIDIRECTIONAL,
357 CIS_TYPE_UNIDIRECTIONAL_SINK,
358 CIS_TYPE_UNIDIRECTIONAL_SOURCE,
359 };
360
361 struct cis {
362 uint8_t id;
363 CisType type;
364 uint16_t conn_handle;
365 RawAddress addr;
366 };
367
368 enum class CodecLocation {
369 HOST,
370 ADSP,
371 CONTROLLER,
372 };
373
374 /* Context Types */
375 enum class LeAudioContextType : uint16_t {
376 UNINITIALIZED = 0x0000,
377 UNSPECIFIED = 0x0001,
378 CONVERSATIONAL = 0x0002,
379 MEDIA = 0x0004,
380 GAME = 0x0008,
381 INSTRUCTIONAL = 0x0010,
382 VOICEASSISTANTS = 0x0020,
383 LIVE = 0x0040,
384 SOUNDEFFECTS = 0x0080,
385 NOTIFICATIONS = 0x0100,
386 RINGTONE = 0x0200,
387 ALERTS = 0x0400,
388 EMERGENCYALARM = 0x0800,
389 RFU = 0x1000,
390 };
391
392 class AudioContexts {
393 using T = std::underlying_type<LeAudioContextType>::type;
394 T mValue;
395
396 public:
AudioContexts()397 explicit constexpr AudioContexts()
398 : mValue(static_cast<T>(LeAudioContextType::UNINITIALIZED)) {}
AudioContexts(const T & v)399 explicit constexpr AudioContexts(const T& v) : mValue(v) {}
AudioContexts(const LeAudioContextType & v)400 explicit constexpr AudioContexts(const LeAudioContextType& v)
401 : mValue(static_cast<T>(v)) {}
AudioContexts(const AudioContexts & other)402 constexpr AudioContexts(const AudioContexts& other)
403 : mValue(static_cast<T>(other.value())) {}
404
value()405 constexpr T value() const { return mValue; }
value_ref()406 T& value_ref() { return mValue; }
none()407 bool none() const {
408 return mValue == static_cast<T>(LeAudioContextType::UNINITIALIZED);
409 }
any()410 bool any() const { return !none(); }
411
set(LeAudioContextType const & v)412 void set(LeAudioContextType const& v) { mValue |= static_cast<T>(v); }
unset(const LeAudioContextType & v)413 void unset(const LeAudioContextType& v) { mValue &= ~static_cast<T>(v); }
414
test(const LeAudioContextType & v)415 bool test(const LeAudioContextType& v) const {
416 return (mValue & static_cast<T>(v)) != 0;
417 }
test_all(const AudioContexts & v)418 bool test_all(const AudioContexts& v) const {
419 return (mValue & v.value()) == v.value();
420 }
test_any(const AudioContexts & v)421 bool test_any(const AudioContexts& v) const {
422 return (mValue & v.value()) != 0;
423 }
clear()424 void clear() { mValue = static_cast<T>(LeAudioContextType::UNINITIALIZED); }
425
426 std::string to_string() const;
427
428 AudioContexts& operator=(AudioContexts&& other) = default;
429 AudioContexts& operator=(const AudioContexts&) = default;
430 bool operator==(const AudioContexts& other) const {
431 return value() == other.value();
432 };
433 bool operator!=(const AudioContexts& other) const {
434 return value() != other.value();
435 };
436 constexpr AudioContexts operator~() const { return AudioContexts(~value()); }
437 };
438
439 AudioContexts operator|(std::underlying_type<LeAudioContextType>::type lhs,
440 const LeAudioContextType rhs);
441 AudioContexts& operator|=(AudioContexts& lhs, AudioContexts const& rhs);
442 AudioContexts& operator&=(AudioContexts& lhs, AudioContexts const& rhs);
443
444 constexpr AudioContexts operator^(const AudioContexts& lhs,
445 const AudioContexts& rhs) {
446 return AudioContexts(lhs.value() ^ rhs.value());
447 }
448 constexpr AudioContexts operator|(const AudioContexts& lhs,
449 const AudioContexts& rhs) {
450 return AudioContexts(lhs.value() | rhs.value());
451 }
452 constexpr AudioContexts operator&(const AudioContexts& lhs,
453 const AudioContexts& rhs) {
454 return AudioContexts(lhs.value() & rhs.value());
455 }
456 constexpr AudioContexts operator|(const LeAudioContextType& lhs,
457 const LeAudioContextType& rhs) {
458 using T = std::underlying_type<LeAudioContextType>::type;
459 return AudioContexts(static_cast<T>(lhs) | static_cast<T>(rhs));
460 }
461 constexpr AudioContexts operator|(const LeAudioContextType& lhs,
462 const AudioContexts& rhs) {
463 return AudioContexts(lhs) | rhs;
464 }
465 constexpr AudioContexts operator|(const AudioContexts& lhs,
466 const LeAudioContextType& rhs) {
467 return lhs | AudioContexts(rhs);
468 }
469
470 std::string ToHexString(const types::LeAudioContextType& value);
471
472 template <typename T>
473 struct BidirectionalPair {
474 T sink;
475 T source;
476
getBidirectionalPair477 T get(uint8_t direction) const {
478 if (direction ==
479 (types::kLeAudioDirectionSink | types::kLeAudioDirectionSource)) {
480 return get_bidirectional(*this);
481 } else if (direction == types::kLeAudioDirectionSink) {
482 return sink;
483 }
484 return source;
485 }
get_refBidirectionalPair486 T& get_ref(uint8_t direction) {
487 return (direction == types::kLeAudioDirectionSink) ? sink : source;
488 }
489
490 BidirectionalPair<T>& operator=(const BidirectionalPair<T>&) = default;
491 bool operator==(const BidirectionalPair<T>& other) const {
492 return (sink == other.sink) && (source == other.source);
493 };
494 bool operator!=(const BidirectionalPair<T>& other) const {
495 return (sink != other.sink) || (source != other.source);
496 };
497 };
498
499 template <typename T>
500 T get_bidirectional(BidirectionalPair<T> p);
501
502 /* Configuration strategy */
503 enum class LeAudioConfigurationStrategy : uint8_t {
504 MONO_ONE_CIS_PER_DEVICE = 0x00, /* Common true wireless speakers */
505 STEREO_TWO_CISES_PER_DEVICE =
506 0x01, /* Requires 2 ASEs and 2 Audio Allocation for left/right */
507 STEREO_ONE_CIS_PER_DEVICE = 0x02, /* Requires channel count 2*/
508 RFU = 0x03,
509 };
510
511 constexpr LeAudioContextType kLeAudioContextAllTypesArray[] = {
512 LeAudioContextType::UNSPECIFIED, LeAudioContextType::CONVERSATIONAL,
513 LeAudioContextType::MEDIA, LeAudioContextType::GAME,
514 LeAudioContextType::INSTRUCTIONAL, LeAudioContextType::VOICEASSISTANTS,
515 LeAudioContextType::LIVE, LeAudioContextType::SOUNDEFFECTS,
516 LeAudioContextType::NOTIFICATIONS, LeAudioContextType::RINGTONE,
517 LeAudioContextType::ALERTS, LeAudioContextType::EMERGENCYALARM,
518 };
519
520 constexpr AudioContexts kLeAudioContextAllTypes =
521 LeAudioContextType::UNSPECIFIED | LeAudioContextType::CONVERSATIONAL |
522 LeAudioContextType::MEDIA | LeAudioContextType::GAME |
523 LeAudioContextType::INSTRUCTIONAL | LeAudioContextType::VOICEASSISTANTS |
524 LeAudioContextType::LIVE | LeAudioContextType::SOUNDEFFECTS |
525 LeAudioContextType::NOTIFICATIONS | LeAudioContextType::RINGTONE |
526 LeAudioContextType::ALERTS | LeAudioContextType::EMERGENCYALARM;
527
528 /* Print formaters for LTV data */
529 std::string CodecCapabilitiesLtvFormat(const uint8_t& type,
530 const std::vector<uint8_t>& value);
531
532 /* Structures */
533 class LeAudioLtvMap {
534 public:
LeAudioLtvMap()535 LeAudioLtvMap() {}
LeAudioLtvMap(std::map<uint8_t,std::vector<uint8_t>> values)536 LeAudioLtvMap(std::map<uint8_t, std::vector<uint8_t>> values)
537 : values(std::move(values)) {}
538
539 std::optional<std::vector<uint8_t>> Find(uint8_t type) const;
Add(uint8_t type,std::vector<uint8_t> value)540 void Add(uint8_t type, std::vector<uint8_t> value) {
541 values.insert_or_assign(type, std::move(value));
542 }
Remove(uint8_t type)543 void Remove(uint8_t type) { values.erase(type); }
IsEmpty()544 bool IsEmpty() const { return values.empty(); }
Clear()545 void Clear() { values.clear(); }
Size()546 size_t Size() const { return values.size(); }
Values()547 const std::map<uint8_t, std::vector<uint8_t>>& Values() const {
548 return values;
549 }
550 std::string ToString(
551 const std::string& indent_string,
552 std::string (*format)(const uint8_t&, const std::vector<uint8_t>&)) const;
553 size_t RawPacketSize() const;
554 uint8_t* RawPacket(uint8_t* p_buf) const;
555 std::vector<uint8_t> RawPacket() const;
556 static LeAudioLtvMap Parse(const uint8_t* value, uint8_t len, bool& success);
557 void Append(const LeAudioLtvMap& other);
558
559 private:
560 std::map<uint8_t, std::vector<uint8_t>> values;
561 };
562
563 struct LeAudioLc3Config {
564 static const std::map<uint8_t, uint32_t> sampling_freq_map;
565 static const std::map<uint8_t, uint32_t> frame_duration_map;
566
567 std::optional<uint8_t> sampling_frequency;
568 std::optional<uint8_t> frame_duration;
569 std::optional<uint32_t> audio_channel_allocation;
570 std::optional<uint16_t> octets_per_codec_frame;
571 std::optional<uint8_t> codec_frames_blocks_per_sdu;
572
573 uint8_t channel_count;
574
575 /** Returns the sampling frequency representation in Hz */
GetSamplingFrequencyHzLeAudioLc3Config576 uint32_t GetSamplingFrequencyHz() const {
577 if (sampling_frequency)
578 return sampling_freq_map.count(*sampling_frequency)
579 ? sampling_freq_map.at(*sampling_frequency)
580 : 0;
581 return 0;
582 }
583
584 /** Returns the frame duration representation in us */
GetFrameDurationUsLeAudioLc3Config585 uint32_t GetFrameDurationUs() const {
586 if (frame_duration)
587 return frame_duration_map.count(*frame_duration)
588 ? frame_duration_map.at(*frame_duration)
589 : 0;
590
591 return 0;
592 }
593
GetChannelCountLeAudioLc3Config594 uint8_t GetChannelCount(void) const {
595 if (channel_count) return channel_count;
596
597 return 0;
598 }
599
GetAsLtvMapLeAudioLc3Config600 LeAudioLtvMap GetAsLtvMap() const {
601 std::map<uint8_t, std::vector<uint8_t>> values;
602
603 if (sampling_frequency) {
604 values[codec_spec_conf::kLeAudioCodecLC3TypeSamplingFreq] =
605 UINT8_TO_VEC_UINT8(*sampling_frequency);
606 }
607
608 if (frame_duration) {
609 values[codec_spec_conf::kLeAudioCodecLC3TypeFrameDuration] =
610 UINT8_TO_VEC_UINT8(*frame_duration);
611 }
612
613 if (audio_channel_allocation) {
614 values[codec_spec_conf::kLeAudioCodecLC3TypeAudioChannelAllocation] =
615 UINT32_TO_VEC_UINT8(*audio_channel_allocation);
616 }
617
618 if (octets_per_codec_frame) {
619 values[codec_spec_conf::kLeAudioCodecLC3TypeOctetPerFrame] =
620 UINT16_TO_VEC_UINT8(*octets_per_codec_frame);
621 }
622
623 if (codec_frames_blocks_per_sdu) {
624 values[codec_spec_conf::kLeAudioCodecLC3TypeCodecFrameBlocksPerSdu] =
625 UINT8_TO_VEC_UINT8(*codec_frames_blocks_per_sdu);
626 }
627
628 return LeAudioLtvMap(values);
629 }
630 };
631
632 struct LeAudioCodecId {
633 uint8_t coding_format;
634 uint16_t vendor_company_id;
635 uint16_t vendor_codec_id;
636
637 friend bool operator==(const LeAudioCodecId& lhs, const LeAudioCodecId& rhs) {
638 if (lhs.coding_format != rhs.coding_format) return false;
639
640 if (lhs.coding_format == kLeAudioCodingFormatVendorSpecific &&
641 (lhs.vendor_company_id != rhs.vendor_company_id ||
642 lhs.vendor_codec_id != rhs.vendor_codec_id))
643 return false;
644
645 return true;
646 }
647
648 friend bool operator!=(const LeAudioCodecId& lhs, const LeAudioCodecId& rhs) {
649 return !(lhs == rhs);
650 }
651 };
652
653 struct hdl_pair {
654 hdl_pair() = default;
hdl_pairhdl_pair655 hdl_pair(uint16_t val_hdl, uint16_t ccc_hdl)
656 : val_hdl(val_hdl), ccc_hdl(ccc_hdl) {}
657
658 uint16_t val_hdl = 0;
659 uint16_t ccc_hdl = 0;
660 };
661
662 struct ase {
663 static constexpr uint8_t kAseIdInvalid = 0x00;
664
665 ase(uint16_t val_hdl, uint16_t ccc_hdl, uint8_t direction,
666 uint8_t initial_id = kAseIdInvalid)
hdlsase667 : hdls(val_hdl, ccc_hdl),
668 id(initial_id),
669 cis_id(kInvalidCisId),
670 direction(direction),
671 target_latency(types::kTargetLatencyBalancedLatencyReliability),
672 active(false),
673 reconfigure(false),
674 data_path_state(AudioStreamDataPathState::IDLE),
675 configured_for_context_type(LeAudioContextType::UNINITIALIZED),
676 preferred_phy(0),
677 max_sdu_size(0),
678 retrans_nb(0),
679 max_transport_latency(0),
680 pres_delay_min(0),
681 pres_delay_max(0),
682 preferred_pres_delay_min(0),
683 preferred_pres_delay_max(0),
684 state(AseState::BTA_LE_AUDIO_ASE_STATE_IDLE) {}
685
686 struct hdl_pair hdls;
687 uint8_t id;
688 uint8_t cis_id;
689 const uint8_t direction;
690 uint8_t target_latency;
691 uint16_t cis_conn_hdl = 0;
692
693 bool active;
694 bool reconfigure;
695 AudioStreamDataPathState data_path_state;
696 LeAudioContextType configured_for_context_type;
697
698 /* Codec configuration */
699 LeAudioCodecId codec_id;
700 LeAudioLc3Config codec_config;
701 uint8_t framing;
702 uint8_t preferred_phy;
703
704 /* Qos configuration */
705 uint16_t max_sdu_size;
706 uint8_t retrans_nb;
707 uint16_t max_transport_latency;
708 uint32_t pres_delay_min;
709 uint32_t pres_delay_max;
710 uint32_t preferred_pres_delay_min;
711 uint32_t preferred_pres_delay_max;
712
713 std::vector<uint8_t> metadata;
714
715 AseState state;
716 };
717
718 struct BidirectAsesPair {
719 struct ase* sink;
720 struct ase* source;
721 };
722
723 struct acs_ac_record {
724 LeAudioCodecId codec_id;
725 LeAudioLtvMap codec_spec_caps;
726 std::vector<uint8_t> metadata;
727 };
728
729 using PublishedAudioCapabilities =
730 std::vector<std::tuple<hdl_pair, std::vector<acs_ac_record>>>;
731 using AudioLocations = std::bitset<32>;
732
733 std::ostream& operator<<(std::ostream& os, const AseState& state);
734 std::ostream& operator<<(std::ostream& os, const CigState& state);
735 std::ostream& operator<<(std::ostream& os, const LeAudioLc3Config& config);
736 std::string contextTypeToStr(const LeAudioContextType& context);
737 std::ostream& operator<<(std::ostream& os, const LeAudioContextType& context);
738 std::ostream& operator<<(std::ostream& os,
739 const AudioStreamDataPathState& state);
740 std::ostream& operator<<(std::ostream& os, const AudioContexts& contexts);
741 } // namespace types
742
743 namespace set_configurations {
744
745 struct CodecCapabilitySetting {
746 types::LeAudioCodecId id;
747
748 /* Codec Specific Configuration variant */
749 std::variant<types::LeAudioLc3Config> config;
750
751 /* Sampling freqency requested for codec */
752 uint32_t GetConfigSamplingFrequency() const;
753 /* Data fetch/feed interval for codec in microseconds */
754 uint32_t GetConfigDataIntervalUs() const;
755 /* Audio bit depth required for codec */
756 uint8_t GetConfigBitsPerSample() const;
757 /* Audio channels number for stream */
758 uint8_t GetConfigChannelCount() const;
759 };
760
761 struct QosConfigSetting {
762 uint8_t target_latency;
763 uint8_t retransmission_number;
764 uint16_t max_transport_latency;
765 };
766
767 struct SetConfiguration {
768 SetConfiguration(uint8_t direction, uint8_t device_cnt, uint8_t ase_cnt,
769 CodecCapabilitySetting codec,
770 QosConfigSetting qos = {.retransmission_number = 0,
771 .max_transport_latency = 0},
772 le_audio::types::LeAudioConfigurationStrategy strategy =
773 le_audio::types::LeAudioConfigurationStrategy::
774 MONO_ONE_CIS_PER_DEVICE)
directionSetConfiguration775 : direction(direction),
776 device_cnt(device_cnt),
777 ase_cnt(ase_cnt),
778 codec(codec),
779 qos(qos),
780 strategy(strategy) {}
781
782 uint8_t direction; /* Direction of set */
783 uint8_t device_cnt; /* How many devices must be in set */
784 uint8_t ase_cnt; /* How many ASE we need in configuration */
785 CodecCapabilitySetting codec;
786 QosConfigSetting qos;
787 types::LeAudioConfigurationStrategy strategy;
788 };
789
790 /* Defined audio scenarios */
791 struct AudioSetConfiguration {
792 std::string name;
793 std::vector<struct SetConfiguration> confs;
794 };
795
796 using AudioSetConfigurations = std::vector<const AudioSetConfiguration*>;
797
798 const types::LeAudioCodecId LeAudioCodecIdLc3 = {
799 .coding_format = types::kLeAudioCodingFormatLC3,
800 .vendor_company_id = types::kLeAudioVendorCompanyIdUndefined,
801 .vendor_codec_id = types::kLeAudioVendorCodecIdUndefined};
802
803 static constexpr uint32_t kChannelAllocationStereo =
804 codec_spec_conf::kLeAudioLocationFrontLeft |
805 codec_spec_conf::kLeAudioLocationFrontRight;
806
807 /* Declarations */
808 void get_cis_count(const AudioSetConfigurations& audio_set_configurations,
809 int expected_device_cnt,
810 types::LeAudioConfigurationStrategy strategy,
811 int group_ase_snk_cnt, int group_ase_src_count,
812 uint8_t& cis_count_bidir, uint8_t& cis_count_unidir_sink,
813 uint8_t& cis_count_unidir_source);
814 bool check_if_may_cover_scenario(
815 const AudioSetConfigurations* audio_set_configurations, uint8_t group_size);
816 bool check_if_may_cover_scenario(
817 const AudioSetConfiguration* audio_set_configuration, uint8_t group_size);
818 bool IsCodecCapabilitySettingSupported(
819 const types::acs_ac_record& pac_record,
820 const CodecCapabilitySetting& codec_capability_setting);
821 uint8_t get_num_of_devices_in_configuration(
822 const AudioSetConfiguration* audio_set_configuration);
823 } // namespace set_configurations
824
825 struct stream_map_info {
stream_map_infostream_map_info826 stream_map_info(uint16_t stream_handle, uint32_t audio_channel_allocation,
827 bool is_stream_active)
828 : stream_handle(stream_handle),
829 audio_channel_allocation(audio_channel_allocation),
830 is_stream_active(is_stream_active) {}
831 uint16_t stream_handle;
832 uint32_t audio_channel_allocation;
833 bool is_stream_active;
834 };
835
836 struct stream_configuration {
837 bool pending_configuration;
838
839 types::LeAudioCodecId id;
840
841 /* Pointer to chosen req */
842 const le_audio::set_configurations::AudioSetConfiguration* conf;
843
844 /* Sink configuration */
845 /* For now we have always same frequency for all the channels */
846 uint32_t sink_sample_frequency_hz;
847 uint32_t sink_frame_duration_us;
848 uint16_t sink_octets_per_codec_frame;
849 uint32_t sink_audio_channel_allocation;
850 uint8_t sink_codec_frames_blocks_per_sdu;
851 /* Number of channels is what we will request from audio framework */
852 uint8_t sink_num_of_channels;
853 int sink_num_of_devices;
854 /* cis_handle, audio location*/
855 std::vector<std::pair<uint16_t, uint32_t>> sink_streams;
856 /* cis_handle, target allocation, stream active state */
857 std::vector<stream_map_info> sink_offloader_streams_target_allocation;
858 /* cis_handle, current allocation, stream active state */
859 std::vector<stream_map_info> sink_offloader_streams_current_allocation;
860 bool sink_offloader_changed;
861 bool sink_is_initial;
862
863 /* Source configuration */
864 /* For now we have always same frequency for all the channels */
865 uint32_t source_sample_frequency_hz;
866 uint32_t source_frame_duration_us;
867 uint16_t source_octets_per_codec_frame;
868 uint32_t source_audio_channel_allocation;
869 uint8_t source_codec_frames_blocks_per_sdu;
870 /* Number of channels is what we will request from audio framework */
871 uint8_t source_num_of_channels;
872 int source_num_of_devices;
873 /* cis_handle, audio location*/
874 std::vector<std::pair<uint16_t, uint32_t>> source_streams;
875 /* cis_handle, target allocation, stream active state */
876 std::vector<stream_map_info> source_offloader_streams_target_allocation;
877 /* cis_handle, current allocation, stream active state */
878 std::vector<stream_map_info> source_offloader_streams_current_allocation;
879 bool source_offloader_changed;
880 bool source_is_initial;
881 bool is_active;
882 };
883
884 void AppendMetadataLtvEntryForCcidList(std::vector<uint8_t>& metadata,
885 const std::vector<uint8_t>& ccid_list);
886 void AppendMetadataLtvEntryForStreamingContext(
887 std::vector<uint8_t>& metadata, types::AudioContexts context_type);
888 uint8_t GetMaxCodecFramesPerSduFromPac(const types::acs_ac_record* pac_record);
889 uint32_t AdjustAllocationForOffloader(uint32_t allocation);
890 } // namespace le_audio