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 "utils/wifi_direct_anonymous.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
GetStaInfo(cJSON * json)49 int32_t WifiStatistic::GetStaInfo(cJSON *json)
50 {
51 if (json == nullptr) {
52 COMM_LOGE(COMM_DFX, "param json is null");
53 return SOFTBUS_INVALID_PARAM;
54 }
55 std::shared_ptr<WifiDevice> wifiStaPtr = WifiDevice::GetInstance(WIFI_DEVICE_ABILITY_ID);
56 if (wifiStaPtr == nullptr) {
57 COMM_LOGW(COMM_DFX, "Get wifi device fail");
58 return SOFTBUS_OK;
59 }
60 cJSON *staJson = cJSON_CreateObject();
61 WifiLinkedInfo wifiLinkedInfo;
62 if (wifiStaPtr->GetLinkedInfo(wifiLinkedInfo) != 0) {
63 (void)AddNumberToJsonObject(staJson, "IsStaExist", 0);
64 (void)cJSON_AddItemToObject(json, "StaInfo", staJson);
65 return SOFTBUS_OK;
66 }
67 (void)AddNumberToJsonObject(staJson, "IsStaExist", 1);
68 (void)AddStringToJsonObject(staJson, "Name", wifiLinkedInfo.ssid.c_str());
69 (void)AddStringToJsonObject(staJson, "Mac",
70 OHOS::SoftBus::WifiDirectAnonymizeMac(wifiLinkedInfo.bssid).c_str());
71 (void)AddNumberToJsonObject(staJson, "Freq", wifiLinkedInfo.frequency);
72 (void)AddNumberToJsonObject(staJson, "chload", wifiLinkedInfo.chload);
73
74 (void)cJSON_AddItemToObject(json, "StaInfo", staJson);
75 return SOFTBUS_OK;
76 }
77
GetSoftApInfo(cJSON * json)78 int32_t WifiStatistic::GetSoftApInfo(cJSON *json)
79 {
80 if (json == nullptr) {
81 COMM_LOGE(COMM_DFX, "param json is null");
82 return SOFTBUS_INVALID_PARAM;
83 }
84 std::shared_ptr<WifiHotspot> wifiSoftApPtr = WifiHotspot::GetInstance(WIFI_HOTSPOT_ABILITY_ID);
85 if (wifiSoftApPtr == nullptr) {
86 COMM_LOGW(COMM_DFX, "Get wifi soft ap fail");
87 return SOFTBUS_OK;
88 }
89 cJSON *softApJson = cJSON_CreateObject();
90 bool isHotspotActive = false;
91 wifiSoftApPtr->IsHotspotActive(isHotspotActive);
92 if (!isHotspotActive) {
93 (void)AddNumberToJsonObject(softApJson, "IsSoftApExist", 0);
94 (void)cJSON_AddItemToObject(json, "SoftApInfo", softApJson);
95 return SOFTBUS_OK;
96 }
97 (void)AddNumberToJsonObject(softApJson, "IsSoftApExist", 1);
98 HotspotConfig config;
99 wifiSoftApPtr->GetHotspotConfig(config);
100 (void)AddNumberToJsonObject(softApJson, "channel", config.GetChannel());
101 std::vector<StationInfo> stationList;
102 if (wifiSoftApPtr->GetStationList(stationList) != 0 || stationList.size() == 0) {
103 (void)AddNumberToJsonObject(softApJson, "StaNum", 0);
104 (void)cJSON_AddItemToObject(json, "SoftApInfo", softApJson);
105 return SOFTBUS_OK;
106 }
107 (void)AddNumberToJsonObject(softApJson, "StaNum", stationList.size());
108 cJSON *stationArray = cJSON_CreateArray();
109 for (size_t i = 0; i < stationList.size(); i++) {
110 cJSON *stationJson = cJSON_CreateObject();
111 (void)AddStringToJsonObject(stationJson, "Name", stationList[i].deviceName.c_str());
112 (void)AddStringToJsonObject(stationJson, "Mac",
113 OHOS::SoftBus::WifiDirectAnonymizeMac(stationList[i].bssid).c_str());
114 (void)cJSON_AddItemToArray(stationArray, stationJson);
115 }
116 (void)cJSON_AddItemToObject(softApJson, "StaDevList", stationArray);
117 (void)cJSON_AddItemToObject(json, "SoftApInfo", softApJson);
118 return SOFTBUS_OK;
119 }
120
GetP2PInfo(cJSON * json)121 int32_t WifiStatistic::GetP2PInfo(cJSON *json)
122 {
123 if (json == nullptr) {
124 COMM_LOGE(COMM_DFX, "param json is null");
125 return SOFTBUS_INVALID_PARAM;
126 }
127 std::shared_ptr<WifiP2p> wifiP2PPtr = WifiP2p::GetInstance(WIFI_P2P_ABILITY_ID);
128 if (wifiP2PPtr == nullptr) {
129 COMM_LOGW(COMM_DFX, "Get wifi p2p fail");
130 return SOFTBUS_OK;
131 }
132 cJSON *p2pJson = cJSON_CreateObject();
133 WifiP2pGroupInfo groupInfo;
134 if (wifiP2PPtr->GetCurrentGroup(groupInfo) != 0) {
135 (void)AddNumberToJsonObject(p2pJson, "IsP2PExist", 0);
136 (void)cJSON_AddItemToObject(json, "P2PInfo", p2pJson);
137 return SOFTBUS_OK;
138 }
139 (void)AddNumberToJsonObject(p2pJson, "IsP2PExist", 1);
140 (void)AddBoolToJsonObject(p2pJson, "IsP2POwner", groupInfo.IsGroupOwner());
141 (void)AddNumberToJsonObject(p2pJson, "Freq", groupInfo.GetFrequency());
142 if (!groupInfo.IsGroupOwner()) {
143 cJSON *goJson = cJSON_CreateObject();
144 (void)AddStringToJsonObject(goJson, "Name", groupInfo.GetOwner().GetDeviceName().c_str());
145 (void)AddStringToJsonObject(goJson, "Mac",
146 OHOS::SoftBus::WifiDirectAnonymizeMac(groupInfo.GetOwner().GetRandomDeviceAddress()).c_str());
147 (void)cJSON_AddItemToObject(p2pJson, "GOInfo", goJson);
148 (void)cJSON_AddItemToObject(json, "P2PInfo", p2pJson);
149 return SOFTBUS_OK;
150 }
151 std::vector<WifiP2pDevice> gcList = groupInfo.GetClientDevices();
152 (void)AddNumberToJsonObject(p2pJson, "GCNum", gcList.size());
153 cJSON *gcArray = cJSON_CreateArray();
154 for (size_t i = 0; i < gcList.size(); i++) {
155 cJSON *gcJson = cJSON_CreateObject();
156 (void)AddStringToJsonObject(gcJson, "Name", gcList[i].GetDeviceName().c_str());
157 (void)AddStringToJsonObject(gcJson, "Mac",
158 OHOS::SoftBus::WifiDirectAnonymizeMac(gcList[i].GetRandomDeviceAddress()).c_str());
159 (void)cJSON_AddItemToArray(gcArray, gcJson);
160 }
161 (void)cJSON_AddItemToObject(p2pJson, "GCInfo", gcArray);
162 (void)cJSON_AddItemToObject(json, "P2PInfo", p2pJson);
163 return SOFTBUS_OK;
164 }
165
166 } // namespace SoftBus
167 } // namespace Communication