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