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