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 BLUEKITCHEN 24 * GMBH 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): BNEP/PANU (Linux only) 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 provides a TAP network interface which you 53 * can configure yourself. 54 * 55 * To enable client mode, uncomment ENABLE_PANU_CLIENT below. 56 */ 57 58 #include <stdio.h> 59 60 #include "btstack_config.h" 61 #include "btstack.h" 62 63 // #define ENABLE_PANU_CLIENT 64 65 // network types 66 #define NETWORK_TYPE_IPv4 0x0800 67 #define NETWORK_TYPE_ARP 0x0806 68 #define NETWORK_TYPE_IPv6 0x86DD 69 70 static uint16_t bnep_cid = 0; 71 72 #ifdef ENABLE_PANU_CLIENT 73 74 static int record_id = -1; 75 76 static uint16_t bnep_version = 0; 77 static uint32_t bnep_remote_uuid = 0; 78 static uint16_t bnep_l2cap_psm = 0; 79 80 static uint16_t sdp_bnep_l2cap_psm = 0; 81 static uint16_t sdp_bnep_version = 0; 82 static uint32_t sdp_bnep_remote_uuid = 0; 83 84 static uint8_t attribute_value[1000]; 85 static const unsigned int attribute_value_buffer_size = sizeof(attribute_value); 86 #endif 87 88 // MBP 2016 89 static const char * remote_addr_string = "78:4F:43:8C:B2:5D"; 90 // Wiko Sunny static const char * remote_addr_string = "A0:4C:5B:0F:B2:42"; 91 92 static bd_addr_t remote_addr; 93 94 static btstack_packet_callback_registration_t hci_event_callback_registration; 95 96 // outgoing network packet 97 static const uint8_t * network_buffer; 98 static uint16_t network_buffer_len; 99 100 static uint8_t panu_sdp_record[220]; 101 102 /* @section Main application configuration 103 * 104 * @text In the application configuration, L2CAP and BNEP are initialized and a BNEP service, for server mode, 105 * is registered, before the Bluetooth stack gets started, as shown in Listing PanuSetup. 106 */ 107 108 /* LISTING_START(PanuSetup): Panu setup */ 109 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 110 #ifdef ENABLE_PANU_CLIENT 111 static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 112 #endif 113 static void network_send_packet_callback(const uint8_t * packet, uint16_t size); 114 115 static void panu_setup(void){ 116 117 // Initialize L2CAP 118 l2cap_init(); 119 120 #ifdef ENABLE_BLE 121 // Initialize LE Security Manager. Needed for cross-transport key derivation 122 sm_init(); 123 #endif 124 125 // init SDP, create record for PANU and register with SDP 126 sdp_init(); 127 memset(panu_sdp_record, 0, sizeof(panu_sdp_record)); 128 uint16_t network_packet_types[] = { NETWORK_TYPE_IPv4, NETWORK_TYPE_ARP, 0}; // 0 as end of list 129 130 // Initialise BNEP 131 bnep_init(); 132 // Minimum L2CAP MTU for bnep is 1691 bytes 133 #ifdef ENABLE_PANU_CLIENT 134 bnep_register_service(packet_handler, BLUETOOTH_SERVICE_CLASS_PANU, 1691); 135 // PANU 136 pan_create_panu_sdp_record(panu_sdp_record, sdp_create_service_record_handle(), network_packet_types, NULL, NULL, BNEP_SECURITY_NONE); 137 #else 138 bnep_register_service(packet_handler, BLUETOOTH_SERVICE_CLASS_NAP, 1691); 139 // NAP Network Access Type: Other, 1 MB/s 140 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); 141 #endif 142 btstack_assert(de_get_len( panu_sdp_record) <= sizeof(panu_sdp_record)); 143 sdp_register_service(panu_sdp_record); 144 145 // Initialize network interface 146 btstack_network_init(&network_send_packet_callback); 147 148 // register for HCI events 149 hci_event_callback_registration.callback = &packet_handler; 150 hci_add_event_handler(&hci_event_callback_registration); 151 } 152 /* LISTING_END */ 153 154 #ifdef ENABLE_PANU_CLIENT 155 156 // PANU client routines 157 static void get_string_from_data_element(uint8_t * element, uint16_t buffer_size, char * buffer_data){ 158 de_size_t de_size = de_get_size_type(element); 159 int pos = de_get_header_size(element); 160 int len = 0; 161 switch (de_size){ 162 case DE_SIZE_VAR_8: 163 len = element[1]; 164 break; 165 case DE_SIZE_VAR_16: 166 len = big_endian_read_16(element, 1); 167 break; 168 default: 169 break; 170 } 171 uint16_t bytes_to_copy = btstack_min(buffer_size-1,len); 172 memcpy(buffer_data, &element[pos], bytes_to_copy); 173 buffer_data[bytes_to_copy] ='\0'; 174 } 175 176 177 /* @section SDP parser callback 178 * 179 * @text The SDP parsers retrieves the BNEP PAN UUID as explained in 180 * Section [on SDP BNEP Query example](#sec:sdpbnepqueryExample}. 181 */ 182 static void handle_sdp_client_record_complete(void){ 183 184 printf("SDP BNEP Record complete\n"); 185 186 // accept first entry or if we foudn a NAP and only have a PANU yet 187 if ((bnep_remote_uuid == 0) || (sdp_bnep_remote_uuid == BLUETOOTH_SERVICE_CLASS_NAP && bnep_remote_uuid == BLUETOOTH_SERVICE_CLASS_PANU)){ 188 bnep_l2cap_psm = sdp_bnep_l2cap_psm; 189 bnep_remote_uuid = sdp_bnep_remote_uuid; 190 bnep_version = sdp_bnep_version; 191 } 192 } 193 194 static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { 195 196 UNUSED(packet_type); 197 UNUSED(channel); 198 UNUSED(size); 199 200 des_iterator_t des_list_it; 201 des_iterator_t prot_it; 202 char str[20]; 203 204 switch (hci_event_packet_get_type(packet)){ 205 case SDP_EVENT_QUERY_ATTRIBUTE_VALUE: 206 // Handle new SDP record 207 if (sdp_event_query_attribute_byte_get_record_id(packet) != record_id) { 208 handle_sdp_client_record_complete(); 209 // next record started 210 record_id = sdp_event_query_attribute_byte_get_record_id(packet); 211 printf("SDP Record: Nr: %d\n", record_id); 212 } 213 214 if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= attribute_value_buffer_size) { 215 attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet); 216 217 if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) { 218 219 switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) { 220 case BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST: 221 if (de_get_element_type(attribute_value) != DE_DES) break; 222 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { 223 uint8_t * element = des_iterator_get_element(&des_list_it); 224 if (de_get_element_type(element) != DE_UUID) continue; 225 uint32_t uuid = de_get_uuid32(element); 226 switch (uuid){ 227 case BLUETOOTH_SERVICE_CLASS_PANU: 228 case BLUETOOTH_SERVICE_CLASS_NAP: 229 case BLUETOOTH_SERVICE_CLASS_GN: 230 printf("SDP Attribute 0x%04x: BNEP PAN protocol UUID: %04x\n", sdp_event_query_attribute_byte_get_attribute_id(packet), uuid); 231 sdp_bnep_remote_uuid = uuid; 232 break; 233 default: 234 break; 235 } 236 } 237 break; 238 case 0x0100: 239 case 0x0101: 240 get_string_from_data_element(attribute_value, sizeof(str), str); 241 printf("SDP Attribute: 0x%04x: %s\n", sdp_event_query_attribute_byte_get_attribute_id(packet), str); 242 break; 243 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST: { 244 printf("SDP Attribute: 0x%04x\n", sdp_event_query_attribute_byte_get_attribute_id(packet)); 245 246 for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { 247 uint8_t *des_element; 248 uint8_t *element; 249 uint32_t uuid; 250 251 if (des_iterator_get_type(&des_list_it) != DE_DES) continue; 252 253 des_element = des_iterator_get_element(&des_list_it); 254 des_iterator_init(&prot_it, des_element); 255 element = des_iterator_get_element(&prot_it); 256 257 if (!element) continue; 258 if (de_get_element_type(element) != DE_UUID) continue; 259 260 uuid = de_get_uuid32(element); 261 des_iterator_next(&prot_it); 262 switch (uuid){ 263 case BLUETOOTH_PROTOCOL_L2CAP: 264 if (!des_iterator_has_more(&prot_it)) continue; 265 de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_bnep_l2cap_psm); 266 break; 267 case BLUETOOTH_PROTOCOL_BNEP: 268 if (!des_iterator_has_more(&prot_it)) continue; 269 de_element_get_uint16(des_iterator_get_element(&prot_it), &sdp_bnep_version); 270 break; 271 default: 272 break; 273 } 274 } 275 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); 276 277 } 278 break; 279 default: 280 break; 281 } 282 } 283 } else { 284 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)); 285 } 286 break; 287 288 case SDP_EVENT_QUERY_COMPLETE: 289 handle_sdp_client_record_complete(); 290 fprintf(stderr, "Query complete, status 0%02x, bnep psm 0x%04x.\n", sdp_event_query_complete_get_status(packet), bnep_l2cap_psm); 291 if (bnep_l2cap_psm){ 292 /* Create BNEP connection */ 293 bnep_connect(packet_handler, remote_addr, bnep_l2cap_psm, BLUETOOTH_SERVICE_CLASS_PANU, bnep_remote_uuid); 294 } else { 295 fprintf(stderr, "No BNEP service found\n"); 296 } 297 298 break; 299 } 300 } 301 #endif 302 303 /* 304 * @section Packet Handler 305 * 306 * @text The packet handler responds to various HCI Events. 307 */ 308 309 /* LISTING_START(packetHandler): Packet Handler */ 310 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) 311 { 312 /* LISTING_PAUSE */ 313 UNUSED(channel); 314 315 uint8_t event; 316 bd_addr_t event_addr; 317 bd_addr_t local_addr; 318 uint16_t uuid_source; 319 uint16_t uuid_dest; 320 uint16_t mtu; 321 322 /* LISTING_RESUME */ 323 switch (packet_type) { 324 case HCI_EVENT_PACKET: 325 event = hci_event_packet_get_type(packet); 326 switch (event) { 327 #ifdef ENABLE_PANU_CLIENT 328 /* @text When BTSTACK_EVENT_STATE with state HCI_STATE_WORKING 329 * is received and the example is started in client mode, the remote SDP BNEP query is started. 330 */ 331 case BTSTACK_EVENT_STATE: 332 if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){ 333 printf("Start SDP BNEP query for remote PAN Network Access Point (NAP).\n"); 334 sdp_client_query_uuid16(&handle_sdp_client_query_result, remote_addr, BLUETOOTH_SERVICE_CLASS_NAP); 335 } 336 break; 337 #endif 338 /* LISTING_PAUSE */ 339 case HCI_EVENT_PIN_CODE_REQUEST: 340 // inform about pin code request 341 printf("Pin code request - using '0000'\n"); 342 hci_event_pin_code_request_get_bd_addr(packet, event_addr); 343 gap_pin_code_response(event_addr, "0000"); 344 break; 345 346 case HCI_EVENT_USER_CONFIRMATION_REQUEST: 347 // inform about user confirmation request 348 printf("SSP User Confirmation Request with numeric value '%06u'\n", little_endian_read_32(packet, 8)); 349 printf("SSP User Confirmation Auto accept\n"); 350 break; 351 352 /* LISTING_RESUME */ 353 354 /* @text BNEP_EVENT_CHANNEL_OPENED is received after a BNEP connection was established or 355 * or when the connection fails. The status field returns the error code. 356 * 357 * The TAP network interface is then configured. A data source is set up and registered with the 358 * run loop to receive Ethernet packets from the TAP interface. 359 * 360 * The event contains both the source and destination UUIDs, as well as the MTU for this connection and 361 * the BNEP Channel ID, which is used for sending Ethernet packets over BNEP. 362 */ 363 case BNEP_EVENT_CHANNEL_OPENED: 364 if (bnep_event_channel_opened_get_status(packet)) { 365 printf("BNEP channel open failed, status 0x%02x\n", bnep_event_channel_opened_get_status(packet)); 366 } else { 367 bnep_cid = bnep_event_channel_opened_get_bnep_cid(packet); 368 uuid_source = bnep_event_channel_opened_get_source_uuid(packet); 369 uuid_dest = bnep_event_channel_opened_get_destination_uuid(packet); 370 mtu = bnep_event_channel_opened_get_mtu(packet); 371 bnep_event_channel_opened_get_remote_address(packet, event_addr); 372 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); 373 374 /* Setup network interface */ 375 gap_local_bd_addr(local_addr); 376 btstack_network_up(local_addr); 377 printf("Network Interface %s activated\n", btstack_network_get_name()); 378 } 379 break; 380 381 /* @text If there is a timeout during the connection setup, BNEP_EVENT_CHANNEL_TIMEOUT will be received 382 * and the BNEP connection will be closed 383 */ 384 case BNEP_EVENT_CHANNEL_TIMEOUT: 385 printf("BNEP channel timeout! Channel will be closed\n"); 386 break; 387 388 /* @text BNEP_EVENT_CHANNEL_CLOSED is received when the connection gets closed. 389 */ 390 case BNEP_EVENT_CHANNEL_CLOSED: 391 printf("BNEP channel closed\n"); 392 btstack_network_down(); 393 break; 394 395 /* @text BNEP_EVENT_CAN_SEND_NOW indicates that a new packet can be send. This triggers the send of a 396 * stored network packet. The tap datas source can be enabled again 397 */ 398 case BNEP_EVENT_CAN_SEND_NOW: 399 if (network_buffer_len > 0) { 400 bnep_send(bnep_cid, (uint8_t*) network_buffer, network_buffer_len); 401 network_buffer_len = 0; 402 btstack_network_packet_sent(); 403 } 404 break; 405 406 default: 407 break; 408 } 409 break; 410 411 /* @text Ethernet packets from the remote device are received in the packet handler with type BNEP_DATA_PACKET. 412 * It is forwarded to the TAP interface. 413 */ 414 case BNEP_DATA_PACKET: 415 // Write out the ethernet frame to the network interface 416 btstack_network_process_packet(packet, size); 417 break; 418 419 default: 420 break; 421 } 422 } 423 /* LISTING_END */ 424 425 /* 426 * @section Network packet handler 427 * 428 * @text A pointer to the network packet is stored and a BNEP_EVENT_CAN_SEND_NOW requested 429 */ 430 431 /* LISTING_START(networkPacketHandler): Network Packet Handler */ 432 static void network_send_packet_callback(const uint8_t * packet, uint16_t size){ 433 network_buffer = packet; 434 network_buffer_len = size; 435 bnep_request_can_send_now_event(bnep_cid); 436 } 437 /* LISTING_END */ 438 439 int btstack_main(int argc, const char * argv[]); 440 int btstack_main(int argc, const char * argv[]){ 441 442 (void)argc; 443 (void)argv; 444 445 panu_setup(); 446 447 // parse human readable Bluetooth address 448 sscanf_bd_addr(remote_addr_string, remote_addr); 449 450 gap_discoverable_control(1); 451 gap_ssp_set_io_capability(SSP_IO_CAPABILITY_DISPLAY_YES_NO); 452 #ifdef ENABLE_PANU_CLIENT 453 gap_set_local_name("PANU Client 00:00:00:00:00:00"); 454 #else 455 gap_set_local_name("NAP Server 00:00:00:00:00:00"); 456 #endif 457 gap_set_class_of_device(0x020300); // Service Class "Networking", Major Device Class "LAN / NAT" 458 459 // Turn on the device 460 hci_power_control(HCI_POWER_ON); 461 return 0; 462 } 463 464 /* EXAMPLE_END */ 465 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4 -*- */ 466 467