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