xref: /btstack/src/classic/sdp_util.h (revision 7cdc89a533ca236b2c2564b759993b788bae89d3)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 /*
39  *  sdp_util.h
40  */
41 
42 #ifndef SDP_UTIL_H
43 #define SDP_UTIL_H
44 
45 #include "btstack_bool.h"
46 
47 #include <stdint.h>
48 
49 #if defined __cplusplus
50 extern "C" {
51 #endif
52 
53 
54 typedef enum {
55     SDP_Invalid = 0,
56     SDP_ErrorResponse = 1,
57     SDP_ServiceSearchRequest,
58     SDP_ServiceSearchResponse,
59     SDP_ServiceAttributeRequest,
60     SDP_ServiceAttributeResponse,
61     SDP_ServiceSearchAttributeRequest,
62     SDP_ServiceSearchAttributeResponse
63 } SDP_PDU_ID_t;
64 
65 
66 /* API_START */
67 
68 // OFFSETS FOR LOCALIZED ATTRIBUTES - BLUETOOTH_ATTRIBUTE_LANGUAGE_BASE_ATTRIBUTE_ID_LIST
69 #define SDP_Offset_ServiceName        0x0000
70 #define SDP_Offset_ServiceDescription 0x0001
71 #define SDP_Offset_ProviderName       0x0002
72 
73 typedef enum {
74     DE_NIL = 0,
75     DE_UINT,
76     DE_INT,
77     DE_UUID,
78     DE_STRING,
79     DE_BOOL,
80     DE_DES,
81     DE_DEA,
82     DE_URL
83 } de_type_t;
84 
85 typedef enum {
86     DE_SIZE_8 = 0,
87     DE_SIZE_16,
88     DE_SIZE_32,
89     DE_SIZE_64,
90     DE_SIZE_128,
91     DE_SIZE_VAR_8,
92     DE_SIZE_VAR_16,
93     DE_SIZE_VAR_32
94 } de_size_t;
95 
96 // MARK: DateElement
97 void      de_dump_data_element(const uint8_t * record);
98 int       de_get_len(const uint8_t * header);
99 
100 
101 // @note returned "string" is not NULL terminated
102 const uint8_t * de_get_string(const uint8_t * element);
103 
104 de_size_t de_get_size_type(const uint8_t * header);
105 de_type_t de_get_element_type(const uint8_t * header);
106 uint32_t  de_get_header_size(const uint8_t * header);
107 int       de_element_get_uint16(const uint8_t * element, uint16_t * value);
108 uint32_t  de_get_data_size(const uint8_t * header);
109 uint32_t  de_get_uuid32(const uint8_t * element);
110 int       de_get_normalized_uuid(uint8_t *uuid128, const uint8_t *element);
111 void      de_create_sequence(uint8_t * header);
112 void      de_store_descriptor_with_len(uint8_t * header, de_type_t type, de_size_t size, uint32_t len);
113 uint8_t * de_push_sequence(uint8_t *header);
114 void      de_pop_sequence(uint8_t * parent, uint8_t * child);
115 void      de_add_number(uint8_t *seq, de_type_t type, de_size_t size, uint32_t value);
116 void      de_add_data( uint8_t *seq, de_type_t type, uint16_t size, uint8_t *data);
117 
118 void      de_add_uuid128(uint8_t * seq, uint8_t * uuid);
119 
120 // returns data element  len if date element is smaller than size
121 uint32_t de_get_len_safe(const uint8_t * header, uint32_t size);
122 
123 // MARK: DES iterator
124 typedef struct {
125     uint8_t * element;
126     uint16_t pos;
127     uint16_t length;
128 } des_iterator_t;
129 
130 bool des_iterator_init(des_iterator_t * it, uint8_t * element);
131 bool des_iterator_has_more(des_iterator_t * it);
132 de_type_t des_iterator_get_type (des_iterator_t * it);
133 uint16_t des_iterator_get_size (des_iterator_t * it);
134 uint8_t * des_iterator_get_element(des_iterator_t * it);
135 void des_iterator_next(des_iterator_t * it);
136 
137 // MARK: SDP
138 uint16_t  sdp_append_attributes_in_attributeIDList(uint8_t *record, uint8_t *attributeIDList, uint16_t startOffset, uint16_t maxBytes, uint8_t *buffer);
139 uint8_t * sdp_get_attribute_value_for_attribute_id(uint8_t * record, uint16_t attributeID);
140 uint8_t   sdp_set_attribute_value_for_attribute_id(uint8_t * record, uint16_t attributeID, uint32_t value);
141 int       sdp_record_matches_service_search_pattern(uint8_t *record, uint8_t *serviceSearchPattern);
142 int       spd_get_filtered_size(uint8_t *record, uint8_t *attributeIDList);
143 int       sdp_filter_attributes_in_attributeIDList(uint8_t *record, uint8_t *attributeIDList, uint16_t startOffset, uint16_t maxBytes, uint16_t *usedBytes, uint8_t *buffer);
144 int       sdp_attribute_list_constains_id(uint8_t *attributeIDList, uint16_t attributeID);
145 int       sdp_traversal_match_pattern(uint8_t * element, de_type_t attributeType, de_size_t size, void *my_context);
146 
147 /*
148  * @brief Returns service search pattern for given UUID-16
149  * @note Uses fixed buffer
150  */
151 uint8_t* sdp_service_search_pattern_for_uuid16(uint16_t uuid16);
152 
153 /*
154  * @brief Returns service search pattern for given UUID-128
155  * @note Uses fixed buffer
156  */
157 uint8_t* sdp_service_search_pattern_for_uuid128(const uint8_t * uuid128);
158 
159 /* API_END */
160 
161 #if defined __cplusplus
162 }
163 #endif
164 
165 #endif // SDP_UTIL_H
166