OSDN Git Service

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