xref: /btstack/example/sdp_bnep_query.c (revision ab2c6ae4b737d5e801d3defe4117331eb244ebb7)
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  */
37*ab2c6ae4SMatthias Ringwald 
38*ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "sdp_bnep_query.c"
39bcf00d8fSMatthias Ringwald 
40bcf00d8fSMatthias Ringwald // *****************************************************************************
41bcf00d8fSMatthias Ringwald /* EXAMPLE_START(sdp_bnep_query): Dump remote BNEP PAN protocol UUID and L2CAP PSM
42bcf00d8fSMatthias Ringwald  *
43bcf00d8fSMatthias Ringwald  * @text The example shows how the SDP Client is used to get all BNEP service
44bcf00d8fSMatthias Ringwald  * records from a remote device. It extracts the remote BNEP PAN protocol
45bcf00d8fSMatthias Ringwald  * UUID and the L2CAP PSM, which are needed to connect to a remote BNEP service.
46bcf00d8fSMatthias Ringwald  */
47bcf00d8fSMatthias Ringwald // *****************************************************************************
48bcf00d8fSMatthias Ringwald 
49bcf00d8fSMatthias Ringwald #include "btstack_config.h"
50bcf00d8fSMatthias Ringwald 
51bcf00d8fSMatthias Ringwald #include <stdint.h>
52bcf00d8fSMatthias Ringwald #include <stdio.h>
53bcf00d8fSMatthias Ringwald #include <stdlib.h>
54bcf00d8fSMatthias Ringwald #include <string.h>
55bcf00d8fSMatthias Ringwald 
56bcf00d8fSMatthias Ringwald #include "btstack_event.h"
57bcf00d8fSMatthias Ringwald #include "btstack_memory.h"
58bcf00d8fSMatthias Ringwald #include "btstack_run_loop.h"
59bcf00d8fSMatthias Ringwald #include "classic/sdp_client.h"
60bcf00d8fSMatthias Ringwald #include "classic/sdp_util.h"
61bcf00d8fSMatthias Ringwald #include "hci.h"
62bcf00d8fSMatthias Ringwald #include "hci_cmd.h"
63bcf00d8fSMatthias Ringwald #include "hci_dump.h"
64bcf00d8fSMatthias Ringwald #include "l2cap.h"
65bcf00d8fSMatthias Ringwald #include "pan.h"
66bcf00d8fSMatthias Ringwald 
67bcf00d8fSMatthias Ringwald int record_id = -1;
68bcf00d8fSMatthias Ringwald int attribute_id = -1;
69bcf00d8fSMatthias Ringwald 
70bcf00d8fSMatthias Ringwald static uint8_t   attribute_value[1000];
71bcf00d8fSMatthias Ringwald static const int attribute_value_buffer_size = sizeof(attribute_value);
72bcf00d8fSMatthias Ringwald 
73bcf00d8fSMatthias Ringwald static bd_addr_t remote = {0x04,0x0C,0xCE,0xE4,0x85,0xD3};
74bcf00d8fSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
75bcf00d8fSMatthias Ringwald 
76bcf00d8fSMatthias Ringwald static void assertBuffer(int size){
77bcf00d8fSMatthias Ringwald     if (size > attribute_value_buffer_size){
78bcf00d8fSMatthias Ringwald         printf("SDP attribute value buffer size exceeded: available %d, required %d", attribute_value_buffer_size, size);
79bcf00d8fSMatthias Ringwald     }
80bcf00d8fSMatthias Ringwald }
81bcf00d8fSMatthias Ringwald 
82bcf00d8fSMatthias Ringwald /* @section SDP Client Setup
83bcf00d8fSMatthias Ringwald  *
84bcf00d8fSMatthias Ringwald  * @text As with the previous example, you must register a
85bcf00d8fSMatthias Ringwald  * callback, i.e. query handler, with the SPD parser, as shown in
86bcf00d8fSMatthias Ringwald  * Listing SDPClientInit. Via this handler, the SDP client will receive events:
87bcf00d8fSMatthias Ringwald  * - SDP_EVENT_QUERY_ATTRIBUTE_VALUE containing the results of the query in chunks,
88bcf00d8fSMatthias Ringwald  * - SDP_EVENT_QUERY_COMPLETE reporting the status and the end of the query.
89bcf00d8fSMatthias Ringwald  */
90bcf00d8fSMatthias Ringwald 
91bcf00d8fSMatthias Ringwald /* LISTING_START(SDPClientInit): SDP client setup */
92bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
93bcf00d8fSMatthias Ringwald static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
94bcf00d8fSMatthias Ringwald 
95bcf00d8fSMatthias Ringwald static void sdp_client_init(void){
96bcf00d8fSMatthias Ringwald 
97bcf00d8fSMatthias Ringwald     // register for HCI events
98bcf00d8fSMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
99bcf00d8fSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
100bcf00d8fSMatthias Ringwald 
101bcf00d8fSMatthias Ringwald     // init L2CAP
102bcf00d8fSMatthias Ringwald     l2cap_init();
103bcf00d8fSMatthias Ringwald }
104bcf00d8fSMatthias Ringwald /* LISTING_END */
105bcf00d8fSMatthias Ringwald 
106bcf00d8fSMatthias Ringwald 
107bcf00d8fSMatthias Ringwald /* @section SDP Client Query
108bcf00d8fSMatthias Ringwald  * To trigger an SDP query to get the a list of service records on a remote device,
109bcf00d8fSMatthias Ringwald  * you need to call sdp_client_query_uuid16() with the remote address and the
110bcf00d8fSMatthias Ringwald  * BNEP protocol UUID, as shown in Listing SDPQueryUUID.
111bcf00d8fSMatthias Ringwald  * In this example we again used fixed address of the remote device. Please update
112bcf00d8fSMatthias Ringwald  * for your environment.
113bcf00d8fSMatthias Ringwald  */
114bcf00d8fSMatthias Ringwald 
115bcf00d8fSMatthias Ringwald /* LISTING_START(SDPQueryUUID): Querying the a list of service records on a remote device. */
116bcf00d8fSMatthias Ringwald static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1179ec2630cSMatthias Ringwald     UNUSED(channel);
1189ec2630cSMatthias Ringwald     UNUSED(size);
1199ec2630cSMatthias Ringwald 
120bcf00d8fSMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
1210e2df43fSMatthias Ringwald     uint8_t event = hci_event_packet_get_type(packet);
122bcf00d8fSMatthias Ringwald 
123bcf00d8fSMatthias Ringwald     switch (event) {
124bcf00d8fSMatthias Ringwald         case BTSTACK_EVENT_STATE:
125bcf00d8fSMatthias Ringwald             // BTstack activated, get started
126fb42b6e5SMilanka Ringwald             if (btstack_event_state_get_state(packet) == HCI_STATE_WORKING){
127bcf00d8fSMatthias Ringwald                 printf("Start SDP BNEP query.\n");
128bcf00d8fSMatthias Ringwald                 sdp_client_query_uuid16(&handle_sdp_client_query_result, remote, SDP_BNEPProtocol);
129bcf00d8fSMatthias Ringwald             }
130bcf00d8fSMatthias Ringwald             break;
131bcf00d8fSMatthias Ringwald         default:
132bcf00d8fSMatthias Ringwald             break;
133bcf00d8fSMatthias Ringwald     }
134bcf00d8fSMatthias Ringwald }
135bcf00d8fSMatthias Ringwald /* LISTING_END */
136bcf00d8fSMatthias Ringwald 
137bcf00d8fSMatthias Ringwald static char * get_string_from_data_element(uint8_t * element){
138bcf00d8fSMatthias Ringwald     de_size_t de_size = de_get_size_type(element);
139bcf00d8fSMatthias Ringwald     int pos     = de_get_header_size(element);
140bcf00d8fSMatthias Ringwald     int len = 0;
141bcf00d8fSMatthias Ringwald     switch (de_size){
142bcf00d8fSMatthias Ringwald         case DE_SIZE_VAR_8:
143bcf00d8fSMatthias Ringwald             len = element[1];
144bcf00d8fSMatthias Ringwald             break;
145bcf00d8fSMatthias Ringwald         case DE_SIZE_VAR_16:
146bcf00d8fSMatthias Ringwald             len = big_endian_read_16(element, 1);
147bcf00d8fSMatthias Ringwald             break;
148bcf00d8fSMatthias Ringwald         default:
149bcf00d8fSMatthias Ringwald             break;
150bcf00d8fSMatthias Ringwald     }
151bcf00d8fSMatthias Ringwald     char * str = (char*)malloc(len+1);
152bcf00d8fSMatthias Ringwald     memcpy(str, &element[pos], len);
153bcf00d8fSMatthias Ringwald     str[len] ='\0';
154bcf00d8fSMatthias Ringwald     return str;
155bcf00d8fSMatthias Ringwald }
156bcf00d8fSMatthias Ringwald 
157bcf00d8fSMatthias Ringwald 
158bcf00d8fSMatthias Ringwald /* @section Handling SDP Client Query Result
159bcf00d8fSMatthias Ringwald  *
160bcf00d8fSMatthias Ringwald  * @text The SDP Client returns the result of the query in chunks. Each result
161bcf00d8fSMatthias Ringwald  * packet contains the record ID, the Attribute ID, and a chunk of the Attribute
162bcf00d8fSMatthias Ringwald  * value, see Listing HandleSDPQUeryResult. Here, we show how to parse the
163bcf00d8fSMatthias Ringwald  * Service Class ID List and Protocol Descriptor List, as they contain
164bcf00d8fSMatthias Ringwald  * the BNEP Protocol UUID and L2CAP PSM respectively.
165bcf00d8fSMatthias Ringwald  */
166bcf00d8fSMatthias Ringwald 
167bcf00d8fSMatthias Ringwald /* LISTING_START(HandleSDPQUeryResult): Extracting BNEP Protcol UUID and L2CAP PSM */
168bcf00d8fSMatthias Ringwald static void handle_sdp_client_query_result(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1699ec2630cSMatthias Ringwald     UNUSED(packet_type);
1709ec2630cSMatthias Ringwald     UNUSED(channel);
1719ec2630cSMatthias Ringwald     UNUSED(size);
1729ec2630cSMatthias Ringwald 
173bcf00d8fSMatthias Ringwald     /* LISTING_PAUSE */
174bcf00d8fSMatthias Ringwald     des_iterator_t des_list_it;
175bcf00d8fSMatthias Ringwald     des_iterator_t prot_it;
176bcf00d8fSMatthias Ringwald     char *str;
177bcf00d8fSMatthias Ringwald 
1780e2df43fSMatthias Ringwald     switch (hci_event_packet_get_type(packet)){
179bcf00d8fSMatthias Ringwald         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
180bcf00d8fSMatthias Ringwald             // handle new record
181bcf00d8fSMatthias Ringwald             if (sdp_event_query_attribute_byte_get_record_id(packet) != record_id){
182bcf00d8fSMatthias Ringwald                 record_id = sdp_event_query_attribute_byte_get_record_id(packet);
183bcf00d8fSMatthias Ringwald                 printf("\n---\nRecord nr. %u\n", record_id);
184bcf00d8fSMatthias Ringwald             }
185bcf00d8fSMatthias Ringwald 
186bcf00d8fSMatthias Ringwald             assertBuffer(sdp_event_query_attribute_byte_get_attribute_length(packet));
187bcf00d8fSMatthias Ringwald 
188bcf00d8fSMatthias Ringwald             attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
189bcf00d8fSMatthias Ringwald             if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)){
190bcf00d8fSMatthias Ringwald 
191bcf00d8fSMatthias Ringwald                 /* LISTING_RESUME */
192bcf00d8fSMatthias Ringwald                 /* @text The Service Class ID List is a Data Element Sequence (DES) of UUIDs.
193bcf00d8fSMatthias Ringwald                  * The BNEP PAN protocol UUID is within this list.
194bcf00d8fSMatthias Ringwald                  */
195bcf00d8fSMatthias Ringwald 
196bcf00d8fSMatthias Ringwald                 switch(sdp_event_query_attribute_byte_get_attribute_id(packet)){
197bcf00d8fSMatthias Ringwald                     // 0x0001 "Service Class ID List"
198bcf00d8fSMatthias Ringwald                     case SDP_ServiceClassIDList:
199bcf00d8fSMatthias Ringwald                         if (de_get_element_type(attribute_value) != DE_DES) break;
200bcf00d8fSMatthias Ringwald                         for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)){
201bcf00d8fSMatthias Ringwald                             uint8_t * element = des_iterator_get_element(&des_list_it);
202bcf00d8fSMatthias Ringwald                             if (de_get_element_type(element) != DE_UUID) continue;
203bcf00d8fSMatthias Ringwald                             uint32_t uuid = de_get_uuid32(element);
204bcf00d8fSMatthias Ringwald                             switch (uuid){
205bcf00d8fSMatthias Ringwald                                 case PANU_UUID:
206bcf00d8fSMatthias Ringwald                                 case NAP_UUID:
207bcf00d8fSMatthias Ringwald                                 case GN_UUID:
208bcf00d8fSMatthias Ringwald                                     printf(" ** Attribute 0x%04x: BNEP PAN protocol UUID: %04x\n", sdp_event_query_attribute_byte_get_attribute_id(packet), uuid);
209bcf00d8fSMatthias Ringwald                                     break;
210bcf00d8fSMatthias Ringwald                                 default:
211bcf00d8fSMatthias Ringwald                                     break;
212bcf00d8fSMatthias Ringwald                             }
213bcf00d8fSMatthias Ringwald                         }
214bcf00d8fSMatthias Ringwald                         break;
215bcf00d8fSMatthias Ringwald                     /* LISTING_PAUSE */
216bcf00d8fSMatthias Ringwald                     // 0x0100 "Service Name"
217bcf00d8fSMatthias Ringwald                     case 0x0100:
218bcf00d8fSMatthias Ringwald                     // 0x0101 "Service Description"
219bcf00d8fSMatthias Ringwald                     case 0x0101:
220bcf00d8fSMatthias Ringwald                         str = get_string_from_data_element(attribute_value);
221bcf00d8fSMatthias Ringwald                         printf(" ** Attribute 0x%04x: %s\n", sdp_event_query_attribute_byte_get_attribute_id(packet), str);
222bcf00d8fSMatthias Ringwald                         free(str);
223bcf00d8fSMatthias Ringwald                         break;
224bcf00d8fSMatthias Ringwald 
225bcf00d8fSMatthias Ringwald                     /* LISTING_RESUME */
226bcf00d8fSMatthias Ringwald                     /* @text The Protocol Descriptor List is DES
227bcf00d8fSMatthias Ringwald                      * which contains one DES for each protocol. For PAN serivces, it contains
228bcf00d8fSMatthias Ringwald                      * a DES with the L2CAP Protocol UUID and a PSM,
229bcf00d8fSMatthias Ringwald                      * and another DES with the BNEP UUID and the the BNEP version.
230bcf00d8fSMatthias Ringwald                      */
231bcf00d8fSMatthias Ringwald                     case SDP_ProtocolDescriptorList:{
232bcf00d8fSMatthias Ringwald                             printf(" ** Attribute 0x%04x: ", sdp_event_query_attribute_byte_get_attribute_id(packet));
233bcf00d8fSMatthias Ringwald 
234bcf00d8fSMatthias Ringwald                             uint16_t l2cap_psm = 0;
235bcf00d8fSMatthias Ringwald                             uint16_t bnep_version = 0;
236bcf00d8fSMatthias Ringwald                             for (des_iterator_init(&des_list_it, attribute_value); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)){
237bcf00d8fSMatthias Ringwald                                 if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
238bcf00d8fSMatthias Ringwald                                 uint8_t * des_element = des_iterator_get_element(&des_list_it);
239bcf00d8fSMatthias Ringwald                                 des_iterator_init(&prot_it, des_element);
240bcf00d8fSMatthias Ringwald                                 uint8_t * element = des_iterator_get_element(&prot_it);
241bcf00d8fSMatthias Ringwald 
242bcf00d8fSMatthias Ringwald                                 if (de_get_element_type(element) != DE_UUID) continue;
243bcf00d8fSMatthias Ringwald                                 uint32_t uuid = de_get_uuid32(element);
244bcf00d8fSMatthias Ringwald                                 switch (uuid){
245bcf00d8fSMatthias Ringwald                                     case SDP_L2CAPProtocol:
246bcf00d8fSMatthias Ringwald                                         if (!des_iterator_has_more(&prot_it)) continue;
247bcf00d8fSMatthias Ringwald                                         des_iterator_next(&prot_it);
248bcf00d8fSMatthias Ringwald                                         de_element_get_uint16(des_iterator_get_element(&prot_it), &l2cap_psm);
249bcf00d8fSMatthias Ringwald                                         break;
250bcf00d8fSMatthias Ringwald                                     case SDP_BNEPProtocol:
251bcf00d8fSMatthias Ringwald                                         if (!des_iterator_has_more(&prot_it)) continue;
252bcf00d8fSMatthias Ringwald                                         des_iterator_next(&prot_it);
253bcf00d8fSMatthias Ringwald                                         de_element_get_uint16(des_iterator_get_element(&prot_it), &bnep_version);
254bcf00d8fSMatthias Ringwald                                         break;
255bcf00d8fSMatthias Ringwald                                     default:
256bcf00d8fSMatthias Ringwald                                         break;
257bcf00d8fSMatthias Ringwald                                 }
258bcf00d8fSMatthias Ringwald                             }
259bcf00d8fSMatthias Ringwald                             printf("l2cap_psm 0x%04x, bnep_version 0x%04x\n", l2cap_psm, bnep_version);
260bcf00d8fSMatthias Ringwald                         }
261bcf00d8fSMatthias Ringwald                         break;
262bcf00d8fSMatthias Ringwald                     /* LISTING_PAUSE */
263bcf00d8fSMatthias Ringwald                     default:
264bcf00d8fSMatthias Ringwald                         break;
265bcf00d8fSMatthias Ringwald                 }
266bcf00d8fSMatthias Ringwald             }
267bcf00d8fSMatthias Ringwald             break;
268bcf00d8fSMatthias Ringwald         case SDP_EVENT_QUERY_COMPLETE:
269bcf00d8fSMatthias Ringwald             printf("General query done with status %d.\n\n", sdp_event_query_complete_get_status(packet));
270bcf00d8fSMatthias Ringwald             break;
271bcf00d8fSMatthias Ringwald     }
272bcf00d8fSMatthias Ringwald     /* LISTING_RESUME */
273bcf00d8fSMatthias Ringwald }
274bcf00d8fSMatthias Ringwald /* LISTING_END */
275bcf00d8fSMatthias Ringwald 
276bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]);
277bcf00d8fSMatthias Ringwald int btstack_main(int argc, const char * argv[]){
2789ec2630cSMatthias Ringwald     (void)argc;
2799ec2630cSMatthias Ringwald     (void)argv;
2809ec2630cSMatthias Ringwald 
281bcf00d8fSMatthias Ringwald     printf("Client HCI init done\r\n");
282bcf00d8fSMatthias Ringwald 
283bcf00d8fSMatthias Ringwald     sdp_client_init();
284bcf00d8fSMatthias Ringwald 
285bcf00d8fSMatthias Ringwald     // turn on!
286bcf00d8fSMatthias Ringwald     hci_power_control(HCI_POWER_ON);
287bcf00d8fSMatthias Ringwald 
288bcf00d8fSMatthias Ringwald     return 0;
289bcf00d8fSMatthias Ringwald }
290bcf00d8fSMatthias Ringwald 
291bcf00d8fSMatthias Ringwald /* EXAMPLE_END */