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