1 /*
2 * Copyright (c) 2021-2023 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_common_cmd.h"
17 #include "hdf_io_service.h"
18 #include "hdf_sbuf.h"
19 #include "hdf_log.h"
20 #include "securec.h"
21 #include "wifi_driver_client.h"
22 #include <osal_mem.h>
23
24 #ifdef __cplusplus
25 #if __cplusplus
26 extern "C" {
27 #endif
28 #endif
29
30 WifiScanResults g_scanResults = { 0 };
31
WifiEventNewStaProcess(const char * ifName,uint32_t event,struct HdfSBuf * reqData)32 static void WifiEventNewStaProcess(const char *ifName, uint32_t event, struct HdfSBuf *reqData)
33 {
34 WifiNewStaInfo staInfo;
35 uint32_t len = 0;
36
37 if (!HdfSbufReadInt32(reqData, &staInfo.reassoc)) {
38 HDF_LOGE("%s: fail to get reassoc", __FUNCTION__);
39 return;
40 }
41 if (!HdfSbufReadBuffer(reqData, (const void **)(&staInfo.ie), &staInfo.ieLen)) {
42 HDF_LOGE("%s: fail to get ie", __FUNCTION__);
43 return;
44 }
45 if (!HdfSbufReadBuffer(reqData, (const void **)(&staInfo.macAddr), &len) || (len != ETH_ADDR_LEN)) {
46 HDF_LOGE("%s: fail to get macAddr", __FUNCTION__);
47 return;
48 }
49 WifiEventReport(ifName, event, &staInfo);
50 }
51
WifiEventDelStaProcess(const char * ifName,uint32_t event,struct HdfSBuf * reqData)52 static void WifiEventDelStaProcess(const char *ifName, uint32_t event, struct HdfSBuf *reqData)
53 {
54 uint8_t *addr = NULL;
55 uint32_t len = 0;
56
57 if ((!HdfSbufReadBuffer(reqData, (const void **)(&addr), &len)) || (len != ETH_ADDR_LEN)) {
58 HDF_LOGE("%s: fail to get macAddr", __FUNCTION__);
59 return;
60 }
61 WifiEventReport(ifName, event, addr);
62 }
63
WifiEventRxMgmtProcess(const char * ifName,uint32_t event,struct HdfSBuf * reqData)64 static void WifiEventRxMgmtProcess(const char *ifName, uint32_t event, struct HdfSBuf *reqData)
65 {
66 WifiRxMgmt rxMgmt;
67
68 if (!HdfSbufReadInt32(reqData, &rxMgmt.freq)) {
69 HDF_LOGE("%s: fail to get freq", __FUNCTION__);
70 return;
71 }
72 if (!HdfSbufReadInt32(reqData, &rxMgmt.sigMbm)) {
73 HDF_LOGE("%s: fail to get sigMbm", __FUNCTION__);
74 return;
75 }
76 if (!HdfSbufReadBuffer(reqData, (const void **)(&rxMgmt.buf), &rxMgmt.len)) {
77 HDF_LOGE("%s: fail to get buf", __FUNCTION__);
78 return;
79 }
80 WifiEventReport(ifName, event, &rxMgmt);
81 }
82
WifiEventTxStatusProcess(const char * ifName,uint32_t event,struct HdfSBuf * reqData)83 static void WifiEventTxStatusProcess(const char *ifName, uint32_t event, struct HdfSBuf *reqData)
84 {
85 WifiTxStatus txStatus;
86
87 if (!HdfSbufReadUint8(reqData, &txStatus.ack)) {
88 HDF_LOGE("%s: fail to get ack", __FUNCTION__);
89 return;
90 }
91 if (!HdfSbufReadBuffer(reqData, (const void **)(&txStatus.buf), &txStatus.len)) {
92 HDF_LOGE("%s: fail to get buf", __FUNCTION__);
93 return;
94 }
95 WifiEventReport(ifName, event, &txStatus);
96 }
97
WifiEventScanDoneProcess(const char * ifName,uint32_t event,struct HdfSBuf * reqData)98 static void WifiEventScanDoneProcess(const char *ifName, uint32_t event, struct HdfSBuf *reqData)
99 {
100 uint32_t status;
101
102 if (!HdfSbufReadUint32(reqData, &status)) {
103 HDF_LOGE("%s: fail to get status", __FUNCTION__);
104 return;
105 }
106 if (g_scanResults.num != 0) {
107 WifiEventReport(ifName, WIFI_EVENT_SCAN_RESULTS, &g_scanResults);
108 FreeScanResults(&g_scanResults);
109 (void)memset_s(&g_scanResults, sizeof(WifiScanResults), 0, sizeof(WifiScanResults));
110 }
111 WifiEventReport(ifName, event, &status);
112 }
113
FillScanResult(WifiScanResult * dst,WifiScanResult * src)114 static int32_t FillScanResult(WifiScanResult *dst, WifiScanResult *src)
115 {
116 int32_t ret = RET_CODE_FAILURE;
117
118 if (dst == NULL || src == NULL || src->bssid == NULL || src->ie == NULL || src->beaconIe == NULL ||
119 src->ieLen == 0 || src->beaconIeLen == 0) {
120 HDF_LOGE("%s: Invalid parameters", __FUNCTION__);
121 return RET_CODE_INVALID_PARAM;
122 }
123 if (memcpy_s(dst, sizeof(WifiScanResult), src, sizeof(WifiScanResult)) != EOK) {
124 return RET_CODE_FAILURE;
125 }
126 do {
127 dst->bssid = OsalMemCalloc(ETH_ADDR_LEN);
128 if (dst->bssid == NULL) {
129 HDF_LOGE("%s: OsalMemCalloc bssid fail", __FUNCTION__);
130 break;
131 }
132 if (memcpy_s(dst->bssid, ETH_ADDR_LEN, src->bssid, ETH_ADDR_LEN) != EOK) {
133 HDF_LOGE("%s: memcpy_s bssid fail", __FUNCTION__);
134 break;
135 }
136 dst->ie = OsalMemCalloc(src->ieLen);
137 if (dst->ie == NULL) {
138 HDF_LOGE("%s: OsalMemCalloc ie fail", __FUNCTION__);
139 break;
140 }
141 if (memcpy_s(dst->ie, src->ieLen, src->ie, src->ieLen) != EOK) {
142 HDF_LOGE("%s: memcpy_s ie fail", __FUNCTION__);
143 break;
144 }
145 dst->beaconIe = OsalMemCalloc(src->beaconIeLen);
146 if (dst->beaconIe == NULL) {
147 HDF_LOGE("%s: OsalMemCalloc beaconIe fail", __FUNCTION__);
148 break;
149 }
150 if (memcpy_s(dst->beaconIe, src->beaconIeLen, src->beaconIe, src->beaconIeLen) != EOK) {
151 HDF_LOGE("%s: memcpy_s beaconIe fail", __FUNCTION__);
152 break;
153 }
154 ret = RET_CODE_SUCCESS;
155 } while (0);
156 if (ret != RET_CODE_SUCCESS) {
157 FreeScanResult(dst);
158 }
159 return ret;
160 }
161
WifiEventScanResultProcess(const char * ifName,uint32_t event,struct HdfSBuf * reqData)162 static void WifiEventScanResultProcess(const char *ifName, uint32_t event, struct HdfSBuf *reqData)
163 {
164 WifiScanResult scanResult = { 0 };
165 uint32_t len = 0;
166
167 if (!HdfSbufReadUint16(reqData, &(scanResult.beaconInt))) {
168 HDF_LOGE("%s: fail to get beaconInt", __FUNCTION__);
169 return;
170 }
171 if (!HdfSbufReadUint16(reqData, &(scanResult.caps))) {
172 HDF_LOGE("%s: fail to get caps", __FUNCTION__);
173 return;
174 }
175 if (!HdfSbufReadInt32(reqData, &(scanResult.level))) {
176 HDF_LOGE("%s: fail to get level", __FUNCTION__);
177 return;
178 }
179 scanResult.level /= SIGNAL_LEVEL_CONFFICIENT; /* mBm to dBm */
180 if (!HdfSbufReadUint32(reqData, &(scanResult.freq))) {
181 HDF_LOGE("%s: fail to get freq", __FUNCTION__);
182 return;
183 }
184 if (!HdfSbufReadUint32(reqData, &(scanResult.flags))) {
185 HDF_LOGE("%s: fail to get flags", __FUNCTION__);
186 return;
187 }
188 if ((!HdfSbufReadBuffer(reqData, (const void **)(&(scanResult.bssid)), &len)) || len != ETH_ADDR_LEN) {
189 HDF_LOGE("%s: fail to get bssid", __FUNCTION__);
190 return;
191 }
192 if (!HdfSbufReadBuffer(reqData, (const void **)(&(scanResult.ie)), &(scanResult.ieLen))) {
193 HDF_LOGE("%s: fail to get ie", __FUNCTION__);
194 return;
195 }
196 if (!HdfSbufReadBuffer(reqData, (const void **)(&(scanResult.beaconIe)), &(scanResult.beaconIeLen))) {
197 HDF_LOGE("%s: fail to get beaconIe", __FUNCTION__);
198 return;
199 }
200 WifiEventReport(ifName, event, &scanResult);
201 if (FillScanResult(&g_scanResults.scanResult[g_scanResults.num], &scanResult) != EOK) {
202 HDF_LOGE("%s: fail to fill scan result", __FUNCTION__);
203 return;
204 }
205 g_scanResults.num++;
206 if (g_scanResults.num == MAX_SCAN_RES_NUM) {
207 WifiEventReport(ifName, WIFI_EVENT_SCAN_RESULTS, &g_scanResults);
208 FreeScanResults(&g_scanResults);
209 (void)memset_s(&g_scanResults, sizeof(WifiScanResults), 0, sizeof(WifiScanResults));
210 }
211 }
212
WifiEventConnectResultProcess(const char * ifName,uint32_t event,struct HdfSBuf * reqData)213 static void WifiEventConnectResultProcess(const char *ifName, uint32_t event, struct HdfSBuf *reqData)
214 {
215 WifiConnectResult result;
216 uint32_t len = 0;
217
218 if (!HdfSbufReadUint16(reqData, &(result.status))) {
219 HDF_LOGE("%s: fail to get status", __FUNCTION__);
220 return;
221 }
222 if (!HdfSbufReadUint16(reqData, &(result.freq))) {
223 HDF_LOGE("%s: fail to get freq", __FUNCTION__);
224 return;
225 }
226 if ((!HdfSbufReadBuffer(reqData, (const void **)(&(result.bssid)), &len)) || len != ETH_ADDR_LEN) {
227 HDF_LOGE("%s: fail to get bssid", __FUNCTION__);
228 return;
229 }
230 if (!HdfSbufReadBuffer(reqData, (const void **)(&(result.reqIe)), &(result.reqIeLen))) {
231 HDF_LOGE("%s: fail to get reqIe", __FUNCTION__);
232 return;
233 }
234 if (!HdfSbufReadBuffer(reqData, (const void **)(&(result.respIe)), &(result.respIeLen))) {
235 HDF_LOGE("%s: fail to get respIe", __FUNCTION__);
236 return;
237 }
238 WifiEventReport(ifName, event, &result);
239 }
240
WifiEventDisconnectProcess(const char * ifName,uint32_t event,struct HdfSBuf * reqData)241 static void WifiEventDisconnectProcess(const char *ifName, uint32_t event, struct HdfSBuf *reqData)
242 {
243 WifiDisconnect result;
244
245 if (!HdfSbufReadUint16(reqData, &(result.reason))) {
246 HDF_LOGE("%s: fail to get reason", __FUNCTION__);
247 return;
248 }
249 if (!HdfSbufReadBuffer(reqData, (const void **)(&(result.ie)), &(result.ieLen))) {
250 HDF_LOGE("%s: fail to get bssid", __FUNCTION__);
251 return;
252 }
253 WifiEventReport(ifName, event, &result);
254 }
255
WifiDriverEventEapolRecvProcess(const char * ifName,uint32_t event,struct HdfSBuf * reqData)256 static void WifiDriverEventEapolRecvProcess(const char *ifName, uint32_t event, struct HdfSBuf *reqData)
257 {
258 WifiEventReport(ifName, event, reqData);
259 }
260
WifiEventResetDriverProcess(const char * ifName,int32_t event,struct HdfSBuf * reqData)261 static void WifiEventResetDriverProcess(const char *ifName, int32_t event, struct HdfSBuf *reqData)
262 {
263 unsigned char chipId;
264 int resetStatus;
265
266 if (!HdfSbufReadInt32(reqData, &resetStatus)) {
267 HDF_LOGE("%s: fail to get resetStatus, line: %d", __FUNCTION__, __LINE__);
268 return;
269 }
270 if (!HdfSbufReadUint8(reqData, &chipId)) {
271 HDF_LOGE("%s: fail to get chipId, line: %d", __FUNCTION__, __LINE__);
272 return;
273 }
274 WifiEventReport(ifName, event, &resetStatus);
275 }
276
WifiDriverEventRemainOnChannelProcess(const char * ifName,uint32_t event,struct HdfSBuf * reqData)277 static void WifiDriverEventRemainOnChannelProcess(const char *ifName, uint32_t event, struct HdfSBuf *reqData)
278 {
279 WifiOnChannel result = {0};
280 if (!HdfSbufReadUint32(reqData, &(result.freq))) {
281 HDF_LOGE("%s failed to get frequency.", __FUNCTION__);
282 return;
283 }
284 if (!HdfSbufReadUint32(reqData, &(result.duration))) {
285 HDF_LOGE("%s failed to get duration.", __FUNCTION__);
286 return;
287 }
288 WifiEventReport(ifName, event, &result);
289 }
290
WifiDriverEventCancelRemainOnChannelProcess(const char * ifName,uint32_t event,struct HdfSBuf * reqData)291 static void WifiDriverEventCancelRemainOnChannelProcess(const char *ifName, uint32_t event, struct HdfSBuf *reqData)
292 {
293 WifiOnChannel result = {0};
294 if (!HdfSbufReadUint32(reqData, &(result.freq))) {
295 HDF_LOGE("%s: fail to get freq", __FUNCTION__);
296 return;
297 }
298 WifiEventReport(ifName, event, &result);
299 }
300
OnWiFiEvents(struct HdfDevEventlistener * listener,struct HdfIoService * service,uint32_t eventId,struct HdfSBuf * data)301 int OnWiFiEvents(struct HdfDevEventlistener *listener,
302 struct HdfIoService *service, uint32_t eventId, struct HdfSBuf *data)
303 {
304 (void)listener;
305 (void)service;
306
307 if (data == NULL) {
308 HDF_LOGE("%s: params is NULL, line: %d", __FUNCTION__, __LINE__);
309 return RET_CODE_FAILURE;
310 }
311 const char *ifName = HdfSbufReadString(data);
312 if (ifName == NULL) {
313 HDF_LOGE("%s fail to get ifName", __FUNCTION__);
314 return RET_CODE_FAILURE;
315 }
316 HDF_LOGI("%s: WifiDriverEventProcess event=%d", __FUNCTION__, eventId);
317 switch (eventId) {
318 case WIFI_EVENT_NEW_STA:
319 WifiEventNewStaProcess(ifName, eventId, data);
320 break;
321 case WIFI_EVENT_DEL_STA:
322 WifiEventDelStaProcess(ifName, eventId, data);
323 break;
324 case WIFI_EVENT_RX_MGMT:
325 WifiEventRxMgmtProcess(ifName, eventId, data);
326 break;
327 case WIFI_EVENT_TX_STATUS:
328 WifiEventTxStatusProcess(ifName, eventId, data);
329 break;
330 case WIFI_EVENT_SCAN_DONE:
331 WifiEventScanDoneProcess(ifName, eventId, data);
332 break;
333 case WIFI_EVENT_SCAN_RESULT:
334 WifiEventScanResultProcess(ifName, eventId, data);
335 break;
336 case WIFI_EVENT_CONNECT_RESULT:
337 WifiEventConnectResultProcess(ifName, eventId, data);
338 break;
339 case WIFI_EVENT_DISCONNECT:
340 WifiEventDisconnectProcess(ifName, eventId, data);
341 break;
342 case WIFI_EVENT_EAPOL_RECV:
343 WifiDriverEventEapolRecvProcess(ifName, eventId, data);
344 break;
345 case WIFI_EVENT_RESET_DRIVER:
346 WifiEventResetDriverProcess(ifName, eventId, data);
347 break;
348 case WIFI_EVENT_REMAIN_ON_CHANNEL:
349 WifiDriverEventRemainOnChannelProcess(ifName, eventId, data);
350 break;
351 case WIFI_EVENT_CANCEL_REMAIN_ON_CHANNEL:
352 WifiDriverEventCancelRemainOnChannelProcess(ifName, eventId, data);
353 break;
354 default:
355 break;
356 }
357 return RET_CODE_SUCCESS;
358 }
359
360 #ifdef __cplusplus
361 #if __cplusplus
362 }
363 #endif
364 #endif
365