1 /******************************************************************************
2  *
3  *  Copyright (C) 2017, The Linux Foundation.
4  *  Copyright 1999-2012 Broadcom Corporation
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at:
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  ******************************************************************************/
19 
20 /******************************************************************************
21  *
22  *  this file contains functions that handle the database
23  *
24  ******************************************************************************/
25 
26 #define LOG_TAG "stack::sdp"
27 
28 #include <bluetooth/log.h>
29 #include <string.h>
30 
31 #include <cstdint>
32 
33 #include "internal_include/bt_target.h"
34 #include "osi/include/allocator.h"
35 #include "stack/include/bt_types.h"
36 #include "stack/include/bt_uuid16.h"
37 #include "stack/include/sdpdefs.h"
38 #include "stack/sdp/internal/sdp_api.h"
39 #include "stack/sdp/sdp_discovery_db.h"
40 #include "stack/sdp/sdpint.h"
41 
42 using namespace bluetooth;
43 
44 /*******************************************************************************
45  *
46  * Function         find_uuid_in_seq
47  *
48  * Description      This function searches a data element sequenct for a UUID.
49  *
50  * Returns          true if found, else false
51  *
52  ******************************************************************************/
find_uuid_in_seq(uint8_t * p,uint32_t seq_len,const uint8_t * p_uuid,uint16_t uuid_len,int nest_level)53 static bool find_uuid_in_seq(uint8_t* p, uint32_t seq_len, const uint8_t* p_uuid, uint16_t uuid_len,
54                              int nest_level) {
55   uint8_t* p_end = p + seq_len;
56   uint8_t type;
57   uint32_t len;
58 
59   /* A little safety check to avoid excessive recursion */
60   if (nest_level > 3) {
61     return false;
62   }
63 
64   while (p < p_end) {
65     type = *p++;
66     p = sdpu_get_len_from_type(p, p_end, type, &len);
67     if (p == NULL || (p + len) > p_end) {
68       log::warn("bad length");
69       break;
70     }
71     type = type >> 3;
72     if (type == UUID_DESC_TYPE) {
73       if (sdpu_compare_uuid_arrays(p, len, p_uuid, uuid_len)) {
74         return true;
75       }
76     } else if (type == DATA_ELE_SEQ_DESC_TYPE) {
77       if (find_uuid_in_seq(p, len, p_uuid, uuid_len, nest_level + 1)) {
78         return true;
79       }
80     }
81     p = p + len;
82   }
83 
84   /* If here, failed to match */
85   return false;
86 }
87 
88 /*******************************************************************************
89  *
90  * Function         sdp_db_service_search
91  *
92  * Description      This function searches for a record that contains the
93  *                  specified UIDs. It is passed either NULL to start at the
94  *                  beginning, or the previous record found.
95  *
96  * Returns          Pointer to the record, or NULL if not found.
97  *
98  ******************************************************************************/
sdp_db_service_search(const tSDP_RECORD * p_rec,const tSDP_UUID_SEQ * p_seq)99 const tSDP_RECORD* sdp_db_service_search(const tSDP_RECORD* p_rec, const tSDP_UUID_SEQ* p_seq) {
100   uint16_t xx, yy;
101   const tSDP_ATTRIBUTE* p_attr;
102   tSDP_RECORD* p_end = &sdp_cb.server_db.record[sdp_cb.server_db.num_records];
103 
104   /* If NULL, start at the beginning, else start at the first specified record
105    */
106   if (!p_rec) {
107     p_rec = &sdp_cb.server_db.record[0];
108   } else {
109     p_rec++;
110   }
111 
112   /* Look through the records. The spec says that a match occurs if */
113   /* the record contains all the passed UUIDs in it.                */
114   for (; p_rec < p_end; p_rec++) {
115     for (yy = 0; yy < p_seq->num_uids; yy++) {
116       p_attr = &p_rec->attribute[0];
117       for (xx = 0; xx < p_rec->num_attributes; xx++, p_attr++) {
118         if (p_attr->type == UUID_DESC_TYPE) {
119           if (sdpu_compare_uuid_arrays(p_attr->value_ptr, p_attr->len,
120                                        &p_seq->uuid_entry[yy].value[0],
121                                        p_seq->uuid_entry[yy].len)) {
122             break;
123           }
124         } else if (p_attr->type == DATA_ELE_SEQ_DESC_TYPE) {
125           if (find_uuid_in_seq(p_attr->value_ptr, p_attr->len, &p_seq->uuid_entry[yy].value[0],
126                                p_seq->uuid_entry[yy].len, 0)) {
127             break;
128           }
129         }
130       }
131       /* If any UUID was not found,  on to the next record */
132       if (xx == p_rec->num_attributes) {
133         break;
134       }
135     }
136 
137     /* If every UUID was found in the record, return the record */
138     if (yy == p_seq->num_uids) {
139       return p_rec;
140     }
141   }
142 
143   /* If here, no more records found */
144   return NULL;
145 }
146 
147 /*******************************************************************************
148  *
149  * Function         sdp_db_find_record
150  *
151  * Description      This function searches for a record with a specific handle
152  *                  It is passed the handle of the record.
153  *
154  * Returns          Pointer to the record, or NULL if not found.
155  *
156  ******************************************************************************/
sdp_db_find_record(uint32_t handle)157 tSDP_RECORD* sdp_db_find_record(uint32_t handle) {
158   tSDP_RECORD* p_rec;
159   tSDP_RECORD* p_end = &sdp_cb.server_db.record[sdp_cb.server_db.num_records];
160 
161   /* Look through the records for the caller's handle */
162   for (p_rec = &sdp_cb.server_db.record[0]; p_rec < p_end; p_rec++) {
163     if (p_rec->record_handle == handle) {
164       return p_rec;
165     }
166   }
167 
168   /* Record with that handle not found. */
169   return NULL;
170 }
171 
172 /*******************************************************************************
173  *
174  * Function         sdp_db_find_attr_in_rec
175  *
176  * Description      This function searches a record for specific attributes.
177  *                  It is passed a pointer to the record. If the record contains
178  *                  the specified attribute, (the caller may specify be a range
179  *                  of attributes), the attribute is returned.
180  *
181  * Returns          Pointer to the attribute, or NULL if not found.
182  *
183  ******************************************************************************/
sdp_db_find_attr_in_rec(const tSDP_RECORD * p_rec,uint16_t start_attr,uint16_t end_attr)184 const tSDP_ATTRIBUTE* sdp_db_find_attr_in_rec(const tSDP_RECORD* p_rec, uint16_t start_attr,
185                                               uint16_t end_attr) {
186   const tSDP_ATTRIBUTE* p_at;
187   uint16_t xx;
188 
189   /* Note that the attributes in a record are assumed to be in sorted order */
190   for (xx = 0, p_at = &p_rec->attribute[0]; xx < p_rec->num_attributes; xx++, p_at++) {
191     if ((p_at->id >= start_attr) && (p_at->id <= end_attr)) {
192       return p_at;
193     }
194   }
195 
196   /* No matching attribute found */
197   return NULL;
198 }
199 
200 /*******************************************************************************
201  *
202  * Function         sdp_compose_proto_list
203  *
204  * Description      This function is called to compose a data sequence from
205  *                  protocol element list struct pointer
206  *
207  * Returns          the length of the data sequence
208  *
209  ******************************************************************************/
sdp_compose_proto_list(uint8_t * p,uint16_t num_elem,tSDP_PROTOCOL_ELEM * p_elem_list)210 static int sdp_compose_proto_list(uint8_t* p, uint16_t num_elem, tSDP_PROTOCOL_ELEM* p_elem_list) {
211   uint16_t xx, yy, len;
212   bool is_rfcomm_scn;
213   uint8_t* p_head = p;
214   uint8_t* p_len;
215 
216   /* First, build the protocol list. This consists of a set of data element
217   ** sequences, one for each layer. Each layer sequence consists of layer's
218   ** UUID and optional parameters
219   */
220   for (xx = 0; xx < num_elem; xx++, p_elem_list++) {
221     len = 3 + (p_elem_list->num_params * 3);
222     UINT8_TO_BE_STREAM(p, (DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
223 
224     p_len = p;
225     *p++ = (uint8_t)len;
226 
227     UINT8_TO_BE_STREAM(p, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES);
228     UINT16_TO_BE_STREAM(p, p_elem_list->protocol_uuid);
229 
230     if (p_elem_list->protocol_uuid == UUID_PROTOCOL_RFCOMM) {
231       is_rfcomm_scn = true;
232     } else {
233       is_rfcomm_scn = false;
234     }
235 
236     for (yy = 0; yy < p_elem_list->num_params; yy++) {
237       if (is_rfcomm_scn) {
238         UINT8_TO_BE_STREAM(p, (UINT_DESC_TYPE << 3) | SIZE_ONE_BYTE);
239         UINT8_TO_BE_STREAM(p, p_elem_list->params[yy]);
240 
241         *p_len -= 1;
242       } else {
243         UINT8_TO_BE_STREAM(p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
244         UINT16_TO_BE_STREAM(p, p_elem_list->params[yy]);
245       }
246     }
247   }
248   return p - p_head;
249 }
250 
251 /*******************************************************************************
252  *
253  * Function         SDP_AddAttribute
254  *
255  * Description      This function is called to add an attribute to a record.
256  *                  This would be through the SDP database maintenance API.
257  *                  If the attribute already exists in the record, it is
258  *                  replaced with the new value.
259  *
260  * NOTE             Attribute values must be passed as a Big Endian stream.
261  *
262  * Returns          true if added OK, else false
263  *
264  ******************************************************************************/
SDP_AddAttribute(uint32_t handle,uint16_t attr_id,uint8_t attr_type,uint32_t attr_len,uint8_t * p_val)265 bool SDP_AddAttribute(uint32_t handle, uint16_t attr_id, uint8_t attr_type, uint32_t attr_len,
266                       uint8_t* p_val) {
267   uint16_t zz;
268   tSDP_RECORD* p_rec = &sdp_cb.server_db.record[0];
269 
270   if (p_val == nullptr) {
271     log::warn("Trying to add attribute with p_val == nullptr, skipped");
272     return false;
273   }
274 
275   // TODO(305066880): invoke would_log when implemented to check
276   // if LOG_VERBOSE is displayed.
277   if (true) {
278     if ((attr_type == UINT_DESC_TYPE) || (attr_type == TWO_COMP_INT_DESC_TYPE) ||
279         (attr_type == UUID_DESC_TYPE) || (attr_type == DATA_ELE_SEQ_DESC_TYPE) ||
280         (attr_type == DATA_ELE_ALT_DESC_TYPE)) {
281 #define MAX_ARR_LEN 200
282       // one extra byte for storing terminating zero byte
283       char num_array[2 * MAX_ARR_LEN + 1] = {0};
284       uint32_t len = (attr_len > MAX_ARR_LEN) ? MAX_ARR_LEN : attr_len;
285 #undef MAX_ARR_LEN
286 
287       for (uint32_t i = 0; i < len; i++) {
288         snprintf(&num_array[i * 2], sizeof(num_array) - i * 2, "%02X", (uint8_t)(p_val[i]));
289       }
290       log::verbose("SDP_AddAttribute: handle:{:X}, id:{:04X}, type:{}, len:{}, p_val:{}, *p_val:{}",
291                    handle, attr_id, attr_type, attr_len, std::format_ptr(p_val), num_array);
292     } else if (attr_type == BOOLEAN_DESC_TYPE) {
293       log::verbose("SDP_AddAttribute: handle:{:X}, id:{:04X}, type:{}, len:{}, p_val:{}, *p_val:{}",
294                    handle, attr_id, attr_type, attr_len, std::format_ptr(p_val), *p_val);
295     } else if ((attr_type == TEXT_STR_DESC_TYPE) || (attr_type == URL_DESC_TYPE)) {
296       if (p_val[attr_len - 1] == '\0') {
297         log::verbose(
298                 "SDP_AddAttribute: handle:{:X}, id:{:04X}, type:{}, len:{}, p_val:{}, *p_val:{}",
299                 handle, attr_id, attr_type, attr_len, std::format_ptr(p_val), (char*)p_val);
300       } else {
301         log::verbose("SDP_AddAttribute: handle:{:X}, id:{:04X}, type:{}, len:{}, p_val:{}", handle,
302                      attr_id, attr_type, attr_len, std::format_ptr(p_val));
303       }
304     } else {
305       log::verbose("SDP_AddAttribute: handle:{:X}, id:{:04X}, type:{}, len:{}, p_val:{}", handle,
306                    attr_id, attr_type, attr_len, std::format_ptr(p_val));
307     }
308   }
309 
310   /* Find the record in the database */
311   for (zz = 0; zz < sdp_cb.server_db.num_records; zz++, p_rec++) {
312     if (p_rec->record_handle == handle) {
313       // error out early, no need to look up
314       if (p_rec->free_pad_ptr >= SDP_MAX_PAD_LEN) {
315         log::error("the free pad for SDP record with handle {} is full, skip adding the attribute",
316                    handle);
317         return false;
318       }
319 
320       return SDP_AddAttributeToRecord(p_rec, attr_id, attr_type, attr_len, p_val);
321     }
322   }
323   return false;
324 }
325 
326 /*******************************************************************************
327  *
328  * Function         SDP_CreateRecord
329  *
330  * Description      This function is called to create a record in the database.
331  *                  This would be through the SDP database maintenance API. The
332  *                  record is created empty, the application should then call
333  *                  "add_attribute" to add the record's attributes.
334  *
335  * Returns          Record handle if OK, else 0.
336  *
337  ******************************************************************************/
SDP_CreateRecord(void)338 uint32_t SDP_CreateRecord(void) {
339   uint32_t handle;
340   uint8_t buf[4];
341   tSDP_DB* p_db = &sdp_cb.server_db;
342 
343   /* First, check if there is a free record */
344   if (p_db->num_records >= SDP_MAX_RECORDS) {
345     log::error("SDP_CreateRecord fail, exceed maximum records:{}", SDP_MAX_RECORDS);
346     return 0;
347   }
348 
349   memset(&p_db->record[p_db->num_records], 0, sizeof(tSDP_RECORD));
350 
351   /* We will use a handle of the first unreserved handle plus last record
352   ** number + 1 */
353   if (p_db->num_records) {
354     handle = p_db->record[p_db->num_records - 1].record_handle + 1;
355   } else {
356     handle = 0x10000;
357   }
358 
359   p_db->record[p_db->num_records].record_handle = handle;
360 
361   p_db->num_records++;
362   log::verbose("SDP_CreateRecord ok, num_records:{}", p_db->num_records);
363   /* Add the first attribute (the handle) automatically */
364   UINT32_TO_BE_FIELD(buf, handle);
365   SDP_AddAttribute(handle, ATTR_ID_SERVICE_RECORD_HDL, UINT_DESC_TYPE, 4, buf);
366 
367   return p_db->record[p_db->num_records - 1].record_handle;
368 }
369 
370 /*******************************************************************************
371  *
372  * Function         SDP_DeleteRecord
373  *
374  * Description      This function is called to add a record (or all records)
375  *                  from the database. This would be through the SDP database
376  *                  maintenance API.
377  *
378  *                  If a record handle of 0 is passed, all records are deleted.
379  *
380  * Returns          true if succeeded, else false
381  *
382  ******************************************************************************/
SDP_DeleteRecord(uint32_t handle)383 bool SDP_DeleteRecord(uint32_t handle) {
384   uint16_t xx, yy, zz;
385   tSDP_RECORD* p_rec = &sdp_cb.server_db.record[0];
386 
387   if (handle == 0 || sdp_cb.server_db.num_records == 0) {
388     /* Delete all records in the database */
389     sdp_cb.server_db.num_records = 0;
390 
391     /* require new DI record to be created in SDP_SetLocalDiRecord */
392     sdp_cb.server_db.di_primary_handle = 0;
393 
394     return true;
395   }
396 
397   /* Find the record in the database */
398   for (xx = 0; xx < sdp_cb.server_db.num_records; xx++, p_rec++) {
399     if (p_rec->record_handle != handle) {
400       continue;
401     }
402 
403     /* Found it. Shift everything up one */
404     for (yy = xx; yy < sdp_cb.server_db.num_records - 1; yy++, p_rec++) {
405       *p_rec = *(p_rec + 1);
406 
407       /* Adjust the attribute value pointer for each attribute */
408       for (zz = 0; zz < p_rec->num_attributes; zz++) {
409         p_rec->attribute[zz].value_ptr -= sizeof(tSDP_RECORD);
410       }
411     }
412 
413     sdp_cb.server_db.num_records--;
414 
415     log::verbose("SDP_DeleteRecord ok, num_records:{}", sdp_cb.server_db.num_records);
416     /* if we're deleting the primary DI record, clear the */
417     /* value in the control block */
418     if (sdp_cb.server_db.di_primary_handle == handle) {
419       sdp_cb.server_db.di_primary_handle = 0;
420     }
421 
422     return true;
423   }
424   return false;
425 }
426 
427 /*******************************************************************************
428  *
429  * Function         SDP_AddAttributeToRecord
430  *
431  * Description      This function is called to add an attribute to a record.
432  *                  This would be through the SDP database maintenance API.
433  *                  If the attribute already exists in the record, it is
434  *                  replaced with the new value.
435  *
436  * NOTE             Attribute values must be passed as a Big Endian stream.
437  *
438  * Returns          true if added OK, else false
439  *
440  ******************************************************************************/
SDP_AddAttributeToRecord(tSDP_RECORD * p_rec,uint16_t attr_id,uint8_t attr_type,uint32_t attr_len,uint8_t * p_val)441 bool SDP_AddAttributeToRecord(tSDP_RECORD* p_rec, uint16_t attr_id, uint8_t attr_type,
442                               uint32_t attr_len, uint8_t* p_val) {
443   uint16_t xx, yy;
444   tSDP_ATTRIBUTE* p_attr = &p_rec->attribute[0];
445 
446   /* Found the record. Now, see if the attribute already exists */
447   for (xx = 0; xx < p_rec->num_attributes; xx++, p_attr++) {
448     /* The attribute exists. replace it */
449     if (p_attr->id == attr_id) {
450       SDP_DeleteAttributeFromRecord(p_rec, attr_id);
451       break;
452     }
453     if (p_attr->id > attr_id) {
454       break;
455     }
456   }
457 
458   if (p_rec->num_attributes >= SDP_MAX_REC_ATTR) {
459     return false;
460   }
461 
462   /* If not found, see if we can allocate a new entry */
463   if (xx == p_rec->num_attributes) {
464     p_attr = &p_rec->attribute[p_rec->num_attributes];
465   } else {
466     /* Since the attributes are kept in sorted order, insert ours here */
467     for (yy = p_rec->num_attributes; yy > xx; yy--) {
468       p_rec->attribute[yy] = p_rec->attribute[yy - 1];
469     }
470   }
471 
472   p_attr->id = attr_id;
473   p_attr->type = attr_type;
474   p_attr->len = attr_len;
475 
476   if (p_rec->free_pad_ptr + attr_len >= SDP_MAX_PAD_LEN) {
477     if (p_rec->free_pad_ptr >= SDP_MAX_PAD_LEN) {
478       log::error(
479               "SDP_AddAttributeToRecord failed: free pad {} equals or exceeds max "
480               "padding length {}",
481               p_rec->free_pad_ptr, SDP_MAX_PAD_LEN);
482       return false;
483     }
484 
485     /* do truncate only for text string type descriptor */
486     if (attr_type == TEXT_STR_DESC_TYPE) {
487       log::warn("SDP_AddAttributeToRecord: attr_len:{} too long. truncate to ({})", attr_len,
488                 SDP_MAX_PAD_LEN - p_rec->free_pad_ptr);
489 
490       attr_len = SDP_MAX_PAD_LEN - p_rec->free_pad_ptr;
491       p_val[SDP_MAX_PAD_LEN - p_rec->free_pad_ptr - 1] = '\0';
492     } else {
493       attr_len = 0;
494     }
495   }
496 
497   if (attr_len > 0) {
498     p_attr->len = attr_len;
499     memcpy(&p_rec->attr_pad[p_rec->free_pad_ptr], p_val, (size_t)attr_len);
500     p_attr->value_ptr = &p_rec->attr_pad[p_rec->free_pad_ptr];
501     p_rec->free_pad_ptr += attr_len;
502   } else if (attr_len == 0 && p_attr->len != 0) {
503     /* if truncate to 0 length, simply don't add */
504     log::error("SDP_AddAttributeToRecord fail, length exceed maximum: ID {}: attr_len:{}", attr_id,
505                attr_len);
506     p_attr->id = p_attr->type = p_attr->len = 0;
507     return false;
508   }
509   p_rec->num_attributes++;
510   return true;
511 }
512 
513 /*******************************************************************************
514  *
515  * Function         SDP_AddSequence
516  *
517  * Description      This function is called to add a sequence to a record.
518  *                  This would be through the SDP database maintenance API.
519  *                  If the sequence already exists in the record, it is replaced
520  *                  with the new sequence.
521  *
522  * NOTE             Element values must be passed as a Big Endian stream.
523  *
524  * Returns          true if added OK, else false
525  *
526  ******************************************************************************/
SDP_AddSequence(uint32_t handle,uint16_t attr_id,uint16_t num_elem,uint8_t type[],uint8_t len[],uint8_t * p_val[])527 bool SDP_AddSequence(uint32_t handle, uint16_t attr_id, uint16_t num_elem, uint8_t type[],
528                      uint8_t len[], uint8_t* p_val[]) {
529   uint16_t xx;
530   uint8_t* p;
531   uint8_t* p_head;
532   bool result;
533   uint8_t* p_buff = (uint8_t*)osi_malloc(sizeof(uint8_t) * SDP_MAX_ATTR_LEN * 2);
534 
535   p = p_buff;
536 
537   /* First, build the sequence */
538   for (xx = 0; xx < num_elem; xx++) {
539     p_head = p;
540     switch (len[xx]) {
541       case 1:
542         UINT8_TO_BE_STREAM(p, (type[xx] << 3) | SIZE_ONE_BYTE);
543         break;
544       case 2:
545         UINT8_TO_BE_STREAM(p, (type[xx] << 3) | SIZE_TWO_BYTES);
546         break;
547       case 4:
548         UINT8_TO_BE_STREAM(p, (type[xx] << 3) | SIZE_FOUR_BYTES);
549         break;
550       case 8:
551         UINT8_TO_BE_STREAM(p, (type[xx] << 3) | SIZE_EIGHT_BYTES);
552         break;
553       case 16:
554         UINT8_TO_BE_STREAM(p, (type[xx] << 3) | SIZE_SIXTEEN_BYTES);
555         break;
556       default:
557         UINT8_TO_BE_STREAM(p, (type[xx] << 3) | SIZE_IN_NEXT_BYTE);
558         UINT8_TO_BE_STREAM(p, len[xx]);
559         break;
560     }
561 
562     ARRAY_TO_BE_STREAM(p, p_val[xx], len[xx]);
563 
564     if (p - p_buff > SDP_MAX_ATTR_LEN) {
565       /* go back to before we add this element */
566       p = p_head;
567       if (p_head == p_buff) {
568         /* the first element exceed the max length */
569         log::error("SDP_AddSequence - too long(attribute is not added)!!");
570         osi_free(p_buff);
571         return false;
572       } else {
573         log::error("SDP_AddSequence - too long, add {} elements of {}", xx, num_elem);
574       }
575       break;
576     }
577   }
578   result =
579           SDP_AddAttribute(handle, attr_id, DATA_ELE_SEQ_DESC_TYPE, (uint32_t)(p - p_buff), p_buff);
580   osi_free(p_buff);
581   return result;
582 }
583 
584 /*******************************************************************************
585  *
586  * Function         SDP_AddUuidSequence
587  *
588  * Description      This function is called to add a UUID sequence to a record.
589  *                  This would be through the SDP database maintenance API.
590  *                  If the sequence already exists in the record, it is replaced
591  *                  with the new sequence.
592  *
593  * Returns          true if added OK, else false
594  *
595  ******************************************************************************/
SDP_AddUuidSequence(uint32_t handle,uint16_t attr_id,uint16_t num_uuids,uint16_t * p_uuids)596 bool SDP_AddUuidSequence(uint32_t handle, uint16_t attr_id, uint16_t num_uuids, uint16_t* p_uuids) {
597   uint16_t xx;
598   uint8_t* p;
599   int32_t max_len = SDP_MAX_ATTR_LEN - 3;
600   bool result;
601   uint8_t* p_buff = (uint8_t*)osi_malloc(sizeof(uint8_t) * SDP_MAX_ATTR_LEN * 2);
602 
603   p = p_buff;
604 
605   /* First, build the sequence */
606   for (xx = 0; xx < num_uuids; xx++, p_uuids++) {
607     UINT8_TO_BE_STREAM(p, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES);
608     UINT16_TO_BE_STREAM(p, *p_uuids);
609 
610     if ((p - p_buff) > max_len) {
611       log::warn("SDP_AddUuidSequence - too long, add {} uuids of {}", xx, num_uuids);
612       break;
613     }
614   }
615 
616   result =
617           SDP_AddAttribute(handle, attr_id, DATA_ELE_SEQ_DESC_TYPE, (uint32_t)(p - p_buff), p_buff);
618   osi_free(p_buff);
619   return result;
620 }
621 
622 /*******************************************************************************
623  *
624  * Function         SDP_AddProtocolList
625  *
626  * Description      This function is called to add a protocol descriptor list to
627  *                  a record. This would be through the SDP database
628  *                  maintenance API. If the protocol list already exists in the
629  *                  record, it is replaced with the new list.
630  *
631  * Returns          true if added OK, else false
632  *
633  ******************************************************************************/
SDP_AddProtocolList(uint32_t handle,uint16_t num_elem,tSDP_PROTOCOL_ELEM * p_elem_list)634 bool SDP_AddProtocolList(uint32_t handle, uint16_t num_elem, tSDP_PROTOCOL_ELEM* p_elem_list) {
635   int offset;
636   bool result;
637   uint8_t* p_buff = (uint8_t*)osi_malloc(sizeof(uint8_t) * SDP_MAX_ATTR_LEN * 2);
638 
639   offset = sdp_compose_proto_list(p_buff, num_elem, p_elem_list);
640   result = SDP_AddAttribute(handle, ATTR_ID_PROTOCOL_DESC_LIST, DATA_ELE_SEQ_DESC_TYPE,
641                             (uint32_t)offset, p_buff);
642   osi_free(p_buff);
643   return result;
644 }
645 
646 /*******************************************************************************
647  *
648  * Function         SDP_AddAdditionProtoLists
649  *
650  * Description      This function is called to add a protocol descriptor list to
651  *                  a record. This would be through the SDP database maintenance
652  *                  API. If the protocol list already exists in the record, it
653  *                  is replaced with the new list.
654  *
655  * Returns          true if added OK, else false
656  *
657  ******************************************************************************/
SDP_AddAdditionProtoLists(uint32_t handle,uint16_t num_elem,tSDP_PROTO_LIST_ELEM * p_proto_list)658 bool SDP_AddAdditionProtoLists(uint32_t handle, uint16_t num_elem,
659                                tSDP_PROTO_LIST_ELEM* p_proto_list) {
660   uint16_t xx;
661   uint8_t* p;
662   uint8_t* p_len;
663   int offset;
664   bool result;
665   uint8_t* p_buff = (uint8_t*)osi_malloc(sizeof(uint8_t) * SDP_MAX_ATTR_LEN * 2);
666 
667   p = p_buff;
668 
669   /* for each ProtocolDescriptorList */
670   for (xx = 0; xx < num_elem; xx++, p_proto_list++) {
671     UINT8_TO_BE_STREAM(p, (DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
672     p_len = p++;
673 
674     offset = sdp_compose_proto_list(p, p_proto_list->num_elems, p_proto_list->list_elem);
675     p += offset;
676 
677     *p_len = (uint8_t)(p - p_len - 1);
678   }
679   result = SDP_AddAttribute(handle, ATTR_ID_ADDITION_PROTO_DESC_LISTS, DATA_ELE_SEQ_DESC_TYPE,
680                             (uint32_t)(p - p_buff), p_buff);
681   osi_free(p_buff);
682   return result;
683 }
684 
685 /*******************************************************************************
686  *
687  * Function         SDP_AddProfileDescriptorList
688  *
689  * Description      This function is called to add a profile descriptor list to
690  *                  a record. This would be through the SDP database maintenance
691  *                  API. If the version already exists in the record, it is
692  *                  replaced with the new one.
693  *
694  * Returns          true if added OK, else false
695  *
696  ******************************************************************************/
SDP_AddProfileDescriptorList(uint32_t handle,uint16_t profile_uuid,uint16_t version)697 bool SDP_AddProfileDescriptorList(uint32_t handle, uint16_t profile_uuid, uint16_t version) {
698   uint8_t* p;
699   bool result;
700   uint8_t* p_buff = (uint8_t*)osi_malloc(sizeof(uint8_t) * SDP_MAX_ATTR_LEN);
701 
702   p = p_buff + 2;
703 
704   /* First, build the profile descriptor list. This consists of a data element
705    * sequence. */
706   /* The sequence consists of profile's UUID and version number  */
707   UINT8_TO_BE_STREAM(p, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES);
708   UINT16_TO_BE_STREAM(p, profile_uuid);
709 
710   UINT8_TO_BE_STREAM(p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
711   UINT16_TO_BE_STREAM(p, version);
712 
713   /* Add in type and length fields */
714   *p_buff = (uint8_t)((DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
715   *(p_buff + 1) = (uint8_t)(p - (p_buff + 2));
716 
717   result = SDP_AddAttribute(handle, ATTR_ID_BT_PROFILE_DESC_LIST, DATA_ELE_SEQ_DESC_TYPE,
718                             (uint32_t)(p - p_buff), p_buff);
719   osi_free(p_buff);
720   return result;
721 }
722 
723 /*******************************************************************************
724  *
725  * Function         SDP_AddProfileDescriptorListToRecord
726  *
727  * Description      This function is called to add a profile descriptor list to
728  *                  a record. This would be through the SDP database maintenance
729  *                  API. If the version already exists in the record, it is
730  *                  replaced with the new one.
731  *
732  * Returns          true if added OK, else false
733  *
734  ******************************************************************************/
SDP_AddProfileDescriptorListToRecord(tSDP_RECORD * prec,uint16_t profile_uuid,uint16_t version)735 bool SDP_AddProfileDescriptorListToRecord(tSDP_RECORD* prec, uint16_t profile_uuid,
736                                           uint16_t version) {
737   uint8_t* p;
738   bool result;
739   uint8_t* p_buff = (uint8_t*)osi_malloc(sizeof(uint8_t) * SDP_MAX_ATTR_LEN);
740 
741   p = p_buff + 2;
742 
743   /* First, build the profile descriptor list. This consists of a data element
744    * sequence. */
745   /* The sequence consists of profile's UUID and version number  */
746   UINT8_TO_BE_STREAM(p, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES);
747   UINT16_TO_BE_STREAM(p, profile_uuid);
748 
749   UINT8_TO_BE_STREAM(p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
750   UINT16_TO_BE_STREAM(p, version);
751 
752   /* Add in type and length fields */
753   *p_buff = (uint8_t)((DATA_ELE_SEQ_DESC_TYPE << 3) | SIZE_IN_NEXT_BYTE);
754   *(p_buff + 1) = (uint8_t)(p - (p_buff + 2));
755 
756   result = SDP_AddAttributeToRecord(prec, ATTR_ID_BT_PROFILE_DESC_LIST, DATA_ELE_SEQ_DESC_TYPE,
757                                     (uint32_t)(p - p_buff), p_buff);
758   osi_free(p_buff);
759   return result;
760 }
761 
762 /*******************************************************************************
763  *
764  * Function         SDP_AddLanguageBaseAttrIDList
765  *
766  * Description      This function is called to add a language base attr list to
767  *                  a record. This would be through the SDP database maintenance
768  *                  API. If the version already exists in the record, it is
769  *                  replaced with the new one.
770  *
771  * Returns          true if added OK, else false
772  *
773  ******************************************************************************/
SDP_AddLanguageBaseAttrIDList(uint32_t handle,uint16_t lang,uint16_t char_enc,uint16_t base_id)774 bool SDP_AddLanguageBaseAttrIDList(uint32_t handle, uint16_t lang, uint16_t char_enc,
775                                    uint16_t base_id) {
776   uint8_t* p;
777   bool result;
778   uint8_t* p_buff = (uint8_t*)osi_malloc(sizeof(uint8_t) * SDP_MAX_ATTR_LEN);
779 
780   p = p_buff;
781 
782   /* First, build the language base descriptor list. This consists of a data */
783   /* element sequence. The sequence consists of 9 bytes (3 UINt16 fields)    */
784   UINT8_TO_BE_STREAM(p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
785   UINT16_TO_BE_STREAM(p, lang);
786 
787   UINT8_TO_BE_STREAM(p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
788   UINT16_TO_BE_STREAM(p, char_enc);
789 
790   UINT8_TO_BE_STREAM(p, (UINT_DESC_TYPE << 3) | SIZE_TWO_BYTES);
791   UINT16_TO_BE_STREAM(p, base_id);
792 
793   result = SDP_AddAttribute(handle, ATTR_ID_LANGUAGE_BASE_ATTR_ID_LIST, DATA_ELE_SEQ_DESC_TYPE,
794                             (uint32_t)(p - p_buff), p_buff);
795   osi_free(p_buff);
796   return result;
797 }
798 
799 /*******************************************************************************
800  *
801  * Function         SDP_AddServiceClassIdList
802  *
803  * Description      This function is called to add a service list to a record.
804  *                  This would be through the SDP database maintenance API.
805  *                  If the service list already exists in the record, it is
806  *                  replaced with the new list.
807  *
808  * Returns          true if added OK, else false
809  *
810  ******************************************************************************/
SDP_AddServiceClassIdList(uint32_t handle,uint16_t num_services,uint16_t * p_service_uuids)811 bool SDP_AddServiceClassIdList(uint32_t handle, uint16_t num_services, uint16_t* p_service_uuids) {
812   uint16_t xx;
813   uint8_t* p;
814   bool result;
815   uint8_t* p_buff = (uint8_t*)osi_malloc(sizeof(uint8_t) * SDP_MAX_ATTR_LEN * 2);
816 
817   p = p_buff;
818 
819   for (xx = 0; xx < num_services; xx++, p_service_uuids++) {
820     UINT8_TO_BE_STREAM(p, (UUID_DESC_TYPE << 3) | SIZE_TWO_BYTES);
821     UINT16_TO_BE_STREAM(p, *p_service_uuids);
822   }
823 
824   result = SDP_AddAttribute(handle, ATTR_ID_SERVICE_CLASS_ID_LIST, DATA_ELE_SEQ_DESC_TYPE,
825                             (uint32_t)(p - p_buff), p_buff);
826   osi_free(p_buff);
827   return result;
828 }
829 
830 /*******************************************************************************
831  *
832  * Function         SDP_DeleteAttributeFromRecord
833  *
834  * Description      This function is called to delete an attribute from a
835  *                  record. This would be through the SDP database maintenance
836  *                  API.
837  *
838  * Returns          true if deleted OK, else false if not found
839  *
840  ******************************************************************************/
841 
SDP_DeleteAttributeFromRecord(tSDP_RECORD * p_rec,uint16_t attr_id)842 bool SDP_DeleteAttributeFromRecord(tSDP_RECORD* p_rec, uint16_t attr_id) {
843   tSDP_ATTRIBUTE* p_attr = &p_rec->attribute[0];
844   uint8_t* pad_ptr;
845   uint32_t len; /* Number of bytes in the entry */
846 
847   /* Found it. Now, find the attribute */
848   for (uint16_t attribute_index = 0; attribute_index < p_rec->num_attributes;
849        attribute_index++, p_attr++) {
850     if (p_attr->id == attr_id) {
851       pad_ptr = p_attr->value_ptr;
852       len = p_attr->len;
853 
854       if (len) {
855         for (uint16_t zz = 0; zz < p_rec->num_attributes; zz++) {
856           if (p_rec->attribute[zz].value_ptr > pad_ptr) {
857             p_rec->attribute[zz].value_ptr -= len;
858           }
859         }
860       }
861 
862       /* Found it. Shift everything up one */
863       p_rec->num_attributes--;
864 
865       for (uint16_t zz = attribute_index; zz < p_rec->num_attributes; zz++, p_attr++) {
866         *p_attr = *(p_attr + 1);
867       }
868 
869       /* adjust attribute values if needed */
870       if (len) {
871         uint16_t last_attribute_to_adjust =
872                 (p_rec->free_pad_ptr - ((pad_ptr + len) - &p_rec->attr_pad[0]));
873         for (uint16_t zz = 0; zz < last_attribute_to_adjust; zz++, pad_ptr++) {
874           *pad_ptr = *(pad_ptr + len);
875         }
876         p_rec->free_pad_ptr -= len;
877       }
878       return true;
879     }
880   }
881   /* If here, not found */
882   return false;
883 }
884