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_errorcode.h"
22 #include "bluetooth_def.h"
23 #include "bluetooth_host_proxy.h"
24 #include "bluetooth_log.h"
25 #include "iosfwd"
26 #include "iservice_registry.h"
27 #include "string"
28 #include "system_ability_definition.h"
29
30 using namespace std;
31
32 namespace OHOS {
33 namespace Bluetooth {
34 constexpr int startPos = 6;
35 constexpr int endPos = 13;
36 constexpr int RANDOM_ADDR_ARRAY_SIZE = 4;
37 constexpr int RANDOM_ADDR_MAC_BIT_SIZE = 12;
38 constexpr int RANDOM_ADDR_FIRST_BIT = 1;
39 constexpr int RANDOM_ADDR_LAST_BIT = 11;
40 constexpr int RANDOM_ADDR_SPLIT_SIZE = 2;
41 constexpr int HEX_BASE = 16;
42 constexpr int OCT_BASE = 8;
43
GetEncryptAddr(std::string addr)44 std::string GetEncryptAddr(std::string addr)
45 {
46 if (addr.empty() || addr.length() != ADDRESS_LENGTH) {
47 HILOGE("addr is invalid.");
48 return std::string("");
49 }
50 std::string tmp = "**:**:**:**:**:**";
51 std::string out = addr;
52 // 00:01:**:**:**:05
53 for (int i = startPos; i <= endPos; i++) {
54 out[i] = tmp[i];
55 }
56 return out;
57 }
58
GetBtStateName(int state)59 std::string GetBtStateName(int state)
60 {
61 switch (state) {
62 case BTStateID::STATE_TURNING_ON:
63 return "STATE_TURNING_ON(0)";
64 case BTStateID::STATE_TURN_ON:
65 return "STATE_TURN_ON(1)";
66 case BTStateID::STATE_TURNING_OFF:
67 return "STATE_TURNING_OFF(2)";
68 case BTStateID::STATE_TURN_OFF:
69 return "STATE_TURN_OFF(3)";
70 default:
71 return "Unknown";
72 }
73 }
74
GetBtTransportName(int transport)75 std::string GetBtTransportName(int transport)
76 {
77 switch (transport) {
78 case BTTransport::ADAPTER_BREDR:
79 return "ADAPTER_BREDR(0)";
80 case BTTransport::ADAPTER_BLE:
81 return "ADAPTER_BLE(1)";
82 default:
83 return "Unknown";
84 }
85 }
86
GetProfileConnStateName(int state)87 std::string GetProfileConnStateName(int state)
88 {
89 switch (state) {
90 case static_cast<int>(BTConnectState::CONNECTING):
91 return "CONNECTING(0)";
92 case static_cast<int>(BTConnectState::CONNECTED):
93 return "CONNECTED(1)";
94 case static_cast<int>(BTConnectState::DISCONNECTING):
95 return "DISCONNECTING(2)";
96 case static_cast<int>(BTConnectState::DISCONNECTED):
97 return "DISCONNECTED(3)";
98 default:
99 return "Unknown";
100 }
101 }
102
103 static std::map<int32_t, std::string> BtErrCodeMap {
104 { BtErrCode::BT_NO_ERROR, "BT_NO_ERROR" },
105 { BtErrCode::BT_ERR_PERMISSION_FAILED, "BT_ERR_PERMISSION_FAILED" },
106 { BtErrCode::BT_ERR_SYSTEM_PERMISSION_FAILED, "BT_ERR_SYSTEM_PERMISSION_FAILED" },
107 { BtErrCode::BT_ERR_INVALID_PARAM, "BT_ERR_INVALID_PARAM" },
108 { BtErrCode::BT_ERR_API_NOT_SUPPORT, "BT_ERR_API_NOT_SUPPORT" },
109 { BtErrCode::BT_ERR_SERVICE_DISCONNECTED, "BT_ERR_SERVICE_DISCONNECTED" },
110 { BtErrCode::BT_ERR_UNBONDED_DEVICE, "BT_ERR_UNBONDED_DEVICE" },
111 { BtErrCode::BT_ERR_INVALID_STATE, "BT_ERR_INVALID_STATE" },
112 { BtErrCode::BT_ERR_PROFILE_DISABLED, "BT_ERR_PROFILE_DISABLED" },
113 { BtErrCode::BT_ERR_DEVICE_DISCONNECTED, "BT_ERR_DEVICE_DISCONNECTED" },
114 { BtErrCode::BT_ERR_MAX_CONNECTION, "BT_ERR_MAX_CONNECTION" },
115 { BtErrCode::BT_ERR_INTERNAL_ERROR, "BT_ERR_INTERNAL_ERROR" },
116 { BtErrCode::BT_ERR_IPC_TRANS_FAILED, "BT_ERR_IPC_TRANS_FAILED" },
117 { BtErrCode::BT_ERR_GATT_READ_NOT_PERMITTED, "BT_ERR_GATT_READ_NOT_PERMITTED" },
118 { BtErrCode::BT_ERR_GATT_WRITE_NOT_PERMITTED, "BT_ERR_GATT_WRITE_NOT_PERMITTED" },
119 { BtErrCode::BT_ERR_GATT_MAX_SERVER, "BT_ERR_GATT_MAX_SERVER" },
120 { BtErrCode::BT_ERR_SPP_SERVER_STATE, "BT_ERR_SPP_SERVER_STATE" },
121 { BtErrCode::BT_ERR_SPP_BUSY, "BT_ERR_SPP_BUSY" },
122 { BtErrCode::BT_ERR_SPP_DEVICE_NOT_FOUND, "BT_ERR_SPP_DEVICE_NOT_FOUND" },
123 { BtErrCode::BT_ERR_SPP_IO, "BT_ERR_SPP_IO" },
124 };
125
GetErrorCode(int32_t errCode)126 std::string GetErrorCode(int32_t errCode)
127 {
128 std::string errlog = "unknown error code: ";
129 auto iter = BtErrCodeMap.find(errCode);
130 if (iter != BtErrCodeMap.end()) {
131 errlog = iter->second;
132 }
133 errlog.append("(").append(std::to_string(errCode)).append(")");
134 return errlog;
135 }
136
GetRemoteObject(const std::string & objectName)137 sptr<IRemoteObject> GetRemoteObject(const std::string &objectName)
138 {
139 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
140 if (!samgr) {
141 HILOGE("samgr is null");
142 return nullptr;
143 }
144 sptr<IRemoteObject> hostRemote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
145 if (!hostRemote) {
146 HILOGE("hostRemote is null");
147 return nullptr;
148 }
149 sptr<IBluetoothHost> hostProxy = iface_cast<IBluetoothHost>(hostRemote);
150 if (!hostProxy) {
151 HILOGE("hostProxy is null");
152 return nullptr;
153 }
154
155 sptr<IRemoteObject> remote = nullptr;
156 if (objectName == BLE_ADVERTISER_SERVER || objectName == BLE_CENTRAL_MANAGER_SERVER) {
157 remote = hostProxy->GetBleRemote(objectName);
158 } else {
159 remote = hostProxy->GetProfile(objectName);
160 }
161 if (!remote) {
162 HILOGE("%{public}s remote is null", objectName.c_str());
163 return nullptr;
164 }
165 return remote;
166 }
167
ToUpper(char * arr)168 void ToUpper(char* arr)
169 {
170 for (size_t i = 0; i < strlen(arr); ++i) {
171 if (arr[i] >= 'a' && arr[i] <= 'z') {
172 arr[i] = toupper(arr[i]);
173 }
174 }
175 }
176
GenerateRandomMacAddress()177 std::string GenerateRandomMacAddress()
178 {
179 std::string randomMac = "";
180 char strMacTmp[RANDOM_ADDR_ARRAY_SIZE] = {0};
181 std::mt19937_64 gen(std::chrono::high_resolution_clock::now().time_since_epoch().count());
182 for (int i = 0; i < RANDOM_ADDR_MAC_BIT_SIZE; i++) {
183 int ret = -1;
184 if (i != RANDOM_ADDR_FIRST_BIT) {
185 std::uniform_int_distribution<> distribution(0, HEX_BASE - 1);
186 ret = sprintf_s(strMacTmp, RANDOM_ADDR_ARRAY_SIZE, "%x", distribution(gen));
187 } else {
188 std::uniform_int_distribution<> distribution(0, OCT_BASE - 1);
189 ret = sprintf_s(strMacTmp, RANDOM_ADDR_ARRAY_SIZE, "%x", RANDOM_ADDR_SPLIT_SIZE * distribution(gen));
190 }
191 if (ret == -1) {
192 HILOGE("GenerateRandomMacAddress failed, sprintf_s return -1!");
193 }
194 ToUpper(strMacTmp);
195 randomMac += strMacTmp;
196 if ((i % RANDOM_ADDR_SPLIT_SIZE != 0 && (i != RANDOM_ADDR_LAST_BIT))) {
197 randomMac.append(":");
198 }
199 }
200 return randomMac;
201 }
202
203 } // namespace Bluetooth
204 } // namespace OHOS