panu_demo.c 19 KB

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