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 "wifi_statistic.h"
17
18 #include "inner_api/wifi_device.h"
19 #include "inner_api/wifi_hotspot.h"
20 #include "inner_api/wifi_p2p.h"
21 #include "comm_log.h"
22 #include "softbus_error_code.h"
23 #include "softbus_json_utils.h"
24 #include "anonymizer.h"
25
26 using namespace OHOS::Wifi;
27
28 namespace Communication {
29 namespace Softbus {
30
GetInstance()31 WifiStatistic& WifiStatistic::GetInstance()
32 {
33 static WifiStatistic instance;
34 return instance;
35 }
36
GetWifiStatisticInfo(cJSON * json)37 int32_t WifiStatistic::GetWifiStatisticInfo(cJSON *json)
38 {
39 if (json == nullptr) {
40 COMM_LOGE(COMM_DFX, "param json is null");
41 return SOFTBUS_INVALID_PARAM;
42 }
43 if (GetStaInfo(json) != SOFTBUS_OK || GetSoftApInfo(json) != SOFTBUS_OK || GetP2PInfo(json) != SOFTBUS_OK) {
44 return SOFTBUS_INVALID_PARAM;
45 }
46 return SOFTBUS_OK;
47 }
48
AnonymizeStr(const std::string & data)49 static std::string AnonymizeStr(const std::string &data)
50 {
51 if (data.empty()) {
52 return "";
53 }
54 char *temp = nullptr;
55 Anonymize(data.c_str(), &temp);
56 std::string result = AnonymizeWrapper(temp);
57 AnonymizeFree(temp);
58 return result;
59 }
60
GetStaInfo(cJSON * json)61 int32_t WifiStatistic::GetStaInfo(cJSON *json)
62 {
63 if (json == nullptr) {
64 COMM_LOGE(COMM_DFX, "param json is null");
65 return SOFTBUS_INVALID_PARAM;
66 }
67 std::shared_ptr<WifiDevice> wifiStaPtr = WifiDevice::GetInstance(WIFI_DEVICE_ABILITY_ID);
68 if (wifiStaPtr == nullptr) {
69 COMM_LOGW(COMM_DFX, "Get wifi device fail");
70 return SOFTBUS_OK;
71 }
72 cJSON *staJson = cJSON_CreateObject();
73 WifiLinkedInfo wifiLinkedInfo;
74 if (wifiStaPtr->GetLinkedInfo(wifiLinkedInfo) != 0) {
75 (void)AddNumberToJsonObject(staJson, "IsStaExist", 0);
76 (void)cJSON_AddItemToObject(json, "StaInfo", staJson);
77 return SOFTBUS_OK;
78 }
79 (void)AddNumberToJsonObject(staJson, "IsStaExist", 1);
80 (void)AddStringToJsonObject(staJson, "Name", wifiLinkedInfo.ssid.c_str());
81 (void)AddStringToJsonObject(staJson, "Mac", AnonymizeStr(wifiLinkedInfo.bssid).c_str());
82 (void)AddNumberToJsonObject(staJson, "Freq", wifiLinkedInfo.frequency);
83 (void)AddNumberToJsonObject(staJson, "chload", wifiLinkedInfo.chload);
84
85 (void)cJSON_AddItemToObject(json, "StaInfo", staJson);
86 return SOFTBUS_OK;
87 }
88
GetSoftApInfo(cJSON * json)89 int32_t WifiStatistic::GetSoftApInfo(cJSON *json)
90 {
91 if (json == nullptr) {
92 COMM_LOGE(COMM_DFX, "param json is null");
93 return SOFTBUS_INVALID_PARAM;
94 }
95 std::shared_ptr<WifiHotspot> wifiSoftApPtr = WifiHotspot::GetInstance(WIFI_HOTSPOT_ABILITY_ID);
96 if (wifiSoftApPtr == nullptr) {
97 COMM_LOGW(COMM_DFX, "Get wifi soft ap fail");
98 return SOFTBUS_OK;
99 }
100 cJSON *softApJson = cJSON_CreateObject();
101 bool isHotspotActive = false;
102 wifiSoftApPtr->IsHotspotActive(isHotspotActive);
103 if (!isHotspotActive) {
104 (void)AddNumberToJsonObject(softApJson, "IsSoftApExist", 0);
105 (void)cJSON_AddItemToObject(json, "SoftApInfo", softApJson);
106 return SOFTBUS_OK;
107 }
108 (void)AddNumberToJsonObject(softApJson, "IsSoftApExist", 1);
109 HotspotConfig config;
110 wifiSoftApPtr->GetHotspotConfig(config);
111 (void)AddNumberToJsonObject(softApJson, "channel", config.GetChannel());
112 std::vector<StationInfo> stationList;
113 if (wifiSoftApPtr->GetStationList(stationList) != 0 || stationList.size() == 0) {
114 (void)AddNumberToJsonObject(softApJson, "StaNum", 0);
115 (void)cJSON_AddItemToObject(json, "SoftApInfo", softApJson);
116 return SOFTBUS_OK;
117 }
118 (void)AddNumberToJsonObject(softApJson, "StaNum", stationList.size());
119 cJSON *stationArray = cJSON_CreateArray();
120 for (size_t i = 0; i < stationList.size(); i++) {
121 cJSON *stationJson = cJSON_CreateObject();
122 (void)AddStringToJsonObject(stationJson, "Name", stationList[i].deviceName.c_str());
123 (void)AddStringToJsonObject(stationJson, "Mac", AnonymizeStr(stationList[i].bssid).c_str());
124 (void)cJSON_AddItemToArray(stationArray, stationJson);
125 }
126 (void)cJSON_AddItemToObject(softApJson, "StaDevList", stationArray);
127 (void)cJSON_AddItemToObject(json, "SoftApInfo", softApJson);
128 return SOFTBUS_OK;
129 }
130
GetP2PInfo(cJSON * json)131 int32_t WifiStatistic::GetP2PInfo(cJSON *json)
132 {
133 if (json == nullptr) {
134 COMM_LOGE(COMM_DFX, "param json is null");
135 return SOFTBUS_INVALID_PARAM;
136 }
137 std::shared_ptr<WifiP2p> wifiP2PPtr = WifiP2p::GetInstance(WIFI_P2P_ABILITY_ID);
138 if (wifiP2PPtr == nullptr) {
139 COMM_LOGW(COMM_DFX, "Get wifi p2p fail");
140 return SOFTBUS_OK;
141 }
142 cJSON *p2pJson = cJSON_CreateObject();
143 WifiP2pGroupInfo groupInfo;
144 if (wifiP2PPtr->GetCurrentGroup(groupInfo) != 0) {
145 (void)AddNumberToJsonObject(p2pJson, "IsP2PExist", 0);
146 (void)cJSON_AddItemToObject(json, "P2PInfo", p2pJson);
147 return SOFTBUS_OK;
148 }
149 (void)AddNumberToJsonObject(p2pJson, "IsP2PExist", 1);
150 (void)AddBoolToJsonObject(p2pJson, "IsP2POwner", groupInfo.IsGroupOwner());
151 (void)AddNumberToJsonObject(p2pJson, "Freq", groupInfo.GetFrequency());
152 if (!groupInfo.IsGroupOwner()) {
153 cJSON *goJson = cJSON_CreateObject();
154 (void)AddStringToJsonObject(goJson, "Name", groupInfo.GetOwner().GetDeviceName().c_str());
155 (void)AddStringToJsonObject(goJson, "Mac",
156 AnonymizeStr(groupInfo.GetOwner().GetRandomDeviceAddress()).c_str());
157 (void)cJSON_AddItemToObject(p2pJson, "GOInfo", goJson);
158 (void)cJSON_AddItemToObject(json, "P2PInfo", p2pJson);
159 return SOFTBUS_OK;
160 }
161 std::vector<WifiP2pDevice> gcList = groupInfo.GetClientDevices();
162 (void)AddNumberToJsonObject(p2pJson, "GCNum", gcList.size());
163 cJSON *gcArray = cJSON_CreateArray();
164 for (size_t i = 0; i < gcList.size(); i++) {
165 cJSON *gcJson = cJSON_CreateObject();
166 (void)AddStringToJsonObject(gcJson, "Name", gcList[i].GetDeviceName().c_str());
167 (void)AddStringToJsonObject(gcJson, "Mac",
168 AnonymizeStr(gcList[i].GetRandomDeviceAddress()).c_str());
169 (void)cJSON_AddItemToArray(gcArray, gcJson);
170 }
171 (void)cJSON_AddItemToObject(p2pJson, "GCInfo", gcArray);
172 (void)cJSON_AddItemToObject(json, "P2PInfo", p2pJson);
173 return SOFTBUS_OK;
174 }
175
176 } // namespace SoftBus
177 } // namespace Communication