1 /******************************************************************************
3 * Copyright (C) 1999-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /******************************************************************************
21 * this file contains SDP discovery functions
23 ******************************************************************************/
29 #include "bt_target.h"
30 #include "bt_common.h"
42 #define SDP_DEBUG_RAW FALSE
45 /********************************************************************************/
46 /* L O C A L F U N C T I O N P R O T O T Y P E S */
47 /********************************************************************************/
48 #if SDP_CLIENT_ENABLED == TRUE
49 static void process_service_search_rsp(tCONN_CB* p_ccb, uint8_t* p_reply,
50 uint8_t* p_reply_end);
51 static void process_service_attr_rsp(tCONN_CB* p_ccb, uint8_t* p_reply,
52 uint8_t* p_reply_end);
53 static void process_service_search_attr_rsp(tCONN_CB* p_ccb, uint8_t* p_reply,
54 uint8_t* p_reply_end);
55 static UINT8 *save_attr_seq (tCONN_CB *p_ccb, UINT8 *p, UINT8 *p_msg_end);
56 static tSDP_DISC_REC *add_record (tSDP_DISCOVERY_DB *p_db, BD_ADDR p_bda);
57 static UINT8 *add_attr (UINT8 *p, UINT8 *p_end, tSDP_DISCOVERY_DB *p_db, tSDP_DISC_REC *p_rec,
58 UINT16 attr_id, tSDP_DISC_ATTR *p_parent_attr, UINT8 nest_level);
60 /* Safety check in case we go crazy */
61 #define MAX_NEST_LEVELS 5
63 extern fixed_queue_t *btu_general_alarm_queue;
65 /*******************************************************************************
67 ** Function sdpu_build_uuid_seq
69 ** Description This function builds a UUID sequence from the list of
70 ** passed UUIDs. It is also passed the address of the output
73 ** Returns Pointer to next byte in the output buffer.
75 *******************************************************************************/
76 static UINT8 *sdpu_build_uuid_seq (UINT8 *p_out, UINT16 num_uuids, tSDP_UUID *p_uuid_list)
81 /* First thing is the data element header */
82 UINT8_TO_BE_STREAM (p_out, (DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
84 /* Remember where the length goes. Leave space for it. */
88 /* Now, loop through and put in all the UUID(s) */
89 for (xx = 0; xx < num_uuids; xx++, p_uuid_list++)
91 if (p_uuid_list->len == 2)
93 UINT8_TO_BE_STREAM (p_out, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES);
94 UINT16_TO_BE_STREAM (p_out, p_uuid_list->uu.uuid16);
96 else if (p_uuid_list->len == 4)
98 UINT8_TO_BE_STREAM (p_out, (UUID_DESC_TYPE << 3) | SIZE_FOUR_BYTES);
99 UINT32_TO_BE_STREAM (p_out, p_uuid_list->uu.uuid32);
103 UINT8_TO_BE_STREAM (p_out, (UUID_DESC_TYPE << 3) | SIZE_SIXTEEN_BYTES);
104 ARRAY_TO_BE_STREAM (p_out, p_uuid_list->uu.uuid128, p_uuid_list->len);
108 /* Now, put in the length */
109 xx = (UINT16)(p_out - p_len - 1);
110 UINT8_TO_BE_STREAM (p_len, xx);
115 /*******************************************************************************
117 ** Function sdp_snd_service_search_req
119 ** Description Send a service search request to the SDP server.
123 *******************************************************************************/
124 static void sdp_snd_service_search_req(tCONN_CB *p_ccb, UINT8 cont_len, UINT8 * p_cont)
126 UINT8 *p, *p_start, *p_param_len;
127 BT_HDR *p_cmd = (BT_HDR *) osi_malloc(SDP_DATA_BUF_SIZE);
130 /* Prepare the buffer for sending the packet to L2CAP */
131 p_cmd->offset = L2CAP_MIN_OFFSET;
132 p = p_start = (UINT8 *)(p_cmd + 1) + L2CAP_MIN_OFFSET;
134 /* Build a service search request packet */
135 UINT8_TO_BE_STREAM (p, SDP_PDU_SERVICE_SEARCH_REQ);
136 UINT16_TO_BE_STREAM (p, p_ccb->transaction_id);
137 p_ccb->transaction_id++;
139 /* Skip the length, we need to add it at the end */
143 /* Build the UID sequence. */
144 #if (defined(SDP_BROWSE_PLUS) && SDP_BROWSE_PLUS == TRUE)
145 p = sdpu_build_uuid_seq (p, 1, &p_ccb->p_db->uuid_filters[p_ccb->cur_uuid_idx]);
147 p = sdpu_build_uuid_seq (p, p_ccb->p_db->num_uuid_filters, p_ccb->p_db->uuid_filters);
150 /* Set max service record count */
151 UINT16_TO_BE_STREAM (p, sdp_cb.max_recs_per_search);
153 /* Set continuation state */
154 UINT8_TO_BE_STREAM (p, cont_len);
156 /* if this is not the first request */
157 if(cont_len && p_cont)
159 memcpy(p, p_cont, cont_len);
163 /* Go back and put the parameter length into the buffer */
164 param_len = (UINT16)(p - p_param_len - 2);
165 UINT16_TO_BE_STREAM (p_param_len, param_len);
167 p_ccb->disc_state = SDP_DISC_WAIT_HANDLES;
169 /* Set the length of the SDP data in the buffer */
170 p_cmd->len = (UINT16)(p - p_start);
172 #if (SDP_DEBUG_RAW == TRUE)
173 SDP_TRACE_WARNING("sdp_snd_service_search_req cont_len :%d disc_state:%d",cont_len, p_ccb->disc_state);
177 L2CA_DataWrite (p_ccb->connection_id, p_cmd);
179 /* Start inactivity timer */
180 alarm_set_on_queue(p_ccb->sdp_conn_timer, SDP_INACT_TIMEOUT_MS,
181 sdp_conn_timer_timeout, p_ccb, btu_general_alarm_queue);
184 /*******************************************************************************
186 ** Function sdp_disc_connected
188 ** Description This function is called when an SDP discovery attempt is
193 *******************************************************************************/
194 void sdp_disc_connected (tCONN_CB *p_ccb)
196 if (p_ccb->is_attr_search)
198 p_ccb->disc_state = SDP_DISC_WAIT_SEARCH_ATTR;
200 process_service_search_attr_rsp(p_ccb, NULL, NULL);
204 /* First step is to get a list of the handles from the server. */
205 /* We are not searching for a specific attribute, so we will */
206 /* first search for the service, then get all attributes of it */
208 p_ccb->num_handles = 0;
209 sdp_snd_service_search_req(p_ccb, 0, NULL);
214 /*******************************************************************************
216 ** Function sdp_disc_server_rsp
218 ** Description This function is called when there is a response from
223 *******************************************************************************/
224 void sdp_disc_server_rsp (tCONN_CB *p_ccb, BT_HDR *p_msg)
227 BOOLEAN invalid_pdu = TRUE;
229 #if (SDP_DEBUG_RAW == TRUE)
230 SDP_TRACE_WARNING("sdp_disc_server_rsp disc_state:%d", p_ccb->disc_state);
233 /* stop inactivity timer when we receive a response */
234 alarm_cancel(p_ccb->sdp_conn_timer);
236 /* Got a reply!! Check what we got back */
237 p = (UINT8 *)(p_msg + 1) + p_msg->offset;
238 uint8_t* p_end = p + p_msg->len;
240 BE_STREAM_TO_UINT8 (rsp_pdu, p);
246 case SDP_PDU_SERVICE_SEARCH_RSP:
247 if (p_ccb->disc_state == SDP_DISC_WAIT_HANDLES)
249 process_service_search_rsp(p_ccb, p, p_end);
254 case SDP_PDU_SERVICE_ATTR_RSP:
255 if (p_ccb->disc_state == SDP_DISC_WAIT_ATTR)
257 process_service_attr_rsp(p_ccb, p, p_end);
262 case SDP_PDU_SERVICE_SEARCH_ATTR_RSP:
263 if (p_ccb->disc_state == SDP_DISC_WAIT_SEARCH_ATTR)
265 process_service_search_attr_rsp(p_ccb, p, p_end);
273 SDP_TRACE_WARNING ("SDP - Unexp. PDU: %d in state: %d", rsp_pdu, p_ccb->disc_state);
274 sdp_disconnect (p_ccb, SDP_GENERIC_ERROR);
278 /******************************************************************************
280 ** Function process_service_search_rsp
282 ** Description This function is called when there is a search response from
287 *******************************************************************************/
288 static void process_service_search_rsp(tCONN_CB *p_ccb, UINT8 *p_reply,
289 uint8_t* p_reply_end) {
291 UINT16 total, cur_handles, orig;
294 if (p_reply + 8 > p_reply_end) {
295 android_errorWriteLog(0x534e4554, "74249842");
296 sdp_disconnect(p_ccb, SDP_GENERIC_ERROR);
299 /* Skip transaction, and param len */
301 BE_STREAM_TO_UINT16 (total, p_reply);
302 BE_STREAM_TO_UINT16 (cur_handles, p_reply);
304 orig = p_ccb->num_handles;
305 p_ccb->num_handles += cur_handles;
306 if (p_ccb->num_handles == 0)
308 SDP_TRACE_WARNING ("SDP - Rcvd ServiceSearchRsp, no matches");
309 sdp_disconnect (p_ccb, SDP_NO_RECS_MATCH);
313 /* Save the handles that match. We will can only process a certain number. */
314 if (total > sdp_cb.max_recs_per_search)
315 total = sdp_cb.max_recs_per_search;
316 if (p_ccb->num_handles > sdp_cb.max_recs_per_search)
317 p_ccb->num_handles = sdp_cb.max_recs_per_search;
319 if (p_reply + ((p_ccb->num_handles - orig) * 4) + 1 > p_reply_end) {
320 android_errorWriteLog(0x534e4554, "74249842");
321 sdp_disconnect(p_ccb, SDP_GENERIC_ERROR);
325 for (xx = orig; xx < p_ccb->num_handles; xx++)
326 BE_STREAM_TO_UINT32 (p_ccb->handles[xx], p_reply);
328 BE_STREAM_TO_UINT8 (cont_len, p_reply);
331 if(cont_len > SDP_MAX_CONTINUATION_LEN)
333 sdp_disconnect (p_ccb, SDP_INVALID_CONT_STATE);
336 if (p_reply + cont_len > p_reply_end) {
337 android_errorWriteLog(0x534e4554, "68161546");
338 sdp_disconnect(p_ccb, SDP_INVALID_CONT_STATE);
341 /* stay in the same state */
342 sdp_snd_service_search_req(p_ccb, cont_len, p_reply);
347 p_ccb->disc_state = SDP_DISC_WAIT_ATTR;
349 /* Kick off the first attribute request */
350 process_service_attr_rsp(p_ccb, NULL, NULL);
354 /*******************************************************************************
356 ** Function sdp_copy_raw_data
358 ** Description copy the raw data
363 *******************************************************************************/
364 #if (SDP_RAW_DATA_INCLUDED == TRUE)
365 static void sdp_copy_raw_data (tCONN_CB *p_ccb, BOOLEAN offset)
367 unsigned int cpy_len, rem_len;
372 #if (SDP_DEBUG_RAW == TRUE)
373 UINT8 num_array[SDP_MAX_LIST_BYTE_COUNT];
376 for (i = 0; i < p_ccb->list_len; i++)
378 sprintf((char *)&num_array[i*2],"%02X",(UINT8)(p_ccb->rsp_list[i]));
380 SDP_TRACE_WARNING("result :%s",num_array);
383 if(p_ccb->p_db->raw_data)
385 cpy_len = p_ccb->p_db->raw_size - p_ccb->p_db->raw_used;
386 list_len = p_ccb->list_len;
387 p = &p_ccb->rsp_list[0];
394 p = sdpu_get_len_from_type (p, type, &list_len);
395 if ((int)cpy_len < (p - old_p)) {
396 SDP_TRACE_WARNING("%s: no bytes left for data", __func__);
399 cpy_len -= (p - old_p);
401 if(list_len < cpy_len )
405 rem_len = SDP_MAX_LIST_BYTE_COUNT - (unsigned int)(p - &p_ccb->rsp_list[0]);
406 if (cpy_len > rem_len) {
407 SDP_TRACE_WARNING("rem_len :%d less than cpy_len:%d", rem_len, cpy_len);
411 "%s: list_len:%d cpy_len:%d p:%p p_ccb:%p p_db:%p raw_size:%d "
412 "raw_used:%d raw_data:%p",
413 __func__, list_len, cpy_len, p, p_ccb, p_ccb->p_db,
414 p_ccb->p_db->raw_size, p_ccb->p_db->raw_used, p_ccb->p_db->raw_data);
415 memcpy (&p_ccb->p_db->raw_data[p_ccb->p_db->raw_used], p, cpy_len);
416 p_ccb->p_db->raw_used += cpy_len;
421 /*******************************************************************************
423 ** Function process_service_attr_rsp
425 ** Description This function is called when there is a attribute response from
430 *******************************************************************************/
431 static void process_service_attr_rsp(tCONN_CB *p_ccb, UINT8 *p_reply,
432 uint8_t* p_reply_end) {
433 UINT8 *p_start, *p_param_len;
434 UINT16 param_len, list_byte_count;
435 BOOLEAN cont_request_needed = FALSE;
437 #if (SDP_DEBUG_RAW == TRUE)
438 SDP_TRACE_WARNING("process_service_attr_rsp raw inc:%d",
439 SDP_RAW_DATA_INCLUDED);
441 /* If p_reply is NULL, we were called after the records handles were read */
444 #if (SDP_DEBUG_RAW == TRUE)
445 SDP_TRACE_WARNING("ID & len: 0x%02x-%02x-%02x-%02x",
446 p_reply[0], p_reply[1], p_reply[2], p_reply[3]);
448 /* Skip transaction ID and length */
451 BE_STREAM_TO_UINT16 (list_byte_count, p_reply);
452 #if (SDP_DEBUG_RAW == TRUE)
453 SDP_TRACE_WARNING("list_byte_count:%d", list_byte_count);
456 /* Copy the response to the scratchpad. First, a safety check on the length */
457 if ((p_ccb->list_len + list_byte_count) > SDP_MAX_LIST_BYTE_COUNT)
459 sdp_disconnect (p_ccb, SDP_INVALID_PDU_SIZE);
463 #if (SDP_DEBUG_RAW == TRUE)
464 SDP_TRACE_WARNING("list_len: %d, list_byte_count: %d",
465 p_ccb->list_len, list_byte_count);
467 if (p_ccb->rsp_list == NULL)
468 p_ccb->rsp_list = (UINT8 *)osi_malloc(SDP_MAX_LIST_BYTE_COUNT);
469 memcpy(&p_ccb->rsp_list[p_ccb->list_len], p_reply, list_byte_count);
470 p_ccb->list_len += list_byte_count;
471 p_reply += list_byte_count;
472 #if (SDP_DEBUG_RAW == TRUE)
473 SDP_TRACE_WARNING("list_len: %d(attr_rsp)", p_ccb->list_len);
475 /* Check if we need to request a continuation */
476 SDP_TRACE_WARNING("*p_reply:%d(%d)", *p_reply, SDP_MAX_CONTINUATION_LEN);
480 if (*p_reply > SDP_MAX_CONTINUATION_LEN)
482 sdp_disconnect (p_ccb, SDP_INVALID_CONT_STATE);
485 cont_request_needed = TRUE;
490 #if (SDP_RAW_DATA_INCLUDED == TRUE)
491 SDP_TRACE_WARNING("process_service_attr_rsp");
492 sdp_copy_raw_data (p_ccb, FALSE);
495 /* Save the response in the database. Stop on any error */
496 if (!save_attr_seq (p_ccb, &p_ccb->rsp_list[0], &p_ccb->rsp_list[p_ccb->list_len]))
498 sdp_disconnect (p_ccb, SDP_DB_FULL);
506 /* Now, ask for the next handle. Re-use the buffer we just got. */
507 if (p_ccb->cur_handle < p_ccb->num_handles)
509 BT_HDR *p_msg = (BT_HDR *)osi_malloc(SDP_DATA_BUF_SIZE);
512 p_msg->offset = L2CAP_MIN_OFFSET;
513 p = p_start = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
515 /* Get all the attributes from the server */
516 UINT8_TO_BE_STREAM (p, SDP_PDU_SERVICE_ATTR_REQ);
517 UINT16_TO_BE_STREAM (p, p_ccb->transaction_id);
518 p_ccb->transaction_id++;
520 /* Skip the length, we need to add it at the end */
524 UINT32_TO_BE_STREAM (p, p_ccb->handles[p_ccb->cur_handle]);
526 /* Max attribute byte count */
527 UINT16_TO_BE_STREAM (p, sdp_cb.max_attr_list_size);
529 /* If no attribute filters, build a wildcard attribute sequence */
530 if (p_ccb->p_db->num_attr_filters)
531 p = sdpu_build_attrib_seq (p, p_ccb->p_db->attr_filters, p_ccb->p_db->num_attr_filters);
533 p = sdpu_build_attrib_seq (p, NULL, 0);
535 /* Was this a continuation request ? */
536 if (cont_request_needed)
538 if ((p_reply + *p_reply + 1) <= p_reply_end) {
539 memcpy(p, p_reply, *p_reply + 1);
542 android_errorWriteLog(0x534e4554, "68161546");
546 UINT8_TO_BE_STREAM (p, 0);
548 /* Go back and put the parameter length into the buffer */
549 param_len = (UINT16)(p - p_param_len - 2);
550 UINT16_TO_BE_STREAM (p_param_len, param_len);
552 /* Set the length of the SDP data in the buffer */
553 p_msg->len = (UINT16)(p - p_start);
556 L2CA_DataWrite (p_ccb->connection_id, p_msg);
558 /* Start inactivity timer */
559 alarm_set_on_queue(p_ccb->sdp_conn_timer, SDP_INACT_TIMEOUT_MS,
560 sdp_conn_timer_timeout, p_ccb,
561 btu_general_alarm_queue);
565 sdp_disconnect (p_ccb, SDP_SUCCESS);
571 /*******************************************************************************
573 ** Function process_service_search_attr_rsp
575 ** Description This function is called when there is a search attribute
576 ** response from the server.
580 *******************************************************************************/
581 static void process_service_search_attr_rsp(tCONN_CB *p_ccb, UINT8 *p_reply,
582 uint8_t* p_reply_end) {
583 UINT8 *p, *p_start, *p_end, *p_param_len;
586 UINT16 param_len, lists_byte_count = 0;
587 BOOLEAN cont_request_needed = FALSE;
589 #if (SDP_DEBUG_RAW == TRUE)
590 SDP_TRACE_WARNING("process_service_search_attr_rsp");
592 /* If p_reply is NULL, we were called for the initial read */
595 if (p_reply + 4 /* transaction ID and length */ + sizeof(lists_byte_count) >
597 android_errorWriteLog(0x534e4554, "79884292");
598 sdp_disconnect(p_ccb, SDP_INVALID_PDU_SIZE);
602 #if (SDP_DEBUG_RAW == TRUE)
603 SDP_TRACE_WARNING("ID & len: 0x%02x-%02x-%02x-%02x",
604 p_reply[0], p_reply[1], p_reply[2], p_reply[3]);
606 /* Skip transaction ID and length */
609 BE_STREAM_TO_UINT16 (lists_byte_count, p_reply);
610 #if (SDP_DEBUG_RAW == TRUE)
611 SDP_TRACE_WARNING("lists_byte_count:%d", lists_byte_count);
614 /* Copy the response to the scratchpad. First, a safety check on the length */
615 if ((p_ccb->list_len + lists_byte_count) > SDP_MAX_LIST_BYTE_COUNT)
617 sdp_disconnect (p_ccb, SDP_INVALID_PDU_SIZE);
621 #if (SDP_DEBUG_RAW == TRUE)
622 SDP_TRACE_WARNING("list_len: %d, list_byte_count: %d",
623 p_ccb->list_len, lists_byte_count);
626 if (p_reply + lists_byte_count + 1 /* continuation */ > p_reply_end) {
627 android_errorWriteLog(0x534e4554, "79884292");
628 sdp_disconnect(p_ccb, SDP_INVALID_PDU_SIZE);
632 if (p_ccb->rsp_list == NULL)
633 p_ccb->rsp_list = (UINT8 *)osi_malloc(SDP_MAX_LIST_BYTE_COUNT);
634 memcpy (&p_ccb->rsp_list[p_ccb->list_len], p_reply, lists_byte_count);
635 p_ccb->list_len += lists_byte_count;
636 p_reply += lists_byte_count;
637 #if (SDP_DEBUG_RAW == TRUE)
638 SDP_TRACE_WARNING("list_len: %d(search_attr_rsp)", p_ccb->list_len);
640 /* Check if we need to request a continuation */
641 SDP_TRACE_WARNING("*p_reply:%d(%d)", *p_reply, SDP_MAX_CONTINUATION_LEN);
645 if (*p_reply > SDP_MAX_CONTINUATION_LEN)
647 sdp_disconnect (p_ccb, SDP_INVALID_CONT_STATE);
651 cont_request_needed = TRUE;
655 #if (SDP_DEBUG_RAW == TRUE)
656 SDP_TRACE_WARNING("cont_request_needed:%d", cont_request_needed);
658 /* If continuation request (or first time request) */
659 if ((cont_request_needed) || (!p_reply))
661 BT_HDR *p_msg = (BT_HDR *)osi_malloc(SDP_DATA_BUF_SIZE);
664 p_msg->offset = L2CAP_MIN_OFFSET;
665 p = p_start = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
667 /* Build a service search request packet */
668 UINT8_TO_BE_STREAM (p, SDP_PDU_SERVICE_SEARCH_ATTR_REQ);
669 UINT16_TO_BE_STREAM (p, p_ccb->transaction_id);
670 p_ccb->transaction_id++;
672 /* Skip the length, we need to add it at the end */
676 /* Build the UID sequence. */
677 #if (defined(SDP_BROWSE_PLUS) && SDP_BROWSE_PLUS == TRUE)
678 p = sdpu_build_uuid_seq (p, 1, &p_ccb->p_db->uuid_filters[p_ccb->cur_uuid_idx]);
680 p = sdpu_build_uuid_seq (p, p_ccb->p_db->num_uuid_filters, p_ccb->p_db->uuid_filters);
683 /* Max attribute byte count */
684 UINT16_TO_BE_STREAM (p, sdp_cb.max_attr_list_size);
686 /* If no attribute filters, build a wildcard attribute sequence */
687 if (p_ccb->p_db->num_attr_filters)
688 p = sdpu_build_attrib_seq (p, p_ccb->p_db->attr_filters, p_ccb->p_db->num_attr_filters);
690 p = sdpu_build_attrib_seq (p, NULL, 0);
692 /* No continuation for first request */
695 if ((p_reply + *p_reply + 1) <= p_reply_end) {
696 memcpy(p, p_reply, *p_reply + 1);
699 android_errorWriteLog(0x534e4554, "68161546");
703 UINT8_TO_BE_STREAM (p, 0);
705 /* Go back and put the parameter length into the buffer */
706 param_len = p - p_param_len - 2;
707 UINT16_TO_BE_STREAM (p_param_len, param_len);
709 /* Set the length of the SDP data in the buffer */
710 p_msg->len = p - p_start;
713 L2CA_DataWrite (p_ccb->connection_id, p_msg);
715 /* Start inactivity timer */
716 alarm_set_on_queue(p_ccb->sdp_conn_timer, SDP_INACT_TIMEOUT_MS,
717 sdp_conn_timer_timeout, p_ccb,
718 btu_general_alarm_queue);
724 /*******************************************************************/
725 /* We now have the full response, which is a sequence of sequences */
726 /*******************************************************************/
728 #if (SDP_RAW_DATA_INCLUDED == TRUE)
729 SDP_TRACE_WARNING("process_service_search_attr_rsp");
730 sdp_copy_raw_data (p_ccb, TRUE);
733 p = &p_ccb->rsp_list[0];
735 /* The contents is a sequence of attribute sequences */
738 if ((type >> 3) != DATA_ELE_SEQ_DESC_TYPE)
740 SDP_TRACE_WARNING ("SDP - Wrong type: 0x%02x in attr_rsp", type);
743 p = sdpu_get_len_from_type (p, type, &seq_len);
745 p_end = &p_ccb->rsp_list[p_ccb->list_len];
747 if ((p + seq_len) != p_end)
749 sdp_disconnect (p_ccb, SDP_INVALID_CONT_STATE);
755 p = save_attr_seq (p_ccb, p, &p_ccb->rsp_list[p_ccb->list_len]);
758 sdp_disconnect (p_ccb, SDP_DB_FULL);
763 /* Since we got everything we need, disconnect the call */
764 sdp_disconnect (p_ccb, SDP_SUCCESS);
767 /*******************************************************************************
769 ** Function save_attr_seq
771 ** Description This function is called when there is a response from
774 ** Returns pointer to next byte or NULL if error
776 *******************************************************************************/
777 static UINT8 *save_attr_seq (tCONN_CB *p_ccb, UINT8 *p, UINT8 *p_msg_end)
779 UINT32 seq_len, attr_len;
781 UINT8 type, *p_seq_end;
782 tSDP_DISC_REC *p_rec;
786 if ((type >> 3) != DATA_ELE_SEQ_DESC_TYPE)
788 SDP_TRACE_WARNING ("SDP - Wrong type: 0x%02x in attr_rsp", type);
792 p = sdpu_get_len_from_type (p, type, &seq_len);
793 if ((p + seq_len) > p_msg_end)
795 SDP_TRACE_WARNING ("SDP - Bad len in attr_rsp %d", seq_len);
799 /* Create a record */
800 p_rec = add_record (p_ccb->p_db, p_ccb->device_address);
803 SDP_TRACE_WARNING ("SDP - DB full add_record");
807 p_seq_end = p + seq_len;
809 while (p < p_seq_end)
811 /* First get the attribute ID */
813 p = sdpu_get_len_from_type (p, type, &attr_len);
814 if (((type >> 3) != UINT_DESC_TYPE) || (attr_len != 2))
816 SDP_TRACE_WARNING ("SDP - Bad type: 0x%02x or len: %d in attr_rsp", type, attr_len);
819 BE_STREAM_TO_UINT16 (attr_id, p);
821 /* Now, add the attribute value */
822 p = add_attr (p, p_seq_end, p_ccb->p_db, p_rec, attr_id, NULL, 0);
826 SDP_TRACE_WARNING ("SDP - DB full add_attr");
835 /*******************************************************************************
837 ** Function add_record
839 ** Description This function allocates space for a record from the DB.
841 ** Returns pointer to next byte in data stream
843 *******************************************************************************/
844 tSDP_DISC_REC *add_record (tSDP_DISCOVERY_DB *p_db, BD_ADDR p_bda)
846 tSDP_DISC_REC *p_rec;
848 /* See if there is enough space in the database */
849 if (p_db->mem_free < sizeof (tSDP_DISC_REC))
852 p_rec = (tSDP_DISC_REC *) p_db->p_free_mem;
853 p_db->p_free_mem += sizeof (tSDP_DISC_REC);
854 p_db->mem_free -= sizeof (tSDP_DISC_REC);
856 p_rec->p_first_attr = NULL;
857 p_rec->p_next_rec = NULL;
859 memcpy (p_rec->remote_bd_addr, p_bda, BD_ADDR_LEN);
861 /* Add the record to the end of chain */
862 if (!p_db->p_first_rec)
863 p_db->p_first_rec = p_rec;
866 tSDP_DISC_REC *p_rec1 = p_db->p_first_rec;
868 while (p_rec1->p_next_rec)
869 p_rec1 = p_rec1->p_next_rec;
871 p_rec1->p_next_rec = p_rec;
877 #define SDP_ADDITIONAL_LIST_MASK 0x80
878 /*******************************************************************************
882 ** Description This function allocates space for an attribute from the DB
883 ** and copies the data into it.
885 ** Returns pointer to next byte in data stream
887 *******************************************************************************/
888 static UINT8 *add_attr (UINT8 *p, UINT8 *p_end, tSDP_DISCOVERY_DB *p_db, tSDP_DISC_REC *p_rec,
889 UINT16 attr_id, tSDP_DISC_ATTR *p_parent_attr, UINT8 nest_level)
891 tSDP_DISC_ATTR *p_attr;
898 UINT8 is_additional_list = nest_level & SDP_ADDITIONAL_LIST_MASK;
900 nest_level &= ~(SDP_ADDITIONAL_LIST_MASK);
903 p = sdpu_get_len_from_type (p, type, &attr_len);
905 attr_len &= SDP_DISC_ATTR_LEN_MASK;
906 attr_type = (type >> 3) & 0x0f;
908 /* See if there is enough space in the database */
910 total_len = attr_len - 4 + (UINT16)sizeof (tSDP_DISC_ATTR);
912 total_len = sizeof (tSDP_DISC_ATTR);
914 p_attr_end = p + attr_len;
915 if (p_attr_end > p_end) {
916 android_errorWriteLog(0x534e4554, "115900043");
917 SDP_TRACE_WARNING("%s: SDP - Attribute length beyond p_end", __func__);
921 /* Ensure it is a multiple of 4 */
922 total_len = (total_len + 3) & ~3;
924 /* See if there is enough space in the database */
925 if (p_db->mem_free < total_len)
928 p_attr = (tSDP_DISC_ATTR *) p_db->p_free_mem;
929 p_attr->attr_id = attr_id;
930 p_attr->attr_len_type = (UINT16)attr_len | (attr_type << 12);
931 p_attr->p_next_attr = NULL;
933 /* Store the attribute value */
937 if( (is_additional_list != 0) && (attr_len == 2) )
939 BE_STREAM_TO_UINT16 (id, p);
940 if(id != ATTR_ID_PROTOCOL_DESC_LIST)
944 /* Reserve the memory for the attribute now, as we need to add sub-attributes */
945 p_db->p_free_mem += sizeof (tSDP_DISC_ATTR);
946 p_db->mem_free -= sizeof (tSDP_DISC_ATTR);
949 /* SDP_TRACE_DEBUG ("SDP - attr nest level:%d(list)", nest_level); */
950 if (nest_level >= MAX_NEST_LEVELS)
952 SDP_TRACE_ERROR ("SDP - attr nesting too deep");
956 /* Now, add the list entry */
957 p = add_attr (p, p_end, p_db, p_rec, ATTR_ID_PROTOCOL_DESC_LIST, p_attr, (UINT8)(nest_level + 1));
962 /* Case falls through */
964 case TWO_COMP_INT_DESC_TYPE:
968 p_attr->attr_value.v.u8 = *p++;
971 BE_STREAM_TO_UINT16 (p_attr->attr_value.v.u16, p);
974 BE_STREAM_TO_UINT32 (p_attr->attr_value.v.u32, p);
977 BE_STREAM_TO_ARRAY (p, p_attr->attr_value.v.array, (INT32)attr_len);
986 BE_STREAM_TO_UINT16 (p_attr->attr_value.v.u16, p);
989 BE_STREAM_TO_UINT32 (p_attr->attr_value.v.u32, p);
990 if (p_attr->attr_value.v.u32 < 0x10000)
993 p_attr->attr_len_type = (UINT16)attr_len | (attr_type << 12);
994 p_attr->attr_value.v.u16 = (UINT16) p_attr->attr_value.v.u32;
999 /* See if we can compress his UUID down to 16 or 32bit UUIDs */
1000 if (sdpu_is_base_uuid (p))
1002 if ((p[0] == 0) && (p[1] == 0))
1004 p_attr->attr_len_type = (p_attr->attr_len_type & ~SDP_DISC_ATTR_LEN_MASK) | 2;
1006 BE_STREAM_TO_UINT16 (p_attr->attr_value.v.u16, p);
1007 p += MAX_UUID_SIZE - 4;
1011 p_attr->attr_len_type = (p_attr->attr_len_type & ~SDP_DISC_ATTR_LEN_MASK) | 4;
1012 BE_STREAM_TO_UINT32 (p_attr->attr_value.v.u32, p);
1013 p += MAX_UUID_SIZE - 4;
1018 /* coverity[overrun-local] */
1020 Event overrun-local: Overrun of static array "p_attr->attr_value.v.array" of size 4 at position 15 with index variable "ijk"
1021 False-positive: SDP uses scratch buffer to hold the attribute value.
1022 The actual size of tSDP_DISC_ATVAL does not matter.
1023 If the array size in tSDP_DISC_ATVAL is increase, we would increase the system RAM usage unnecessarily
1025 BE_STREAM_TO_ARRAY (p, p_attr->attr_value.v.array, (INT32)attr_len);
1029 SDP_TRACE_WARNING ("SDP - bad len in UUID attr: %d", attr_len);
1034 case DATA_ELE_SEQ_DESC_TYPE:
1035 case DATA_ELE_ALT_DESC_TYPE:
1036 /* Reserve the memory for the attribute now, as we need to add sub-attributes */
1037 p_db->p_free_mem += sizeof (tSDP_DISC_ATTR);
1038 p_db->mem_free -= sizeof (tSDP_DISC_ATTR);
1041 /* SDP_TRACE_DEBUG ("SDP - attr nest level:%d", nest_level); */
1042 if (nest_level >= MAX_NEST_LEVELS)
1044 SDP_TRACE_ERROR ("SDP - attr nesting too deep");
1047 if(is_additional_list != 0 || attr_id == ATTR_ID_ADDITION_PROTO_DESC_LISTS)
1048 nest_level |= SDP_ADDITIONAL_LIST_MASK;
1049 /* SDP_TRACE_DEBUG ("SDP - attr nest level:0x%x(finish)", nest_level); */
1051 while (p < p_attr_end)
1053 /* Now, add the list entry */
1054 p = add_attr (p, p_end, p_db, p_rec, 0, p_attr, (UINT8)(nest_level + 1));
1061 case TEXT_STR_DESC_TYPE:
1063 BE_STREAM_TO_ARRAY (p, p_attr->attr_value.v.array, (INT32)attr_len);
1066 case BOOLEAN_DESC_TYPE:
1070 p_attr->attr_value.v.u8 = *p++;
1073 SDP_TRACE_WARNING ("SDP - bad len in boolean attr: %d", attr_len);
1078 default: /* switch (attr_type) */
1082 p_db->p_free_mem += total_len;
1083 p_db->mem_free -= total_len;
1085 /* Add the attribute to the end of the chain */
1088 if (!p_rec->p_first_attr)
1089 p_rec->p_first_attr = p_attr;
1092 tSDP_DISC_ATTR *p_attr1 = p_rec->p_first_attr;
1094 while (p_attr1->p_next_attr)
1095 p_attr1 = p_attr1->p_next_attr;
1097 p_attr1->p_next_attr = p_attr;
1102 if (!p_parent_attr->attr_value.v.p_sub_attr)
1104 p_parent_attr->attr_value.v.p_sub_attr = p_attr;
1105 /* SDP_TRACE_DEBUG ("parent:0x%x(id:%d), ch:0x%x(id:%d)",
1106 p_parent_attr, p_parent_attr->attr_id, p_attr, p_attr->attr_id); */
1110 tSDP_DISC_ATTR *p_attr1 = p_parent_attr->attr_value.v.p_sub_attr;
1111 /* SDP_TRACE_DEBUG ("parent:0x%x(id:%d), ch1:0x%x(id:%d)",
1112 p_parent_attr, p_parent_attr->attr_id, p_attr1, p_attr1->attr_id); */
1114 while (p_attr1->p_next_attr)
1115 p_attr1 = p_attr1->p_next_attr;
1117 p_attr1->p_next_attr = p_attr;
1118 /* SDP_TRACE_DEBUG ("new ch:0x%x(id:%d)", p_attr, p_attr->attr_id); */
1125 #endif /* CLIENT_ENABLED == TRUE */