1 /*
2 * Copyright (c) 2024 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
16 #include "bt_statistic.h"
17
18 #include "bluetooth_a2dp_snk.h"
19 #include "bluetooth_a2dp_src.h"
20 #include "bluetooth_avrcp_ct.h"
21 #include "bluetooth_avrcp_tg.h"
22 #include "bluetooth_hfp_ag.h"
23 #include "bluetooth_hfp_hf.h"
24 #include "bluetooth_hid_host.h"
25 #include "bluetooth_host.h"
26 #include "bluetooth_map_mse.h"
27 #include "bluetooth_opp.h"
28 #include "bluetooth_pan.h"
29 #include "bluetooth_pbap_pse.h"
30 #include "bluetooth_gatt_manager.h"
31 #include "comm_log.h"
32 #include "softbus_error_code.h"
33 #include "softbus_json_utils.h"
34 #include "anonymizer.h"
35
36 using namespace OHOS::Bluetooth;
37
38 namespace Communication {
39 namespace Softbus {
40
BtStatistic()41 BtStatistic::BtStatistic()
42 {
43 connectState_ = { static_cast<int32_t>(BTConnectState::CONNECTED) };
44 getProfileDeviceInfoMap_ = {
45 {PROFILE_ID_GATT_CLIENT, &BtStatistic::GetGattClientDeviceInfo},
46 {PROFILE_ID_GATT_SERVER, &BtStatistic::GetGattServerDeviceInfo},
47 {PROFILE_ID_A2DP_SRC, &BtStatistic::GetA2dpSrcDeviceInfo},
48 {PROFILE_ID_A2DP_SINK, &BtStatistic::GetA2dpSinkDeviceInfo},
49 {PROFILE_ID_AVRCP_CT, &BtStatistic::GetAvrCTDeviceInfo},
50 {PROFILE_ID_AVRCP_TG, &BtStatistic::GetAvrTGDeviceInfo},
51 {PROFILE_ID_HFP_AG, &BtStatistic::GetHfpAGDeviceInfo},
52 {PROFILE_ID_HFP_HF, &BtStatistic::GetHfpHFDeviceInfo},
53 {PROFILE_ID_MAP_MCE, nullptr},
54 {PROFILE_ID_MAP_MSE, &BtStatistic::GetMapMseDeviceInfo},
55 {PROFILE_ID_PBAP_PCE, nullptr},
56 {PROFILE_ID_PBAP_PSE, &BtStatistic::GetPbapPseDeviceInfo},
57 {PROFILE_ID_SPP, nullptr},
58 {PROFILE_ID_DI, nullptr},
59 {PROFILE_ID_BLE_ADVERTISER, &BtStatistic::GetBleAdvertiserDeviceInfo},
60 {PROFILE_ID_BLE_CENTRAL_MANAGER_SERVER, &BtStatistic::GetBleCentralDeviceInfo},
61 {PROFILE_ID_BLE_GATT_MANAGER, &BtStatistic::GetBleGattDeviceInfo},
62 {PROFILE_ID_HID_HOST, &BtStatistic::GetHidHostDeviceInfo},
63 {PROFILE_ID_OPP, &BtStatistic::GetOppDeviceInfo},
64 {PROFILE_ID_PAN, &BtStatistic::GetPanDeviceInfo},
65 {PROFILE_ID_HOST, nullptr},
66 };
67 }
68
GetInstance()69 BtStatistic& BtStatistic::GetInstance()
70 {
71 static BtStatistic instance;
72 return instance;
73 }
74
GetBtStatisticInfo(cJSON * json)75 int32_t BtStatistic::GetBtStatisticInfo(cJSON *json)
76 {
77 if (json == nullptr) {
78 COMM_LOGE(COMM_DFX, "param json is null");
79 return SOFTBUS_INVALID_PARAM;
80 }
81 cJSON *btDeviceArray = cJSON_CreateArray();
82 std::vector<uint32_t> profileList = BluetoothHost::GetDefaultHost().GetProfileList();
83 for (size_t i = 0; i < profileList.size(); i++) {
84 if (getProfileDeviceInfoMap_.find(profileList[i]) == getProfileDeviceInfoMap_.end()) {
85 continue;
86 }
87 GetProfileDeviceInfo func = getProfileDeviceInfoMap_[profileList[i]];
88 if (func != nullptr) {
89 (this->*func)(btDeviceArray);
90 }
91 }
92 (void)cJSON_AddItemToObject(json, "BtDeviceList", btDeviceArray);
93 return SOFTBUS_OK;
94 }
95
AnonymizeStr(const std::string & data)96 static std::string AnonymizeStr(const std::string &data)
97 {
98 if (data.empty()) {
99 return "";
100 }
101 char *temp = nullptr;
102 Anonymize(data.c_str(), &temp);
103 std::string result = AnonymizeWrapper(temp);
104 AnonymizeFree(temp);
105 return result;
106 }
107
AddDevicesToArray(cJSON * json,const std::vector<OHOS::Bluetooth::BluetoothRemoteDevice> & devices,uint32_t profileId)108 static void AddDevicesToArray(
109 cJSON *json, const std::vector<OHOS::Bluetooth::BluetoothRemoteDevice>& devices, uint32_t profileId)
110 {
111 if (json == nullptr) {
112 COMM_LOGE(COMM_DFX, "param json is null");
113 return;
114 }
115 for (size_t i = 0; i < devices.size(); i++) {
116 cJSON *deviceJson = cJSON_CreateObject();
117 (void)AddStringToJsonObject(deviceJson, "Name", AnonymizeStr(devices[i].GetDeviceName()).c_str());
118 (void)AddStringToJsonObject(deviceJson, "Mac", AnonymizeStr(devices[i].GetDeviceAddr()).c_str());
119 (void)AddNumberToJsonObject(deviceJson, "Profile", static_cast<int32_t>(profileId));
120 if (profileId == PROFILE_ID_A2DP_SRC) {
121 (void)AddStringToJsonObject(deviceJson, "IsPlaying",
122 std::to_string(A2dpSource::GetProfile()->GetPlayingState(devices[i])).c_str());
123 } else if (profileId == PROFILE_ID_A2DP_SINK) {
124 (void)AddStringToJsonObject(deviceJson, "IsPlaying",
125 std::to_string(A2dpSink::GetProfile()->GetPlayingState(devices[i])).c_str());
126 } else if (profileId == PROFILE_ID_HFP_HF) {
127 (void)AddStringToJsonObject(deviceJson, "ScoState",
128 std::to_string(HandsFreeUnit::GetProfile()->GetScoState(devices[i])).c_str());
129 } else if (profileId == PROFILE_ID_HFP_AG) {
130 (void)AddStringToJsonObject(deviceJson, "ScoState",
131 std::to_string(HandsFreeAudioGateway::GetProfile()->GetScoState(devices[i])).c_str());
132 }
133 (void)cJSON_AddItemToArray(json, deviceJson);
134 }
135 }
136
GetGattDeviceInfo(cJSON * json,uint32_t gattId)137 void BtStatistic::GetGattDeviceInfo(cJSON *json, uint32_t gattId)
138 {
139 if (json == nullptr) {
140 COMM_LOGE(COMM_DFX, "param json is null");
141 return;
142 }
143 std::vector<BluetoothRemoteDevice> devices;
144 GattManager gattManager;
145 devices = gattManager.GetConnectedDevices();
146 AddDevicesToArray(json, devices, gattId);
147 }
148
GetGattClientDeviceInfo(cJSON * json)149 void BtStatistic::GetGattClientDeviceInfo(cJSON *json)
150 {
151 GetGattDeviceInfo(json, PROFILE_ID_GATT_CLIENT);
152 }
153
GetGattServerDeviceInfo(cJSON * json)154 void BtStatistic::GetGattServerDeviceInfo(cJSON *json)
155 {
156 GetGattDeviceInfo(json, PROFILE_ID_GATT_SERVER);
157 }
158
GetBleAdvertiserDeviceInfo(cJSON * json)159 void BtStatistic::GetBleAdvertiserDeviceInfo(cJSON *json)
160 {
161 GetGattDeviceInfo(json, PROFILE_ID_BLE_ADVERTISER);
162 }
163
GetBleCentralDeviceInfo(cJSON * json)164 void BtStatistic::GetBleCentralDeviceInfo(cJSON *json)
165 {
166 GetGattDeviceInfo(json, PROFILE_ID_BLE_CENTRAL_MANAGER_SERVER);
167 }
168
GetBleGattDeviceInfo(cJSON * json)169 void BtStatistic::GetBleGattDeviceInfo(cJSON *json)
170 {
171 GetGattDeviceInfo(json, PROFILE_ID_BLE_GATT_MANAGER);
172 }
173
GetA2dpSrcDeviceInfo(cJSON * json)174 void BtStatistic::GetA2dpSrcDeviceInfo(cJSON *json)
175 {
176 std::vector<BluetoothRemoteDevice> devices;
177 A2dpSource::GetProfile()->GetDevicesByStates(connectState_, devices);
178 AddDevicesToArray(json, devices, PROFILE_ID_A2DP_SRC);
179 }
180
GetA2dpSinkDeviceInfo(cJSON * json)181 void BtStatistic::GetA2dpSinkDeviceInfo(cJSON *json)
182 {
183 std::vector<BluetoothRemoteDevice> devices;
184 devices = A2dpSink::GetProfile()->GetDevicesByStates(connectState_);
185 AddDevicesToArray(json, devices, PROFILE_ID_A2DP_SINK);
186 }
187
GetAvrCTDeviceInfo(cJSON * json)188 void BtStatistic::GetAvrCTDeviceInfo(cJSON *json)
189 {
190 std::vector<BluetoothRemoteDevice> devices;
191 devices = AvrcpController::GetProfile()->GetDevicesByStates(connectState_);
192 AddDevicesToArray(json, devices, PROFILE_ID_AVRCP_CT);
193 }
194
GetAvrTGDeviceInfo(cJSON * json)195 void BtStatistic::GetAvrTGDeviceInfo(cJSON *json)
196 {
197 std::vector<BluetoothRemoteDevice> devices;
198 devices = AvrcpTarget::GetProfile()->GetDevicesByStates(connectState_);
199 AddDevicesToArray(json, devices, PROFILE_ID_AVRCP_TG);
200 }
201
GetHfpAGDeviceInfo(cJSON * json)202 void BtStatistic::GetHfpAGDeviceInfo(cJSON *json)
203 {
204 std::vector<BluetoothRemoteDevice> devices;
205 devices = HandsFreeAudioGateway::GetProfile()->GetDevicesByStates(connectState_);
206 AddDevicesToArray(json, devices, PROFILE_ID_HFP_AG);
207 }
208
GetHfpHFDeviceInfo(cJSON * json)209 void BtStatistic::GetHfpHFDeviceInfo(cJSON *json)
210 {
211 std::vector<BluetoothRemoteDevice> devices;
212 devices = HandsFreeUnit::GetProfile()->GetDevicesByStates(connectState_);
213 AddDevicesToArray(json, devices, PROFILE_ID_HFP_HF);
214 }
215
GetMapMseDeviceInfo(cJSON * json)216 void BtStatistic::GetMapMseDeviceInfo(cJSON *json)
217 {
218 std::vector<BluetoothRemoteDevice> devices;
219 MapMse::GetProfile()->GetDevicesByStates(connectState_, devices);
220 AddDevicesToArray(json, devices, PROFILE_ID_MAP_MSE);
221 }
222
GetPbapPseDeviceInfo(cJSON * json)223 void BtStatistic::GetPbapPseDeviceInfo(cJSON *json)
224 {
225 std::vector<BluetoothRemoteDevice> devices;
226 PbapPse::GetProfile()->GetDevicesByStates(connectState_, devices);
227 AddDevicesToArray(json, devices, PROFILE_ID_PBAP_PSE);
228 }
229
GetHidHostDeviceInfo(cJSON * json)230 void BtStatistic::GetHidHostDeviceInfo(cJSON *json)
231 {
232 std::vector<BluetoothRemoteDevice> devices;
233 HidHost::GetProfile()->GetDevicesByStates(connectState_, devices);
234 AddDevicesToArray(json, devices, PROFILE_ID_HID_HOST);
235 }
236
GetOppDeviceInfo(cJSON * json)237 void BtStatistic::GetOppDeviceInfo(cJSON *json)
238 {
239 std::vector<BluetoothRemoteDevice> devices;
240 Opp::GetProfile()->GetDevicesByStates(connectState_, devices);
241 AddDevicesToArray(json, devices, PROFILE_ID_OPP);
242 }
243
GetPanDeviceInfo(cJSON * json)244 void BtStatistic::GetPanDeviceInfo(cJSON *json)
245 {
246 std::vector<BluetoothRemoteDevice> devices;
247 Pan::GetProfile()->GetDevicesByStates(connectState_, devices);
248 AddDevicesToArray(json, devices, PROFILE_ID_PAN);
249 }
250
251 } // namespace SoftBus
252 } // namespace Communication