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_common.h"
30 #include "bt_target.h"
41 #define SDP_DEBUG_RAW false
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 static void process_service_search_rsp(tCONN_CB* p_ccb, uint8_t* p_reply,
48 uint8_t* p_reply_end);
49 static void process_service_attr_rsp(tCONN_CB* p_ccb, uint8_t* p_reply,
50 uint8_t* p_reply_end);
51 static void process_service_search_attr_rsp(tCONN_CB* p_ccb, uint8_t* p_reply,
52 uint8_t* p_reply_end);
53 static uint8_t* save_attr_seq(tCONN_CB* p_ccb, uint8_t* p, uint8_t* p_msg_end);
54 static tSDP_DISC_REC* add_record(tSDP_DISCOVERY_DB* p_db, BD_ADDR p_bda);
55 static uint8_t* add_attr(uint8_t* p, tSDP_DISCOVERY_DB* p_db,
56 tSDP_DISC_REC* p_rec, uint16_t attr_id,
57 tSDP_DISC_ATTR* p_parent_attr, uint8_t nest_level);
59 /* Safety check in case we go crazy */
60 #define MAX_NEST_LEVELS 5
62 extern fixed_queue_t* btu_general_alarm_queue;
64 /*******************************************************************************
66 * Function sdpu_build_uuid_seq
68 * Description This function builds a UUID sequence from the list of
69 * passed UUIDs. It is also passed the address of the output
72 * Returns Pointer to next byte in the output buffer.
74 ******************************************************************************/
75 static uint8_t* sdpu_build_uuid_seq(uint8_t* p_out, uint16_t num_uuids,
76 tSDP_UUID* p_uuid_list) {
80 /* First thing is the data element header */
81 UINT8_TO_BE_STREAM(p_out, (DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
83 /* Remember where the length goes. Leave space for it. */
87 /* Now, loop through and put in all the UUID(s) */
88 for (xx = 0; xx < num_uuids; xx++, p_uuid_list++) {
89 if (p_uuid_list->len == 2) {
90 UINT8_TO_BE_STREAM(p_out, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES);
91 UINT16_TO_BE_STREAM(p_out, p_uuid_list->uu.uuid16);
92 } else if (p_uuid_list->len == 4) {
93 UINT8_TO_BE_STREAM(p_out, (UUID_DESC_TYPE << 3) | SIZE_FOUR_BYTES);
94 UINT32_TO_BE_STREAM(p_out, p_uuid_list->uu.uuid32);
96 UINT8_TO_BE_STREAM(p_out, (UUID_DESC_TYPE << 3) | SIZE_SIXTEEN_BYTES);
97 ARRAY_TO_BE_STREAM(p_out, p_uuid_list->uu.uuid128, p_uuid_list->len);
101 /* Now, put in the length */
102 xx = (uint16_t)(p_out - p_len - 1);
103 UINT8_TO_BE_STREAM(p_len, xx);
108 /*******************************************************************************
110 * Function sdp_snd_service_search_req
112 * Description Send a service search request to the SDP server.
116 ******************************************************************************/
117 static void sdp_snd_service_search_req(tCONN_CB* p_ccb, uint8_t cont_len,
119 uint8_t *p, *p_start, *p_param_len;
120 BT_HDR* p_cmd = (BT_HDR*)osi_malloc(SDP_DATA_BUF_SIZE);
123 /* Prepare the buffer for sending the packet to L2CAP */
124 p_cmd->offset = L2CAP_MIN_OFFSET;
125 p = p_start = (uint8_t*)(p_cmd + 1) + L2CAP_MIN_OFFSET;
127 /* Build a service search request packet */
128 UINT8_TO_BE_STREAM(p, SDP_PDU_SERVICE_SEARCH_REQ);
129 UINT16_TO_BE_STREAM(p, p_ccb->transaction_id);
130 p_ccb->transaction_id++;
132 /* Skip the length, we need to add it at the end */
136 /* Build the UID sequence. */
137 #if (SDP_BROWSE_PLUS == TRUE)
138 p = sdpu_build_uuid_seq(p, 1,
139 &p_ccb->p_db->uuid_filters[p_ccb->cur_uuid_idx]);
141 p = sdpu_build_uuid_seq(p, p_ccb->p_db->num_uuid_filters,
142 p_ccb->p_db->uuid_filters);
145 /* Set max service record count */
146 UINT16_TO_BE_STREAM(p, sdp_cb.max_recs_per_search);
148 /* Set continuation state */
149 UINT8_TO_BE_STREAM(p, cont_len);
151 /* if this is not the first request */
152 if (cont_len && p_cont) {
153 memcpy(p, p_cont, cont_len);
157 /* Go back and put the parameter length into the buffer */
158 param_len = (uint16_t)(p - p_param_len - 2);
159 UINT16_TO_BE_STREAM(p_param_len, param_len);
161 p_ccb->disc_state = SDP_DISC_WAIT_HANDLES;
163 /* Set the length of the SDP data in the buffer */
164 p_cmd->len = (uint16_t)(p - p_start);
166 #if (SDP_DEBUG_RAW == TRUE)
167 SDP_TRACE_WARNING("sdp_snd_service_search_req cont_len :%d disc_state:%d",
168 cont_len, p_ccb->disc_state);
171 L2CA_DataWrite(p_ccb->connection_id, p_cmd);
173 /* Start inactivity timer */
174 alarm_set_on_queue(p_ccb->sdp_conn_timer, SDP_INACT_TIMEOUT_MS,
175 sdp_conn_timer_timeout, p_ccb, btu_general_alarm_queue);
178 /*******************************************************************************
180 * Function sdp_disc_connected
182 * Description This function is called when an SDP discovery attempt is
187 ******************************************************************************/
188 void sdp_disc_connected(tCONN_CB* p_ccb) {
189 if (p_ccb->is_attr_search) {
190 p_ccb->disc_state = SDP_DISC_WAIT_SEARCH_ATTR;
192 process_service_search_attr_rsp(p_ccb, NULL, NULL);
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 */
198 p_ccb->num_handles = 0;
199 sdp_snd_service_search_req(p_ccb, 0, NULL);
203 /*******************************************************************************
205 * Function sdp_disc_server_rsp
207 * Description This function is called when there is a response from
212 ******************************************************************************/
213 void sdp_disc_server_rsp(tCONN_CB* p_ccb, BT_HDR* p_msg) {
215 bool invalid_pdu = true;
217 #if (SDP_DEBUG_RAW == TRUE)
218 SDP_TRACE_WARNING("sdp_disc_server_rsp disc_state:%d", p_ccb->disc_state);
221 /* stop inactivity timer when we receive a response */
222 alarm_cancel(p_ccb->sdp_conn_timer);
224 /* Got a reply!! Check what we got back */
225 p = (uint8_t*)(p_msg + 1) + p_msg->offset;
226 uint8_t* p_end = p + p_msg->len;
228 BE_STREAM_TO_UINT8(rsp_pdu, p);
233 case SDP_PDU_SERVICE_SEARCH_RSP:
234 if (p_ccb->disc_state == SDP_DISC_WAIT_HANDLES) {
235 process_service_search_rsp(p_ccb, p, p_end);
240 case SDP_PDU_SERVICE_ATTR_RSP:
241 if (p_ccb->disc_state == SDP_DISC_WAIT_ATTR) {
242 process_service_attr_rsp(p_ccb, p, p_end);
247 case SDP_PDU_SERVICE_SEARCH_ATTR_RSP:
248 if (p_ccb->disc_state == SDP_DISC_WAIT_SEARCH_ATTR) {
249 process_service_search_attr_rsp(p_ccb, p, p_end);
256 SDP_TRACE_WARNING("SDP - Unexp. PDU: %d in state: %d", rsp_pdu,
258 sdp_disconnect(p_ccb, SDP_GENERIC_ERROR);
262 /******************************************************************************
264 * Function process_service_search_rsp
266 * Description This function is called when there is a search response from
271 ******************************************************************************/
272 static void process_service_search_rsp(tCONN_CB* p_ccb, uint8_t* p_reply,
273 uint8_t* p_reply_end) {
275 uint16_t total, cur_handles, orig;
278 /* Skip transaction, and param len */
280 BE_STREAM_TO_UINT16(total, p_reply);
281 BE_STREAM_TO_UINT16(cur_handles, p_reply);
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");
287 sdp_disconnect(p_ccb, SDP_NO_RECS_MATCH);
291 /* Save the handles that match. We will can only process a certain number. */
292 if (total > sdp_cb.max_recs_per_search) total = sdp_cb.max_recs_per_search;
293 if (p_ccb->num_handles > sdp_cb.max_recs_per_search)
294 p_ccb->num_handles = sdp_cb.max_recs_per_search;
296 for (xx = orig; xx < p_ccb->num_handles; xx++)
297 BE_STREAM_TO_UINT32(p_ccb->handles[xx], p_reply);
299 BE_STREAM_TO_UINT8(cont_len, p_reply);
301 if (cont_len > SDP_MAX_CONTINUATION_LEN) {
302 sdp_disconnect(p_ccb, SDP_INVALID_CONT_STATE);
305 if (p_reply + cont_len > p_reply_end) {
306 android_errorWriteLog(0x534e4554, "68161546");
307 sdp_disconnect(p_ccb, SDP_INVALID_CONT_STATE);
310 /* stay in the same state */
311 sdp_snd_service_search_req(p_ccb, cont_len, p_reply);
314 p_ccb->disc_state = SDP_DISC_WAIT_ATTR;
316 /* Kick off the first attribute request */
317 process_service_attr_rsp(p_ccb, NULL, NULL);
321 /*******************************************************************************
323 * Function sdp_copy_raw_data
325 * Description copy the raw data
330 ******************************************************************************/
331 #if (SDP_RAW_DATA_INCLUDED == TRUE)
332 static void sdp_copy_raw_data(tCONN_CB* p_ccb, bool offset) {
333 unsigned int cpy_len;
338 #if (SDP_DEBUG_RAW == TRUE)
339 uint8_t num_array[SDP_MAX_LIST_BYTE_COUNT];
342 for (i = 0; i < p_ccb->list_len; i++) {
343 snprintf((char*)&num_array[i * 2], sizeof(num_array) - i * 2, "%02X",
344 (uint8_t)(p_ccb->rsp_list[i]));
346 SDP_TRACE_WARNING("result :%s", num_array);
349 if (p_ccb->p_db->raw_data) {
350 cpy_len = p_ccb->p_db->raw_size - p_ccb->p_db->raw_used;
351 list_len = p_ccb->list_len;
352 p = &p_ccb->rsp_list[0];
356 p = sdpu_get_len_from_type(p, type, &list_len);
358 if (list_len < cpy_len) {
362 "%s: list_len:%d cpy_len:%d p:%p p_ccb:%p p_db:%p raw_size:%d "
363 "raw_used:%d raw_data:%p",
364 __func__, list_len, cpy_len, p, p_ccb, p_ccb->p_db,
365 p_ccb->p_db->raw_size, p_ccb->p_db->raw_used, p_ccb->p_db->raw_data);
366 memcpy(&p_ccb->p_db->raw_data[p_ccb->p_db->raw_used], p, cpy_len);
367 p_ccb->p_db->raw_used += cpy_len;
372 /*******************************************************************************
374 * Function process_service_attr_rsp
376 * Description This function is called when there is a attribute response
381 ******************************************************************************/
382 static void process_service_attr_rsp(tCONN_CB* p_ccb, uint8_t* p_reply,
383 uint8_t* p_reply_end) {
384 uint8_t *p_start, *p_param_len;
385 uint16_t param_len, list_byte_count;
386 bool cont_request_needed = false;
388 #if (SDP_DEBUG_RAW == TRUE)
389 SDP_TRACE_WARNING("process_service_attr_rsp raw inc:%d",
390 SDP_RAW_DATA_INCLUDED);
392 /* If p_reply is NULL, we were called after the records handles were read */
394 #if (SDP_DEBUG_RAW == TRUE)
395 SDP_TRACE_WARNING("ID & len: 0x%02x-%02x-%02x-%02x", p_reply[0], p_reply[1],
396 p_reply[2], p_reply[3]);
398 /* Skip transaction ID and length */
401 BE_STREAM_TO_UINT16(list_byte_count, p_reply);
402 #if (SDP_DEBUG_RAW == TRUE)
403 SDP_TRACE_WARNING("list_byte_count:%d", list_byte_count);
406 /* Copy the response to the scratchpad. First, a safety check on the length
408 if ((p_ccb->list_len + list_byte_count) > SDP_MAX_LIST_BYTE_COUNT) {
409 sdp_disconnect(p_ccb, SDP_INVALID_PDU_SIZE);
413 #if (SDP_DEBUG_RAW == TRUE)
414 SDP_TRACE_WARNING("list_len: %d, list_byte_count: %d", p_ccb->list_len,
417 if (p_ccb->rsp_list == NULL)
418 p_ccb->rsp_list = (uint8_t*)osi_malloc(SDP_MAX_LIST_BYTE_COUNT);
419 memcpy(&p_ccb->rsp_list[p_ccb->list_len], p_reply, list_byte_count);
420 p_ccb->list_len += list_byte_count;
421 p_reply += list_byte_count;
422 #if (SDP_DEBUG_RAW == TRUE)
423 SDP_TRACE_WARNING("list_len: %d(attr_rsp)", p_ccb->list_len);
425 /* Check if we need to request a continuation */
426 SDP_TRACE_WARNING("*p_reply:%d(%d)", *p_reply, SDP_MAX_CONTINUATION_LEN);
429 if (*p_reply > SDP_MAX_CONTINUATION_LEN) {
430 sdp_disconnect(p_ccb, SDP_INVALID_CONT_STATE);
433 cont_request_needed = true;
435 #if (SDP_RAW_DATA_INCLUDED == TRUE)
436 SDP_TRACE_WARNING("process_service_attr_rsp");
437 sdp_copy_raw_data(p_ccb, false);
440 /* Save the response in the database. Stop on any error */
441 if (!save_attr_seq(p_ccb, &p_ccb->rsp_list[0],
442 &p_ccb->rsp_list[p_ccb->list_len])) {
443 sdp_disconnect(p_ccb, SDP_DB_FULL);
451 /* Now, ask for the next handle. Re-use the buffer we just got. */
452 if (p_ccb->cur_handle < p_ccb->num_handles) {
453 BT_HDR* p_msg = (BT_HDR*)osi_malloc(SDP_DATA_BUF_SIZE);
456 p_msg->offset = L2CAP_MIN_OFFSET;
457 p = p_start = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
459 /* Get all the attributes from the server */
460 UINT8_TO_BE_STREAM(p, SDP_PDU_SERVICE_ATTR_REQ);
461 UINT16_TO_BE_STREAM(p, p_ccb->transaction_id);
462 p_ccb->transaction_id++;
464 /* Skip the length, we need to add it at the end */
468 UINT32_TO_BE_STREAM(p, p_ccb->handles[p_ccb->cur_handle]);
470 /* Max attribute byte count */
471 UINT16_TO_BE_STREAM(p, sdp_cb.max_attr_list_size);
473 /* If no attribute filters, build a wildcard attribute sequence */
474 if (p_ccb->p_db->num_attr_filters)
475 p = sdpu_build_attrib_seq(p, p_ccb->p_db->attr_filters,
476 p_ccb->p_db->num_attr_filters);
478 p = sdpu_build_attrib_seq(p, NULL, 0);
480 /* Was this a continuation request ? */
481 if (cont_request_needed) {
482 if ((p_reply + *p_reply + 1) <= p_reply_end) {
483 memcpy(p, p_reply, *p_reply + 1);
486 android_errorWriteLog(0x534e4554, "68161546");
489 UINT8_TO_BE_STREAM(p, 0);
491 /* Go back and put the parameter length into the buffer */
492 param_len = (uint16_t)(p - p_param_len - 2);
493 UINT16_TO_BE_STREAM(p_param_len, param_len);
495 /* Set the length of the SDP data in the buffer */
496 p_msg->len = (uint16_t)(p - p_start);
498 L2CA_DataWrite(p_ccb->connection_id, p_msg);
500 /* Start inactivity timer */
501 alarm_set_on_queue(p_ccb->sdp_conn_timer, SDP_INACT_TIMEOUT_MS,
502 sdp_conn_timer_timeout, p_ccb, btu_general_alarm_queue);
504 sdp_disconnect(p_ccb, SDP_SUCCESS);
509 /*******************************************************************************
511 * Function process_service_search_attr_rsp
513 * Description This function is called when there is a search attribute
514 * response from the server.
518 ******************************************************************************/
519 static void process_service_search_attr_rsp(tCONN_CB* p_ccb, uint8_t* p_reply,
520 uint8_t* p_reply_end) {
521 uint8_t *p, *p_start, *p_end, *p_param_len;
524 uint16_t param_len, lists_byte_count = 0;
525 bool cont_request_needed = false;
527 #if (SDP_DEBUG_RAW == TRUE)
528 SDP_TRACE_WARNING("process_service_search_attr_rsp");
530 /* If p_reply is NULL, we were called for the initial read */
532 #if (SDP_DEBUG_RAW == TRUE)
533 SDP_TRACE_WARNING("ID & len: 0x%02x-%02x-%02x-%02x", p_reply[0], p_reply[1],
534 p_reply[2], p_reply[3]);
536 /* Skip transaction ID and length */
539 BE_STREAM_TO_UINT16(lists_byte_count, p_reply);
540 #if (SDP_DEBUG_RAW == TRUE)
541 SDP_TRACE_WARNING("lists_byte_count:%d", lists_byte_count);
544 /* Copy the response to the scratchpad. First, a safety check on the length
546 if ((p_ccb->list_len + lists_byte_count) > SDP_MAX_LIST_BYTE_COUNT) {
547 sdp_disconnect(p_ccb, SDP_INVALID_PDU_SIZE);
551 #if (SDP_DEBUG_RAW == TRUE)
552 SDP_TRACE_WARNING("list_len: %d, list_byte_count: %d", p_ccb->list_len,
555 if (p_ccb->rsp_list == NULL)
556 p_ccb->rsp_list = (uint8_t*)osi_malloc(SDP_MAX_LIST_BYTE_COUNT);
557 memcpy(&p_ccb->rsp_list[p_ccb->list_len], p_reply, lists_byte_count);
558 p_ccb->list_len += lists_byte_count;
559 p_reply += lists_byte_count;
560 #if (SDP_DEBUG_RAW == TRUE)
561 SDP_TRACE_WARNING("list_len: %d(search_attr_rsp)", p_ccb->list_len);
563 /* Check if we need to request a continuation */
564 SDP_TRACE_WARNING("*p_reply:%d(%d)", *p_reply, SDP_MAX_CONTINUATION_LEN);
567 if (*p_reply > SDP_MAX_CONTINUATION_LEN) {
568 sdp_disconnect(p_ccb, SDP_INVALID_CONT_STATE);
572 cont_request_needed = true;
576 #if (SDP_DEBUG_RAW == TRUE)
577 SDP_TRACE_WARNING("cont_request_needed:%d", cont_request_needed);
579 /* If continuation request (or first time request) */
580 if ((cont_request_needed) || (!p_reply)) {
581 BT_HDR* p_msg = (BT_HDR*)osi_malloc(SDP_DATA_BUF_SIZE);
584 p_msg->offset = L2CAP_MIN_OFFSET;
585 p = p_start = (uint8_t*)(p_msg + 1) + L2CAP_MIN_OFFSET;
587 /* Build a service search request packet */
588 UINT8_TO_BE_STREAM(p, SDP_PDU_SERVICE_SEARCH_ATTR_REQ);
589 UINT16_TO_BE_STREAM(p, p_ccb->transaction_id);
590 p_ccb->transaction_id++;
592 /* Skip the length, we need to add it at the end */
596 /* Build the UID sequence. */
597 #if (SDP_BROWSE_PLUS == TRUE)
598 p = sdpu_build_uuid_seq(p, 1,
599 &p_ccb->p_db->uuid_filters[p_ccb->cur_uuid_idx]);
601 p = sdpu_build_uuid_seq(p, p_ccb->p_db->num_uuid_filters,
602 p_ccb->p_db->uuid_filters);
605 /* Max attribute byte count */
606 UINT16_TO_BE_STREAM(p, sdp_cb.max_attr_list_size);
608 /* If no attribute filters, build a wildcard attribute sequence */
609 if (p_ccb->p_db->num_attr_filters)
610 p = sdpu_build_attrib_seq(p, p_ccb->p_db->attr_filters,
611 p_ccb->p_db->num_attr_filters);
613 p = sdpu_build_attrib_seq(p, NULL, 0);
615 /* No continuation for first request */
617 if ((p_reply + *p_reply + 1) <= p_reply_end) {
618 memcpy(p, p_reply, *p_reply + 1);
621 android_errorWriteLog(0x534e4554, "68161546");
624 UINT8_TO_BE_STREAM(p, 0);
626 /* Go back and put the parameter length into the buffer */
627 param_len = p - p_param_len - 2;
628 UINT16_TO_BE_STREAM(p_param_len, param_len);
630 /* Set the length of the SDP data in the buffer */
631 p_msg->len = p - p_start;
633 L2CA_DataWrite(p_ccb->connection_id, p_msg);
635 /* Start inactivity timer */
636 alarm_set_on_queue(p_ccb->sdp_conn_timer, SDP_INACT_TIMEOUT_MS,
637 sdp_conn_timer_timeout, p_ccb, btu_general_alarm_queue);
642 /*******************************************************************/
643 /* We now have the full response, which is a sequence of sequences */
644 /*******************************************************************/
646 #if (SDP_RAW_DATA_INCLUDED == TRUE)
647 SDP_TRACE_WARNING("process_service_search_attr_rsp");
648 sdp_copy_raw_data(p_ccb, true);
651 p = &p_ccb->rsp_list[0];
653 /* The contents is a sequence of attribute sequences */
656 if ((type >> 3) != DATA_ELE_SEQ_DESC_TYPE) {
657 SDP_TRACE_WARNING("SDP - Wrong type: 0x%02x in attr_rsp", type);
660 p = sdpu_get_len_from_type(p, type, &seq_len);
662 p_end = &p_ccb->rsp_list[p_ccb->list_len];
664 if ((p + seq_len) != p_end) {
665 sdp_disconnect(p_ccb, SDP_INVALID_CONT_STATE);
670 p = save_attr_seq(p_ccb, p, &p_ccb->rsp_list[p_ccb->list_len]);
672 sdp_disconnect(p_ccb, SDP_DB_FULL);
677 /* Since we got everything we need, disconnect the call */
678 sdp_disconnect(p_ccb, SDP_SUCCESS);
681 /*******************************************************************************
683 * Function save_attr_seq
685 * Description This function is called when there is a response from
688 * Returns pointer to next byte or NULL if error
690 ******************************************************************************/
691 static uint8_t* save_attr_seq(tCONN_CB* p_ccb, uint8_t* p, uint8_t* p_msg_end) {
692 uint32_t seq_len, attr_len;
694 uint8_t type, *p_seq_end;
695 tSDP_DISC_REC* p_rec;
699 if ((type >> 3) != DATA_ELE_SEQ_DESC_TYPE) {
700 SDP_TRACE_WARNING("SDP - Wrong type: 0x%02x in attr_rsp", type);
704 p = sdpu_get_len_from_type(p, type, &seq_len);
705 if ((p + seq_len) > p_msg_end) {
706 SDP_TRACE_WARNING("SDP - Bad len in attr_rsp %d", seq_len);
710 /* Create a record */
711 p_rec = add_record(p_ccb->p_db, p_ccb->device_address);
713 SDP_TRACE_WARNING("SDP - DB full add_record");
717 p_seq_end = p + seq_len;
719 while (p < p_seq_end) {
720 /* First get the attribute ID */
722 p = sdpu_get_len_from_type(p, type, &attr_len);
723 if (((type >> 3) != UINT_DESC_TYPE) || (attr_len != 2)) {
724 SDP_TRACE_WARNING("SDP - Bad type: 0x%02x or len: %d in attr_rsp", type,
728 BE_STREAM_TO_UINT16(attr_id, p);
730 /* Now, add the attribute value */
731 p = add_attr(p, p_ccb->p_db, p_rec, attr_id, NULL, 0);
734 SDP_TRACE_WARNING("SDP - DB full add_attr");
742 /*******************************************************************************
744 * Function add_record
746 * Description This function allocates space for a record from the DB.
748 * Returns pointer to next byte in data stream
750 ******************************************************************************/
751 tSDP_DISC_REC* add_record(tSDP_DISCOVERY_DB* p_db, BD_ADDR p_bda) {
752 tSDP_DISC_REC* p_rec;
754 /* See if there is enough space in the database */
755 if (p_db->mem_free < sizeof(tSDP_DISC_REC)) return (NULL);
757 p_rec = (tSDP_DISC_REC*)p_db->p_free_mem;
758 p_db->p_free_mem += sizeof(tSDP_DISC_REC);
759 p_db->mem_free -= sizeof(tSDP_DISC_REC);
761 p_rec->p_first_attr = NULL;
762 p_rec->p_next_rec = NULL;
764 memcpy(p_rec->remote_bd_addr, p_bda, BD_ADDR_LEN);
766 /* Add the record to the end of chain */
767 if (!p_db->p_first_rec)
768 p_db->p_first_rec = p_rec;
770 tSDP_DISC_REC* p_rec1 = p_db->p_first_rec;
772 while (p_rec1->p_next_rec) p_rec1 = p_rec1->p_next_rec;
774 p_rec1->p_next_rec = p_rec;
780 #define SDP_ADDITIONAL_LIST_MASK 0x80
781 /*******************************************************************************
785 * Description This function allocates space for an attribute from the DB
786 * and copies the data into it.
788 * Returns pointer to next byte in data stream
790 ******************************************************************************/
791 static uint8_t* add_attr(uint8_t* p, tSDP_DISCOVERY_DB* p_db,
792 tSDP_DISC_REC* p_rec, uint16_t attr_id,
793 tSDP_DISC_ATTR* p_parent_attr, uint8_t nest_level) {
794 tSDP_DISC_ATTR* p_attr;
801 uint8_t is_additional_list = nest_level & SDP_ADDITIONAL_LIST_MASK;
803 nest_level &= ~(SDP_ADDITIONAL_LIST_MASK);
806 p = sdpu_get_len_from_type(p, type, &attr_len);
808 attr_len &= SDP_DISC_ATTR_LEN_MASK;
809 attr_type = (type >> 3) & 0x0f;
811 /* See if there is enough space in the database */
813 total_len = attr_len - 4 + (uint16_t)sizeof(tSDP_DISC_ATTR);
815 total_len = sizeof(tSDP_DISC_ATTR);
817 /* Ensure it is a multiple of 4 */
818 total_len = (total_len + 3) & ~3;
820 /* See if there is enough space in the database */
821 if (p_db->mem_free < total_len) return (NULL);
823 p_attr = (tSDP_DISC_ATTR*)p_db->p_free_mem;
824 p_attr->attr_id = attr_id;
825 p_attr->attr_len_type = (uint16_t)attr_len | (attr_type << 12);
826 p_attr->p_next_attr = NULL;
828 /* Store the attribute value */
831 if ((is_additional_list != 0) && (attr_len == 2)) {
832 BE_STREAM_TO_UINT16(id, p);
833 if (id != ATTR_ID_PROTOCOL_DESC_LIST)
836 /* Reserve the memory for the attribute now, as we need to add
838 p_db->p_free_mem += sizeof(tSDP_DISC_ATTR);
839 p_db->mem_free -= sizeof(tSDP_DISC_ATTR);
840 p_end = p + attr_len;
843 /* SDP_TRACE_DEBUG ("SDP - attr nest level:%d(list)", nest_level); */
844 if (nest_level >= MAX_NEST_LEVELS) {
845 SDP_TRACE_ERROR("SDP - attr nesting too deep");
849 /* Now, add the list entry */
850 p = add_attr(p, p_db, p_rec, ATTR_ID_PROTOCOL_DESC_LIST, p_attr,
851 (uint8_t)(nest_level + 1));
856 /* Case falls through */
858 case TWO_COMP_INT_DESC_TYPE:
861 p_attr->attr_value.v.u8 = *p++;
864 BE_STREAM_TO_UINT16(p_attr->attr_value.v.u16, p);
867 BE_STREAM_TO_UINT32(p_attr->attr_value.v.u32, p);
870 BE_STREAM_TO_ARRAY(p, p_attr->attr_value.v.array, (int32_t)attr_len);
878 BE_STREAM_TO_UINT16(p_attr->attr_value.v.u16, p);
881 BE_STREAM_TO_UINT32(p_attr->attr_value.v.u32, p);
882 if (p_attr->attr_value.v.u32 < 0x10000) {
884 p_attr->attr_len_type = (uint16_t)attr_len | (attr_type << 12);
885 p_attr->attr_value.v.u16 = (uint16_t)p_attr->attr_value.v.u32;
889 /* See if we can compress his UUID down to 16 or 32bit UUIDs */
890 if (sdpu_is_base_uuid(p)) {
891 if ((p[0] == 0) && (p[1] == 0)) {
892 p_attr->attr_len_type =
893 (p_attr->attr_len_type & ~SDP_DISC_ATTR_LEN_MASK) | 2;
895 BE_STREAM_TO_UINT16(p_attr->attr_value.v.u16, p);
896 p += MAX_UUID_SIZE - 4;
898 p_attr->attr_len_type =
899 (p_attr->attr_len_type & ~SDP_DISC_ATTR_LEN_MASK) | 4;
900 BE_STREAM_TO_UINT32(p_attr->attr_value.v.u32, p);
901 p += MAX_UUID_SIZE - 4;
904 BE_STREAM_TO_ARRAY(p, p_attr->attr_value.v.array,
909 SDP_TRACE_WARNING("SDP - bad len in UUID attr: %d", attr_len);
910 return (p + attr_len);
914 case DATA_ELE_SEQ_DESC_TYPE:
915 case DATA_ELE_ALT_DESC_TYPE:
916 /* Reserve the memory for the attribute now, as we need to add
918 p_db->p_free_mem += sizeof(tSDP_DISC_ATTR);
919 p_db->mem_free -= sizeof(tSDP_DISC_ATTR);
920 p_end = p + attr_len;
923 /* SDP_TRACE_DEBUG ("SDP - attr nest level:%d", nest_level); */
924 if (nest_level >= MAX_NEST_LEVELS) {
925 SDP_TRACE_ERROR("SDP - attr nesting too deep");
928 if (is_additional_list != 0 ||
929 attr_id == ATTR_ID_ADDITION_PROTO_DESC_LISTS)
930 nest_level |= SDP_ADDITIONAL_LIST_MASK;
931 /* SDP_TRACE_DEBUG ("SDP - attr nest level:0x%x(finish)", nest_level); */
934 /* Now, add the list entry */
935 p = add_attr(p, p_db, p_rec, 0, p_attr, (uint8_t)(nest_level + 1));
937 if (!p) return (NULL);
941 case TEXT_STR_DESC_TYPE:
943 BE_STREAM_TO_ARRAY(p, p_attr->attr_value.v.array, (int32_t)attr_len);
946 case BOOLEAN_DESC_TYPE:
949 p_attr->attr_value.v.u8 = *p++;
952 SDP_TRACE_WARNING("SDP - bad len in boolean attr: %d", attr_len);
953 return (p + attr_len);
957 default: /* switch (attr_type) */
961 p_db->p_free_mem += total_len;
962 p_db->mem_free -= total_len;
964 /* Add the attribute to the end of the chain */
965 if (!p_parent_attr) {
966 if (!p_rec->p_first_attr)
967 p_rec->p_first_attr = p_attr;
969 tSDP_DISC_ATTR* p_attr1 = p_rec->p_first_attr;
971 while (p_attr1->p_next_attr) p_attr1 = p_attr1->p_next_attr;
973 p_attr1->p_next_attr = p_attr;
976 if (!p_parent_attr->attr_value.v.p_sub_attr) {
977 p_parent_attr->attr_value.v.p_sub_attr = p_attr;
978 /* SDP_TRACE_DEBUG ("parent:0x%x(id:%d), ch:0x%x(id:%d)",
979 p_parent_attr, p_parent_attr->attr_id, p_attr, p_attr->attr_id); */
981 tSDP_DISC_ATTR* p_attr1 = p_parent_attr->attr_value.v.p_sub_attr;
982 /* SDP_TRACE_DEBUG ("parent:0x%x(id:%d), ch1:0x%x(id:%d)",
983 p_parent_attr, p_parent_attr->attr_id, p_attr1, p_attr1->attr_id); */
985 while (p_attr1->p_next_attr) p_attr1 = p_attr1->p_next_attr;
987 p_attr1->p_next_attr = p_attr;
988 /* SDP_TRACE_DEBUG ("new ch:0x%x(id:%d)", p_attr, p_attr->attr_id); */