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.scanResult == NULL) {
107         HDF_LOGE("%s: g_scanResults.scanResult is NULL", __FUNCTION__);
108         return;
109     }
110     WifiEventReport(ifName, WIFI_EVENT_SCAN_RESULTS, &g_scanResults);
111     HDF_LOGI("%s: g_scanResults.num = %u", __FUNCTION__, g_scanResults.num);
112     FreeScanResults(&g_scanResults);
113     WifiEventReport(ifName, event, &status);
114 }
115 
FillScanResult(WifiScanResult * dst,WifiScanResult * src)116 static int32_t FillScanResult(WifiScanResult *dst, WifiScanResult *src)
117 {
118     int32_t ret = RET_CODE_FAILURE;
119 
120     if (dst == NULL || src == NULL || src->bssid == NULL || src->ie == NULL || src->beaconIe == NULL ||
121         src->ieLen == 0 || src->beaconIeLen == 0) {
122         HDF_LOGE("%s: Invalid parameters", __FUNCTION__);
123         return RET_CODE_INVALID_PARAM;
124     }
125     if (memcpy_s(dst, sizeof(WifiScanResult), src, sizeof(WifiScanResult)) != EOK) {
126         return RET_CODE_FAILURE;
127     }
128     do {
129         dst->bssid = OsalMemCalloc(ETH_ADDR_LEN);
130         if (dst->bssid == NULL) {
131             HDF_LOGE("%s: OsalMemCalloc bssid fail", __FUNCTION__);
132             break;
133         }
134         if (memcpy_s(dst->bssid, ETH_ADDR_LEN, src->bssid, ETH_ADDR_LEN) != EOK) {
135             HDF_LOGE("%s: memcpy_s bssid fail", __FUNCTION__);
136             break;
137         }
138         dst->ie = OsalMemCalloc(src->ieLen);
139         if (dst->ie == NULL) {
140             HDF_LOGE("%s: OsalMemCalloc ie fail", __FUNCTION__);
141             break;
142         }
143         if (memcpy_s(dst->ie, src->ieLen, src->ie, src->ieLen) != EOK) {
144             HDF_LOGE("%s: memcpy_s ie fail", __FUNCTION__);
145             break;
146         }
147         dst->beaconIe = OsalMemCalloc(src->beaconIeLen);
148         if (dst->beaconIe == NULL) {
149             HDF_LOGE("%s: OsalMemCalloc beaconIe fail", __FUNCTION__);
150             break;
151         }
152         if (memcpy_s(dst->beaconIe, src->beaconIeLen, src->beaconIe, src->beaconIeLen) != EOK) {
153             HDF_LOGE("%s: memcpy_s beaconIe fail", __FUNCTION__);
154             break;
155         }
156         ret = RET_CODE_SUCCESS;
157     } while (0);
158     if (ret != RET_CODE_SUCCESS) {
159         FreeScanResult(dst);
160     }
161     return ret;
162 }
163 
WifiEventScanResultProcess(const char * ifName,uint32_t event,struct HdfSBuf * reqData)164 static void WifiEventScanResultProcess(const char *ifName, uint32_t event, struct HdfSBuf *reqData)
165 {
166     WifiScanResult scanResult = { 0 };
167     uint32_t len = 0;
168 
169     if (g_scanResults.scanResult == NULL) {
170         if (InitScanResults(&g_scanResults) != RET_CODE_SUCCESS) {
171             HDF_LOGE("%s: InitScanResults failed",  __FUNCTION__);
172             return;
173         }
174     }
175     if (!HdfSbufReadUint16(reqData, &(scanResult.beaconInt))) {
176         HDF_LOGE("%s: fail to get beaconInt", __FUNCTION__);
177         return;
178     }
179     if (!HdfSbufReadUint16(reqData, &(scanResult.caps))) {
180         HDF_LOGE("%s: fail to get caps", __FUNCTION__);
181         return;
182     }
183     if (!HdfSbufReadInt32(reqData, &(scanResult.level))) {
184         HDF_LOGE("%s: fail to get level", __FUNCTION__);
185         return;
186     }
187     scanResult.level /= SIGNAL_LEVEL_CONFFICIENT;  /* mBm to dBm */
188     if (!HdfSbufReadUint32(reqData, &(scanResult.freq))) {
189         HDF_LOGE("%s: fail to get freq", __FUNCTION__);
190         return;
191     }
192     if (!HdfSbufReadUint32(reqData, &(scanResult.flags))) {
193         HDF_LOGE("%s: fail to get flags", __FUNCTION__);
194         return;
195     }
196     if ((!HdfSbufReadBuffer(reqData, (const void **)(&(scanResult.bssid)), &len)) || len != ETH_ADDR_LEN) {
197         HDF_LOGE("%s: fail to get bssid", __FUNCTION__);
198         return;
199     }
200     if (!HdfSbufReadBuffer(reqData, (const void **)(&(scanResult.ie)), &(scanResult.ieLen))) {
201         HDF_LOGE("%s: fail to get ie", __FUNCTION__);
202         return;
203     }
204     if (!HdfSbufReadBuffer(reqData, (const void **)(&(scanResult.beaconIe)), &(scanResult.beaconIeLen))) {
205         HDF_LOGE("%s: fail to get beaconIe", __FUNCTION__);
206         return;
207     }
208     WifiEventReport(ifName, event, &scanResult);
209     if (FillScanResult(&g_scanResults.scanResult[g_scanResults.num], &scanResult) != EOK) {
210         HDF_LOGE("%s: fail to fill scan result", __FUNCTION__);
211         return;
212     }
213     g_scanResults.num++;
214     if (g_scanResults.num == g_scanResults.scanResultCapacity) {
215         g_scanResults.scanResultCapacity += INIT_SCAN_RES_NUM;
216         WifiScanResult *newScanResult = NULL;
217         newScanResult = (WifiScanResult *)OsalMemCalloc(sizeof(WifiScanResult) * (g_scanResults.scanResultCapacity));
218         if (newScanResult == NULL) {
219             HDF_LOGE("%s: newscanResult is NULL",  __FUNCTION__);
220             g_scanResults.num = 0;
221             g_scanResults.scanResultCapacity -= INIT_SCAN_RES_NUM;
222             return;
223         }
224         if (memcpy_s((void *)newScanResult, sizeof(WifiScanResult) * (g_scanResults.scanResultCapacity),
225             (void *)g_scanResults.scanResult, sizeof(WifiScanResult) * (g_scanResults.num)) != RET_CODE_SUCCESS) {
226             HDF_LOGE("%s: memcpy_s fail",  __FUNCTION__);
227         }
228         OsalMemFree(&g_scanResults.scanResult);
229         g_scanResults.scanResult = newScanResult;
230         newScanResult = NULL;
231     }
232 }
233 
WifiEventConnectResultProcess(const char * ifName,uint32_t event,struct HdfSBuf * reqData)234 static void WifiEventConnectResultProcess(const char *ifName, uint32_t event, struct HdfSBuf *reqData)
235 {
236     WifiConnectResult result;
237     uint32_t len = 0;
238 
239     if (!HdfSbufReadUint16(reqData, &(result.status))) {
240         HDF_LOGE("%s: fail to get status", __FUNCTION__);
241         return;
242     }
243     if (!HdfSbufReadUint16(reqData, &(result.freq))) {
244         HDF_LOGE("%s: fail to get freq", __FUNCTION__);
245         return;
246     }
247     if ((!HdfSbufReadBuffer(reqData, (const void **)(&(result.bssid)), &len)) || len != ETH_ADDR_LEN) {
248         HDF_LOGE("%s: fail to get bssid", __FUNCTION__);
249         return;
250     }
251     if (!HdfSbufReadBuffer(reqData, (const void **)(&(result.reqIe)), &(result.reqIeLen))) {
252         HDF_LOGE("%s: fail to get reqIe", __FUNCTION__);
253         return;
254     }
255     if (!HdfSbufReadBuffer(reqData, (const void **)(&(result.respIe)), &(result.respIeLen))) {
256         HDF_LOGE("%s: fail to get respIe", __FUNCTION__);
257         return;
258     }
259     WifiEventReport(ifName, event, &result);
260 }
261 
WifiEventDisconnectProcess(const char * ifName,uint32_t event,struct HdfSBuf * reqData)262 static void WifiEventDisconnectProcess(const char *ifName, uint32_t event, struct HdfSBuf *reqData)
263 {
264     WifiDisconnect result;
265 
266     if (!HdfSbufReadUint16(reqData, &(result.reason))) {
267         HDF_LOGE("%s: fail to get reason", __FUNCTION__);
268         return;
269     }
270     if (!HdfSbufReadBuffer(reqData, (const void **)(&(result.ie)), &(result.ieLen))) {
271         HDF_LOGE("%s: fail to get bssid", __FUNCTION__);
272         return;
273     }
274     WifiEventReport(ifName, event, &result);
275 }
276 
WifiDriverEventEapolRecvProcess(const char * ifName,uint32_t event,struct HdfSBuf * reqData)277 static void WifiDriverEventEapolRecvProcess(const char *ifName, uint32_t event, struct HdfSBuf *reqData)
278 {
279     WifiEventReport(ifName, event, reqData);
280 }
281 
WifiEventResetDriverProcess(const char * ifName,int32_t event,struct HdfSBuf * reqData)282 static void WifiEventResetDriverProcess(const char *ifName, int32_t event, struct HdfSBuf *reqData)
283 {
284     unsigned char chipId;
285     int resetStatus;
286 
287     if (!HdfSbufReadInt32(reqData, &resetStatus)) {
288         HDF_LOGE("%s: fail to get resetStatus, line: %d", __FUNCTION__, __LINE__);
289         return;
290     }
291     if (!HdfSbufReadUint8(reqData, &chipId)) {
292         HDF_LOGE("%s: fail to get chipId, line: %d", __FUNCTION__, __LINE__);
293         return;
294     }
295     WifiEventReport(ifName, event, &resetStatus);
296 }
297 
WifiDriverEventRemainOnChannelProcess(const char * ifName,uint32_t event,struct HdfSBuf * reqData)298 static void WifiDriverEventRemainOnChannelProcess(const char *ifName, uint32_t event, struct HdfSBuf *reqData)
299 {
300     WifiOnChannel result = {0};
301     if (!HdfSbufReadUint32(reqData, &(result.freq))) {
302         HDF_LOGE("%s failed to get frequency.", __FUNCTION__);
303         return;
304     }
305     if (!HdfSbufReadUint32(reqData, &(result.duration))) {
306         HDF_LOGE("%s failed to get duration.", __FUNCTION__);
307         return;
308     }
309     WifiEventReport(ifName, event, &result);
310 }
311 
WifiDriverEventCancelRemainOnChannelProcess(const char * ifName,uint32_t event,struct HdfSBuf * reqData)312 static void WifiDriverEventCancelRemainOnChannelProcess(const char *ifName, uint32_t event, struct HdfSBuf *reqData)
313 {
314     WifiOnChannel result = {0};
315     if (!HdfSbufReadUint32(reqData, &(result.freq))) {
316         HDF_LOGE("%s: fail to get freq", __FUNCTION__);
317         return;
318     }
319     WifiEventReport(ifName, event, &result);
320 }
321 
OnWiFiEvents(struct HdfDevEventlistener * listener,struct HdfIoService * service,uint32_t eventId,struct HdfSBuf * data)322 int OnWiFiEvents(struct HdfDevEventlistener *listener,
323     struct HdfIoService *service, uint32_t eventId, struct HdfSBuf *data)
324 {
325     (void)listener;
326     (void)service;
327 
328     if (data == NULL) {
329         HDF_LOGE("%s: params is NULL, line: %d", __FUNCTION__, __LINE__);
330         return RET_CODE_FAILURE;
331     }
332     const char *ifName = HdfSbufReadString(data);
333     if (ifName == NULL) {
334         HDF_LOGE("%s fail to get ifName", __FUNCTION__);
335         return RET_CODE_FAILURE;
336     }
337     HDF_LOGI("%s: WifiDriverEventProcess event=%u", __FUNCTION__, eventId);
338     switch (eventId) {
339         case WIFI_EVENT_NEW_STA:
340             WifiEventNewStaProcess(ifName, eventId, data);
341             break;
342         case WIFI_EVENT_DEL_STA:
343             WifiEventDelStaProcess(ifName, eventId, data);
344             break;
345         case WIFI_EVENT_RX_MGMT:
346             WifiEventRxMgmtProcess(ifName, eventId, data);
347             break;
348         case WIFI_EVENT_TX_STATUS:
349             WifiEventTxStatusProcess(ifName, eventId, data);
350             break;
351         case WIFI_EVENT_SCAN_DONE:
352             WifiEventScanDoneProcess(ifName, eventId, data);
353             break;
354         case WIFI_EVENT_SCAN_RESULT:
355             WifiEventScanResultProcess(ifName, eventId, data);
356             break;
357         case WIFI_EVENT_CONNECT_RESULT:
358             WifiEventConnectResultProcess(ifName, eventId, data);
359             break;
360         case WIFI_EVENT_DISCONNECT:
361             WifiEventDisconnectProcess(ifName, eventId, data);
362             break;
363         case WIFI_EVENT_EAPOL_RECV:
364             WifiDriverEventEapolRecvProcess(ifName, eventId, data);
365             break;
366         case WIFI_EVENT_RESET_DRIVER:
367             WifiEventResetDriverProcess(ifName, eventId, data);
368             break;
369         case WIFI_EVENT_REMAIN_ON_CHANNEL:
370             WifiDriverEventRemainOnChannelProcess(ifName, eventId, data);
371             break;
372         case WIFI_EVENT_CANCEL_REMAIN_ON_CHANNEL:
373             WifiDriverEventCancelRemainOnChannelProcess(ifName, eventId, data);
374             break;
375         default:
376             break;
377     }
378     return RET_CODE_SUCCESS;
379 }
380 
381 #ifdef __cplusplus
382 #if __cplusplus
383 }
384 #endif
385 #endif
386