xref: /aosp_15_r20/external/pigweed/pw_bluetooth_sapphire/public/pw_bluetooth_sapphire/internal/host/sdp/sdp.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #pragma once
16 #include <list>
17 #include <map>
18 #include <optional>
19 #include <string>
20 #include <vector>
21 
22 #include "pw_bluetooth_sapphire/internal/host/common/uuid.h"
23 #include "pw_bluetooth_sapphire/internal/host/sdp/data_element.h"
24 
25 namespace bt::sdp {
26 
27 // Each service has a service record handle that uniquely identifies that
28 // service within an SDP server.
29 using ServiceHandle = uint32_t;
30 
31 // Handle to service record representing SDP itself. (Vol 3, Part B, 2.2)
32 constexpr ServiceHandle kSDPHandle = 0x00000000;
33 // Handles 0x0000001 - 0x0000FFFF are reserved (Vol 3, Part B, 5.1.1)
34 constexpr ServiceHandle kFirstUnreservedHandle = 0x00010000;
35 constexpr ServiceHandle kLastHandle = 0xFFFFFFFF;
36 
37 // Valid security levels for services. (Vol 3, Part C, 5.2.2)
38 enum class SecurityLevel : uint8_t {
39   kNone = 0,
40   kEncOptional = 1,
41   kEncRequired = 2,
42   kMITMProtected = 3,
43   kHighStrength = 4,
44 };
45 
46 // Vol 3, Part B, 2.3.1
47 using AttributeId = uint16_t;
48 
49 // Referred to as "PDU ID" in the spec.
50 using OpCode = uint8_t;
51 
52 using TransactionId = uint16_t;
53 
54 // Header for each SDP PDU.
55 // v5.0, Vol 3, Part B, 4.2
56 struct Header {
57   OpCode pdu_id;
58   TransactionId tid;
59   uint16_t param_length;
60 } __attribute__((packed));
61 
62 // v5.0, Vol 3, Part B, 4.4.1
63 enum class ErrorCode : uint16_t {
64   kUnsupportedVersion = 0x0001,
65   kInvalidRecordHandle = 0x0002,
66   kInvalidRequestSyntax = 0x0003,
67   kInvalidSize = 0x0004,
68   kInvalidContinuationState = 0x0005,
69   kInsufficientResources = 0x0006,
70 };
71 
72 // ===== SDP PDUs =====
73 constexpr OpCode kReserved = 0x00;
74 
75 // Error Handling
76 constexpr OpCode kErrorResponse = 0x01;
77 
78 // Service Search Transaction
79 constexpr OpCode kServiceSearchRequest = 0x02;
80 constexpr OpCode kServiceSearchResponse = 0x03;
81 
82 // Service Attribute Transaction
83 constexpr OpCode kServiceAttributeRequest = 0x04;
84 constexpr OpCode kServiceAttributeResponse = 0x05;
85 
86 // Service Search Attribute Transaction
87 constexpr OpCode kServiceSearchAttributeRequest = 0x06;
88 constexpr OpCode kServiceSearchAttributeResponse = 0x07;
89 
90 // ====== SDP Protocol UUIDs ======
91 // Defined in the Bluetooth Assigned Numbers:
92 // https://www.bluetooth.com/specifications/assigned-numbers/service-discovery
93 
94 namespace protocol {
95 
96 constexpr UUID kSDP(uint16_t{0x0001});
97 constexpr UUID kRFCOMM(uint16_t{0x0003});
98 constexpr UUID kATT(uint16_t{0x0007});
99 constexpr UUID kOBEX(uint16_t{0x0008});  // IrDA Interop
100 constexpr UUID kBNEP(uint16_t{0x000F});
101 constexpr UUID kHIDP(uint16_t{0x0011});
102 constexpr UUID kAVCTP(uint16_t{0x0017});
103 constexpr UUID kAVDTP(uint16_t{0x0019});
104 constexpr UUID kL2CAP(uint16_t{0x0100});
105 
106 }  // namespace protocol
107 
108 // ====== SDP Profile / Class UUIDs =====
109 // Defined in the Bluetooth Assigned Numbers:
110 // https://www.bluetooth.com/specifications/assigned-numbers/service-discovery
111 
112 namespace profile {
113 
114 // Service Discovery Profile (SDP)
115 constexpr UUID kServiceDiscoveryClass(uint16_t{0x1000});
116 constexpr UUID kBrowseGroupClass(uint16_t{0x1001});
117 // Serial Port Profile (SPP)
118 constexpr UUID kSerialPort(uint16_t{0x1101});
119 // Dial-up Networking Profile (DUN)
120 constexpr UUID kDialupNetworking(uint16_t{0x1103});
121 // Object Push Profile (OPP)
122 constexpr UUID kObexObjectPush(uint16_t{0x1105});
123 // File Transfer Profile (FTP)
124 constexpr UUID kObexFileTransfer(uint16_t{0x1106});
125 // Headset Profile (HSP)
126 constexpr UUID kHeadset(uint16_t{0x1108});
127 constexpr UUID kHeadsetAudioGateway(uint16_t{0x1112});
128 constexpr UUID kHeadsetHS(uint16_t{0x1131});
129 // Advanced Audio Distribution Profile (A2DP)
130 constexpr UUID kAudioSource(uint16_t{0x110A});
131 constexpr UUID kAudioSink(uint16_t{0x110B});
132 constexpr UUID kAdvancedAudioDistribution(uint16_t{0x110D});
133 // Audio/Video Remote Control Profile (AVRCP)
134 constexpr UUID kAVRemoteControlTarget(uint16_t{0x110C});
135 constexpr UUID kAVRemoteControl(uint16_t{0x110E});
136 constexpr UUID kAVRemoteControlController(uint16_t{0x110F});
137 // Personal Area Networking (PAN)
138 constexpr UUID kPANU(uint16_t{0x1115});
139 constexpr UUID kNAP(uint16_t{0x1116});
140 constexpr UUID kGN(uint16_t{0x1117});
141 // Basic Printing and Basic Imaging Profiles omitted (unsupported)
142 // Hands-Free Profile (HFP)
143 constexpr UUID kHandsfree(uint16_t{0x111E});
144 constexpr UUID kHandsfreeAudioGateway(uint16_t{0x111F});
145 // Human Interface Device omitted (unsupported)
146 // Hardcopy Cable Replacement Profile omitted (unsupported)
147 // Sim Access Profile (SAP)
148 constexpr UUID kSIM_Access(uint16_t{0x112D});
149 // Phonebook Access Profile (PBAP)
150 constexpr UUID kPhonebookPCE(uint16_t{0x112E});
151 constexpr UUID kPhonebookPSE(uint16_t{0x112F});
152 constexpr UUID kPhonebook(uint16_t{0x1130});
153 // Message Access Profile (MAP)
154 constexpr UUID kMessageAccessServer(uint16_t{0x1132});
155 constexpr UUID kMessageNotificationServer(uint16_t{0x1133});
156 constexpr UUID kMessageAccessProfile(uint16_t{0x1134});
157 // GNSS and 3DSP omitted (unsupported)
158 // Multi-Profile Specification (MPS)
159 constexpr UUID kMPSProfile(uint16_t{0x113A});
160 constexpr UUID kMPSClass(uint16_t{0x113B});
161 // Calendar, Task, and Notes Profile omitted (unsupported)
162 // Device ID
163 constexpr UUID kPeerIdentification(uint16_t{0x1200});
164 // Video Distribution Profile (VDP)
165 constexpr UUID kVideoSource(uint16_t{0x1303});
166 constexpr UUID kVideoSink(uint16_t{0x1304});
167 constexpr UUID kVideoDistribution(uint16_t{0x1305});
168 // Health Device Profile (HDP)
169 constexpr UUID kHDP(uint16_t{0x1400});
170 constexpr UUID kHDPSource(uint16_t{0x1401});
171 constexpr UUID kHDPSink(uint16_t{0x1402});
172 
173 }  // namespace profile
174 
175 // ====== SDP Attribute IDs ======
176 
177 // ====== Universal Attribute Definitions =====
178 // v5.0, Vol 3, Part B, Sec 5.1
179 
180 // Service Record Handle
181 constexpr AttributeId kServiceRecordHandle = 0x0000;
182 
183 using ServiceRecordHandleValueType = uint32_t;
184 
185 // Service Class ID List
186 constexpr AttributeId kServiceClassIdList = 0x0001;
187 
188 // A sequence of UUIDs.
189 // Must contain at least one UUID.
190 using ServiceClassIdListValueType = std::vector<DataElement>;
191 
192 // Service Record State
193 // Used to facilitate caching. If any part of the service record changes,
194 // this value must change.
195 constexpr AttributeId kServiceRecordState = 0x0002;
196 
197 using ServiceRecordStateValueType = uint32_t;
198 
199 // Service ID
200 constexpr AttributeId kServiceId = 0x0003;
201 
202 using ServiceIdValueType = UUID;
203 
204 // Protocol Descriptor List
205 constexpr AttributeId kProtocolDescriptorList = 0x0004;
206 
207 // This is a list of DataElementSequences, of which each has as its first
208 // element a Protocol UUID, followed by protocol-specific parameters.
209 // See v5.0, Vol 3, Part B, Sec 5.1.5
210 using ProtocolDescriptorListValueType = std::vector<DataElement>;
211 
212 // AdditionalProtocolDescriptorList
213 constexpr AttributeId kAdditionalProtocolDescriptorList = 0x000D;
214 
215 // This is a sequence of Protocol Descriptor Lists
216 using AdditionalProtocolDescriptorListValueType = std::vector<DataElement>;
217 
218 // Browse Group List
219 // Browse Group lists are described in v5.0, Vol 3, Part B, Sec 2.6
220 constexpr AttributeId kBrowseGroupList = 0x0005;
221 
222 // This is a sequence which is composed of UUIDs of the groups that this
223 // service belongs to.
224 using BrowseGroupListValueType = std::vector<DataElement>;
225 
226 // The UUID used for the root of the browsing hierarchy
227 constexpr UUID kPublicBrowseRootUuid(uint16_t{0x1002});
228 
229 // Language Base Attribute Id List
230 constexpr AttributeId kLanguageBaseAttributeIdList = 0x0006;
231 
232 // A sequence of uint16_t triplets containing:
233 //  - An identifier for a natural language from ISO 639:1988 (E/F)
234 //  - A character encoding to use for the language (UTF-8 is 106)
235 //  - An attribute ID as the base attribute for offset for human-readable
236 //    information about the service.
237 using LanguageBaseAttributeIdListValueType = std::vector<DataElement>;
238 
239 // Service Info TTL
240 // Number of seconds that the service record is expected to be valid and
241 // unchanged.
242 constexpr AttributeId kServiceInfoTimeToLive = 0x0007;
243 
244 using ServiceInfoTimeToLiveValueType = uint32_t;
245 
246 // Service Availability
247 // Represents the relative ability of the service to accept additional clients.
248 // 0x00 means no clients can connect, 0xFF means no one is using it.
249 // See Vol 3, Part B, 5.1.10
250 constexpr AttributeId kServiceAvailability = 0x0008;
251 
252 using ServiceAvailabilityValueType = uint8_t;
253 
254 // Bluetooth Profile Descriptor List
255 constexpr AttributeId kBluetoothProfileDescriptorList = 0x0009;
256 
257 // A Sequence of Sequences with:
258 //  - a UUID for a profile
259 //  - A 16-bit unsigned version number with:
260 //     - 8 MSbits major version
261 //     - 8 LSbits minor version
262 using BluetoothProfileDescriptorListValueType = std::vector<DataElement>;
263 
264 // TODO(fxbug.dev/42078669): Documentation URL, ClientExecutableURL, IconURL
265 // When we support the URL type.
266 
267 // ##### Language Attribute Offsets #####
268 // These must be added to the attribute ID retrieved from the
269 // LanguageBaseAttributeIdList
270 
271 // Service Name
272 constexpr AttributeId kServiceNameOffset = 0x0000;
273 
274 using ServiceNameValueType = std::string;
275 
276 // Service Description
277 constexpr AttributeId kServiceDescriptionOffset = 0x0001;
278 
279 using ServiceDescriptionValueType = std::string;
280 
281 // Provider Name
282 constexpr AttributeId kProviderNameOffset = 0x0002;
283 
284 using ProviderNameValueType = std::string;
285 
286 // ===== ServiceDiscoveryServer Service Class Attribute Definitions ======
287 // These attributes are defined as valid for the ServiceDiscoveryServer.
288 // See Spec v5.0, Vol 3, Part B, Section 5.2
289 
290 // VersionNumberList is a list of the versions supported by the SDP server.
291 // See v5.0, Vol 3, Part B, Section 5.2.3
292 constexpr AttributeId kSDP_VersionNumberList = 0x0200;
293 
294 using SDP_VersionNumberListType = std::vector<DataElement>;
295 
296 // ServiceDatabaseState is a 32-bit integer that is changed whenever any other
297 // service records are added or deleted from the database.
298 constexpr AttributeId kSDP_ServiceDatabaseState = 0x0201;
299 
300 // ===== Advanced Audio Distribution Profile Attribute Definitions ======
301 // These attributes are defined as valid for the AudioSource and AudioSink
302 // Service Class UUIDs in the Assigned Numbers for SDP
303 // https://www.bluetooth.com/specifications/assigned-numbers/service-discovery
304 constexpr AttributeId kA2DP_SupportedFeatures = 0x0311;
305 
306 // ===== OBEX Protocol Attribute Definitions =====
307 // This attribute is defined on a per-profile basis, and is the same for all
308 // relevant profiles that require OBEX. See
309 // https://www.bluetooth.com/specifications/assigned-numbers/service-discovery
310 constexpr AttributeId kGoepL2capPsm = 0x0200;
311 
312 }  // namespace bt::sdp
313