1 /*
2 * Copyright (C) 2022 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_fwk_utils"
17 #endif
18
19 #include "bluetooth_utils.h"
20 #include <map>
21 #include <chrono>
22 #include <random>
23 #if defined(IOS_PLATFORM)
24 #include <regex>
25 #endif
26 #include "securec.h"
27 #include "__config"
28 #include "bluetooth_def.h"
29 #include "bluetooth_host_proxy.h"
30 #include "bluetooth_log.h"
31 #include "iosfwd"
32 #include "iservice_registry.h"
33 #include "string"
34 #include "system_ability_definition.h"
35
36 using namespace std;
37
38 namespace OHOS {
39 namespace Bluetooth {
40 constexpr int startPos = 6;
41 constexpr int endPos = 13;
42 #if defined(IOS_PLATFORM)
43 constexpr int START_POS_IOS_PLATFORM = 9;
44 constexpr int END_POS_IOS_PLATFORM = 22;
45 #endif
46 constexpr int RANDOM_ADDR_ARRAY_SIZE = 4;
47 constexpr int RANDOM_ADDR_MAC_BIT_SIZE = 12;
48 constexpr int RANDOM_ADDR_FIRST_BIT = 1;
49 constexpr int RANDOM_ADDR_LAST_BIT = 11;
50 constexpr int RANDOM_ADDR_SPLIT_SIZE = 2;
51 constexpr int HEX_BASE = 16;
52 constexpr int OCT_BASE = 8;
53
GetEncryptAddr(std::string addr)54 std::string GetEncryptAddr(std::string addr)
55 {
56 #if defined(IOS_PLATFORM)
57 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}$");
58 if (!regex_match(addr, deviceIdRegex)) {
59 return std::string("");
60 }
61 std::string tmp = "********-****-****-****-************";
62 std::string out = addr;
63 for (int i = START_POS_IOS_PLATFORM; i <= END_POS_IOS_PLATFORM; i++) {
64 out[i] = tmp[i];
65 }
66 return out;
67 #else
68 if (addr.empty() || addr.length() != ADDRESS_LENGTH) {
69 HILOGD("addr is invalid.");
70 return std::string("");
71 }
72 std::string tmp = "**:**:**:**:**:**";
73 std::string out = addr;
74 // 00:01:**:**:**:05
75 for (int i = startPos; i <= endPos; i++) {
76 out[i] = tmp[i];
77 }
78 return out;
79 #endif
80 }
81
GetBtStateName(int state)82 std::string GetBtStateName(int state)
83 {
84 switch (state) {
85 case BTStateID::STATE_TURNING_ON:
86 return "STATE_TURNING_ON(0)";
87 case BTStateID::STATE_TURN_ON:
88 return "STATE_TURN_ON(1)";
89 case BTStateID::STATE_TURNING_OFF:
90 return "STATE_TURNING_OFF(2)";
91 case BTStateID::STATE_TURN_OFF:
92 return "STATE_TURN_OFF(3)";
93 default:
94 return "Unknown";
95 }
96 }
97
GetBtTransportName(int transport)98 std::string GetBtTransportName(int transport)
99 {
100 switch (transport) {
101 case BTTransport::ADAPTER_BREDR:
102 return "ADAPTER_BREDR(0)";
103 case BTTransport::ADAPTER_BLE:
104 return "ADAPTER_BLE(1)";
105 default:
106 return "Unknown";
107 }
108 }
109
GetProfileConnStateName(int state)110 std::string GetProfileConnStateName(int state)
111 {
112 switch (state) {
113 case static_cast<int>(BTConnectState::CONNECTING):
114 return "CONNECTING(0)";
115 case static_cast<int>(BTConnectState::CONNECTED):
116 return "CONNECTED(1)";
117 case static_cast<int>(BTConnectState::DISCONNECTING):
118 return "DISCONNECTING(2)";
119 case static_cast<int>(BTConnectState::DISCONNECTED):
120 return "DISCONNECTED(3)";
121 default:
122 return "Unknown";
123 }
124 }
125
GetUpdateOutputStackActionName(int action)126 std::string GetUpdateOutputStackActionName(int action)
127 {
128 switch (action) {
129 case static_cast<int>(UpdateOutputStackAction::ACTION_WEAR):
130 return "WEAR(0)";
131 case static_cast<int>(UpdateOutputStackAction::ACTION_UNWEAR):
132 return "UNWEAR(1)";
133 case static_cast<int>(UpdateOutputStackAction::ACTION_ENABLE_FROM_REMOTE):
134 return "ENABLE_FROM_REMOTE(2)";
135 case static_cast<int>(UpdateOutputStackAction::ACTION_DISABLE_FROM_REMOTE):
136 return "DISABLE_FROM_REMOTE(3)";
137 case static_cast<int>(UpdateOutputStackAction::ACTION_ENABLE_WEAR_DETECTION):
138 return "ENABLE_WEAR_DETECTION(4)";
139 case static_cast<int>(UpdateOutputStackAction::ACTION_DISABLE_WEAR_DETECTION):
140 return "DISABLE_WEAR_DETECTION(5)";
141 case static_cast<int>(UpdateOutputStackAction::ACTION_USER_OPERATION):
142 return "USER_OPERATION(6)";
143 case static_cast<int>(UpdateOutputStackAction::ACTION_STOP_VIRTUAL_CALL):
144 return "STOP_VIRTUAL_CALL(7)";
145 default:
146 return "Unknown";
147 }
148 }
149
150 static std::map<int32_t, std::string> BtErrCodeMap {
151 { BtErrCode::BT_NO_ERROR, "BT_NO_ERROR" },
152 { BtErrCode::BT_ERR_PERMISSION_FAILED, "BT_ERR_PERMISSION_FAILED" },
153 { BtErrCode::BT_ERR_SYSTEM_PERMISSION_FAILED, "BT_ERR_SYSTEM_PERMISSION_FAILED" },
154 { BtErrCode::BT_ERR_INVALID_PARAM, "BT_ERR_INVALID_PARAM" },
155 { BtErrCode::BT_ERR_API_NOT_SUPPORT, "BT_ERR_API_NOT_SUPPORT" },
156 { BtErrCode::BT_ERR_SERVICE_DISCONNECTED, "BT_ERR_SERVICE_DISCONNECTED" },
157 { BtErrCode::BT_ERR_UNBONDED_DEVICE, "BT_ERR_UNBONDED_DEVICE" },
158 { BtErrCode::BT_ERR_INVALID_STATE, "BT_ERR_INVALID_STATE" },
159 { BtErrCode::BT_ERR_PROFILE_DISABLED, "BT_ERR_PROFILE_DISABLED" },
160 { BtErrCode::BT_ERR_DEVICE_DISCONNECTED, "BT_ERR_DEVICE_DISCONNECTED" },
161 { BtErrCode::BT_ERR_MAX_CONNECTION, "BT_ERR_MAX_CONNECTION" },
162 { BtErrCode::BT_ERR_INTERNAL_ERROR, "BT_ERR_INTERNAL_ERROR" },
163 { BtErrCode::BT_ERR_IPC_TRANS_FAILED, "BT_ERR_IPC_TRANS_FAILED" },
164 { BtErrCode::BT_ERR_GATT_READ_NOT_PERMITTED, "BT_ERR_GATT_READ_NOT_PERMITTED" },
165 { BtErrCode::BT_ERR_GATT_WRITE_NOT_PERMITTED, "BT_ERR_GATT_WRITE_NOT_PERMITTED" },
166 { BtErrCode::BT_ERR_GATT_MAX_SERVER, "BT_ERR_GATT_MAX_SERVER" },
167 { BtErrCode::BT_ERR_SPP_SERVER_STATE, "BT_ERR_SPP_SERVER_STATE" },
168 { BtErrCode::BT_ERR_SPP_BUSY, "BT_ERR_SPP_BUSY" },
169 { BtErrCode::BT_ERR_SPP_DEVICE_NOT_FOUND, "BT_ERR_SPP_DEVICE_NOT_FOUND" },
170 { BtErrCode::BT_ERR_SPP_IO, "BT_ERR_SPP_IO" },
171 { BtErrCode::BT_ERR_NO_ACTIVE_HFP_DEVICE, "Active hfp device is not exist." },
172 { BtErrCode::BT_ERR_NULL_HFP_STATE_MACHINE, "Hfp state machine is not null." },
173 { BtErrCode::BT_ERR_HFP_NOT_CONNECT, "Hfp is not connected." },
174 { BtErrCode::BT_ERR_SCO_HAS_BEEN_CONNECTED, "Sco has been connected." },
175 { BtErrCode::BT_ERR_VR_HAS_BEEN_STARTED, "Voice recognition has been started." },
176 { BtErrCode::BT_ERR_AUDIO_NOT_IDLE, "Audio is not idle." },
177 { BtErrCode::BT_ERR_VIRTUAL_CALL_NOT_STARTED, "Virtual call is not started." },
178 { BtErrCode::BT_ERR_DISCONNECT_SCO_FAILED, "Disconnect sco failed." },
179 };
180
GetErrorCode(int32_t errCode)181 std::string GetErrorCode(int32_t errCode)
182 {
183 std::string errlog = "unknown error code: ";
184 auto iter = BtErrCodeMap.find(errCode);
185 if (iter != BtErrCodeMap.end()) {
186 errlog = iter->second;
187 }
188 errlog.append("(").append(std::to_string(errCode)).append(")");
189 return errlog;
190 }
191
ToUpper(char * arr)192 void ToUpper(char* arr)
193 {
194 for (size_t i = 0; i < strlen(arr); ++i) {
195 if (arr[i] >= 'a' && arr[i] <= 'z') {
196 arr[i] = toupper(arr[i]);
197 }
198 }
199 }
200
GenerateRandomMacAddress()201 std::string GenerateRandomMacAddress()
202 {
203 std::string randomMac = "";
204 char strMacTmp[RANDOM_ADDR_ARRAY_SIZE] = {0};
205 std::mt19937_64 gen(std::chrono::high_resolution_clock::now().time_since_epoch().count());
206 for (int i = 0; i < RANDOM_ADDR_MAC_BIT_SIZE; i++) {
207 int ret = -1;
208 if (i != RANDOM_ADDR_FIRST_BIT) {
209 std::uniform_int_distribution<> distribution(0, HEX_BASE - 1);
210 ret = sprintf_s(strMacTmp, RANDOM_ADDR_ARRAY_SIZE, "%x", distribution(gen));
211 } else {
212 std::uniform_int_distribution<> distribution(0, OCT_BASE - 1);
213 ret = sprintf_s(strMacTmp, RANDOM_ADDR_ARRAY_SIZE, "%x", RANDOM_ADDR_SPLIT_SIZE * distribution(gen));
214 }
215 if (ret == -1) {
216 HILOGE("GenerateRandomMacAddress failed, sprintf_s return -1!");
217 }
218 ToUpper(strMacTmp);
219 randomMac += strMacTmp;
220 if ((i % RANDOM_ADDR_SPLIT_SIZE != 0 && (i != RANDOM_ADDR_LAST_BIT))) {
221 randomMac.append(":");
222 }
223 }
224 return randomMac;
225 }
226
CheckConnectionStrategyInvalid(int32_t strategy)227 bool CheckConnectionStrategyInvalid(int32_t strategy)
228 {
229 if (strategy == static_cast<int32_t>(BTStrategyType::CONNECTION_ALLOWED) ||
230 strategy == static_cast<int32_t>(BTStrategyType::CONNECTION_FORBIDDEN)) {
231 return true;
232 }
233 return false;
234 }
235
CheckAccessAuthorizationInvalid(int32_t accessAuthorization)236 bool CheckAccessAuthorizationInvalid(int32_t accessAuthorization)
237 {
238 if (accessAuthorization == static_cast<int32_t>(BTPermissionType::ACCESS_UNKNOWN) ||
239 accessAuthorization == static_cast<int32_t>(BTPermissionType::ACCESS_ALLOWED) ||
240 accessAuthorization == static_cast<int32_t>(BTPermissionType::ACCESS_FORBIDDEN)) {
241 return true;
242 }
243 return false;
244 }
245
246 } // namespace Bluetooth
247 } // namespace OHOS
248