1*bcf00d8fSMatthias Ringwald /* 2*bcf00d8fSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3*bcf00d8fSMatthias Ringwald * 4*bcf00d8fSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5*bcf00d8fSMatthias Ringwald * modification, are permitted provided that the following conditions 6*bcf00d8fSMatthias Ringwald * are met: 7*bcf00d8fSMatthias Ringwald * 8*bcf00d8fSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9*bcf00d8fSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10*bcf00d8fSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11*bcf00d8fSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12*bcf00d8fSMatthias Ringwald * documentation and/or other materials provided with the distribution. 13*bcf00d8fSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14*bcf00d8fSMatthias Ringwald * contributors may be used to endorse or promote products derived 15*bcf00d8fSMatthias Ringwald * from this software without specific prior written permission. 16*bcf00d8fSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17*bcf00d8fSMatthias Ringwald * personal benefit and not for any commercial purpose or for 18*bcf00d8fSMatthias Ringwald * monetary gain. 19*bcf00d8fSMatthias Ringwald * 20*bcf00d8fSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21*bcf00d8fSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22*bcf00d8fSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23*bcf00d8fSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24*bcf00d8fSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25*bcf00d8fSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26*bcf00d8fSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27*bcf00d8fSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28*bcf00d8fSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29*bcf00d8fSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30*bcf00d8fSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31*bcf00d8fSMatthias Ringwald * SUCH DAMAGE. 32*bcf00d8fSMatthias Ringwald * 33*bcf00d8fSMatthias Ringwald * Please inquire about commercial licensing options at 34*bcf00d8fSMatthias Ringwald * [email protected] 35*bcf00d8fSMatthias Ringwald * 36*bcf00d8fSMatthias Ringwald */ 37*bcf00d8fSMatthias Ringwald 38*bcf00d8fSMatthias Ringwald // ***************************************************************************** 39*bcf00d8fSMatthias Ringwald /* EXAMPLE_START(sdp_bnep_query): Dump remote BNEP PAN protocol UUID and L2CAP PSM 40*bcf00d8fSMatthias Ringwald * 41*bcf00d8fSMatthias Ringwald * @text The example shows how the SDP Client is used to get all BNEP service 42*bcf00d8fSMatthias Ringwald * records from a remote device. It extracts the remote BNEP PAN protocol 43*bcf00d8fSMatthias Ringwald * UUID and the L2CAP PSM, which are needed to connect to a remote BNEP service. 44*bcf00d8fSMatthias Ringwald */ 45*bcf00d8fSMatthias Ringwald // ***************************************************************************** 46*bcf00d8fSMatthias Ringwald 47*bcf00d8fSMatthias Ringwald #include "btstack_config.h" 48*bcf00d8fSMatthias Ringwald 49*bcf00d8fSMatthias Ringwald #include <stdint.h> 50*bcf00d8fSMatthias Ringwald #include <stdio.h> 51*bcf00d8fSMatthias Ringwald #include <stdlib.h> 52*bcf00d8fSMatthias Ringwald #include <string.h> 53*bcf00d8fSMatthias Ringwald 54*bcf00d8fSMatthias Ringwald #include "btstack_event.h" 55*bcf00d8fSMatthias Ringwald #include "btstack_memory.h" 56*bcf00d8fSMatthias Ringwald #include "btstack_run_loop.h" 57*bcf00d8fSMatthias Ringwald #include "classic/sdp_client.h" 58*bcf00d8fSMatthias Ringwald #include "classic/sdp_util.h" 59*bcf00d8fSMatthias Ringwald #include "hci.h" 60*bcf00d8fSMatthias Ringwald #include "hci_cmd.h" 61*bcf00d8fSMatthias Ringwald #include "hci_dump.h" 62*bcf00d8fSMatthias Ringwald #include "l2cap.h" 63*bcf00d8fSMatthias Ringwald #include "pan.h" 64*bcf00d8fSMatthias Ringwald 65*bcf00d8fSMatthias Ringwald int record_id = -1; 66*bcf00d8fSMatthias Ringwald int attribute_id = -1; 67*bcf00d8fSMatthias Ringwald 68*bcf00d8fSMatthias Ringwald static uint8_t attribute_value[1000]; 69*bcf00d8fSMatthias Ringwald static const int attribute_value_buffer_size = sizeof(attribute_value); 70*bcf00d8fSMatthias Ringwald 71*bcf00d8fSMatthias Ringwald static bd_addr_t remote = {0x04,0x0C,0xCE,0xE4,0x85,0xD3}; 72*bcf00d8fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 73*bcf00d8fSMatthias Ringwald 74*bcf00d8fSMatthias Ringwald static void assertBuffer(int size){ 75*bcf00d8fSMatthias Ringwald if (size > attribute_value_buffer_size){ 76*bcf00d8fSMatthias Ringwald printf("SDP attribute value buffer size exceeded: available %d, required %d", attribute_value_buffer_size, size); 77*bcf00d8fSMatthias Ringwald } 78*bcf00d8fSMatthias Ringwald } 79*bcf00d8fSMatthias Ringwald 80*bcf00d8fSMatthias Ringwald /* @section SDP Client Setup 81*bcf00d8fSMatthias Ringwald * 82*bcf00d8fSMatthias Ringwald * @text As with the previous example, you must register a 83*bcf00d8fSMatthias Ringwald * callback, i.e. query handler, with the SPD parser, as shown in 84*bcf00d8fSMatthias Ringwald * Listing SDPClientInit. Via this handler, the SDP client will receive events: 85*bcf00d8fSMatthias Ringwald * - SDP_EVENT_QUERY_ATTRIBUTE_VALUE containing the results of the query in chunks, 86*bcf00d8fSMatthias Ringwald * - SDP_EVENT_QUERY_COMPLETE reporting the status and the end of the query. 87*bcf00d8fSMatthias Ringwald */ 88*bcf00d8fSMatthias Ringwald 89*bcf00d8fSMatthias Ringwald /* LISTING_START(SDPClientInit): SDP client setup */ 90*bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 91*bcf00d8fSMatthias Ringwald static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 92*bcf00d8fSMatthias Ringwald 93*bcf00d8fSMatthias Ringwald static void sdp_client_init(void){ 94*bcf00d8fSMatthias Ringwald 95*bcf00d8fSMatthias Ringwald // register for HCI events 96*bcf00d8fSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 97*bcf00d8fSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 98*bcf00d8fSMatthias Ringwald 99*bcf00d8fSMatthias Ringwald // init L2CAP 100*bcf00d8fSMatthias Ringwald l2cap_init(); 101*bcf00d8fSMatthias Ringwald } 102*bcf00d8fSMatthias Ringwald /* LISTING_END */ 103*bcf00d8fSMatthias Ringwald 104*bcf00d8fSMatthias Ringwald 105*bcf00d8fSMatthias Ringwald /* @section SDP Client Query 106*bcf00d8fSMatthias Ringwald * To trigger an SDP query to get the a list of service records on a remote device, 107*bcf00d8fSMatthias Ringwald * you need to call sdp_client_query_uuid16() with the remote address and the 108*bcf00d8fSMatthias Ringwald * BNEP protocol UUID, as shown in Listing SDPQueryUUID. 109*bcf00d8fSMatthias Ringwald * In this example we again used fixed address of the remote device. Please update 110*bcf00d8fSMatthias Ringwald * for your environment. 111*bcf00d8fSMatthias Ringwald */ 112*bcf00d8fSMatthias Ringwald 113*bcf00d8fSMatthias Ringwald /* LISTING_START(SDPQueryUUID): Querying the a list of service records on a remote device. */ 114*bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 115*bcf00d8fSMatthias Ringwald if (packet_type != HCI_EVENT_PACKET) return; 116*bcf00d8fSMatthias Ringwald uint8_t event = packet[0]; 117*bcf00d8fSMatthias Ringwald 118*bcf00d8fSMatthias Ringwald switch (event) { 119*bcf00d8fSMatthias Ringwald case BTSTACK_EVENT_STATE: 120*bcf00d8fSMatthias Ringwald // BTstack activated, get started 121*bcf00d8fSMatthias Ringwald if (packet[2] == HCI_STATE_WORKING){ 122*bcf00d8fSMatthias Ringwald printf("Start SDP BNEP query.\n"); 123*bcf00d8fSMatthias Ringwald sdp_client_query_uuid16(&handle_sdp_client_query_result, remote, SDP_BNEPProtocol); 124*bcf00d8fSMatthias Ringwald } 125*bcf00d8fSMatthias Ringwald break; 126*bcf00d8fSMatthias Ringwald default: 127*bcf00d8fSMatthias Ringwald break; 128*bcf00d8fSMatthias Ringwald } 129*bcf00d8fSMatthias Ringwald } 130*bcf00d8fSMatthias Ringwald /* LISTING_END */ 131*bcf00d8fSMatthias Ringwald 132*bcf00d8fSMatthias Ringwald static char * get_string_from_data_element(uint8_t * element){ 133*bcf00d8fSMatthias Ringwald de_size_t de_size = de_get_size_type(element); 134*bcf00d8fSMatthias Ringwald int pos = de_get_header_size(element); 135*bcf00d8fSMatthias Ringwald int len = 0; 136*bcf00d8fSMatthias Ringwald switch (de_size){ 137*bcf00d8fSMatthias Ringwald case DE_SIZE_VAR_8: 138*bcf00d8fSMatthias Ringwald len = element[1]; 139*bcf00d8fSMatthias Ringwald break; 140*bcf00d8fSMatthias Ringwald case DE_SIZE_VAR_16: 141*bcf00d8fSMatthias Ringwald len = big_endian_read_16(element, 1); 142*bcf00d8fSMatthias Ringwald break; 143*bcf00d8fSMatthias Ringwald default: 144*bcf00d8fSMatthias Ringwald break; 145*bcf00d8fSMatthias Ringwald } 146*bcf00d8fSMatthias Ringwald char * str = (char*)malloc(len+1); 147*bcf00d8fSMatthias Ringwald memcpy(str, &element[pos], len); 148*bcf00d8fSMatthias Ringwald str[len] ='\0'; 149*bcf00d8fSMatthias Ringwald return str; 150*bcf00d8fSMatthias Ringwald } 151*bcf00d8fSMatthias Ringwald 152*bcf00d8fSMatthias Ringwald 153*bcf00d8fSMatthias Ringwald /* @section Handling SDP Client Query Result 154*bcf00d8fSMatthias Ringwald * 155*bcf00d8fSMatthias Ringwald * @text The SDP Client returns the result of the query in chunks. Each result 156*bcf00d8fSMatthias Ringwald * packet contains the record ID, the Attribute ID, and a chunk of the Attribute 157*bcf00d8fSMatthias Ringwald * value, see Listing HandleSDPQUeryResult. Here, we show how to parse the 158*bcf00d8fSMatthias Ringwald * Service Class ID List and Protocol Descriptor List, as they contain 159*bcf00d8fSMatthias Ringwald * the BNEP Protocol UUID and L2CAP PSM respectively. 160*bcf00d8fSMatthias Ringwald */ 161*bcf00d8fSMatthias Ringwald 162*bcf00d8fSMatthias Ringwald /* LISTING_START(HandleSDPQUeryResult): Extracting BNEP Protcol UUID and L2CAP PSM */ 163*bcf00d8fSMatthias Ringwald static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 164*bcf00d8fSMatthias Ringwald /* LISTING_PAUSE */ 165*bcf00d8fSMatthias Ringwald des_iterator_t des_list_it; 166*bcf00d8fSMatthias Ringwald des_iterator_t prot_it; 167*bcf00d8fSMatthias Ringwald char *str; 168*bcf00d8fSMatthias Ringwald 169*bcf00d8fSMatthias Ringwald switch (packet[0]){ 170*bcf00d8fSMatthias Ringwald case SDP_EVENT_QUERY_ATTRIBUTE_VALUE: 171*bcf00d8fSMatthias Ringwald // handle new record 172*bcf00d8fSMatthias Ringwald if (sdp_event_query_attribute_byte_get_record_id(packet) != record_id){ 173*bcf00d8fSMatthias Ringwald record_id = sdp_event_query_attribute_byte_get_record_id(packet); 174*bcf00d8fSMatthias Ringwald printf("\n---\nRecord nr. %u\n", record_id); 175*bcf00d8fSMatthias Ringwald } 176*bcf00d8fSMatthias Ringwald 177*bcf00d8fSMatthias Ringwald assertBuffer(sdp_event_query_attribute_byte_get_attribute_length(packet)); 178*bcf00d8fSMatthias Ringwald 179*bcf00d8fSMatthias Ringwald attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet); 180*bcf00d8fSMatthias Ringwald if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)){ 181*bcf00d8fSMatthias Ringwald 182*bcf00d8fSMatthias Ringwald /* LISTING_RESUME */ 183*bcf00d8fSMatthias Ringwald /* @text The Service Class ID List is a Data Element Sequence (DES) of UUIDs. 184*bcf00d8fSMatthias Ringwald * The BNEP PAN protocol UUID is within this list. 185*bcf00d8fSMatthias Ringwald */ 186*bcf00d8fSMatthias Ringwald 187*bcf00d8fSMatthias Ringwald switch(sdp_event_query_attribute_byte_get_attribute_id(packet)){ 188*bcf00d8fSMatthias Ringwald // 0x0001 "Service Class ID List" 189*bcf00d8fSMatthias Ringwald case SDP_ServiceClassIDList: 190*bcf00d8fSMatthias Ringwald if (de_get_element_type(attribute_value) != DE_DES) break; 191*bcf00d8fSMatthias Ringwald for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)){ 192*bcf00d8fSMatthias Ringwald uint8_t * element = des_iterator_get_element(&des_list_it); 193*bcf00d8fSMatthias Ringwald if (de_get_element_type(element) != DE_UUID) continue; 194*bcf00d8fSMatthias Ringwald uint32_t uuid = de_get_uuid32(element); 195*bcf00d8fSMatthias Ringwald switch (uuid){ 196*bcf00d8fSMatthias Ringwald case PANU_UUID: 197*bcf00d8fSMatthias Ringwald case NAP_UUID: 198*bcf00d8fSMatthias Ringwald case GN_UUID: 199*bcf00d8fSMatthias Ringwald printf(" ** Attribute 0x%04x: BNEP PAN protocol UUID: %04x\n", sdp_event_query_attribute_byte_get_attribute_id(packet), uuid); 200*bcf00d8fSMatthias Ringwald break; 201*bcf00d8fSMatthias Ringwald default: 202*bcf00d8fSMatthias Ringwald break; 203*bcf00d8fSMatthias Ringwald } 204*bcf00d8fSMatthias Ringwald } 205*bcf00d8fSMatthias Ringwald break; 206*bcf00d8fSMatthias Ringwald /* LISTING_PAUSE */ 207*bcf00d8fSMatthias Ringwald // 0x0100 "Service Name" 208*bcf00d8fSMatthias Ringwald case 0x0100: 209*bcf00d8fSMatthias Ringwald // 0x0101 "Service Description" 210*bcf00d8fSMatthias Ringwald case 0x0101: 211*bcf00d8fSMatthias Ringwald str = get_string_from_data_element(attribute_value); 212*bcf00d8fSMatthias Ringwald printf(" ** Attribute 0x%04x: %s\n", sdp_event_query_attribute_byte_get_attribute_id(packet), str); 213*bcf00d8fSMatthias Ringwald free(str); 214*bcf00d8fSMatthias Ringwald break; 215*bcf00d8fSMatthias Ringwald 216*bcf00d8fSMatthias Ringwald /* LISTING_RESUME */ 217*bcf00d8fSMatthias Ringwald /* @text The Protocol Descriptor List is DES 218*bcf00d8fSMatthias Ringwald * which contains one DES for each protocol. For PAN serivces, it contains 219*bcf00d8fSMatthias Ringwald * a DES with the L2CAP Protocol UUID and a PSM, 220*bcf00d8fSMatthias Ringwald * and another DES with the BNEP UUID and the the BNEP version. 221*bcf00d8fSMatthias Ringwald */ 222*bcf00d8fSMatthias Ringwald case SDP_ProtocolDescriptorList:{ 223*bcf00d8fSMatthias Ringwald printf(" ** Attribute 0x%04x: ", sdp_event_query_attribute_byte_get_attribute_id(packet)); 224*bcf00d8fSMatthias Ringwald 225*bcf00d8fSMatthias Ringwald uint16_t l2cap_psm = 0; 226*bcf00d8fSMatthias Ringwald uint16_t bnep_version = 0; 227*bcf00d8fSMatthias Ringwald for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)){ 228*bcf00d8fSMatthias Ringwald if (des_iterator_get_type(&des_list_it) != DE_DES) continue; 229*bcf00d8fSMatthias Ringwald uint8_t * des_element = des_iterator_get_element(&des_list_it); 230*bcf00d8fSMatthias Ringwald des_iterator_init(&prot_it, des_element); 231*bcf00d8fSMatthias Ringwald uint8_t * element = des_iterator_get_element(&prot_it); 232*bcf00d8fSMatthias Ringwald 233*bcf00d8fSMatthias Ringwald if (de_get_element_type(element) != DE_UUID) continue; 234*bcf00d8fSMatthias Ringwald uint32_t uuid = de_get_uuid32(element); 235*bcf00d8fSMatthias Ringwald switch (uuid){ 236*bcf00d8fSMatthias Ringwald case SDP_L2CAPProtocol: 237*bcf00d8fSMatthias Ringwald if (!des_iterator_has_more(&prot_it)) continue; 238*bcf00d8fSMatthias Ringwald des_iterator_next(&prot_it); 239*bcf00d8fSMatthias Ringwald de_element_get_uint16(des_iterator_get_element(&prot_it), &l2cap_psm); 240*bcf00d8fSMatthias Ringwald break; 241*bcf00d8fSMatthias Ringwald case SDP_BNEPProtocol: 242*bcf00d8fSMatthias Ringwald if (!des_iterator_has_more(&prot_it)) continue; 243*bcf00d8fSMatthias Ringwald des_iterator_next(&prot_it); 244*bcf00d8fSMatthias Ringwald de_element_get_uint16(des_iterator_get_element(&prot_it), &bnep_version); 245*bcf00d8fSMatthias Ringwald break; 246*bcf00d8fSMatthias Ringwald default: 247*bcf00d8fSMatthias Ringwald break; 248*bcf00d8fSMatthias Ringwald } 249*bcf00d8fSMatthias Ringwald } 250*bcf00d8fSMatthias Ringwald printf("l2cap_psm 0x%04x, bnep_version 0x%04x\n", l2cap_psm, bnep_version); 251*bcf00d8fSMatthias Ringwald } 252*bcf00d8fSMatthias Ringwald break; 253*bcf00d8fSMatthias Ringwald /* LISTING_PAUSE */ 254*bcf00d8fSMatthias Ringwald default: 255*bcf00d8fSMatthias Ringwald break; 256*bcf00d8fSMatthias Ringwald } 257*bcf00d8fSMatthias Ringwald } 258*bcf00d8fSMatthias Ringwald break; 259*bcf00d8fSMatthias Ringwald case SDP_EVENT_QUERY_COMPLETE: 260*bcf00d8fSMatthias Ringwald printf("General query done with status %d.\n\n", sdp_event_query_complete_get_status(packet)); 261*bcf00d8fSMatthias Ringwald break; 262*bcf00d8fSMatthias Ringwald } 263*bcf00d8fSMatthias Ringwald /* LISTING_RESUME */ 264*bcf00d8fSMatthias Ringwald } 265*bcf00d8fSMatthias Ringwald /* LISTING_END */ 266*bcf00d8fSMatthias Ringwald 267*bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 268*bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 269*bcf00d8fSMatthias Ringwald printf("Client HCI init done\r\n"); 270*bcf00d8fSMatthias Ringwald 271*bcf00d8fSMatthias Ringwald sdp_client_init(); 272*bcf00d8fSMatthias Ringwald 273*bcf00d8fSMatthias Ringwald // turn on! 274*bcf00d8fSMatthias Ringwald hci_power_control(HCI_POWER_ON); 275*bcf00d8fSMatthias Ringwald 276*bcf00d8fSMatthias Ringwald return 0; 277*bcf00d8fSMatthias Ringwald } 278*bcf00d8fSMatthias Ringwald 279*bcf00d8fSMatthias Ringwald /* EXAMPLE_END */