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