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