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 #define BTSTACK_FILE__ "sdp_bnep_query.c" 39 40 // ***************************************************************************** 41 /* EXAMPLE_START(sdp_bnep_query): SDP Records - Dump BNEP PAN protocol 42 * 43 * @text The example shows how the SDP Client is used to get all BNEP service 44 * records from a remote device. It extracts the remote BNEP PAN protocol 45 * UUID and the L2CAP PSM, which are needed to connect to a remote BNEP service. 46 */ 47 // ***************************************************************************** 48 49 #include "btstack_config.h" 50 51 #include <stdint.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 56 #include "btstack.h" 57 58 int record_id = -1; 59 int attribute_id = -1; 60 61 static uint8_t attribute_value[1000]; 62 static const int attribute_value_buffer_size = sizeof(attribute_value); 63 64 static bd_addr_t remote = {0x04,0x0C,0xCE,0xE4,0x85,0xD3}; 65 static btstack_packet_callback_registration_t hci_event_callback_registration; 66 67 static void assertBuffer(int size){ 68 if (size > attribute_value_buffer_size){ 69 printf("SDP attribute value buffer size exceeded: available %d, required %d", attribute_value_buffer_size, size); 70 } 71 } 72 73 /* @section SDP Client Setup 74 * 75 * @text As with the previous example, you must register a 76 * callback, i.e. query handler, with the SPD parser, as shown in 77 * Listing SDPClientInit. Via this handler, the SDP client will receive events: 78 * - SDP_EVENT_QUERY_ATTRIBUTE_VALUE containing the results of the query in chunks, 79 * - SDP_EVENT_QUERY_COMPLETE reporting the status and the end of the query. 80 */ 81 82 /* LISTING_START(SDPClientInit): SDP client setup */ 83 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 84 static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 85 86 static void sdp_client_init(void){ 87 // init L2CAP 88 l2cap_init(); 89 90 // register for HCI events 91 hci_event_callback_registration.callback = &packet_handler; 92 hci_add_event_handler(&hci_event_callback_registration); 93 } 94 /* LISTING_END */ 95 96 97 /* @section SDP Client Query 98 * To trigger an SDP query to get the a list of service records on a remote device, 99 * you need to call sdp_client_query_uuid16() with the remote address and the 100 * BNEP protocol UUID, as shown in Listing SDPQueryUUID. 101 * In this example we again used fixed address of the remote device. Please update 102 * for your environment. 103 */ 104 105 /* LISTING_START(SDPQueryUUID): Querying the a list of service records on a remote device. */ 106 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 107 UNUSED(channel); 108 UNUSED(size); 109 110 if (packet_type != HCI_EVENT_PACKET) return; 111 uint8_t event = hci_event_packet_get_type(packet); 112 113 switch (event) { 114 case BTSTACK_EVENT_STATE: 115 // BTstack activated, get started 116 if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 117 printf("Start SDP BNEP query.\n"); 118 sdp_client_query_uuid16(&handle_sdp_client_query_result, remote, BLUETOOTH_PROTOCOL_BNEP); 119 } 120 break; 121 default: 122 break; 123 } 124 } 125 /* LISTING_END */ 126 127 static void get_string_from_data_element(uint8_t * element, uint16_t buffer_size, char * buffer_data){ 128 de_size_t de_size = de_get_size_type(element); 129 int pos = de_get_header_size(element); 130 int len = 0; 131 switch (de_size){ 132 case DE_SIZE_VAR_8: 133 len = element[1]; 134 break; 135 case DE_SIZE_VAR_16: 136 len = big_endian_read_16(element, 1); 137 break; 138 default: 139 break; 140 } 141 uint16_t bytes_to_copy = btstack_min(buffer_size-1,len); 142 memcpy(buffer_data, &element[pos], bytes_to_copy); 143 buffer_data[bytes_to_copy] ='\0'; 144 } 145 146 147 /* @section Handling SDP Client Query Result 148 * 149 * @text The SDP Client returns the result of the query in chunks. Each result 150 * packet contains the record ID, the Attribute ID, and a chunk of the Attribute 151 * value, see Listing HandleSDPQUeryResult. Here, we show how to parse the 152 * Service Class ID List and Protocol Descriptor List, as they contain 153 * the BNEP Protocol UUID and L2CAP PSM respectively. 154 */ 155 156 /* LISTING_START(HandleSDPQUeryResult): Extracting BNEP Protcol UUID and L2CAP PSM */ 157 static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 158 UNUSED(packet_type); 159 UNUSED(channel); 160 UNUSED(size); 161 162 /* LISTING_PAUSE */ 163 des_iterator_t des_list_it; 164 des_iterator_t prot_it; 165 char str[20]; 166 167 switch (hci_event_packet_get_type(packet)){ 168 case SDP_EVENT_QUERY_ATTRIBUTE_VALUE: 169 // handle new record 170 if (sdp_event_query_attribute_byte_get_record_id(packet) != record_id){ 171 record_id = sdp_event_query_attribute_byte_get_record_id(packet); 172 printf("\n---\nRecord nr. %u\n", record_id); 173 } 174 175 assertBuffer(sdp_event_query_attribute_byte_get_attribute_length(packet)); 176 177 attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet); 178 if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)){ 179 180 /* LISTING_RESUME */ 181 /* @text The Service Class ID List is a Data Element Sequence (DES) of UUIDs. 182 * The BNEP PAN protocol UUID is within this list. 183 */ 184 185 switch(sdp_event_query_attribute_byte_get_attribute_id(packet)){ 186 // 0x0001 "Service Class ID List" 187 case BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST: 188 if (de_get_element_type(attribute_value) != DE_DES) break; 189 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)){ 190 uint8_t * element = des_iterator_get_element(&des_list_it); 191 if (de_get_element_type(element) != DE_UUID) continue; 192 uint32_t uuid = de_get_uuid32(element); 193 switch (uuid){ 194 case BLUETOOTH_SERVICE_CLASS_PANU: 195 case BLUETOOTH_SERVICE_CLASS_NAP: 196 case BLUETOOTH_SERVICE_CLASS_GN: 197 printf(" ** Attribute 0x%04x: BNEP PAN protocol UUID: %04x\n", sdp_event_query_attribute_byte_get_attribute_id(packet), (int) uuid); 198 break; 199 default: 200 break; 201 } 202 } 203 break; 204 /* LISTING_PAUSE */ 205 // 0x0100 "Service Name" 206 case 0x0100: 207 // 0x0101 "Service Description" 208 case 0x0101: 209 get_string_from_data_element(attribute_value, sizeof(str), str); 210 printf(" ** Attribute 0x%04x: %s\n", sdp_event_query_attribute_byte_get_attribute_id(packet), str); 211 break; 212 213 /* LISTING_RESUME */ 214 /* @text The Protocol Descriptor List is DES 215 * which contains one DES for each protocol. For PAN serivces, it contains 216 * a DES with the L2CAP Protocol UUID and a PSM, 217 * and another DES with the BNEP UUID and the the BNEP version. 218 */ 219 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST:{ 220 printf(" ** Attribute 0x%04x: ", sdp_event_query_attribute_byte_get_attribute_id(packet)); 221 222 uint16_t l2cap_psm = 0; 223 uint16_t bnep_version = 0; 224 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)){ 225 if (des_iterator_get_type(&des_list_it) != DE_DES) continue; 226 uint8_t * des_element = des_iterator_get_element(&des_list_it); 227 des_iterator_init(&prot_it, des_element); 228 uint8_t * element = des_iterator_get_element(&prot_it); 229 230 if (de_get_element_type(element) != DE_UUID) continue; 231 uint32_t uuid = de_get_uuid32(element); 232 des_iterator_next(&prot_it); 233 switch (uuid){ 234 case BLUETOOTH_PROTOCOL_L2CAP: 235 if (!des_iterator_has_more(&prot_it)) continue; 236 de_element_get_uint16(des_iterator_get_element(&prot_it), &l2cap_psm); 237 break; 238 case BLUETOOTH_PROTOCOL_BNEP: 239 if (!des_iterator_has_more(&prot_it)) continue; 240 de_element_get_uint16(des_iterator_get_element(&prot_it), &bnep_version); 241 break; 242 default: 243 break; 244 } 245 } 246 printf("l2cap_psm 0x%04x, bnep_version 0x%04x\n", l2cap_psm, bnep_version); 247 } 248 break; 249 /* LISTING_PAUSE */ 250 default: 251 break; 252 } 253 } 254 break; 255 case SDP_EVENT_QUERY_COMPLETE: 256 if (sdp_event_query_complete_get_status(packet)){ 257 printf("SDP query failed 0x%02x\n", sdp_event_query_complete_get_status(packet)); 258 break; 259 } 260 printf("SDP query done.\n"); 261 break; 262 } 263 /* LISTING_RESUME */ 264 } 265 /* LISTING_END */ 266 267 int btstack_main(int argc, const char * argv[]); 268 int btstack_main(int argc, const char * argv[]){ 269 (void)argc; 270 (void)argv; 271 272 printf("Client HCI init done\r\n"); 273 274 sdp_client_init(); 275 276 // turn on! 277 hci_power_control(HCI_POWER_ON); 278 279 return 0; 280 } 281 282 /* EXAMPLE_END */ 283