xref: /btstack/example/panu_demo.c (revision 25c92c243797abf98ec103015a489f168ed94c64)
1bcf00d8fSMatthias Ringwald /*
2bcf00d8fSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3bcf00d8fSMatthias Ringwald  *
4bcf00d8fSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5bcf00d8fSMatthias Ringwald  * modification, are permitted provided that the following conditions
6bcf00d8fSMatthias Ringwald  * are met:
7bcf00d8fSMatthias Ringwald  *
8bcf00d8fSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9bcf00d8fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10bcf00d8fSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11bcf00d8fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12bcf00d8fSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13bcf00d8fSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14bcf00d8fSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15bcf00d8fSMatthias Ringwald  *    from this software without specific prior written permission.
16bcf00d8fSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17bcf00d8fSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18bcf00d8fSMatthias Ringwald  *    monetary gain.
19bcf00d8fSMatthias Ringwald  *
20bcf00d8fSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21bcf00d8fSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22bcf00d8fSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25bcf00d8fSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26bcf00d8fSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27bcf00d8fSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28bcf00d8fSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29bcf00d8fSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30bcf00d8fSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31bcf00d8fSMatthias Ringwald  * SUCH DAMAGE.
32bcf00d8fSMatthias Ringwald  *
33bcf00d8fSMatthias Ringwald  * Please inquire about commercial licensing options at
34bcf00d8fSMatthias Ringwald  * [email protected]
35bcf00d8fSMatthias Ringwald  *
36bcf00d8fSMatthias Ringwald  */
37bcf00d8fSMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "panu_demo.c"
39ab2c6ae4SMatthias Ringwald 
40bcf00d8fSMatthias Ringwald /*
41bcf00d8fSMatthias Ringwald  * panu_demo.c
42bcf00d8fSMatthias Ringwald  * Author: Ole Reinhardt <[email protected]>
43bcf00d8fSMatthias Ringwald  */
44bcf00d8fSMatthias Ringwald 
45ec8ae085SMilanka Ringwald /* EXAMPLE_START(panu_demo): BNEP/PANU (Linux only)
46bcf00d8fSMatthias Ringwald  *
47bcf00d8fSMatthias Ringwald  * @text This example implements both a PANU client and a server. In server mode, it
48bcf00d8fSMatthias Ringwald  * sets up a BNEP server and registers a PANU SDP record and waits for incoming connections.
49bcf00d8fSMatthias Ringwald  * In client mode, it connects to a remote device, does an SDP Query to identify the PANU
50bcf00d8fSMatthias Ringwald  * service and initiates a BNEP connection.
5158d7a529SMilanka Ringwald  *
52ec8ae085SMilanka Ringwald  * Note: currently supported only on Linux and provides a TAP network interface which you
53ec8ae085SMilanka Ringwald  *       can configure yourself.
54cb3797fcSMatthias Ringwald  *
55ec8ae085SMilanka Ringwald  * To enable client mode, uncomment ENABLE_PANU_CLIENT below.
56bcf00d8fSMatthias Ringwald  */
57bcf00d8fSMatthias Ringwald 
58bcf00d8fSMatthias Ringwald #include <stdio.h>
59bcf00d8fSMatthias Ringwald 
60a7473022SMatthias Ringwald #include "btstack_config.h"
614d2bcd2aSMatthias Ringwald #include "btstack.h"
62bcf00d8fSMatthias Ringwald 
63cb3797fcSMatthias Ringwald // #define ENABLE_PANU_CLIENT
64cb3797fcSMatthias Ringwald 
656a4fc708SMatthias Ringwald // network types
666a4fc708SMatthias Ringwald #define NETWORK_TYPE_IPv4       0x0800
676a4fc708SMatthias Ringwald #define NETWORK_TYPE_ARP        0x0806
686a4fc708SMatthias Ringwald #define NETWORK_TYPE_IPv6       0x86DD
696a4fc708SMatthias Ringwald 
70bcf00d8fSMatthias Ringwald static uint16_t bnep_cid            = 0;
71bcf00d8fSMatthias Ringwald 
72e5d43f34SMatthias Ringwald #ifdef ENABLE_PANU_CLIENT
73e5d43f34SMatthias Ringwald 
74e5d43f34SMatthias Ringwald static int record_id = -1;
75e5d43f34SMatthias Ringwald 
76e5d43f34SMatthias Ringwald static uint16_t bnep_version        = 0;
77e5d43f34SMatthias Ringwald static uint32_t bnep_remote_uuid    = 0;
78e5d43f34SMatthias Ringwald static uint16_t bnep_l2cap_psm      = 0;
79e5d43f34SMatthias Ringwald 
80b5a442a7SMatthias Ringwald static uint16_t sdp_bnep_l2cap_psm      = 0;
81b5a442a7SMatthias Ringwald static uint16_t sdp_bnep_version        = 0;
82b5a442a7SMatthias Ringwald static uint32_t sdp_bnep_remote_uuid    = 0;
83b5a442a7SMatthias Ringwald 
84bcf00d8fSMatthias Ringwald static uint8_t   attribute_value[1000];
85bcf00d8fSMatthias Ringwald static const unsigned int attribute_value_buffer_size = sizeof(attribute_value);
86e5d43f34SMatthias Ringwald #endif
87bcf00d8fSMatthias Ringwald 
88a7473022SMatthias Ringwald // MBP 2016
896a4fc708SMatthias Ringwald static const char * remote_addr_string = "78:4F:43:8C:B2:5D";
90a7473022SMatthias Ringwald // Wiko Sunny static const char * remote_addr_string = "A0:4C:5B:0F:B2:42";
91a7473022SMatthias Ringwald 
927693aad0SMatthias Ringwald static bd_addr_t remote_addr;
93bcf00d8fSMatthias Ringwald 
94bcf00d8fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
95bcf00d8fSMatthias Ringwald 
96a7473022SMatthias Ringwald // outgoing network packet
97a7473022SMatthias Ringwald static const uint8_t * network_buffer;
98a7473022SMatthias Ringwald static uint16_t network_buffer_len;
99a7473022SMatthias Ringwald 
100cb3797fcSMatthias Ringwald static uint8_t panu_sdp_record[220];
1016a4fc708SMatthias Ringwald 
102bcf00d8fSMatthias Ringwald /* @section Main application configuration
103bcf00d8fSMatthias Ringwald  *
104bcf00d8fSMatthias Ringwald  * @text In the application configuration, L2CAP and BNEP are initialized and a BNEP service, for server mode,
105bcf00d8fSMatthias Ringwald  * is registered, before the Bluetooth stack gets started, as shown in Listing PanuSetup.
106bcf00d8fSMatthias Ringwald  */
107bcf00d8fSMatthias Ringwald 
108bcf00d8fSMatthias Ringwald /* LISTING_START(PanuSetup): Panu setup */
109bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
110*25c92c24SMatthias Ringwald #ifdef ENABLE_PANU_CLIENT
111423c667cSMatthias Ringwald static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
112*25c92c24SMatthias Ringwald #endif
113a7473022SMatthias Ringwald static void network_send_packet_callback(const uint8_t * packet, uint16_t size);
114bcf00d8fSMatthias Ringwald 
panu_setup(void)115bcf00d8fSMatthias Ringwald static void panu_setup(void){
116bcf00d8fSMatthias Ringwald 
117bcf00d8fSMatthias Ringwald     // Initialize L2CAP
118bcf00d8fSMatthias Ringwald     l2cap_init();
119bcf00d8fSMatthias Ringwald 
1208c9bb29eSMatthias Ringwald #ifdef ENABLE_BLE
1218c9bb29eSMatthias Ringwald     // Initialize LE Security Manager. Needed for cross-transport key derivation
1228c9bb29eSMatthias Ringwald     sm_init();
1238c9bb29eSMatthias Ringwald #endif
124bcf00d8fSMatthias Ringwald 
1256a4fc708SMatthias Ringwald     // init SDP, create record for PANU and register with SDP
1266a4fc708SMatthias Ringwald     sdp_init();
127cb3797fcSMatthias Ringwald     memset(panu_sdp_record, 0, sizeof(panu_sdp_record));
1286a4fc708SMatthias Ringwald     uint16_t network_packet_types[] = { NETWORK_TYPE_IPv4, NETWORK_TYPE_ARP, 0};    // 0 as end of list
1294c9cb4f5SMatthias Ringwald 
1304c9cb4f5SMatthias Ringwald     // Initialise BNEP
1314c9cb4f5SMatthias Ringwald     bnep_init();
1324c9cb4f5SMatthias Ringwald     // Minimum L2CAP MTU for bnep is 1691 bytes
133cb3797fcSMatthias Ringwald #ifdef ENABLE_PANU_CLIENT
1344c9cb4f5SMatthias Ringwald     bnep_register_service(packet_handler, BLUETOOTH_SERVICE_CLASS_PANU, 1691);
1354c9cb4f5SMatthias Ringwald     // PANU
136cb3797fcSMatthias Ringwald     pan_create_panu_sdp_record(panu_sdp_record, sdp_create_service_record_handle(), network_packet_types, NULL, NULL, BNEP_SECURITY_NONE);
137cb3797fcSMatthias Ringwald #else
1384c9cb4f5SMatthias Ringwald     bnep_register_service(packet_handler, BLUETOOTH_SERVICE_CLASS_NAP, 1691);
1394c9cb4f5SMatthias Ringwald     // NAP Network Access Type: Other, 1 MB/s
1404c9cb4f5SMatthias Ringwald     pan_create_nap_sdp_record(panu_sdp_record, sdp_create_service_record_handle(), network_packet_types, NULL, NULL, BNEP_SECURITY_NONE, PAN_NET_ACCESS_TYPE_OTHER, 1000000, NULL, NULL);
141cb3797fcSMatthias Ringwald #endif
142762141afSMatthias Ringwald     btstack_assert(de_get_len( panu_sdp_record) <= sizeof(panu_sdp_record));
143cb3797fcSMatthias Ringwald     sdp_register_service(panu_sdp_record);
1446a4fc708SMatthias Ringwald 
145a7473022SMatthias Ringwald     // Initialize network interface
146a7473022SMatthias Ringwald     btstack_network_init(&network_send_packet_callback);
147a4fe6467SMatthias Ringwald 
148a4fe6467SMatthias Ringwald     // register for HCI events
149a4fe6467SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
150a4fe6467SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
151bcf00d8fSMatthias Ringwald }
152bcf00d8fSMatthias Ringwald /* LISTING_END */
153bcf00d8fSMatthias Ringwald 
154cb3797fcSMatthias Ringwald #ifdef ENABLE_PANU_CLIENT
155cb3797fcSMatthias Ringwald 
156bcf00d8fSMatthias Ringwald // PANU client routines
get_string_from_data_element(uint8_t * element,uint16_t buffer_size,char * buffer_data)1579975f1a3SMatthias Ringwald static void get_string_from_data_element(uint8_t * element, uint16_t buffer_size, char * buffer_data){
158bcf00d8fSMatthias Ringwald     de_size_t de_size = de_get_size_type(element);
159bcf00d8fSMatthias Ringwald     int pos     = de_get_header_size(element);
160bcf00d8fSMatthias Ringwald     int len = 0;
161bcf00d8fSMatthias Ringwald     switch (de_size){
162bcf00d8fSMatthias Ringwald         case DE_SIZE_VAR_8:
163bcf00d8fSMatthias Ringwald             len = element[1];
164bcf00d8fSMatthias Ringwald             break;
165bcf00d8fSMatthias Ringwald         case DE_SIZE_VAR_16:
166bcf00d8fSMatthias Ringwald             len = big_endian_read_16(element, 1);
167bcf00d8fSMatthias Ringwald             break;
168bcf00d8fSMatthias Ringwald         default:
169bcf00d8fSMatthias Ringwald             break;
170bcf00d8fSMatthias Ringwald     }
1719975f1a3SMatthias Ringwald     uint16_t bytes_to_copy = btstack_min(buffer_size-1,len);
1729975f1a3SMatthias Ringwald     memcpy(buffer_data, &element[pos], bytes_to_copy);
1739975f1a3SMatthias Ringwald     buffer_data[bytes_to_copy] ='\0';
174bcf00d8fSMatthias Ringwald }
175bcf00d8fSMatthias Ringwald 
176bcf00d8fSMatthias Ringwald 
177bcf00d8fSMatthias Ringwald /* @section SDP parser callback
178bcf00d8fSMatthias Ringwald  *
179bcf00d8fSMatthias Ringwald  * @text The SDP parsers retrieves the BNEP PAN UUID as explained in
180bcf00d8fSMatthias Ringwald  * Section [on SDP BNEP Query example](#sec:sdpbnepqueryExample}.
181bcf00d8fSMatthias Ringwald  */
handle_sdp_client_record_complete(void)182b5a442a7SMatthias Ringwald static void handle_sdp_client_record_complete(void){
183b5a442a7SMatthias Ringwald 
184b5a442a7SMatthias Ringwald     printf("SDP BNEP Record complete\n");
185b5a442a7SMatthias Ringwald 
186b5a442a7SMatthias Ringwald     // accept first entry or if we foudn a NAP and only have a PANU yet
187b5a442a7SMatthias Ringwald     if ((bnep_remote_uuid == 0) || (sdp_bnep_remote_uuid == BLUETOOTH_SERVICE_CLASS_NAP && bnep_remote_uuid == BLUETOOTH_SERVICE_CLASS_PANU)){
188b5a442a7SMatthias Ringwald         bnep_l2cap_psm   = sdp_bnep_l2cap_psm;
189b5a442a7SMatthias Ringwald         bnep_remote_uuid = sdp_bnep_remote_uuid;
190b5a442a7SMatthias Ringwald         bnep_version     = sdp_bnep_version;
191b5a442a7SMatthias Ringwald     }
192b5a442a7SMatthias Ringwald }
193b5a442a7SMatthias Ringwald 
handle_sdp_client_query_result(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)194423c667cSMatthias Ringwald static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
195bcf00d8fSMatthias Ringwald 
196c83b0215SMatthias Ringwald     UNUSED(packet_type);
197c83b0215SMatthias Ringwald     UNUSED(channel);
198c83b0215SMatthias Ringwald     UNUSED(size);
199c83b0215SMatthias Ringwald 
200bcf00d8fSMatthias Ringwald     des_iterator_t des_list_it;
201bcf00d8fSMatthias Ringwald     des_iterator_t prot_it;
2029975f1a3SMatthias Ringwald     char str[20];
203bcf00d8fSMatthias Ringwald 
2040e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
205bcf00d8fSMatthias Ringwald         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
206bcf00d8fSMatthias Ringwald             // Handle new SDP record
207bcf00d8fSMatthias Ringwald             if (sdp_event_query_attribute_byte_get_record_id(packet) != record_id) {
208b5a442a7SMatthias Ringwald                 handle_sdp_client_record_complete();
209b5a442a7SMatthias Ringwald                 // next record started
210bcf00d8fSMatthias Ringwald                 record_id = sdp_event_query_attribute_byte_get_record_id(packet);
211bcf00d8fSMatthias Ringwald                 printf("SDP Record: Nr: %d\n", record_id);
212bcf00d8fSMatthias Ringwald             }
213bcf00d8fSMatthias Ringwald 
214bcf00d8fSMatthias Ringwald             if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) {
215bcf00d8fSMatthias Ringwald                 attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
216bcf00d8fSMatthias Ringwald 
217bcf00d8fSMatthias Ringwald                 if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) {
218bcf00d8fSMatthias Ringwald 
219bcf00d8fSMatthias Ringwald                     switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) {
220235946f1SMatthias Ringwald                         case BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST:
221bcf00d8fSMatthias Ringwald                             if (de_get_element_type(attribute_value) != DE_DES) break;
222bcf00d8fSMatthias Ringwald                             for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
223bcf00d8fSMatthias Ringwald                                 uint8_t * element = des_iterator_get_element(&des_list_it);
224bcf00d8fSMatthias Ringwald                                 if (de_get_element_type(element) != DE_UUID) continue;
225bcf00d8fSMatthias Ringwald                                 uint32_t uuid = de_get_uuid32(element);
226bcf00d8fSMatthias Ringwald                                 switch (uuid){
227235946f1SMatthias Ringwald                                     case BLUETOOTH_SERVICE_CLASS_PANU:
228235946f1SMatthias Ringwald                                     case BLUETOOTH_SERVICE_CLASS_NAP:
229235946f1SMatthias Ringwald                                     case BLUETOOTH_SERVICE_CLASS_GN:
230bcf00d8fSMatthias Ringwald                                         printf("SDP Attribute 0x%04x: BNEP PAN protocol UUID: %04x\n", sdp_event_query_attribute_byte_get_attribute_id(packet), uuid);
231b5a442a7SMatthias Ringwald                                         sdp_bnep_remote_uuid = uuid;
232bcf00d8fSMatthias Ringwald                                         break;
233bcf00d8fSMatthias Ringwald                                     default:
234bcf00d8fSMatthias Ringwald                                         break;
235bcf00d8fSMatthias Ringwald                                 }
236bcf00d8fSMatthias Ringwald                             }
237bcf00d8fSMatthias Ringwald                             break;
238bcf00d8fSMatthias Ringwald                         case 0x0100:
239bcf00d8fSMatthias Ringwald                         case 0x0101:
2409975f1a3SMatthias Ringwald                             get_string_from_data_element(attribute_value, sizeof(str), str);
241bcf00d8fSMatthias Ringwald                             printf("SDP Attribute: 0x%04x: %s\n", sdp_event_query_attribute_byte_get_attribute_id(packet), str);
242bcf00d8fSMatthias Ringwald                             break;
243235946f1SMatthias Ringwald                         case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: {
244bcf00d8fSMatthias Ringwald                                 printf("SDP Attribute: 0x%04x\n", sdp_event_query_attribute_byte_get_attribute_id(packet));
245bcf00d8fSMatthias Ringwald 
246bcf00d8fSMatthias Ringwald                                 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
247bcf00d8fSMatthias Ringwald                                     uint8_t       *des_element;
248bcf00d8fSMatthias Ringwald                                     uint8_t       *element;
249bcf00d8fSMatthias Ringwald                                     uint32_t       uuid;
250bcf00d8fSMatthias Ringwald 
251bcf00d8fSMatthias Ringwald                                     if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
252bcf00d8fSMatthias Ringwald 
253bcf00d8fSMatthias Ringwald                                     des_element = des_iterator_get_element(&des_list_it);
254bcf00d8fSMatthias Ringwald                                     des_iterator_init(&prot_it, des_element);
255bcf00d8fSMatthias Ringwald                                     element = des_iterator_get_element(&prot_it);
256bcf00d8fSMatthias Ringwald 
25714fd128cSMatthias Ringwald                                     if (!element) continue;
258bcf00d8fSMatthias Ringwald                                     if (de_get_element_type(element) != DE_UUID) continue;
259bcf00d8fSMatthias Ringwald 
260bcf00d8fSMatthias Ringwald                                     uuid = de_get_uuid32(element);
26114fd128cSMatthias Ringwald                                     des_iterator_next(&prot_it);
262bcf00d8fSMatthias Ringwald                                     switch (uuid){
263235946f1SMatthias Ringwald                                         case BLUETOOTH_PROTOCOL_L2CAP:
264bcf00d8fSMatthias Ringwald                                             if (!des_iterator_has_more(&prot_it)) continue;
265b5a442a7SMatthias Ringwald                                             de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_bnep_l2cap_psm);
266bcf00d8fSMatthias Ringwald                                             break;
267235946f1SMatthias Ringwald                                         case BLUETOOTH_PROTOCOL_BNEP:
268bcf00d8fSMatthias Ringwald                                             if (!des_iterator_has_more(&prot_it)) continue;
269b5a442a7SMatthias Ringwald                                             de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_bnep_version);
270bcf00d8fSMatthias Ringwald                                             break;
271bcf00d8fSMatthias Ringwald                                         default:
272bcf00d8fSMatthias Ringwald                                             break;
273bcf00d8fSMatthias Ringwald                                     }
274bcf00d8fSMatthias Ringwald                                 }
275e0f9c2f9SMatthias Ringwald                                 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);
276bcf00d8fSMatthias Ringwald 
277bcf00d8fSMatthias Ringwald                             }
278bcf00d8fSMatthias Ringwald                             break;
279bcf00d8fSMatthias Ringwald                         default:
280bcf00d8fSMatthias Ringwald                             break;
281bcf00d8fSMatthias Ringwald                     }
282bcf00d8fSMatthias Ringwald                 }
283bcf00d8fSMatthias Ringwald             } else {
284bcf00d8fSMatthias 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));
285bcf00d8fSMatthias Ringwald             }
286bcf00d8fSMatthias Ringwald             break;
287bcf00d8fSMatthias Ringwald 
288bcf00d8fSMatthias Ringwald         case SDP_EVENT_QUERY_COMPLETE:
289b5a442a7SMatthias Ringwald             handle_sdp_client_record_complete();
290fcd55a0bSMilanka Ringwald             fprintf(stderr, "Query complete, status 0%02x, bnep psm 0x%04x.\n", sdp_event_query_complete_get_status(packet), bnep_l2cap_psm);
2917693aad0SMatthias Ringwald             if (bnep_l2cap_psm){
2927693aad0SMatthias Ringwald                 /* Create BNEP connection */
2937693aad0SMatthias Ringwald                 bnep_connect(packet_handler, remote_addr, bnep_l2cap_psm, BLUETOOTH_SERVICE_CLASS_PANU, bnep_remote_uuid);
2947693aad0SMatthias Ringwald             } else {
2957693aad0SMatthias Ringwald                 fprintf(stderr, "No BNEP service found\n");
2967693aad0SMatthias Ringwald             }
297bcf00d8fSMatthias Ringwald 
298bcf00d8fSMatthias Ringwald             break;
299bcf00d8fSMatthias Ringwald     }
300bcf00d8fSMatthias Ringwald }
301cb3797fcSMatthias Ringwald #endif
302bcf00d8fSMatthias Ringwald 
303bcf00d8fSMatthias Ringwald /*
304bcf00d8fSMatthias Ringwald  * @section Packet Handler
305bcf00d8fSMatthias Ringwald  *
306bcf00d8fSMatthias Ringwald  * @text The packet handler responds to various HCI Events.
307bcf00d8fSMatthias Ringwald  */
308bcf00d8fSMatthias Ringwald 
309bcf00d8fSMatthias Ringwald /* LISTING_START(packetHandler): Packet Handler */
packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)310bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)
311bcf00d8fSMatthias Ringwald {
312bcf00d8fSMatthias Ringwald /* LISTING_PAUSE */
313c83b0215SMatthias Ringwald     UNUSED(channel);
314c83b0215SMatthias Ringwald 
315bcf00d8fSMatthias Ringwald     uint8_t   event;
316bcf00d8fSMatthias Ringwald     bd_addr_t event_addr;
317bcf00d8fSMatthias Ringwald     bd_addr_t local_addr;
318bcf00d8fSMatthias Ringwald     uint16_t  uuid_source;
319bcf00d8fSMatthias Ringwald     uint16_t  uuid_dest;
320bcf00d8fSMatthias Ringwald     uint16_t  mtu;
321bcf00d8fSMatthias Ringwald 
322bcf00d8fSMatthias Ringwald     /* LISTING_RESUME */
323bcf00d8fSMatthias Ringwald     switch (packet_type) {
324bcf00d8fSMatthias Ringwald 		case HCI_EVENT_PACKET:
3250e2df43fSMatthias Ringwald             event = hci_event_packet_get_type(packet);
326bcf00d8fSMatthias Ringwald             switch (event) {
327cb3797fcSMatthias Ringwald #ifdef ENABLE_PANU_CLIENT
328bcf00d8fSMatthias Ringwald                 /* @text When BTSTACK_EVENT_STATE with state HCI_STATE_WORKING
329bcf00d8fSMatthias Ringwald                  * is received and the example is started in client mode, the remote SDP BNEP query is started.
330bcf00d8fSMatthias Ringwald                  */
331bcf00d8fSMatthias Ringwald                 case BTSTACK_EVENT_STATE:
332be7cc9a0SMilanka Ringwald                     if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
333e0f9c2f9SMatthias Ringwald                         printf("Start SDP BNEP query for remote PAN Network Access Point (NAP).\n");
334e0f9c2f9SMatthias Ringwald                         sdp_client_query_uuid16(&handle_sdp_client_query_result, remote_addr, BLUETOOTH_SERVICE_CLASS_NAP);
335bcf00d8fSMatthias Ringwald                     }
336bcf00d8fSMatthias Ringwald                     break;
337cb3797fcSMatthias Ringwald #endif
338bcf00d8fSMatthias Ringwald                 /* LISTING_PAUSE */
339bcf00d8fSMatthias Ringwald                 case HCI_EVENT_PIN_CODE_REQUEST:
340bcf00d8fSMatthias Ringwald 					// inform about pin code request
341bcf00d8fSMatthias Ringwald                     printf("Pin code request - using '0000'\n");
342a6ef64baSMilanka Ringwald                     hci_event_pin_code_request_get_bd_addr(packet, event_addr);
3434d2bcd2aSMatthias Ringwald                     gap_pin_code_response(event_addr, "0000");
344bcf00d8fSMatthias Ringwald 					break;
345bcf00d8fSMatthias Ringwald 
346bcf00d8fSMatthias Ringwald                 case HCI_EVENT_USER_CONFIRMATION_REQUEST:
347bcf00d8fSMatthias Ringwald                     // inform about user confirmation request
348bcf00d8fSMatthias Ringwald                     printf("SSP User Confirmation Request with numeric value '%06u'\n", little_endian_read_32(packet, 8));
349bcf00d8fSMatthias Ringwald                     printf("SSP User Confirmation Auto accept\n");
350bcf00d8fSMatthias Ringwald                     break;
351bcf00d8fSMatthias Ringwald 
352bcf00d8fSMatthias Ringwald                 /* LISTING_RESUME */
353bcf00d8fSMatthias Ringwald 
354423c667cSMatthias Ringwald                 /* @text BNEP_EVENT_CHANNEL_OPENED is received after a BNEP connection was established or
355bcf00d8fSMatthias Ringwald                  * or when the connection fails. The status field returns the error code.
356bcf00d8fSMatthias Ringwald                  *
357bcf00d8fSMatthias Ringwald                  * The TAP network interface is then configured. A data source is set up and registered with the
358bcf00d8fSMatthias Ringwald                  * run loop to receive Ethernet packets from the TAP interface.
359bcf00d8fSMatthias Ringwald                  *
360bcf00d8fSMatthias Ringwald                  * The event contains both the source and destination UUIDs, as well as the MTU for this connection and
361bcf00d8fSMatthias Ringwald                  * the BNEP Channel ID, which is used for sending Ethernet packets over BNEP.
362bcf00d8fSMatthias Ringwald                  */
363423c667cSMatthias Ringwald 				case BNEP_EVENT_CHANNEL_OPENED:
364508c11d6SMilanka Ringwald                     if (bnep_event_channel_opened_get_status(packet)) {
365fcd55a0bSMilanka Ringwald                         printf("BNEP channel open failed, status 0x%02x\n", bnep_event_channel_opened_get_status(packet));
366bcf00d8fSMatthias Ringwald                     } else {
367508c11d6SMilanka Ringwald                         bnep_cid    = bnep_event_channel_opened_get_bnep_cid(packet);
368508c11d6SMilanka Ringwald                         uuid_source = bnep_event_channel_opened_get_source_uuid(packet);
369508c11d6SMilanka Ringwald                         uuid_dest   = bnep_event_channel_opened_get_destination_uuid(packet);
370508c11d6SMilanka Ringwald                         mtu         = bnep_event_channel_opened_get_mtu(packet);
371fb389631SMatthias Ringwald                         bnep_event_channel_opened_get_remote_address(packet, event_addr);
372bcf00d8fSMatthias 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);
373fb389631SMatthias Ringwald 
374a7473022SMatthias Ringwald                         /* Setup network interface */
375bcf00d8fSMatthias Ringwald                         gap_local_bd_addr(local_addr);
376a7473022SMatthias Ringwald                         btstack_network_up(local_addr);
37784693d68SMatthias Ringwald                         printf("Network Interface %s activated\n", btstack_network_get_name());
378bcf00d8fSMatthias Ringwald                     }
379bcf00d8fSMatthias Ringwald 					break;
380bcf00d8fSMatthias Ringwald 
381bcf00d8fSMatthias Ringwald                 /* @text If there is a timeout during the connection setup, BNEP_EVENT_CHANNEL_TIMEOUT will be received
382bcf00d8fSMatthias Ringwald                  * and the BNEP connection  will be closed
383bcf00d8fSMatthias Ringwald                  */
384bcf00d8fSMatthias Ringwald                 case BNEP_EVENT_CHANNEL_TIMEOUT:
385bcf00d8fSMatthias Ringwald                     printf("BNEP channel timeout! Channel will be closed\n");
386bcf00d8fSMatthias Ringwald                     break;
387bcf00d8fSMatthias Ringwald 
388bcf00d8fSMatthias Ringwald                 /* @text BNEP_EVENT_CHANNEL_CLOSED is received when the connection gets closed.
389bcf00d8fSMatthias Ringwald                  */
390bcf00d8fSMatthias Ringwald                 case BNEP_EVENT_CHANNEL_CLOSED:
391bcf00d8fSMatthias Ringwald                     printf("BNEP channel closed\n");
392a7473022SMatthias Ringwald                     btstack_network_down();
393bcf00d8fSMatthias Ringwald                     break;
394bcf00d8fSMatthias Ringwald 
39536b4a12cSMatthias Ringwald                 /* @text BNEP_EVENT_CAN_SEND_NOW indicates that a new packet can be send. This triggers the send of a
39636b4a12cSMatthias Ringwald                  * stored network packet. The tap datas source can be enabled again
397bcf00d8fSMatthias Ringwald                  */
3982d4c8c04SMatthias Ringwald                 case BNEP_EVENT_CAN_SEND_NOW:
399bcf00d8fSMatthias Ringwald                     if (network_buffer_len > 0) {
400a7473022SMatthias Ringwald                         bnep_send(bnep_cid, (uint8_t*) network_buffer, network_buffer_len);
401bcf00d8fSMatthias Ringwald                         network_buffer_len = 0;
402a7473022SMatthias Ringwald                         btstack_network_packet_sent();
403bcf00d8fSMatthias Ringwald                     }
404bcf00d8fSMatthias Ringwald                     break;
405bcf00d8fSMatthias Ringwald 
406bcf00d8fSMatthias Ringwald                 default:
407bcf00d8fSMatthias Ringwald                     break;
408bcf00d8fSMatthias Ringwald             }
409bcf00d8fSMatthias Ringwald             break;
410bcf00d8fSMatthias Ringwald 
411bcf00d8fSMatthias Ringwald         /* @text Ethernet packets from the remote device are received in the packet handler with type BNEP_DATA_PACKET.
412bcf00d8fSMatthias Ringwald          * It is forwarded to the TAP interface.
413bcf00d8fSMatthias Ringwald          */
414bcf00d8fSMatthias Ringwald         case BNEP_DATA_PACKET:
415a7473022SMatthias Ringwald             // Write out the ethernet frame to the network interface
416a7473022SMatthias Ringwald             btstack_network_process_packet(packet, size);
417bcf00d8fSMatthias Ringwald             break;
418bcf00d8fSMatthias Ringwald 
419bcf00d8fSMatthias Ringwald         default:
420bcf00d8fSMatthias Ringwald             break;
421bcf00d8fSMatthias Ringwald     }
422bcf00d8fSMatthias Ringwald }
423bcf00d8fSMatthias Ringwald /* LISTING_END */
424bcf00d8fSMatthias Ringwald 
425a7473022SMatthias Ringwald /*
426a7473022SMatthias Ringwald  * @section Network packet handler
427a7473022SMatthias Ringwald  *
428a7473022SMatthias Ringwald  * @text A pointer to the network packet is stored and a BNEP_EVENT_CAN_SEND_NOW requested
429a7473022SMatthias Ringwald  */
430a7473022SMatthias Ringwald 
431a7473022SMatthias Ringwald /* LISTING_START(networkPacketHandler): Network Packet Handler */
network_send_packet_callback(const uint8_t * packet,uint16_t size)432a7473022SMatthias Ringwald static void network_send_packet_callback(const uint8_t * packet, uint16_t size){
433a7473022SMatthias Ringwald     network_buffer = packet;
434a7473022SMatthias Ringwald     network_buffer_len = size;
435a7473022SMatthias Ringwald     bnep_request_can_send_now_event(bnep_cid);
436a7473022SMatthias Ringwald }
437a7473022SMatthias Ringwald /* LISTING_END */
438bcf00d8fSMatthias Ringwald 
439bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
btstack_main(int argc,const char * argv[])440bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){
441bcf00d8fSMatthias Ringwald 
4426316c8edSMatthias Ringwald     (void)argc;
4436316c8edSMatthias Ringwald     (void)argv;
444c83b0215SMatthias Ringwald 
445bcf00d8fSMatthias Ringwald     panu_setup();
4467693aad0SMatthias Ringwald 
4477693aad0SMatthias Ringwald     // parse human readable Bluetooth address
4487693aad0SMatthias Ringwald     sscanf_bd_addr(remote_addr_string, remote_addr);
4497693aad0SMatthias Ringwald 
450cb3797fcSMatthias Ringwald     gap_discoverable_control(1);
451cb3797fcSMatthias Ringwald     gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO);
452cb3797fcSMatthias Ringwald #ifdef ENABLE_PANU_CLIENT
453cb3797fcSMatthias Ringwald     gap_set_local_name("PANU Client 00:00:00:00:00:00");
454cb3797fcSMatthias Ringwald #else
455cb3797fcSMatthias Ringwald     gap_set_local_name("NAP Server 00:00:00:00:00:00");
456cb3797fcSMatthias Ringwald #endif
457cb3797fcSMatthias Ringwald     gap_set_class_of_device(0x020300); // Service Class "Networking", Major Device Class "LAN / NAT"
458cb3797fcSMatthias Ringwald 
459bcf00d8fSMatthias Ringwald     // Turn on the device
460bcf00d8fSMatthias Ringwald     hci_power_control(HCI_POWER_ON);
461bcf00d8fSMatthias Ringwald     return 0;
462bcf00d8fSMatthias Ringwald }
463bcf00d8fSMatthias Ringwald 
464bcf00d8fSMatthias Ringwald /* EXAMPLE_END */
465bcf00d8fSMatthias Ringwald /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4 -*-  */
466bcf00d8fSMatthias Ringwald 
467