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