1 /******************************************************************************
2  *
3  *  Copyright 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 interface functions
22  *
23  ******************************************************************************/
24 
25 #define LOG_TAG "stack::sdp"
26 
27 #include "stack/include/sdp_api.h"
28 
29 #include <base/strings/stringprintf.h>
30 #include <bluetooth/log.h>
31 #include <string.h>
32 
33 #include <cstdint>
34 #include <utility>
35 
36 #include "internal_include/bt_target.h"
37 #include "main/shim/dumpsys.h"
38 #include "stack/include/bt_types.h"
39 #include "stack/include/bt_uuid16.h"
40 #include "stack/include/sdpdefs.h"
41 #include "stack/sdp/internal/sdp_api.h"
42 #include "stack/sdp/sdpint.h"
43 #include "types/bluetooth/uuid.h"
44 #include "types/raw_address.h"
45 
46 using bluetooth::Uuid;
47 using namespace bluetooth;
48 
49 namespace {
50 constexpr unsigned kMaxSdpConnections = static_cast<unsigned>(SDP_MAX_CONNECTIONS);
51 constexpr unsigned kMaxSdpRecords = static_cast<unsigned>(SDP_MAX_DISC_SERVER_RECS);
52 }  // namespace
53 
54 /*******************************************************************************
55  *
56  * Function         SDP_InitDiscoveryDb
57  *
58  * Description      This function is called to initialize a discovery database.
59  *
60  * Parameters:      p_db        - (input) address of an area of memory where the
61  *                                        discovery database is managed.
62  *                  len         - (input) size (in bytes) of the memory
63  *                                 NOTE: This must be larger than
64  *                                       sizeof(tSDP_DISCOVERY_DB)
65  *                  num_uuid    - (input) number of UUID filters applied
66  *                  p_uuid_list - (input) list of UUID filters
67  *                  num_attr    - (input) number of attribute filters applied
68  *                  p_attr_list - (input) list of attribute filters
69  *
70  *
71  * Returns          bool
72  *                          true if successful
73  *                          false if one or more parameters are bad
74  *
75  ******************************************************************************/
SDP_InitDiscoveryDb(tSDP_DISCOVERY_DB * p_db,uint32_t len,uint16_t num_uuid,const Uuid * p_uuid_list,uint16_t num_attr,const uint16_t * p_attr_list)76 bool SDP_InitDiscoveryDb(tSDP_DISCOVERY_DB* p_db, uint32_t len, uint16_t num_uuid,
77                          const Uuid* p_uuid_list, uint16_t num_attr, const uint16_t* p_attr_list) {
78   uint16_t xx;
79 
80   /* verify the parameters */
81   if (p_db == NULL || (sizeof(tSDP_DISCOVERY_DB) > len) || num_attr > SDP_MAX_ATTR_FILTERS ||
82       num_uuid > SDP_MAX_UUID_FILTERS) {
83     log::error("SDP_InitDiscoveryDb Illegal param: p_db {}, len {}, num_uuid {}, num_attr {}",
84                std::format_ptr(p_db), len, num_uuid, num_attr);
85 
86     return false;
87   }
88 
89   memset(p_db, 0, static_cast<size_t>(len));
90 
91   p_db->mem_size = len - sizeof(tSDP_DISCOVERY_DB);
92   p_db->mem_free = p_db->mem_size;
93   p_db->p_first_rec = NULL;
94   p_db->p_free_mem = (uint8_t*)(p_db + 1);
95 
96   for (xx = 0; xx < num_uuid; xx++) {
97     p_db->uuid_filters[xx] = *p_uuid_list++;
98   }
99 
100   p_db->num_uuid_filters = num_uuid;
101 
102   for (xx = 0; xx < num_attr; xx++) {
103     p_db->attr_filters[xx] = *p_attr_list++;
104   }
105 
106   /* sort attributes */
107   sdpu_sort_attr_list(num_attr, p_db);
108 
109   p_db->num_attr_filters = num_attr;
110   return true;
111 }
112 
113 /*******************************************************************************
114  *
115  * Function         SDP_CancelServiceSearch
116  *
117  * Description      This function cancels an active query to an SDP server.
118  *
119  * Returns          true if discovery cancelled, false if a matching activity is
120  *                  not found.
121  *
122  ******************************************************************************/
SDP_CancelServiceSearch(const tSDP_DISCOVERY_DB * p_db)123 bool SDP_CancelServiceSearch(const tSDP_DISCOVERY_DB* p_db) {
124   tCONN_CB* p_ccb = sdpu_find_ccb_by_db(p_db);
125   if (!p_ccb) {
126     return false;
127   }
128 
129   sdp_disconnect(p_ccb, tSDP_STATUS::SDP_CANCEL);
130   p_ccb->disc_state = SDP_DISC_WAIT_CANCEL;
131   return true;
132 }
133 
134 /*******************************************************************************
135  *
136  * Function         SDP_ServiceSearchRequest
137  *
138  * Description      This function queries an SDP server for information.
139  *
140  * Returns          true if discovery started, false if failed.
141  *
142  ******************************************************************************/
SDP_ServiceSearchRequest(const RawAddress & bd_addr,tSDP_DISCOVERY_DB * p_db,tSDP_DISC_CMPL_CB * p_cb)143 bool SDP_ServiceSearchRequest(const RawAddress& bd_addr, tSDP_DISCOVERY_DB* p_db,
144                               tSDP_DISC_CMPL_CB* p_cb) {
145   /* Specific BD address */
146   tCONN_CB* p_ccb = sdp_conn_originate(bd_addr);
147   if (!p_ccb) {
148     log::warn("no spare CCB for peer:{} max:{}", bd_addr, kMaxSdpConnections);
149     sdpu_dump_all_ccb();
150     return false;
151   }
152 
153   p_ccb->disc_state = SDP_DISC_WAIT_CONN;
154   p_ccb->p_db = p_db;
155   p_ccb->p_cb = p_cb;
156 
157   return true;
158 }
159 
160 /*******************************************************************************
161  *
162  * Function         SDP_ServiceSearchAttributeRequest
163  *
164  * Description      This function queries an SDP server for information.
165  *
166  *                  The difference between this API function and the function
167  *                  SDP_ServiceSearchRequest is that this one does a
168  *                  combined ServiceSearchAttributeRequest SDP function.
169  *                  (This is for Unplug Testing)
170  *
171  * Returns          true if discovery started, false if failed.
172  *
173  ******************************************************************************/
SDP_ServiceSearchAttributeRequest(const RawAddress & bd_addr,tSDP_DISCOVERY_DB * p_db,tSDP_DISC_CMPL_CB * p_cb)174 bool SDP_ServiceSearchAttributeRequest(const RawAddress& bd_addr, tSDP_DISCOVERY_DB* p_db,
175                                        tSDP_DISC_CMPL_CB* p_cb) {
176   /* Specific BD address */
177   tCONN_CB* p_ccb = sdp_conn_originate(bd_addr);
178   if (!p_ccb) {
179     log::warn("no spare CCB for peer:{} max:{}", bd_addr, kMaxSdpConnections);
180     sdpu_dump_all_ccb();
181     return false;
182   }
183 
184   p_ccb->disc_state = SDP_DISC_WAIT_CONN;
185   p_ccb->p_db = p_db;
186   p_ccb->p_cb = p_cb;
187 
188   p_ccb->is_attr_search = true;
189 
190   return true;
191 }
192 /*******************************************************************************
193  *
194  * Function         SDP_ServiceSearchAttributeRequest2
195  *
196  * Description      This function queries an SDP server for information.
197  *
198  *                  The difference between this API function and the function
199  *                  SDP_ServiceSearchRequest is that this one does a
200  *                  combined ServiceSearchAttributeRequest SDP function.
201  *                  (This is for Unplug Testing)
202  *
203  * Returns          true if discovery started, false if failed.
204  *
205  ******************************************************************************/
SDP_ServiceSearchAttributeRequest2(const RawAddress & bd_addr,tSDP_DISCOVERY_DB * p_db,base::RepeatingCallback<tSDP_DISC_CMPL_CB> complete_callback)206 bool SDP_ServiceSearchAttributeRequest2(
207         const RawAddress& bd_addr, tSDP_DISCOVERY_DB* p_db,
208         base::RepeatingCallback<tSDP_DISC_CMPL_CB> complete_callback) {
209   /* Specific BD address */
210   tCONN_CB* p_ccb = sdp_conn_originate(bd_addr);
211   if (!p_ccb) {
212     log::warn("no spare CCB for peer:{} max:{}", bd_addr, kMaxSdpConnections);
213     sdpu_dump_all_ccb();
214     return false;
215   }
216 
217   p_ccb->disc_state = SDP_DISC_WAIT_CONN;
218   p_ccb->p_db = p_db;
219   p_ccb->complete_callback = std::move(complete_callback);
220 
221   p_ccb->is_attr_search = true;
222 
223   return true;
224 }
225 
226 /*******************************************************************************
227  *
228  * Function         SDP_FindAttributeInRec
229  *
230  * Description      This function searches an SDP discovery record for a
231  *                  specific attribute.
232  *
233  * Returns          Pointer to matching attribute entry, or NULL
234  *
235  ******************************************************************************/
SDP_FindAttributeInRec(const tSDP_DISC_REC * p_rec,uint16_t attr_id)236 tSDP_DISC_ATTR* SDP_FindAttributeInRec(const tSDP_DISC_REC* p_rec, uint16_t attr_id) {
237   tSDP_DISC_ATTR* p_attr;
238 
239   p_attr = p_rec->p_first_attr;
240   while (p_attr) {
241     if (p_attr->attr_id == attr_id) {
242       return p_attr;
243     }
244 
245     p_attr = p_attr->p_next_attr;
246   }
247 
248   /* If here, no matching attribute found */
249   return NULL;
250 }
251 
252 /*******************************************************************************
253  *
254  * Function         SDP_FindServiceUUIDInRec
255  *
256  * Description      This function is called to read the service UUID within a
257  *                  record if there is any.
258  *
259  * Parameters:      p_rec      - pointer to a SDP record.
260  *                  p_uuid     - output parameter to save the UUID found.
261  *
262  * Returns          true if found, otherwise false.
263  *
264  ******************************************************************************/
SDP_FindServiceUUIDInRec(const tSDP_DISC_REC * p_rec,Uuid * p_uuid)265 bool SDP_FindServiceUUIDInRec(const tSDP_DISC_REC* p_rec, Uuid* p_uuid) {
266   tSDP_DISC_ATTR *p_attr, *p_sattr, *p_extra_sattr;
267 
268   p_attr = p_rec->p_first_attr;
269 
270   while (p_attr) {
271     if ((p_attr->attr_id == ATTR_ID_SERVICE_CLASS_ID_LIST) &&
272         (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE)) {
273       for (p_sattr = p_attr->attr_value.v.p_sub_attr; p_sattr; p_sattr = p_sattr->p_next_attr) {
274         if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE) {
275           if (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == Uuid::kNumBytes16) {
276             *p_uuid = Uuid::From16Bit(p_sattr->attr_value.v.u16);
277           } else if (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == Uuid::kNumBytes128) {
278             *p_uuid = Uuid::From128BitBE(p_sattr->attr_value.v.array);
279           } else if (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == Uuid::kNumBytes32) {
280             *p_uuid = Uuid::From32Bit(p_sattr->attr_value.v.u32);
281           }
282 
283           return true;
284         } else {
285           /* Checking for Toyota G Block Car Kit:
286            **  This car kit puts an extra data element sequence
287            **  where the UUID is suppose to be!!!
288            */
289           if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE) {
290             /* Look through data element sequence until no more UUIDs */
291             for (p_extra_sattr = p_sattr->attr_value.v.p_sub_attr; p_extra_sattr;
292                  p_extra_sattr = p_extra_sattr->p_next_attr) {
293               /* Increment past this to see if the next attribut is UUID */
294               if ((SDP_DISC_ATTR_TYPE(p_extra_sattr->attr_len_type) == UUID_DESC_TYPE)
295                   /* only support 16 bits UUID for now */
296                   && (SDP_DISC_ATTR_LEN(p_extra_sattr->attr_len_type) == 2)) {
297                 *p_uuid = Uuid::From16Bit(p_extra_sattr->attr_value.v.u16);
298                 return true;
299               }
300             }
301           }
302         }
303       }
304       break;
305     } else if (p_attr->attr_id == ATTR_ID_SERVICE_ID) {
306       if ((SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == UUID_DESC_TYPE)
307           /* only support 16 bits UUID for now */
308           && (SDP_DISC_ATTR_LEN(p_attr->attr_len_type) == 2)) {
309         *p_uuid = Uuid::From16Bit(p_attr->attr_value.v.u16);
310         return true;
311       }
312     }
313     p_attr = p_attr->p_next_attr;
314   }
315   return false;
316 }
317 
318 /*******************************************************************************
319  *
320  * Function         SDP_FindServiceUUIDInRec_128bit
321  *
322  * Description      This function is called to read the 128-bit service UUID
323  *                  within a record if there is any.
324  *
325  * Parameters:      p_rec      - pointer to a SDP record.
326  *                  p_uuid     - output parameter to save the UUID found.
327  *
328  * Returns          true if found, otherwise false.
329  *
330  ******************************************************************************/
SDP_FindServiceUUIDInRec_128bit(const tSDP_DISC_REC * p_rec,Uuid * p_uuid)331 bool SDP_FindServiceUUIDInRec_128bit(const tSDP_DISC_REC* p_rec, Uuid* p_uuid) {
332   tSDP_DISC_ATTR* p_attr = p_rec->p_first_attr;
333   while (p_attr) {
334     if ((p_attr->attr_id == ATTR_ID_SERVICE_CLASS_ID_LIST) &&
335         (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE)) {
336       tSDP_DISC_ATTR* p_sattr = p_attr->attr_value.v.p_sub_attr;
337       while (p_sattr) {
338         if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE) {
339           /* only support 128 bits UUID for now */
340           if (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 16) {
341             *p_uuid = Uuid::From128BitBE(p_sattr->attr_value.v.array);
342           }
343           return true;
344         }
345 
346         p_sattr = p_sattr->p_next_attr;
347       }
348       break;
349     } else if (p_attr->attr_id == ATTR_ID_SERVICE_ID) {
350       if ((SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == UUID_DESC_TYPE)
351           /* only support 128 bits UUID for now */
352           && (SDP_DISC_ATTR_LEN(p_attr->attr_len_type) == 16)) {
353         *p_uuid = Uuid::From128BitBE(p_attr->attr_value.v.array);
354         return true;
355       }
356     }
357     p_attr = p_attr->p_next_attr;
358   }
359   return false;
360 }
361 
362 /*******************************************************************************
363  *
364  * Function         SDP_FindServiceInDb
365  *
366  * Description      This function queries an SDP database for a specific
367  *                  service. If the p_start_rec pointer is NULL, it looks from
368  *                  the beginning of the database, else it continues from the
369  *                  next record after p_start_rec.
370  *
371  * Returns          Pointer to record containing service class, or NULL
372  *
373  ******************************************************************************/
SDP_FindServiceInDb(const tSDP_DISCOVERY_DB * p_db,uint16_t service_uuid,tSDP_DISC_REC * p_start_rec)374 tSDP_DISC_REC* SDP_FindServiceInDb(const tSDP_DISCOVERY_DB* p_db, uint16_t service_uuid,
375                                    tSDP_DISC_REC* p_start_rec) {
376   tSDP_DISC_REC* p_rec;
377   tSDP_DISC_ATTR *p_attr, *p_sattr, *p_extra_sattr;
378 
379   /* Must have a valid database */
380   if (p_db == NULL) {
381     return NULL;
382   }
383 
384   if (!p_start_rec) {
385     p_rec = p_db->p_first_rec;
386   } else {
387     p_rec = p_start_rec->p_next_rec;
388   }
389 
390   while (p_rec) {
391     p_attr = p_rec->p_first_attr;
392     while (p_attr) {
393       if ((p_attr->attr_id == ATTR_ID_SERVICE_CLASS_ID_LIST) &&
394           (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE)) {
395         for (p_sattr = p_attr->attr_value.v.p_sub_attr; p_sattr; p_sattr = p_sattr->p_next_attr) {
396           if ((SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE) &&
397               (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 2)) {
398             log::verbose("SDP_FindServiceInDb - p_sattr value = 0x{:x} serviceuuid = 0x{:x}",
399                          p_sattr->attr_value.v.u16, service_uuid);
400             if (service_uuid == UUID_SERVCLASS_HDP_PROFILE) {
401               if ((p_sattr->attr_value.v.u16 == UUID_SERVCLASS_HDP_SOURCE) ||
402                   (p_sattr->attr_value.v.u16 == UUID_SERVCLASS_HDP_SINK)) {
403                 log::verbose("SDP_FindServiceInDb found HDP source or sink\n");
404                 return p_rec;
405               }
406             }
407           }
408 
409           if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE &&
410               (service_uuid == 0 || (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 2 &&
411                                      p_sattr->attr_value.v.u16 == service_uuid))) {
412             /* for a specific uuid, or any one */
413             return p_rec;
414           } else {
415             /* Checking for Toyota G Block Car Kit:
416              **  This car kit puts an extra data element sequence
417              **  where the UUID is suppose to be!!!
418              */
419             if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE) {
420               /* Look through data element sequence until no more UUIDs */
421               for (p_extra_sattr = p_sattr->attr_value.v.p_sub_attr; p_extra_sattr;
422                    p_extra_sattr = p_extra_sattr->p_next_attr) {
423                 /* Increment past this to see if the next attribut is UUID */
424                 if ((SDP_DISC_ATTR_TYPE(p_extra_sattr->attr_len_type) == UUID_DESC_TYPE) &&
425                     (SDP_DISC_ATTR_LEN(p_extra_sattr->attr_len_type) == 2)
426                     /* for a specific uuid, or any one */
427                     && ((p_extra_sattr->attr_value.v.u16 == service_uuid) || (service_uuid == 0))) {
428                   return p_rec;
429                 }
430               }
431             }
432           }
433         }
434         break;
435       } else if (p_attr->attr_id == ATTR_ID_SERVICE_ID) {
436         if ((SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == UUID_DESC_TYPE) &&
437             (SDP_DISC_ATTR_LEN(p_attr->attr_len_type) == 2)
438             /* find a specific UUID or anyone */
439             && ((p_attr->attr_value.v.u16 == service_uuid) || service_uuid == 0)) {
440           return p_rec;
441         }
442       }
443 
444       p_attr = p_attr->p_next_attr;
445     }
446 
447     p_rec = p_rec->p_next_rec;
448   }
449   /* If here, no matching UUID found */
450   return NULL;
451 }
452 
453 /*******************************************************************************
454  *
455  * Function         SDP_FindServiceInDb_128bit
456  *
457  * Description      Query an SDP database for a specific service. If the
458  *                  p_start_rec pointer is NULL, it looks from the beginning of
459  *                  the database, else it continues from the next record after
460  *                  p_start_rec.
461  *
462  *                  This function is kept separate from SDP_FindServiceInDb
463  *                  since that API is expected to return only 16-bit UUIDs
464  *
465  * Returns          Pointer to record containing service class, or NULL
466  *
467  ******************************************************************************/
SDP_FindServiceInDb_128bit(const tSDP_DISCOVERY_DB * p_db,tSDP_DISC_REC * p_start_rec)468 tSDP_DISC_REC* SDP_FindServiceInDb_128bit(const tSDP_DISCOVERY_DB* p_db,
469                                           tSDP_DISC_REC* p_start_rec) {
470   tSDP_DISC_REC* p_rec;
471   tSDP_DISC_ATTR *p_attr, *p_sattr;
472 
473   /* Must have a valid database */
474   if (p_db == NULL) {
475     return NULL;
476   }
477 
478   if (!p_start_rec) {
479     p_rec = p_db->p_first_rec;
480   } else {
481     p_rec = p_start_rec->p_next_rec;
482   }
483 
484   while (p_rec) {
485     p_attr = p_rec->p_first_attr;
486     while (p_attr) {
487       if ((p_attr->attr_id == ATTR_ID_SERVICE_CLASS_ID_LIST) &&
488           (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE)) {
489         for (p_sattr = p_attr->attr_value.v.p_sub_attr; p_sattr; p_sattr = p_sattr->p_next_attr) {
490           if ((SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE) &&
491               (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 16)) {
492             return p_rec;
493           }
494         }
495         break;
496       } else if (p_attr->attr_id == ATTR_ID_SERVICE_ID) {
497         if ((SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == UUID_DESC_TYPE) &&
498             (SDP_DISC_ATTR_LEN(p_attr->attr_len_type) == 16)) {
499           return p_rec;
500         }
501       }
502 
503       p_attr = p_attr->p_next_attr;
504     }
505 
506     p_rec = p_rec->p_next_rec;
507   }
508   /* If here, no matching UUID found */
509   return NULL;
510 }
511 
512 /*******************************************************************************
513  *
514  * Function         SDP_FindServiceUUIDInDb
515  *
516  * Description      Query an SDP database for a specific service. If the
517  *                  p_start_rec pointer is NULL, it looks from the beginning of
518  *                  the database, else it continues from the next record after
519  *                  p_start_rec.
520  *
521  * NOTE             the only difference between this function and the previous
522  *                  function "SDP_FindServiceInDb()" is that this function takes
523  *                  a Uuid input
524  *
525  * Returns          Pointer to record containing service class, or NULL
526  *
527  ******************************************************************************/
SDP_FindServiceUUIDInDb(const tSDP_DISCOVERY_DB * p_db,const Uuid & uuid,tSDP_DISC_REC * p_start_rec)528 tSDP_DISC_REC* SDP_FindServiceUUIDInDb(const tSDP_DISCOVERY_DB* p_db, const Uuid& uuid,
529                                        tSDP_DISC_REC* p_start_rec) {
530   tSDP_DISC_REC* p_rec;
531   tSDP_DISC_ATTR *p_attr, *p_sattr;
532 
533   /* Must have a valid database */
534   if (p_db == NULL) {
535     return NULL;
536   }
537 
538   if (!p_start_rec) {
539     p_rec = p_db->p_first_rec;
540   } else {
541     p_rec = p_start_rec->p_next_rec;
542   }
543 
544   while (p_rec) {
545     p_attr = p_rec->p_first_attr;
546     while (p_attr) {
547       if ((p_attr->attr_id == ATTR_ID_SERVICE_CLASS_ID_LIST) &&
548           (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE)) {
549         for (p_sattr = p_attr->attr_value.v.p_sub_attr; p_sattr; p_sattr = p_sattr->p_next_attr) {
550           if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE) {
551             if (sdpu_compare_uuid_with_attr(uuid, p_sattr)) {
552               return p_rec;
553             }
554           }
555         }
556         break;
557       } else if (p_attr->attr_id == ATTR_ID_SERVICE_ID) {
558         if (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == UUID_DESC_TYPE) {
559           if (sdpu_compare_uuid_with_attr(uuid, p_attr)) {
560             return p_rec;
561           }
562         }
563       }
564 
565       p_attr = p_attr->p_next_attr;
566     }
567 
568     p_rec = p_rec->p_next_rec;
569   }
570   /* If here, no matching UUID found */
571   return NULL;
572 }
573 
574 /*******************************************************************************
575  *
576  * Function         sdp_fill_proto_elem
577  *
578  * Description      This function retrieves the protocol element.
579  *
580  * Returns          true if found, false if not
581  *                  If found, the passed protocol list element is filled in.
582  *
583  ******************************************************************************/
sdp_fill_proto_elem(const tSDP_DISC_ATTR * p_attr,uint16_t layer_uuid,tSDP_PROTOCOL_ELEM * p_elem)584 static bool sdp_fill_proto_elem(const tSDP_DISC_ATTR* p_attr, uint16_t layer_uuid,
585                                 tSDP_PROTOCOL_ELEM* p_elem) {
586   tSDP_DISC_ATTR* p_sattr;
587 
588   /* Walk through the protocol descriptor list */
589   for (p_attr = p_attr->attr_value.v.p_sub_attr; p_attr; p_attr = p_attr->p_next_attr) {
590     /* Safety check - each entry should itself be a sequence */
591     if (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) != DATA_ELE_SEQ_DESC_TYPE) {
592       return false;
593     }
594 
595     /* Now, see if the entry contains the layer we are interested in */
596     for (p_sattr = p_attr->attr_value.v.p_sub_attr; p_sattr; p_sattr = p_sattr->p_next_attr) {
597       /* LOG_VERBOSE ("SDP - p_sattr 0x%x, layer_uuid:0x%x, u16:0x%x####",
598           p_sattr, layer_uuid, p_sattr->attr_value.v.u16); */
599 
600       if ((SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE) &&
601           (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 2) &&
602           (p_sattr->attr_value.v.u16 == layer_uuid)) {
603         /* Bingo. Now fill in the passed element */
604         p_elem->protocol_uuid = layer_uuid;
605         p_elem->num_params = 0;
606 
607         /* Store the parameters, if any */
608         for (p_sattr = p_sattr->p_next_attr; p_sattr; p_sattr = p_sattr->p_next_attr) {
609           if (SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) != UINT_DESC_TYPE) {
610             break;
611           }
612 
613           if (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 2) {
614             p_elem->params[p_elem->num_params++] = p_sattr->attr_value.v.u16;
615           } else {
616             p_elem->params[p_elem->num_params++] = p_sattr->attr_value.v.u8;
617           }
618 
619           if (p_elem->num_params >= SDP_MAX_PROTOCOL_PARAMS) {
620             break;
621           }
622         }
623         return true;
624       }
625     }
626   }
627 
628   return false;
629 }
630 
631 /*******************************************************************************
632  *
633  * Function         SDP_FindProtocolListElemInRec
634  *
635  * Description      This function looks at a specific discovery record for a
636  *                  protocol list element.
637  *
638  * Returns          true if found, false if not
639  *                  If found, the passed protocol list element is filled in.
640  *
641  ******************************************************************************/
SDP_FindProtocolListElemInRec(const tSDP_DISC_REC * p_rec,uint16_t layer_uuid,tSDP_PROTOCOL_ELEM * p_elem)642 bool SDP_FindProtocolListElemInRec(const tSDP_DISC_REC* p_rec, uint16_t layer_uuid,
643                                    tSDP_PROTOCOL_ELEM* p_elem) {
644   tSDP_DISC_ATTR* p_attr;
645 
646   p_attr = p_rec->p_first_attr;
647   while (p_attr) {
648     /* Find the protocol descriptor list */
649     if ((p_attr->attr_id == ATTR_ID_PROTOCOL_DESC_LIST) &&
650         (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE)) {
651       return sdp_fill_proto_elem(p_attr, layer_uuid, p_elem);
652     }
653     p_attr = p_attr->p_next_attr;
654   }
655   /* If here, no match found */
656   return false;
657 }
658 
659 /*******************************************************************************
660  *
661  * Function         SDP_FindProfileVersionInRec
662  *
663  * Description      This function looks at a specific discovery record for the
664  *                  Profile list descriptor, and pulls out the version number.
665  *                  The version number consists of an 8-bit major version and
666  *                  an 8-bit minor version.
667  *
668  * Returns          true if found, false if not
669  *                  If found, the major and minor version numbers that were
670  *                  passed in are filled in.
671  *
672  ******************************************************************************/
SDP_FindProfileVersionInRec(const tSDP_DISC_REC * p_rec,uint16_t profile_uuid,uint16_t * p_version)673 bool SDP_FindProfileVersionInRec(const tSDP_DISC_REC* p_rec, uint16_t profile_uuid,
674                                  uint16_t* p_version) {
675   tSDP_DISC_ATTR *p_attr, *p_sattr;
676 
677   p_attr = p_rec->p_first_attr;
678   while (p_attr) {
679     /* Find the profile descriptor list */
680     if ((p_attr->attr_id == ATTR_ID_BT_PROFILE_DESC_LIST) &&
681         (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) == DATA_ELE_SEQ_DESC_TYPE)) {
682       /* Walk through the protocol descriptor list */
683       for (p_attr = p_attr->attr_value.v.p_sub_attr; p_attr; p_attr = p_attr->p_next_attr) {
684         /* Safety check - each entry should itself be a sequence */
685         if (SDP_DISC_ATTR_TYPE(p_attr->attr_len_type) != DATA_ELE_SEQ_DESC_TYPE) {
686           return false;
687         }
688 
689         /* Now, see if the entry contains the profile UUID we are interested in
690          */
691         for (p_sattr = p_attr->attr_value.v.p_sub_attr; p_sattr; p_sattr = p_sattr->p_next_attr) {
692           if ((SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UUID_DESC_TYPE) &&
693               (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) ==
694                2) /* <- This is bytes, not size code! */
695               && (p_sattr->attr_value.v.u16 == profile_uuid)) {
696             /* Now fill in the major and minor numbers */
697             /* if the attribute matches the description for version (type UINT,
698              * size 2 bytes) */
699             p_sattr = p_sattr->p_next_attr;
700 
701             if ((SDP_DISC_ATTR_TYPE(p_sattr->attr_len_type) == UINT_DESC_TYPE) &&
702                 (SDP_DISC_ATTR_LEN(p_sattr->attr_len_type) == 2)) {
703               /* The high order 8 bits is the major number, low order is the
704                * minor number (big endian) */
705               *p_version = p_sattr->attr_value.v.u16;
706 
707               return true;
708             } else {
709               return false;  // The type and/or size was not valid for the
710                              //   profile list version
711             }
712           }
713         }
714       }
715 
716       return false;
717     }
718     p_attr = p_attr->p_next_attr;
719   }
720 
721   /* If here, no match found */
722   return false;
723 }
724 
725 /*******************************************************************************
726  *                   Device Identification (DI) Client Functions
727  ******************************************************************************/
728 
729 /*******************************************************************************
730  *
731  * Function         SDP_DiDiscover
732  *
733  * Description      This function queries a remote device for DI information.
734  *
735  * Returns          tSDP_STATUS::SDP_SUCCESS if query started successfully, else error
736  *
737  ******************************************************************************/
SDP_DiDiscover(const RawAddress & remote_device,tSDP_DISCOVERY_DB * p_db,uint32_t len,tSDP_DISC_CMPL_CB * p_cb)738 tSDP_STATUS SDP_DiDiscover(const RawAddress& remote_device, tSDP_DISCOVERY_DB* p_db, uint32_t len,
739                            tSDP_DISC_CMPL_CB* p_cb) {
740   tSDP_STATUS result = tSDP_STATUS::SDP_DI_DISC_FAILED;
741   uint16_t num_uuids = 1;
742   uint16_t di_uuid = UUID_SERVCLASS_PNP_INFORMATION;
743 
744   /* build uuid for db init */
745   Uuid init_uuid = Uuid::From16Bit(di_uuid);
746 
747   if (SDP_InitDiscoveryDb(p_db, len, num_uuids, &init_uuid, 0, NULL)) {
748     if (SDP_ServiceSearchRequest(remote_device, p_db, p_cb)) {
749       result = tSDP_STATUS::SDP_SUCCESS;
750     }
751   }
752 
753   return result;
754 }
755 
756 /*******************************************************************************
757  *
758  * Function         SDP_GetNumDiRecords
759  *
760  * Description      Searches specified database for DI records
761  *
762  * Returns          number of DI records found
763  *
764  ******************************************************************************/
SDP_GetNumDiRecords(const tSDP_DISCOVERY_DB * p_db)765 uint8_t SDP_GetNumDiRecords(const tSDP_DISCOVERY_DB* p_db) {
766   uint8_t num_records = 0;
767   tSDP_DISC_REC* p_curr_record = NULL;
768 
769   do {
770     p_curr_record = SDP_FindServiceInDb(p_db, UUID_SERVCLASS_PNP_INFORMATION, p_curr_record);
771     if (p_curr_record) {
772       num_records++;
773     }
774   } while (p_curr_record);
775 
776   return num_records;
777 }
778 
779 /*******************************************************************************
780  *
781  * Function         SDP_AttrStringCopy
782  *
783  * Description      This function copy given attribute to specified buffer as a
784  *                  string
785  *
786  * Returns          none
787  *
788  ******************************************************************************/
SDP_AttrStringCopy(char * dst,const tSDP_DISC_ATTR * p_attr,uint16_t dst_size,uint8_t expected_type)789 static void SDP_AttrStringCopy(char* dst, const tSDP_DISC_ATTR* p_attr, uint16_t dst_size,
790                                uint8_t expected_type) {
791   if (dst == NULL) {
792     return;
793   }
794 
795   dst[0] = '\0';
796 
797   if (p_attr) {
798     uint8_t type = SDP_DISC_ATTR_TYPE(p_attr->attr_len_type);
799 
800     if (type == expected_type) {
801       uint16_t len = SDP_DISC_ATTR_LEN(p_attr->attr_len_type);
802       if (len > dst_size - 1) {
803         len = dst_size - 1;
804       }
805       memcpy(dst, (const void*)p_attr->attr_value.v.array, len);
806       dst[len] = '\0';
807     } else {
808       log::error("unexpected attr type={}, expected={}", type, expected_type);
809     }
810   } else {
811     log::error("p_attr is NULL");
812   }
813 }
814 
815 /*******************************************************************************
816  *
817  * Function         SDP_GetDiRecord
818  *
819  * Description      This function retrieves a remote device's DI record from
820  *                  the specified database.
821  *
822  * Returns          tSDP_STATUS::SDP_SUCCESS if record retrieved, else error
823  *
824  ******************************************************************************/
SDP_GetDiRecord(uint8_t get_record_index,tSDP_DI_GET_RECORD * p_device_info,const tSDP_DISCOVERY_DB * p_db)825 tSDP_STATUS SDP_GetDiRecord(uint8_t get_record_index, tSDP_DI_GET_RECORD* p_device_info,
826                             const tSDP_DISCOVERY_DB* p_db) {
827   tSDP_STATUS result = tSDP_STATUS::SDP_NO_DI_RECORD_FOUND;
828   uint8_t curr_record_index = 1;
829 
830   tSDP_DISC_REC* p_curr_record = NULL;
831 
832   /* find the requested SDP record in the discovery database */
833   do {
834     p_curr_record = SDP_FindServiceInDb(p_db, UUID_SERVCLASS_PNP_INFORMATION, p_curr_record);
835     if (p_curr_record) {
836       if (curr_record_index++ == get_record_index) {
837         result = tSDP_STATUS::SDP_SUCCESS;
838         break;
839       }
840     }
841   } while (p_curr_record);
842 
843   if (result == tSDP_STATUS::SDP_SUCCESS) {
844     /* copy the information from the SDP record to the DI record */
845     tSDP_DISC_ATTR* p_curr_attr = NULL;
846 
847     /* ClientExecutableURL is optional */
848     p_curr_attr = SDP_FindAttributeInRec(p_curr_record, ATTR_ID_CLIENT_EXE_URL);
849     SDP_AttrStringCopy(p_device_info->rec.client_executable_url, p_curr_attr, SDP_MAX_ATTR_LEN,
850                        URL_DESC_TYPE);
851 
852     /* Service Description is optional */
853     /* 5.1.16 ServiceDescription attribute */
854     p_curr_attr = SDP_FindAttributeInRec(p_curr_record, ATTR_ID_SERVICE_DESCRIPTION);
855     SDP_AttrStringCopy(p_device_info->rec.service_description, p_curr_attr, SDP_MAX_ATTR_LEN,
856                        TEXT_STR_DESC_TYPE);
857 
858     /* DocumentationURL is optional */
859     p_curr_attr = SDP_FindAttributeInRec(p_curr_record, ATTR_ID_DOCUMENTATION_URL);
860     SDP_AttrStringCopy(p_device_info->rec.documentation_url, p_curr_attr, SDP_MAX_ATTR_LEN,
861                        URL_DESC_TYPE);
862 
863     p_curr_attr = SDP_FindAttributeInRec(p_curr_record, ATTR_ID_SPECIFICATION_ID);
864     if (p_curr_attr && SDP_DISC_ATTR_TYPE(p_curr_attr->attr_len_type) == UINT_DESC_TYPE &&
865         SDP_DISC_ATTR_LEN(p_curr_attr->attr_len_type) >= 2) {
866       p_device_info->spec_id = p_curr_attr->attr_value.v.u16;
867     } else {
868       result = tSDP_STATUS::SDP_ERR_ATTR_NOT_PRESENT;
869     }
870 
871     p_curr_attr = SDP_FindAttributeInRec(p_curr_record, ATTR_ID_VENDOR_ID);
872     if (p_curr_attr && SDP_DISC_ATTR_TYPE(p_curr_attr->attr_len_type) == UINT_DESC_TYPE &&
873         SDP_DISC_ATTR_LEN(p_curr_attr->attr_len_type) >= 2) {
874       p_device_info->rec.vendor = p_curr_attr->attr_value.v.u16;
875     } else {
876       result = tSDP_STATUS::SDP_ERR_ATTR_NOT_PRESENT;
877     }
878 
879     p_curr_attr = SDP_FindAttributeInRec(p_curr_record, ATTR_ID_VENDOR_ID_SOURCE);
880     if (p_curr_attr && SDP_DISC_ATTR_TYPE(p_curr_attr->attr_len_type) == UINT_DESC_TYPE &&
881         SDP_DISC_ATTR_LEN(p_curr_attr->attr_len_type) >= 2) {
882       p_device_info->rec.vendor_id_source = p_curr_attr->attr_value.v.u16;
883     } else {
884       result = tSDP_STATUS::SDP_ERR_ATTR_NOT_PRESENT;
885     }
886 
887     p_curr_attr = SDP_FindAttributeInRec(p_curr_record, ATTR_ID_PRODUCT_ID);
888     if (p_curr_attr && SDP_DISC_ATTR_TYPE(p_curr_attr->attr_len_type) == UINT_DESC_TYPE &&
889         SDP_DISC_ATTR_LEN(p_curr_attr->attr_len_type) >= 2) {
890       p_device_info->rec.product = p_curr_attr->attr_value.v.u16;
891     } else {
892       result = tSDP_STATUS::SDP_ERR_ATTR_NOT_PRESENT;
893     }
894 
895     p_curr_attr = SDP_FindAttributeInRec(p_curr_record, ATTR_ID_PRODUCT_VERSION);
896     if (p_curr_attr && SDP_DISC_ATTR_TYPE(p_curr_attr->attr_len_type) == UINT_DESC_TYPE &&
897         SDP_DISC_ATTR_LEN(p_curr_attr->attr_len_type) >= 2) {
898       p_device_info->rec.version = p_curr_attr->attr_value.v.u16;
899     } else {
900       result = tSDP_STATUS::SDP_ERR_ATTR_NOT_PRESENT;
901     }
902 
903     p_curr_attr = SDP_FindAttributeInRec(p_curr_record, ATTR_ID_PRIMARY_RECORD);
904     if (p_curr_attr && SDP_DISC_ATTR_TYPE(p_curr_attr->attr_len_type) == BOOLEAN_DESC_TYPE &&
905         SDP_DISC_ATTR_LEN(p_curr_attr->attr_len_type) >= 1) {
906       p_device_info->rec.primary_record = (bool)p_curr_attr->attr_value.v.u8;
907     } else {
908       result = tSDP_STATUS::SDP_ERR_ATTR_NOT_PRESENT;
909     }
910   }
911 
912   return result;
913 }
914 
915 /*******************************************************************************
916  *                   Device Identification (DI) Server Functions
917  ******************************************************************************/
918 
919 /*******************************************************************************
920  *
921  * Function         SDP_SetLocalDiRecord
922  *
923  * Description      This function adds a DI record to the local SDP database.
924  *
925  *
926  *
927  * Returns          Returns tSDP_STATUS::SDP_SUCCESS if record added successfully, else error
928  *
929  ******************************************************************************/
SDP_SetLocalDiRecord(const tSDP_DI_RECORD * p_device_info,uint32_t * p_handle)930 tSDP_STATUS SDP_SetLocalDiRecord(const tSDP_DI_RECORD* p_device_info, uint32_t* p_handle) {
931   tSDP_STATUS result = tSDP_STATUS::SDP_SUCCESS;
932   uint32_t handle;
933   uint16_t di_uuid = UUID_SERVCLASS_PNP_INFORMATION;
934   uint16_t di_specid = BLUETOOTH_DI_SPECIFICATION;
935   uint8_t temp_u16[2];
936   uint8_t* p_temp;
937   uint8_t u8;
938 
939   *p_handle = 0;
940   if (p_device_info == NULL) {
941     return tSDP_STATUS::SDP_ILLEGAL_PARAMETER;
942   }
943 
944   /* if record is to be primary record, get handle to replace old primary */
945   if (p_device_info->primary_record && sdp_cb.server_db.di_primary_handle) {
946     handle = sdp_cb.server_db.di_primary_handle;
947   } else {
948     handle = SDP_CreateRecord();
949     if (handle == 0) {
950       return tSDP_STATUS::SDP_NO_RESOURCES;
951     }
952   }
953 
954   *p_handle = handle;
955 
956   /* build the SDP entry */
957   /* Add the UUID to the Service Class ID List */
958   if (!(SDP_AddServiceClassIdList(handle, 1, &di_uuid))) {
959     result = tSDP_STATUS::SDP_DI_REG_FAILED;
960   }
961 
962   /* mandatory */
963   if (result == tSDP_STATUS::SDP_SUCCESS) {
964     p_temp = temp_u16;
965     UINT16_TO_BE_STREAM(p_temp, di_specid);
966     if (!(SDP_AddAttribute(handle, ATTR_ID_SPECIFICATION_ID, UINT_DESC_TYPE, sizeof(di_specid),
967                            temp_u16))) {
968       result = tSDP_STATUS::SDP_DI_REG_FAILED;
969     }
970   }
971 
972   /* optional - if string is null, do not add attribute */
973   if (result == tSDP_STATUS::SDP_SUCCESS) {
974     if (p_device_info->client_executable_url[0] != '\0') {
975       if (!((strlen(p_device_info->client_executable_url) + 1 <= SDP_MAX_ATTR_LEN) &&
976             SDP_AddAttribute(handle, ATTR_ID_CLIENT_EXE_URL, URL_DESC_TYPE,
977                              (uint32_t)(strlen(p_device_info->client_executable_url) + 1),
978                              (uint8_t*)p_device_info->client_executable_url))) {
979         result = tSDP_STATUS::SDP_DI_REG_FAILED;
980       }
981     }
982   }
983 
984   /* optional - if string is null, do not add attribute */
985   if (result == tSDP_STATUS::SDP_SUCCESS) {
986     if (p_device_info->service_description[0] != '\0') {
987       if (!((strlen(p_device_info->service_description) + 1 <= SDP_MAX_ATTR_LEN) &&
988             SDP_AddAttribute(handle, ATTR_ID_SERVICE_DESCRIPTION, TEXT_STR_DESC_TYPE,
989                              (uint32_t)(strlen(p_device_info->service_description) + 1),
990                              (uint8_t*)p_device_info->service_description))) {
991         result = tSDP_STATUS::SDP_DI_REG_FAILED;
992       }
993     }
994   }
995 
996   /* optional - if string is null, do not add attribute */
997   if (result == tSDP_STATUS::SDP_SUCCESS) {
998     if (p_device_info->documentation_url[0] != '\0') {
999       if (!((strlen(p_device_info->documentation_url) + 1 <= SDP_MAX_ATTR_LEN) &&
1000             SDP_AddAttribute(handle, ATTR_ID_DOCUMENTATION_URL, URL_DESC_TYPE,
1001                              (uint32_t)(strlen(p_device_info->documentation_url) + 1),
1002                              (uint8_t*)p_device_info->documentation_url))) {
1003         result = tSDP_STATUS::SDP_DI_REG_FAILED;
1004       }
1005     }
1006   }
1007 
1008   /* mandatory */
1009   if (result == tSDP_STATUS::SDP_SUCCESS) {
1010     p_temp = temp_u16;
1011     UINT16_TO_BE_STREAM(p_temp, p_device_info->vendor);
1012     if (!(SDP_AddAttribute(handle, ATTR_ID_VENDOR_ID, UINT_DESC_TYPE, sizeof(p_device_info->vendor),
1013                            temp_u16))) {
1014       result = tSDP_STATUS::SDP_DI_REG_FAILED;
1015     }
1016   }
1017 
1018   /* mandatory */
1019   if (result == tSDP_STATUS::SDP_SUCCESS) {
1020     p_temp = temp_u16;
1021     UINT16_TO_BE_STREAM(p_temp, p_device_info->product);
1022     if (!(SDP_AddAttribute(handle, ATTR_ID_PRODUCT_ID, UINT_DESC_TYPE,
1023                            sizeof(p_device_info->product), temp_u16))) {
1024       result = tSDP_STATUS::SDP_DI_REG_FAILED;
1025     }
1026   }
1027 
1028   /* mandatory */
1029   if (result == tSDP_STATUS::SDP_SUCCESS) {
1030     p_temp = temp_u16;
1031     UINT16_TO_BE_STREAM(p_temp, p_device_info->version);
1032     if (!(SDP_AddAttribute(handle, ATTR_ID_PRODUCT_VERSION, UINT_DESC_TYPE,
1033                            sizeof(p_device_info->version), temp_u16))) {
1034       result = tSDP_STATUS::SDP_DI_REG_FAILED;
1035     }
1036   }
1037 
1038   /* mandatory */
1039   if (result == tSDP_STATUS::SDP_SUCCESS) {
1040     u8 = (uint8_t)p_device_info->primary_record;
1041     if (!(SDP_AddAttribute(handle, ATTR_ID_PRIMARY_RECORD, BOOLEAN_DESC_TYPE, 1, &u8))) {
1042       result = tSDP_STATUS::SDP_DI_REG_FAILED;
1043     }
1044   }
1045 
1046   /* mandatory */
1047   if (result == tSDP_STATUS::SDP_SUCCESS) {
1048     p_temp = temp_u16;
1049     UINT16_TO_BE_STREAM(p_temp, p_device_info->vendor_id_source);
1050     if (!(SDP_AddAttribute(handle, ATTR_ID_VENDOR_ID_SOURCE, UINT_DESC_TYPE,
1051                            sizeof(p_device_info->vendor_id_source), temp_u16))) {
1052       result = tSDP_STATUS::SDP_DI_REG_FAILED;
1053     }
1054   }
1055 
1056   if (result != tSDP_STATUS::SDP_SUCCESS) {
1057     SDP_DeleteRecord(handle);
1058   } else if (p_device_info->primary_record) {
1059     sdp_cb.server_db.di_primary_handle = handle;
1060   }
1061 
1062   return result;
1063 }
1064 
1065 namespace {
1066 bluetooth::legacy::stack::sdp::tSdpApi api_ = {
1067         .service =
1068                 {
1069                         .SDP_InitDiscoveryDb = ::SDP_InitDiscoveryDb,
1070                         .SDP_CancelServiceSearch = ::SDP_CancelServiceSearch,
1071                         .SDP_ServiceSearchRequest = ::SDP_ServiceSearchRequest,
1072                         .SDP_ServiceSearchAttributeRequest = ::SDP_ServiceSearchAttributeRequest,
1073                         .SDP_ServiceSearchAttributeRequest2 = ::SDP_ServiceSearchAttributeRequest2,
1074                 },
1075         .db =
1076                 {
1077                         .SDP_FindServiceInDb = ::SDP_FindServiceInDb,
1078                         .SDP_FindServiceUUIDInDb = ::SDP_FindServiceUUIDInDb,
1079                         .SDP_FindServiceInDb_128bit = ::SDP_FindServiceInDb_128bit,
1080                 },
1081         .record =
1082                 {
1083                         .SDP_FindAttributeInRec = ::SDP_FindAttributeInRec,
1084                         .SDP_FindServiceUUIDInRec_128bit = ::SDP_FindServiceUUIDInRec_128bit,
1085                         .SDP_FindProtocolListElemInRec = ::SDP_FindProtocolListElemInRec,
1086                         .SDP_FindProfileVersionInRec = ::SDP_FindProfileVersionInRec,
1087                         .SDP_FindServiceUUIDInRec = ::SDP_FindServiceUUIDInRec,
1088                 },
1089         .handle =
1090                 {
1091                         .SDP_CreateRecord = ::SDP_CreateRecord,
1092                         .SDP_DeleteRecord = ::SDP_DeleteRecord,
1093                         .SDP_AddAttribute = ::SDP_AddAttribute,
1094                         .SDP_AddSequence = ::SDP_AddSequence,
1095                         .SDP_AddUuidSequence = ::SDP_AddUuidSequence,
1096                         .SDP_AddProtocolList = ::SDP_AddProtocolList,
1097                         .SDP_AddAdditionProtoLists = ::SDP_AddAdditionProtoLists,
1098                         .SDP_AddProfileDescriptorList = ::SDP_AddProfileDescriptorList,
1099                         .SDP_AddLanguageBaseAttrIDList = ::SDP_AddLanguageBaseAttrIDList,
1100                         .SDP_AddServiceClassIdList = ::SDP_AddServiceClassIdList,
1101                 },
1102         .device_id =
1103                 {
1104                         .SDP_SetLocalDiRecord = ::SDP_SetLocalDiRecord,
1105                         .SDP_DiDiscover = ::SDP_DiDiscover,
1106                         .SDP_GetNumDiRecords = ::SDP_GetNumDiRecords,
1107                         .SDP_GetDiRecord = ::SDP_GetDiRecord,
1108                 },
1109 };
1110 }  // namespace
1111 
1112 const bluetooth::legacy::stack::sdp::tSdpApi*
get_legacy_stack_sdp_api()1113 bluetooth::legacy::stack::sdp::get_legacy_stack_sdp_api() {
1114   return &api_;
1115 }
1116 
1117 extern void BTA_SdpDumpsys(int fd);
1118 
1119 #define DUMPSYS_TAG "shim::legacy::sdp"
1120 
1121 namespace {
1122 
SDP_DumpConnectionControlBlock(int fd,const tCONN_CB & conn_cb)1123 void SDP_DumpConnectionControlBlock(int fd, const tCONN_CB& conn_cb) {
1124   if (conn_cb.device_address == RawAddress::kEmpty) {
1125     return;
1126   }
1127   LOG_DUMPSYS(fd, "peer:%s discovery_state:%s", std::format("{}", conn_cb.device_address).c_str(),
1128               sdp_disc_wait_text(conn_cb.disc_state).c_str());
1129   LOG_DUMPSYS(fd, "  connection_state:%s connection_flags:0x%02x mtu:%hu l2cap_cid:%hu",
1130               sdp_state_text(conn_cb.con_state).c_str(), conn_cb.con_flags, conn_cb.rem_mtu_size,
1131               conn_cb.connection_id);
1132 
1133   const uint64_t remaining_ms = alarm_get_remaining_ms(conn_cb.sdp_conn_timer);
1134   if (remaining_ms) {
1135     LOG_DUMPSYS(fd, "  timer_set:%Lu ms", static_cast<long long>(remaining_ms));
1136   }
1137   if (conn_cb.num_handles >= kMaxSdpRecords) {
1138     LOG_DUMPSYS(fd, "  WARNING - Number handles:%hu exceeds max handles:%u", conn_cb.num_handles,
1139                 kMaxSdpRecords);
1140   } else {
1141     for (int i = 0; i < conn_cb.num_handles; i++) {
1142       LOG_DUMPSYS(fd, "  handle:%u", conn_cb.handles[i]);
1143     }
1144   }
1145 }
1146 
1147 }  // namespace
1148 
SDP_Dumpsys(int fd)1149 void SDP_Dumpsys(int fd) {
1150   LOG_DUMPSYS_TITLE(fd, DUMPSYS_TAG);
1151   LOG_DUMPSYS(fd, "max_attribute_list_size:%hu max_records_per_search:%hu",
1152               sdp_cb.max_attr_list_size, sdp_cb.max_recs_per_search);
1153   for (unsigned i = 0; i < kMaxSdpConnections; i++) {
1154     SDP_DumpConnectionControlBlock(fd, sdp_cb.ccb[i]);
1155   }
1156 }
1157 #undef DUMPSYS_TAG
1158