]> granicus.if.org Git - esp-idf/blob - components/bt/bluedroid/stack/sdp/sdp_utils.c
component/bt: free timer resources after using them
[esp-idf] / components / bt / bluedroid / stack / sdp / sdp_utils.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 utility functions
22  *
23  ******************************************************************************/
24
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "allocator.h"
29
30 #include "bt_defs.h"
31
32 #include "bt_types.h"
33
34 #include "l2cdefs.h"
35 #include "hcidefs.h"
36 #include "hcimsgs.h"
37
38 #include "sdp_api.h"
39 #include "sdpint.h"
40
41 #include "btu.h"
42
43 #if (SDP_INCLUDED == TRUE)
44 static const UINT8  sdp_base_uuid[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
45                                        0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB
46                                       };
47
48 /*******************************************************************************
49 **
50 ** Function         sdpu_find_ccb_by_cid
51 **
52 ** Description      This function searches the CCB table for an entry with the
53 **                  passed CID.
54 **
55 ** Returns          the CCB address, or NULL if not found.
56 **
57 *******************************************************************************/
58 tCONN_CB *sdpu_find_ccb_by_cid (UINT16 cid)
59 {
60     UINT16       xx;
61     tCONN_CB     *p_ccb;
62
63     /* Look through each connection control block */
64     for (xx = 0, p_ccb = sdp_cb.ccb; xx < SDP_MAX_CONNECTIONS; xx++, p_ccb++) {
65         if ((p_ccb->con_state != SDP_STATE_IDLE) && (p_ccb->connection_id == cid)) {
66             return (p_ccb);
67         }
68     }
69
70     /* If here, not found */
71     return (NULL);
72 }
73
74
75 /*******************************************************************************
76 **
77 ** Function         sdpu_find_ccb_by_db
78 **
79 ** Description      This function searches the CCB table for an entry with the
80 **                  passed discovery db.
81 **
82 ** Returns          the CCB address, or NULL if not found.
83 **
84 *******************************************************************************/
85 tCONN_CB *sdpu_find_ccb_by_db (tSDP_DISCOVERY_DB *p_db)
86 {
87 #if SDP_CLIENT_ENABLED == TRUE
88     UINT16       xx;
89     tCONN_CB     *p_ccb;
90
91     if (p_db) {
92         /* Look through each connection control block */
93         for (xx = 0, p_ccb = sdp_cb.ccb; xx < SDP_MAX_CONNECTIONS; xx++, p_ccb++) {
94             if ((p_ccb->con_state != SDP_STATE_IDLE) && (p_ccb->p_db == p_db)) {
95                 return (p_ccb);
96             }
97         }
98     }
99 #endif
100     /* If here, not found */
101     return (NULL);
102 }
103
104
105 /*******************************************************************************
106 **
107 ** Function         sdpu_allocate_ccb
108 **
109 ** Description      This function allocates a new CCB.
110 **
111 ** Returns          CCB address, or NULL if none available.
112 **
113 *******************************************************************************/
114 tCONN_CB *sdpu_allocate_ccb (void)
115 {
116     UINT16       xx;
117     tCONN_CB     *p_ccb;
118
119     /* Look through each connection control block for a free one */
120     for (xx = 0, p_ccb = sdp_cb.ccb; xx < SDP_MAX_CONNECTIONS; xx++, p_ccb++) {
121         if (p_ccb->con_state == SDP_STATE_IDLE) {
122             btu_free_timer(&p_ccb->timer_entry);
123             memset (p_ccb, 0, sizeof (tCONN_CB));
124
125             p_ccb->timer_entry.param = (UINT32) p_ccb;
126
127             return (p_ccb);
128         }
129     }
130
131     /* If here, no free CCB found */
132     return (NULL);
133 }
134
135
136 /*******************************************************************************
137 **
138 ** Function         sdpu_release_ccb
139 **
140 ** Description      This function releases a CCB.
141 **
142 ** Returns          void
143 **
144 *******************************************************************************/
145 void sdpu_release_ccb (tCONN_CB *p_ccb)
146 {
147     /* Ensure timer is stopped and released */
148     btu_free_timer(&p_ccb->timer_entry);
149
150     /* Drop any response pointer we may be holding */
151     p_ccb->con_state = SDP_STATE_IDLE;
152 #if SDP_CLIENT_ENABLED == TRUE
153     p_ccb->is_attr_search = FALSE;
154 #endif
155
156     /* Free the response buffer */
157     if (p_ccb->rsp_list) {
158         SDP_TRACE_DEBUG("releasing SDP rsp_list\n");
159
160         osi_free(p_ccb->rsp_list);
161         p_ccb->rsp_list = NULL;
162     }
163 }
164
165
166 /*******************************************************************************
167 **
168 ** Function         sdpu_build_attrib_seq
169 **
170 ** Description      This function builds an attribute sequence from the list of
171 **                  passed attributes. It is also passed the address of the output
172 **                  buffer.
173 **
174 ** Returns          Pointer to next byte in the output buffer.
175 **
176 *******************************************************************************/
177 UINT8 *sdpu_build_attrib_seq (UINT8 *p_out, UINT16 *p_attr, UINT16 num_attrs)
178 {
179     UINT16  xx;
180
181     /* First thing is the data element header. See if the length fits 1 byte */
182     /* If no attributes, assume a 4-byte wildcard */
183     if (!p_attr) {
184         xx = 5;
185     } else {
186         xx = num_attrs * 3;
187     }
188
189     if (xx > 255) {
190         UINT8_TO_BE_STREAM  (p_out, (DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_WORD);
191         UINT16_TO_BE_STREAM (p_out, xx);
192     } else {
193         UINT8_TO_BE_STREAM (p_out, (DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
194         UINT8_TO_BE_STREAM (p_out, xx);
195     }
196
197     /* If there are no attributes specified, assume caller wants wildcard */
198     if (!p_attr) {
199         UINT8_TO_BE_STREAM  (p_out, (UINT_DESC_TYPE << 3) | SIZE_FOUR_BYTES);
200         UINT16_TO_BE_STREAM (p_out, 0);
201         UINT16_TO_BE_STREAM (p_out, 0xFFFF);
202     } else {
203         /* Loop through and put in all the attributes(s) */
204         for (xx = 0; xx < num_attrs; xx++, p_attr++) {
205             UINT8_TO_BE_STREAM  (p_out, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
206             UINT16_TO_BE_STREAM (p_out, *p_attr);
207         }
208     }
209
210     return (p_out);
211 }
212
213
214 /*******************************************************************************
215 **
216 ** Function         sdpu_build_attrib_entry
217 **
218 ** Description      This function builds an attribute entry from the passed
219 **                  attribute record. It is also passed the address of the output
220 **                  buffer.
221 **
222 ** Returns          Pointer to next byte in the output buffer.
223 **
224 *******************************************************************************/
225 UINT8 *sdpu_build_attrib_entry (UINT8 *p_out, tSDP_ATTRIBUTE *p_attr)
226 {
227     /* First, store the attribute ID. Goes as a UINT */
228     UINT8_TO_BE_STREAM  (p_out, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
229     UINT16_TO_BE_STREAM (p_out, p_attr->id);
230
231     /* the attribute is in the db record.
232      * assuming the attribute len is less than SDP_MAX_ATTR_LEN */
233     switch (p_attr->type) {
234     case TEXT_STR_DESC_TYPE:    /* 4 */
235     case DATA_ELE_SEQ_DESC_TYPE:/* 6 */
236     case DATA_ELE_ALT_DESC_TYPE:/* 7 */
237     case URL_DESC_TYPE:         /* 8 */
238 #if (SDP_MAX_ATTR_LEN > 0xFFFF)
239         if (p_attr->len > 0xFFFF) {
240             UINT8_TO_BE_STREAM (p_out, (p_attr->type << 3) | SIZE_IN_NEXT_LONG);
241             UINT32_TO_BE_STREAM (p_out, p_attr->len);
242         } else
243
244 #endif /* 0xFFFF - 0xFF */
245 #if (SDP_MAX_ATTR_LEN > 0xFF)
246             if (p_attr->len > 0xFF) {
247                 UINT8_TO_BE_STREAM (p_out, (p_attr->type << 3) | SIZE_IN_NEXT_WORD);
248                 UINT16_TO_BE_STREAM (p_out, p_attr->len);
249             } else
250
251 #endif /* 0xFF and less*/
252             {
253                 UINT8_TO_BE_STREAM (p_out, (p_attr->type << 3) | SIZE_IN_NEXT_BYTE);
254                 UINT8_TO_BE_STREAM (p_out, p_attr->len);
255             }
256
257         if (p_attr->value_ptr != NULL) {
258             ARRAY_TO_BE_STREAM (p_out, p_attr->value_ptr, (int)p_attr->len);
259         }
260
261         return (p_out);
262     }
263
264     /* Now, store the attribute value */
265     switch (p_attr->len) {
266     case 1:
267         UINT8_TO_BE_STREAM (p_out, (p_attr->type << 3) | SIZE_ONE_BYTE);
268         break;
269     case 2:
270         UINT8_TO_BE_STREAM  (p_out, (p_attr->type << 3) | SIZE_TWO_BYTES);
271         break;
272     case 4:
273         UINT8_TO_BE_STREAM  (p_out, (p_attr->type << 3) | SIZE_FOUR_BYTES);
274         break;
275     case 8:
276         UINT8_TO_BE_STREAM  (p_out, (p_attr->type << 3) | SIZE_EIGHT_BYTES);
277         break;
278     case 16:
279         UINT8_TO_BE_STREAM  (p_out, (p_attr->type << 3) | SIZE_SIXTEEN_BYTES);
280         break;
281     default:
282         UINT8_TO_BE_STREAM (p_out, (p_attr->type << 3) | SIZE_IN_NEXT_BYTE);
283         UINT8_TO_BE_STREAM (p_out, p_attr->len);
284         break;
285     }
286
287     if (p_attr->value_ptr != NULL) {
288         ARRAY_TO_BE_STREAM (p_out, p_attr->value_ptr, (int)p_attr->len);
289     }
290
291     return (p_out);
292 }
293
294
295 /*******************************************************************************
296 **
297 ** Function         sdpu_build_n_send_error
298 **
299 ** Description      This function builds and sends an error packet.
300 **
301 ** Returns          void
302 **
303 *******************************************************************************/
304 void sdpu_build_n_send_error (tCONN_CB *p_ccb, UINT16 trans_num, UINT16 error_code, char *p_error_text)
305 {
306     UINT8           *p_rsp, *p_rsp_start, *p_rsp_param_len;
307     UINT16          rsp_param_len;
308     BT_HDR          *p_buf;
309
310
311     SDP_TRACE_WARNING ("SDP - sdpu_build_n_send_error  code: 0x%x  CID: 0x%x\n",
312                        error_code, p_ccb->connection_id);
313
314     /* Get a buffer to use to build and send the packet to L2CAP */
315     if ((p_buf = (BT_HDR *)osi_malloc(SDP_DATA_BUF_SIZE)) == NULL) {
316         SDP_TRACE_ERROR ("SDP - no buf for err msg\n");
317         return;
318     }
319     p_buf->offset = L2CAP_MIN_OFFSET;
320     p_rsp = p_rsp_start = (UINT8 *)(p_buf + 1) + L2CAP_MIN_OFFSET;
321
322     UINT8_TO_BE_STREAM  (p_rsp, SDP_PDU_ERROR_RESPONSE);
323     UINT16_TO_BE_STREAM  (p_rsp, trans_num);
324
325     /* Skip the parameter length, we need to add it at the end */
326     p_rsp_param_len = p_rsp;
327     p_rsp += 2;
328
329     UINT16_TO_BE_STREAM  (p_rsp, error_code);
330
331     /* Unplugfest example traces do not have any error text */
332     if (p_error_text) {
333         ARRAY_TO_BE_STREAM (p_rsp, p_error_text, (int) strlen (p_error_text));
334     }
335
336     /* Go back and put the parameter length into the buffer */
337     rsp_param_len = p_rsp - p_rsp_param_len - 2;
338     UINT16_TO_BE_STREAM (p_rsp_param_len, rsp_param_len);
339
340     /* Set the length of the SDP data in the buffer */
341     p_buf->len = p_rsp - p_rsp_start;
342
343
344     /* Send the buffer through L2CAP */
345     L2CA_DataWrite (p_ccb->connection_id, p_buf);
346 }
347
348
349
350 /*******************************************************************************
351 **
352 ** Function         sdpu_extract_uid_seq
353 **
354 ** Description      This function extracts a UUID sequence from the passed input
355 **                  buffer, and puts it into the passed output list.
356 **
357 ** Returns          Pointer to next byte in the input buffer after the sequence.
358 **
359 *******************************************************************************/
360 UINT8 *sdpu_extract_uid_seq (UINT8 *p, UINT16 param_len, tSDP_UUID_SEQ *p_seq)
361 {
362     UINT8   *p_seq_end;
363     UINT8   descr, type, size;
364     UINT32  seq_len, uuid_len;
365
366     /* Assume none found */
367     p_seq->num_uids = 0;
368
369     /* A UID sequence is composed of a bunch of UIDs. */
370
371     BE_STREAM_TO_UINT8 (descr, p);
372     type = descr >> 3;
373     size = descr & 7;
374
375     if (type != DATA_ELE_SEQ_DESC_TYPE) {
376         return (NULL);
377     }
378
379     switch (size) {
380     case SIZE_TWO_BYTES:
381         seq_len = 2;
382         break;
383     case SIZE_FOUR_BYTES:
384         seq_len = 4;
385         break;
386     case SIZE_SIXTEEN_BYTES:
387         seq_len = 16;
388         break;
389     case SIZE_IN_NEXT_BYTE:
390         BE_STREAM_TO_UINT8 (seq_len, p);
391         break;
392     case SIZE_IN_NEXT_WORD:
393         BE_STREAM_TO_UINT16 (seq_len, p);
394         break;
395     case SIZE_IN_NEXT_LONG:
396         BE_STREAM_TO_UINT32 (seq_len, p);
397         break;
398     default:
399         return (NULL);
400     }
401
402     if (seq_len >= param_len) {
403         return (NULL);
404     }
405
406     p_seq_end = p + seq_len;
407
408     /* Loop through, extracting the UIDs */
409     for ( ; p < p_seq_end ; ) {
410         BE_STREAM_TO_UINT8 (descr, p);
411         type = descr >> 3;
412         size = descr & 7;
413
414         if (type != UUID_DESC_TYPE) {
415             return (NULL);
416         }
417
418         switch (size) {
419         case SIZE_TWO_BYTES:
420             uuid_len = 2;
421             break;
422         case SIZE_FOUR_BYTES:
423             uuid_len = 4;
424             break;
425         case SIZE_SIXTEEN_BYTES:
426             uuid_len = 16;
427             break;
428         case SIZE_IN_NEXT_BYTE:
429             BE_STREAM_TO_UINT8 (uuid_len, p);
430             break;
431         case SIZE_IN_NEXT_WORD:
432             BE_STREAM_TO_UINT16 (uuid_len, p);
433             break;
434         case SIZE_IN_NEXT_LONG:
435             BE_STREAM_TO_UINT32 (uuid_len, p);
436             break;
437         default:
438             return (NULL);
439         }
440
441         /* If UUID length is valid, copy it across */
442         if ((uuid_len == 2) || (uuid_len == 4) || (uuid_len == 16)) {
443             p_seq->uuid_entry[p_seq->num_uids].len = (UINT16) uuid_len;
444             BE_STREAM_TO_ARRAY (p, p_seq->uuid_entry[p_seq->num_uids].value, (int)uuid_len);
445             p_seq->num_uids++;
446         } else {
447             return (NULL);
448         }
449
450         /* We can only do so many */
451         if (p_seq->num_uids >= MAX_UUIDS_PER_SEQ) {
452             return (NULL);
453         }
454     }
455
456     if (p != p_seq_end) {
457         return (NULL);
458     }
459
460     return (p);
461 }
462
463
464
465 /*******************************************************************************
466 **
467 ** Function         sdpu_extract_attr_seq
468 **
469 ** Description      This function extracts an attribute sequence from the passed
470 **                  input buffer, and puts it into the passed output list.
471 **
472 ** Returns          Pointer to next byte in the input buffer after the sequence.
473 **
474 *******************************************************************************/
475 UINT8 *sdpu_extract_attr_seq (UINT8 *p, UINT16 param_len, tSDP_ATTR_SEQ *p_seq)
476 {
477     UINT8   *p_end_list;
478     UINT8   descr, type, size;
479     UINT32  list_len, attr_len;
480
481     /* Assume none found */
482     p_seq->num_attr = 0;
483
484     /* Get attribute sequence info */
485     BE_STREAM_TO_UINT8 (descr, p);
486     type = descr >> 3;
487     size = descr & 7;
488
489     if (type != DATA_ELE_SEQ_DESC_TYPE) {
490         return (p);
491     }
492
493     switch (size) {
494     case SIZE_IN_NEXT_BYTE:
495         BE_STREAM_TO_UINT8 (list_len, p);
496         break;
497
498     case SIZE_IN_NEXT_WORD:
499         BE_STREAM_TO_UINT16 (list_len, p);
500         break;
501
502     case SIZE_IN_NEXT_LONG:
503         BE_STREAM_TO_UINT32 (list_len, p);
504         break;
505
506     default:
507         return (p);
508     }
509
510     if (list_len > param_len) {
511         return (p);
512     }
513
514     p_end_list = p + list_len;
515
516     /* Loop through, extracting the attribute IDs */
517     for ( ; p < p_end_list ; ) {
518         BE_STREAM_TO_UINT8 (descr, p);
519         type = descr >> 3;
520         size = descr & 7;
521
522         if (type != UINT_DESC_TYPE) {
523             return (p);
524         }
525
526         switch (size) {
527         case SIZE_TWO_BYTES:
528             attr_len = 2;
529             break;
530         case SIZE_FOUR_BYTES:
531             attr_len = 4;
532             break;
533         case SIZE_IN_NEXT_BYTE:
534             BE_STREAM_TO_UINT8 (attr_len, p);
535             break;
536         case SIZE_IN_NEXT_WORD:
537             BE_STREAM_TO_UINT16 (attr_len, p);
538             break;
539         case SIZE_IN_NEXT_LONG:
540             BE_STREAM_TO_UINT32 (attr_len, p);
541             break;
542         default:
543             return (NULL);
544             break;
545         }
546
547         /* Attribute length must be 2-bytes or 4-bytes for a paired entry. */
548         if (attr_len == 2) {
549             BE_STREAM_TO_UINT16 (p_seq->attr_entry[p_seq->num_attr].start, p);
550             p_seq->attr_entry[p_seq->num_attr].end = p_seq->attr_entry[p_seq->num_attr].start;
551         } else if (attr_len == 4) {
552             BE_STREAM_TO_UINT16 (p_seq->attr_entry[p_seq->num_attr].start, p);
553             BE_STREAM_TO_UINT16 (p_seq->attr_entry[p_seq->num_attr].end, p);
554         } else {
555             return (NULL);
556         }
557
558         /* We can only do so many */
559         if (++p_seq->num_attr >= MAX_ATTR_PER_SEQ) {
560             return (NULL);
561         }
562     }
563
564     return (p);
565 }
566
567
568 /*******************************************************************************
569 **
570 ** Function         sdpu_get_len_from_type
571 **
572 ** Description      This function gets the length
573 **
574 ** Returns          void
575 **
576 *******************************************************************************/
577 UINT8 *sdpu_get_len_from_type (UINT8 *p, UINT8 type, UINT32 *p_len)
578 {
579     UINT8   u8;
580     UINT16  u16;
581     UINT32  u32;
582
583     switch (type & 7) {
584     case SIZE_ONE_BYTE:
585         *p_len = 1;
586         break;
587     case SIZE_TWO_BYTES:
588         *p_len = 2;
589         break;
590     case SIZE_FOUR_BYTES:
591         *p_len = 4;
592         break;
593     case SIZE_EIGHT_BYTES:
594         *p_len = 8;
595         break;
596     case SIZE_SIXTEEN_BYTES:
597         *p_len = 16;
598         break;
599     case SIZE_IN_NEXT_BYTE:
600         BE_STREAM_TO_UINT8 (u8, p);
601         *p_len = u8;
602         break;
603     case SIZE_IN_NEXT_WORD:
604         BE_STREAM_TO_UINT16 (u16, p);
605         *p_len = u16;
606         break;
607     case SIZE_IN_NEXT_LONG:
608         BE_STREAM_TO_UINT32 (u32, p);
609         *p_len = (UINT16) u32;
610         break;
611     }
612
613     return (p);
614 }
615
616
617 /*******************************************************************************
618 **
619 ** Function         sdpu_is_base_uuid
620 **
621 ** Description      This function checks a 128-bit UUID with the base to see if
622 **                  it matches. Only the last 12 bytes are compared.
623 **
624 ** Returns          TRUE if matched, else FALSE
625 **
626 *******************************************************************************/
627 BOOLEAN sdpu_is_base_uuid (UINT8 *p_uuid)
628 {
629     UINT16    xx;
630
631     for (xx = 4; xx < MAX_UUID_SIZE; xx++)
632         if (p_uuid[xx] != sdp_base_uuid[xx]) {
633             return (FALSE);
634         }
635
636     /* If here, matched */
637     return (TRUE);
638 }
639
640
641 /*******************************************************************************
642 **
643 ** Function         sdpu_compare_uuid_arrays
644 **
645 ** Description      This function compares 2 BE UUIDs. If needed, they are expanded
646 **                  to 128-bit UUIDs, then compared.
647 **
648 ** NOTE             it is assumed that the arrays are in Big Endian format
649 **
650 ** Returns          TRUE if matched, else FALSE
651 **
652 *******************************************************************************/
653 BOOLEAN sdpu_compare_uuid_arrays (UINT8 *p_uuid1, UINT32 len1, UINT8 *p_uuid2, UINT16 len2)
654 {
655     UINT8       nu1[MAX_UUID_SIZE];
656     UINT8       nu2[MAX_UUID_SIZE];
657
658     if ( ((len1 != 2) && (len1 != 4) && (len1 != 16)) ||
659             ((len2 != 2) && (len2 != 4) && (len2 != 16)) ) {
660         SDP_TRACE_ERROR("%s: invalid length\n", __func__);
661         return FALSE;
662     }
663
664     /* If lengths match, do a straight compare */
665     if (len1 == len2) {
666         if (len1 == 2) {
667             return ((p_uuid1[0] == p_uuid2[0]) && (p_uuid1[1] == p_uuid2[1]));
668         }
669         if (len1 == 4)
670             return (  (p_uuid1[0] == p_uuid2[0]) && (p_uuid1[1] == p_uuid2[1])
671                       && (p_uuid1[2] == p_uuid2[2]) && (p_uuid1[3] == p_uuid2[3]) );
672         else {
673             return (memcmp (p_uuid1, p_uuid2, (size_t)len1) == 0);
674         }
675     } else if (len1 > len2) {
676         /* If the len1 was 4-byte, (so len2 is 2-byte), compare on the fly */
677         if (len1 == 4) {
678             return ( (p_uuid1[0] == 0) && (p_uuid1[1] == 0)
679                      && (p_uuid1[2] == p_uuid2[0]) && (p_uuid1[3] == p_uuid2[1]) );
680         } else {
681             /* Normalize UUIDs to 16-byte form, then compare. Len1 must be 16 */
682             memcpy (nu1, p_uuid1,       MAX_UUID_SIZE);
683             memcpy (nu2, sdp_base_uuid, MAX_UUID_SIZE);
684
685             if (len2 == 4) {
686                 memcpy (nu2, p_uuid2, len2);
687             } else if (len2 == 2) {
688                 memcpy (nu2 + 2, p_uuid2, len2);
689             }
690
691             return (memcmp (nu1, nu2, MAX_UUID_SIZE) == 0);
692         }
693     } else {
694         /* len2 is greater than len1 */
695         /* If the len2 was 4-byte, (so len1 is 2-byte), compare on the fly */
696         if (len2 == 4) {
697             return ( (p_uuid2[0] == 0) && (p_uuid2[1] == 0)
698                      && (p_uuid2[2] == p_uuid1[0]) && (p_uuid2[3] == p_uuid1[1]) );
699         } else {
700             /* Normalize UUIDs to 16-byte form, then compare. Len1 must be 16 */
701             memcpy (nu2, p_uuid2,       MAX_UUID_SIZE);
702             memcpy (nu1, sdp_base_uuid, MAX_UUID_SIZE);
703
704             if (len1 == 4) {
705                 memcpy (nu1, p_uuid1, (size_t)len1);
706             } else if (len1 == 2) {
707                 memcpy (nu1 + 2, p_uuid1, (size_t)len1);
708             }
709
710             return (memcmp (nu1, nu2, MAX_UUID_SIZE) == 0);
711         }
712     }
713 }
714
715
716 /*******************************************************************************
717 **
718 ** Function         sdpu_compare_bt_uuids
719 **
720 ** Description      This function compares 2 BT UUID structures.
721 **
722 ** NOTE             it is assumed that BT UUID structures are compressed to the
723 **                  smallest possible UUIDs (by removing the base SDP UUID)
724 **
725 ** Returns          TRUE if matched, else FALSE
726 **
727 *******************************************************************************/
728 BOOLEAN sdpu_compare_bt_uuids (tBT_UUID *p_uuid1, tBT_UUID *p_uuid2)
729 {
730     /* Lengths must match for BT UUIDs to match */
731     if (p_uuid1->len == p_uuid2->len) {
732         if (p_uuid1->len == 2) {
733             return (p_uuid1->uu.uuid16 == p_uuid2->uu.uuid16);
734         } else if (p_uuid1->len == 4) {
735             return (p_uuid1->uu.uuid32 == p_uuid2->uu.uuid32);
736         } else if (!memcmp (p_uuid1->uu.uuid128, p_uuid2->uu.uuid128, 16)) {
737             return (TRUE);
738         }
739     }
740
741     return (FALSE);
742 }
743
744
745 /*******************************************************************************
746 **
747 ** Function         sdpu_compare_uuid_with_attr
748 **
749 ** Description      This function compares a BT UUID structure with the UUID in an
750 **                  SDP attribute record. If needed, they are expanded to 128-bit
751 **                  UUIDs, then compared.
752 **
753 ** NOTE           - it is assumed that BT UUID structures are compressed to the
754 **                  smallest possible UUIDs (by removing the base SDP UUID).
755 **                - it is also assumed that the discovery atribute is compressed
756 **                  to the smallest possible
757 **
758 ** Returns          TRUE if matched, else FALSE
759 **
760 *******************************************************************************/
761 BOOLEAN sdpu_compare_uuid_with_attr (tBT_UUID *p_btuuid, tSDP_DISC_ATTR *p_attr)
762 {
763     UINT16      attr_len = SDP_DISC_ATTR_LEN (p_attr->attr_len_type);
764
765     /* Since both UUIDs are compressed, lengths must match  */
766     if (p_btuuid->len != attr_len) {
767         return (FALSE);
768     }
769
770     if (p_btuuid->len == 2) {
771         return (BOOLEAN)(p_btuuid->uu.uuid16 == p_attr->attr_value.v.u16);
772     } else if (p_btuuid->len == 4) {
773         return (BOOLEAN)(p_btuuid->uu.uuid32 == p_attr->attr_value.v.u32);
774     }
775     /* coverity[overrun-buffer-arg] */
776     /*
777        Event overrun-buffer-arg: Overrun of static array "&p_attr->attr_value.v.array" of size 4 bytes by passing it to a function which indexes it with argument "16U" at byte position 15
778        FALSE-POSITIVE error from Coverity test tool. Please do NOT remove following comment.
779        False-positive: SDP uses scratch buffer to hold the attribute value.
780        The actual size of tSDP_DISC_ATVAL does not matter.
781        If the array size in tSDP_DISC_ATVAL is increase, we would increase the system RAM usage unnecessarily
782     */
783     else if (!memcmp (p_btuuid->uu.uuid128, (void *) p_attr->attr_value.v.array, MAX_UUID_SIZE)) {
784         return (TRUE);
785     }
786
787     return (FALSE);
788 }
789
790 /*******************************************************************************
791 **
792 ** Function         sdpu_sort_attr_list
793 **
794 ** Description      sorts a list of attributes in numeric order from lowest to
795 **                  highest to conform to SDP specification
796 **
797 ** Returns          void
798 **
799 *******************************************************************************/
800 void sdpu_sort_attr_list( UINT16 num_attr, tSDP_DISCOVERY_DB *p_db )
801 {
802     UINT16 i;
803     UINT16 x;
804
805     /* Done if no attributes to sort */
806     if (num_attr <= 1) {
807         return;
808     } else if (num_attr > SDP_MAX_ATTR_FILTERS) {
809         num_attr = SDP_MAX_ATTR_FILTERS;
810     }
811
812     num_attr--; /* for the for-loop */
813     for ( i = 0; i < num_attr; ) {
814         if ( p_db->attr_filters[i] > p_db->attr_filters[i + 1] ) {
815             /* swap the attribute IDs and start from the beginning */
816             x = p_db->attr_filters[i];
817             p_db->attr_filters[i] = p_db->attr_filters[i + 1];
818             p_db->attr_filters[i + 1] = x;
819
820             i = 0;
821         } else {
822             i++;
823         }
824     }
825 }
826
827
828 /*******************************************************************************
829 **
830 ** Function         sdpu_get_list_len
831 **
832 ** Description      gets the total list length in the sdp database for a given
833 **                  uid sequence and attr sequence
834 **
835 ** Returns          void
836 **
837 *******************************************************************************/
838 UINT16 sdpu_get_list_len(tSDP_UUID_SEQ *uid_seq, tSDP_ATTR_SEQ *attr_seq)
839 {
840     tSDP_RECORD    *p_rec;
841     UINT16 len = 0;
842     UINT16 len1;
843
844     for (p_rec = sdp_db_service_search (NULL, uid_seq); p_rec; p_rec = sdp_db_service_search (p_rec, uid_seq)) {
845         len += 3;
846
847         len1 = sdpu_get_attrib_seq_len(p_rec, attr_seq );
848
849         if (len1 != 0) {
850             len += len1;
851         } else {
852             len -= 3;
853         }
854     }
855     return len;
856 }
857
858 /*******************************************************************************
859 **
860 ** Function         sdpu_get_attrib_seq_len
861 **
862 ** Description      gets the length of the specific attributes in a given
863 **                  sdp record
864 **
865 ** Returns          void
866 **
867 *******************************************************************************/
868 UINT16 sdpu_get_attrib_seq_len(tSDP_RECORD *p_rec, tSDP_ATTR_SEQ *attr_seq)
869 {
870     tSDP_ATTRIBUTE *p_attr;
871     UINT16 len1 = 0;
872     UINT16 xx;
873     BOOLEAN is_range = FALSE;
874     UINT16 start_id = 0, end_id = 0;
875
876     for (xx = 0; xx < attr_seq->num_attr; xx++) {
877         if (is_range == FALSE) {
878             start_id = attr_seq->attr_entry[xx].start;
879             end_id = attr_seq->attr_entry[xx].end;
880         }
881         p_attr = sdp_db_find_attr_in_rec (p_rec,
882                                           start_id,
883                                           end_id);
884         if (p_attr) {
885             len1 += sdpu_get_attrib_entry_len (p_attr);
886
887             /* If doing a range, stick with this one till no more attributes found */
888             if (start_id != end_id) {
889                 /* Update for next time through */
890                 start_id = p_attr->id + 1;
891                 xx--;
892                 is_range = TRUE;
893             } else {
894                 is_range = FALSE;
895             }
896         } else {
897             is_range = FALSE;
898         }
899     }
900     return len1;
901 }
902
903 /*******************************************************************************
904 **
905 ** Function         sdpu_get_attrib_entry_len
906 **
907 ** Description      gets the length of a specific attribute
908 **
909 ** Returns          void
910 **
911 *******************************************************************************/
912 UINT16 sdpu_get_attrib_entry_len(tSDP_ATTRIBUTE *p_attr)
913 {
914     UINT16 len = 3;
915
916     /* the attribute is in the db record.
917      * assuming the attribute len is less than SDP_MAX_ATTR_LEN */
918     switch (p_attr->type) {
919     case TEXT_STR_DESC_TYPE:    /* 4 */
920     case DATA_ELE_SEQ_DESC_TYPE:/* 6 */
921     case DATA_ELE_ALT_DESC_TYPE:/* 7 */
922     case URL_DESC_TYPE:         /* 8 */
923 #if (SDP_MAX_ATTR_LEN > 0xFFFF)
924         if (p_attr->len > 0xFFFF) {
925             len += 5;
926         } else
927
928 #endif/* 0xFFFF - 0xFF */
929 #if (SDP_MAX_ATTR_LEN > 0xFF)
930             if (p_attr->len > 0xFF) {
931                 len += 3;
932             } else
933
934 #endif /* 0xFF and less*/
935             {
936                 len += 2;
937             }
938         len += p_attr->len;
939         return len;
940     }
941
942     /* Now, the attribute value */
943     switch (p_attr->len) {
944     case 1:
945     case 2:
946     case 4:
947     case 8:
948     case 16:
949         len += 1;
950         break;
951     default:
952         len += 2;
953         break;
954     }
955
956     len += p_attr->len;
957     return len;
958 }
959
960
961 /*******************************************************************************
962 **
963 ** Function         sdpu_build_partial_attrib_entry
964 **
965 ** Description      This function fills a buffer with partial attribute. It is
966 **                  assumed that the maximum size of any attribute is 256 bytes.
967 **
968 **                  p_out: output buffer
969 **                  p_attr: attribute to be copied partially into p_out
970 **                  rem_len: num bytes to copy into p_out
971 **                  offset: current start offset within the attr that needs to be copied
972 **
973 ** Returns          Pointer to next byte in the output buffer.
974 **                  offset is also updated
975 **
976 *******************************************************************************/
977 UINT8 *sdpu_build_partial_attrib_entry (UINT8 *p_out, tSDP_ATTRIBUTE *p_attr, UINT16 len, UINT16 *offset)
978 {
979     UINT8   *p_attr_buff;
980     UINT8   *p_tmp_attr;
981     size_t  len_to_copy;
982     UINT16  attr_len;
983
984     if ((p_attr_buff = (UINT8 *) osi_malloc(sizeof(UINT8) * SDP_MAX_ATTR_LEN )) == NULL) {
985         SDP_TRACE_ERROR("sdpu_build_partial_attrib_entry cannot get a buffer!\n");
986         return NULL;
987     }
988     p_tmp_attr = p_attr_buff;
989
990     sdpu_build_attrib_entry(p_tmp_attr, p_attr);
991     attr_len = sdpu_get_attrib_entry_len(p_attr);
992
993     len_to_copy = ((attr_len - *offset) < len) ? (attr_len - *offset) : len;
994
995     memcpy(p_out, &p_attr_buff[*offset], len_to_copy);
996
997     p_out = &p_out[len_to_copy];
998     *offset += len_to_copy;
999
1000     osi_free(p_attr_buff);
1001     return p_out;
1002 }
1003
1004 /*******************************************************************************
1005 **
1006 ** Function         sdpu_uuid16_to_uuid128
1007 **
1008 ** Description      This function converts UUID-16 to UUID-128 by including the base UUID
1009 **
1010 **                  uuid16: 2-byte UUID
1011 **                  p_uuid128: Expanded 128-bit UUID
1012 **
1013 ** Returns          None
1014 **
1015 *******************************************************************************/
1016
1017 void sdpu_uuid16_to_uuid128(UINT16 uuid16, UINT8 *p_uuid128)
1018 {
1019     UINT16 uuid16_bo;
1020     memset(p_uuid128, 0, 16);
1021
1022     memcpy(p_uuid128, sdp_base_uuid, MAX_UUID_SIZE);
1023     uuid16_bo = ntohs(uuid16);
1024     memcpy(p_uuid128 + 2, &uuid16_bo, sizeof(uint16_t));
1025 }
1026
1027 #endif  ///SDP_INCLUDED == TRUE