• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  *    conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  *    of conditions and the following disclaimer in the documentation and/or other materials
12  *    provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15  *    to endorse or promote products derived from this software without specific prior written
16  *    permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "net_device_adapter.h"
32 
33 #include <los_task_pri.h>
34 #include <lwip/netif.h>
35 #include <lwip/pbuf.h>
36 #include <lwip/opt.h>
37 #include <lwip/netifapi.h>
38 #include "net_device.h"
39 #include "osal_mem.h"
40 #include "securec.h"
41 
42 #define HDF_LOG_TAG NetDeviceLite
43 #define FREE_SPACE_SIZE 2
44 #define P2P_HEAD 3
45 
IsInSystemTaskContext(void)46 static bool IsInSystemTaskContext(void)
47 {
48     LosTaskCB *runTask = (LosTaskCB *)OsCurrTaskGet();
49 
50     if (runTask == NULL) {
51         return false;
52     }
53     return (runTask->taskStatus & OS_TASK_FLAG_SYSTEM_TASK) != 0;
54 }
55 
SetNetIfLinkStatus(struct netif * lwipNf,NetIfLinkStatus status)56 static int32_t SetNetIfLinkStatus(struct netif *lwipNf, NetIfLinkStatus status)
57 {
58     int32_t ret = 0;
59     if (status == NETIF_LINK_DOWN) {
60         if (IsInSystemTaskContext()) {
61             netif_set_link_down(lwipNf);
62         } else {
63             ret = netifapi_netif_set_link_down(lwipNf);
64         }
65     } else if (status == NETIF_LINK_UP) {
66         if (IsInSystemTaskContext()) {
67             netif_set_link_up(lwipNf);
68         } else {
69             ret = netifapi_netif_set_link_up(lwipNf);
70         }
71     } else {
72         HDF_LOGE("%s fail: status error!", __func__);
73         return HDF_ERR_INVALID_PARAM;
74     }
75     if (ret == ERR_OK) {
76         return HDF_SUCCESS;
77     }
78     HDF_LOGE("%s fail ret = %d!", __func__, ret);
79     return HDF_FAILURE;
80 }
81 
LiteNetDevInit(struct NetDeviceImpl * netDeviceImpl)82 static int32_t LiteNetDevInit(struct NetDeviceImpl *netDeviceImpl)
83 {
84     struct NetDeviceAdapterLite *liteNdPri = NULL;
85     if (netDeviceImpl == NULL) {
86         HDF_LOGE("%s fail : netdeviceImpl NULL!", __func__);
87         return HDF_ERR_INVALID_PARAM;
88     }
89     liteNdPri = (struct NetDeviceAdapterLite *)OsalMemCalloc(sizeof(struct NetDeviceAdapterLite));
90     if (liteNdPri == NULL) {
91         HDF_LOGE("%s fail : malloc fail!", __func__);
92         return HDF_ERR_MALLOC_FAIL;
93     }
94     netDeviceImpl->osPrivate = (void *)liteNdPri;
95     HDF_LOGI("%s Success!", __func__);
96     return HDF_SUCCESS;
97 }
LiteNetDevDeInit(struct NetDeviceImpl * netDeviceImpl)98 static int32_t LiteNetDevDeInit(struct NetDeviceImpl *netDeviceImpl)
99 {
100     if (netDeviceImpl == NULL) {
101         HDF_LOGE("netdevice lite deinit already free.");
102         return HDF_SUCCESS;
103     }
104     if (netDeviceImpl->osPrivate != NULL) {
105         OsalMemFree(netDeviceImpl->osPrivate);
106         netDeviceImpl->osPrivate = NULL;
107     }
108     HDF_LOGI("net device lite deinit success!");
109     return HDF_SUCCESS;
110 }
ConverPbuffToNetBuf(const NetDevice * netDev,struct pbuf * lwipBuf)111 static struct NetBuf *ConverPbuffToNetBuf(const NetDevice *netDev, struct pbuf *lwipBuf)
112 {
113     if (netDev == NULL || lwipBuf == NULL) {
114         HDF_LOGE("Convert tot net buff fail : netdev = NULL or lwipBuf = NULL!");
115         return NULL;
116     }
117     return Pbuf2NetBuf(netDev, lwipBuf);
118 }
119 
ConverNetBufToPBuf(const struct NetBuf * netBuff)120 static struct pbuf *ConverNetBufToPBuf(const struct NetBuf *netBuff)
121 {
122     if (netBuff == NULL) {
123         HDF_LOGE("Conver to pbufff fail : netBuff = NULL!");
124         return NULL;
125     }
126     return NetBuf2Pbuf(netBuff);
127 }
128 
LwipSendCheck(struct netif * netif,struct pbuf * lwipBuf)129 static int32_t LwipSendCheck(struct netif *netif, struct pbuf *lwipBuf)
130 {
131     if (netif == NULL || lwipBuf == NULL) {
132         HDF_LOGE("%s : netif = NUll or lwipBuf = NULL!", __func__);
133         return HDF_ERR_INVALID_PARAM;
134     }
135     struct NetDeviceImpl *ndImpl = (struct NetDeviceImpl *)netif->state;
136     if (ndImpl == NULL || ndImpl->netDevice == NULL) {
137         HDF_LOGE("%s fail : state = NUll or netdevice = NULL!", __func__);
138         return HDF_ERR_INVALID_PARAM;
139     }
140     return HDF_SUCCESS;
141 }
142 
LwipSend(struct netif * netif,struct pbuf * lwipBuf)143 void LwipSend(struct netif *netif, struct pbuf *lwipBuf)
144 {
145     if (LwipSendCheck(netif, lwipBuf) != HDF_SUCCESS) {
146         return;
147     }
148     struct NetDeviceImpl *ndImpl = (struct NetDeviceImpl *)netif->state;
149     struct NetDevice *netDev = ndImpl->netDevice;
150     struct NetDeviceInterFace *netDevIf = NULL;
151 
152     struct NetBuf *netBuff = ConverPbuffToNetBuf(netDev, lwipBuf);
153     if (netBuff == NULL) {
154         return;
155     }
156     netDevIf = netDev->netDeviceIf;
157     if (netDevIf != NULL && netDevIf->xmit != NULL) {
158         netDevIf->xmit(netDev, netBuff);
159     } else {
160         HDF_LOGE("%s fail : netdevIf = null or xmit = null!", __func__);
161         NetBufFree(netBuff);
162     }
163     return;
164 }
165 
LwipSetHwaddr(struct netif * netif,uint8_t * addr,uint8_t len)166 uint8_t LwipSetHwaddr(struct netif *netif, uint8_t *addr, uint8_t len)
167 {
168     if (netif == NULL || addr == NULL) {
169         HDF_LOGE("%s fail : netif = null or addr = null!", __func__);
170         return (uint8_t)COMMON_ERROR;
171     }
172     if (len != MAC_ADDR_SIZE) {
173         HDF_LOGE("%s fail : len not right", __func__);
174         return (uint8_t)COMMON_ERROR;
175     }
176     struct NetDeviceImpl *ndImpl = (struct NetDeviceImpl *)netif->state;
177     struct NetDevice *netDev = NULL;
178     struct NetDeviceInterFace *netDevIf = NULL;
179     if (ndImpl == NULL || ndImpl->netDevice == NULL) {
180         HDF_LOGE("%s fail : state = null or netdevice = null!", __func__);
181         return (uint8_t)COMMON_ERROR;
182     }
183     netDev = ndImpl->netDevice;
184     netDevIf = netDev->netDeviceIf;
185     if (netDevIf == NULL || netDevIf->setMacAddr == NULL) {
186         HDF_LOGE("%s fail : netdevice if = null or setMacAddr = null!", __func__);
187         return (uint8_t)COMMON_ERROR;
188     }
189     return netDevIf->setMacAddr(netDev, addr);
190 }
191 
LwipDrvConfig(struct netif * netif,uint32_t conFlags,uint8_t bit)192 void LwipDrvConfig(struct netif *netif, uint32_t conFlags, uint8_t bit)
193 {
194     (void)netif;
195     (void)conFlags;
196     (void)bit;
197     return;
198 }
199 
CreateLwipNetIf(const struct NetDeviceImpl * netDeviceImpl,const struct NetDevice * netDev)200 static struct netif *CreateLwipNetIf(const struct NetDeviceImpl *netDeviceImpl, const struct NetDevice *netDev)
201 {
202     if (netDev == NULL) {
203         HDF_LOGE("%s : netDev = null!", __func__);
204         return NULL;
205     }
206     struct netif *lwipNf = NULL;
207     lwipNf = (struct netif *)OsalMemCalloc(sizeof(struct netif));
208     if (lwipNf == NULL) {
209         HDF_LOGE("%s fail : netif malloc fail!", __func__);
210         return NULL;
211     }
212 
213     /* register netif to lwip */
214     lwipNf->state = (void *)netDeviceImpl;
215     lwipNf->drv_send = LwipSend;
216     lwipNf->drv_set_hwaddr = LwipSetHwaddr;
217     lwipNf->link_layer_type = netDev->LinkLayerType;
218     lwipNf->hwaddr_len = MAC_ADDR_SIZE;
219     lwipNf->drv_config = LwipDrvConfig;
220 #if LOSCFG_NET_LWIP_SACK_2_0
221     if (strncpy_s(lwipNf->name, IFNAMSIZ, netDev->name, IFNAMSIZ - FREE_SPACE_SIZE) != EOK) {
222         HDF_LOGE("lite netif add fail : strncpy_s fail!");
223         OsalMemFree(lwipNf);
224         return NULL;
225     }
226     lwipNf->name[IFNAMSIZ - FREE_SPACE_SIZE] = '\0';
227 #endif
228     return lwipNf;
229 }
230 
DestroyLwipNetIf(struct netif * lwipNf)231 static void DestroyLwipNetIf(struct netif *lwipNf)
232 {
233     if (lwipNf != NULL) {
234         OsalMemFree(lwipNf);
235     }
236 }
237 
IpV6SpecialProc(struct NetDevice * lwipNd,struct netif * lwipNf)238 static void IpV6SpecialProc(struct NetDevice *lwipNd, struct netif *lwipNf)
239 {
240     Protocol80211IfType ifType = lwipNd->funType.wlanType;
241 
242     if (lwipNd->LinkLayerType != WIFI_LINK) {
243         return;
244     }
245 
246     if ((ifType == PROTOCOL_80211_IFTYPE_AP) || (ifType == PROTOCOL_80211_IFTYPE_MESH_POINT)) {
247 #ifdef _PRE_WLAN_FEATURE_LWIP_IPV6_AUTOCONFIG
248         (hi_void)netifapi_set_ip6_autoconfig_disabled(lwipNf);
249         (hi_void)netifapi_set_accept_ra(lwipNf, false);
250         (hi_void)netifapi_set_ipv6_forwarding(lwipNf, true);
251         (hi_void)netifapi_set_ra_enable(lwipNf, true);
252 #endif
253     }
254     return;
255 }
256 
LiteNetifLinkChangeCallback(struct netif * netif)257 static void LiteNetifLinkChangeCallback(struct netif *netif)
258 {
259     if (netif == NULL || netif->state == NULL) {
260         return;
261     }
262     struct NetDeviceImpl *netDeviceImpl = (struct NetDeviceImpl *)netif->state;
263     if (netDeviceImpl->netDevice != NULL && netDeviceImpl->netDevice->netDeviceIf != NULL &&
264         netDeviceImpl->netDevice->netDeviceIf->linkStatusChanged != NULL) {
265             netDeviceImpl->netDevice->netDeviceIf->linkStatusChanged(netDeviceImpl->netDevice);
266     }
267 }
268 
AddP2pNetDev(struct netif * lwipNf,struct NetDevice * lwipNd)269 static int32_t AddP2pNetDev(struct netif *lwipNf, struct NetDevice *lwipNd)
270 {
271     if (lwipNf == NULL || lwipNd == NULL) {
272         return HDF_FAILURE;
273     }
274     if  (strncmp(lwipNd->name, "p2p", P2P_HEAD) == 0) {
275         if (strncpy_s(lwipNf->full_name, IFNAMSIZ, lwipNd->name, IFNAMSIZ - FREE_SPACE_SIZE) != EOK) {
276             HDF_LOGE("lite netif add p2p netdevice fail : strncpy_s fail!");
277             return HDF_FAILURE;
278         }
279         lwipNf->full_name[IFNAMSIZ - FREE_SPACE_SIZE] = '\0';
280     }
281     return HDF_SUCCESS;
282 }
283 
LiteNetDevAdd(struct NetDeviceImpl * netDeviceImpl)284 static int32_t LiteNetDevAdd(struct NetDeviceImpl *netDeviceImpl)
285 {
286     if (netDeviceImpl == NULL || netDeviceImpl->osPrivate == NULL || netDeviceImpl->netDevice == NULL) {
287         HDF_LOGE("Lite netif add fail : impl = null , osPrivate = null, or netdevice = null!");
288         return HDF_ERR_INVALID_PARAM;
289     }
290 
291     struct NetDeviceAdapterLite *liteNdPri = (struct NetDeviceAdapterLite *)netDeviceImpl->osPrivate;
292     struct NetDevice *lwipNd = netDeviceImpl->netDevice;
293     struct netif *lwipNf = NULL;
294     ip4_addr_t gw, ipaddr, netmask;
295     IP4_ADDR(&gw, 0, 0, 0, 0);
296     IP4_ADDR(&ipaddr, 0, 0, 0, 0);
297     IP4_ADDR(&netmask, 0, 0, 0, 0);
298     int ret = 0;
299     lwipNf = CreateLwipNetIf(netDeviceImpl, lwipNd);
300     if (lwipNf == NULL) {
301         HDF_LOGE("%s fail : CreateLwipNetIf fail!", __func__);
302         return HDF_FAILURE;
303     }
304     if ((ret = netifapi_netif_add(lwipNf, &ipaddr, &netmask, &gw)) != ERR_OK) {
305         HDF_LOGE("%s : netifapi_netif_add fail!,ret=%d", __func__, ret);
306         DestroyLwipNetIf(lwipNf);
307         return HDF_FAILURE;
308     }
309     if (AddP2pNetDev(lwipNf, lwipNd) != HDF_SUCCESS) {
310         DestroyLwipNetIf(lwipNf);
311         return HDF_FAILURE;
312     }
313     HDF_LOGI("netifapi_netif_add success!");
314 #if LOSCFG_NET_LWIP_SACK_2_0
315     /* update netdevice name -  wipNf->name + lwipNf->num */
316     int32_t num = snprintf_s(lwipNd->name, IFNAMSIZ, (IFNAMSIZ - 1), "%s%" U16_F, lwipNf->name, lwipNf->num);
317     if (num < 0) {
318         HDF_LOGI("Lite netif add fail : snprintf_s fail!");
319         netifapi_netif_remove(lwipNf);
320         DestroyLwipNetIf(lwipNf);
321         return HDF_FAILURE;
322     }
323 #endif
324     /* copy MAC ADDR TO LWIP */
325     if (memcpy_s(lwipNf->hwaddr, NETIF_MAX_HWADDR_LEN, lwipNd->macAddr, MAC_ADDR_SIZE) != EOK) {
326         HDF_LOGI("%s fail : memcpy_s fail!", __func__);
327         netifapi_netif_remove(lwipNf);
328         DestroyLwipNetIf(lwipNf);
329         return HDF_FAILURE;
330     }
331 
332     liteNdPri->lwipNetif = lwipNf;
333     IpV6SpecialProc(lwipNd, lwipNf);
334     /* set netif default status */
335     netifapi_netif_set_default(lwipNf);
336     netif_set_link_callback(lwipNf, LiteNetifLinkChangeCallback);
337     HDF_LOGI("%s success!!", __func__);
338     return HDF_SUCCESS;
339 }
340 
GetNetIfFromDevImpl(const struct NetDeviceImpl * netDeviceImpl)341 static struct netif *GetNetIfFromDevImpl(const struct NetDeviceImpl *netDeviceImpl)
342 {
343     if (netDeviceImpl == NULL || netDeviceImpl->osPrivate == NULL) {
344         HDF_LOGE("%s fail : impl = null or osPrivate = null!", __func__);
345         return NULL;
346     }
347     struct NetDeviceAdapterLite *liteNdPri = (struct NetDeviceAdapterLite *)netDeviceImpl->osPrivate;
348     return liteNdPri->lwipNetif;
349 }
350 
LiteNetDevDelete(struct NetDeviceImpl * netDeviceImpl)351 static int32_t LiteNetDevDelete(struct NetDeviceImpl *netDeviceImpl)
352 {
353     if (netDeviceImpl == NULL || netDeviceImpl->osPrivate == NULL) {
354         HDF_LOGE("%s fail : impl = null or osPrivate = null!", __func__);
355         return HDF_ERR_INVALID_PARAM;
356     }
357     struct NetDeviceAdapterLite *liteNdPri = (struct NetDeviceAdapterLite *)netDeviceImpl->osPrivate;
358     struct netif *lwipNf = liteNdPri->lwipNetif;
359     if (lwipNf != NULL) {
360         netifapi_netif_remove(lwipNf);
361         OsalMemFree(lwipNf);
362         liteNdPri->lwipNetif = NULL;
363     }
364     HDF_LOGI("%s success!", __func__);
365     return HDF_SUCCESS;
366 }
367 
LiteNetDevSetStatus(struct NetDeviceImpl * netDeviceImpl,NetIfStatus status)368 static int32_t LiteNetDevSetStatus(struct NetDeviceImpl *netDeviceImpl, NetIfStatus status)
369 {
370     struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
371     if (lwipNf == NULL) {
372         HDF_LOGE("%s fail : netif = null!", __func__);
373         return HDF_ERR_INVALID_PARAM;
374     }
375     int32_t ret = 0;
376     if (status == NETIF_DOWN) {
377         ret = netifapi_netif_set_down(lwipNf);
378     } else if (status == NETIF_UP) {
379         ret = netifapi_netif_set_up(lwipNf);
380     } else {
381         HDF_LOGE("%s fail : status error!", __func__);
382         return HDF_ERR_INVALID_PARAM;
383     }
384     if (ret == ERR_OK) {
385         return HDF_SUCCESS;
386     }
387     HDF_LOGE("%s fail ret = %d!", __func__, ret);
388     return HDF_FAILURE;
389 }
390 
LiteNetDevSetLinkStatus(struct NetDeviceImpl * netDeviceImpl,NetIfLinkStatus status)391 static int32_t LiteNetDevSetLinkStatus(struct NetDeviceImpl *netDeviceImpl, NetIfLinkStatus status)
392 {
393     struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
394     if (lwipNf == NULL) {
395         HDF_LOGE("%s fail : netif = null!", __func__);
396         return HDF_ERR_INVALID_PARAM;
397     }
398 	return SetNetIfLinkStatus(lwipNf, status);
399 }
400 
LiteNetDevGetLinkStatus(struct NetDeviceImpl * netDeviceImpl,NetIfLinkStatus * status)401 int32_t LiteNetDevGetLinkStatus(struct NetDeviceImpl *netDeviceImpl, NetIfLinkStatus *status)
402 {
403     struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
404     if (lwipNf == NULL) {
405         HDF_LOGE("%s fail : netif = null!", __func__);
406         return HDF_ERR_INVALID_PARAM;
407     }
408     if (status == NULL) {
409         HDF_LOGE("%s fail : status is null", __func__);
410         return HDF_ERR_INVALID_PARAM;
411     }
412     if (netif_is_link_up(lwipNf)) {
413         *status = NETIF_LINK_UP;
414     } else {
415         *status = NETIF_LINK_DOWN;
416     }
417     return HDF_SUCCESS;
418 }
419 
LiteNetDevDataFilter(struct NetDeviceImpl * netDeviceImpl,struct NetBuf * buff)420 static ProcessingResult LiteNetDevDataFilter(struct NetDeviceImpl *netDeviceImpl, struct NetBuf *buff)
421 {
422     struct NetDevice *lwipNd = NULL;
423 
424     if (netDeviceImpl == NULL || buff == NULL) {
425         HDF_LOGE("%s fail : buff = null!", __func__);
426         return PROCESSING_ERROR;
427     }
428     lwipNd = netDeviceImpl->netDevice;
429 
430     struct EtherHeader *header = (struct EtherHeader *)NetBufGetAddress(buff, E_DATA_BUF);
431     uint16_t etherType = ntohs(header->etherType);
432     if ((lwipNd->LinkLayerType == ETHERNET_LINK) || (etherType == ETHER_TYPE_IP) || (etherType == ETHER_TYPE_ARP) ||
433         (etherType == ETHER_TYPE_RARP) || (etherType == ETHER_TYPE_IPV6) || (etherType == ETHER_TYPE_6LO) ||
434         (etherType == ETHER_TYPE_PAE)) {
435         return PROCESSING_CONTINUE;
436     } else {
437         NetBufFree(buff);
438         return PROCESSING_COMPLETE;
439     }
440 }
441 
LiteNetDevDataReceive(struct NetDeviceImpl * netDeviceImpl,struct NetBuf * buff)442 static int32_t LiteNetDevDataReceive(struct NetDeviceImpl *netDeviceImpl, struct NetBuf *buff)
443 {
444     struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
445     if (lwipNf == NULL) {
446         NetBufFree(buff);
447         HDF_LOGE("%s fail : lwipnf = null!", __func__);
448         return HDF_ERR_INVALID_PARAM;
449     }
450     struct pbuf *pBuff = ConverNetBufToPBuf(buff);
451     if (pBuff == NULL) {
452         NetBufFree(buff);
453         HDF_LOGE("%s fail : pBuff = null!", __func__);
454         return HDF_FAILURE;
455     }
456     driverif_input(lwipNf, pBuff);
457     NetBufFree(buff);
458     return HDF_SUCCESS;
459 }
460 
LiteNetDevReceive(struct NetDeviceImpl * netDeviceImpl,struct NetBuf * buff,ReceiveFlag flag)461 static int32_t LiteNetDevReceive(struct NetDeviceImpl *netDeviceImpl, struct NetBuf *buff, ReceiveFlag flag)
462 {
463     if (flag >= MAX_RECEIVE_FLAG) {
464         HDF_LOGE("%s fail : flag = %d.", __func__, flag);
465         return HDF_ERR_INVALID_PARAM;
466     }
467     struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
468     if (lwipNf == NULL || buff == NULL) {
469         HDF_LOGE("%s fail : lwipnf = null or buff = null!", __func__);
470         return HDF_ERR_INVALID_PARAM;
471     }
472     ProcessingResult ret = LiteNetDevDataFilter(netDeviceImpl, buff);
473     if (ret == PROCESSING_CONTINUE) {
474         return LiteNetDevDataReceive(netDeviceImpl, buff);
475     } else if (ret == PROCESSING_COMPLETE) {
476         return HDF_SUCCESS;
477     } else {
478         return HDF_FAILURE;
479     }
480 }
481 
LiteNetSetIpAddr(struct NetDeviceImpl * netDeviceImpl,const IpV4Addr * ipAddr,const IpV4Addr * netMask,const IpV4Addr * gw)482 int32_t LiteNetSetIpAddr(struct NetDeviceImpl *netDeviceImpl, const IpV4Addr *ipAddr, const IpV4Addr *netMask,
483     const IpV4Addr *gw)
484 {
485     struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
486     if (lwipNf == NULL) {
487         HDF_LOGE("%s fail : lwipNf = null!", __func__);
488         return HDF_ERR_INVALID_PARAM;
489     }
490     netifapi_netif_set_addr(lwipNf, (ip4_addr_t *)ipAddr, (ip4_addr_t *)netMask, (ip4_addr_t *)gw);
491     return HDF_SUCCESS;
492 }
493 
LiteNetDhcpsStart(struct NetDeviceImpl * netDeviceImpl,char * ip,uint16_t ipNum)494 int32_t LiteNetDhcpsStart(struct NetDeviceImpl *netDeviceImpl, char *ip, uint16_t ipNum)
495 {
496     struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
497     if (lwipNf == NULL) {
498         HDF_LOGE("%s fail : lwipNf = null!", __func__);
499         return HDF_ERR_INVALID_PARAM;
500     }
501     int32_t ret = 0;
502     if ((ret = netifapi_dhcps_start(lwipNf, ip, ipNum)) == ERR_OK) {
503         HDF_LOGI("%s success!", __func__);
504         return HDF_SUCCESS;
505     }
506     HDF_LOGE("%s fail, ret = %d!", __func__, ret);
507     return HDF_FAILURE;
508 }
509 
LiteNetDhcpsStop(struct NetDeviceImpl * netDeviceImpl)510 int32_t LiteNetDhcpsStop(struct NetDeviceImpl *netDeviceImpl)
511 {
512     struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
513     if (lwipNf == NULL) {
514         HDF_LOGE("%s fail : lwipNf = null!", __func__);
515         return HDF_ERR_INVALID_PARAM;
516     }
517     int32_t ret = 0;
518     if (ERR_OK == (ret = netifapi_dhcps_stop(lwipNf))) {
519         HDF_LOGI("%s success!", __func__);
520         return HDF_SUCCESS;
521     }
522     HDF_LOGE("%s fail, ret = %d!", __func__, ret);
523     return HDF_FAILURE;
524 }
525 
LiteNetDhcpStart(struct NetDeviceImpl * netDeviceImpl)526 int32_t LiteNetDhcpStart(struct NetDeviceImpl *netDeviceImpl)
527 {
528     struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
529     if (lwipNf == NULL) {
530         HDF_LOGE("%s fail : lwipNf = null!", __func__);
531         return HDF_ERR_INVALID_PARAM;
532     }
533     int32_t ret = 0;
534     if ((ret = netifapi_dhcp_start(lwipNf)) == ERR_OK) {
535         HDF_LOGI("%s success!", __func__);
536         return HDF_SUCCESS;
537     }
538     HDF_LOGE("%s fail, ret = %d!", __func__, ret);
539     return HDF_FAILURE;
540 }
541 
LiteNetDhcpStop(struct NetDeviceImpl * netDeviceImpl)542 int32_t LiteNetDhcpStop(struct NetDeviceImpl *netDeviceImpl)
543 {
544     struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
545     if (lwipNf == NULL) {
546         HDF_LOGE("%s fail : lwipNf = null!", __func__);
547         return HDF_ERR_INVALID_PARAM;
548     }
549     int32_t ret = 0;
550     if ((ret = netifapi_dhcp_stop(lwipNf)) == ERR_OK) {
551         dhcp_cleanup(lwipNf);
552         HDF_LOGI("%s success!", __func__);
553         return HDF_SUCCESS;
554     }
555     HDF_LOGE("%s fail, ret = %d!", __func__, ret);
556     return HDF_FAILURE;
557 }
558 
LiteNetDhcpIsBound(struct NetDeviceImpl * netDeviceImpl)559 int32_t LiteNetDhcpIsBound(struct NetDeviceImpl *netDeviceImpl)
560 {
561     struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
562     if (lwipNf == NULL) {
563         HDF_LOGE("%s fail : lwipNf = null!", __func__);
564         return HDF_ERR_INVALID_PARAM;
565     }
566 
567     if (dhcp_is_bound(lwipNf) == ERR_OK) {
568         HDF_LOGI("%s success!", __func__);
569         return HDF_SUCCESS;
570     }
571     return HDF_FAILURE;
572 }
573 
LiteNetChangeMacAddr(struct NetDeviceImpl * netDeviceImpl)574 int32_t LiteNetChangeMacAddr(struct NetDeviceImpl *netDeviceImpl)
575 {
576     struct NetDevice *lwipNd = NULL;
577     struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
578     if (lwipNf == NULL) {
579         HDF_LOGE("%s fail : lwipNf = null!", __func__);
580         return HDF_ERR_INVALID_PARAM;
581     }
582     lwipNd = netDeviceImpl->netDevice;
583     if (memcpy_s(lwipNf->hwaddr, NETIF_MAX_HWADDR_LEN, lwipNd->macAddr, MAC_ADDR_SIZE) != EOK) {
584         HDF_LOGE("%s fail : memcpy_s fail!", __func__);
585         return HDF_FAILURE;
586     }
587     return HDF_SUCCESS;
588 }
589 
590 static struct NetDeviceImplOp g_liteNdImplOps = {
591     .init = LiteNetDevInit,
592     .deInit = LiteNetDevDeInit,
593     .add = LiteNetDevAdd,
594     .delete = LiteNetDevDelete,
595     .setStatus = LiteNetDevSetStatus,
596     .setLinkStatus = LiteNetDevSetLinkStatus,
597     .getLinkStatus = LiteNetDevGetLinkStatus,
598     .receive = LiteNetDevReceive,
599     .setIpAddr = LiteNetSetIpAddr,
600     .dhcpsStart = LiteNetDhcpsStart,
601     .dhcpsStop = LiteNetDhcpsStop,
602     .dhcpStart = LiteNetDhcpStart,
603     .dhcpStop = LiteNetDhcpStop,
604     .dhcpIsBound = LiteNetDhcpIsBound,
605     .changeMacAddr = LiteNetChangeMacAddr,
606 };
607 
RegisterNetDeviceImpl(struct NetDeviceImpl * ndImpl)608 int32_t RegisterNetDeviceImpl(struct NetDeviceImpl *ndImpl)
609 {
610     if (ndImpl == NULL) {
611         HDF_LOGE("%s fail : impl = null!", __func__);
612         return HDF_ERR_INVALID_PARAM;
613     }
614 
615     ndImpl->interFace = &g_liteNdImplOps;
616     HDF_LOGI("register lite netdevice impl success.");
617     return HDF_SUCCESS;
618 }
UnRegisterNetDeviceImpl(struct NetDeviceImpl * ndImpl)619 int32_t UnRegisterNetDeviceImpl(struct NetDeviceImpl *ndImpl)
620 {
621     if (ndImpl == NULL) {
622         HDF_LOGI("%s already unregister.", __func__);
623         return HDF_SUCCESS;
624     }
625 
626     ndImpl->interFace = NULL;
627     HDF_LOGI("%s success.", __func__);
628     return HDF_SUCCESS;
629 }
630