sdp_general_query.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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_general_query.c"
  38. // *****************************************************************************
  39. /* EXAMPLE_START(sdp_general_query): Dump remote SDP Records
  40. *
  41. * @text The example shows how the SDP Client is used to get a list of
  42. * service records on a remote device.
  43. */
  44. // *****************************************************************************
  45. #include "btstack_config.h"
  46. #include <stdint.h>
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include "btstack.h"
  51. #ifndef HAVE_POSIX_FILE_IO
  52. // Jambox static const char * remote_addr_string = "00:21:3C:AC:F7:38";
  53. // Mac static const char * remote_addr_string = "F0:18:98:60:3E:E5";
  54. // iPhone 5S:
  55. static const char * remote_addr_string = "6C:72:E7:10:22:EE";
  56. #endif
  57. static bd_addr_t remote_addr;
  58. static int record_id = -1;
  59. static uint8_t attribute_value[1000];
  60. static const int attribute_value_buffer_size = sizeof(attribute_value);
  61. static btstack_packet_callback_registration_t hci_event_callback_registration;
  62. /* @section SDP Client Setup
  63. *
  64. * @text SDP is based on L2CAP. To receive SDP query events you must register a
  65. * callback, i.e. query handler, with the SPD parser, as shown in
  66. * Listing SDPClientInit. Via this handler, the SDP client will receive the following events:
  67. * - SDP_EVENT_QUERY_ATTRIBUTE_VALUE containing the results of the query in chunks,
  68. * - SDP_EVENT_QUERY_COMPLETE indicating the end of the query and the status
  69. */
  70. /* LISTING_START(SDPClientInit): SDP client setup */
  71. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
  72. static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
  73. static void sdp_client_init(void){
  74. // init L2CAP
  75. l2cap_init();
  76. // register for HCI events
  77. hci_event_callback_registration.callback = &packet_handler;
  78. hci_add_event_handler(&hci_event_callback_registration);
  79. }
  80. /* LISTING_END */
  81. /* @section SDP Client Query
  82. *
  83. * @text 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. * UUID of the public browse group, as shown in Listing SDPQueryUUID.
  86. * In this example we used fixed address of the remote device shown in Listing Remote.
  87. * Please update it with the address of a device in your vicinity, e.g., one reported
  88. * by the GAP Inquiry example in the previous section.
  89. */
  90. /* LISTING_START(SDPQueryUUID): Querying a list of service records on a remote device. */
  91. static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  92. UNUSED(channel);
  93. UNUSED(size);
  94. if (packet_type != HCI_EVENT_PACKET) return;
  95. uint8_t event = hci_event_packet_get_type(packet);
  96. switch (event) {
  97. case BTSTACK_EVENT_STATE:
  98. // BTstack activated, get started
  99. if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
  100. printf("Connecting to %s\n", bd_addr_to_str(remote_addr));
  101. sdp_client_query_uuid16(&handle_sdp_client_query_result, remote_addr, BLUETOOTH_PROTOCOL_L2CAP);
  102. }
  103. break;
  104. default:
  105. break;
  106. }
  107. }
  108. /* LISTING_END */
  109. static void assertBuffer(int size){
  110. if (size > attribute_value_buffer_size){
  111. printf("SDP attribute value buffer size exceeded: available %d, required %d", attribute_value_buffer_size, size);
  112. }
  113. }
  114. /* @section Handling SDP Client Query Results
  115. *
  116. * @text The SDP Client returns the results of the query in chunks. Each result
  117. * packet contains the record ID, the Attribute ID, and a chunk of the Attribute
  118. * value.
  119. * In this example, we append new chunks for the same Attribute ID in a large buffer,
  120. * see Listing HandleSDPQUeryResult.
  121. *
  122. * To save memory, it's also possible to process these chunks directly by a custom stream parser,
  123. * similar to the way XML files are parsed by a SAX parser. Have a look at *src/sdp_client_rfcomm.c*
  124. * which retrieves the RFCOMM channel number and the service name.
  125. */
  126. /* LISTING_START(HandleSDPQUeryResult): Handling query result chunks. */
  127. static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
  128. UNUSED(packet_type);
  129. UNUSED(channel);
  130. UNUSED(size);
  131. switch (packet[0]){
  132. case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
  133. // handle new record
  134. if (sdp_event_query_attribute_byte_get_record_id(packet) != record_id){
  135. record_id = sdp_event_query_attribute_byte_get_record_id(packet);
  136. printf("\n---\nRecord nr. %u\n", record_id);
  137. }
  138. assertBuffer(sdp_event_query_attribute_byte_get_attribute_length(packet));
  139. attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
  140. if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)){
  141. printf("Attribute 0x%04x: ", sdp_event_query_attribute_byte_get_attribute_id(packet));
  142. de_dump_data_element(attribute_value);
  143. }
  144. break;
  145. case SDP_EVENT_QUERY_COMPLETE:
  146. if (sdp_event_query_complete_get_status(packet)){
  147. printf("SDP query failed 0x%02x\n", sdp_event_query_complete_get_status(packet));
  148. break;
  149. }
  150. printf("SDP query done.\n");
  151. break;
  152. }
  153. }
  154. /* LISTING_END */
  155. #ifdef HAVE_POSIX_FILE_IO
  156. static void usage(const char *name){
  157. printf("\nUsage: %s -a|--address aa:bb:cc:dd:ee:ff\n", name);
  158. printf("Use argument -a to connect to a specific device and dump the result of SDP query for L2CAP services.\n\n");
  159. }
  160. #endif
  161. int btstack_main(int argc, const char * argv[]);
  162. int btstack_main(int argc, const char * argv[]){
  163. #ifdef HAVE_POSIX_FILE_IO
  164. int remote_addr_found = 0;
  165. if (argc == 3) {
  166. if(!strcmp(argv[1], "-a") || !strcmp(argv[1], "--address")){
  167. remote_addr_found = sscanf_bd_addr(argv[2], remote_addr);
  168. }
  169. }
  170. if (!remote_addr_found){
  171. usage(argv[0]);
  172. exit(1);
  173. }
  174. #else
  175. (void)argc;
  176. (void)argv;
  177. sscanf_bd_addr(remote_addr_string, remote_addr);
  178. #endif
  179. sdp_client_init();
  180. // turn on!
  181. hci_power_control(HCI_POWER_ON);
  182. return 0;
  183. }
  184. /* EXAMPLE_END */