xref: /btstack/example/panu_demo.c (revision dbc206868bbbb9eae971122ef209312babb4bb14)
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         log_info("direct send");
284         bnep_send(bnep_cid, network_buffer, network_buffer_len);
285         network_buffer_len = 0;
286     } else {
287         log_info("cannort send, request permission");
288         // park the current network packet
289         btstack_run_loop_remove_data_source(&tap_dev_ds);
290         // and request a send permission
291         bnep_request_can_send_now_event(bnep_cid);
292     }
293     return;
294 }
295 /* LISTING_END */
296 
297 // PANU client routines
298 static char * get_string_from_data_element(uint8_t * element){
299     de_size_t de_size = de_get_size_type(element);
300     int pos     = de_get_header_size(element);
301     int len = 0;
302     switch (de_size){
303         case DE_SIZE_VAR_8:
304             len = element[1];
305             break;
306         case DE_SIZE_VAR_16:
307             len = big_endian_read_16(element, 1);
308             break;
309         default:
310             break;
311     }
312     char * str = (char*)malloc(len+1);
313     memcpy(str, &element[pos], len);
314     str[len] ='\0';
315     return str;
316 }
317 
318 
319 /* @section SDP parser callback
320  *
321  * @text The SDP parsers retrieves the BNEP PAN UUID as explained in
322  * Section [on SDP BNEP Query example](#sec:sdpbnepqueryExample}.
323  */
324 static void handle_sdp_client_record_complete(void){
325 
326     printf("SDP BNEP Record complete\n");
327 
328     // accept first entry or if we foudn a NAP and only have a PANU yet
329     if ((bnep_remote_uuid == 0) || (sdp_bnep_remote_uuid == BLUETOOTH_SERVICE_CLASS_NAP && bnep_remote_uuid == BLUETOOTH_SERVICE_CLASS_PANU)){
330         bnep_l2cap_psm   = sdp_bnep_l2cap_psm;
331         bnep_remote_uuid = sdp_bnep_remote_uuid;
332         bnep_version     = sdp_bnep_version;
333     }
334 }
335 
336 static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
337 
338     UNUSED(packet_type);
339     UNUSED(channel);
340     UNUSED(size);
341 
342     des_iterator_t des_list_it;
343     des_iterator_t prot_it;
344     char *str;
345 
346     switch (hci_event_packet_get_type(packet)){
347         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
348             // Handle new SDP record
349             if (sdp_event_query_attribute_byte_get_record_id(packet) != record_id) {
350                 handle_sdp_client_record_complete();
351                 // next record started
352                 record_id = sdp_event_query_attribute_byte_get_record_id(packet);
353                 printf("SDP Record: Nr: %d\n", record_id);
354             }
355 
356             if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) {
357                 attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
358 
359                 if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) {
360 
361                     switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) {
362                         case BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST:
363                             if (de_get_element_type(attribute_value) != DE_DES) break;
364                             for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
365                                 uint8_t * element = des_iterator_get_element(&des_list_it);
366                                 if (de_get_element_type(element) != DE_UUID) continue;
367                                 uint32_t uuid = de_get_uuid32(element);
368                                 switch (uuid){
369                                     case BLUETOOTH_SERVICE_CLASS_PANU:
370                                     case BLUETOOTH_SERVICE_CLASS_NAP:
371                                     case BLUETOOTH_SERVICE_CLASS_GN:
372                                         printf("SDP Attribute 0x%04x: BNEP PAN protocol UUID: %04x\n", sdp_event_query_attribute_byte_get_attribute_id(packet), uuid);
373                                         sdp_bnep_remote_uuid = uuid;
374                                         break;
375                                     default:
376                                         break;
377                                 }
378                             }
379                             break;
380                         case 0x0100:
381                         case 0x0101:
382                             str = get_string_from_data_element(attribute_value);
383                             printf("SDP Attribute: 0x%04x: %s\n", sdp_event_query_attribute_byte_get_attribute_id(packet), str);
384                             free(str);
385                             break;
386                         case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: {
387                                 printf("SDP Attribute: 0x%04x\n", sdp_event_query_attribute_byte_get_attribute_id(packet));
388 
389                                 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
390                                     uint8_t       *des_element;
391                                     uint8_t       *element;
392                                     uint32_t       uuid;
393 
394                                     if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
395 
396                                     des_element = des_iterator_get_element(&des_list_it);
397                                     des_iterator_init(&prot_it, des_element);
398                                     element = des_iterator_get_element(&prot_it);
399 
400                                     if (de_get_element_type(element) != DE_UUID) continue;
401 
402                                     uuid = de_get_uuid32(element);
403                                     switch (uuid){
404                                         case BLUETOOTH_PROTOCOL_L2CAP:
405                                             if (!des_iterator_has_more(&prot_it)) continue;
406                                             des_iterator_next(&prot_it);
407                                             de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_bnep_l2cap_psm);
408                                             break;
409                                         case BLUETOOTH_PROTOCOL_BNEP:
410                                             if (!des_iterator_has_more(&prot_it)) continue;
411                                             des_iterator_next(&prot_it);
412                                             de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_bnep_version);
413                                             break;
414                                         default:
415                                             break;
416                                     }
417                                 }
418                                 printf("l2cap_psm 0x%04x, bnep_version 0x%04x\n", bnep_l2cap_psm, bnep_version);
419 
420                             }
421                             break;
422                         default:
423                             break;
424                     }
425                 }
426             } else {
427                 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));
428             }
429             break;
430 
431         case SDP_EVENT_QUERY_COMPLETE:
432             handle_sdp_client_record_complete();
433             fprintf(stderr, "General query done with status %d, bnep psm %04x.\n", sdp_event_query_complete_get_status(packet), bnep_l2cap_psm);
434             if (bnep_l2cap_psm){
435                 /* Create BNEP connection */
436                 bnep_connect(packet_handler, remote_addr, bnep_l2cap_psm, BLUETOOTH_SERVICE_CLASS_PANU, bnep_remote_uuid);
437             } else {
438                 fprintf(stderr, "No BNEP service found\n");
439             }
440 
441             break;
442     }
443 }
444 
445 /*
446  * @section Packet Handler
447  *
448  * @text The packet handler responds to various HCI Events.
449  */
450 
451 
452 /* LISTING_START(packetHandler): Packet Handler */
453 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)
454 {
455 /* LISTING_PAUSE */
456     UNUSED(channel);
457 
458     int       rc;
459     uint8_t   event;
460     bd_addr_t event_addr;
461     bd_addr_t local_addr;
462     uint16_t  uuid_source;
463     uint16_t  uuid_dest;
464     uint16_t  mtu;
465 
466     /* LISTING_RESUME */
467     switch (packet_type) {
468 		case HCI_EVENT_PACKET:
469             event = hci_event_packet_get_type(packet);
470             switch (event) {
471                 /* @text When BTSTACK_EVENT_STATE with state HCI_STATE_WORKING
472                  * is received and the example is started in client mode, the remote SDP BNEP query is started.
473                  */
474                 case BTSTACK_EVENT_STATE:
475                     if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
476                         printf("Start SDP BNEP query.\n");
477                         sdp_client_query_uuid16(&handle_sdp_client_query_result, remote_addr, BLUETOOTH_PROTOCOL_BNEP);
478                     }
479                     break;
480 
481                 /* LISTING_PAUSE */
482                 case HCI_EVENT_PIN_CODE_REQUEST:
483 					// inform about pin code request
484                     printf("Pin code request - using '0000'\n");
485                     hci_event_pin_code_request_get_bd_addr(packet, event_addr);
486                     gap_pin_code_response(event_addr, "0000");
487 					break;
488 
489                 case HCI_EVENT_USER_CONFIRMATION_REQUEST:
490                     // inform about user confirmation request
491                     printf("SSP User Confirmation Request with numeric value '%06u'\n", little_endian_read_32(packet, 8));
492                     printf("SSP User Confirmation Auto accept\n");
493                     break;
494 
495                 /* LISTING_RESUME */
496 
497                 /* @text BNEP_EVENT_CHANNEL_OPENED is received after a BNEP connection was established or
498                  * or when the connection fails. The status field returns the error code.
499                  *
500                  * The TAP network interface is then configured. A data source is set up and registered with the
501                  * run loop to receive Ethernet packets from the TAP interface.
502                  *
503                  * The event contains both the source and destination UUIDs, as well as the MTU for this connection and
504                  * the BNEP Channel ID, which is used for sending Ethernet packets over BNEP.
505                  */
506 				case BNEP_EVENT_CHANNEL_OPENED:
507                     if (bnep_event_channel_opened_get_status(packet)) {
508                         printf("BNEP channel open failed, status %02x\n", bnep_event_channel_opened_get_status(packet));
509                     } else {
510                         bnep_cid    = bnep_event_channel_opened_get_bnep_cid(packet);
511                         uuid_source = bnep_event_channel_opened_get_source_uuid(packet);
512                         uuid_dest   = bnep_event_channel_opened_get_destination_uuid(packet);
513                         mtu         = bnep_event_channel_opened_get_mtu(packet);
514                         //bt_flip_addr(event_addr, &packet[9]);
515                         memcpy(&event_addr, &packet[11], sizeof(bd_addr_t));
516                         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);
517                         /* Create the tap interface */
518                         gap_local_bd_addr(local_addr);
519                         tap_fd = tap_alloc(tap_dev_name, local_addr);
520                         if (tap_fd < 0) {
521                             printf("Creating BNEP tap device failed: %s\n", strerror(errno));
522                         } else {
523                             printf("BNEP device \"%s\" allocated.\n", tap_dev_name);
524                             /* Create and register a new runloop data source */
525                             btstack_run_loop_set_data_source_fd(&tap_dev_ds, tap_fd);
526                             btstack_run_loop_set_data_source_handler(&tap_dev_ds, &process_tap_dev_data);
527                             btstack_run_loop_add_data_source(&tap_dev_ds);
528                         }
529                     }
530 					break;
531 
532                 /* @text If there is a timeout during the connection setup, BNEP_EVENT_CHANNEL_TIMEOUT will be received
533                  * and the BNEP connection  will be closed
534                  */
535                 case BNEP_EVENT_CHANNEL_TIMEOUT:
536                     printf("BNEP channel timeout! Channel will be closed\n");
537                     break;
538 
539                 /* @text BNEP_EVENT_CHANNEL_CLOSED is received when the connection gets closed.
540                  */
541                 case BNEP_EVENT_CHANNEL_CLOSED:
542                     printf("BNEP channel closed\n");
543                     btstack_run_loop_remove_data_source(&tap_dev_ds);
544                     if (tap_fd > 0) {
545                         close(tap_fd);
546                         tap_fd = -1;
547                     }
548                     break;
549 
550                 /* @text BNEP_EVENT_CAN_SEND_NOW indicates that a new packet can be send. This triggers the retry of a
551                  * parked network packet. If this succeeds, the data source element is added to the run loop again.
552                  */
553                 case BNEP_EVENT_CAN_SEND_NOW:
554                     // Check for parked network packets and send it out now
555                     if (network_buffer_len > 0) {
556                         log_info("indirect send");
557                         bnep_send(bnep_cid, network_buffer, network_buffer_len);
558                         network_buffer_len = 0;
559                         // Re-add the tap device data source
560                         btstack_run_loop_add_data_source(&tap_dev_ds);
561                     }
562 
563                     break;
564 
565                 default:
566                     break;
567             }
568             break;
569 
570         /* @text Ethernet packets from the remote device are received in the packet handler with type BNEP_DATA_PACKET.
571          * It is forwarded to the TAP interface.
572          */
573         case BNEP_DATA_PACKET:
574             // Write out the ethernet frame to the tap device
575             if (tap_fd > 0) {
576                 rc = write(tap_fd, packet, size);
577                 if (rc < 0) {
578                     fprintf(stderr, "TAP: Could not write to TAP device: %s\n", strerror(errno));
579                 } else
580                 if (rc != size) {
581                     fprintf(stderr, "TAP: Package written only partially %d of %d bytes\n", rc, size);
582                 }
583             }
584             break;
585 
586         default:
587             break;
588     }
589 }
590 /* LISTING_END */
591 
592 
593 int btstack_main(int argc, const char * argv[]);
594 int btstack_main(int argc, const char * argv[]){
595 
596     (void)argc;
597     (void)argv;
598 
599     printf("Client HCI init done\n");
600 
601     panu_setup();
602 
603     // parse human readable Bluetooth address
604     sscanf_bd_addr(remote_addr_string, remote_addr);
605 
606     // Turn on the device
607     hci_power_control(HCI_POWER_ON);
608     return 0;
609 }
610 
611 /* EXAMPLE_END */
612 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4 -*-  */
613 
614