3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2001-2002 Nokia Corporation
6 * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
7 * Copyright (C) 2002-2008 Marcel Holtmann <marcel@holtmann.org>
8 * Copyright (C) 2002-2003 Stephen Crane <steve.crane@rococosoft.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <sys/socket.h>
31 #include <bluetooth/bluetooth.h>
32 #include <bluetooth/hci.h>
41 typedef void(*sdp_list_func_t)(void *, void *);
42 typedef void(*sdp_free_func_t)(void *);
43 typedef int (*sdp_comp_func_t)(const void *, const void *);
45 sdp_list_t *sdp_list_append(sdp_list_t *list, void *d);
46 sdp_list_t *sdp_list_remove(sdp_list_t *list, void *d);
47 sdp_list_t *sdp_list_insert_sorted(sdp_list_t *list, void *data, sdp_comp_func_t f);
48 void sdp_list_free(sdp_list_t *list, sdp_free_func_t f);
50 static inline int sdp_list_len(const sdp_list_t *list)
53 for (; list; list = list->next)
58 static inline sdp_list_t *sdp_list_find(sdp_list_t *list, void *u, sdp_comp_func_t f)
60 for (; list; list = list->next)
61 if (f(list->data, u) == 0)
66 static inline void sdp_list_foreach(sdp_list_t *list, sdp_list_func_t f, void *u)
68 for (; list; list = list->next)
73 * Values of the flags parameter to sdp_record_register
75 #define SDP_RECORD_PERSIST 0x01
76 #define SDP_DEVICE_RECORD 0x02
79 * Values of the flags parameter to sdp_connect
81 #define SDP_RETRY_IF_BUSY 0x01
82 #define SDP_WAIT_ON_CLOSE 0x02
83 #define SDP_NON_BLOCKING 0x04
86 * a session with an SDP server
93 uint16_t tid; // Current transaction ID
99 * Attributes are specified as individual elements
101 SDP_ATTR_REQ_INDIVIDUAL = 1,
103 * Attributes are specified as a range
106 } sdp_attrreq_type_t;
109 * When the pdu_id(type) is a sdp error response, check the status value
110 * to figure out the error reason. For status values 0x0001-0x0006 check
111 * Bluetooth SPEC. If the status is 0xffff, call sdp_get_error function
112 * to get the real reason:
113 * - wrong transaction ID(EPROTO)
114 * - wrong PDU id or(EPROTO)
117 typedef void sdp_callback_t(uint8_t type, uint16_t status, uint8_t *rsp, size_t size, void *udata);
120 * create an L2CAP connection to a Bluetooth device
125 * Address of the local device to use to make the connection
129 * Address of the SDP server device
131 sdp_session_t *sdp_connect(const bdaddr_t *src, const bdaddr_t *dst, uint32_t flags);
132 int sdp_close(sdp_session_t *session);
133 int sdp_get_socket(const sdp_session_t *session);
136 * SDP transaction: functions for asynchronous search.
138 sdp_session_t *sdp_create(int sk, uint32_t flags);
139 int sdp_get_error(sdp_session_t *session);
140 int sdp_process(sdp_session_t *session);
141 int sdp_set_notify(sdp_session_t *session, sdp_callback_t *func, void *udata);
143 int sdp_service_search_async(sdp_session_t *session, const sdp_list_t *search, uint16_t max_rec_num);
144 int sdp_service_attr_async(sdp_session_t *session, uint32_t handle, sdp_attrreq_type_t reqtype, const sdp_list_t *attrid_list);
145 int sdp_service_search_attr_async(sdp_session_t *session, const sdp_list_t *search, sdp_attrreq_type_t reqtype, const sdp_list_t *attrid_list);
147 uint16_t sdp_gen_tid(sdp_session_t *session);
150 * find all devices in the piconet
152 int sdp_general_inquiry(inquiry_info *ii, int dev_num, int duration, uint8_t *found);
154 /* flexible extraction of basic attributes - Jean II */
155 int sdp_get_int_attr(const sdp_record_t *rec, uint16_t attr, int *value);
156 int sdp_get_string_attr(const sdp_record_t *rec, uint16_t attr, char *value, int valuelen);
159 * Basic sdp data functions
161 sdp_data_t *sdp_data_alloc(uint8_t dtd, const void *value);
162 sdp_data_t *sdp_data_alloc_with_length(uint8_t dtd, const void *value, uint32_t length);
163 void sdp_data_free(sdp_data_t *data);
164 sdp_data_t *sdp_data_get(const sdp_record_t *rec, uint16_t attr_id);
166 sdp_data_t *sdp_seq_alloc(void **dtds, void **values, int len);
167 sdp_data_t *sdp_seq_alloc_with_length(void **dtds, void **values, int *length, int len);
168 sdp_data_t *sdp_seq_append(sdp_data_t *seq, sdp_data_t *data);
170 int sdp_attr_add(sdp_record_t *rec, uint16_t attr, sdp_data_t *data);
171 void sdp_attr_remove(sdp_record_t *rec, uint16_t attr);
172 void sdp_attr_replace(sdp_record_t *rec, uint16_t attr, sdp_data_t *data);
173 int sdp_set_uuidseq_attr(sdp_record_t *rec, uint16_t attr, sdp_list_t *seq);
174 int sdp_get_uuidseq_attr(const sdp_record_t *rec, uint16_t attr, sdp_list_t **seqp);
177 * NOTE that none of the functions below will update the SDP server,
178 * unless the {register, update}sdp_record_t() function is invoked.
179 * All functions which return an integer value, return 0 on success
184 * Create an attribute and add it to the service record's attribute list.
185 * This consists of the data type descriptor of the attribute,
186 * the value of the attribute and the attribute identifier.
188 int sdp_attr_add_new(sdp_record_t *rec, uint16_t attr, uint8_t dtd, const void *p);
191 * Set the information attributes of the service record.
192 * The set of attributes comprises service name, description
195 void sdp_set_info_attr(sdp_record_t *rec, const char *name, const char *prov, const char *desc);
198 * Set the ServiceClassID attribute to the sequence specified by seq.
199 * Note that the identifiers need to be in sorted order from the most
200 * specific to the most generic service class that this service
203 static inline int sdp_set_service_classes(sdp_record_t *rec, sdp_list_t *seq)
205 return sdp_set_uuidseq_attr(rec, SDP_ATTR_SVCLASS_ID_LIST, seq);
209 * Get the service classes to which the service conforms.
211 * When set, the list contains elements of ServiceClassIdentifer(uint16_t)
212 * ordered from most specific to most generic
214 static inline int sdp_get_service_classes(const sdp_record_t *rec, sdp_list_t **seqp)
216 return sdp_get_uuidseq_attr(rec, SDP_ATTR_SVCLASS_ID_LIST, seqp);
220 * Set the BrowseGroupList attribute to the list specified by seq.
222 * A service can belong to one or more service groups
223 * and the list comprises such group identifiers (UUIDs)
225 static inline int sdp_set_browse_groups(sdp_record_t *rec, sdp_list_t *seq)
227 return sdp_set_uuidseq_attr(rec, SDP_ATTR_BROWSE_GRP_LIST, seq);
231 * Set the access protocols of the record to those specified in proto
233 int sdp_set_access_protos(sdp_record_t *rec, const sdp_list_t *proto);
236 * Set the additional access protocols of the record to those specified in proto
238 int sdp_set_add_access_protos(sdp_record_t *rec, const sdp_list_t *proto);
241 * Get protocol port (i.e. PSM for L2CAP, Channel for RFCOMM)
243 int sdp_get_proto_port(const sdp_list_t *list, int proto);
246 * Get protocol descriptor.
248 sdp_data_t *sdp_get_proto_desc(sdp_list_t *list, int proto);
251 * Set the LanguageBase attributes to the values specified in list
252 * (a linked list of sdp_lang_attr_t objects, one for each language in
253 * which user-visible attributes are present).
255 int sdp_set_lang_attr(sdp_record_t *rec, const sdp_list_t *list);
258 * Set the ServiceInfoTimeToLive attribute of the service.
259 * This is the number of seconds that this record is guaranteed
260 * not to change after being obtained by a client.
262 static inline int sdp_set_service_ttl(sdp_record_t *rec, uint32_t ttl)
264 return sdp_attr_add_new(rec, SDP_ATTR_SVCINFO_TTL, SDP_UINT32, &ttl);
268 * Set the ServiceRecordState attribute of a service. This is
269 * guaranteed to change if there is any kind of modification to
272 static inline int sdp_set_record_state(sdp_record_t *rec, uint32_t state)
274 return sdp_attr_add_new(rec, SDP_ATTR_RECORD_STATE, SDP_UINT32, &state);
278 * Set the ServiceID attribute of a service.
280 void sdp_set_service_id(sdp_record_t *rec, uuid_t uuid);
283 * Set the GroupID attribute of a service
285 void sdp_set_group_id(sdp_record_t *rec, uuid_t grouuuid);
288 * Set the ServiceAvailability attribute of a service.
290 * Note that this represents the relative availability
291 * of the service: 0x00 means completely unavailable;
292 * 0xFF means maximum availability.
294 static inline int sdp_set_service_avail(sdp_record_t *rec, uint8_t avail)
296 return sdp_attr_add_new(rec, SDP_ATTR_SERVICE_AVAILABILITY, SDP_UINT8, &avail);
300 * Set the profile descriptor list attribute of a record.
302 * Each element in the list is an object of type
303 * sdp_profile_desc_t which is a definition of the
304 * Bluetooth profile that this service conforms to.
306 int sdp_set_profile_descs(sdp_record_t *rec, const sdp_list_t *desc);
309 * Set URL attributes of a record.
311 * ClientExecutableURL: a URL to a client's platform specific (WinCE,
312 * PalmOS) executable code that can be used to access this service.
314 * DocumentationURL: a URL pointing to service documentation
316 * IconURL: a URL to an icon that can be used to represent this service.
318 * Note: pass NULL for any URLs that you don't want to set or remove
320 void sdp_set_url_attr(sdp_record_t *rec, const char *clientExecURL, const char *docURL, const char *iconURL);
323 * a service search request.
328 * list containing elements of the search
329 * pattern. Each entry in the list is a UUID
330 * of the service to be searched
332 * uint16_t max_rec_num
333 * An integer specifying the maximum number of
334 * entries that the client can handle in the response.
340 * The request completed successfully. This does not
341 * mean the requested services were found
343 * The request completed unsuccessfully
345 * sdp_list_t *rsp_list
346 * This variable is set on a successful return if there are
347 * non-zero service handles. It is a singly linked list of
348 * service record handles (uint16_t)
350 int sdp_service_search_req(sdp_session_t *session, const sdp_list_t *search, uint16_t max_rec_num, sdp_list_t **rsp_list);
353 * a service attribute request.
358 * The handle of the service for which the attribute(s) are
361 * sdp_attrreq_type_t reqtype
362 * Attribute identifiers are 16 bit unsigned integers specified
363 * in one of 2 ways described below :
364 * SDP_ATTR_REQ_INDIVIDUAL - 16bit individual identifiers
365 * They are the actual attribute identifiers in ascending order
367 * SDP_ATTR_REQ_RANGE - 32bit identifier range
368 * The high-order 16bits is the start of range
369 * the low-order 16bits are the end of range
370 * 0x0000 to 0xFFFF gets all attributes
372 * sdp_list_t *attrid_list
373 * Singly linked list containing attribute identifiers desired.
374 * Every element is either a uint16_t(attrSpec = SDP_ATTR_REQ_INDIVIDUAL)
375 * or a uint32_t(attrSpec=SDP_ATTR_REQ_RANGE)
380 * The request completed successfully. This does not
381 * mean the requested services were found
383 * The request completed unsuccessfully due to a timeout
385 sdp_record_t *sdp_service_attr_req(sdp_session_t *session, uint32_t handle, sdp_attrreq_type_t reqtype, const sdp_list_t *attrid_list);
388 * This is a service search request combined with the service
389 * attribute request. First a service class match is done and
390 * for matching service, requested attributes are extracted
395 * Singly linked list containing elements of the search
396 * pattern. Each entry in the list is a UUID(DataTypeSDP_UUID16)
397 * of the service to be searched
399 * AttributeSpecification attrSpec
400 * Attribute identifiers are 16 bit unsigned integers specified
401 * in one of 2 ways described below :
402 * SDP_ATTR_REQ_INDIVIDUAL - 16bit individual identifiers
403 * They are the actual attribute identifiers in ascending order
405 * SDP_ATTR_REQ_RANGE - 32bit identifier range
406 * The high-order 16bits is the start of range
407 * the low-order 16bits are the end of range
408 * 0x0000 to 0xFFFF gets all attributes
410 * sdp_list_t *attrid_list
411 * Singly linked list containing attribute identifiers desired.
412 * Every element is either a uint16_t(attrSpec = SDP_ATTR_REQ_INDIVIDUAL)
413 * or a uint32_t(attrSpec=SDP_ATTR_REQ_RANGE)
418 * The request completed successfully. This does not
419 * mean the requested services were found
421 * The request completed unsuccessfully due to a timeout
423 * sdp_list_t *rsp_list
424 * This variable is set on a successful return to point to
425 * service(s) found. Each element of this list is of type
428 int sdp_service_search_attr_req(sdp_session_t *session, const sdp_list_t *search, sdp_attrreq_type_t reqtype, const sdp_list_t *attrid_list, sdp_list_t **rsp_list);
431 * Allocate/free a service record and its attributes
433 sdp_record_t *sdp_record_alloc(void);
434 void sdp_record_free(sdp_record_t *rec);
437 * Register a service record.
439 * Note: It is the responsbility of the Service Provider to create the
440 * record first and set its attributes using setXXX() methods.
442 * The service provider must then call sdp_record_register() to make
443 * the service record visible to SDP clients. This function returns 0
444 * on success or -1 on failure (and sets errno).
446 int sdp_device_record_register_binary(sdp_session_t *session, bdaddr_t *device, uint8_t *data, uint32_t size, uint8_t flags, uint32_t *handle);
447 int sdp_device_record_register(sdp_session_t *session, bdaddr_t *device, sdp_record_t *rec, uint8_t flags);
448 int sdp_record_register(sdp_session_t *session, sdp_record_t *rec, uint8_t flags);
451 * Unregister a service record.
453 int sdp_device_record_unregister_binary(sdp_session_t *session, bdaddr_t *device, uint32_t handle);
454 int sdp_device_record_unregister(sdp_session_t *session, bdaddr_t *device, sdp_record_t *rec);
455 int sdp_record_unregister(sdp_session_t *session, sdp_record_t *rec);
458 * Update an existing service record. (Calling this function
459 * before a previous call to sdp_record_register() will result
462 int sdp_device_record_update_binary(sdp_session_t *session, bdaddr_t *device, uint32_t handle, uint8_t *data, uint32_t size);
463 int sdp_device_record_update(sdp_session_t *session, bdaddr_t *device, const sdp_record_t *rec);
464 int sdp_record_update(sdp_session_t *sess, const sdp_record_t *rec);
466 void sdp_record_print(const sdp_record_t *rec);
471 uuid_t *sdp_uuid16_create(uuid_t *uuid, uint16_t data);
472 uuid_t *sdp_uuid32_create(uuid_t *uuid, uint32_t data);
473 uuid_t *sdp_uuid128_create(uuid_t *uuid, const void *data);
474 int sdp_uuid16_cmp(const void *p1, const void *p2);
475 int sdp_uuid128_cmp(const void *p1, const void *p2);
476 uuid_t *sdp_uuid_to_uuid128(uuid_t *uuid);
477 void sdp_uuid16_to_uuid128(uuid_t *uuid128, uuid_t *uuid16);
478 void sdp_uuid32_to_uuid128(uuid_t *uuid128, uuid_t *uuid32);
479 int sdp_uuid128_to_uuid(uuid_t *uuid);
480 int sdp_uuid_to_proto(uuid_t *uuid);
481 int sdp_uuid_extract(const uint8_t *buffer, uuid_t *uuid, int *scanned);
482 int sdp_uuid_extract_safe(const uint8_t *buffer, int bufsize, uuid_t *uuid, int *scanned);
483 void sdp_uuid_print(const uuid_t *uuid);
485 #define MAX_LEN_UUID_STR 37
486 #define MAX_LEN_PROTOCOL_UUID_STR 8
487 #define MAX_LEN_SERVICECLASS_UUID_STR 28
488 #define MAX_LEN_PROFILEDESCRIPTOR_UUID_STR 28
490 int sdp_uuid2strn(const uuid_t *uuid, char *str, size_t n);
491 int sdp_proto_uuid2strn(const uuid_t *uuid, char *str, size_t n);
492 int sdp_svclass_uuid2strn(const uuid_t *uuid, char *str, size_t n);
493 int sdp_profile_uuid2strn(const uuid_t *uuid, char *str, size_t n);
496 * In all the sdp_get_XXX(handle, XXX *xxx) functions below,
497 * the XXX * is set to point to the value, should it exist
498 * and 0 is returned. If the value does not exist, -1 is
499 * returned and errno set to ENODATA.
501 * In all the methods below, the memory management rules are
502 * simple. Don't free anything! The pointer returned, in the
503 * case of constructed types, is a pointer to the contents
504 * of the sdp_record_t.
508 * Get the access protocols from the service record
510 int sdp_get_access_protos(const sdp_record_t *rec, sdp_list_t **protos);
513 * Get the additional access protocols from the service record
515 int sdp_get_add_access_protos(const sdp_record_t *rec, sdp_list_t **protos);
518 * Extract the list of browse groups to which the service belongs.
519 * When set, seqp contains elements of GroupID (uint16_t)
521 static inline int sdp_get_browse_groups(const sdp_record_t *rec, sdp_list_t **seqp)
523 return sdp_get_uuidseq_attr(rec, SDP_ATTR_BROWSE_GRP_LIST, seqp);
527 * Extract language attribute meta-data of the service record.
528 * For each language in the service record, LangSeq has a struct of type
531 int sdp_get_lang_attr(const sdp_record_t *rec, sdp_list_t **langSeq);
534 * Extract the Bluetooth profile descriptor sequence from a record.
535 * Each element in the list is of type sdp_profile_desc_t
536 * which contains the UUID of the profile and its version number
537 * (encoded as major and minor in the high-order 8bits
538 * and low-order 8bits respectively of the uint16_t)
540 int sdp_get_profile_descs(const sdp_record_t *rec, sdp_list_t **profDesc);
543 * Extract SDP server version numbers
545 * Note: that this is an attribute of the SDP server only and
546 * contains a list of uint16_t each of which represent the
547 * major and minor SDP version numbers supported by this server
549 int sdp_get_server_ver(const sdp_record_t *rec, sdp_list_t **pVnumList);
551 int sdp_get_service_id(const sdp_record_t *rec, uuid_t *uuid);
552 int sdp_get_group_id(const sdp_record_t *rec, uuid_t *uuid);
553 int sdp_get_record_state(const sdp_record_t *rec, uint32_t *svcRecState);
554 int sdp_get_service_avail(const sdp_record_t *rec, uint8_t *svcAvail);
555 int sdp_get_service_ttl(const sdp_record_t *rec, uint32_t *svcTTLInfo);
556 int sdp_get_database_state(const sdp_record_t *rec, uint32_t *svcDBState);
558 static inline int sdp_get_service_name(const sdp_record_t *rec, char *str, int len)
560 return sdp_get_string_attr(rec, SDP_ATTR_SVCNAME_PRIMARY, str, len);
563 static inline int sdp_get_service_desc(const sdp_record_t *rec, char *str, int len)
565 return sdp_get_string_attr(rec, SDP_ATTR_SVCDESC_PRIMARY, str, len);
568 static inline int sdp_get_provider_name(const sdp_record_t *rec, char *str, int len)
570 return sdp_get_string_attr(rec, SDP_ATTR_PROVNAME_PRIMARY, str, len);
573 static inline int sdp_get_doc_url(const sdp_record_t *rec, char *str, int len)
575 return sdp_get_string_attr(rec, SDP_ATTR_DOC_URL, str, len);
578 static inline int sdp_get_clnt_exec_url(const sdp_record_t *rec, char *str, int len)
580 return sdp_get_string_attr(rec, SDP_ATTR_CLNT_EXEC_URL, str, len);
583 static inline int sdp_get_icon_url(const sdp_record_t *rec, char *str, int len)
585 return sdp_get_string_attr(rec, SDP_ATTR_ICON_URL, str, len);
588 sdp_record_t *sdp_extract_pdu(const uint8_t *pdata, int *scanned);
589 sdp_record_t *sdp_extract_pdu_safe(const uint8_t *pdata, int bufsize, int *scanned);
591 void sdp_data_print(sdp_data_t *data);
592 void sdp_print_service_attr(sdp_list_t *alist);
594 int sdp_attrid_comp_func(const void *key1, const void *key2);
596 void sdp_set_seq_len(uint8_t *ptr, uint32_t length);
597 void sdp_set_attrid(sdp_buf_t *pdu, uint16_t id);
598 void sdp_append_to_pdu(sdp_buf_t *dst, sdp_data_t *d);
599 void sdp_append_to_buf(sdp_buf_t *dst, uint8_t *data, uint32_t len);
601 int sdp_gen_pdu(sdp_buf_t *pdu, sdp_data_t *data);
602 int sdp_gen_record_pdu(const sdp_record_t *rec, sdp_buf_t *pdu);
604 int sdp_extract_seqtype(const uint8_t *buf, uint8_t *dtdp, int *seqlen);
605 int sdp_extract_seqtype_safe(const uint8_t *buf, int bufsize, uint8_t *dtdp, int *size);
607 sdp_data_t *sdp_extract_attr(const uint8_t *pdata, int *extractedLength, sdp_record_t *rec);
608 sdp_data_t *sdp_extract_attr_safe(const uint8_t *pdata, int bufsize, int *extractedLength, sdp_record_t *rec);
610 void sdp_pattern_add_uuid(sdp_record_t *rec, uuid_t *uuid);
611 void sdp_pattern_add_uuidseq(sdp_record_t *rec, sdp_list_t *seq);
613 int sdp_send_req_w4_rsp(sdp_session_t *session, uint8_t *req, uint8_t *rsp, uint32_t reqsize, uint32_t *rspsize);
619 #endif /* __SDP_LIB_H */