xref: /btstack/example/sdp_general_query.c (revision bcf00d8fa453bd3a5c441c3b99e6e4b685f0d8f0)
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  * [email protected]
35  *
36  */
37 
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 
46 #include "btstack_config.h"
47 
48 #include <stdint.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 
53 #include "btstack_event.h"
54 #include "btstack_memory.h"
55 #include "btstack_run_loop.h"
56 #include "classic/sdp_client.h"
57 #include "classic/sdp_util.h"
58 #include "hci.h"
59 #include "hci_cmd.h"
60 #include "hci_dump.h"
61 #include "l2cap.h"
62 
63 int record_id = -1;
64 int attribute_id = -1;
65 
66 static uint8_t   attribute_value[1000];
67 static const int attribute_value_buffer_size = sizeof(attribute_value);
68 static btstack_packet_callback_registration_t hci_event_callback_registration;
69 
70 /* @section SDP Client Setup
71  *
72  * @text SDP is based on L2CAP. To receive SDP query events you must register a
73  * callback, i.e. query handler, with the SPD parser, as shown in
74  * Listing SDPClientInit. Via this handler, the SDP client will receive the following events:
75  * - SDP_EVENT_QUERY_ATTRIBUTE_VALUE containing the results of the query in chunks,
76  * - SDP_EVENT_QUERY_COMPLETE indicating the end of the query and the status
77  */
78 
79 /* LISTING_START(SDPClientInit): SDP client setup */
80 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
81 static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
82 
83 static void sdp_client_init(void){
84 
85     // register for HCI events
86     hci_event_callback_registration.callback = &packet_handler;
87     hci_add_event_handler(&hci_event_callback_registration);
88 
89     // init L2CAP
90     l2cap_init();
91 }
92 /* LISTING_END */
93 
94 /* @section SDP Client Query
95  *
96  * @text To trigger an SDP query to get the a list of service records on a remote device,
97  * you need to call sdp_client_query_uuid16() with the remote address and the
98  * UUID of the public browse group, as shown in Listing SDPQueryUUID.
99  * In this example we used fixed address of the remote device shown in Listing Remote.
100  * Please update it with the address of a device in your vicinity, e.g., one reported
101  * by the GAP Inquiry example in the previous section.
102  */
103 
104 /* LISTING_START(Remote): Address of remote device in big-endian order */
105 static bd_addr_t remote = {0x04,0x0C,0xCE,0xE4,0x85,0xD3};
106 /* LISTING_END */
107 
108 /* LISTING_START(SDPQueryUUID): Querying a list of service records on a remote device. */
109 static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
110     // printf("packet_handler type %u, packet[0] %x\n", packet_type, packet[0]);
111 
112     if (packet_type != HCI_EVENT_PACKET) return;
113     uint8_t event = packet[0];
114 
115     switch (event) {
116         case BTSTACK_EVENT_STATE:
117             if (packet[2] == HCI_STATE_WORKING){
118                 sdp_client_query_uuid16(&handle_sdp_client_query_result, remote, SDP_PublicBrowseGroup);
119             }
120             break;
121         default:
122             break;
123     }
124 }
125 /* LISTING_END */
126 
127 static void assertBuffer(int size){
128     if (size > attribute_value_buffer_size){
129         printf("SDP attribute value buffer size exceeded: available %d, required %d", attribute_value_buffer_size, size);
130     }
131 }
132 
133 /* @section Handling SDP Client Query Results
134  *
135  * @text The SDP Client returns the results of the query in chunks. Each result
136  * packet contains the record ID, the Attribute ID, and a chunk of the Attribute
137  * value.
138  * In this example, we append new chunks for the same Attribute ID in a large buffer,
139  * see Listing HandleSDPQUeryResult.
140  *
141  * To save memory, it's also possible to process these chunks directly by a custom stream parser,
142  * similar to the way XML files are parsed by a SAX parser. Have a look at *src/sdp_query_rfcomm.c*
143  * which retrieves the RFCOMM channel number and the service name.
144  */
145 
146 /* LISTING_START(HandleSDPQUeryResult): Handling query result chunks. */
147 static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
148     switch (packet[0]){
149         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
150             // handle new record
151             if (sdp_event_query_attribute_byte_get_record_id(packet) != record_id){
152                 record_id = sdp_event_query_attribute_byte_get_record_id(packet);
153                 printf("\n---\nRecord nr. %u\n", record_id);
154             }
155 
156             assertBuffer(sdp_event_query_attribute_byte_get_attribute_length(packet));
157 
158             attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
159             if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)){
160                printf("Attribute 0x%04x: ", sdp_event_query_attribute_byte_get_attribute_id(packet));
161                de_dump_data_element(attribute_value);
162             }
163             break;
164         case SDP_EVENT_QUERY_COMPLETE:
165             printf("General query done with status %d.\n\n", sdp_event_query_complete_get_status(packet));
166             exit(0);
167             break;
168     }
169 }
170 /* LISTING_END */
171 
172 int btstack_main(int argc, const char * argv[]);
173 int btstack_main(int argc, const char * argv[]){
174 
175     printf("Client HCI init done\r\n");
176 
177     sdp_client_init();
178 
179     // turn on!
180     hci_power_control(HCI_POWER_ON);
181 
182     return 0;
183 }
184 
185 /* EXAMPLE_END */
186