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 case static_cast<int>(UpdateOutputStackAction::ACTION_UPDATE_DEVICE_TYPE):
146 return "UPDATE_DEVICE_TYPE(8)";
147 default:
148 return "Unknown";
149 }
150 }
151
152 static std::map<int32_t, std::string> BtErrCodeMap {
153 { BtErrCode::BT_NO_ERROR, "BT_NO_ERROR" },
154 { BtErrCode::BT_ERR_PERMISSION_FAILED, "BT_ERR_PERMISSION_FAILED" },
155 { BtErrCode::BT_ERR_SYSTEM_PERMISSION_FAILED, "BT_ERR_SYSTEM_PERMISSION_FAILED" },
156 { BtErrCode::BT_ERR_INVALID_PARAM, "BT_ERR_INVALID_PARAM" },
157 { BtErrCode::BT_ERR_API_NOT_SUPPORT, "BT_ERR_API_NOT_SUPPORT" },
158 { BtErrCode::BT_ERR_SERVICE_DISCONNECTED, "BT_ERR_SERVICE_DISCONNECTED" },
159 { BtErrCode::BT_ERR_UNBONDED_DEVICE, "BT_ERR_UNBONDED_DEVICE" },
160 { BtErrCode::BT_ERR_INVALID_STATE, "BT_ERR_INVALID_STATE" },
161 { BtErrCode::BT_ERR_PROFILE_DISABLED, "BT_ERR_PROFILE_DISABLED" },
162 { BtErrCode::BT_ERR_DEVICE_DISCONNECTED, "BT_ERR_DEVICE_DISCONNECTED" },
163 { BtErrCode::BT_ERR_MAX_CONNECTION, "BT_ERR_MAX_CONNECTION" },
164 { BtErrCode::BT_ERR_INTERNAL_ERROR, "BT_ERR_INTERNAL_ERROR" },
165 { BtErrCode::BT_ERR_IPC_TRANS_FAILED, "BT_ERR_IPC_TRANS_FAILED" },
166 { BtErrCode::BT_ERR_GATT_READ_NOT_PERMITTED, "BT_ERR_GATT_READ_NOT_PERMITTED" },
167 { BtErrCode::BT_ERR_GATT_WRITE_NOT_PERMITTED, "BT_ERR_GATT_WRITE_NOT_PERMITTED" },
168 { BtErrCode::BT_ERR_GATT_MAX_SERVER, "BT_ERR_GATT_MAX_SERVER" },
169 { BtErrCode::BT_ERR_SPP_SERVER_STATE, "BT_ERR_SPP_SERVER_STATE" },
170 { BtErrCode::BT_ERR_SPP_BUSY, "BT_ERR_SPP_BUSY" },
171 { BtErrCode::BT_ERR_SPP_DEVICE_NOT_FOUND, "BT_ERR_SPP_DEVICE_NOT_FOUND" },
172 { BtErrCode::BT_ERR_SPP_IO, "BT_ERR_SPP_IO" },
173 { BtErrCode::BT_ERR_NO_ACTIVE_HFP_DEVICE, "Active hfp device is not exist." },
174 { BtErrCode::BT_ERR_NULL_HFP_STATE_MACHINE, "Hfp state machine is not null." },
175 { BtErrCode::BT_ERR_HFP_NOT_CONNECT, "Hfp is not connected." },
176 { BtErrCode::BT_ERR_SCO_HAS_BEEN_CONNECTED, "Sco has been connected." },
177 { BtErrCode::BT_ERR_VR_HAS_BEEN_STARTED, "Voice recognition has been started." },
178 { BtErrCode::BT_ERR_AUDIO_NOT_IDLE, "Audio is not idle." },
179 { BtErrCode::BT_ERR_VIRTUAL_CALL_NOT_STARTED, "Virtual call is not started." },
180 { BtErrCode::BT_ERR_DISCONNECT_SCO_FAILED, "Disconnect sco failed." },
181 };
182
GetErrorCode(int32_t errCode)183 std::string GetErrorCode(int32_t errCode)
184 {
185 std::string errlog = "unknown error code: ";
186 auto iter = BtErrCodeMap.find(errCode);
187 if (iter != BtErrCodeMap.end()) {
188 errlog = iter->second;
189 }
190 errlog.append("(").append(std::to_string(errCode)).append(")");
191 return errlog;
192 }
193
ToUpper(char * arr)194 void ToUpper(char* arr)
195 {
196 for (size_t i = 0; i < strlen(arr); ++i) {
197 if (arr[i] >= 'a' && arr[i] <= 'z') {
198 arr[i] = toupper(arr[i]);
199 }
200 }
201 }
202
GenerateRandomMacAddress()203 std::string GenerateRandomMacAddress()
204 {
205 std::string randomMac = "";
206 char strMacTmp[RANDOM_ADDR_ARRAY_SIZE] = {0};
207 std::mt19937_64 gen(std::chrono::high_resolution_clock::now().time_since_epoch().count());
208 for (int i = 0; i < RANDOM_ADDR_MAC_BIT_SIZE; i++) {
209 int ret = -1;
210 if (i != RANDOM_ADDR_FIRST_BIT) {
211 std::uniform_int_distribution<> distribution(0, HEX_BASE - 1);
212 ret = sprintf_s(strMacTmp, RANDOM_ADDR_ARRAY_SIZE, "%x", distribution(gen));
213 } else {
214 std::uniform_int_distribution<> distribution(0, OCT_BASE - 1);
215 ret = sprintf_s(strMacTmp, RANDOM_ADDR_ARRAY_SIZE, "%x", RANDOM_ADDR_SPLIT_SIZE * distribution(gen));
216 }
217 if (ret == -1) {
218 HILOGE("GenerateRandomMacAddress failed, sprintf_s return -1!");
219 }
220 ToUpper(strMacTmp);
221 randomMac += strMacTmp;
222 if ((i % RANDOM_ADDR_SPLIT_SIZE != 0 && (i != RANDOM_ADDR_LAST_BIT))) {
223 randomMac.append(":");
224 }
225 }
226 return randomMac;
227 }
228
CheckConnectionStrategyInvalid(int32_t strategy)229 bool CheckConnectionStrategyInvalid(int32_t strategy)
230 {
231 if (strategy == static_cast<int32_t>(BTStrategyType::CONNECTION_ALLOWED) ||
232 strategy == static_cast<int32_t>(BTStrategyType::CONNECTION_FORBIDDEN)) {
233 return true;
234 }
235 return false;
236 }
237
CheckAccessAuthorizationInvalid(int32_t accessAuthorization)238 bool CheckAccessAuthorizationInvalid(int32_t accessAuthorization)
239 {
240 if (accessAuthorization == static_cast<int32_t>(BTPermissionType::ACCESS_UNKNOWN) ||
241 accessAuthorization == static_cast<int32_t>(BTPermissionType::ACCESS_ALLOWED) ||
242 accessAuthorization == static_cast<int32_t>(BTPermissionType::ACCESS_FORBIDDEN)) {
243 return true;
244 }
245 return false;
246 }
247
248 } // namespace Bluetooth
249 } // namespace OHOS