1 /******************************************************************************
2  *
3  *  Copyright 2000-2012 Broadcom Corporation
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 #pragma once
20 #include <bluetooth/log.h>
21 
22 #include <cstdint>
23 #include <optional>
24 
25 /* Profile supported features */
26 #define A2DP_SUPF_PLAYER 0x0001
27 #define A2DP_SUPF_MIC 0x0002
28 #define A2DP_SUPF_TUNER 0x0004
29 #define A2DP_SUPF_MIXER 0x0008
30 
31 #define A2DP_SUPF_HEADPHONE 0x0001
32 #define A2DP_SUPF_SPEAKER 0x0002
33 #define A2DP_SUPF_RECORDER 0x0004
34 #define A2DP_SUPF_AMP 0x0008
35 
36 // AV Media Codec Types (Audio Codec ID).
37 // cf. Assigned Numbers § 6.5.1 Audio Codec ID
38 enum tA2DP_CODEC_TYPE : uint8_t {
39   A2DP_MEDIA_CT_SBC = 0x00,
40   A2DP_MEDIA_CT_MPEG_AUDIO = 0x01,
41   A2DP_MEDIA_CT_AAC = 0x02,
42   A2DP_MEDIA_CT_MPEG_USAC = 0x03,
43   A2DP_MEDIA_CT_ATRAC = 0x04,
44   A2DP_MEDIA_CT_NON_A2DP = 0xff,
45 };
46 
47 namespace bluetooth::a2dp {
48 
49 /// Generate the `CodecId` tag value for a vendor codec identified
50 /// by the input `company_id` and `codec_id`.
VendorCodecId(uint16_t company_id,uint16_t codec_id)51 constexpr static uint64_t VendorCodecId(uint16_t company_id, uint16_t codec_id) {
52   return 0xff | (static_cast<uint64_t>(company_id) << 8) | (static_cast<uint64_t>(codec_id) << 24);
53 }
54 
55 static constexpr uint16_t kAptxCompanyId = 0x004F;
56 static constexpr uint16_t kAptxHdCompanyId = 0x00D7;
57 static constexpr uint16_t kLdacCompanyId = 0x012D;
58 static constexpr uint16_t kOpusCompanyId = 0x00E0;
59 
60 static constexpr uint16_t kAptxCodecId = 0x0001;
61 static constexpr uint16_t kAptxHdCodecId = 0x0024;
62 static constexpr uint16_t kLdacCodecId = 0x00AA;
63 static constexpr uint16_t kOpusCodecId = 0x0001;
64 
65 /// Standardized codec identifiers.
66 ///
67 /// The codec identifier is 40 bits,
68 ///  - Bits 0-7: Media Codec Type, as defined by Assigned Numbers § 6.5.1 Audio Codec ID
69 ///          0x00: SBC
70 ///          0x02: MPEG-AAC
71 ///          0xFF: Vendor
72 ///  - Bits 8-23: Company ID,
73 ///          set to 0, if octet 0 is not 0xFF. The Company ID is contained
74 ///          within the codec Vendor ID, and specified by
75 ///          Assigned Numbers § 7 Company identifiers.
76 ///  - Bits 24-39: Vendor-defined codec ID,
77 ///          set to 0, if octet 0 is not 0xFF.
78 ///
79 /// Values are defined here for codecs that are supported by default.
80 /// Offloaded codecs may be referenced using this identifier and in this case the
81 /// identifier is constructed from the Media Codec Capabilities information.
82 enum class CodecId : uint64_t {
83   SBC = 0x00,
84   AAC = 0x02,
85   APTX = VendorCodecId(kAptxCompanyId, kAptxCodecId),
86   APTX_HD = VendorCodecId(kAptxHdCompanyId, kAptxHdCodecId),
87   LDAC = VendorCodecId(kLdacCompanyId, kLdacCodecId),
88   OPUS = VendorCodecId(kOpusCompanyId, kOpusCodecId),
89 };
90 
91 /// Parse the standardized codec identifier from the Media Codec Capabilities.
92 /// `media_codec_capabilities` must point to a non-null buffer that contains
93 /// well formed Media Codec Capabilities.
94 ///
95 /// Returns an error in one of three cases:
96 ///  - unsupported Media Codec Type
97 ///  - incorrectly formatted Media Codec Capabilities (e.g. truncated
98 ///    Codec Specific Information Elements for non-A2DP codecs)
99 ///  - incorrectly formatted Vendor ID (The upper 16 bits of the 32-
100 ///    bit Vendor ID shall be set to zero)
101 std::optional<CodecId> ParseCodecId(uint8_t const media_codec_capabilities[]);
102 
103 }  // namespace bluetooth::a2dp
104 
105 // Error codes returned in AVDTP reject signalling messages.
106 // The codes are specified from multiple sources as documented in the enum.
107 enum tA2DP_STATUS : uint8_t {
108   A2DP_SUCCESS = 0,
109 
110   // Custom error codes.
111   A2DP_FAIL = 0x0A,
112   A2DP_BUSY = 0x0B,
113 
114   // [AVDTP_1.3] 8.20.6.2 ERROR_CODE tables.
115   AVDTP_BAD_HEADER_FORMAT = 0x01,
116   AVDTP_BAD_LENGTH = 0x11,
117   AVDTP_BAD_ACP_SEID = 0x12,
118   AVDTP_SEP_IN_USE = 0x13,
119   AVDTP_SEP_NOT_IN_USE = 0x14,
120   AVDTP_BAD_SERV_CATEGORY = 0x17,
121   AVDTP_BAD_PAYLOAD_FORMAT = 0x18,
122   AVDTP_NOT_SUPPORTED_COMMAND = 0x19,
123   AVDTP_INVALID_CAPABILITIES = 0x1A,
124   AVDTP_BAD_RECOVERY_TYPE = 0x22,
125   AVDTP_BAD_MEDIA_TRANSPORT_FORMAT = 0x23,
126   AVDTP_BAD_RECOVERY_FORMAT = 0x25,
127   AVDTP_BAD_ROHC_FORMAT = 0x26,
128   AVDTP_BAD_CP_FORMAT = 0x27,
129   AVDTP_BAD_MULTIPLEXING_FORMAT = 0x28,
130   AVDTP_UNSUPPORTED_CONFIGURATION = 0x29,
131   AVDTP_BAD_STATE = 0x31,
132 
133   // [GAVDTP_1.3] 3.3 Error codes.
134   GAVDTP_BAD_SERVICE = 0x80,
135   GAVDTP_INSUFFICIENT_RESOURCES = 0x81,
136 
137   // [A2DP_1.3.2] 5.1.3 Error Codes.
138   A2DP_INVALID_CODEC_TYPE = 0xC1,
139   A2DP_NOT_SUPPORTED_CODEC_TYPE = 0xC2,
140   A2DP_INVALID_SAMPLING_FREQUENCY = 0xC3,
141   A2DP_NOT_SUPPORTED_SAMPLING_FREQUENCY = 0xC4,
142   A2DP_INVALID_CHANNEL_MODE = 0xC5,
143   A2DP_NOT_SUPPORTED_CHANNEL_MODE = 0xC6,
144   A2DP_INVALID_SUBBANDS = 0xC7,
145   A2DP_NOT_SUPPORTED_SUBBANDS = 0xC8,
146   A2DP_INVALID_ALLOCATION_METHOD = 0xC9,
147   A2DP_NOT_SUPPORTED_ALLOCATION_METHOD = 0xCA,
148   A2DP_INVALID_MINIMUM_BITPOOL_VALUE = 0xCB,
149   A2DP_NOT_SUPPORTED_MINIMUM_BITPOOL_VALUE = 0xCC,
150   A2DP_INVALID_MAXIMUM_BITPOOL_VALUE = 0xCD,
151   A2DP_NOT_SUPPORTED_MAXIMUM_BITPOOL_VALUE = 0xCE,
152   A2DP_INVALID_LAYER = 0xCF,
153   A2DP_NOT_SUPPORTED_LAYER = 0xD0,
154   A2DP_NOT_SUPPORTED_CRC = 0xD1,
155   A2DP_NOT_SUPPORTED_MPF = 0xD2,
156   A2DP_NOT_SUPPORTED_VBR = 0xD3,
157   A2DP_INVALID_BIT_RATE = 0xD4,
158   A2DP_NOT_SUPPORTED_BIT_RATE = 0xD5,
159   A2DP_INVALID_OBJECT_TYPE = 0xD6,
160   A2DP_NOT_SUPPORTED_OBJECT_TYPE = 0xD7,
161   A2DP_INVALID_CHANNELS = 0xD8,
162   A2DP_NOT_SUPPORTED_CHANNELS = 0xD9,
163   A2DP_INVALID_BLOCK_LENGTH = 0xDD,
164   A2DP_INVALID_CP_TYPE = 0xE0,
165   A2DP_INVALID_CP_FORMAT = 0xE1,
166   A2DP_INVALID_CODEC_PARAMETER = 0xE2,
167   A2DP_NOT_SUPPORTED_CODEC_PARAMETER = 0xE3,
168 };
169 
170 namespace std {
171 template <>
172 struct formatter<bluetooth::a2dp::CodecId> : enum_formatter<bluetooth::a2dp::CodecId> {};
173 template <>
174 struct formatter<tA2DP_CODEC_TYPE> : enum_formatter<tA2DP_CODEC_TYPE> {};
175 template <>
176 struct formatter<tA2DP_STATUS> : enum_formatter<tA2DP_STATUS> {};
177 }  // namespace std
178