• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License")
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef LOG_TAG
16 #define LOG_TAG "bt_cj_connection_common"
17 #endif
18 
19 #include "bluetooth_connection_common.h"
20 
21 #include <regex>
22 
23 #include "bluetooth_def.h"
24 #include "bluetooth_log.h"
25 
26 namespace OHOS {
27 namespace CJSystemapi {
28 namespace CJBluetoothConnection {
29 
30 using Bluetooth::PAIR_NONE;
31 using Bluetooth::PAIR_PAIRED;
32 using Bluetooth::PAIR_PAIRING;
33 using Bluetooth::PIN_TYPE_CONFIRM_PASSKEY;
34 using Bluetooth::PIN_TYPE_NOTIFY_PASSKEY;
35 
IsValidAddress(std::string bdaddr)36 bool IsValidAddress(std::string bdaddr)
37 {
38 #if defined(IOS_PLATFORM)
39     const std::regex deviceIdRegex("^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$");
40     return regex_match(bdaddr, deviceIdRegex);
41 #else
42     const std::regex deviceIdRegex("^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$");
43     return regex_match(bdaddr, deviceIdRegex);
44 #endif
45 }
46 
GetFormatPinCode(uint32_t pinType,uint32_t pinCode)47 std::string GetFormatPinCode(uint32_t pinType, uint32_t pinCode)
48 {
49     std::string pinCodeStr = std::to_string(pinCode);
50     if (pinType != PIN_TYPE_CONFIRM_PASSKEY && pinType != PIN_TYPE_NOTIFY_PASSKEY) {
51         return pinCodeStr;
52     }
53 
54     const uint32_t FORMAT_PINCODE_LENGTH = 6;
55     while (pinCodeStr.length() < FORMAT_PINCODE_LENGTH) {
56         pinCodeStr = "0" + pinCodeStr;
57     }
58     return pinCodeStr;
59 }
60 
MallocCString(const std::string & origin)61 char* MallocCString(const std::string& origin)
62 {
63     if (origin.empty()) {
64         return nullptr;
65     }
66     auto length = origin.length() + 1;
67     char* res = static_cast<char*>(malloc(sizeof(char) * length));
68     if (res == nullptr) {
69         return nullptr;
70     }
71     return std::char_traits<char>::copy(res, origin.c_str(), length);
72 }
73 
Convert2CArrString(std::vector<std::string> & tids)74 CArrString Convert2CArrString(std::vector<std::string>& tids)
75 {
76     CArrString res { 0 };
77     if (tids.empty()) {
78         return res;
79     }
80 
81     size_t size = tids.size();
82     if (size == 0 || size > std::numeric_limits<size_t>::max() / sizeof(char*)) {
83         return res;
84     }
85     res.head = static_cast<char**>(malloc(sizeof(char*) * size));
86     if (!res.head) {
87         return res;
88     }
89 
90     size_t i = 0;
91     for (; i < size; ++i) {
92         res.head[i] = MallocCString(tids[i]);
93     }
94     res.size = static_cast<int64_t>(i);
95 
96     return res;
97 }
98 
DealPairStatus(const int & status,int & bondStatus)99 void DealPairStatus(const int& status, int& bondStatus)
100 {
101     HILOGD("status is %{public}d", status);
102     switch (status) {
103         case PAIR_NONE:
104             bondStatus = static_cast<int>(BondState::BOND_STATE_INVALID);
105             break;
106         case PAIR_PAIRING:
107             bondStatus = static_cast<int>(BondState::BOND_STATE_BONDING);
108             break;
109         case PAIR_PAIRED:
110             bondStatus = static_cast<int>(BondState::BOND_STATE_BONDED);
111             break;
112         default:
113             break;
114     }
115 }
116 
117 } // namespace CJBluetoothConnection
118 } // namespace CJSystemapi
119 } // namespace OHOS