sdp_bnep_query.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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__ "sdp_bnep_query.c"
  38. // *****************************************************************************
  39. /* EXAMPLE_START(sdp_bnep_query): Dump remote BNEP PAN protocol UUID and L2CAP PSM
  40. *
  41. * @text The example shows how the SDP Client is used to get all BNEP service
  42. * records from a remote device. It extracts the remote BNEP PAN protocol
  43. * UUID and the L2CAP PSM, which are needed to connect to a remote BNEP service.
  44. */
  45. // *****************************************************************************
  46. #include "btstack_config.h"
  47. #include <stdint.h>
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include "btstack.h"
  52. int record_id = -1;
  53. int attribute_id = -1;
  54. static uint8_t attribute_value[1000];
  55. static const int attribute_value_buffer_size = sizeof(attribute_value);
  56. static bd_addr_t remote = {0x04,0x0C,0xCE,0xE4,0x85,0xD3};
  57. static btstack_packet_callback_registration_t hci_event_callback_registration;
  58. static void assertBuffer(int size){
  59. if (size > attribute_value_buffer_size){
  60. printf("SDP attribute value buffer size exceeded: available %d, required %d", attribute_value_buffer_size, size);
  61. }
  62. }
  63. /* @section SDP Client Setup
  64. *
  65. * @text As with the previous example, you must register a
  66. * callback, i.e. query handler, with the SPD parser, as shown in
  67. * Listing SDPClientInit. Via this handler, the SDP client will receive events:
  68. * - SDP_EVENT_QUERY_ATTRIBUTE_VALUE containing the results of the query in chunks,
  69. * - SDP_EVENT_QUERY_COMPLETE reporting the status and the end of the query.
  70. */
  71. /* LISTING_START(SDPClientInit): SDP client setup */
  72. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
  73. static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
  74. static void sdp_client_init(void){
  75. // init L2CAP
  76. l2cap_init();
  77. // register for HCI events
  78. hci_event_callback_registration.callback = &packet_handler;
  79. hci_add_event_handler(&hci_event_callback_registration);
  80. }
  81. /* LISTING_END */
  82. /* @section SDP Client Query
  83. * To trigger an SDP query to get the a list of service records on a remote device,
  84. * you need to call sdp_client_query_uuid16() with the remote address and the
  85. * BNEP protocol UUID, as shown in Listing SDPQueryUUID.
  86. * In this example we again used fixed address of the remote device. Please update
  87. * for your environment.
  88. */
  89. /* LISTING_START(SDPQueryUUID): Querying the a list of service records on a remote device. */
  90. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  91. UNUSED(channel);
  92. UNUSED(size);
  93. if (packet_type != HCI_EVENT_PACKET) return;
  94. uint8_t event = hci_event_packet_get_type(packet);
  95. switch (event) {
  96. case BTSTACK_EVENT_STATE:
  97. // BTstack activated, get started
  98. if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
  99. printf("Start SDP BNEP query.\n");
  100. sdp_client_query_uuid16(&handle_sdp_client_query_result, remote, BLUETOOTH_PROTOCOL_BNEP);
  101. }
  102. break;
  103. default:
  104. break;
  105. }
  106. }
  107. /* LISTING_END */
  108. static void get_string_from_data_element(uint8_t * element, uint16_t buffer_size, char * buffer_data){
  109. de_size_t de_size = de_get_size_type(element);
  110. int pos = de_get_header_size(element);
  111. int len = 0;
  112. switch (de_size){
  113. case DE_SIZE_VAR_8:
  114. len = element[1];
  115. break;
  116. case DE_SIZE_VAR_16:
  117. len = big_endian_read_16(element, 1);
  118. break;
  119. default:
  120. break;
  121. }
  122. uint16_t bytes_to_copy = btstack_min(buffer_size-1,len);
  123. memcpy(buffer_data, &element[pos], bytes_to_copy);
  124. buffer_data[bytes_to_copy] ='\0';
  125. }
  126. /* @section Handling SDP Client Query Result
  127. *
  128. * @text The SDP Client returns the result of the query in chunks. Each result
  129. * packet contains the record ID, the Attribute ID, and a chunk of the Attribute
  130. * value, see Listing HandleSDPQUeryResult. Here, we show how to parse the
  131. * Service Class ID List and Protocol Descriptor List, as they contain
  132. * the BNEP Protocol UUID and L2CAP PSM respectively.
  133. */
  134. /* LISTING_START(HandleSDPQUeryResult): Extracting BNEP Protcol UUID and L2CAP PSM */
  135. static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  136. UNUSED(packet_type);
  137. UNUSED(channel);
  138. UNUSED(size);
  139. /* LISTING_PAUSE */
  140. des_iterator_t des_list_it;
  141. des_iterator_t prot_it;
  142. char str[20];
  143. switch (hci_event_packet_get_type(packet)){
  144. case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
  145. // handle new record
  146. if (sdp_event_query_attribute_byte_get_record_id(packet) != record_id){
  147. record_id = sdp_event_query_attribute_byte_get_record_id(packet);
  148. printf("\n---\nRecord nr. %u\n", record_id);
  149. }
  150. assertBuffer(sdp_event_query_attribute_byte_get_attribute_length(packet));
  151. attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
  152. if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)){
  153. /* LISTING_RESUME */
  154. /* @text The Service Class ID List is a Data Element Sequence (DES) of UUIDs.
  155. * The BNEP PAN protocol UUID is within this list.
  156. */
  157. switch(sdp_event_query_attribute_byte_get_attribute_id(packet)){
  158. // 0x0001 "Service Class ID List"
  159. case BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST:
  160. if (de_get_element_type(attribute_value) != DE_DES) break;
  161. for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)){
  162. uint8_t * element = des_iterator_get_element(&des_list_it);
  163. if (de_get_element_type(element) != DE_UUID) continue;
  164. uint32_t uuid = de_get_uuid32(element);
  165. switch (uuid){
  166. case BLUETOOTH_SERVICE_CLASS_PANU:
  167. case BLUETOOTH_SERVICE_CLASS_NAP:
  168. case BLUETOOTH_SERVICE_CLASS_GN:
  169. printf(" ** Attribute 0x%04x: BNEP PAN protocol UUID: %04x\n", sdp_event_query_attribute_byte_get_attribute_id(packet), (int) uuid);
  170. break;
  171. default:
  172. break;
  173. }
  174. }
  175. break;
  176. /* LISTING_PAUSE */
  177. // 0x0100 "Service Name"
  178. case 0x0100:
  179. // 0x0101 "Service Description"
  180. case 0x0101:
  181. get_string_from_data_element(attribute_value, sizeof(str), str);
  182. printf(" ** Attribute 0x%04x: %s\n", sdp_event_query_attribute_byte_get_attribute_id(packet), str);
  183. break;
  184. /* LISTING_RESUME */
  185. /* @text The Protocol Descriptor List is DES
  186. * which contains one DES for each protocol. For PAN serivces, it contains
  187. * a DES with the L2CAP Protocol UUID and a PSM,
  188. * and another DES with the BNEP UUID and the the BNEP version.
  189. */
  190. case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST:{
  191. printf(" ** Attribute 0x%04x: ", sdp_event_query_attribute_byte_get_attribute_id(packet));
  192. uint16_t l2cap_psm = 0;
  193. uint16_t bnep_version = 0;
  194. for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)){
  195. if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
  196. uint8_t * des_element = des_iterator_get_element(&des_list_it);
  197. des_iterator_init(&prot_it, des_element);
  198. uint8_t * element = des_iterator_get_element(&prot_it);
  199. if (de_get_element_type(element) != DE_UUID) continue;
  200. uint32_t uuid = de_get_uuid32(element);
  201. des_iterator_next(&prot_it);
  202. switch (uuid){
  203. case BLUETOOTH_PROTOCOL_L2CAP:
  204. if (!des_iterator_has_more(&prot_it)) continue;
  205. de_element_get_uint16(des_iterator_get_element(&prot_it), &l2cap_psm);
  206. break;
  207. case BLUETOOTH_PROTOCOL_BNEP:
  208. if (!des_iterator_has_more(&prot_it)) continue;
  209. de_element_get_uint16(des_iterator_get_element(&prot_it), &bnep_version);
  210. break;
  211. default:
  212. break;
  213. }
  214. }
  215. printf("l2cap_psm 0x%04x, bnep_version 0x%04x\n", l2cap_psm, bnep_version);
  216. }
  217. break;
  218. /* LISTING_PAUSE */
  219. default:
  220. break;
  221. }
  222. }
  223. break;
  224. case SDP_EVENT_QUERY_COMPLETE:
  225. if (sdp_event_query_complete_get_status(packet)){
  226. printf("SDP query failed 0x%02x\n", sdp_event_query_complete_get_status(packet));
  227. break;
  228. }
  229. printf("SDP query done.\n");
  230. break;
  231. }
  232. /* LISTING_RESUME */
  233. }
  234. /* LISTING_END */
  235. int btstack_main(int argc, const char * argv[]);
  236. int btstack_main(int argc, const char * argv[]){
  237. (void)argc;
  238. (void)argv;
  239. printf("Client HCI init done\r\n");
  240. sdp_client_init();
  241. // turn on!
  242. hci_power_control(HCI_POWER_ON);
  243. return 0;
  244. }
  245. /* EXAMPLE_END */