1 /*
2  * Copyright (c) 2020-2022 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "hdf_wifi_event.h"
10 #include "hdf_log.h"
11 #include "hdf_sbuf.h"
12 #include "hdf_slist.h"
13 #include "hdf_wifi_product.h"
14 #include "osal_timer.h"
15 
16 #define HDF_LOG_TAG HDF_WIFI_CORE
17 
18 #ifdef __cplusplus
19 #if __cplusplus
20 extern "C" {
21 #endif
22 #endif
23 
HdfWifiEventNewSta(const struct NetDevice * netDev,const uint8_t * macAddr,uint8_t addrLen,const struct StationInfo * info)24 int32_t HdfWifiEventNewSta(const struct NetDevice *netDev, const uint8_t *macAddr, uint8_t addrLen,
25     const struct StationInfo *info)
26 {
27     struct HdfSBuf *data = NULL;
28     const uint32_t reassoc = 0;
29     int32_t ret;
30 
31     if (netDev == NULL || macAddr == NULL || info == NULL) {
32         HDF_LOGE("%s param is null", __func__);
33         return HDF_ERR_INVALID_PARAM;
34     }
35 
36     if (info->assocReqIesLen == 0) {
37         HDF_LOGE("%s assocReqIesLen is 0", __func__);
38         return HDF_FAILURE;
39     }
40 
41     data = HdfSbufObtainDefaultSize();
42     if (data == NULL) {
43         HDF_LOGE("%s InitDataBlock failed", __func__);
44         return HDF_FAILURE;
45     }
46 
47     if (!HdfSbufWriteString(data, netDev->name) || !HdfSbufWriteUint32(data, reassoc) ||
48         !HdfSbufWriteBuffer(data, info->assocReqIes, info->assocReqIesLen) ||
49         !HdfSbufWriteBuffer(data, macAddr, addrLen)) {
50         HDF_LOGE("%s sbuf write failed", __func__);
51         HdfSbufRecycle(data);
52         return HDF_FAILURE;
53     }
54 
55     HDF_LOGI("%s: netDev->name=%s, MAC=XX:XX:XX:XX:XX:%02X", __func__, netDev->name,
56         (addrLen > 0) ? macAddr[addrLen - 1] : -1);
57 
58     ret = HdfWlanSendBroadcastEvent(WIFI_WPA_EVENT_NEW_STA, data);
59     HdfSbufRecycle(data);
60     return ret;
61 }
62 
HdfWifiEventDelSta(struct NetDevice * netDev,const uint8_t * macAddr,uint8_t addrLen)63 int32_t HdfWifiEventDelSta(struct NetDevice *netDev, const uint8_t *macAddr, uint8_t addrLen)
64 {
65     struct HdfSBuf *data = NULL;
66     int32_t ret;
67 
68     if ((netDev == NULL) || (macAddr == NULL)) {
69         HDF_LOGE("%s param is null", __func__);
70         return HDF_ERR_INVALID_PARAM;
71     }
72 
73     data = HdfSbufObtainDefaultSize();
74     if (data == NULL) {
75         HDF_LOGE("%s InitDataBlock failed", __func__);
76         return HDF_FAILURE;
77     }
78 
79     if (!HdfSbufWriteString(data, netDev->name) || !HdfSbufWriteBuffer(data, macAddr, addrLen)) {
80         HDF_LOGE("%s sbuf write failed", __func__);
81         HdfSbufRecycle(data);
82         return HDF_FAILURE;
83     }
84 
85     ret = HdfWlanSendBroadcastEvent(WIFI_WPA_EVENT_DEL_STA, data);
86     HdfSbufRecycle(data);
87     return ret;
88 }
89 
HdfWifiEventInformBssFrame(const struct NetDevice * netDev,const struct WlanChannel * channel,const struct ScannedBssInfo * bssInfo)90 int32_t HdfWifiEventInformBssFrame(const struct NetDevice *netDev,
91     const struct WlanChannel *channel, const struct ScannedBssInfo *bssInfo)
92 {
93     struct HdfSBuf *data = NULL;
94     uint32_t ieLen;
95     uint32_t beaconLen;
96     int32_t ret;
97 
98     if ((netDev == NULL) || (channel == NULL) || (bssInfo == NULL) || (bssInfo->mgmt == NULL)) {
99         HDF_LOGE("%s param is null", __func__);
100         return HDF_ERR_INVALID_PARAM;
101     }
102 
103     data = HdfSbufObtainDefaultSize();
104     if (data == NULL) {
105         HDF_LOGE("%s InitDataBlock failed", __func__);
106         return HDF_FAILURE;
107     }
108 
109     ieLen = bssInfo->mgmtLen - (uint32_t)OFFSET_OF(struct Ieee80211Mgmt, u.probeResp.variable);
110     beaconLen = bssInfo->mgmtLen - (uint32_t)OFFSET_OF(struct Ieee80211Mgmt, u.beacon.variable);
111     if (!HdfSbufWriteString(data, netDev->name) ||
112         !HdfSbufWriteUint16(data, bssInfo->mgmt->u.probeResp.beaconInt) ||
113         !HdfSbufWriteUint16(data, bssInfo->mgmt->u.probeResp.capabInfo) ||
114         !HdfSbufWriteInt32(data, bssInfo->signal) || !HdfSbufWriteUint32(data, channel->centerFreq) ||
115         !HdfSbufWriteUint32(data, channel->flags) ||
116         !HdfSbufWriteBuffer(data, bssInfo->mgmt->bssid, ETH_ADDR_LEN) ||
117         !HdfSbufWriteBuffer(data, bssInfo->mgmt->u.probeResp.variable, ieLen) ||
118         !HdfSbufWriteBuffer(data, bssInfo->mgmt->u.beacon.variable, beaconLen)) {
119         HDF_LOGE("%s sbuf write failed", __func__);
120         HdfSbufRecycle(data);
121         return HDF_FAILURE;
122     }
123 
124     ret = HdfWlanSendBroadcastEvent(WIFI_WPA_EVENT_SCAN_RESULT, data);
125     HdfSbufRecycle(data);
126     return ret;
127 }
128 
HdfWifiEventScanDone(const struct NetDevice * netDev,WifiScanStatus status)129 int32_t HdfWifiEventScanDone(const struct NetDevice *netDev, WifiScanStatus status)
130 {
131     uint32_t code = status;
132     struct HdfSBuf *data = NULL;
133     int32_t ret;
134 
135     if (netDev == NULL) {
136         HDF_LOGE("%s param is null", __func__);
137         return HDF_ERR_INVALID_PARAM;
138     }
139 
140     data = HdfSbufObtainDefaultSize();
141     if (data == NULL) {
142         HDF_LOGE("%s InitDataBlock failed", __func__);
143         return HDF_FAILURE;
144     }
145 
146     if (!HdfSbufWriteString(data, netDev->name) || !HdfSbufWriteUint32(data, code)) {
147         HDF_LOGE("%s sbuf write failed", __func__);
148         HdfSbufRecycle(data);
149         return HDF_FAILURE;
150     }
151 
152     ret = HdfWlanSendBroadcastEvent(WIFI_WPA_EVENT_SCAN_DONE, data);
153     HdfSbufRecycle(data);
154     return ret;
155 }
156 
157 #if (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
158 #define DHCP_CHECK_CNT 30
159 #define DHCP_CHECK_TIME 1000
160 uint32_t g_ipCheckOut = 0;
161 OSAL_DECLARE_TIMER(g_dhcpTimer);
162 
HdfDhcpResultCheck(uintptr_t para)163 static void HdfDhcpResultCheck(uintptr_t para)
164 {
165     int32_t ret;
166     struct NetDevice *dev = (struct NetDevice *)para;
167 
168     if (dev == NULL) {
169         return;
170     }
171     ret = NetIfDhcpIsBound(dev);
172     if (ret == 0) {
173         OsalTimerDelete(&g_dhcpTimer);
174         return;
175     }
176 
177     if (g_ipCheckOut++ > DHCP_CHECK_CNT) {
178         OsalTimerDelete(&g_dhcpTimer);
179         return;
180     }
181 }
182 
HdfStartDhcpClient(const struct NetDevice * netDev)183 static void HdfStartDhcpClient(const struct NetDevice *netDev)
184 {
185     int32_t ret;
186 
187     /* start dhcp client */
188     (void)NetIfDhcpStop((struct NetDevice *)netDev);
189     ret = NetIfDhcpStart((struct NetDevice *)netDev);
190     if (ret != HDF_SUCCESS) {
191         HDF_LOGE("HdfWifiEventConnectResult::start dhcp client failed");
192     }
193 
194     if (g_dhcpTimer.realTimer != NULL) {
195         OsalTimerDelete(&g_dhcpTimer);
196     }
197 
198     g_ipCheckOut = 0;
199     OsalTimerCreate(&g_dhcpTimer, DHCP_CHECK_TIME, HdfDhcpResultCheck, (uintptr_t)netDev);
200     OsalTimerStartLoop(&g_dhcpTimer);
201 }
202 #endif
203 
HdfWifiEventConnectResult(const struct NetDevice * netDev,const struct ConnetResult * result)204 int32_t HdfWifiEventConnectResult(const struct NetDevice *netDev, const struct ConnetResult *result)
205 {
206     struct HdfSBuf *data = NULL;
207     int32_t ret;
208 
209     if (netDev == NULL || result == NULL) {
210         HDF_LOGE("%s param is null", __func__);
211         return HDF_ERR_INVALID_PARAM;
212     }
213 
214     data = HdfSbufObtainDefaultSize();
215     if (data == NULL) {
216         HDF_LOGE("%s InitDataBlock failed", __func__);
217         return HDF_FAILURE;
218     }
219 
220     if (!HdfSbufWriteString(data, netDev->name) || !HdfSbufWriteUint16(data, result->connectStatus) ||
221         !HdfSbufWriteUint16(data, result->freq) || !HdfSbufWriteBuffer(data, result->bssid, ETH_ADDR_LEN) ||
222         !HdfSbufWriteBuffer(data, result->reqIe, result->reqIeLen) ||
223         !HdfSbufWriteBuffer(data, result->rspIe, result->rspIeLen)) {
224         HDF_LOGE("%s sbuf write failed", __func__);
225         HdfSbufRecycle(data);
226         return HDF_FAILURE;
227     }
228 
229     ret = HdfWlanSendBroadcastEvent(WIFI_WPA_EVENT_CONNECT_RESULT, data);
230     HdfSbufRecycle(data);
231 
232 #if (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
233     if (result->connectStatus == WIFI_HMAC_MGMT_SUCCESS) {
234         HdfStartDhcpClient(netDev);
235     }
236 #endif
237 
238     return ret;
239 }
240 
HdfWifiEventDisconnected(const struct NetDevice * netDev,uint16_t reason,const uint8_t * ie,uint32_t len)241 int32_t HdfWifiEventDisconnected(const struct NetDevice *netDev, uint16_t reason, const uint8_t *ie, uint32_t len)
242 {
243     struct HdfSBuf *data = NULL;
244     int32_t ret;
245 
246     if (netDev == NULL) {
247         HDF_LOGE("%s param is null", __func__);
248         return HDF_ERR_INVALID_PARAM;
249     }
250 
251     data = HdfSbufObtainDefaultSize();
252     if (data == NULL) {
253         HDF_LOGE("%s InitDataBlock failed", __func__);
254         return HDF_FAILURE;
255     }
256 
257     if (!HdfSbufWriteString(data, netDev->name) || !HdfSbufWriteUint16(data, reason) ||
258         !HdfSbufWriteBuffer(data, ie, len)) {
259         HDF_LOGE("%s sbuf write failed", __func__);
260         HdfSbufRecycle(data);
261         return HDF_FAILURE;
262     }
263 
264     ret = HdfWlanSendBroadcastEvent(WIFI_WPA_EVENT_DISCONNECT, data);
265     HdfSbufRecycle(data);
266     return ret;
267 }
268 
HdfWifiEventMgmtTxStatus(const struct NetDevice * netDev,const uint8_t * buf,size_t len,uint8_t ack)269 int32_t HdfWifiEventMgmtTxStatus(const struct NetDevice *netDev, const uint8_t *buf, size_t len, uint8_t ack)
270 {
271     struct HdfSBuf *data = NULL;
272     int32_t ret;
273 
274     if (netDev == NULL) {
275         HDF_LOGE("%s param is null", __func__);
276         return HDF_ERR_INVALID_PARAM;
277     }
278 
279     data = HdfSbufObtainDefaultSize();
280     if (data == NULL) {
281         HDF_LOGE("%s InitDataBlock failed", __func__);
282         return HDF_FAILURE;
283     }
284 
285     if (!HdfSbufWriteString(data, netDev->name) || !HdfSbufWriteUint8(data, ack) ||
286         !HdfSbufWriteBuffer(data, buf, len)) {
287         HDF_LOGE("%s sbuf write failed", __func__);
288         HdfSbufRecycle(data);
289         return HDF_FAILURE;
290     }
291 
292     ret = HdfWlanSendBroadcastEvent(WIFI_WPA_EVENT_TX_STATUS, data);
293     HdfSbufRecycle(data);
294     return ret;
295 }
296 
HdfWifiEventCsaChannelSwitch(const struct NetDevice * netDev,int32_t freq)297 int32_t HdfWifiEventCsaChannelSwitch(const struct NetDevice *netDev, int32_t freq)
298 {
299     struct HdfSBuf *data = NULL;
300     int32_t ret;
301 
302     if (netDev == NULL) {
303         HDF_LOGE("%s param is null", __func__);
304         return HDF_ERR_INVALID_PARAM;
305     }
306 
307     data = HdfSbufObtainDefaultSize();
308     if (data == NULL) {
309         HDF_LOGE("%s InitDataBlock failed", __func__);
310         return HDF_FAILURE;
311     }
312 
313     if (!HdfSbufWriteString(data, netDev->name) || !HdfSbufWriteUint32(data, freq)) {
314         HDF_LOGE("%s sbuf write failed", __func__);
315         HdfSbufRecycle(data);
316         return HDF_FAILURE;
317     }
318 
319     ret = HdfWlanSendBroadcastEvent(WIFI_WPA_EVENT_CHANNEL_SWITCH, data);
320     HdfSbufRecycle(data);
321     return ret;
322 }
323 
HdfWifiEventTimeoutDisconnected(const struct NetDevice * netDev)324 int32_t HdfWifiEventTimeoutDisconnected(const struct NetDevice *netDev)
325 {
326     struct HdfSBuf *data = NULL;
327     int32_t ret;
328 
329     if (netDev == NULL) {
330         HDF_LOGE("%s param is null", __func__);
331         return HDF_ERR_INVALID_PARAM;
332     }
333 
334     data = HdfSbufObtainDefaultSize();
335     if (data == NULL) {
336         HDF_LOGE("%s InitDataBlock failed", __func__);
337         return HDF_FAILURE;
338     }
339     if (!HdfSbufWriteString(data, netDev->name)) {
340         HDF_LOGE("%s sbuf write failed", __func__);
341         HdfSbufRecycle(data);
342         return HDF_FAILURE;
343     }
344 
345     ret = HdfWlanSendBroadcastEvent(WIFI_WPA_EVENT_TIMEOUT_DISCONN, data);
346     HdfSbufRecycle(data);
347     return ret;
348 }
349 
HdfWifiEventRxMgmt(const struct NetDevice * netDev,int32_t freq,int32_t sigMbm,const uint8_t * buf,size_t len)350 int32_t HdfWifiEventRxMgmt(const struct NetDevice *netDev, int32_t freq, int32_t sigMbm, const uint8_t *buf, size_t len)
351 {
352     struct HdfSBuf *data = NULL;
353     int32_t ret;
354 
355     if (netDev == NULL) {
356         HDF_LOGE("%s param is null", __func__);
357         return HDF_ERR_INVALID_PARAM;
358     }
359 
360     data = HdfSbufObtainDefaultSize();
361     if (data == NULL) {
362         HDF_LOGE("%s InitDataBlock failed", __func__);
363         return HDF_FAILURE;
364     }
365 
366     if (!HdfSbufWriteString(data, netDev->name) || !HdfSbufWriteUint32(data, freq) ||
367         !HdfSbufWriteUint32(data, sigMbm) || !HdfSbufWriteBuffer(data, buf, len)) {
368         HDF_LOGE("%s sbuf write failed", __func__);
369         HdfSbufRecycle(data);
370         return HDF_FAILURE;
371     }
372 
373     ret = HdfWlanSendBroadcastEvent(WIFI_WPA_EVENT_RX_MGMT, data);
374     HdfSbufRecycle(data);
375     return ret;
376 }
377 
HdfWifiEventEapolRecv(const char * name,void * context)378 void HdfWifiEventEapolRecv(const char *name, void *context)
379 {
380     struct HdfSBuf *data = NULL;
381     int32_t ret;
382 
383     (void)context;
384     if (name == NULL) {
385         HDF_LOGE("%s name is NULL", __func__);
386         return;
387     }
388     data = HdfSbufObtainDefaultSize();
389     if (data == NULL) {
390         HDF_LOGE("%s InitDataBlock failed", __func__);
391         return;
392     }
393     if (!HdfSbufWriteString(data, name)) {
394         HDF_LOGE("%s sbuf write failed", __func__);
395         HdfSbufRecycle(data);
396         return;
397     }
398 
399     ret = HdfWlanSendBroadcastEvent(WIFI_WPA_EVENT_EAPOL_RECV, data);
400     if (ret != HDF_SUCCESS) {
401         HDF_LOGE("%s send event failed", __func__);
402     }
403     HdfSbufRecycle(data);
404 }
405 
HdfWifiEventResetResult(const uint8_t chipId,int32_t resetStatus,const char * ifName)406 int32_t HdfWifiEventResetResult(const uint8_t chipId, int32_t resetStatus, const char *ifName)
407 {
408     struct HdfSBuf *data = NULL;
409     int32_t ret;
410 
411     data = HdfSbufObtainDefaultSize();
412     if (data == NULL) {
413         HDF_LOGE("%s InitDataBlock failed", __func__);
414         return HDF_FAILURE;
415     }
416     if (!HdfSbufWriteString(data, ifName)) {
417         HDF_LOGE("%s: Serialize failed!", __func__);
418         HdfSbufRecycle(data);
419         return HDF_FAILURE;
420     }
421     if (!HdfSbufWriteInt32(data, resetStatus)) {
422         HDF_LOGE("%s sbuf write failed", __func__);
423         HdfSbufRecycle(data);
424         return HDF_FAILURE;
425     }
426     if (!HdfSbufWriteUint8(data, chipId)) {
427         HDF_LOGE("%s sbuf write failed", __func__);
428         HdfSbufRecycle(data);
429         return HDF_FAILURE;
430     }
431     ret = HdfWlanSendBroadcastEvent(WIFI_WPA_EVENT_RESET_DRIVER, data);
432     HdfSbufRecycle(data);
433     return ret;
434 }
435 
HdfWifiEventRemainOnChannel(const struct NetDevice * netDev,uint32_t freq,uint32_t duration)436 int32_t HdfWifiEventRemainOnChannel(const struct NetDevice *netDev, uint32_t freq, uint32_t duration)
437 {
438     struct HdfSBuf *data = NULL;
439     int32_t ret;
440 
441     data = HdfSbufObtainDefaultSize();
442     if (data == NULL) {
443         HDF_LOGE("%s InitDataBlock failed", __func__);
444         return HDF_FAILURE;
445     }
446     if (!HdfSbufWriteString(data, netDev->name) || !HdfSbufWriteUint32(data, freq)) {
447         HDF_LOGE("%s sbuf write failed", __func__);
448         HdfSbufRecycle(data);
449         return HDF_FAILURE;
450     }
451     if (!HdfSbufWriteUint32(data, duration)) {
452         HDF_LOGE("%s sbuf write failed", __func__);
453         HdfSbufRecycle(data);
454         return HDF_FAILURE;
455     }
456     ret = HdfWlanSendBroadcastEvent(WIFI_WPA_EVENT_REMAIN_ON_CHANNEL, data);
457     HdfSbufRecycle(data);
458     return ret;
459 }
460 
HdfWifiEventCancelRemainOnChannel(const struct NetDevice * netDev,uint32_t freq)461 int32_t HdfWifiEventCancelRemainOnChannel(const struct NetDevice *netDev, uint32_t freq)
462 {
463     struct HdfSBuf *data = NULL;
464     int32_t ret;
465 
466     data = HdfSbufObtainDefaultSize();
467     if (data == NULL) {
468         HDF_LOGE("%s InitDataBlock failed", __func__);
469         return HDF_FAILURE;
470     }
471     if (!HdfSbufWriteString(data, netDev->name) || !HdfSbufWriteUint32(data, freq)) {
472         HDF_LOGE("%s sbuf write failed", __func__);
473         HdfSbufRecycle(data);
474         return HDF_FAILURE;
475     }
476     ret = HdfWlanSendBroadcastEvent(WIFI_WPA_EVENT_CANCEL_REMAIN_ON_CHANNEL, data);
477     HdfSbufRecycle(data);
478     return ret;
479 }
480 
481 #ifdef __cplusplus
482 #if __cplusplus
483 }
484 #endif
485 #endif
486