OSDN Git Service

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