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