1 /******************************************************************************
2  *
3  *  Copyright 1999-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 
21 #include <base/functional/callback_forward.h>
22 #include <base/strings/stringprintf.h>
23 
24 #include <cstdint>
25 
26 #include "stack/include/sdp_callback.h"
27 #include "stack/include/sdp_device_id.h"
28 #include "stack/include/sdp_status.h"
29 #include "stack/include/sdpdefs.h"
30 #include "stack/sdp/internal/sdp_api.h"
31 #include "stack/sdp/sdp_discovery_db.h"
32 #include "types/bluetooth/uuid.h"
33 #include "types/raw_address.h"
34 
35 namespace bluetooth {
36 namespace legacy {
37 namespace stack {
38 namespace sdp {
39 
40 struct tSdpApi {
41   struct {
42     /*******************************************************************************
43       Function         SDP_InitDiscoveryDb
44 
45       Description      This function is called to initialize a discovery
46                        database.
47 
48       Parameters:      p_db        - (input) address of an area of memory where
49                                              the discovery database is managed.
50                        len         - (input) size (in bytes) of the memory
51                         NOTE: This must be larger than sizeof(tSDP_DISCOVERY_DB)
52                        num_uuid    - (input) number of UUID filters applied
53                        p_uuid_list - (input) list of UUID filters
54                        num_attr    - (input) number of attribute filters
55                                              applied
56                        p_attr_list - (input) list of attribute filters
57 
58       Returns          true if successful, false if one or more parameters are
59                        bad
60      ******************************************************************************/
61     [[nodiscard]] bool (*SDP_InitDiscoveryDb)(tSDP_DISCOVERY_DB*, uint32_t, uint16_t,
62                                               const bluetooth::Uuid*, uint16_t, const uint16_t*);
63 
64     /*******************************************************************************
65 
66       Function         SDP_CancelServiceSearch
67 
68       Description      This function cancels an active query to an SDP server.
69 
70       Parameters:      p_db        - (input) address of an area of memory where
71                                              the discovery database is managed.
72 
73       Returns          true if discovery cancelled, false if a matching
74                        activity is not found.
75 
76      ******************************************************************************/
77     [[nodiscard]] bool (*SDP_CancelServiceSearch)(const tSDP_DISCOVERY_DB*);
78 
79     /*******************************************************************************
80 
81       Function         SDP_ServiceSearchRequest
82 
83       Description      This function queries an SDP server for information.
84 
85       Parameters:      p_db        - (input) address of an area of memory where
86                                              the discovery database is managed.
87                        p_cb        - (input) callback executed when complete
88 
89       Returns          true if discovery started, false if failed.
90 
91      ******************************************************************************/
92     [[nodiscard]] bool (*SDP_ServiceSearchRequest)(const RawAddress&, tSDP_DISCOVERY_DB*,
93                                                    tSDP_DISC_CMPL_CB*);
94 
95     /*******************************************************************************
96 
97       Function         SDP_ServiceSearchAttributeRequest
98 
99       Description      This function queries an SDP server for information.
100 
101                        The difference between this API function and the
102                        function SDP_ServiceSearchRequest is that this one does
103                        a combined ServiceSearchAttributeRequest SDP function.
104 
105       Parameters:      bd_addr     - (input) device address for service search
106                        p_db        - (input) address of an area of memory where
107                                              the discovery database is managed.
108                        p_cb        - (input) callback executed when complete
109 
110       Returns          true if discovery started, false if failed.
111 
112      ******************************************************************************/
113     [[nodiscard]] bool (*SDP_ServiceSearchAttributeRequest)(const RawAddress&, tSDP_DISCOVERY_DB*,
114                                                             tSDP_DISC_CMPL_CB*);
115 
116     /*******************************************************************************
117 
118       Function         SDP_ServiceSearchAttributeRequest2
119 
120       Description      This function queries an SDP server for information.
121 
122                        The difference between this API function and the
123                        function SDP_ServiceSearchRequest is that this one does
124                        a combined ServiceSearchAttributeRequest SDP function
125                        with the user data piggyback
126 
127       parameters:      bd_addr     - (input) device address for service search
128                        p_db        - (input) address of an area of memory where
129                                              the discovery database is managed.
130                        complete_callback - (input) callback executed when
131      complete
132 
133       Returns          true if discovery started, false if failed.
134 
135      ******************************************************************************/
136     [[nodiscard]] bool (*SDP_ServiceSearchAttributeRequest2)(
137             const RawAddress&, tSDP_DISCOVERY_DB*,
138             base::RepeatingCallback<tSDP_DISC_CMPL_CB> complete_callback);
139   } service;
140 
141   struct {
142     /*******************************************************************************
143 
144       Function         SDP_FindServiceInDb
145 
146       Description      This function queries an SDP database for a specific
147                        service. If the p_start_rec pointer is NULL, it looks
148                        from the beginning of the database, else it continues
149                        from the next record after p_start_rec.
150 
151       parameters:      p_db        - (input) address of an area of memory where
152                                              the discovery database is managed.
153                        uuid16      - (input) Uuid to search in db
154                        disc_rec    - (output) Record found, null otherwise
155 
156       Returns          Pointer to record containing service class, or NULL
157 
158      ******************************************************************************/
159     [[nodiscard]] tSDP_DISC_REC* (*SDP_FindServiceInDb)(const tSDP_DISCOVERY_DB*, uint16_t,
160                                                         tSDP_DISC_REC*);
161 
162     /*******************************************************************************
163 
164       Function         SDP_FindServiceUUIDInDb
165 
166       Description      This function queries an SDP database for a specific
167                        service. If the p_start_rec pointer is NULL, it looks
168                        from the beginning of the database, else it continues
169                        from the next record after p_start_rec.
170 
171       NOTE             the only difference between this function and the
172                        previous function "SDP_FindServiceInDb()" is that this
173                        function takes a Uuid input.
174 
175       parameters:      p_db        - (input) address of an area of memory where
176                                              the discovery database is managed.
177                        uuid        - (input) Uuid to search in db
178                        disc_rec    - (input) Start record, null from beginning
179 
180       Returns          Pointer to record containing service class, or NULL
181 
182      ******************************************************************************/
183     [[nodiscard]] tSDP_DISC_REC* (*SDP_FindServiceUUIDInDb)(const tSDP_DISCOVERY_DB*,
184                                                             const bluetooth::Uuid&, tSDP_DISC_REC*);
185 
186     /*******************************************************************************
187 
188       Function         SDP_FindServiceInDb_128bit
189 
190       Description      Query an SDP database for a specific service.
191                        If the p_start_rec pointer is NULL, look from the
192                        beginning of the database, else continue from the next
193                        record after p_start_rec.
194 
195       parameters:      p_db        - (input) address of an area of memory where
196                                              the discovery database is managed.
197                        disc_rec    - (input) Start record, null from beginning
198 
199       Returns          Pointer to record containing service class, or NULL
200 
201      ******************************************************************************/
202     [[nodiscard]] tSDP_DISC_REC* (*SDP_FindServiceInDb_128bit)(const tSDP_DISCOVERY_DB*,
203                                                                tSDP_DISC_REC*);
204   } db;
205 
206   struct {
207     /*******************************************************************************
208 
209       Local discovery database API
210 
211       Function         SDP_FindAttributeInRec
212 
213       Description      This function searches an SDP discovery record for a
214                        specific attribute.
215 
216       parameters:      disc_rec    - (input) Start record must not be null
217                        attr_id     - (input) Attribute id to search
218 
219       Returns          Pointer to matching attribute entry, or NULL
220 
221      ******************************************************************************/
222     [[nodiscard]] tSDP_DISC_ATTR* (*SDP_FindAttributeInRec)(const tSDP_DISC_REC*, uint16_t);
223 
224     /*******************************************************************************
225 
226       Function         SDP_FindServiceUUIDInRec_128bit
227 
228       Description      Read the 128-bit service UUID within a record;
229                        if there is any.
230 
231       Parameters:      p_rec      - (input) pointer to a SDP record.
232                        p_uuid     - (output) parameter to save the UUID found.
233 
234       Returns          true if found, otherwise false.
235 
236      ******************************************************************************/
237     [[nodiscard]] bool (*SDP_FindServiceUUIDInRec_128bit)(const tSDP_DISC_REC*, bluetooth::Uuid*);
238 
239     /*******************************************************************************
240 
241       Function         SDP_FindProtocolListElemInRec
242 
243       Description      This function looks at a specific discovery record for a
244                        protocol list element.
245 
246       Parameters:      p_rec      - (input) pointer to a SDP record.
247                        p_uuid     - (input) layer UUID.
248                        p_elem     - (output) protocol element
249 
250       Returns          true if found, false if not
251                        If found, the passed protocol list element is filled in.
252 
253      ******************************************************************************/
254     [[nodiscard]] bool (*SDP_FindProtocolListElemInRec)(const tSDP_DISC_REC*, uint16_t,
255                                                         tSDP_PROTOCOL_ELEM*);
256 
257     /*******************************************************************************
258 
259       Function         SDP_FindProfileVersionInRec
260 
261       Description      This function looks at a specific discovery record for
262                        the Profile list descriptor, and pulls out the version
263                        number. The version number consists of an 8-bit major
264                        version and an 8-bit minor version.
265 
266       Parameters:      p_rec      - (input) pointer to a SDP record.
267                        p_uuid     - (input) profile UUID.
268                        p_elem     - (output) major and minor version numbers
269 
270       Returns          true if found, false if not
271 
272      ******************************************************************************/
273     [[nodiscard]] bool (*SDP_FindProfileVersionInRec)(const tSDP_DISC_REC*, uint16_t, uint16_t*);
274 
275     /*******************************************************************************
276 
277       Function         SDP_FindServiceUUIDInRec
278 
279       Description      Read the service UUID within a record;
280                        if there is any.
281 
282       Parameters:      p_rec      - (input) pointer to a SDP record.
283                        p_uuid     - (output) found UUID or null.
284 
285       Returns          true if found, otherwise false.
286 
287      ******************************************************************************/
288     [[nodiscard]] bool (*SDP_FindServiceUUIDInRec)(const tSDP_DISC_REC* p_rec,
289                                                    bluetooth::Uuid* p_uuid);
290   } record;
291 
292   struct {
293     /*******************************************************************************
294 
295       API into SDP for Local service database updates
296 
297       Function         SDP_CreateRecord
298 
299       Description      This function is called to create a record in the
300                        database. This would be through the SDP database
301                        maintenance API. The record is created empty, the
302                        application should then call "add_attribute" *to add
303                        the record's attributes.
304 
305       Returns          Record handle if OK, else 0.
306 
307      ******************************************************************************/
308     [[nodiscard]] uint32_t (*SDP_CreateRecord)(void);
309 
310     /*******************************************************************************
311 
312       Function         SDP_DeleteRecord
313 
314       Description      This function is called to add a record (or all records)
315                        from the database. This would be through the SDP
316                        database maintenance API.
317 
318       Parameters:      handle     - (input) Handle to delete, 0 for all records
319                                             to be deleted
320 
321       Returns          true if succeeded, else false
322 
323      ******************************************************************************/
324     [[nodiscard]] bool (*SDP_DeleteRecord)(uint32_t);
325 
326     /*******************************************************************************
327 
328       Function         SDP_AddAttribute
329 
330       Description      This function is called to add an attribute to a record.
331                        This would be through the SDP database maintenance API.
332                        If the attribute already exists in the record, it is
333                        replaced with the new value.
334 
335       NOTE             Attribute values must be passed as a Big Endian stream.
336 
337       Parameters:      handle     - (input) Handle to add
338                        attr_id    - (input) Attribute id to add
339                        attr_type  - (input) Attribute type to add
340                        attr_len   - (input) Attribute data length
341                        p_val      - (input) Attribute data value
342 
343       Returns          true if added OK, else false
344 
345      ******************************************************************************/
346     [[nodiscard]] bool (*SDP_AddAttribute)(uint32_t handle, uint16_t attr_id, uint8_t attr_type,
347                                            uint32_t attr_len, uint8_t* p_val);
348 
349     /*******************************************************************************
350 
351       Function         SDP_AddSequence
352 
353       Description      This function is called to add a sequence to a record.
354                        This would be through the SDP database maintenance API.
355                        If the sequence already exists in the record, it is
356                        replaced with the new sequence.
357 
358       NOTE             Element values must be passed as a Big Endian stream.
359 
360       Parameters:      handle     - (input) Handle to add
361                        attr_id    - (input) Attribute id to add
362                        num_elem   - (input) Number of elements in array
363                        type[]     - (input) Element type
364                        len[]      - (input) Element data length
365                        p_val[]    - (input) Element data value
366 
367       Returns          true if added OK, else false
368 
369      ******************************************************************************/
370     [[nodiscard]] bool (*SDP_AddSequence)(uint32_t handle, uint16_t attr_id, uint16_t num_elem,
371                                           uint8_t type[], uint8_t len[], uint8_t* p_val[]);
372 
373     /*******************************************************************************
374 
375       Function         SDP_AddUuidSequence
376 
377       Description      This function is called to add a UUID sequence to a
378                        record. This would be through the SDP database
379                        maintenance API. If the sequence already exists in the
380                       record, it is replaced with the new sequence.
381 
382       Parameters:      handle     - (input) Handle to add
383                        attr_id    - (input) Attribute id to add
384                        num_uuids  - (input) Number of uuids in array
385                        p_uuids[]  - (input) Array uuid
386 
387       Returns          true if added OK, else false
388 
389      ******************************************************************************/
390     [[nodiscard]] bool (*SDP_AddUuidSequence)(uint32_t handle, uint16_t attr_id, uint16_t num_uuids,
391                                               uint16_t* p_uuids);
392 
393     /*******************************************************************************
394 
395       Function         SDP_AddProtocolList
396 
397       Description      This function is called to add a protocol descriptor
398                        list to a record. This would be through the SDP database
399                        maintenance API. If the protocol list already exists in
400                        the record, it is replaced with the new list.
401 
402       Parameters:      handle     - (input) Handle to add
403                        num_elem   - (input) Number of elements to add
404                        elem_list[]- (input) Element data list to add
405 
406       Returns          true if added OK, else false
407 
408      ******************************************************************************/
409     [[nodiscard]] bool (*SDP_AddProtocolList)(uint32_t handle, uint16_t num_elem,
410                                               tSDP_PROTOCOL_ELEM* p_elem_list);
411 
412     /*******************************************************************************
413 
414       Function         SDP_AddAdditionProtoLists
415 
416       Description      This function is called to add a protocol descriptor
417                        list to a record. This would be through the SDP database
418                        maintenance API. If the protocol list already exists in
419                        the record, it is replaced with the new list.
420 
421       Parameters:      handle     - (input) Handle to add
422                        num_elem   - (input) Number of elements to add
423                        proto_list[]- (input) Element data list to add
424 
425       Returns          true if added OK, else false
426 
427      ******************************************************************************/
428     [[nodiscard]] bool (*SDP_AddAdditionProtoLists)(uint32_t handle, uint16_t num_elem,
429                                                     tSDP_PROTO_LIST_ELEM* p_proto_list);
430 
431     /*******************************************************************************
432 
433       Function         SDP_AddProfileDescriptorList
434 
435       Description      This function is called to add a profile descriptor list
436                        to a record. This would be through the SDP database
437                        maintenance API. If the version already exists in the
438                        record, it is replaced with the new one.
439 
440       Parameters:      handle     - (input) Handle to add
441                        uuid       - (input) Uuid to add
442                        version    - (input) major and minor version
443 
444       Returns          true if added OK, else false
445 
446      ******************************************************************************/
447     [[nodiscard]] bool (*SDP_AddProfileDescriptorList)(uint32_t handle, uint16_t profile_uuid,
448                                                        uint16_t version);
449 
450     /*******************************************************************************
451 
452       Function         SDP_AddLanguageBaseAttrIDList
453 
454       Description      This function is called to add a language base attr list
455                        to a record. This would be through the SDP database
456                        maintenance API. If the version already exists in the
457                        record, it is replaced with the new one.
458 
459       Parameters:      handle     - (input) Handle to add
460                        lang       - (input) language base descriptor
461                        char_enc   - (input) character encoding
462                        base_id    - (input) base id
463 
464       Returns          true if added OK, else false
465 
466      ******************************************************************************/
467     [[nodiscard]] bool (*SDP_AddLanguageBaseAttrIDList)(uint32_t handle, uint16_t lang,
468                                                         uint16_t char_enc, uint16_t base_id);
469 
470     /*******************************************************************************
471 
472       Function         SDP_AddServiceClassIdList
473 
474       Description      This function is called to add a service list to a
475                        record. This would be through the SDP database
476                        maintenance API. If the service list already exists in
477                        the record, it is replaced with the new list.
478 
479       Parameters:      handle       - (input) Handle to add
480                        num_services - (input) number of services to add
481                        uuids[]      - (input) list of service uuids to add
482 
483       Returns          true if added OK, else false
484 
485      ******************************************************************************/
486     [[nodiscard]] bool (*SDP_AddServiceClassIdList)(uint32_t handle, uint16_t num_services,
487                                                     uint16_t* p_service_uuids);
488   } handle;
489 
490   struct {
491     /*******************************************************************************
492 
493       Device Identification API
494 
495       Function         SDP_SetLocalDiRecord
496 
497       Description      This function adds a DI record to the local SDP
498                        database.
499 
500       Parameters:      info         - (input) device identification record
501                        p_handle     - (output) handle of record if successful
502 
503       Returns          Returns tSDP_STATUS::SDP_SUCCESS if record added successfully, else
504                        error
505 
506      ******************************************************************************/
507     [[nodiscard]] tSDP_STATUS (*SDP_SetLocalDiRecord)(const tSDP_DI_RECORD* device_info,
508                                                       uint32_t* p_handle);
509 
510     /*******************************************************************************
511 
512       Device Identification API
513 
514       Function         SDP_DiDiscover
515 
516       Description      This function queries a remote device for DI
517                        information.
518 
519       Parameters:      bd_addr      - (input) remote device
520                        p_db         - (input) dicovery database
521                        len          - (input ) data base length
522                        p_cb         - (input) callback when complete
523 
524       Returns          tSDP_STATUS::SDP_SUCCESS if query started successfully, else error
525 
526      ******************************************************************************/
527     [[nodiscard]] tSDP_STATUS (*SDP_DiDiscover)(const RawAddress& remote_device,
528                                                 tSDP_DISCOVERY_DB* p_db, uint32_t len,
529                                                 tSDP_DISC_CMPL_CB* p_cb);
530 
531     /*******************************************************************************
532 
533       Device Identification API
534 
535       Function         SDP_GetNumDiRecords
536 
537       Description      Searches specified database for DI records
538 
539       Parameters:      p_db         - (input) dicovery database
540 
541       Returns          number of DI records found
542 
543      ******************************************************************************/
544     [[nodiscard]] uint8_t (*SDP_GetNumDiRecords)(const tSDP_DISCOVERY_DB* p_db);
545 
546     /*******************************************************************************
547 
548       Device Identification API
549 
550       Function         SDP_GetDiRecord
551 
552       Description      This function retrieves a remote device's DI record from
553                        the specified database.
554 
555       Parameters:      index        - (input) record index to retrieve
556                        device_info  - (input) dicovery database
557                        p_cb         - (input) callback when complete
558 
559       Returns          tSDP_STATUS::SDP_SUCCESS if record retrieved, else error
560 
561      ******************************************************************************/
562     [[nodiscard]] tSDP_STATUS (*SDP_GetDiRecord)(uint8_t getRecordIndex,
563                                                  tSDP_DI_GET_RECORD* device_info,
564                                                  const tSDP_DISCOVERY_DB* p_db);
565   } device_id;
566 };
567 
568 const struct tSdpApi* get_legacy_stack_sdp_api();
569 
570 struct tLegacyStackSdbCallback {
571   void(tSDP_DISC_CMPL_CB)(const RawAddress& bd_addr, tSDP_RESULT result);
572   void(tSDP_DISC_CMPL_CB2)(const RawAddress& bd_addr, tSDP_RESULT result, const void* user_data);
573 };
574 
575 }  // namespace sdp
576 }  // namespace stack
577 }  // namespace legacy
578 }  // namespace bluetooth
579 
580 /*******************************************************************************
581  *
582  * Function         SDP_Dumpsys
583  *
584  * Description      Dumps readable content of the module to the filedescriptor
585  *
586  * Parameters:      fd        - Valid file descriptor
587  *
588  * Returns          None
589  *
590  ******************************************************************************/
591 void SDP_Dumpsys(int fd);
592