xref: /btstack/example/panu_demo.c (revision a4fe6467953bdb173fdf96a604f6527ed88f81c3)
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
23bcf00d8fSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24bcf00d8fSMatthias Ringwald  * RINGWALD 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 
38ab2c6ae4SMatthias 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 
45bcf00d8fSMatthias Ringwald /* EXAMPLE_START(panu_demo): PANU Demo
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  *
5258d7a529SMilanka Ringwald  * Note: currently supported only on Linux and Mac.
53bcf00d8fSMatthias Ringwald  */
54bcf00d8fSMatthias Ringwald 
55bcf00d8fSMatthias Ringwald #include <stdio.h>
56bcf00d8fSMatthias Ringwald 
57a7473022SMatthias Ringwald #include "btstack_config.h"
584d2bcd2aSMatthias Ringwald #include "btstack.h"
59bcf00d8fSMatthias Ringwald 
60bcf00d8fSMatthias Ringwald static int record_id = -1;
61bcf00d8fSMatthias Ringwald static uint16_t bnep_l2cap_psm      = 0;
62bcf00d8fSMatthias Ringwald static uint32_t bnep_remote_uuid    = 0;
63bcf00d8fSMatthias Ringwald static uint16_t bnep_version        = 0;
64bcf00d8fSMatthias Ringwald static uint16_t bnep_cid            = 0;
65bcf00d8fSMatthias Ringwald 
66b5a442a7SMatthias Ringwald static uint16_t sdp_bnep_l2cap_psm      = 0;
67b5a442a7SMatthias Ringwald static uint16_t sdp_bnep_version        = 0;
68b5a442a7SMatthias Ringwald static uint32_t sdp_bnep_remote_uuid    = 0;
69b5a442a7SMatthias Ringwald 
70bcf00d8fSMatthias Ringwald static uint8_t   attribute_value[1000];
71bcf00d8fSMatthias Ringwald static const unsigned int attribute_value_buffer_size = sizeof(attribute_value);
72bcf00d8fSMatthias Ringwald 
73a7473022SMatthias Ringwald // MBP 2016
74a7473022SMatthias Ringwald static const char * remote_addr_string = "F4-0F-24-3B-1B-E1";
75a7473022SMatthias Ringwald // Wiko Sunny static const char * remote_addr_string = "A0:4C:5B:0F:B2:42";
76a7473022SMatthias Ringwald 
777693aad0SMatthias Ringwald static bd_addr_t remote_addr;
78bcf00d8fSMatthias Ringwald 
79bcf00d8fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
80bcf00d8fSMatthias Ringwald 
81a7473022SMatthias Ringwald // outgoing network packet
82a7473022SMatthias Ringwald static const uint8_t * network_buffer;
83a7473022SMatthias Ringwald static uint16_t network_buffer_len;
84a7473022SMatthias Ringwald 
85bcf00d8fSMatthias Ringwald /* @section Main application configuration
86bcf00d8fSMatthias Ringwald  *
87bcf00d8fSMatthias Ringwald  * @text In the application configuration, L2CAP and BNEP are initialized and a BNEP service, for server mode,
88bcf00d8fSMatthias Ringwald  * is registered, before the Bluetooth stack gets started, as shown in Listing PanuSetup.
89bcf00d8fSMatthias Ringwald  */
90bcf00d8fSMatthias Ringwald 
91bcf00d8fSMatthias Ringwald /* LISTING_START(PanuSetup): Panu setup */
92bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
93423c667cSMatthias Ringwald static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
94a7473022SMatthias Ringwald static void network_send_packet_callback(const uint8_t * packet, uint16_t size);
95bcf00d8fSMatthias Ringwald 
96bcf00d8fSMatthias Ringwald static void panu_setup(void){
97bcf00d8fSMatthias Ringwald 
98bcf00d8fSMatthias Ringwald     // Initialize L2CAP
99bcf00d8fSMatthias Ringwald     l2cap_init();
100bcf00d8fSMatthias Ringwald 
101bcf00d8fSMatthias Ringwald     // Initialise BNEP
102bcf00d8fSMatthias Ringwald     bnep_init();
103a7473022SMatthias Ringwald 
104bcf00d8fSMatthias Ringwald     // Minimum L2CAP MTU for bnep is 1691 bytes
105235946f1SMatthias Ringwald     bnep_register_service(packet_handler, BLUETOOTH_SERVICE_CLASS_PANU, 1691);
106bcf00d8fSMatthias Ringwald 
107a7473022SMatthias Ringwald     // Initialize network interface
108a7473022SMatthias Ringwald     btstack_network_init(&network_send_packet_callback);
109*a4fe6467SMatthias Ringwald 
110*a4fe6467SMatthias Ringwald     // register for HCI events
111*a4fe6467SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
112*a4fe6467SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
113bcf00d8fSMatthias Ringwald }
114bcf00d8fSMatthias Ringwald /* LISTING_END */
115bcf00d8fSMatthias Ringwald 
116bcf00d8fSMatthias Ringwald // PANU client routines
117bcf00d8fSMatthias Ringwald static char * get_string_from_data_element(uint8_t * element){
118bcf00d8fSMatthias Ringwald     de_size_t de_size = de_get_size_type(element);
119bcf00d8fSMatthias Ringwald     int pos     = de_get_header_size(element);
120bcf00d8fSMatthias Ringwald     int len = 0;
121bcf00d8fSMatthias Ringwald     switch (de_size){
122bcf00d8fSMatthias Ringwald         case DE_SIZE_VAR_8:
123bcf00d8fSMatthias Ringwald             len = element[1];
124bcf00d8fSMatthias Ringwald             break;
125bcf00d8fSMatthias Ringwald         case DE_SIZE_VAR_16:
126bcf00d8fSMatthias Ringwald             len = big_endian_read_16(element, 1);
127bcf00d8fSMatthias Ringwald             break;
128bcf00d8fSMatthias Ringwald         default:
129bcf00d8fSMatthias Ringwald             break;
130bcf00d8fSMatthias Ringwald     }
131bcf00d8fSMatthias Ringwald     char * str = (char*)malloc(len+1);
132bcf00d8fSMatthias Ringwald     memcpy(str, &element[pos], len);
133bcf00d8fSMatthias Ringwald     str[len] ='\0';
134bcf00d8fSMatthias Ringwald     return str;
135bcf00d8fSMatthias Ringwald }
136bcf00d8fSMatthias Ringwald 
137bcf00d8fSMatthias Ringwald 
138bcf00d8fSMatthias Ringwald /* @section SDP parser callback
139bcf00d8fSMatthias Ringwald  *
140bcf00d8fSMatthias Ringwald  * @text The SDP parsers retrieves the BNEP PAN UUID as explained in
141bcf00d8fSMatthias Ringwald  * Section [on SDP BNEP Query example](#sec:sdpbnepqueryExample}.
142bcf00d8fSMatthias Ringwald  */
143b5a442a7SMatthias Ringwald static void handle_sdp_client_record_complete(void){
144b5a442a7SMatthias Ringwald 
145b5a442a7SMatthias Ringwald     printf("SDP BNEP Record complete\n");
146b5a442a7SMatthias Ringwald 
147b5a442a7SMatthias Ringwald     // accept first entry or if we foudn a NAP and only have a PANU yet
148b5a442a7SMatthias Ringwald     if ((bnep_remote_uuid == 0) || (sdp_bnep_remote_uuid == BLUETOOTH_SERVICE_CLASS_NAP && bnep_remote_uuid == BLUETOOTH_SERVICE_CLASS_PANU)){
149b5a442a7SMatthias Ringwald         bnep_l2cap_psm   = sdp_bnep_l2cap_psm;
150b5a442a7SMatthias Ringwald         bnep_remote_uuid = sdp_bnep_remote_uuid;
151b5a442a7SMatthias Ringwald         bnep_version     = sdp_bnep_version;
152b5a442a7SMatthias Ringwald     }
153b5a442a7SMatthias Ringwald }
154b5a442a7SMatthias Ringwald 
155423c667cSMatthias Ringwald static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
156bcf00d8fSMatthias Ringwald 
157c83b0215SMatthias Ringwald     UNUSED(packet_type);
158c83b0215SMatthias Ringwald     UNUSED(channel);
159c83b0215SMatthias Ringwald     UNUSED(size);
160c83b0215SMatthias Ringwald 
161bcf00d8fSMatthias Ringwald     des_iterator_t des_list_it;
162bcf00d8fSMatthias Ringwald     des_iterator_t prot_it;
163bcf00d8fSMatthias Ringwald     char *str;
164bcf00d8fSMatthias Ringwald 
1650e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
166bcf00d8fSMatthias Ringwald         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
167bcf00d8fSMatthias Ringwald             // Handle new SDP record
168bcf00d8fSMatthias Ringwald             if (sdp_event_query_attribute_byte_get_record_id(packet) != record_id) {
169b5a442a7SMatthias Ringwald                 handle_sdp_client_record_complete();
170b5a442a7SMatthias Ringwald                 // next record started
171bcf00d8fSMatthias Ringwald                 record_id = sdp_event_query_attribute_byte_get_record_id(packet);
172bcf00d8fSMatthias Ringwald                 printf("SDP Record: Nr: %d\n", record_id);
173bcf00d8fSMatthias Ringwald             }
174bcf00d8fSMatthias Ringwald 
175bcf00d8fSMatthias Ringwald             if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) {
176bcf00d8fSMatthias Ringwald                 attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
177bcf00d8fSMatthias Ringwald 
178bcf00d8fSMatthias Ringwald                 if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) {
179bcf00d8fSMatthias Ringwald 
180bcf00d8fSMatthias Ringwald                     switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) {
181235946f1SMatthias Ringwald                         case BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST:
182bcf00d8fSMatthias Ringwald                             if (de_get_element_type(attribute_value) != DE_DES) break;
183bcf00d8fSMatthias Ringwald                             for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
184bcf00d8fSMatthias Ringwald                                 uint8_t * element = des_iterator_get_element(&des_list_it);
185bcf00d8fSMatthias Ringwald                                 if (de_get_element_type(element) != DE_UUID) continue;
186bcf00d8fSMatthias Ringwald                                 uint32_t uuid = de_get_uuid32(element);
187bcf00d8fSMatthias Ringwald                                 switch (uuid){
188235946f1SMatthias Ringwald                                     case BLUETOOTH_SERVICE_CLASS_PANU:
189235946f1SMatthias Ringwald                                     case BLUETOOTH_SERVICE_CLASS_NAP:
190235946f1SMatthias Ringwald                                     case BLUETOOTH_SERVICE_CLASS_GN:
191bcf00d8fSMatthias Ringwald                                         printf("SDP Attribute 0x%04x: BNEP PAN protocol UUID: %04x\n", sdp_event_query_attribute_byte_get_attribute_id(packet), uuid);
192b5a442a7SMatthias Ringwald                                         sdp_bnep_remote_uuid = uuid;
193bcf00d8fSMatthias Ringwald                                         break;
194bcf00d8fSMatthias Ringwald                                     default:
195bcf00d8fSMatthias Ringwald                                         break;
196bcf00d8fSMatthias Ringwald                                 }
197bcf00d8fSMatthias Ringwald                             }
198bcf00d8fSMatthias Ringwald                             break;
199bcf00d8fSMatthias Ringwald                         case 0x0100:
200bcf00d8fSMatthias Ringwald                         case 0x0101:
201bcf00d8fSMatthias Ringwald                             str = get_string_from_data_element(attribute_value);
202bcf00d8fSMatthias Ringwald                             printf("SDP Attribute: 0x%04x: %s\n", sdp_event_query_attribute_byte_get_attribute_id(packet), str);
203bcf00d8fSMatthias Ringwald                             free(str);
204bcf00d8fSMatthias Ringwald                             break;
205235946f1SMatthias Ringwald                         case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: {
206bcf00d8fSMatthias Ringwald                                 printf("SDP Attribute: 0x%04x\n", sdp_event_query_attribute_byte_get_attribute_id(packet));
207bcf00d8fSMatthias Ringwald 
208bcf00d8fSMatthias Ringwald                                 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
209bcf00d8fSMatthias Ringwald                                     uint8_t       *des_element;
210bcf00d8fSMatthias Ringwald                                     uint8_t       *element;
211bcf00d8fSMatthias Ringwald                                     uint32_t       uuid;
212bcf00d8fSMatthias Ringwald 
213bcf00d8fSMatthias Ringwald                                     if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
214bcf00d8fSMatthias Ringwald 
215bcf00d8fSMatthias Ringwald                                     des_element = des_iterator_get_element(&des_list_it);
216bcf00d8fSMatthias Ringwald                                     des_iterator_init(&prot_it, des_element);
217bcf00d8fSMatthias Ringwald                                     element = des_iterator_get_element(&prot_it);
218bcf00d8fSMatthias Ringwald 
219bcf00d8fSMatthias Ringwald                                     if (de_get_element_type(element) != DE_UUID) continue;
220bcf00d8fSMatthias Ringwald 
221bcf00d8fSMatthias Ringwald                                     uuid = de_get_uuid32(element);
222bcf00d8fSMatthias Ringwald                                     switch (uuid){
223235946f1SMatthias Ringwald                                         case BLUETOOTH_PROTOCOL_L2CAP:
224bcf00d8fSMatthias Ringwald                                             if (!des_iterator_has_more(&prot_it)) continue;
225bcf00d8fSMatthias Ringwald                                             des_iterator_next(&prot_it);
226b5a442a7SMatthias Ringwald                                             de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_bnep_l2cap_psm);
227bcf00d8fSMatthias Ringwald                                             break;
228235946f1SMatthias Ringwald                                         case BLUETOOTH_PROTOCOL_BNEP:
229bcf00d8fSMatthias Ringwald                                             if (!des_iterator_has_more(&prot_it)) continue;
230bcf00d8fSMatthias Ringwald                                             des_iterator_next(&prot_it);
231b5a442a7SMatthias Ringwald                                             de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_bnep_version);
232bcf00d8fSMatthias Ringwald                                             break;
233bcf00d8fSMatthias Ringwald                                         default:
234bcf00d8fSMatthias Ringwald                                             break;
235bcf00d8fSMatthias Ringwald                                     }
236bcf00d8fSMatthias Ringwald                                 }
237e0f9c2f9SMatthias 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);
238bcf00d8fSMatthias Ringwald 
239bcf00d8fSMatthias Ringwald                             }
240bcf00d8fSMatthias Ringwald                             break;
241bcf00d8fSMatthias Ringwald                         default:
242bcf00d8fSMatthias Ringwald                             break;
243bcf00d8fSMatthias Ringwald                     }
244bcf00d8fSMatthias Ringwald                 }
245bcf00d8fSMatthias Ringwald             } else {
246bcf00d8fSMatthias 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));
247bcf00d8fSMatthias Ringwald             }
248bcf00d8fSMatthias Ringwald             break;
249bcf00d8fSMatthias Ringwald 
250bcf00d8fSMatthias Ringwald         case SDP_EVENT_QUERY_COMPLETE:
251b5a442a7SMatthias Ringwald             handle_sdp_client_record_complete();
2527693aad0SMatthias Ringwald             fprintf(stderr, "General query done with status %d, bnep psm %04x.\n", sdp_event_query_complete_get_status(packet), bnep_l2cap_psm);
2537693aad0SMatthias Ringwald             if (bnep_l2cap_psm){
2547693aad0SMatthias Ringwald                 /* Create BNEP connection */
2557693aad0SMatthias Ringwald                 bnep_connect(packet_handler, remote_addr, bnep_l2cap_psm, BLUETOOTH_SERVICE_CLASS_PANU, bnep_remote_uuid);
2567693aad0SMatthias Ringwald             } else {
2577693aad0SMatthias Ringwald                 fprintf(stderr, "No BNEP service found\n");
2587693aad0SMatthias Ringwald             }
259bcf00d8fSMatthias Ringwald 
260bcf00d8fSMatthias Ringwald             break;
261bcf00d8fSMatthias Ringwald     }
262bcf00d8fSMatthias Ringwald }
263bcf00d8fSMatthias Ringwald 
264bcf00d8fSMatthias Ringwald /*
265bcf00d8fSMatthias Ringwald  * @section Packet Handler
266bcf00d8fSMatthias Ringwald  *
267bcf00d8fSMatthias Ringwald  * @text The packet handler responds to various HCI Events.
268bcf00d8fSMatthias Ringwald  */
269bcf00d8fSMatthias Ringwald 
270bcf00d8fSMatthias Ringwald /* LISTING_START(packetHandler): Packet Handler */
271bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)
272bcf00d8fSMatthias Ringwald {
273bcf00d8fSMatthias Ringwald /* LISTING_PAUSE */
274c83b0215SMatthias Ringwald     UNUSED(channel);
275c83b0215SMatthias Ringwald 
276bcf00d8fSMatthias Ringwald     uint8_t   event;
277bcf00d8fSMatthias Ringwald     bd_addr_t event_addr;
278bcf00d8fSMatthias Ringwald     bd_addr_t local_addr;
279bcf00d8fSMatthias Ringwald     uint16_t  uuid_source;
280bcf00d8fSMatthias Ringwald     uint16_t  uuid_dest;
281bcf00d8fSMatthias Ringwald     uint16_t  mtu;
282bcf00d8fSMatthias Ringwald 
283bcf00d8fSMatthias Ringwald     /* LISTING_RESUME */
284bcf00d8fSMatthias Ringwald     switch (packet_type) {
285bcf00d8fSMatthias Ringwald 		case HCI_EVENT_PACKET:
2860e2df43fSMatthias Ringwald             event = hci_event_packet_get_type(packet);
287bcf00d8fSMatthias Ringwald             switch (event) {
288bcf00d8fSMatthias Ringwald                 /* @text When BTSTACK_EVENT_STATE with state HCI_STATE_WORKING
289bcf00d8fSMatthias Ringwald                  * is received and the example is started in client mode, the remote SDP BNEP query is started.
290bcf00d8fSMatthias Ringwald                  */
291bcf00d8fSMatthias Ringwald                 case BTSTACK_EVENT_STATE:
292be7cc9a0SMilanka Ringwald                     if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
293e0f9c2f9SMatthias Ringwald                         printf("Start SDP BNEP query for remote PAN Network Access Point (NAP).\n");
294e0f9c2f9SMatthias Ringwald                         sdp_client_query_uuid16(&handle_sdp_client_query_result, remote_addr, BLUETOOTH_SERVICE_CLASS_NAP);
295bcf00d8fSMatthias Ringwald                     }
296bcf00d8fSMatthias Ringwald                     break;
297bcf00d8fSMatthias Ringwald 
298bcf00d8fSMatthias Ringwald                 /* LISTING_PAUSE */
299bcf00d8fSMatthias Ringwald                 case HCI_EVENT_PIN_CODE_REQUEST:
300bcf00d8fSMatthias Ringwald 					// inform about pin code request
301bcf00d8fSMatthias Ringwald                     printf("Pin code request - using '0000'\n");
302a6ef64baSMilanka Ringwald                     hci_event_pin_code_request_get_bd_addr(packet, event_addr);
3034d2bcd2aSMatthias Ringwald                     gap_pin_code_response(event_addr, "0000");
304bcf00d8fSMatthias Ringwald 					break;
305bcf00d8fSMatthias Ringwald 
306bcf00d8fSMatthias Ringwald                 case HCI_EVENT_USER_CONFIRMATION_REQUEST:
307bcf00d8fSMatthias Ringwald                     // inform about user confirmation request
308bcf00d8fSMatthias Ringwald                     printf("SSP User Confirmation Request with numeric value '%06u'\n", little_endian_read_32(packet, 8));
309bcf00d8fSMatthias Ringwald                     printf("SSP User Confirmation Auto accept\n");
310bcf00d8fSMatthias Ringwald                     break;
311bcf00d8fSMatthias Ringwald 
312bcf00d8fSMatthias Ringwald                 /* LISTING_RESUME */
313bcf00d8fSMatthias Ringwald 
314423c667cSMatthias Ringwald                 /* @text BNEP_EVENT_CHANNEL_OPENED is received after a BNEP connection was established or
315bcf00d8fSMatthias Ringwald                  * or when the connection fails. The status field returns the error code.
316bcf00d8fSMatthias Ringwald                  *
317bcf00d8fSMatthias Ringwald                  * The TAP network interface is then configured. A data source is set up and registered with the
318bcf00d8fSMatthias Ringwald                  * run loop to receive Ethernet packets from the TAP interface.
319bcf00d8fSMatthias Ringwald                  *
320bcf00d8fSMatthias Ringwald                  * The event contains both the source and destination UUIDs, as well as the MTU for this connection and
321bcf00d8fSMatthias Ringwald                  * the BNEP Channel ID, which is used for sending Ethernet packets over BNEP.
322bcf00d8fSMatthias Ringwald                  */
323423c667cSMatthias Ringwald 				case BNEP_EVENT_CHANNEL_OPENED:
324508c11d6SMilanka Ringwald                     if (bnep_event_channel_opened_get_status(packet)) {
325508c11d6SMilanka Ringwald                         printf("BNEP channel open failed, status %02x\n", bnep_event_channel_opened_get_status(packet));
326bcf00d8fSMatthias Ringwald                     } else {
327508c11d6SMilanka Ringwald                         bnep_cid    = bnep_event_channel_opened_get_bnep_cid(packet);
328508c11d6SMilanka Ringwald                         uuid_source = bnep_event_channel_opened_get_source_uuid(packet);
329508c11d6SMilanka Ringwald                         uuid_dest   = bnep_event_channel_opened_get_destination_uuid(packet);
330508c11d6SMilanka Ringwald                         mtu         = bnep_event_channel_opened_get_mtu(packet);
331423c667cSMatthias Ringwald                         memcpy(&event_addr, &packet[11], sizeof(bd_addr_t));
332bcf00d8fSMatthias 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);
333a7473022SMatthias Ringwald                         /* Setup network interface */
334bcf00d8fSMatthias Ringwald                         gap_local_bd_addr(local_addr);
335a7473022SMatthias Ringwald                         btstack_network_up(local_addr);
33684693d68SMatthias Ringwald                         printf("Network Interface %s activated\n", btstack_network_get_name());
337bcf00d8fSMatthias Ringwald                     }
338bcf00d8fSMatthias Ringwald 					break;
339bcf00d8fSMatthias Ringwald 
340bcf00d8fSMatthias Ringwald                 /* @text If there is a timeout during the connection setup, BNEP_EVENT_CHANNEL_TIMEOUT will be received
341bcf00d8fSMatthias Ringwald                  * and the BNEP connection  will be closed
342bcf00d8fSMatthias Ringwald                  */
343bcf00d8fSMatthias Ringwald                 case BNEP_EVENT_CHANNEL_TIMEOUT:
344bcf00d8fSMatthias Ringwald                     printf("BNEP channel timeout! Channel will be closed\n");
345bcf00d8fSMatthias Ringwald                     break;
346bcf00d8fSMatthias Ringwald 
347bcf00d8fSMatthias Ringwald                 /* @text BNEP_EVENT_CHANNEL_CLOSED is received when the connection gets closed.
348bcf00d8fSMatthias Ringwald                  */
349bcf00d8fSMatthias Ringwald                 case BNEP_EVENT_CHANNEL_CLOSED:
350bcf00d8fSMatthias Ringwald                     printf("BNEP channel closed\n");
351a7473022SMatthias Ringwald                     btstack_network_down();
352bcf00d8fSMatthias Ringwald                     break;
353bcf00d8fSMatthias Ringwald 
35436b4a12cSMatthias Ringwald                 /* @text BNEP_EVENT_CAN_SEND_NOW indicates that a new packet can be send. This triggers the send of a
35536b4a12cSMatthias Ringwald                  * stored network packet. The tap datas source can be enabled again
356bcf00d8fSMatthias Ringwald                  */
3572d4c8c04SMatthias Ringwald                 case BNEP_EVENT_CAN_SEND_NOW:
358bcf00d8fSMatthias Ringwald                     if (network_buffer_len > 0) {
359a7473022SMatthias Ringwald                         bnep_send(bnep_cid, (uint8_t*) network_buffer, network_buffer_len);
360bcf00d8fSMatthias Ringwald                         network_buffer_len = 0;
361a7473022SMatthias Ringwald                         btstack_network_packet_sent();
362bcf00d8fSMatthias Ringwald                     }
363bcf00d8fSMatthias Ringwald                     break;
364bcf00d8fSMatthias Ringwald 
365bcf00d8fSMatthias Ringwald                 default:
366bcf00d8fSMatthias Ringwald                     break;
367bcf00d8fSMatthias Ringwald             }
368bcf00d8fSMatthias Ringwald             break;
369bcf00d8fSMatthias Ringwald 
370bcf00d8fSMatthias Ringwald         /* @text Ethernet packets from the remote device are received in the packet handler with type BNEP_DATA_PACKET.
371bcf00d8fSMatthias Ringwald          * It is forwarded to the TAP interface.
372bcf00d8fSMatthias Ringwald          */
373bcf00d8fSMatthias Ringwald         case BNEP_DATA_PACKET:
374a7473022SMatthias Ringwald             // Write out the ethernet frame to the network interface
375a7473022SMatthias Ringwald             btstack_network_process_packet(packet, size);
376bcf00d8fSMatthias Ringwald             break;
377bcf00d8fSMatthias Ringwald 
378bcf00d8fSMatthias Ringwald         default:
379bcf00d8fSMatthias Ringwald             break;
380bcf00d8fSMatthias Ringwald     }
381bcf00d8fSMatthias Ringwald }
382bcf00d8fSMatthias Ringwald /* LISTING_END */
383bcf00d8fSMatthias Ringwald 
384a7473022SMatthias Ringwald /*
385a7473022SMatthias Ringwald  * @section Network packet handler
386a7473022SMatthias Ringwald  *
387a7473022SMatthias Ringwald  * @text A pointer to the network packet is stored and a BNEP_EVENT_CAN_SEND_NOW requested
388a7473022SMatthias Ringwald  */
389a7473022SMatthias Ringwald 
390a7473022SMatthias Ringwald /* LISTING_START(networkPacketHandler): Network Packet Handler */
391a7473022SMatthias Ringwald static void network_send_packet_callback(const uint8_t * packet, uint16_t size){
392a7473022SMatthias Ringwald     network_buffer = packet;
393a7473022SMatthias Ringwald     network_buffer_len = size;
394a7473022SMatthias Ringwald     bnep_request_can_send_now_event(bnep_cid);
395a7473022SMatthias Ringwald }
396a7473022SMatthias Ringwald /* LISTING_END */
397bcf00d8fSMatthias Ringwald 
398bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
399bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){
400bcf00d8fSMatthias Ringwald 
4016316c8edSMatthias Ringwald     (void)argc;
4026316c8edSMatthias Ringwald     (void)argv;
403c83b0215SMatthias Ringwald 
404bcf00d8fSMatthias Ringwald     panu_setup();
4057693aad0SMatthias Ringwald 
4067693aad0SMatthias Ringwald     // parse human readable Bluetooth address
4077693aad0SMatthias Ringwald     sscanf_bd_addr(remote_addr_string, remote_addr);
4087693aad0SMatthias Ringwald 
409bcf00d8fSMatthias Ringwald     // Turn on the device
410bcf00d8fSMatthias Ringwald     hci_power_control(HCI_POWER_ON);
411bcf00d8fSMatthias Ringwald     return 0;
412bcf00d8fSMatthias Ringwald }
413bcf00d8fSMatthias Ringwald 
414bcf00d8fSMatthias Ringwald /* EXAMPLE_END */
415bcf00d8fSMatthias Ringwald /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4 -*-  */
416bcf00d8fSMatthias Ringwald 
417