1 /******************************************************************************
2 *
3 * Copyright (C) 1999-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /******************************************************************************
20 *
21 * this file contains SDP discovery functions
22 *
23 ******************************************************************************/
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28
29 #include "common/bt_target.h"
30 #include "osi/allocator.h"
31 #include "stack/l2cdefs.h"
32 #include "stack/hcidefs.h"
33 #include "stack/hcimsgs.h"
34 #include "stack/sdp_api.h"
35 #include "sdpint.h"
36 #include "stack/btu.h"
37 #include "stack/btm_api.h"
38
39
40 #ifndef SDP_DEBUG_RAW
41 #define SDP_DEBUG_RAW FALSE
42 #endif
43
44 /********************************************************************************/
45 /* L O C A L F U N C T I O N P R O T O T Y P E S */
46 /********************************************************************************/
47 #if SDP_CLIENT_ENABLED == TRUE
48 static void process_service_search_rsp (tCONN_CB *p_ccb, UINT8 *p_reply);
49 static void process_service_attr_rsp (tCONN_CB *p_ccb, UINT8 *p_reply);
50 static void process_service_search_attr_rsp (tCONN_CB *p_ccb, UINT8 *p_reply);
51 static UINT8 *save_attr_seq (tCONN_CB *p_ccb, UINT8 *p, UINT8 *p_msg_end);
52 static tSDP_DISC_REC *add_record (tSDP_DISCOVERY_DB *p_db, BD_ADDR p_bda);
53 static UINT8 *add_attr (UINT8 *p, tSDP_DISCOVERY_DB *p_db, tSDP_DISC_REC *p_rec,
54 UINT16 attr_id, tSDP_DISC_ATTR *p_parent_attr, UINT8 nest_level);
55
56 /* Safety check in case we go crazy */
57 #define MAX_NEST_LEVELS 5
58
59
60 /*******************************************************************************
61 **
62 ** Function sdpu_build_uuid_seq
63 **
64 ** Description This function builds a UUID sequence from the list of
65 ** passed UUIDs. It is also passed the address of the output
66 ** buffer.
67 **
68 ** Returns Pointer to next byte in the output buffer.
69 **
70 *******************************************************************************/
sdpu_build_uuid_seq(UINT8 * p_out,UINT16 num_uuids,tSDP_UUID * p_uuid_list)71 static UINT8 *sdpu_build_uuid_seq (UINT8 *p_out, UINT16 num_uuids, tSDP_UUID *p_uuid_list)
72 {
73 UINT16 xx;
74 UINT8 *p_len;
75
76 /* First thing is the data element header */
77 UINT8_TO_BE_STREAM (p_out, (DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
78
79 /* Remember where the length goes. Leave space for it. */
80 p_len = p_out;
81 p_out += 1;
82
83 /* Now, loop through and put in all the UUID(s) */
84 for (xx = 0; xx < num_uuids; xx++, p_uuid_list++) {
85 if (p_uuid_list->len == 2) {
86 UINT8_TO_BE_STREAM (p_out, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES);
87 UINT16_TO_BE_STREAM (p_out, p_uuid_list->uu.uuid16);
88 } else if (p_uuid_list->len == 4) {
89 UINT8_TO_BE_STREAM (p_out, (UUID_DESC_TYPE << 3) | SIZE_FOUR_BYTES);
90 UINT32_TO_BE_STREAM (p_out, p_uuid_list->uu.uuid32);
91 } else {
92 UINT8_TO_BE_STREAM (p_out, (UUID_DESC_TYPE << 3) | SIZE_SIXTEEN_BYTES);
93 ARRAY_TO_BE_STREAM (p_out, p_uuid_list->uu.uuid128, p_uuid_list->len);
94 }
95 }
96
97 /* Now, put in the length */
98 xx = (UINT16)(p_out - p_len - 1);
99 UINT8_TO_BE_STREAM (p_len, xx);
100
101 return (p_out);
102 }
103
104 /*******************************************************************************
105 **
106 ** Function sdp_snd_service_search_req
107 **
108 ** Description Send a service search request to the SDP server.
109 **
110 ** Returns void
111 **
112 *******************************************************************************/
sdp_snd_service_search_req(tCONN_CB * p_ccb,UINT8 cont_len,UINT8 * p_cont)113 static void sdp_snd_service_search_req(tCONN_CB *p_ccb, UINT8 cont_len, UINT8 *p_cont)
114 {
115 UINT8 *p, *p_start, *p_param_len;
116 BT_HDR *p_cmd;
117 UINT16 param_len;
118
119 /* Get a buffer to send the packet to L2CAP */
120 if ((p_cmd = (BT_HDR *) osi_malloc(SDP_DATA_BUF_SIZE)) == NULL) {
121 sdp_disconnect (p_ccb, SDP_NO_RESOURCES);
122 return;
123 }
124
125 p_cmd->offset = L2CAP_MIN_OFFSET;
126 p = p_start = (UINT8 *)(p_cmd + 1) + L2CAP_MIN_OFFSET;
127
128 /* Build a service search request packet */
129 UINT8_TO_BE_STREAM (p, SDP_PDU_SERVICE_SEARCH_REQ);
130 UINT16_TO_BE_STREAM (p, p_ccb->transaction_id);
131 p_ccb->transaction_id++;
132
133 /* Skip the length, we need to add it at the end */
134 p_param_len = p;
135 p += 2;
136
137 /* Build the UID sequence. */
138 #if (defined(SDP_BROWSE_PLUS) && SDP_BROWSE_PLUS == TRUE)
139 p = sdpu_build_uuid_seq (p, 1, &p_ccb->p_db->uuid_filters[p_ccb->cur_uuid_idx]);
140 #else
141 p = sdpu_build_uuid_seq (p, p_ccb->p_db->num_uuid_filters, p_ccb->p_db->uuid_filters);
142 #endif
143
144 /* Set max service record count */
145 UINT16_TO_BE_STREAM (p, sdp_cb.max_recs_per_search);
146
147 /* Set continuation state */
148 UINT8_TO_BE_STREAM (p, cont_len);
149
150 /* if this is not the first request */
151 if (cont_len && p_cont) {
152 memcpy(p, p_cont, cont_len);
153 p += cont_len;
154 }
155
156 /* Go back and put the parameter length into the buffer */
157 param_len = (UINT16)(p - p_param_len - 2);
158 UINT16_TO_BE_STREAM (p_param_len, param_len);
159
160 p_ccb->disc_state = SDP_DISC_WAIT_HANDLES;
161
162 /* Set the length of the SDP data in the buffer */
163 p_cmd->len = (UINT16)(p - p_start);
164
165 #if (SDP_DEBUG_RAW == TRUE)
166 SDP_TRACE_WARNING("sdp_snd_service_search_req cont_len :%d disc_state:%d\n", cont_len, p_ccb->disc_state);
167 #endif
168
169
170 L2CA_DataWrite (p_ccb->connection_id, p_cmd);
171
172 /* Start inactivity timer */
173 btu_start_timer (&p_ccb->timer_entry, BTU_TTYPE_SDP, SDP_INACT_TIMEOUT);
174
175 }
176
177 /*******************************************************************************
178 **
179 ** Function sdp_disc_connected
180 **
181 ** Description This function is called when an SDP discovery attempt is
182 ** connected.
183 **
184 ** Returns void
185 **
186 *******************************************************************************/
sdp_disc_connected(tCONN_CB * p_ccb)187 void sdp_disc_connected (tCONN_CB *p_ccb)
188 {
189 if (p_ccb->is_attr_search) {
190 p_ccb->disc_state = SDP_DISC_WAIT_SEARCH_ATTR;
191
192 process_service_search_attr_rsp (p_ccb, NULL);
193 } else {
194 /* First step is to get a list of the handles from the server. */
195 /* We are not searching for a specific attribute, so we will */
196 /* first search for the service, then get all attributes of it */
197
198 p_ccb->num_handles = 0;
199 sdp_snd_service_search_req(p_ccb, 0, NULL);
200 }
201
202 }
203
204 /*******************************************************************************
205 **
206 ** Function sdp_disc_server_rsp
207 **
208 ** Description This function is called when there is a response from
209 ** the server.
210 **
211 ** Returns void
212 **
213 *******************************************************************************/
sdp_disc_server_rsp(tCONN_CB * p_ccb,BT_HDR * p_msg)214 void sdp_disc_server_rsp (tCONN_CB *p_ccb, BT_HDR *p_msg)
215 {
216 UINT8 *p, rsp_pdu;
217 BOOLEAN invalid_pdu = TRUE;
218
219 #if (SDP_DEBUG_RAW == TRUE)
220 SDP_TRACE_WARNING("sdp_disc_server_rsp disc_state:%d\n", p_ccb->disc_state);
221 #endif
222
223 /* stop inactivity timer when we receive a response */
224 btu_stop_timer (&p_ccb->timer_entry);
225
226 /* Got a reply!! Check what we got back */
227 p = (UINT8 *)(p_msg + 1) + p_msg->offset;
228
229 BE_STREAM_TO_UINT8 (rsp_pdu, p);
230
231 p_msg->len--;
232
233 switch (rsp_pdu) {
234 case SDP_PDU_SERVICE_SEARCH_RSP:
235 if (p_ccb->disc_state == SDP_DISC_WAIT_HANDLES) {
236 process_service_search_rsp (p_ccb, p);
237 invalid_pdu = FALSE;
238 }
239 break;
240
241 case SDP_PDU_SERVICE_ATTR_RSP:
242 if (p_ccb->disc_state == SDP_DISC_WAIT_ATTR) {
243 process_service_attr_rsp (p_ccb, p);
244 invalid_pdu = FALSE;
245 }
246 break;
247
248 case SDP_PDU_SERVICE_SEARCH_ATTR_RSP:
249 if (p_ccb->disc_state == SDP_DISC_WAIT_SEARCH_ATTR) {
250 process_service_search_attr_rsp (p_ccb, p);
251 invalid_pdu = FALSE;
252 }
253 break;
254 }
255
256 if (invalid_pdu) {
257 SDP_TRACE_WARNING ("SDP - Unexp. PDU: %d in state: %d\n", rsp_pdu, p_ccb->disc_state);
258 sdp_disconnect (p_ccb, SDP_GENERIC_ERROR);
259 }
260 }
261
262 /******************************************************************************
263 **
264 ** Function process_service_search_rsp
265 **
266 ** Description This function is called when there is a search response from
267 ** the server.
268 **
269 ** Returns void
270 **
271 *******************************************************************************/
process_service_search_rsp(tCONN_CB * p_ccb,UINT8 * p_reply)272 static void process_service_search_rsp (tCONN_CB *p_ccb, UINT8 *p_reply)
273 {
274 UINT16 xx;
275 UINT16 total, cur_handles, orig;
276 UINT8 cont_len;
277
278 /* Skip transaction, and param len */
279 p_reply += 4;
280 BE_STREAM_TO_UINT16 (total, p_reply);
281 BE_STREAM_TO_UINT16 (cur_handles, p_reply);
282
283 orig = p_ccb->num_handles;
284 p_ccb->num_handles += cur_handles;
285 if (p_ccb->num_handles == 0) {
286 SDP_TRACE_WARNING ("SDP - Rcvd ServiceSearchRsp, no matches\n");
287 sdp_disconnect (p_ccb, SDP_NO_RECS_MATCH);
288 return;
289 }
290
291 /* Save the handles that match. We will can only process a certain number. */
292 if (total > sdp_cb.max_recs_per_search) {
293 total = sdp_cb.max_recs_per_search;
294 }
295 if (p_ccb->num_handles > sdp_cb.max_recs_per_search) {
296 p_ccb->num_handles = sdp_cb.max_recs_per_search;
297 }
298
299 for (xx = orig; xx < p_ccb->num_handles; xx++) {
300 BE_STREAM_TO_UINT32 (p_ccb->handles[xx], p_reply);
301 }
302
303 BE_STREAM_TO_UINT8 (cont_len, p_reply);
304 if (cont_len != 0) {
305 if (cont_len > SDP_MAX_CONTINUATION_LEN) {
306 sdp_disconnect (p_ccb, SDP_INVALID_CONT_STATE);
307 return;
308 }
309 /* stay in the same state */
310 sdp_snd_service_search_req(p_ccb, cont_len, p_reply);
311 } else {
312 /* change state */
313 p_ccb->disc_state = SDP_DISC_WAIT_ATTR;
314
315 /* Kick off the first attribute request */
316 process_service_attr_rsp (p_ccb, NULL);
317 }
318 }
319
320 /*******************************************************************************
321 **
322 ** Function sdp_copy_raw_data
323 **
324 ** Description copy the raw data
325 **
326 **
327 ** Returns void
328 **
329 *******************************************************************************/
330 #if (SDP_RAW_DATA_INCLUDED == TRUE)
sdp_copy_raw_data(tCONN_CB * p_ccb,BOOLEAN offset)331 static void sdp_copy_raw_data (tCONN_CB *p_ccb, BOOLEAN offset)
332 {
333 unsigned int cpy_len;
334 UINT32 list_len;
335 UINT8 *p;
336 UINT8 type;
337
338 #if (SDP_DEBUG_RAW == TRUE)
339 UINT8 num_array[SDP_MAX_LIST_BYTE_COUNT];
340 UINT32 i;
341
342 for (i = 0; i < p_ccb->list_len; i++) {
343 sprintf((char *)&num_array[i * 2], "%02X\n", (UINT8)(p_ccb->rsp_list[i]));
344 }
345 SDP_TRACE_WARNING("result :%s\n", num_array);
346 #endif
347
348 if (p_ccb->p_db->raw_data) {
349 cpy_len = p_ccb->p_db->raw_size - p_ccb->p_db->raw_used;
350 list_len = p_ccb->list_len;
351 p = &p_ccb->rsp_list[0];
352
353 if (offset) {
354 type = *p++;
355 p = sdpu_get_len_from_type (p, type, &list_len);
356 }
357 if (list_len < cpy_len ) {
358 cpy_len = list_len;
359 }
360 #if (SDP_DEBUG_RAW == TRUE)
361 SDP_TRACE_DEBUG("list_len :%d cpy_len:%d raw_size:%d raw_used:%d\n",
362 list_len, cpy_len, p_ccb->p_db->raw_size, p_ccb->p_db->raw_used);
363 #endif
364 if (cpy_len != 0){
365 memcpy (&p_ccb->p_db->raw_data[p_ccb->p_db->raw_used], p, cpy_len);
366 p_ccb->p_db->raw_used += cpy_len;
367 }
368 }
369 }
370 #endif
371
372 /*******************************************************************************
373 **
374 ** Function process_service_attr_rsp
375 **
376 ** Description This function is called when there is a attribute response from
377 ** the server.
378 **
379 ** Returns void
380 **
381 *******************************************************************************/
process_service_attr_rsp(tCONN_CB * p_ccb,UINT8 * p_reply)382 static void process_service_attr_rsp (tCONN_CB *p_ccb, UINT8 *p_reply)
383 {
384 UINT8 *p_start, *p_param_len;
385 UINT16 param_len, list_byte_count;
386 BOOLEAN cont_request_needed = FALSE;
387
388 #if (SDP_DEBUG_RAW == TRUE)
389 SDP_TRACE_WARNING("process_service_attr_rsp raw inc:%d\n",
390 SDP_RAW_DATA_INCLUDED);
391 #endif
392 /* If p_reply is NULL, we were called after the records handles were read */
393 if (p_reply) {
394 #if (SDP_DEBUG_RAW == TRUE)
395 SDP_TRACE_WARNING("ID & len: 0x%02x-%02x-%02x-%02x\n",
396 p_reply[0], p_reply[1], p_reply[2], p_reply[3]);
397 #endif
398 /* Skip transaction ID and length */
399 p_reply += 4;
400
401 BE_STREAM_TO_UINT16 (list_byte_count, p_reply);
402 #if (SDP_DEBUG_RAW == TRUE)
403 SDP_TRACE_WARNING("list_byte_count:%d\n", list_byte_count);
404 #endif
405
406 /* Copy the response to the scratchpad. First, a safety check on the length */
407 if ((p_ccb->list_len + list_byte_count) > SDP_MAX_LIST_BYTE_COUNT) {
408 sdp_disconnect (p_ccb, SDP_INVALID_PDU_SIZE);
409 return;
410 }
411
412 #if (SDP_DEBUG_RAW == TRUE)
413 SDP_TRACE_WARNING("list_len: %d, list_byte_count: %d\n",
414 p_ccb->list_len, list_byte_count);
415 #endif
416 if (p_ccb->rsp_list == NULL) {
417 p_ccb->rsp_list = (UINT8 *)osi_malloc (SDP_MAX_LIST_BYTE_COUNT);
418 if (p_ccb->rsp_list == NULL) {
419 SDP_TRACE_ERROR ("SDP - no gki buf to save rsp\n");
420 sdp_disconnect (p_ccb, SDP_NO_RESOURCES);
421 return;
422 }
423 }
424 memcpy (&p_ccb->rsp_list[p_ccb->list_len], p_reply, list_byte_count);
425 p_ccb->list_len += list_byte_count;
426 p_reply += list_byte_count;
427 #if (SDP_DEBUG_RAW == TRUE)
428 SDP_TRACE_WARNING("list_len: %d(attr_rsp)\n", p_ccb->list_len);
429
430 /* Check if we need to request a continuation */
431 SDP_TRACE_WARNING("*p_reply:%d(%d)\n", *p_reply, SDP_MAX_CONTINUATION_LEN);
432 #endif
433 if (*p_reply) {
434 if (*p_reply > SDP_MAX_CONTINUATION_LEN) {
435 sdp_disconnect (p_ccb, SDP_INVALID_CONT_STATE);
436 return;
437 }
438 cont_request_needed = TRUE;
439 } else {
440
441 #if (SDP_RAW_DATA_INCLUDED == TRUE)
442 SDP_TRACE_WARNING("process_service_attr_rsp\n");
443 sdp_copy_raw_data (p_ccb, FALSE);
444 #endif
445
446 /* Save the response in the database. Stop on any error */
447 if (!save_attr_seq (p_ccb, &p_ccb->rsp_list[0], &p_ccb->rsp_list[p_ccb->list_len])) {
448 sdp_disconnect (p_ccb, SDP_DB_FULL);
449 return;
450 }
451 p_ccb->list_len = 0;
452 p_ccb->cur_handle++;
453 }
454 }
455
456 /* Now, ask for the next handle. Re-use the buffer we just got. */
457 if (p_ccb->cur_handle < p_ccb->num_handles) {
458 BT_HDR *p_msg = (BT_HDR *) osi_malloc(SDP_DATA_BUF_SIZE);
459 UINT8 *p;
460
461 if (!p_msg) {
462 sdp_disconnect (p_ccb, SDP_NO_RESOURCES);
463 return;
464 }
465
466 p_msg->offset = L2CAP_MIN_OFFSET;
467 p = p_start = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
468
469 /* Get all the attributes from the server */
470 UINT8_TO_BE_STREAM (p, SDP_PDU_SERVICE_ATTR_REQ);
471 UINT16_TO_BE_STREAM (p, p_ccb->transaction_id);
472 p_ccb->transaction_id++;
473
474 /* Skip the length, we need to add it at the end */
475 p_param_len = p;
476 p += 2;
477
478 UINT32_TO_BE_STREAM (p, p_ccb->handles[p_ccb->cur_handle]);
479
480 /* Max attribute byte count */
481 UINT16_TO_BE_STREAM (p, sdp_cb.max_attr_list_size);
482
483 /* If no attribute filters, build a wildcard attribute sequence */
484 if (p_ccb->p_db->num_attr_filters) {
485 p = sdpu_build_attrib_seq (p, p_ccb->p_db->attr_filters, p_ccb->p_db->num_attr_filters);
486 } else {
487 p = sdpu_build_attrib_seq (p, NULL, 0);
488 }
489
490 /* Was this a continuation request ? */
491 if (cont_request_needed) {
492 memcpy (p, p_reply, *p_reply + 1);
493 p += *p_reply + 1;
494 } else {
495 UINT8_TO_BE_STREAM (p, 0);
496 }
497
498 /* Go back and put the parameter length into the buffer */
499 param_len = (UINT16)(p - p_param_len - 2);
500 UINT16_TO_BE_STREAM (p_param_len, param_len);
501
502 /* Set the length of the SDP data in the buffer */
503 p_msg->len = (UINT16)(p - p_start);
504
505
506 L2CA_DataWrite (p_ccb->connection_id, p_msg);
507
508 /* Start inactivity timer */
509 btu_start_timer (&p_ccb->timer_entry, BTU_TTYPE_SDP, SDP_INACT_TIMEOUT);
510 } else {
511 sdp_disconnect (p_ccb, SDP_SUCCESS);
512 return;
513 }
514 }
515
516
517 /*******************************************************************************
518 **
519 ** Function process_service_search_attr_rsp
520 **
521 ** Description This function is called when there is a search attribute
522 ** response from the server.
523 **
524 ** Returns void
525 **
526 *******************************************************************************/
process_service_search_attr_rsp(tCONN_CB * p_ccb,UINT8 * p_reply)527 static void process_service_search_attr_rsp (tCONN_CB *p_ccb, UINT8 *p_reply)
528 {
529 UINT8 *p, *p_start, *p_end, *p_param_len;
530 UINT8 type;
531 UINT32 seq_len;
532 UINT16 param_len, lists_byte_count = 0;
533 BOOLEAN cont_request_needed = FALSE;
534
535 #if (SDP_DEBUG_RAW == TRUE)
536 SDP_TRACE_WARNING("process_service_search_attr_rsp\n");
537 #endif
538 /* If p_reply is NULL, we were called for the initial read */
539 if (p_reply) {
540 #if (SDP_DEBUG_RAW == TRUE)
541 SDP_TRACE_WARNING("ID & len: 0x%02x-%02x-%02x-%02x\n",
542 p_reply[0], p_reply[1], p_reply[2], p_reply[3]);
543 #endif
544 /* Skip transaction ID and length */
545 p_reply += 4;
546
547 BE_STREAM_TO_UINT16 (lists_byte_count, p_reply);
548 #if (SDP_DEBUG_RAW == TRUE)
549 SDP_TRACE_WARNING("lists_byte_count:%d\n", lists_byte_count);
550 #endif
551
552 /* Copy the response to the scratchpad. First, a safety check on the length */
553 if ((p_ccb->list_len + lists_byte_count) > SDP_MAX_LIST_BYTE_COUNT) {
554 sdp_disconnect (p_ccb, SDP_INVALID_PDU_SIZE);
555 return;
556 }
557
558 #if (SDP_DEBUG_RAW == TRUE)
559 SDP_TRACE_WARNING("list_len: %d, list_byte_count: %d\n",
560 p_ccb->list_len, lists_byte_count);
561 #endif
562 if (p_ccb->rsp_list == NULL) {
563 p_ccb->rsp_list = (UINT8 *)osi_malloc (SDP_MAX_LIST_BYTE_COUNT);
564 if (p_ccb->rsp_list == NULL) {
565 SDP_TRACE_ERROR ("SDP - no gki buf to save rsp\n");
566 sdp_disconnect (p_ccb, SDP_NO_RESOURCES);
567 return;
568 }
569 }
570 memcpy (&p_ccb->rsp_list[p_ccb->list_len], p_reply, lists_byte_count);
571 p_ccb->list_len += lists_byte_count;
572 p_reply += lists_byte_count;
573 #if (SDP_DEBUG_RAW == TRUE)
574 SDP_TRACE_WARNING("list_len: %d(search_attr_rsp)\n", p_ccb->list_len);
575
576 /* Check if we need to request a continuation */
577 SDP_TRACE_WARNING("*p_reply:%d(%d)\n", *p_reply, SDP_MAX_CONTINUATION_LEN);
578 #endif
579 if (*p_reply) {
580 if (*p_reply > SDP_MAX_CONTINUATION_LEN) {
581 sdp_disconnect (p_ccb, SDP_INVALID_CONT_STATE);
582 return;
583 }
584
585 cont_request_needed = TRUE;
586 }
587 }
588
589 #if (SDP_DEBUG_RAW == TRUE)
590 SDP_TRACE_WARNING("cont_request_needed:%d\n", cont_request_needed);
591 #endif
592 /* If continuation request (or first time request) */
593 if ((cont_request_needed) || (!p_reply)) {
594 BT_HDR *p_msg = (BT_HDR *) osi_malloc(SDP_DATA_BUF_SIZE);
595 UINT8 *p;
596
597 if (!p_msg) {
598 sdp_disconnect (p_ccb, SDP_NO_RESOURCES);
599 return;
600 }
601
602 p_msg->offset = L2CAP_MIN_OFFSET;
603 p = p_start = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
604
605 /* Build a service search request packet */
606 UINT8_TO_BE_STREAM (p, SDP_PDU_SERVICE_SEARCH_ATTR_REQ);
607 UINT16_TO_BE_STREAM (p, p_ccb->transaction_id);
608 p_ccb->transaction_id++;
609
610 /* Skip the length, we need to add it at the end */
611 p_param_len = p;
612 p += 2;
613
614 /* Build the UID sequence. */
615 #if (defined(SDP_BROWSE_PLUS) && SDP_BROWSE_PLUS == TRUE)
616 p = sdpu_build_uuid_seq (p, 1, &p_ccb->p_db->uuid_filters[p_ccb->cur_uuid_idx]);
617 #else
618 p = sdpu_build_uuid_seq (p, p_ccb->p_db->num_uuid_filters, p_ccb->p_db->uuid_filters);
619 #endif
620
621 /* Max attribute byte count */
622 UINT16_TO_BE_STREAM (p, sdp_cb.max_attr_list_size);
623
624 /* If no attribute filters, build a wildcard attribute sequence */
625 if (p_ccb->p_db->num_attr_filters) {
626 p = sdpu_build_attrib_seq (p, p_ccb->p_db->attr_filters, p_ccb->p_db->num_attr_filters);
627 } else {
628 p = sdpu_build_attrib_seq (p, NULL, 0);
629 }
630
631 /* No continuation for first request */
632 if (p_reply) {
633 memcpy (p, p_reply, *p_reply + 1);
634 p += *p_reply + 1;
635 } else {
636 UINT8_TO_BE_STREAM (p, 0);
637 }
638
639 /* Go back and put the parameter length into the buffer */
640 param_len = p - p_param_len - 2;
641 UINT16_TO_BE_STREAM (p_param_len, param_len);
642
643 /* Set the length of the SDP data in the buffer */
644 p_msg->len = p - p_start;
645
646
647 L2CA_DataWrite (p_ccb->connection_id, p_msg);
648
649 /* Start inactivity timer */
650 btu_start_timer (&p_ccb->timer_entry, BTU_TTYPE_SDP, SDP_INACT_TIMEOUT);
651
652 return;
653 }
654
655
656 /*******************************************************************/
657 /* We now have the full response, which is a sequence of sequences */
658 /*******************************************************************/
659
660 #if (SDP_RAW_DATA_INCLUDED == TRUE)
661 SDP_TRACE_DEBUG("process_service_search_attr_rsp\n");
662 sdp_copy_raw_data (p_ccb, TRUE);
663 #endif
664
665 p = &p_ccb->rsp_list[0];
666
667 /* The contents is a sequence of attribute sequences */
668 type = *p++;
669
670 if ((type >> 3) != DATA_ELE_SEQ_DESC_TYPE) {
671 SDP_TRACE_WARNING ("SDP - Wrong type: 0x%02x in attr_rsp\n", type);
672 return;
673 }
674 p = sdpu_get_len_from_type (p, type, &seq_len);
675
676 p_end = &p_ccb->rsp_list[p_ccb->list_len];
677
678 if ((p + seq_len) != p_end) {
679 sdp_disconnect (p_ccb, SDP_INVALID_CONT_STATE);
680 return;
681 }
682
683 while (p < p_end) {
684 p = save_attr_seq (p_ccb, p, &p_ccb->rsp_list[p_ccb->list_len]);
685 if (!p) {
686 sdp_disconnect (p_ccb, SDP_DB_FULL);
687 return;
688 }
689 }
690
691 /* Since we got everything we need, disconnect the call */
692 sdp_disconnect (p_ccb, SDP_SUCCESS);
693 }
694
695 /*******************************************************************************
696 **
697 ** Function save_attr_seq
698 **
699 ** Description This function is called when there is a response from
700 ** the server.
701 **
702 ** Returns pointer to next byte or NULL if error
703 **
704 *******************************************************************************/
save_attr_seq(tCONN_CB * p_ccb,UINT8 * p,UINT8 * p_msg_end)705 static UINT8 *save_attr_seq (tCONN_CB *p_ccb, UINT8 *p, UINT8 *p_msg_end)
706 {
707 UINT32 seq_len, attr_len;
708 UINT16 attr_id;
709 UINT8 type, *p_seq_end;
710 tSDP_DISC_REC *p_rec;
711
712 type = *p++;
713
714 if ((type >> 3) != DATA_ELE_SEQ_DESC_TYPE) {
715 SDP_TRACE_WARNING ("SDP - Wrong type: 0x%02x in attr_rsp\n", type);
716 return (NULL);
717 }
718
719 p = sdpu_get_len_from_type (p, type, &seq_len);
720 if ((p + seq_len) > p_msg_end) {
721 SDP_TRACE_WARNING ("SDP - Bad len in attr_rsp %d\n", seq_len);
722 return (NULL);
723 }
724
725 /* Create a record */
726 p_rec = add_record (p_ccb->p_db, p_ccb->device_address);
727 if (!p_rec) {
728 SDP_TRACE_WARNING ("SDP - DB full add_record\n");
729 return (NULL);
730 }
731
732 p_seq_end = p + seq_len;
733
734 while (p < p_seq_end) {
735 /* First get the attribute ID */
736 type = *p++;
737 p = sdpu_get_len_from_type (p, type, &attr_len);
738 if (((type >> 3) != UINT_DESC_TYPE) || (attr_len != 2)) {
739 SDP_TRACE_WARNING ("SDP - Bad type: 0x%02x or len: %d in attr_rsp\n", type, attr_len);
740 return (NULL);
741 }
742 BE_STREAM_TO_UINT16 (attr_id, p);
743
744 /* Now, add the attribute value */
745 p = add_attr (p, p_ccb->p_db, p_rec, attr_id, NULL, 0);
746
747 if (!p) {
748 SDP_TRACE_WARNING ("SDP - DB full add_attr\n");
749 return (NULL);
750 }
751 }
752
753 return (p);
754 }
755
756
757 /*******************************************************************************
758 **
759 ** Function add_record
760 **
761 ** Description This function allocates space for a record from the DB.
762 **
763 ** Returns pointer to next byte in data stream
764 **
765 *******************************************************************************/
add_record(tSDP_DISCOVERY_DB * p_db,BD_ADDR p_bda)766 tSDP_DISC_REC *add_record (tSDP_DISCOVERY_DB *p_db, BD_ADDR p_bda)
767 {
768 tSDP_DISC_REC *p_rec;
769
770 /* See if there is enough space in the database */
771 if (p_db->mem_free < sizeof (tSDP_DISC_REC)) {
772 return (NULL);
773 }
774
775 p_rec = (tSDP_DISC_REC *) p_db->p_free_mem;
776 p_db->p_free_mem += sizeof (tSDP_DISC_REC);
777 p_db->mem_free -= sizeof (tSDP_DISC_REC);
778
779 p_rec->p_first_attr = NULL;
780 p_rec->p_next_rec = NULL;
781
782 memcpy (p_rec->remote_bd_addr, p_bda, BD_ADDR_LEN);
783
784 /* Add the record to the end of chain */
785 if (!p_db->p_first_rec) {
786 p_db->p_first_rec = p_rec;
787 } else {
788 tSDP_DISC_REC *p_rec1 = p_db->p_first_rec;
789
790 while (p_rec1->p_next_rec) {
791 p_rec1 = p_rec1->p_next_rec;
792 }
793
794 p_rec1->p_next_rec = p_rec;
795 }
796
797 return (p_rec);
798 }
799
800 #define SDP_ADDITIONAL_LIST_MASK 0x80
801 /*******************************************************************************
802 **
803 ** Function add_attr
804 **
805 ** Description This function allocates space for an attribute from the DB
806 ** and copies the data into it.
807 **
808 ** Returns pointer to next byte in data stream
809 **
810 *******************************************************************************/
add_attr(UINT8 * p,tSDP_DISCOVERY_DB * p_db,tSDP_DISC_REC * p_rec,UINT16 attr_id,tSDP_DISC_ATTR * p_parent_attr,UINT8 nest_level)811 static UINT8 *add_attr (UINT8 *p, tSDP_DISCOVERY_DB *p_db, tSDP_DISC_REC *p_rec,
812 UINT16 attr_id, tSDP_DISC_ATTR *p_parent_attr, UINT8 nest_level)
813 {
814 tSDP_DISC_ATTR *p_attr;
815 UINT32 attr_len;
816 UINT32 total_len;
817 UINT16 attr_type;
818 UINT16 id;
819 UINT8 type;
820 UINT8 *p_end;
821 UINT8 is_additional_list = nest_level & SDP_ADDITIONAL_LIST_MASK;
822
823 nest_level &= ~(SDP_ADDITIONAL_LIST_MASK);
824
825 type = *p++;
826 p = sdpu_get_len_from_type (p, type, &attr_len);
827
828 attr_len &= SDP_DISC_ATTR_LEN_MASK;
829 attr_type = (type >> 3) & 0x0f;
830
831 /* See if there is enough space in the database */
832 if (attr_len > 4) {
833 total_len = attr_len - 4 + (UINT16)sizeof (tSDP_DISC_ATTR);
834 } else {
835 total_len = sizeof (tSDP_DISC_ATTR);
836 }
837
838 /* Ensure it is a multiple of 4 */
839 total_len = (total_len + 3) & ~3;
840
841 /* See if there is enough space in the database */
842 if (p_db->mem_free < total_len) {
843 return (NULL);
844 }
845
846 p_attr = (tSDP_DISC_ATTR *) p_db->p_free_mem;
847 p_attr->attr_id = attr_id;
848 p_attr->attr_len_type = (UINT16)attr_len | (attr_type << 12);
849 p_attr->p_next_attr = NULL;
850
851 /* Store the attribute value */
852 switch (attr_type) {
853 case UINT_DESC_TYPE:
854 if ( (is_additional_list != 0) && (attr_len == 2) ) {
855 BE_STREAM_TO_UINT16 (id, p);
856 if (id != ATTR_ID_PROTOCOL_DESC_LIST) {
857 p -= 2;
858 } else {
859 /* Reserve the memory for the attribute now, as we need to add sub-attributes */
860 p_db->p_free_mem += sizeof (tSDP_DISC_ATTR);
861 p_db->mem_free -= sizeof (tSDP_DISC_ATTR);
862 p_end = p + attr_len;
863 total_len = 0;
864
865 /* SDP_TRACE_DEBUG ("SDP - attr nest level:%d(list)", nest_level); */
866 if (nest_level >= MAX_NEST_LEVELS) {
867 SDP_TRACE_ERROR ("SDP - attr nesting too deep\n");
868 return (p_end);
869 }
870
871 /* Now, add the list entry */
872 p = add_attr (p, p_db, p_rec, ATTR_ID_PROTOCOL_DESC_LIST, p_attr, (UINT8)(nest_level + 1));
873
874 break;
875 }
876 }
877 /* Case falls through */
878
879 case TWO_COMP_INT_DESC_TYPE:
880 switch (attr_len) {
881 case 1:
882 p_attr->attr_value.v.u8 = *p++;
883 break;
884 case 2:
885 BE_STREAM_TO_UINT16 (p_attr->attr_value.v.u16, p);
886 break;
887 case 4:
888 BE_STREAM_TO_UINT32 (p_attr->attr_value.v.u32, p);
889 break;
890 default:
891 BE_STREAM_TO_ARRAY (p, p_attr->attr_value.v.array, (INT32)attr_len);
892 break;
893 }
894 break;
895
896 case UUID_DESC_TYPE:
897 switch (attr_len) {
898 case 2:
899 BE_STREAM_TO_UINT16 (p_attr->attr_value.v.u16, p);
900 break;
901 case 4:
902 BE_STREAM_TO_UINT32 (p_attr->attr_value.v.u32, p);
903 if (p_attr->attr_value.v.u32 < 0x10000) {
904 attr_len = 2;
905 p_attr->attr_len_type = (UINT16)attr_len | (attr_type << 12);
906 p_attr->attr_value.v.u16 = (UINT16) p_attr->attr_value.v.u32;
907
908 }
909 break;
910 case 16:
911 /* See if we can compress his UUID down to 16 or 32bit UUIDs */
912 if (sdpu_is_base_uuid (p)) {
913 if ((p[0] == 0) && (p[1] == 0)) {
914 p_attr->attr_len_type = (p_attr->attr_len_type & ~SDP_DISC_ATTR_LEN_MASK) | 2;
915 p += 2;
916 BE_STREAM_TO_UINT16 (p_attr->attr_value.v.u16, p);
917 p += MAX_UUID_SIZE - 4;
918 } else {
919 p_attr->attr_len_type = (p_attr->attr_len_type & ~SDP_DISC_ATTR_LEN_MASK) | 4;
920 BE_STREAM_TO_UINT32 (p_attr->attr_value.v.u32, p);
921 p += MAX_UUID_SIZE - 4;
922 }
923 } else {
924 /* coverity[overrun-local] */
925 /*
926 Event overrun-local: Overrun of static array "p_attr->attr_value.v.array" of size 4 at position 15 with index variable "ijk"
927 False-positive: SDP uses scratch buffer to hold the attribute value.
928 The actual size of tSDP_DISC_ATVAL does not matter.
929 If the array size in tSDP_DISC_ATVAL is increase, we would increase the system RAM usage unnecessarily
930 */
931 BE_STREAM_TO_ARRAY (p, p_attr->attr_value.v.array, (INT32)attr_len);
932 }
933 break;
934 default:
935 SDP_TRACE_WARNING ("SDP - bad len in UUID attr: %d\n", attr_len);
936 return (p + attr_len);
937 }
938 break;
939
940 case DATA_ELE_SEQ_DESC_TYPE:
941 case DATA_ELE_ALT_DESC_TYPE:
942 /* Reserve the memory for the attribute now, as we need to add sub-attributes */
943 p_db->p_free_mem += sizeof (tSDP_DISC_ATTR);
944 p_db->mem_free -= sizeof (tSDP_DISC_ATTR);
945 p_end = p + attr_len;
946 total_len = 0;
947
948 /* SDP_TRACE_DEBUG ("SDP - attr nest level:%d", nest_level); */
949 if (nest_level >= MAX_NEST_LEVELS) {
950 SDP_TRACE_ERROR ("SDP - attr nesting too deep\n");
951 return (p_end);
952 }
953 if (is_additional_list != 0 || attr_id == ATTR_ID_ADDITION_PROTO_DESC_LISTS) {
954 nest_level |= SDP_ADDITIONAL_LIST_MASK;
955 }
956 /* SDP_TRACE_DEBUG ("SDP - attr nest level:0x%x(finish)", nest_level); */
957
958 while (p < p_end) {
959 /* Now, add the list entry */
960 p = add_attr (p, p_db, p_rec, 0, p_attr, (UINT8)(nest_level + 1));
961
962 if (!p) {
963 return (NULL);
964 }
965 }
966 break;
967
968 case TEXT_STR_DESC_TYPE:
969 case URL_DESC_TYPE:
970 BE_STREAM_TO_ARRAY (p, p_attr->attr_value.v.array, (INT32)attr_len);
971 break;
972
973 case BOOLEAN_DESC_TYPE:
974 switch (attr_len) {
975 case 1:
976 p_attr->attr_value.v.u8 = *p++;
977 break;
978 default:
979 SDP_TRACE_WARNING ("SDP - bad len in boolean attr: %d\n", attr_len);
980 return (p + attr_len);
981 }
982 break;
983
984 default: /* switch (attr_type) */
985 break;
986 }
987
988 p_db->p_free_mem += total_len;
989 p_db->mem_free -= total_len;
990
991 /* Add the attribute to the end of the chain */
992 if (!p_parent_attr) {
993 if (!p_rec->p_first_attr) {
994 p_rec->p_first_attr = p_attr;
995 } else {
996 tSDP_DISC_ATTR *p_attr1 = p_rec->p_first_attr;
997
998 while (p_attr1->p_next_attr) {
999 p_attr1 = p_attr1->p_next_attr;
1000 }
1001
1002 p_attr1->p_next_attr = p_attr;
1003 }
1004 } else {
1005 if (!p_parent_attr->attr_value.v.p_sub_attr) {
1006 p_parent_attr->attr_value.v.p_sub_attr = p_attr;
1007 /* SDP_TRACE_DEBUG ("parent:0x%x(id:%d), ch:0x%x(id:%d)",
1008 p_parent_attr, p_parent_attr->attr_id, p_attr, p_attr->attr_id); */
1009 } else {
1010 tSDP_DISC_ATTR *p_attr1 = p_parent_attr->attr_value.v.p_sub_attr;
1011 /* SDP_TRACE_DEBUG ("parent:0x%x(id:%d), ch1:0x%x(id:%d)",
1012 p_parent_attr, p_parent_attr->attr_id, p_attr1, p_attr1->attr_id); */
1013
1014 while (p_attr1->p_next_attr) {
1015 p_attr1 = p_attr1->p_next_attr;
1016 }
1017
1018 p_attr1->p_next_attr = p_attr;
1019 /* SDP_TRACE_DEBUG ("new ch:0x%x(id:%d)", p_attr, p_attr->attr_id); */
1020 }
1021 }
1022
1023 return (p);
1024 }
1025
1026 #endif /* CLIENT_ENABLED == TRUE */
1027