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 BLUEKITCHEN 24 * GMBH 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 * @title SDP Utils 40 * 41 */ 42 43 #ifndef SDP_UTIL_H 44 #define SDP_UTIL_H 45 46 #include "btstack_bool.h" 47 48 #include <stdint.h> 49 50 #if defined __cplusplus 51 extern "C" { 52 #endif 53 54 55 typedef enum { 56 SDP_Invalid = 0, 57 SDP_ErrorResponse = 1, 58 SDP_ServiceSearchRequest, 59 SDP_ServiceSearchResponse, 60 SDP_ServiceAttributeRequest, 61 SDP_ServiceAttributeResponse, 62 SDP_ServiceSearchAttributeRequest, 63 SDP_ServiceSearchAttributeResponse 64 } sdp_pdu_id_t; 65 66 67 /* API_START */ 68 69 // OFFSETS FOR LOCALIZED ATTRIBUTES - BLUETOOTH_ATTRIBUTE_LANGUAGE_BASE_ATTRIBUTE_ID_LIST 70 #define SDP_Offset_ServiceName 0x0000 71 #define SDP_Offset_ServiceDescription 0x0001 72 #define SDP_Offset_ProviderName 0x0002 73 74 typedef enum { 75 DE_NIL = 0, 76 DE_UINT, 77 DE_INT, 78 DE_UUID, 79 DE_STRING, 80 DE_BOOL, 81 DE_DES, 82 DE_DEA, 83 DE_URL 84 } de_type_t; 85 86 typedef enum { 87 DE_SIZE_8 = 0, 88 DE_SIZE_16, 89 DE_SIZE_32, 90 DE_SIZE_64, 91 DE_SIZE_128, 92 DE_SIZE_VAR_8, 93 DE_SIZE_VAR_16, 94 DE_SIZE_VAR_32 95 } de_size_t; 96 97 // MARK: DateElement 98 void de_dump_data_element(const uint8_t * record); 99 uint32_t de_get_len(const uint8_t * header); 100 101 102 // @note returned "string" is not NULL terminated 103 const uint8_t * de_get_string(const uint8_t * element); 104 105 de_size_t de_get_size_type(const uint8_t * header); 106 de_type_t de_get_element_type(const uint8_t * header); 107 uint32_t de_get_header_size(const uint8_t * header); 108 // returns true if uint16 was read 109 bool de_element_get_uint16(const uint8_t * element, uint16_t * value); 110 uint32_t de_get_data_size(const uint8_t * header); 111 uint32_t de_get_uuid32(const uint8_t * element); 112 // returns true if uint16 was read 113 bool de_get_normalized_uuid(uint8_t *uuid128, const uint8_t *element); 114 115 void de_create_sequence(uint8_t * header); 116 void de_store_descriptor_with_len(uint8_t * header, de_type_t type, de_size_t size, uint32_t len); 117 uint8_t * de_push_sequence(uint8_t *sequence); 118 void de_pop_sequence(uint8_t * parent, uint8_t * child); 119 void de_add_number(uint8_t *sequence, de_type_t type, de_size_t size, uint32_t value); 120 void de_add_data(uint8_t *sequence, de_type_t type, uint16_t size, uint8_t *data); 121 void de_add_uuid128(uint8_t * sequence, uint8_t * uuid); 122 123 // returns data element len if date element is smaller than size 124 uint32_t de_get_len_safe(const uint8_t * header, uint32_t size); 125 126 // MARK: DES iterator 127 typedef struct { 128 uint8_t * element; 129 uint16_t pos; 130 uint16_t length; 131 } des_iterator_t; 132 133 bool des_iterator_init(des_iterator_t * it, uint8_t * element); 134 bool des_iterator_has_more(des_iterator_t * it); 135 de_type_t des_iterator_get_type (des_iterator_t * it); 136 uint16_t des_iterator_get_size (des_iterator_t * it); 137 uint8_t * des_iterator_get_element(des_iterator_t * it); 138 void des_iterator_next(des_iterator_t * it); 139 140 // MARK: SDP 141 uint16_t sdp_append_attributes_in_attributeIDList(uint8_t *record, uint8_t *attributeIDList, uint16_t startOffset, uint16_t maxBytes, uint8_t *buffer); 142 uint8_t * sdp_get_attribute_value_for_attribute_id(uint8_t * record, uint16_t attributeID); 143 bool sdp_set_attribute_value_for_attribute_id(uint8_t * record, uint16_t attributeID, uint32_t value); 144 bool sdp_record_matches_service_search_pattern(uint8_t *record, uint8_t *serviceSearchPattern); 145 uint16_t sdp_get_filtered_size(uint8_t *record, uint8_t *attributeIDList); 146 bool sdp_filter_attributes_in_attributeIDList(uint8_t *record, uint8_t *attributeIDList, uint16_t startOffset, uint16_t maxBytes, uint16_t *usedBytes, uint8_t *buffer); 147 bool sdp_attribute_list_contains_id(uint8_t *attributeIDList, uint16_t attributeID); 148 bool sdp_attribute_list_valid(uint8_t *attributeIDList); 149 bool sdp_valid_service_search_pattern(uint8_t *service_search_pattern); 150 /* 151 * @brief Returns service search pattern for given UUID-16 152 * @note Uses fixed buffer 153 */ 154 uint8_t* sdp_service_search_pattern_for_uuid16(uint16_t uuid16); 155 156 /* 157 * @brief Returns service search pattern for given UUID-128 158 * @note Uses fixed buffer 159 */ 160 uint8_t* sdp_service_search_pattern_for_uuid128(const uint8_t * uuid128); 161 162 /* API_END */ 163 164 #if defined __cplusplus 165 } 166 #endif 167 168 #endif // SDP_UTIL_H 169