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 
bd_name_copy(BD_NAME bd_name_dest,const BD_NAME bd_name_src)30 inline size_t bd_name_copy(BD_NAME bd_name_dest, const BD_NAME bd_name_src) {
31   return osi_strlcpy(reinterpret_cast<char*>(bd_name_dest),
32                      reinterpret_cast<const char*>(bd_name_src), kBdNameLength + 1);
33 }
bd_name_clear(BD_NAME bd_name)34 inline void bd_name_clear(BD_NAME bd_name) { *bd_name = {0}; }
bd_name_is_empty(const BD_NAME bd_name)35 inline bool bd_name_is_empty(const BD_NAME bd_name) { return bd_name[0] == '\0'; }
36 
bd_name_from_char_pointer(BD_NAME bd_name_dest,const char * bd_name_char)37 inline void bd_name_from_char_pointer(BD_NAME bd_name_dest, const char* bd_name_char) {
38   if (bd_name_char == nullptr) {
39     bd_name_clear(bd_name_dest);
40     return;
41   }
42 
43   size_t src_len =
44           osi_strlcpy(reinterpret_cast<char*>(bd_name_dest), bd_name_char, sizeof(BD_NAME));
45   if (src_len < sizeof(BD_NAME) - 1) {
46     /* Zero the remaining destination memory */
47     memset(bd_name_dest + src_len, 0, sizeof(BD_NAME) - src_len);
48   }
49 }
bd_name_is_equal(const BD_NAME bd_name1,const BD_NAME bd_name2)50 inline bool bd_name_is_equal(const BD_NAME bd_name1, const BD_NAME bd_name2) {
51   return memcmp(reinterpret_cast<void*>(const_cast<uint8_t*>(bd_name1)),
52                 reinterpret_cast<void*>(const_cast<uint8_t*>(bd_name2)), kBdNameLength + 1) == 0;
53 }
54