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