xref: /btstack/src/classic/sdp_client.c (revision fffd8860e2b667c8b7e15b5d85b6c3ddcf305f26)
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  *  sdp_client.c
40  */
41 
42 #include "btstack_config.h"
43 #include "btstack_debug.h"
44 #include "btstack_event.h"
45 #include "classic/core.h"
46 #include "classic/sdp_client.h"
47 #include "classic/sdp_server.h"
48 #include "classic/sdp_util.h"
49 #include "hci_cmd.h"
50 #include "l2cap.h"
51 
52 // Types SDP Parser - Data Element stream helper
53 typedef enum {
54     GET_LIST_LENGTH = 1,
55     GET_RECORD_LENGTH,
56     GET_ATTRIBUTE_ID_HEADER_LENGTH,
57     GET_ATTRIBUTE_ID,
58     GET_ATTRIBUTE_VALUE_LENGTH,
59     GET_ATTRIBUTE_VALUE
60 } sdp_parser_state_t;
61 
62 // Types SDP Client
63 typedef enum {
64     INIT, W4_CONNECT, W2_SEND, W4_RESPONSE, QUERY_COMPLETE
65 } sdp_client_state_t;
66 
67 
68 // Prototypes SDP Parser
69 void sdp_parser_init(btstack_packet_handler_t callback);
70 void sdp_parser_handle_chunk(uint8_t * data, uint16_t size);
71 void sdp_parser_handle_done(uint8_t status);
72 void sdp_parser_init_service_attribute_search(void);
73 void sdp_parser_init_service_search(void);
74 void sdp_parser_handle_service_search(uint8_t * data, uint16_t total_count, uint16_t record_handle_count);
75 
76 // Prototypes SDP Client
77 void sdp_client_reset(void);
78 void sdp_client_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
79 static uint16_t sdp_client_setup_service_search_attribute_request(uint8_t * data);
80 #ifdef ENABLE_SDP_EXTRA_QUERIES
81 static uint16_t sdp_client_setup_service_search_request(uint8_t * data);
82 static uint16_t sdp_client_setup_service_attribute_request(uint8_t * data);
83 static void     sdp_client_parse_service_search_response(uint8_t* packet);
84 static void     sdp_client_parse_service_attribute_response(uint8_t* packet);
85 #endif
86 
87 static uint8_t des_attributeIDList[] = { 0x35, 0x05, 0x0A, 0x00, 0x01, 0xff, 0xff};  // Attribute: 0x0001 - 0x0100
88 
89 // State DES Parser
90 static de_state_t de_header_state;
91 
92 // State SDP Parser
93 static sdp_parser_state_t  state = GET_LIST_LENGTH;
94 static uint16_t attribute_id = 0;
95 static uint16_t attribute_bytes_received = 0;
96 static uint16_t attribute_bytes_delivered = 0;
97 static uint16_t list_offset = 0;
98 static uint16_t list_size;
99 static uint16_t record_offset = 0;
100 static uint16_t record_size;
101 static uint16_t attribute_value_size;
102 static int record_counter = 0;
103 static btstack_packet_handler_t sdp_parser_callback;
104 
105 // State SDP Client
106 static uint16_t  mtu;
107 static uint16_t  sdp_cid = 0x40;
108 static const uint8_t * service_search_pattern;
109 static const uint8_t * attribute_id_list;
110 static uint16_t  transactionID = 0;
111 static uint8_t   continuationState[16];
112 static uint8_t   continuationStateLen;
113 static sdp_client_state_t sdp_client_state = INIT;
114 static SDP_PDU_ID_t PDU_ID = SDP_Invalid;
115 #ifdef ENABLE_SDP_EXTRA_QUERIES
116 static uint32_t serviceRecordHandle;
117 static uint32_t record_handle;
118 #endif
119 
120 // DES Parser
121 void de_state_init(de_state_t * de_state){
122     de_state->in_state_GET_DE_HEADER_LENGTH = 1;
123     de_state->addon_header_bytes = 0;
124     de_state->de_size = 0;
125     de_state->de_offset = 0;
126 }
127 
128 int de_state_size(uint8_t eventByte, de_state_t *de_state){
129     if (de_state->in_state_GET_DE_HEADER_LENGTH){
130         de_state->addon_header_bytes = de_get_header_size(&eventByte) - 1;
131         de_state->de_size = 0;
132         de_state->de_offset = 0;
133 
134         if (de_state->addon_header_bytes == 0){
135             de_state->de_size = de_get_data_size(&eventByte);
136             if (de_state->de_size == 0) {
137                 log_error("  ERROR: ID size is zero");
138             }
139             // log_info("Data element payload is %d bytes.", de_state->de_size);
140             return 1;
141         }
142         de_state->in_state_GET_DE_HEADER_LENGTH = 0;
143         return 0;
144     }
145 
146     if (de_state->addon_header_bytes > 0){
147         de_state->de_size = (de_state->de_size << 8) | eventByte;
148         de_state->addon_header_bytes--;
149     }
150     if (de_state->addon_header_bytes > 0) return 0;
151     // log_info("Data element payload is %d bytes.", de_state->de_size);
152     de_state->in_state_GET_DE_HEADER_LENGTH = 1;
153     return 1;
154 }
155 
156 // SDP Parser
157 static void sdp_parser_emit_value_byte(uint8_t event_byte){
158     uint8_t event[11];
159     event[0] = SDP_EVENT_QUERY_ATTRIBUTE_VALUE;
160     event[1] = 9;
161     little_endian_store_16(event, 2, record_counter);
162     little_endian_store_16(event, 4, attribute_id);
163     little_endian_store_16(event, 6, attribute_value_size);
164     little_endian_store_16(event, 8, attribute_bytes_delivered);
165     event[10] = event_byte;
166     (*sdp_parser_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
167 }
168 
169 static void sdp_parser_process_byte(uint8_t eventByte){
170     // count all bytes
171     list_offset++;
172     record_offset++;
173 
174     // log_info(" parse BYTE_RECEIVED %02x", eventByte);
175     switch(state){
176         case GET_LIST_LENGTH:
177             if (!de_state_size(eventByte, &de_header_state)) break;
178             list_offset = de_header_state.de_offset;
179             list_size = de_header_state.de_size;
180             // log_info("parser: List offset %u, list size %u", list_offset, list_size);
181 
182             record_counter = 0;
183             state = GET_RECORD_LENGTH;
184             break;
185 
186         case GET_RECORD_LENGTH:
187             // check size
188             if (!de_state_size(eventByte, &de_header_state)) break;
189             // log_info("parser: Record payload is %d bytes.", de_header_state.de_size);
190             record_offset = de_header_state.de_offset;
191             record_size = de_header_state.de_size;
192             state = GET_ATTRIBUTE_ID_HEADER_LENGTH;
193             break;
194 
195         case GET_ATTRIBUTE_ID_HEADER_LENGTH:
196             if (!de_state_size(eventByte, &de_header_state)) break;
197             attribute_id = 0;
198             log_info("ID data is stored in %d bytes.", (int) de_header_state.de_size);
199             state = GET_ATTRIBUTE_ID;
200             break;
201 
202         case GET_ATTRIBUTE_ID:
203             attribute_id = (attribute_id << 8) | eventByte;
204             de_header_state.de_size--;
205             if (de_header_state.de_size > 0) break;
206             log_info("parser: Attribute ID: %04x.", attribute_id);
207 
208             state = GET_ATTRIBUTE_VALUE_LENGTH;
209             attribute_bytes_received  = 0;
210             attribute_bytes_delivered = 0;
211             attribute_value_size      = 0;
212             de_state_init(&de_header_state);
213             break;
214 
215         case GET_ATTRIBUTE_VALUE_LENGTH:
216             attribute_bytes_received++;
217             sdp_parser_emit_value_byte(eventByte);
218             attribute_bytes_delivered++;
219             if (!de_state_size(eventByte, &de_header_state)) break;
220 
221             attribute_value_size = de_header_state.de_size + attribute_bytes_received;
222 
223             state = GET_ATTRIBUTE_VALUE;
224             break;
225 
226         case GET_ATTRIBUTE_VALUE:
227             attribute_bytes_received++;
228             sdp_parser_emit_value_byte(eventByte);
229             attribute_bytes_delivered++;
230             // log_info("paser: attribute_bytes_received %u, attribute_value_size %u", attribute_bytes_received, attribute_value_size);
231 
232             if (attribute_bytes_received < attribute_value_size) break;
233             // log_info("parser: Record offset %u, record size %u", record_offset, record_size);
234             if (record_offset != record_size){
235                 state = GET_ATTRIBUTE_ID_HEADER_LENGTH;
236                 // log_info("Get next attribute");
237                 break;
238             }
239             record_offset = 0;
240             // log_info("parser: List offset %u, list size %u", list_offset, list_size);
241 
242             if (list_size > 0 && list_offset != list_size){
243                 record_counter++;
244                 state = GET_RECORD_LENGTH;
245                 log_info("parser: END_OF_RECORD");
246                 break;
247             }
248             list_offset = 0;
249             de_state_init(&de_header_state);
250             state = GET_LIST_LENGTH;
251             record_counter = 0;
252             log_info("parser: END_OF_RECORD & DONE");
253             break;
254         default:
255             break;
256     }
257 }
258 
259 void sdp_parser_init(btstack_packet_handler_t callback){
260     // init
261     sdp_parser_callback = callback;
262     de_state_init(&de_header_state);
263     state = GET_LIST_LENGTH;
264     list_offset = 0;
265     record_offset = 0;
266     record_counter = 0;
267 }
268 
269 void sdp_parser_handle_chunk(uint8_t * data, uint16_t size){
270     int i;
271     for (i=0;i<size;i++){
272         sdp_parser_process_byte(data[i]);
273     }
274 }
275 
276 #ifdef ENABLE_SDP_EXTRA_QUERIES
277 void sdp_parser_init_service_attribute_search(void){
278     // init
279     de_state_init(&de_header_state);
280     state = GET_RECORD_LENGTH;
281     list_offset = 0;
282     record_offset = 0;
283     record_counter = 0;
284 }
285 
286 void sdp_parser_init_service_search(void){
287     record_offset = 0;
288 }
289 
290 void sdp_parser_handle_service_search(uint8_t * data, uint16_t total_count, uint16_t record_handle_count){
291     int i;
292     for (i=0;i<record_handle_count;i++){
293         record_handle = big_endian_read_32(data, i*4);
294         record_counter++;
295         uint8_t event[10];
296         event[0] = SDP_EVENT_QUERY_SERVICE_RECORD_HANDLE;
297         event[1] = 8;
298         little_endian_store_16(event, 2, total_count);
299         little_endian_store_16(event, 4, record_counter);
300         little_endian_store_32(event, 6, record_handle);
301         (*sdp_parser_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
302     }
303 }
304 #endif
305 
306 void sdp_parser_handle_done(uint8_t status){
307     uint8_t event[3];
308     event[0] = SDP_EVENT_QUERY_COMPLETE;
309     event[1] = 1;
310     event[2] = status;
311     (*sdp_parser_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
312 }
313 
314 static void sdp_client_emit_busy(btstack_packet_handler_t callback){
315     log_error("sdp_client query initiated when not ready");
316     uint8_t event[] = { SDP_EVENT_QUERY_COMPLETE, 1, SDP_QUERY_BUSY};
317     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
318 }
319 
320 // SDP Client
321 
322 // TODO: inline if not needed (des(des))
323 
324 static void sdp_client_parse_attribute_lists(uint8_t* packet, uint16_t length){
325     sdp_parser_handle_chunk(packet, length);
326 }
327 
328 
329 static void sdp_client_send_request(uint16_t channel){
330 
331     if (sdp_client_state != W2_SEND) return;
332 
333     l2cap_reserve_packet_buffer();
334     uint8_t * data = l2cap_get_outgoing_buffer();
335     uint16_t request_len = 0;
336 
337     switch (PDU_ID){
338 #ifdef ENABLE_SDP_EXTRA_QUERIES
339         case SDP_ServiceSearchResponse:
340             request_len = sdp_client_setup_service_search_request(data);
341             break;
342         case SDP_ServiceAttributeResponse:
343             request_len = sdp_client_setup_service_attribute_request(data);
344             break;
345 #endif
346         case SDP_ServiceSearchAttributeResponse:
347             request_len = sdp_client_setup_service_search_attribute_request(data);
348             break;
349         default:
350             log_error("SDP Client sdp_client_send_request :: PDU ID invalid. %u", PDU_ID);
351             return;
352     }
353 
354     // prevent re-entrance
355     sdp_client_state = W4_RESPONSE;
356     PDU_ID = SDP_Invalid;
357     l2cap_send_prepared(channel, request_len);
358 }
359 
360 
361 static void sdp_client_parse_service_search_attribute_response(uint8_t* packet){
362     uint16_t offset = 3;
363     uint16_t parameterLength = big_endian_read_16(packet,offset);
364     offset+=2;
365     // AttributeListByteCount <= mtu
366     uint16_t attributeListByteCount = big_endian_read_16(packet,offset);
367     offset+=2;
368 
369     if (attributeListByteCount > mtu){
370         log_error("Error parsing ServiceSearchAttributeResponse: Number of bytes in found attribute list is larger then the MaximumAttributeByteCount.");
371         return;
372     }
373 
374     // AttributeLists
375     sdp_client_parse_attribute_lists(packet+offset, attributeListByteCount);
376     offset+=attributeListByteCount;
377 
378     continuationStateLen = packet[offset];
379     offset++;
380 
381     if (continuationStateLen > 16){
382         log_error("Error parsing ServiceSearchAttributeResponse: Number of bytes in continuation state exceedes 16.");
383         return;
384     }
385     memcpy(continuationState, packet+offset, continuationStateLen);
386     offset+=continuationStateLen;
387 
388     if (parameterLength != offset - 5){
389         log_error("Error parsing ServiceSearchAttributeResponse: wrong size of parameters, number of expected bytes%u, actual number %u.", parameterLength, offset);
390     }
391 }
392 
393 void sdp_client_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
394     // uint16_t handle;
395     if (packet_type == L2CAP_DATA_PACKET){
396         uint16_t responseTransactionID = big_endian_read_16(packet,1);
397         if ( responseTransactionID != transactionID){
398             log_error("Missmatching transaction ID, expected %u, found %u.", transactionID, responseTransactionID);
399             return;
400         }
401 
402         if (packet[0] != SDP_ServiceSearchAttributeResponse
403             && packet[0] != SDP_ServiceSearchResponse
404             && packet[0] != SDP_ServiceAttributeResponse){
405             log_error("Not a valid PDU ID, expected %u, %u or %u, found %u.", SDP_ServiceSearchResponse,
406                                     SDP_ServiceAttributeResponse, SDP_ServiceSearchAttributeResponse, packet[0]);
407             return;
408         }
409 
410         PDU_ID = (SDP_PDU_ID_t)packet[0];
411         log_info("SDP Client :: PDU ID. %u ,%u", PDU_ID, packet[0]);
412         switch (PDU_ID){
413 #ifdef ENABLE_SDP_EXTRA_QUERIES
414             case SDP_ServiceSearchResponse:
415                 sdp_client_parse_service_search_response(packet);
416                 break;
417             case SDP_ServiceAttributeResponse:
418                 sdp_client_parse_service_attribute_response(packet);
419                 break;
420 #endif
421             case SDP_ServiceSearchAttributeResponse:
422                 sdp_client_parse_service_search_attribute_response(packet);
423                 break;
424             default:
425                 log_error("SDP Client :: PDU ID invalid. %u ,%u", PDU_ID, packet[0]);
426                 return;
427         }
428 
429         // continuation set or DONE?
430         if (continuationStateLen == 0){
431             log_info("SDP Client Query DONE! ");
432             sdp_client_state = QUERY_COMPLETE;
433             l2cap_disconnect(sdp_cid, 0);
434             // sdp_parser_handle_done(0);
435             return;
436         }
437         // prepare next request and send
438         sdp_client_state = W2_SEND;
439         l2cap_request_can_send_now_event(sdp_cid);
440         return;
441     }
442 
443     if (packet_type != HCI_EVENT_PACKET) return;
444 
445     switch(hci_event_packet_get_type(packet)){
446         case L2CAP_EVENT_CHANNEL_OPENED:
447             if (sdp_client_state != W4_CONNECT) break;
448             // data: event (8), len(8), status (8), address(48), handle (16), psm (16), local_cid(16), remote_cid (16), local_mtu(16), remote_mtu(16)
449             if (packet[2]) {
450                 log_error("SDP Client Connection failed.");
451                 sdp_parser_handle_done(packet[2]);
452                 break;
453             }
454             sdp_cid = channel;
455             mtu = little_endian_read_16(packet, 17);
456             // handle = little_endian_read_16(packet, 9);
457             log_info("SDP Client Connected, cid %x, mtu %u.", sdp_cid, mtu);
458 
459             sdp_client_state = W2_SEND;
460             l2cap_request_can_send_now_event(sdp_cid);
461             break;
462 
463         case L2CAP_EVENT_CAN_SEND_NOW:
464             if(l2cap_event_can_send_now_get_local_cid(packet) == sdp_cid){
465                 sdp_client_send_request(sdp_cid);
466             }
467             break;
468         case L2CAP_EVENT_CHANNEL_CLOSED: {
469             if (sdp_cid != little_endian_read_16(packet, 2)) {
470                 // log_info("Received L2CAP_EVENT_CHANNEL_CLOSED for cid %x, current cid %x\n",  little_endian_read_16(packet, 2),sdp_cid);
471                 break;
472             }
473             log_info("SDP Client disconnected.");
474             uint8_t status = sdp_client_state == QUERY_COMPLETE ? 0 : SDP_QUERY_INCOMPLETE;
475             sdp_client_state = INIT;
476             sdp_parser_handle_done(status);
477             break;
478         }
479         default:
480             break;
481     }
482 }
483 
484 
485 static uint16_t sdp_client_setup_service_search_attribute_request(uint8_t * data){
486 
487     uint16_t offset = 0;
488     transactionID++;
489     // uint8_t SDP_PDU_ID_t.SDP_ServiceSearchRequest;
490     data[offset++] = SDP_ServiceSearchAttributeRequest;
491     // uint16_t transactionID
492     big_endian_store_16(data, offset, transactionID);
493     offset += 2;
494 
495     // param legnth
496     offset += 2;
497 
498     // parameters:
499     //     Service_search_pattern - DES (min 1 UUID, max 12)
500     uint16_t service_search_pattern_len = de_get_len(service_search_pattern);
501     memcpy(data + offset, service_search_pattern, service_search_pattern_len);
502     offset += service_search_pattern_len;
503 
504     //     MaximumAttributeByteCount - uint16_t  0x0007 - 0xffff -> mtu
505     big_endian_store_16(data, offset, mtu);
506     offset += 2;
507 
508     //     AttibuteIDList
509     uint16_t attribute_id_list_len = de_get_len(attribute_id_list);
510     memcpy(data + offset, attribute_id_list, attribute_id_list_len);
511     offset += attribute_id_list_len;
512 
513     //     ContinuationState - uint8_t number of cont. bytes N<=16
514     data[offset++] = continuationStateLen;
515     //                       - N-bytes previous response from server
516     memcpy(data + offset, continuationState, continuationStateLen);
517     offset += continuationStateLen;
518 
519     // uint16_t paramLength
520     big_endian_store_16(data, 3, offset - 5);
521 
522     return offset;
523 }
524 
525 #ifdef ENABLE_SDP_EXTRA_QUERIES
526 void sdp_client_parse_service_record_handle_list(uint8_t* packet, uint16_t total_count, uint16_t current_count){
527     sdp_parser_handle_service_search(packet, total_count, current_count);
528 }
529 
530 static uint16_t sdp_client_setup_service_search_request(uint8_t * data){
531     uint16_t offset = 0;
532     transactionID++;
533     // uint8_t SDP_PDU_ID_t.SDP_ServiceSearchRequest;
534     data[offset++] = SDP_ServiceSearchRequest;
535     // uint16_t transactionID
536     big_endian_store_16(data, offset, transactionID);
537     offset += 2;
538 
539     // param legnth
540     offset += 2;
541 
542     // parameters:
543     //     Service_search_pattern - DES (min 1 UUID, max 12)
544     uint16_t service_search_pattern_len = de_get_len(service_search_pattern);
545     memcpy(data + offset, service_search_pattern, service_search_pattern_len);
546     offset += service_search_pattern_len;
547 
548     //     MaximumAttributeByteCount - uint16_t  0x0007 - 0xffff -> mtu
549     big_endian_store_16(data, offset, mtu);
550     offset += 2;
551 
552     //     ContinuationState - uint8_t number of cont. bytes N<=16
553     data[offset++] = continuationStateLen;
554     //                       - N-bytes previous response from server
555     memcpy(data + offset, continuationState, continuationStateLen);
556     offset += continuationStateLen;
557 
558     // uint16_t paramLength
559     big_endian_store_16(data, 3, offset - 5);
560 
561     return offset;
562 }
563 
564 
565 static uint16_t sdp_client_setup_service_attribute_request(uint8_t * data){
566 
567     uint16_t offset = 0;
568     transactionID++;
569     // uint8_t SDP_PDU_ID_t.SDP_ServiceSearchRequest;
570     data[offset++] = SDP_ServiceAttributeRequest;
571     // uint16_t transactionID
572     big_endian_store_16(data, offset, transactionID);
573     offset += 2;
574 
575     // param legnth
576     offset += 2;
577 
578     // parameters:
579     //     ServiceRecordHandle
580     big_endian_store_32(data, offset, serviceRecordHandle);
581     offset += 4;
582 
583     //     MaximumAttributeByteCount - uint16_t  0x0007 - 0xffff -> mtu
584     big_endian_store_16(data, offset, mtu);
585     offset += 2;
586 
587     //     AttibuteIDList
588     uint16_t attribute_id_list_len = de_get_len(attribute_id_list);
589     memcpy(data + offset, attribute_id_list, attribute_id_list_len);
590     offset += attribute_id_list_len;
591 
592     //     ContinuationState - uint8_t number of cont. bytes N<=16
593     data[offset++] = continuationStateLen;
594     //                       - N-bytes previous response from server
595     memcpy(data + offset, continuationState, continuationStateLen);
596     offset += continuationStateLen;
597 
598     // uint16_t paramLength
599     big_endian_store_16(data, 3, offset - 5);
600 
601     return offset;
602 }
603 
604 static void sdp_client_parse_service_search_response(uint8_t* packet){
605     uint16_t offset = 3;
606     uint16_t parameterLength = big_endian_read_16(packet,offset);
607     offset+=2;
608 
609     uint16_t totalServiceRecordCount = big_endian_read_16(packet,offset);
610     offset+=2;
611 
612     uint16_t currentServiceRecordCount = big_endian_read_16(packet,offset);
613     offset+=2;
614     if (currentServiceRecordCount > totalServiceRecordCount){
615         log_error("CurrentServiceRecordCount is larger then TotalServiceRecordCount.");
616         return;
617     }
618 
619     sdp_client_parse_service_record_handle_list(packet+offset, totalServiceRecordCount, currentServiceRecordCount);
620     offset+=(currentServiceRecordCount * 4);
621 
622     continuationStateLen = packet[offset];
623     offset++;
624     if (continuationStateLen > 16){
625         log_error("Error parsing ServiceSearchResponse: Number of bytes in continuation state exceedes 16.");
626         return;
627     }
628     memcpy(continuationState, packet+offset, continuationStateLen);
629     offset+=continuationStateLen;
630 
631     if (parameterLength != offset - 5){
632         log_error("Error parsing ServiceSearchResponse: wrong size of parameters, number of expected bytes%u, actual number %u.", parameterLength, offset);
633     }
634 }
635 
636 static void sdp_client_parse_service_attribute_response(uint8_t* packet){
637     uint16_t offset = 3;
638     uint16_t parameterLength = big_endian_read_16(packet,offset);
639     offset+=2;
640 
641     // AttributeListByteCount <= mtu
642     uint16_t attributeListByteCount = big_endian_read_16(packet,offset);
643     offset+=2;
644 
645     if (attributeListByteCount > mtu){
646         log_error("Error parsing ServiceSearchAttributeResponse: Number of bytes in found attribute list is larger then the MaximumAttributeByteCount.");
647         return;
648     }
649 
650     // AttributeLists
651     sdp_client_parse_attribute_lists(packet+offset, attributeListByteCount);
652     offset+=attributeListByteCount;
653 
654     continuationStateLen = packet[offset];
655     offset++;
656 
657     if (continuationStateLen > 16){
658         log_error("Error parsing ServiceAttributeResponse: Number of bytes in continuation state exceedes 16.");
659         return;
660     }
661     memcpy(continuationState, packet+offset, continuationStateLen);
662     offset+=continuationStateLen;
663 
664     if (parameterLength != offset - 5){
665         log_error("Error parsing ServiceAttributeResponse: wrong size of parameters, number of expected bytes%u, actual number %u.", parameterLength, offset);
666     }
667 }
668 #endif
669 
670 // for testing only
671 void sdp_client_reset(void){
672     sdp_client_state = INIT;
673 }
674 
675 // Public API
676 
677 int sdp_client_ready(void){
678     return sdp_client_state == INIT;
679 }
680 
681 void sdp_client_query(btstack_packet_handler_t callback, bd_addr_t remote, const uint8_t * des_service_search_pattern, const uint8_t * des_attribute_id_list){
682     if (!sdp_client_ready()) {
683         sdp_client_emit_busy(callback);
684         return;
685     }
686     sdp_parser_init(callback);
687     service_search_pattern = des_service_search_pattern;
688     attribute_id_list = des_attribute_id_list;
689     continuationStateLen = 0;
690     PDU_ID = SDP_ServiceSearchAttributeResponse;
691 
692     sdp_client_state = W4_CONNECT;
693     l2cap_create_channel(sdp_client_packet_handler, remote, PSM_SDP, l2cap_max_mtu(), NULL);
694 }
695 
696 void sdp_client_query_uuid16(btstack_packet_handler_t callback, bd_addr_t remote, uint16_t uuid){
697     if (!sdp_client_ready()){
698         sdp_client_emit_busy(callback);
699         return;
700     }
701     uint8_t * service_service_search_pattern = sdp_service_search_pattern_for_uuid16(uuid);
702     sdp_client_query(callback, remote, service_service_search_pattern, des_attributeIDList);
703 }
704 
705 void sdp_client_query_uuid128(btstack_packet_handler_t callback, bd_addr_t remote, const uint8_t* uuid){
706     if (!sdp_client_ready()){
707         sdp_client_emit_busy(callback);
708         return;
709     }
710     uint8_t * service_service_search_pattern = sdp_service_search_pattern_for_uuid128(uuid);
711     sdp_client_query(callback, remote, service_service_search_pattern, des_attributeIDList);
712 }
713 
714 #ifdef ENABLE_SDP_EXTRA_QUERIES
715 void sdp_client_service_attribute_search(btstack_packet_handler_t callback, bd_addr_t remote, uint32_t search_service_record_handle, uint8_t * des_attribute_id_list){
716     if (!sdp_client_ready()) {
717         sdp_client_emit_busy(callback);
718         return;
719     }
720     sdp_parser_init(callback);
721     serviceRecordHandle = search_service_record_handle;
722     attribute_id_list = des_attribute_id_list;
723     continuationStateLen = 0;
724     PDU_ID = SDP_ServiceAttributeResponse;
725 
726     sdp_client_state = W4_CONNECT;
727     l2cap_create_channel(sdp_client_packet_handler, remote, PSM_SDP, l2cap_max_mtu(), NULL);
728 }
729 
730 void sdp_client_service_search(btstack_packet_handler_t callback, bd_addr_t remote, uint8_t * des_service_search_pattern){
731     if (!sdp_client_ready()) {
732         sdp_client_emit_busy(callback);
733         return;
734     }
735     sdp_parser_init(callback);
736     service_search_pattern = des_service_search_pattern;
737     continuationStateLen = 0;
738     PDU_ID = SDP_ServiceSearchResponse;
739 
740     sdp_client_state = W4_CONNECT;
741     l2cap_create_channel(sdp_client_packet_handler, remote, PSM_SDP, l2cap_max_mtu(), NULL);
742 }
743 #endif
744 
745