1bcf00d8fSMatthias Ringwald /* 2bcf00d8fSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3bcf00d8fSMatthias Ringwald * 4bcf00d8fSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5bcf00d8fSMatthias Ringwald * modification, are permitted provided that the following conditions 6bcf00d8fSMatthias Ringwald * are met: 7bcf00d8fSMatthias Ringwald * 8bcf00d8fSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9bcf00d8fSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10bcf00d8fSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11bcf00d8fSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12bcf00d8fSMatthias Ringwald * documentation and/or other materials provided with the distribution. 13bcf00d8fSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14bcf00d8fSMatthias Ringwald * contributors may be used to endorse or promote products derived 15bcf00d8fSMatthias Ringwald * from this software without specific prior written permission. 16bcf00d8fSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17bcf00d8fSMatthias Ringwald * personal benefit and not for any commercial purpose or for 18bcf00d8fSMatthias Ringwald * monetary gain. 19bcf00d8fSMatthias Ringwald * 20bcf00d8fSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21bcf00d8fSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22bcf00d8fSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23bcf00d8fSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24bcf00d8fSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25bcf00d8fSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26bcf00d8fSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27bcf00d8fSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28bcf00d8fSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29bcf00d8fSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30bcf00d8fSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31bcf00d8fSMatthias Ringwald * SUCH DAMAGE. 32bcf00d8fSMatthias Ringwald * 33bcf00d8fSMatthias Ringwald * Please inquire about commercial licensing options at 34bcf00d8fSMatthias Ringwald * [email protected] 35bcf00d8fSMatthias Ringwald * 36bcf00d8fSMatthias Ringwald */ 37bcf00d8fSMatthias Ringwald 38bcf00d8fSMatthias Ringwald /* 39bcf00d8fSMatthias Ringwald * panu_demo.c 40bcf00d8fSMatthias Ringwald * Author: Ole Reinhardt <[email protected]> 41bcf00d8fSMatthias Ringwald */ 42bcf00d8fSMatthias Ringwald 43bcf00d8fSMatthias Ringwald /* EXAMPLE_START(panu_demo): PANU Demo 44bcf00d8fSMatthias Ringwald * 45bcf00d8fSMatthias Ringwald * @text This example implements both a PANU client and a server. In server mode, it 46bcf00d8fSMatthias Ringwald * sets up a BNEP server and registers a PANU SDP record and waits for incoming connections. 47bcf00d8fSMatthias Ringwald * In client mode, it connects to a remote device, does an SDP Query to identify the PANU 48bcf00d8fSMatthias Ringwald * service and initiates a BNEP connection. 49bcf00d8fSMatthias Ringwald */ 50bcf00d8fSMatthias Ringwald 51bcf00d8fSMatthias Ringwald #include "btstack_config.h" 52bcf00d8fSMatthias Ringwald 53bcf00d8fSMatthias Ringwald #include <arpa/inet.h> 54bcf00d8fSMatthias Ringwald #include <errno.h> 55bcf00d8fSMatthias Ringwald #include <fcntl.h> 56bcf00d8fSMatthias Ringwald #include <ifaddrs.h> 57bcf00d8fSMatthias Ringwald #include <stdint.h> 58bcf00d8fSMatthias Ringwald #include <stdio.h> 59bcf00d8fSMatthias Ringwald #include <stdlib.h> 60bcf00d8fSMatthias Ringwald #include <string.h> 61bcf00d8fSMatthias Ringwald #include <unistd.h> 62bcf00d8fSMatthias Ringwald 63bcf00d8fSMatthias Ringwald #include <net/if_arp.h> 64bcf00d8fSMatthias Ringwald 65bcf00d8fSMatthias Ringwald #ifdef __APPLE__ 66bcf00d8fSMatthias Ringwald #include <net/if.h> 67bcf00d8fSMatthias Ringwald #include <net/if_types.h> 68bcf00d8fSMatthias Ringwald 69bcf00d8fSMatthias Ringwald #include <netinet/if_ether.h> 70bcf00d8fSMatthias Ringwald #include <netinet/in.h> 71bcf00d8fSMatthias Ringwald #endif 72bcf00d8fSMatthias Ringwald 73bcf00d8fSMatthias Ringwald #include <sys/ioctl.h> 74bcf00d8fSMatthias Ringwald #include <sys/param.h> 75bcf00d8fSMatthias Ringwald #include <sys/socket.h> 76bcf00d8fSMatthias Ringwald #include <sys/stat.h> 77bcf00d8fSMatthias Ringwald #include <sys/types.h> 78bcf00d8fSMatthias Ringwald 79bcf00d8fSMatthias Ringwald #ifdef __linux 80bcf00d8fSMatthias Ringwald #include <linux/if.h> 81bcf00d8fSMatthias Ringwald #include <linux/if_tun.h> 82bcf00d8fSMatthias Ringwald #endif 83bcf00d8fSMatthias Ringwald 84bcf00d8fSMatthias Ringwald #include "btstack_memory.h" 85bcf00d8fSMatthias Ringwald #include "btstack_event.h" 86bcf00d8fSMatthias Ringwald #include "btstack_run_loop.h" 87bcf00d8fSMatthias Ringwald #include "classic/sdp_client.h" 88bcf00d8fSMatthias Ringwald #include "classic/sdp_util.h" 89bcf00d8fSMatthias Ringwald #include "hci.h" 90bcf00d8fSMatthias Ringwald #include "hci_cmd.h" 91bcf00d8fSMatthias Ringwald #include "hci_dump.h" 92bcf00d8fSMatthias Ringwald #include "l2cap.h" 93bcf00d8fSMatthias Ringwald #include "pan.h" 94bcf00d8fSMatthias Ringwald 95bcf00d8fSMatthias Ringwald static int record_id = -1; 96bcf00d8fSMatthias Ringwald static uint16_t bnep_l2cap_psm = 0; 97bcf00d8fSMatthias Ringwald static uint32_t bnep_remote_uuid = 0; 98bcf00d8fSMatthias Ringwald static uint16_t bnep_version = 0; 99bcf00d8fSMatthias Ringwald static uint16_t bnep_cid = 0; 100bcf00d8fSMatthias Ringwald 101bcf00d8fSMatthias Ringwald static uint8_t attribute_value[1000]; 102bcf00d8fSMatthias Ringwald static const unsigned int attribute_value_buffer_size = sizeof(attribute_value); 103bcf00d8fSMatthias Ringwald 104bcf00d8fSMatthias Ringwald //static bd_addr_t remote = {0x04,0x0C,0xCE,0xE4,0x85,0xD3}; 105bcf00d8fSMatthias Ringwald // static bd_addr_t remote = {0xE0,0x06,0xE6,0xBB,0x95,0x79}; // Ole Thinkpad 106bcf00d8fSMatthias Ringwald static bd_addr_t remote = {0x84,0x38,0x35,0x65,0xD1,0x15}; // MacBook 2013 107bcf00d8fSMatthias Ringwald 108bcf00d8fSMatthias Ringwald static int tap_fd = -1; 109bcf00d8fSMatthias Ringwald static uint8_t network_buffer[BNEP_MTU_MIN]; 110bcf00d8fSMatthias Ringwald static size_t network_buffer_len = 0; 111bcf00d8fSMatthias Ringwald 112bcf00d8fSMatthias Ringwald #ifdef __APPLE__ 113bcf00d8fSMatthias Ringwald // tuntaposx provides fixed set of tapX devices 114bcf00d8fSMatthias Ringwald static const char * tap_dev = "/dev/tap0"; 115bcf00d8fSMatthias Ringwald static char tap_dev_name[16] = "tap0"; 116bcf00d8fSMatthias Ringwald #endif 117bcf00d8fSMatthias Ringwald 118bcf00d8fSMatthias Ringwald #ifdef __linux 119bcf00d8fSMatthias Ringwald // Linux uses single control device to bring up tunX or tapX interface 120bcf00d8fSMatthias Ringwald static const char * tap_dev = "/dev/net/tun"; 121bcf00d8fSMatthias Ringwald static char tap_dev_name[16] = "bnep%d"; 122bcf00d8fSMatthias Ringwald #endif 123bcf00d8fSMatthias Ringwald 124bcf00d8fSMatthias Ringwald 125bcf00d8fSMatthias Ringwald static btstack_data_source_t tap_dev_ds; 126bcf00d8fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 127bcf00d8fSMatthias Ringwald 128bcf00d8fSMatthias Ringwald /* @section Main application configuration 129bcf00d8fSMatthias Ringwald * 130bcf00d8fSMatthias Ringwald * @text In the application configuration, L2CAP and BNEP are initialized and a BNEP service, for server mode, 131bcf00d8fSMatthias Ringwald * is registered, before the Bluetooth stack gets started, as shown in Listing PanuSetup. 132bcf00d8fSMatthias Ringwald */ 133bcf00d8fSMatthias Ringwald 134bcf00d8fSMatthias Ringwald /* LISTING_START(PanuSetup): Panu setup */ 135bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 136423c667cSMatthias Ringwald static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 137bcf00d8fSMatthias Ringwald 138bcf00d8fSMatthias Ringwald static void panu_setup(void){ 139bcf00d8fSMatthias Ringwald 140bcf00d8fSMatthias Ringwald // register for HCI events 141bcf00d8fSMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 142bcf00d8fSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 143bcf00d8fSMatthias Ringwald 144bcf00d8fSMatthias Ringwald // Initialize L2CAP 145bcf00d8fSMatthias Ringwald l2cap_init(); 146bcf00d8fSMatthias Ringwald 147bcf00d8fSMatthias Ringwald // Initialise BNEP 148bcf00d8fSMatthias Ringwald bnep_init(); 149bcf00d8fSMatthias Ringwald // Minimum L2CAP MTU for bnep is 1691 bytes 1500c249750SMatthias Ringwald bnep_register_service(packet_handler, SDP_PANU, 1691); 151bcf00d8fSMatthias Ringwald } 152bcf00d8fSMatthias Ringwald /* LISTING_END */ 153bcf00d8fSMatthias Ringwald 154bcf00d8fSMatthias Ringwald /* @section TUN / TAP interface routines 155bcf00d8fSMatthias Ringwald * 156bcf00d8fSMatthias Ringwald * @text This example requires a TUN/TAP interface to connect the Bluetooth network interface 157bcf00d8fSMatthias Ringwald * with the native system. It has been tested on Linux and OS X, but should work on any 158bcf00d8fSMatthias Ringwald * system that provides TUN/TAP with minor modifications. 159bcf00d8fSMatthias Ringwald * 160bcf00d8fSMatthias Ringwald * On Linux, TUN/TAP is available by default. On OS X, tuntaposx from 161bcf00d8fSMatthias Ringwald * http://tuntaposx.sourceforge.net needs to be installed. 162bcf00d8fSMatthias Ringwald * 163bcf00d8fSMatthias Ringwald * The *tap_alloc* function sets up a virtual network interface with the given Bluetooth Address. 164bcf00d8fSMatthias Ringwald * It is rather low-level as it sets up and configures a network interface. 165bcf00d8fSMatthias Ringwald */ 166bcf00d8fSMatthias Ringwald 167bcf00d8fSMatthias Ringwald static int tap_alloc(char *dev, bd_addr_t bd_addr) 168bcf00d8fSMatthias Ringwald { 169bcf00d8fSMatthias Ringwald struct ifreq ifr; 170bcf00d8fSMatthias Ringwald int fd_dev; 171bcf00d8fSMatthias Ringwald int fd_socket; 172bcf00d8fSMatthias Ringwald 173bcf00d8fSMatthias Ringwald if( (fd_dev = open(tap_dev, O_RDWR)) < 0 ) { 174bcf00d8fSMatthias Ringwald fprintf(stderr, "TAP: Error opening %s: %s\n", tap_dev, strerror(errno)); 175bcf00d8fSMatthias Ringwald return -1; 176bcf00d8fSMatthias Ringwald } 177bcf00d8fSMatthias Ringwald 178bcf00d8fSMatthias Ringwald #ifdef __linux 179bcf00d8fSMatthias Ringwald memset(&ifr, 0, sizeof(ifr)); 180bcf00d8fSMatthias Ringwald 181bcf00d8fSMatthias Ringwald ifr.ifr_flags = IFF_TAP | IFF_NO_PI; 182bcf00d8fSMatthias Ringwald if( *dev ) { 183bcf00d8fSMatthias Ringwald strncpy(ifr.ifr_name, dev, IFNAMSIZ); 184bcf00d8fSMatthias Ringwald } 185bcf00d8fSMatthias Ringwald 186bcf00d8fSMatthias Ringwald int err; 187bcf00d8fSMatthias Ringwald if( (err = ioctl(fd_dev, TUNSETIFF, (void *) &ifr)) < 0 ) { 188bcf00d8fSMatthias Ringwald fprintf(stderr, "TAP: Error setting device name: %s\n", strerror(errno)); 189bcf00d8fSMatthias Ringwald close(fd_dev); 190bcf00d8fSMatthias Ringwald return -1; 191bcf00d8fSMatthias Ringwald } 192bcf00d8fSMatthias Ringwald strcpy(dev, ifr.ifr_name); 193bcf00d8fSMatthias Ringwald #endif 194bcf00d8fSMatthias Ringwald #ifdef __APPLE__ 195bcf00d8fSMatthias Ringwald dev = tap_dev_name; 196bcf00d8fSMatthias Ringwald #endif 197bcf00d8fSMatthias Ringwald 198bcf00d8fSMatthias Ringwald fd_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); 199bcf00d8fSMatthias Ringwald if (fd_socket < 0) { 200bcf00d8fSMatthias Ringwald close(fd_dev); 201bcf00d8fSMatthias Ringwald fprintf(stderr, "TAP: Error opening netlink socket: %s\n", strerror(errno)); 202bcf00d8fSMatthias Ringwald return -1; 203bcf00d8fSMatthias Ringwald } 204bcf00d8fSMatthias Ringwald 205bcf00d8fSMatthias Ringwald // Configure the MAC address of the newly created bnep(x) 206bcf00d8fSMatthias Ringwald // device to the local bd_address 207bcf00d8fSMatthias Ringwald memset (&ifr, 0, sizeof(struct ifreq)); 208bcf00d8fSMatthias Ringwald strcpy(ifr.ifr_name, dev); 209bcf00d8fSMatthias Ringwald #ifdef __linux 210bcf00d8fSMatthias Ringwald ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER; 211bcf00d8fSMatthias Ringwald memcpy(ifr.ifr_hwaddr.sa_data, bd_addr, sizeof(bd_addr_t)); 212bcf00d8fSMatthias Ringwald if (ioctl(fd_socket, SIOCSIFHWADDR, &ifr) == -1) { 213bcf00d8fSMatthias Ringwald close(fd_dev); 214bcf00d8fSMatthias Ringwald close(fd_socket); 215bcf00d8fSMatthias Ringwald fprintf(stderr, "TAP: Error setting hw addr: %s\n", strerror(errno)); 216bcf00d8fSMatthias Ringwald exit(1); 217bcf00d8fSMatthias Ringwald return -1; 218bcf00d8fSMatthias Ringwald } 219bcf00d8fSMatthias Ringwald #endif 220bcf00d8fSMatthias Ringwald #ifdef __APPLE__ 221bcf00d8fSMatthias Ringwald ifr.ifr_addr.sa_len = ETHER_ADDR_LEN; 222bcf00d8fSMatthias Ringwald ifr.ifr_addr.sa_family = AF_LINK; 223bcf00d8fSMatthias Ringwald (void)memcpy(ifr.ifr_addr.sa_data, bd_addr, ETHER_ADDR_LEN); 224bcf00d8fSMatthias Ringwald if (ioctl(fd_socket, SIOCSIFLLADDR, &ifr) == -1) { 225bcf00d8fSMatthias Ringwald close(fd_dev); 226bcf00d8fSMatthias Ringwald close(fd_socket); 227bcf00d8fSMatthias Ringwald fprintf(stderr, "TAP: Error setting hw addr: %s\n", strerror(errno)); 228bcf00d8fSMatthias Ringwald exit(1); 229bcf00d8fSMatthias Ringwald return -1; 230bcf00d8fSMatthias Ringwald } 231bcf00d8fSMatthias Ringwald #endif 232bcf00d8fSMatthias Ringwald 233bcf00d8fSMatthias Ringwald // Bring the interface up 234bcf00d8fSMatthias Ringwald if (ioctl(fd_socket, SIOCGIFFLAGS, &ifr) == -1) { 235bcf00d8fSMatthias Ringwald close(fd_dev); 236bcf00d8fSMatthias Ringwald close(fd_socket); 237bcf00d8fSMatthias Ringwald fprintf(stderr, "TAP: Error reading interface flags: %s\n", strerror(errno)); 238bcf00d8fSMatthias Ringwald return -1; 239bcf00d8fSMatthias Ringwald } 240bcf00d8fSMatthias Ringwald 241bcf00d8fSMatthias Ringwald if ((ifr.ifr_flags & IFF_UP) == 0) { 242bcf00d8fSMatthias Ringwald ifr.ifr_flags |= IFF_UP; 243bcf00d8fSMatthias Ringwald 244bcf00d8fSMatthias Ringwald if (ioctl(fd_socket, SIOCSIFFLAGS, &ifr) == -1) { 245bcf00d8fSMatthias Ringwald close(fd_dev); 246bcf00d8fSMatthias Ringwald close(fd_socket); 247bcf00d8fSMatthias Ringwald fprintf(stderr, "TAP: Error set IFF_UP: %s\n", strerror(errno)); 248bcf00d8fSMatthias Ringwald return -1; 249bcf00d8fSMatthias Ringwald } 250bcf00d8fSMatthias Ringwald } 251bcf00d8fSMatthias Ringwald 252bcf00d8fSMatthias Ringwald close(fd_socket); 253bcf00d8fSMatthias Ringwald 254bcf00d8fSMatthias Ringwald return fd_dev; 255bcf00d8fSMatthias Ringwald } 256bcf00d8fSMatthias Ringwald 257bcf00d8fSMatthias Ringwald /* 258bcf00d8fSMatthias Ringwald * @text Listing processTapData shows how a packet is received from the TAP network interface 259bcf00d8fSMatthias Ringwald * and forwarded over the BNEP connection. 260bcf00d8fSMatthias Ringwald * 261bcf00d8fSMatthias Ringwald * After successfully reading a network packet, the call to 262bcf00d8fSMatthias Ringwald * the *bnep_can_send_packet_now* function checks, if BTstack can forward 263bcf00d8fSMatthias Ringwald * a network packet now. If that's not possible, the received data stays 264bcf00d8fSMatthias Ringwald * in the network buffer and the data source elements is removed from the 265bcf00d8fSMatthias Ringwald * run loop. The *process_tap_dev_data* function will not be called until 266bcf00d8fSMatthias Ringwald * the data source is registered again. This provides a basic flow control. 267bcf00d8fSMatthias Ringwald */ 268bcf00d8fSMatthias Ringwald 269bcf00d8fSMatthias Ringwald /* LISTING_START(processTapData): Process incoming network packets */ 270423c667cSMatthias Ringwald static void process_tap_dev_data(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type) 271bcf00d8fSMatthias Ringwald { 272bcf00d8fSMatthias Ringwald ssize_t len; 273bcf00d8fSMatthias Ringwald len = read(ds->fd, network_buffer, sizeof(network_buffer)); 274bcf00d8fSMatthias Ringwald if (len <= 0){ 275bcf00d8fSMatthias Ringwald fprintf(stderr, "TAP: Error while reading: %s\n", strerror(errno)); 276423c667cSMatthias Ringwald return; 277bcf00d8fSMatthias Ringwald } 278bcf00d8fSMatthias Ringwald 279bcf00d8fSMatthias Ringwald network_buffer_len = len; 280bcf00d8fSMatthias Ringwald if (bnep_can_send_packet_now(bnep_cid)) { 281bcf00d8fSMatthias Ringwald bnep_send(bnep_cid, network_buffer, network_buffer_len); 282bcf00d8fSMatthias Ringwald network_buffer_len = 0; 283bcf00d8fSMatthias Ringwald } else { 284bcf00d8fSMatthias Ringwald // park the current network packet 285bcf00d8fSMatthias Ringwald btstack_run_loop_remove_data_source(&tap_dev_ds); 286bcf00d8fSMatthias Ringwald } 287423c667cSMatthias Ringwald return; 288bcf00d8fSMatthias Ringwald } 289bcf00d8fSMatthias Ringwald /* LISTING_END */ 290bcf00d8fSMatthias Ringwald 291bcf00d8fSMatthias Ringwald // PANU client routines 292bcf00d8fSMatthias Ringwald static char * get_string_from_data_element(uint8_t * element){ 293bcf00d8fSMatthias Ringwald de_size_t de_size = de_get_size_type(element); 294bcf00d8fSMatthias Ringwald int pos = de_get_header_size(element); 295bcf00d8fSMatthias Ringwald int len = 0; 296bcf00d8fSMatthias Ringwald switch (de_size){ 297bcf00d8fSMatthias Ringwald case DE_SIZE_VAR_8: 298bcf00d8fSMatthias Ringwald len = element[1]; 299bcf00d8fSMatthias Ringwald break; 300bcf00d8fSMatthias Ringwald case DE_SIZE_VAR_16: 301bcf00d8fSMatthias Ringwald len = big_endian_read_16(element, 1); 302bcf00d8fSMatthias Ringwald break; 303bcf00d8fSMatthias Ringwald default: 304bcf00d8fSMatthias Ringwald break; 305bcf00d8fSMatthias Ringwald } 306bcf00d8fSMatthias Ringwald char * str = (char*)malloc(len+1); 307bcf00d8fSMatthias Ringwald memcpy(str, &element[pos], len); 308bcf00d8fSMatthias Ringwald str[len] ='\0'; 309bcf00d8fSMatthias Ringwald return str; 310bcf00d8fSMatthias Ringwald } 311bcf00d8fSMatthias Ringwald 312bcf00d8fSMatthias Ringwald 313bcf00d8fSMatthias Ringwald /* @section SDP parser callback 314bcf00d8fSMatthias Ringwald * 315bcf00d8fSMatthias Ringwald * @text The SDP parsers retrieves the BNEP PAN UUID as explained in 316bcf00d8fSMatthias Ringwald * Section [on SDP BNEP Query example](#sec:sdpbnepqueryExample}. 317bcf00d8fSMatthias Ringwald */ 318423c667cSMatthias Ringwald static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { 319bcf00d8fSMatthias Ringwald 320bcf00d8fSMatthias Ringwald des_iterator_t des_list_it; 321bcf00d8fSMatthias Ringwald des_iterator_t prot_it; 322bcf00d8fSMatthias Ringwald char *str; 323bcf00d8fSMatthias Ringwald 3240e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)){ 325bcf00d8fSMatthias Ringwald case SDP_EVENT_QUERY_ATTRIBUTE_VALUE: 326bcf00d8fSMatthias Ringwald // Handle new SDP record 327bcf00d8fSMatthias Ringwald if (sdp_event_query_attribute_byte_get_record_id(packet) != record_id) { 328bcf00d8fSMatthias Ringwald record_id = sdp_event_query_attribute_byte_get_record_id(packet); 329bcf00d8fSMatthias Ringwald printf("SDP Record: Nr: %d\n", record_id); 330bcf00d8fSMatthias Ringwald } 331bcf00d8fSMatthias Ringwald 332bcf00d8fSMatthias Ringwald if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) { 333bcf00d8fSMatthias Ringwald attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet); 334bcf00d8fSMatthias Ringwald 335bcf00d8fSMatthias Ringwald if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) { 336bcf00d8fSMatthias Ringwald 337bcf00d8fSMatthias Ringwald switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) { 338bcf00d8fSMatthias Ringwald case SDP_ServiceClassIDList: 339bcf00d8fSMatthias Ringwald if (de_get_element_type(attribute_value) != DE_DES) break; 340bcf00d8fSMatthias Ringwald for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { 341bcf00d8fSMatthias Ringwald uint8_t * element = des_iterator_get_element(&des_list_it); 342bcf00d8fSMatthias Ringwald if (de_get_element_type(element) != DE_UUID) continue; 343bcf00d8fSMatthias Ringwald uint32_t uuid = de_get_uuid32(element); 344bcf00d8fSMatthias Ringwald switch (uuid){ 345bcf00d8fSMatthias Ringwald case SDP_PANU: 346bcf00d8fSMatthias Ringwald case SDP_NAP: 347bcf00d8fSMatthias Ringwald case SDP_GN: 348bcf00d8fSMatthias Ringwald printf("SDP Attribute 0x%04x: BNEP PAN protocol UUID: %04x\n", sdp_event_query_attribute_byte_get_attribute_id(packet), uuid); 349bcf00d8fSMatthias Ringwald bnep_remote_uuid = uuid; 350bcf00d8fSMatthias Ringwald break; 351bcf00d8fSMatthias Ringwald default: 352bcf00d8fSMatthias Ringwald break; 353bcf00d8fSMatthias Ringwald } 354bcf00d8fSMatthias Ringwald } 355bcf00d8fSMatthias Ringwald break; 356bcf00d8fSMatthias Ringwald case 0x0100: 357bcf00d8fSMatthias Ringwald case 0x0101: 358bcf00d8fSMatthias Ringwald str = get_string_from_data_element(attribute_value); 359bcf00d8fSMatthias Ringwald printf("SDP Attribute: 0x%04x: %s\n", sdp_event_query_attribute_byte_get_attribute_id(packet), str); 360bcf00d8fSMatthias Ringwald free(str); 361bcf00d8fSMatthias Ringwald break; 362bcf00d8fSMatthias Ringwald case 0x0004: { 363bcf00d8fSMatthias Ringwald printf("SDP Attribute: 0x%04x\n", sdp_event_query_attribute_byte_get_attribute_id(packet)); 364bcf00d8fSMatthias Ringwald 365bcf00d8fSMatthias Ringwald for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { 366bcf00d8fSMatthias Ringwald uint8_t *des_element; 367bcf00d8fSMatthias Ringwald uint8_t *element; 368bcf00d8fSMatthias Ringwald uint32_t uuid; 369bcf00d8fSMatthias Ringwald 370bcf00d8fSMatthias Ringwald if (des_iterator_get_type(&des_list_it) != DE_DES) continue; 371bcf00d8fSMatthias Ringwald 372bcf00d8fSMatthias Ringwald des_element = des_iterator_get_element(&des_list_it); 373bcf00d8fSMatthias Ringwald des_iterator_init(&prot_it, des_element); 374bcf00d8fSMatthias Ringwald element = des_iterator_get_element(&prot_it); 375bcf00d8fSMatthias Ringwald 376bcf00d8fSMatthias Ringwald if (de_get_element_type(element) != DE_UUID) continue; 377bcf00d8fSMatthias Ringwald 378bcf00d8fSMatthias Ringwald uuid = de_get_uuid32(element); 379bcf00d8fSMatthias Ringwald switch (uuid){ 380bcf00d8fSMatthias Ringwald case SDP_L2CAPProtocol: 381bcf00d8fSMatthias Ringwald if (!des_iterator_has_more(&prot_it)) continue; 382bcf00d8fSMatthias Ringwald des_iterator_next(&prot_it); 383bcf00d8fSMatthias Ringwald de_element_get_uint16(des_iterator_get_element(&prot_it), &bnep_l2cap_psm); 384bcf00d8fSMatthias Ringwald break; 385bcf00d8fSMatthias Ringwald case SDP_BNEPProtocol: 386bcf00d8fSMatthias Ringwald if (!des_iterator_has_more(&prot_it)) continue; 387bcf00d8fSMatthias Ringwald des_iterator_next(&prot_it); 388bcf00d8fSMatthias Ringwald de_element_get_uint16(des_iterator_get_element(&prot_it), &bnep_version); 389bcf00d8fSMatthias Ringwald break; 390bcf00d8fSMatthias Ringwald default: 391bcf00d8fSMatthias Ringwald break; 392bcf00d8fSMatthias Ringwald } 393bcf00d8fSMatthias Ringwald } 394bcf00d8fSMatthias Ringwald printf("l2cap_psm 0x%04x, bnep_version 0x%04x\n", bnep_l2cap_psm, bnep_version); 395bcf00d8fSMatthias Ringwald 396bcf00d8fSMatthias Ringwald /* Create BNEP connection */ 3970c249750SMatthias Ringwald bnep_connect(packet_handler, remote, bnep_l2cap_psm, PANU_UUID, bnep_remote_uuid); 398bcf00d8fSMatthias Ringwald } 399bcf00d8fSMatthias Ringwald break; 400bcf00d8fSMatthias Ringwald default: 401bcf00d8fSMatthias Ringwald break; 402bcf00d8fSMatthias Ringwald } 403bcf00d8fSMatthias Ringwald } 404bcf00d8fSMatthias Ringwald } else { 405bcf00d8fSMatthias Ringwald fprintf(stderr, "SDP attribute value buffer size exceeded: available %d, required %d\n", attribute_value_buffer_size, sdp_event_query_attribute_byte_get_attribute_length(packet)); 406bcf00d8fSMatthias Ringwald } 407bcf00d8fSMatthias Ringwald break; 408bcf00d8fSMatthias Ringwald 409bcf00d8fSMatthias Ringwald case SDP_EVENT_QUERY_COMPLETE: 410bcf00d8fSMatthias Ringwald fprintf(stderr, "General query done with status %d.\n", sdp_event_query_complete_get_status(packet)); 411bcf00d8fSMatthias Ringwald 412bcf00d8fSMatthias Ringwald break; 413bcf00d8fSMatthias Ringwald } 414bcf00d8fSMatthias Ringwald } 415bcf00d8fSMatthias Ringwald 416bcf00d8fSMatthias Ringwald /* 417bcf00d8fSMatthias Ringwald * @section Packet Handler 418bcf00d8fSMatthias Ringwald * 419bcf00d8fSMatthias Ringwald * @text The packet handler responds to various HCI Events. 420bcf00d8fSMatthias Ringwald */ 421bcf00d8fSMatthias Ringwald 422bcf00d8fSMatthias Ringwald 423bcf00d8fSMatthias Ringwald /* LISTING_START(packetHandler): Packet Handler */ 424bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) 425bcf00d8fSMatthias Ringwald { 426bcf00d8fSMatthias Ringwald /* LISTING_PAUSE */ 427bcf00d8fSMatthias Ringwald int rc; 428bcf00d8fSMatthias Ringwald uint8_t event; 429bcf00d8fSMatthias Ringwald bd_addr_t event_addr; 430bcf00d8fSMatthias Ringwald bd_addr_t local_addr; 431bcf00d8fSMatthias Ringwald uint16_t uuid_source; 432bcf00d8fSMatthias Ringwald uint16_t uuid_dest; 433bcf00d8fSMatthias Ringwald uint16_t mtu; 434bcf00d8fSMatthias Ringwald 435bcf00d8fSMatthias Ringwald /* LISTING_RESUME */ 436bcf00d8fSMatthias Ringwald switch (packet_type) { 437bcf00d8fSMatthias Ringwald case HCI_EVENT_PACKET: 4380e2df43fSMatthias Ringwald event = hci_event_packet_get_type(packet); 439bcf00d8fSMatthias Ringwald switch (event) { 440bcf00d8fSMatthias Ringwald /* @text When BTSTACK_EVENT_STATE with state HCI_STATE_WORKING 441bcf00d8fSMatthias Ringwald * is received and the example is started in client mode, the remote SDP BNEP query is started. 442bcf00d8fSMatthias Ringwald */ 443bcf00d8fSMatthias Ringwald case BTSTACK_EVENT_STATE: 444be7cc9a0SMilanka Ringwald if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 445bcf00d8fSMatthias Ringwald printf("Start SDP BNEP query.\n"); 446423c667cSMatthias Ringwald sdp_client_query_uuid16(&handle_sdp_client_query_result, remote, SDP_BNEPProtocol); 447bcf00d8fSMatthias Ringwald } 448bcf00d8fSMatthias Ringwald break; 449bcf00d8fSMatthias Ringwald 450bcf00d8fSMatthias Ringwald /* LISTING_PAUSE */ 451bcf00d8fSMatthias Ringwald case HCI_EVENT_PIN_CODE_REQUEST: 452bcf00d8fSMatthias Ringwald // inform about pin code request 453bcf00d8fSMatthias Ringwald printf("Pin code request - using '0000'\n"); 454a6ef64baSMilanka Ringwald hci_event_pin_code_request_get_bd_addr(packet, event_addr); 455bcf00d8fSMatthias Ringwald hci_send_cmd(&hci_pin_code_request_reply, &event_addr, 4, "0000"); 456bcf00d8fSMatthias Ringwald break; 457bcf00d8fSMatthias Ringwald 458bcf00d8fSMatthias Ringwald case HCI_EVENT_USER_CONFIRMATION_REQUEST: 459bcf00d8fSMatthias Ringwald // inform about user confirmation request 460bcf00d8fSMatthias Ringwald printf("SSP User Confirmation Request with numeric value '%06u'\n", little_endian_read_32(packet, 8)); 461bcf00d8fSMatthias Ringwald printf("SSP User Confirmation Auto accept\n"); 462bcf00d8fSMatthias Ringwald break; 463bcf00d8fSMatthias Ringwald 464bcf00d8fSMatthias Ringwald /* LISTING_RESUME */ 465bcf00d8fSMatthias Ringwald 466423c667cSMatthias Ringwald /* @text BNEP_EVENT_CHANNEL_OPENED is received after a BNEP connection was established or 467bcf00d8fSMatthias Ringwald * or when the connection fails. The status field returns the error code. 468bcf00d8fSMatthias Ringwald * 469bcf00d8fSMatthias Ringwald * The TAP network interface is then configured. A data source is set up and registered with the 470bcf00d8fSMatthias Ringwald * run loop to receive Ethernet packets from the TAP interface. 471bcf00d8fSMatthias Ringwald * 472bcf00d8fSMatthias Ringwald * The event contains both the source and destination UUIDs, as well as the MTU for this connection and 473bcf00d8fSMatthias Ringwald * the BNEP Channel ID, which is used for sending Ethernet packets over BNEP. 474bcf00d8fSMatthias Ringwald */ 475423c667cSMatthias Ringwald case BNEP_EVENT_CHANNEL_OPENED: 476*508c11d6SMilanka Ringwald if (bnep_event_channel_opened_get_status(packet)) { 477*508c11d6SMilanka Ringwald printf("BNEP channel open failed, status %02x\n", bnep_event_channel_opened_get_status(packet)); 478bcf00d8fSMatthias Ringwald } else { 479*508c11d6SMilanka Ringwald bnep_cid = bnep_event_channel_opened_get_bnep_cid(packet); 480*508c11d6SMilanka Ringwald uuid_source = bnep_event_channel_opened_get_source_uuid(packet); 481*508c11d6SMilanka Ringwald uuid_dest = bnep_event_channel_opened_get_destination_uuid(packet); 482*508c11d6SMilanka Ringwald mtu = bnep_event_channel_opened_get_mtu(packet); 483bcf00d8fSMatthias Ringwald //bt_flip_addr(event_addr, &packet[9]); 484423c667cSMatthias Ringwald memcpy(&event_addr, &packet[11], sizeof(bd_addr_t)); 485bcf00d8fSMatthias Ringwald printf("BNEP connection open succeeded to %s source UUID 0x%04x dest UUID: 0x%04x, max frame size %u\n", bd_addr_to_str(event_addr), uuid_source, uuid_dest, mtu); 486bcf00d8fSMatthias Ringwald /* Create the tap interface */ 487bcf00d8fSMatthias Ringwald gap_local_bd_addr(local_addr); 488bcf00d8fSMatthias Ringwald tap_fd = tap_alloc(tap_dev_name, local_addr); 489bcf00d8fSMatthias Ringwald if (tap_fd < 0) { 490bcf00d8fSMatthias Ringwald printf("Creating BNEP tap device failed: %s\n", strerror(errno)); 491bcf00d8fSMatthias Ringwald } else { 492bcf00d8fSMatthias Ringwald printf("BNEP device \"%s\" allocated.\n", tap_dev_name); 493bcf00d8fSMatthias Ringwald /* Create and register a new runloop data source */ 49480edae71SMatthias Ringwald btstack_run_loop_set_data_source_fd(&tap_dev_ds, tap_fd); 495423c667cSMatthias Ringwald btstack_run_loop_set_data_source_handler(&tap_dev_ds, &process_tap_dev_data); 496bcf00d8fSMatthias Ringwald btstack_run_loop_add_data_source(&tap_dev_ds); 497bcf00d8fSMatthias Ringwald } 498bcf00d8fSMatthias Ringwald } 499bcf00d8fSMatthias Ringwald break; 500bcf00d8fSMatthias Ringwald 501bcf00d8fSMatthias Ringwald /* @text If there is a timeout during the connection setup, BNEP_EVENT_CHANNEL_TIMEOUT will be received 502bcf00d8fSMatthias Ringwald * and the BNEP connection will be closed 503bcf00d8fSMatthias Ringwald */ 504bcf00d8fSMatthias Ringwald case BNEP_EVENT_CHANNEL_TIMEOUT: 505bcf00d8fSMatthias Ringwald printf("BNEP channel timeout! Channel will be closed\n"); 506bcf00d8fSMatthias Ringwald break; 507bcf00d8fSMatthias Ringwald 508bcf00d8fSMatthias Ringwald /* @text BNEP_EVENT_CHANNEL_CLOSED is received when the connection gets closed. 509bcf00d8fSMatthias Ringwald */ 510bcf00d8fSMatthias Ringwald case BNEP_EVENT_CHANNEL_CLOSED: 511bcf00d8fSMatthias Ringwald printf("BNEP channel closed\n"); 512bcf00d8fSMatthias Ringwald btstack_run_loop_remove_data_source(&tap_dev_ds); 513bcf00d8fSMatthias Ringwald if (tap_fd > 0) { 514bcf00d8fSMatthias Ringwald close(tap_fd); 515bcf00d8fSMatthias Ringwald tap_fd = -1; 516bcf00d8fSMatthias Ringwald } 517bcf00d8fSMatthias Ringwald break; 518bcf00d8fSMatthias Ringwald 5192d4c8c04SMatthias Ringwald /* @text BNEP_EVENT_CAN_SEND_NOW indicates that a new packet can be send. This triggers the retry of a 520bcf00d8fSMatthias Ringwald * parked network packet. If this succeeds, the data source element is added to the run loop again. 521bcf00d8fSMatthias Ringwald */ 5222d4c8c04SMatthias Ringwald case BNEP_EVENT_CAN_SEND_NOW: 523bcf00d8fSMatthias Ringwald // Check for parked network packets and send it out now 524bcf00d8fSMatthias Ringwald if (network_buffer_len > 0) { 525bcf00d8fSMatthias Ringwald bnep_send(bnep_cid, network_buffer, network_buffer_len); 526bcf00d8fSMatthias Ringwald network_buffer_len = 0; 527bcf00d8fSMatthias Ringwald // Re-add the tap device data source 528bcf00d8fSMatthias Ringwald btstack_run_loop_add_data_source(&tap_dev_ds); 529bcf00d8fSMatthias Ringwald } 530bcf00d8fSMatthias Ringwald 531bcf00d8fSMatthias Ringwald break; 532bcf00d8fSMatthias Ringwald 533bcf00d8fSMatthias Ringwald default: 534bcf00d8fSMatthias Ringwald break; 535bcf00d8fSMatthias Ringwald } 536bcf00d8fSMatthias Ringwald break; 537bcf00d8fSMatthias Ringwald 538bcf00d8fSMatthias Ringwald /* @text Ethernet packets from the remote device are received in the packet handler with type BNEP_DATA_PACKET. 539bcf00d8fSMatthias Ringwald * It is forwarded to the TAP interface. 540bcf00d8fSMatthias Ringwald */ 541bcf00d8fSMatthias Ringwald case BNEP_DATA_PACKET: 542bcf00d8fSMatthias Ringwald // Write out the ethernet frame to the tap device 543bcf00d8fSMatthias Ringwald if (tap_fd > 0) { 544bcf00d8fSMatthias Ringwald rc = write(tap_fd, packet, size); 545bcf00d8fSMatthias Ringwald if (rc < 0) { 546bcf00d8fSMatthias Ringwald fprintf(stderr, "TAP: Could not write to TAP device: %s\n", strerror(errno)); 547bcf00d8fSMatthias Ringwald } else 548bcf00d8fSMatthias Ringwald if (rc != size) { 549bcf00d8fSMatthias Ringwald fprintf(stderr, "TAP: Package written only partially %d of %d bytes\n", rc, size); 550bcf00d8fSMatthias Ringwald } 551bcf00d8fSMatthias Ringwald } 552bcf00d8fSMatthias Ringwald break; 553bcf00d8fSMatthias Ringwald 554bcf00d8fSMatthias Ringwald default: 555bcf00d8fSMatthias Ringwald break; 556bcf00d8fSMatthias Ringwald } 557bcf00d8fSMatthias Ringwald } 558bcf00d8fSMatthias Ringwald /* LISTING_END */ 559bcf00d8fSMatthias Ringwald 560bcf00d8fSMatthias Ringwald 561bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]); 562bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){ 563bcf00d8fSMatthias Ringwald 564bcf00d8fSMatthias Ringwald printf("Client HCI init done\n"); 565bcf00d8fSMatthias Ringwald 566bcf00d8fSMatthias Ringwald panu_setup(); 567bcf00d8fSMatthias Ringwald // Turn on the device 568bcf00d8fSMatthias Ringwald hci_power_control(HCI_POWER_ON); 569bcf00d8fSMatthias Ringwald return 0; 570bcf00d8fSMatthias Ringwald } 571bcf00d8fSMatthias Ringwald 572bcf00d8fSMatthias Ringwald /* EXAMPLE_END */ 573bcf00d8fSMatthias Ringwald /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4 -*- */ 574bcf00d8fSMatthias Ringwald 575