• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 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, (int16_t)bssInfo->mgmt->u.probeResp.beaconInt) ||
113         !HdfSbufWriteUint16(data, (int16_t)bssInfo->mgmt->u.probeResp.capabInfo) ||
114         !HdfSbufWriteUint32(data, bssInfo->signal) || !HdfSbufWriteUint32(data, channel->centerFreq) ||
115         !HdfSbufWriteUint32(data, (int32_t)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 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 int32_t 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         return HDF_ERR_INVALID_PARAM;
386     }
387     data = HdfSbufObtainDefaultSize();
388     if (data == NULL) {
389         HDF_LOGE("%s InitDataBlock failed", __func__);
390         return HDF_FAILURE;
391     }
392     if (!HdfSbufWriteString(data, name)) {
393         HDF_LOGE("%s sbuf write failed", __func__);
394         HdfSbufRecycle(data);
395         return HDF_FAILURE;
396     }
397 
398     ret = HdfWlanSendBroadcastEvent(WIFI_WPA_EVENT_EAPOL_RECV, data);
399     HdfSbufRecycle(data);
400     return ret;
401 }
402 
HdfWifiEventResetResult(const uint8_t chipId,int32_t resetStatus,const char * ifName)403 int32_t HdfWifiEventResetResult(const uint8_t chipId, int32_t resetStatus, const char *ifName)
404 {
405     struct HdfSBuf *data = NULL;
406     int32_t ret;
407 
408     data = HdfSbufObtainDefaultSize();
409     if (data == NULL) {
410         HDF_LOGE("%s InitDataBlock failed", __func__);
411         return HDF_FAILURE;
412     }
413     if (!HdfSbufWriteString(data, ifName)) {
414         HDF_LOGE("%s: Serialize failed!", __func__);
415         HdfSbufRecycle(data);
416         return HDF_FAILURE;
417     }
418     if (!HdfSbufWriteInt32(data, resetStatus)) {
419         HDF_LOGE("%s sbuf write failed", __func__);
420         HdfSbufRecycle(data);
421         return HDF_FAILURE;
422     }
423     if (!HdfSbufWriteUint8(data, chipId)) {
424         HDF_LOGE("%s sbuf write failed", __func__);
425         HdfSbufRecycle(data);
426         return HDF_FAILURE;
427     }
428     ret = HdfWlanSendBroadcastEvent(WIFI_WPA_EVENT_RESET_DRIVER, data);
429     HdfSbufRecycle(data);
430     return ret;
431 }
432 
HdfWifiEventRemainOnChannel(const struct NetDevice * netDev,uint32_t freq,uint32_t duration)433 int32_t HdfWifiEventRemainOnChannel(const struct NetDevice *netDev, uint32_t freq, uint32_t duration)
434 {
435     struct HdfSBuf *data = NULL;
436     int32_t ret;
437 
438     data = HdfSbufObtainDefaultSize();
439     if (data == NULL) {
440         HDF_LOGE("%s InitDataBlock failed", __func__);
441         return HDF_FAILURE;
442     }
443     if (!HdfSbufWriteString(data, netDev->name) || !HdfSbufWriteUint32(data, freq)) {
444         HDF_LOGE("%s sbuf write failed", __func__);
445         HdfSbufRecycle(data);
446         return HDF_FAILURE;
447     }
448     if (!HdfSbufWriteUint32(data, duration)) {
449         HDF_LOGE("%s sbuf write failed", __func__);
450         HdfSbufRecycle(data);
451         return HDF_FAILURE;
452     }
453     ret = HdfWlanSendBroadcastEvent(WIFI_WPA_EVENT_REMAIN_ON_CHANNEL, data);
454     HdfSbufRecycle(data);
455     return ret;
456 }
457 
HdfWifiEventCancelRemainOnChannel(const struct NetDevice * netDev,uint32_t freq)458 int32_t HdfWifiEventCancelRemainOnChannel(const struct NetDevice *netDev, uint32_t freq)
459 {
460     struct HdfSBuf *data = NULL;
461     int32_t ret;
462 
463     data = HdfSbufObtainDefaultSize();
464     if (data == NULL) {
465         HDF_LOGE("%s InitDataBlock failed", __func__);
466         return HDF_FAILURE;
467     }
468     if (!HdfSbufWriteString(data, netDev->name) || !HdfSbufWriteUint32(data, freq)) {
469         HDF_LOGE("%s sbuf write failed", __func__);
470         HdfSbufRecycle(data);
471         return HDF_FAILURE;
472     }
473     ret = HdfWlanSendBroadcastEvent(WIFI_WPA_EVENT_CANCEL_REMAIN_ON_CHANNEL, data);
474     HdfSbufRecycle(data);
475     return ret;
476 }
477 
478 #ifdef __cplusplus
479 #if __cplusplus
480 }
481 #endif
482 #endif
483