xref: /btstack/example/sdp_general_query.c (revision bcf00d8fa453bd3a5c441c3b99e6e4b685f0d8f0)
1*bcf00d8fSMatthias Ringwald /*
2*bcf00d8fSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3*bcf00d8fSMatthias Ringwald  *
4*bcf00d8fSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*bcf00d8fSMatthias Ringwald  * modification, are permitted provided that the following conditions
6*bcf00d8fSMatthias Ringwald  * are met:
7*bcf00d8fSMatthias Ringwald  *
8*bcf00d8fSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*bcf00d8fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*bcf00d8fSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*bcf00d8fSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*bcf00d8fSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*bcf00d8fSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*bcf00d8fSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*bcf00d8fSMatthias Ringwald  *    from this software without specific prior written permission.
16*bcf00d8fSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17*bcf00d8fSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18*bcf00d8fSMatthias Ringwald  *    monetary gain.
19*bcf00d8fSMatthias Ringwald  *
20*bcf00d8fSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21*bcf00d8fSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*bcf00d8fSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*bcf00d8fSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24*bcf00d8fSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*bcf00d8fSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*bcf00d8fSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27*bcf00d8fSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*bcf00d8fSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*bcf00d8fSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30*bcf00d8fSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*bcf00d8fSMatthias Ringwald  * SUCH DAMAGE.
32*bcf00d8fSMatthias Ringwald  *
33*bcf00d8fSMatthias Ringwald  * Please inquire about commercial licensing options at
34*bcf00d8fSMatthias Ringwald  * [email protected]
35*bcf00d8fSMatthias Ringwald  *
36*bcf00d8fSMatthias Ringwald  */
37*bcf00d8fSMatthias Ringwald 
38*bcf00d8fSMatthias Ringwald // *****************************************************************************
39*bcf00d8fSMatthias Ringwald /* EXAMPLE_START(sdp_general_query): Dump remote SDP Records
40*bcf00d8fSMatthias Ringwald  *
41*bcf00d8fSMatthias Ringwald  * @text The example shows how the SDP Client is used to get a list of
42*bcf00d8fSMatthias Ringwald  * service records on a remote device.
43*bcf00d8fSMatthias Ringwald  */
44*bcf00d8fSMatthias Ringwald // *****************************************************************************
45*bcf00d8fSMatthias Ringwald 
46*bcf00d8fSMatthias Ringwald #include "btstack_config.h"
47*bcf00d8fSMatthias Ringwald 
48*bcf00d8fSMatthias Ringwald #include <stdint.h>
49*bcf00d8fSMatthias Ringwald #include <stdio.h>
50*bcf00d8fSMatthias Ringwald #include <stdlib.h>
51*bcf00d8fSMatthias Ringwald #include <string.h>
52*bcf00d8fSMatthias Ringwald 
53*bcf00d8fSMatthias Ringwald #include "btstack_event.h"
54*bcf00d8fSMatthias Ringwald #include "btstack_memory.h"
55*bcf00d8fSMatthias Ringwald #include "btstack_run_loop.h"
56*bcf00d8fSMatthias Ringwald #include "classic/sdp_client.h"
57*bcf00d8fSMatthias Ringwald #include "classic/sdp_util.h"
58*bcf00d8fSMatthias Ringwald #include "hci.h"
59*bcf00d8fSMatthias Ringwald #include "hci_cmd.h"
60*bcf00d8fSMatthias Ringwald #include "hci_dump.h"
61*bcf00d8fSMatthias Ringwald #include "l2cap.h"
62*bcf00d8fSMatthias Ringwald 
63*bcf00d8fSMatthias Ringwald int record_id = -1;
64*bcf00d8fSMatthias Ringwald int attribute_id = -1;
65*bcf00d8fSMatthias Ringwald 
66*bcf00d8fSMatthias Ringwald static uint8_t   attribute_value[1000];
67*bcf00d8fSMatthias Ringwald static const int attribute_value_buffer_size = sizeof(attribute_value);
68*bcf00d8fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
69*bcf00d8fSMatthias Ringwald 
70*bcf00d8fSMatthias Ringwald /* @section SDP Client Setup
71*bcf00d8fSMatthias Ringwald  *
72*bcf00d8fSMatthias Ringwald  * @text SDP is based on L2CAP. To receive SDP query events you must register a
73*bcf00d8fSMatthias Ringwald  * callback, i.e. query handler, with the SPD parser, as shown in
74*bcf00d8fSMatthias Ringwald  * Listing SDPClientInit. Via this handler, the SDP client will receive the following events:
75*bcf00d8fSMatthias Ringwald  * - SDP_EVENT_QUERY_ATTRIBUTE_VALUE containing the results of the query in chunks,
76*bcf00d8fSMatthias Ringwald  * - SDP_EVENT_QUERY_COMPLETE indicating the end of the query and the status
77*bcf00d8fSMatthias Ringwald  */
78*bcf00d8fSMatthias Ringwald 
79*bcf00d8fSMatthias Ringwald /* LISTING_START(SDPClientInit): SDP client setup */
80*bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
81*bcf00d8fSMatthias Ringwald static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
82*bcf00d8fSMatthias Ringwald 
83*bcf00d8fSMatthias Ringwald static void sdp_client_init(void){
84*bcf00d8fSMatthias Ringwald 
85*bcf00d8fSMatthias Ringwald     // register for HCI events
86*bcf00d8fSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
87*bcf00d8fSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
88*bcf00d8fSMatthias Ringwald 
89*bcf00d8fSMatthias Ringwald     // init L2CAP
90*bcf00d8fSMatthias Ringwald     l2cap_init();
91*bcf00d8fSMatthias Ringwald }
92*bcf00d8fSMatthias Ringwald /* LISTING_END */
93*bcf00d8fSMatthias Ringwald 
94*bcf00d8fSMatthias Ringwald /* @section SDP Client Query
95*bcf00d8fSMatthias Ringwald  *
96*bcf00d8fSMatthias Ringwald  * @text To trigger an SDP query to get the a list of service records on a remote device,
97*bcf00d8fSMatthias Ringwald  * you need to call sdp_client_query_uuid16() with the remote address and the
98*bcf00d8fSMatthias Ringwald  * UUID of the public browse group, as shown in Listing SDPQueryUUID.
99*bcf00d8fSMatthias Ringwald  * In this example we used fixed address of the remote device shown in Listing Remote.
100*bcf00d8fSMatthias Ringwald  * Please update it with the address of a device in your vicinity, e.g., one reported
101*bcf00d8fSMatthias Ringwald  * by the GAP Inquiry example in the previous section.
102*bcf00d8fSMatthias Ringwald  */
103*bcf00d8fSMatthias Ringwald 
104*bcf00d8fSMatthias Ringwald /* LISTING_START(Remote): Address of remote device in big-endian order */
105*bcf00d8fSMatthias Ringwald static bd_addr_t remote = {0x04,0x0C,0xCE,0xE4,0x85,0xD3};
106*bcf00d8fSMatthias Ringwald /* LISTING_END */
107*bcf00d8fSMatthias Ringwald 
108*bcf00d8fSMatthias Ringwald /* LISTING_START(SDPQueryUUID): Querying a list of service records on a remote device. */
109*bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
110*bcf00d8fSMatthias Ringwald     // printf("packet_handler type %u, packet[0] %x\n", packet_type, packet[0]);
111*bcf00d8fSMatthias Ringwald 
112*bcf00d8fSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
113*bcf00d8fSMatthias Ringwald     uint8_t event = packet[0];
114*bcf00d8fSMatthias Ringwald 
115*bcf00d8fSMatthias Ringwald     switch (event) {
116*bcf00d8fSMatthias Ringwald         case BTSTACK_EVENT_STATE:
117*bcf00d8fSMatthias Ringwald             if (packet[2] == HCI_STATE_WORKING){
118*bcf00d8fSMatthias Ringwald                 sdp_client_query_uuid16(&handle_sdp_client_query_result, remote, SDP_PublicBrowseGroup);
119*bcf00d8fSMatthias Ringwald             }
120*bcf00d8fSMatthias Ringwald             break;
121*bcf00d8fSMatthias Ringwald         default:
122*bcf00d8fSMatthias Ringwald             break;
123*bcf00d8fSMatthias Ringwald     }
124*bcf00d8fSMatthias Ringwald }
125*bcf00d8fSMatthias Ringwald /* LISTING_END */
126*bcf00d8fSMatthias Ringwald 
127*bcf00d8fSMatthias Ringwald static void assertBuffer(int size){
128*bcf00d8fSMatthias Ringwald     if (size > attribute_value_buffer_size){
129*bcf00d8fSMatthias Ringwald         printf("SDP attribute value buffer size exceeded: available %d, required %d", attribute_value_buffer_size, size);
130*bcf00d8fSMatthias Ringwald     }
131*bcf00d8fSMatthias Ringwald }
132*bcf00d8fSMatthias Ringwald 
133*bcf00d8fSMatthias Ringwald /* @section Handling SDP Client Query Results
134*bcf00d8fSMatthias Ringwald  *
135*bcf00d8fSMatthias Ringwald  * @text The SDP Client returns the results of the query in chunks. Each result
136*bcf00d8fSMatthias Ringwald  * packet contains the record ID, the Attribute ID, and a chunk of the Attribute
137*bcf00d8fSMatthias Ringwald  * value.
138*bcf00d8fSMatthias Ringwald  * In this example, we append new chunks for the same Attribute ID in a large buffer,
139*bcf00d8fSMatthias Ringwald  * see Listing HandleSDPQUeryResult.
140*bcf00d8fSMatthias Ringwald  *
141*bcf00d8fSMatthias Ringwald  * To save memory, it's also possible to process these chunks directly by a custom stream parser,
142*bcf00d8fSMatthias Ringwald  * similar to the way XML files are parsed by a SAX parser. Have a look at *src/sdp_query_rfcomm.c*
143*bcf00d8fSMatthias Ringwald  * which retrieves the RFCOMM channel number and the service name.
144*bcf00d8fSMatthias Ringwald  */
145*bcf00d8fSMatthias Ringwald 
146*bcf00d8fSMatthias Ringwald /* LISTING_START(HandleSDPQUeryResult): Handling query result chunks. */
147*bcf00d8fSMatthias Ringwald static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
148*bcf00d8fSMatthias Ringwald     switch (packet[0]){
149*bcf00d8fSMatthias Ringwald         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
150*bcf00d8fSMatthias Ringwald             // handle new record
151*bcf00d8fSMatthias Ringwald             if (sdp_event_query_attribute_byte_get_record_id(packet) != record_id){
152*bcf00d8fSMatthias Ringwald                 record_id = sdp_event_query_attribute_byte_get_record_id(packet);
153*bcf00d8fSMatthias Ringwald                 printf("\n---\nRecord nr. %u\n", record_id);
154*bcf00d8fSMatthias Ringwald             }
155*bcf00d8fSMatthias Ringwald 
156*bcf00d8fSMatthias Ringwald             assertBuffer(sdp_event_query_attribute_byte_get_attribute_length(packet));
157*bcf00d8fSMatthias Ringwald 
158*bcf00d8fSMatthias Ringwald             attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
159*bcf00d8fSMatthias Ringwald             if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)){
160*bcf00d8fSMatthias Ringwald                printf("Attribute 0x%04x: ", sdp_event_query_attribute_byte_get_attribute_id(packet));
161*bcf00d8fSMatthias Ringwald                de_dump_data_element(attribute_value);
162*bcf00d8fSMatthias Ringwald             }
163*bcf00d8fSMatthias Ringwald             break;
164*bcf00d8fSMatthias Ringwald         case SDP_EVENT_QUERY_COMPLETE:
165*bcf00d8fSMatthias Ringwald             printf("General query done with status %d.\n\n", sdp_event_query_complete_get_status(packet));
166*bcf00d8fSMatthias Ringwald             exit(0);
167*bcf00d8fSMatthias Ringwald             break;
168*bcf00d8fSMatthias Ringwald     }
169*bcf00d8fSMatthias Ringwald }
170*bcf00d8fSMatthias Ringwald /* LISTING_END */
171*bcf00d8fSMatthias Ringwald 
172*bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
173*bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){
174*bcf00d8fSMatthias Ringwald 
175*bcf00d8fSMatthias Ringwald     printf("Client HCI init done\r\n");
176*bcf00d8fSMatthias Ringwald 
177*bcf00d8fSMatthias Ringwald     sdp_client_init();
178*bcf00d8fSMatthias Ringwald 
179*bcf00d8fSMatthias Ringwald     // turn on!
180*bcf00d8fSMatthias Ringwald     hci_power_control(HCI_POWER_ON);
181*bcf00d8fSMatthias Ringwald 
182*bcf00d8fSMatthias Ringwald     return 0;
183*bcf00d8fSMatthias Ringwald }
184*bcf00d8fSMatthias Ringwald 
185*bcf00d8fSMatthias Ringwald /* EXAMPLE_END */
186