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