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