OSDN Git Service

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