1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <cstdint>
20 #include <cstring>
21 
22 #include "osi/include/compat.h"  // strlcpy
23 
24 #define BD_NAME_LEN 248
25 typedef uint8_t BD_NAME[BD_NAME_LEN + 1]; /* Device name */
26 
27 inline constexpr BD_NAME kBtmBdNameEmpty = {};
28 constexpr size_t kBdNameLength = static_cast<size_t>(BD_NAME_LEN);
29 constexpr uint8_t kBdNameDelim = (uint8_t)NULL;
30 
bd_name_copy(BD_NAME bd_name_dest,const BD_NAME bd_name_src)31 inline size_t bd_name_copy(BD_NAME bd_name_dest, const BD_NAME bd_name_src) {
32   return strlcpy(reinterpret_cast<char*>(bd_name_dest),
33                  reinterpret_cast<const char*>(bd_name_src), kBdNameLength + 1);
34 }
bd_name_clear(BD_NAME bd_name)35 inline void bd_name_clear(BD_NAME bd_name) { *bd_name = {0}; }
bd_name_is_empty(const BD_NAME bd_name)36 inline bool bd_name_is_empty(const BD_NAME bd_name) {
37   return bd_name[0] == '\0';
38 }
39 
bd_name_from_char_pointer(BD_NAME bd_name_dest,const char * bd_name_char)40 inline void bd_name_from_char_pointer(BD_NAME bd_name_dest,
41                                       const char* bd_name_char) {
42   if (bd_name_char == nullptr) {
43     bd_name_clear(bd_name_dest);
44     return;
45   }
46 
47   size_t src_len = strlcpy(reinterpret_cast<char*>(bd_name_dest), bd_name_char,
48                            sizeof(BD_NAME));
49   if (src_len < sizeof(BD_NAME) - 1) {
50     /* Zero the remaining destination memory */
51     memset(bd_name_dest + src_len, 0, sizeof(BD_NAME) - src_len);
52   }
53 }
bd_name_is_equal(const BD_NAME bd_name1,const BD_NAME bd_name2)54 inline bool bd_name_is_equal(const BD_NAME bd_name1, const BD_NAME bd_name2) {
55   return memcmp(reinterpret_cast<void*>(const_cast<uint8_t*>(bd_name1)),
56                 reinterpret_cast<void*>(const_cast<uint8_t*>(bd_name2)),
57                 kBdNameLength + 1) == 0;
58 }
59