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 #ifndef SDP_API_H
19 #define SDP_API_H
20 
21 #include "bt_target.h"
22 #include "sdpdefs.h"
23 
24 /*****************************************************************************
25  *  Constants
26  ****************************************************************************/
27 
28 /* Success code and error codes */
29 typedef enum : uint16_t {
30   SDP_SUCCESS = 0x0000,
31   SDP_INVALID_VERSION = 0x0001,
32   SDP_INVALID_SERV_REC_HDL = 0x0002,
33   SDP_INVALID_REQ_SYNTAX = 0x0003,
34   SDP_INVALID_PDU_SIZE = 0x0004,
35   SDP_INVALID_CONT_STATE = 0x0005,
36   SDP_NO_RESOURCES = 0x0006,
37   SDP_DI_REG_FAILED = 0x0007,
38   SDP_DI_DISC_FAILED = 0x0008,
39   SDP_NO_DI_RECORD_FOUND = 0x0009,
40   SDP_ERR_ATTR_NOT_PRESENT = 0x000A,
41   SDP_ILLEGAL_PARAMETER = 0x000B,
42 
43   HID_SDP_NO_SERV_UUID = (SDP_ILLEGAL_PARAMETER + 1),
44   HID_SDP_MANDATORY_MISSING,
45 
46   SDP_NO_RECS_MATCH = 0xFFF0,
47   SDP_CONN_FAILED = 0xFFF1,
48   SDP_CFG_FAILED = 0xFFF2,
49   SDP_GENERIC_ERROR = 0xFFF3,
50   SDP_DB_FULL = 0xFFF4,
51   SDP_CANCEL = 0xFFF8,
52 } tSDP_STATUS;
53 using tSDP_RESULT = tSDP_STATUS;
54 using tSDP_REASON = tSDP_STATUS;
55 
56 #define CASE_RETURN_TEXT(code) \
57   case code:                   \
58     return #code
59 
sdp_status_text(const tSDP_STATUS & status)60 static inline std::string sdp_status_text(const tSDP_STATUS& status) {
61   switch (status) {
62     CASE_RETURN_TEXT(SDP_SUCCESS);
63     CASE_RETURN_TEXT(SDP_INVALID_VERSION);
64     CASE_RETURN_TEXT(SDP_INVALID_SERV_REC_HDL);
65     CASE_RETURN_TEXT(SDP_INVALID_REQ_SYNTAX);
66     CASE_RETURN_TEXT(SDP_INVALID_PDU_SIZE);
67     CASE_RETURN_TEXT(SDP_INVALID_CONT_STATE);
68     CASE_RETURN_TEXT(SDP_NO_RESOURCES);
69     CASE_RETURN_TEXT(SDP_DI_REG_FAILED);
70     CASE_RETURN_TEXT(SDP_DI_DISC_FAILED);
71     CASE_RETURN_TEXT(SDP_NO_DI_RECORD_FOUND);
72     CASE_RETURN_TEXT(SDP_ERR_ATTR_NOT_PRESENT);
73     CASE_RETURN_TEXT(SDP_ILLEGAL_PARAMETER);
74 
75     CASE_RETURN_TEXT(HID_SDP_NO_SERV_UUID);
76     CASE_RETURN_TEXT(HID_SDP_MANDATORY_MISSING);
77 
78     CASE_RETURN_TEXT(SDP_NO_RECS_MATCH);
79     CASE_RETURN_TEXT(SDP_CONN_FAILED);
80     CASE_RETURN_TEXT(SDP_CFG_FAILED);
81     CASE_RETURN_TEXT(SDP_GENERIC_ERROR);
82     CASE_RETURN_TEXT(SDP_DB_FULL);
83     CASE_RETURN_TEXT(SDP_CANCEL);
84     default:
85       return std::string("UNKNOWN[%hhu]", status);
86   }
87 }
88 #undef CASE_RETURN_TEXT
89 
90 /* Masks for attr_value field of tSDP_DISC_ATTR */
91 #define SDP_DISC_ATTR_LEN_MASK 0x0FFF
92 #define SDP_DISC_ATTR_TYPE(len_type) ((len_type) >> 12)
93 #define SDP_DISC_ATTR_LEN(len_type) ((len_type)&SDP_DISC_ATTR_LEN_MASK)
94 
95 /* Maximum number of protocol list items (list_elem in tSDP_PROTOCOL_ELEM) */
96 #define SDP_MAX_LIST_ELEMS 3
97 
98 /*****************************************************************************
99  *  Type Definitions
100  ****************************************************************************/
101 
102 /* Define a callback function for when discovery is complete. */
103 typedef void(tSDP_DISC_CMPL_CB)(tSDP_RESULT result);
104 typedef void(tSDP_DISC_CMPL_CB2)(tSDP_RESULT result, void* user_data);
105 
106 typedef struct {
107   RawAddress peer_addr;
108   uint16_t peer_mtu;
109 } tSDP_DR_OPEN;
110 
111 typedef struct {
112   uint8_t* p_data;
113   uint16_t data_len;
114 } tSDP_DR_DATA;
115 
116 typedef union {
117   tSDP_DR_OPEN open;
118   tSDP_DR_DATA data;
119 } tSDP_DATA;
120 
121 /* Define a structure to hold the discovered service information. */
122 typedef struct {
123   union {
124     uint8_t u8;                         /* 8-bit integer            */
125     uint16_t u16;                       /* 16-bit integer           */
126     uint32_t u32;                       /* 32-bit integer           */
127     uint8_t array[4];                   /* Variable length field    */
128     struct t_sdp_disc_attr* p_sub_attr; /* Addr of first sub-attr (list)*/
129   } v;
130 
131 } tSDP_DISC_ATVAL;
132 
133 typedef struct t_sdp_disc_attr {
134   struct t_sdp_disc_attr* p_next_attr; /* Addr of next linked attr     */
135   uint16_t attr_id;                    /* Attribute ID                 */
136   uint16_t attr_len_type;              /* Length and type fields       */
137   tSDP_DISC_ATVAL attr_value;          /* Variable length entry data   */
138 } tSDP_DISC_ATTR;
139 
140 typedef struct t_sdp_disc_rec {
141   tSDP_DISC_ATTR* p_first_attr;      /* First attribute of record    */
142   struct t_sdp_disc_rec* p_next_rec; /* Addr of next linked record   */
143   uint32_t time_read;                /* The time the record was read */
144   RawAddress remote_bd_addr;         /* Remote BD address            */
145 } tSDP_DISC_REC;
146 
147 typedef struct {
148   uint32_t mem_size;          /* Memory size of the DB        */
149   uint32_t mem_free;          /* Memory still available       */
150   tSDP_DISC_REC* p_first_rec; /* Addr of first record in DB   */
151   uint16_t num_uuid_filters;  /* Number of UUIds to filter    */
152   bluetooth::Uuid uuid_filters[SDP_MAX_UUID_FILTERS]; /* UUIDs to filter */
153   uint16_t num_attr_filters; /* Number of attribute filters  */
154   uint16_t attr_filters[SDP_MAX_ATTR_FILTERS]; /* Attributes to filter */
155   uint8_t* p_free_mem; /* Pointer to free memory       */
156 #if (SDP_RAW_DATA_INCLUDED == TRUE)
157   uint8_t*
158       raw_data; /* Received record from server. allocated/released by client  */
159   uint32_t raw_size; /* size of raw_data */
160   uint32_t raw_used; /* length of raw_data used */
161 #endif
162 } tSDP_DISCOVERY_DB;
163 
164 /* This structure is used to add protocol lists and find protocol elements */
165 typedef struct {
166   uint16_t protocol_uuid;
167   uint16_t num_params;
168   uint16_t params[SDP_MAX_PROTOCOL_PARAMS];
169 } tSDP_PROTOCOL_ELEM;
170 
171 typedef struct {
172   uint16_t num_elems;
173   tSDP_PROTOCOL_ELEM list_elem[SDP_MAX_LIST_ELEMS];
174 } tSDP_PROTO_LIST_ELEM;
175 
176 /* Device Identification (DI) data structure
177 */
178 /* Used to set the DI record */
179 typedef struct t_sdp_di_record {
180   uint16_t vendor;
181   uint16_t vendor_id_source;
182   uint16_t product;
183   uint16_t version;
184   bool primary_record;
185   char client_executable_url[SDP_MAX_ATTR_LEN]; /* optional */
186   char service_description[SDP_MAX_ATTR_LEN];   /* optional */
187   char documentation_url[SDP_MAX_ATTR_LEN];     /* optional */
188 } tSDP_DI_RECORD;
189 
190 /* Used to get the DI record */
191 typedef struct t_sdp_di_get_record {
192   uint16_t spec_id;
193   tSDP_DI_RECORD rec;
194 } tSDP_DI_GET_RECORD;
195 
196 /* API into the SDP layer for service discovery. */
197 
198 /*******************************************************************************
199  *
200  * Function         SDP_InitDiscoveryDb
201  *
202  * Description      This function is called to initialize a discovery database.
203  *
204  * Returns          true if successful, false if one or more parameters are bad
205  *
206  ******************************************************************************/
207 bool SDP_InitDiscoveryDb(tSDP_DISCOVERY_DB* p_db, uint32_t len,
208                          uint16_t num_uuid, const bluetooth::Uuid* p_uuid_list,
209                          uint16_t num_attr, uint16_t* p_attr_list);
210 
211 /*******************************************************************************
212  *
213  * Function         SDP_CancelServiceSearch
214  *
215  * Description      This function cancels an active query to an SDP server.
216  *
217  * Returns          true if discovery cancelled, false if a matching activity is
218  *                  not found.
219  *
220  ******************************************************************************/
221 bool SDP_CancelServiceSearch(tSDP_DISCOVERY_DB* p_db);
222 
223 /*******************************************************************************
224  *
225  * Function         SDP_ServiceSearchRequest
226  *
227  * Description      This function queries an SDP server for information.
228  *
229  * Returns          true if discovery started, false if failed.
230  *
231  ******************************************************************************/
232 bool SDP_ServiceSearchRequest(const RawAddress& p_bd_addr,
233                               tSDP_DISCOVERY_DB* p_db, tSDP_DISC_CMPL_CB* p_cb);
234 
235 /*******************************************************************************
236  *
237  * Function         SDP_ServiceSearchAttributeRequest
238  *
239  * Description      This function queries an SDP server for information.
240  *
241  *                  The difference between this API function and the function
242  *                  SDP_ServiceSearchRequest is that this one does a
243  *                  combined ServiceSearchAttributeRequest SDP function.
244  *
245  * Returns          true if discovery started, false if failed.
246  *
247  ******************************************************************************/
248 bool SDP_ServiceSearchAttributeRequest(const RawAddress& p_bd_addr,
249                                        tSDP_DISCOVERY_DB* p_db,
250                                        tSDP_DISC_CMPL_CB* p_cb);
251 
252 /*******************************************************************************
253  *
254  * Function         SDP_ServiceSearchAttributeRequest2
255  *
256  * Description      This function queries an SDP server for information.
257  *
258  *                  The difference between this API function and the function
259  *                  SDP_ServiceSearchRequest is that this one does a
260  *                  combined ServiceSearchAttributeRequest SDP function with the
261  *                  user data piggyback
262  *
263  * Returns          true if discovery started, false if failed.
264  *
265  ******************************************************************************/
266 bool SDP_ServiceSearchAttributeRequest2(const RawAddress& p_bd_addr,
267                                         tSDP_DISCOVERY_DB* p_db,
268                                         tSDP_DISC_CMPL_CB2* p_cb,
269                                         void* user_data);
270 
271 /* API of utilities to find data in the local discovery database */
272 
273 /*******************************************************************************
274  *
275  * Function         SDP_FindAttributeInRec
276  *
277  * Description      This function searches an SDP discovery record for a
278  *                  specific attribute.
279  *
280  * Returns          Pointer to matching attribute entry, or NULL
281  *
282  ******************************************************************************/
283 tSDP_DISC_ATTR* SDP_FindAttributeInRec(tSDP_DISC_REC* p_rec, uint16_t attr_id);
284 
285 /*******************************************************************************
286  *
287  * Function         SDP_FindServiceInDb
288  *
289  * Description      This function queries an SDP database for a specific
290  *                  service. If the p_start_rec pointer is NULL, it looks from
291  *                  the beginning of the database, else it continues from the
292  *                  next record after p_start_rec.
293  *
294  * Returns          Pointer to record containing service class, or NULL
295  *
296  ******************************************************************************/
297 tSDP_DISC_REC* SDP_FindServiceInDb(tSDP_DISCOVERY_DB* p_db,
298                                    uint16_t service_uuid,
299                                    tSDP_DISC_REC* p_start_rec);
300 
301 /*******************************************************************************
302  *
303  * Function         SDP_FindServiceUUIDInDb
304  *
305  * Description      This function queries an SDP database for a specific
306  *                  service. If the p_start_rec pointer is NULL, it looks from
307  *                  the beginning of the database, else it continues from the
308  *                  next record after p_start_rec.
309  *
310  * NOTE             the only difference between this function and the previous
311  *                  function "SDP_FindServiceInDb()" is that this function takes
312  *                  a Uuid input.
313  *
314  * Returns          Pointer to record containing service class, or NULL
315  *
316  ******************************************************************************/
317 tSDP_DISC_REC* SDP_FindServiceUUIDInDb(tSDP_DISCOVERY_DB* p_db,
318                                        const bluetooth::Uuid& uuid,
319                                        tSDP_DISC_REC* p_start_rec);
320 
321 /*******************************************************************************
322  *
323  * Function         SDP_FindServiceUUIDInRec_128bit
324  *
325  * Description      Read the 128-bit service UUID within a record,
326  *                  if there is any.
327  *
328  * Parameters:      p_rec      - pointer to a SDP record.
329  *                  p_uuid     - output parameter to save the UUID found.
330  *
331  * Returns          true if found, otherwise false.
332  *
333  ******************************************************************************/
334 bool SDP_FindServiceUUIDInRec_128bit(tSDP_DISC_REC* p_rec,
335                                      bluetooth::Uuid* p_uuid);
336 
337 /*******************************************************************************
338  *
339  * Function         SDP_FindServiceInDb_128bit
340  *
341  * Description      Query an SDP database for a specific service.
342  *                  If the p_start_rec pointer is NULL, look from the beginning
343  *                  of the database, else continue from the next record after
344  *                  p_start_rec.
345  *
346  * Returns          Pointer to record containing service class, or NULL
347  *
348  ******************************************************************************/
349 tSDP_DISC_REC* SDP_FindServiceInDb_128bit(tSDP_DISCOVERY_DB* p_db,
350                                           tSDP_DISC_REC* p_start_rec);
351 
352 /*******************************************************************************
353  *
354  * Function         SDP_FindProtocolListElemInRec
355  *
356  * Description      This function looks at a specific discovery record for a
357  *                  protocol list element.
358  *
359  * Returns          true if found, false if not
360  *                  If found, the passed protocol list element is filled in.
361  *
362  ******************************************************************************/
363 bool SDP_FindProtocolListElemInRec(tSDP_DISC_REC* p_rec, uint16_t layer_uuid,
364                                    tSDP_PROTOCOL_ELEM* p_elem);
365 
366 /*******************************************************************************
367  *
368  * Function         SDP_FindProfileVersionInRec
369  *
370  * Description      This function looks at a specific discovery record for the
371  *                  Profile list descriptor, and pulls out the version number.
372  *                  The version number consists of an 8-bit major version and
373  *                  an 8-bit minor version.
374  *
375  * Returns          true if found, false if not
376  *                  If found, the major and minor version numbers that were
377  *                  passed in are filled in.
378  *
379  ******************************************************************************/
380 bool SDP_FindProfileVersionInRec(tSDP_DISC_REC* p_rec, uint16_t profile_uuid,
381                                  uint16_t* p_version);
382 
383 /* API into SDP for local service database updates */
384 
385 /*******************************************************************************
386  *
387  * Function         SDP_CreateRecord
388  *
389  * Description      This function is called to create a record in the database.
390  *                  This would be through the SDP database maintenance API. The
391  *                  record is created empty, teh application should then call
392  *                  "add_attribute" to add the record's attributes.
393  *
394  * Returns          Record handle if OK, else 0.
395  *
396  ******************************************************************************/
397 uint32_t SDP_CreateRecord(void);
398 
399 /*******************************************************************************
400  *
401  * Function         SDP_DeleteRecord
402  *
403  * Description      This function is called to add a record (or all records)
404  *                  from the database. This would be through the SDP database
405  *                  maintenance API.
406  *
407  *                  If a record handle of 0 is passed, all records are deleted.
408  *
409  * Returns          true if succeeded, else false
410  *
411  ******************************************************************************/
412 bool SDP_DeleteRecord(uint32_t handle);
413 
414 /*******************************************************************************
415  *
416  * Function         SDP_AddAttribute
417  *
418  * Description      This function is called to add an attribute to a record.
419  *                  This would be through the SDP database maintenance API.
420  *                  If the attribute already exists in the record, it is
421  *                  replaced with the new value.
422  *
423  * NOTE             Attribute values must be passed as a Big Endian stream.
424  *
425  * Returns          true if added OK, else false
426  *
427  ******************************************************************************/
428 bool SDP_AddAttribute(uint32_t handle, uint16_t attr_id, uint8_t attr_type,
429                       uint32_t attr_len, uint8_t* p_val);
430 
431 /*******************************************************************************
432  *
433  * Function         SDP_AddSequence
434  *
435  * Description      This function is called to add a sequence to a record.
436  *                  This would be through the SDP database maintenance API.
437  *                  If the sequence already exists in the record, it is replaced
438  *                  with the new sequence.
439  *
440  * NOTE             Element values must be passed as a Big Endian stream.
441  *
442  * Returns          true if added OK, else false
443  *
444  ******************************************************************************/
445 bool SDP_AddSequence(uint32_t handle, uint16_t attr_id, uint16_t num_elem,
446                      uint8_t type[], uint8_t len[], uint8_t* p_val[]);
447 
448 /*******************************************************************************
449  *
450  * Function         SDP_AddUuidSequence
451  *
452  * Description      This function is called to add a UUID sequence to a record.
453  *                  This would be through the SDP database maintenance API.
454  *                  If the sequence already exists in the record, it is replaced
455  *                  with the new sequence.
456  *
457  * Returns          true if added OK, else false
458  *
459  ******************************************************************************/
460 bool SDP_AddUuidSequence(uint32_t handle, uint16_t attr_id, uint16_t num_uuids,
461                          uint16_t* p_uuids);
462 
463 /*******************************************************************************
464  *
465  * Function         SDP_AddProtocolList
466  *
467  * Description      This function is called to add a protocol descriptor list to
468  *                  a record. This would be through the SDP database
469  *                  maintenance API. If the protocol list already exists in the
470  *                  record, it is replaced with the new list.
471  *
472  * Returns          true if added OK, else false
473  *
474  ******************************************************************************/
475 bool SDP_AddProtocolList(uint32_t handle, uint16_t num_elem,
476                          tSDP_PROTOCOL_ELEM* p_elem_list);
477 
478 /*******************************************************************************
479  *
480  * Function         SDP_AddAdditionProtoLists
481  *
482  * Description      This function is called to add a protocol descriptor list to
483  *                  a record. This would be through the SDP database maintenance
484  *                  API. If the protocol list already exists in the record, it
485  *                  is replaced with the new list.
486  *
487  * Returns          true if added OK, else false
488  *
489  ******************************************************************************/
490 bool SDP_AddAdditionProtoLists(uint32_t handle, uint16_t num_elem,
491                                tSDP_PROTO_LIST_ELEM* p_proto_list);
492 
493 /*******************************************************************************
494  *
495  * Function         SDP_AddProfileDescriptorList
496  *
497  * Description      This function is called to add a profile descriptor list to
498  *                  a record. This would be through the SDP database maintenance
499  *                  API. If the version already exists in the record, it is
500  *                  replaced with the new one.
501  *
502  * Returns          true if added OK, else false
503  *
504  ******************************************************************************/
505 bool SDP_AddProfileDescriptorList(uint32_t handle, uint16_t profile_uuid,
506                                   uint16_t version);
507 
508 /*******************************************************************************
509  *
510  * Function         SDP_AddLanguageBaseAttrIDList
511  *
512  * Description      This function is called to add a language base attr list to
513  *                  a record. This would be through the SDP database maintenance
514  *                  API. If the version already exists in the record, it is
515  *                  replaced with the new one.
516  *
517  * Returns          true if added OK, else false
518  *
519  ******************************************************************************/
520 bool SDP_AddLanguageBaseAttrIDList(uint32_t handle, uint16_t lang,
521                                    uint16_t char_enc, uint16_t base_id);
522 
523 /*******************************************************************************
524  *
525  * Function         SDP_AddServiceClassIdList
526  *
527  * Description      This function is called to add a service list to a record.
528  *                  This would be through the SDP database maintenance API.
529  *                  If the service list already exists in the record, it is
530  *                  replaced with the new list.
531  *
532  * Returns          true if added OK, else false
533  *
534  ******************************************************************************/
535 bool SDP_AddServiceClassIdList(uint32_t handle, uint16_t num_services,
536                                uint16_t* p_service_uuids);
537 
538 /*******************************************************************************
539  *
540  * Function         SDP_DeleteAttribute
541  *
542  * Description      Delete an attribute from a record.
543  *                  This would be through the SDP database maintenance API.
544  *
545  * Returns          true if deleted OK, else false if not found
546  *
547  ******************************************************************************/
548 bool SDP_DeleteAttribute(uint32_t handle, uint16_t attr_id);
549 
550 /* Device Identification APIs */
551 
552 /*******************************************************************************
553  *
554  * Function         SDP_SetLocalDiRecord
555  *
556  * Description      This function adds a DI record to the local SDP database.
557  *
558  * Returns          Returns SDP_SUCCESS if record added successfully, else error
559  *
560  ******************************************************************************/
561 uint16_t SDP_SetLocalDiRecord(tSDP_DI_RECORD* device_info, uint32_t* p_handle);
562 
563 /*******************************************************************************
564  *
565  * Function         SDP_DiDiscover
566  *
567  * Description      This function queries a remote device for DI information.
568  *
569  * Returns          SDP_SUCCESS if query started successfully, else error
570  *
571  ******************************************************************************/
572 tSDP_STATUS SDP_DiDiscover(const RawAddress& remote_device,
573                            tSDP_DISCOVERY_DB* p_db, uint32_t len,
574                            tSDP_DISC_CMPL_CB* p_cb);
575 
576 /*******************************************************************************
577  *
578  * Function         SDP_GetNumDiRecords
579  *
580  * Description      Searches specified database for DI records
581  *
582  * Returns          number of DI records found
583  *
584  ******************************************************************************/
585 uint8_t SDP_GetNumDiRecords(tSDP_DISCOVERY_DB* p_db);
586 
587 /*******************************************************************************
588  *
589  * Function         SDP_GetDiRecord
590  *
591  * Description      This function retrieves a remote device's DI record from
592  *                  the specified database.
593  *
594  * Returns          SDP_SUCCESS if record retrieved, else error
595  *
596  ******************************************************************************/
597 uint16_t SDP_GetDiRecord(uint8_t getRecordIndex,
598                          tSDP_DI_GET_RECORD* device_info,
599                          tSDP_DISCOVERY_DB* p_db);
600 
601 /*******************************************************************************
602  *
603  * Function         SDP_SetTraceLevel
604  *
605  * Description      This function sets the trace level for SDP. If called with
606  *                  a value of 0xFF, it simply reads the current trace level.
607  *
608  * Returns          the new (current) trace level
609  *
610  ******************************************************************************/
611 uint8_t SDP_SetTraceLevel(uint8_t new_level);
612 
613 /*******************************************************************************
614  *
615  * Function         SDP_FindServiceUUIDInRec
616  *
617  * Description      Read the service UUID within a record,
618  *                  if there is any.
619  *
620  * Parameters:      p_rec      - pointer to a SDP record.
621  *
622  * Returns          true if found, otherwise false.
623  *
624  ******************************************************************************/
625 bool SDP_FindServiceUUIDInRec(tSDP_DISC_REC* p_rec, bluetooth::Uuid* p_uuid);
626 
627 #endif /* SDP_API_H */
628