1 /*
2 * Copyright (c) 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 <lwip/netif.h>
34 #include <lwip/pbuf.h>
35 #include <lwip/opt.h>
36 #include <lwip/netifapi.h>
37 #include "net_device.h"
38 #include "osal_mem.h"
39 #include "securec.h"
40
41 #define HDF_LOG_TAG NetDeviceLite
42 #define FREE_SPACE_SIZE 2
43
SetNetIfLinkStatus(struct netif * lwipNf,NetIfLinkStatus status)44 static int32_t SetNetIfLinkStatus(struct netif *lwipNf, NetIfLinkStatus status)
45 {
46 if (status == NETIF_LINK_DOWN) {
47 netif_set_link_down(lwipNf);
48 } else if (status == NETIF_LINK_UP) {
49 netif_set_link_up(lwipNf);
50 } else {
51 HDF_LOGE("%s fail: status error!", __func__);
52 return HDF_ERR_INVALID_PARAM;
53 }
54 return HDF_SUCCESS;
55 }
56
LiteNetDevInit(struct NetDeviceImpl * netDeviceImpl)57 static int32_t LiteNetDevInit(struct NetDeviceImpl *netDeviceImpl)
58 {
59 struct NetDeviceAdapterLite *liteNdPri = NULL;
60 if (netDeviceImpl == NULL) {
61 HDF_LOGE("%s fail : netdeviceImpl NULL!", __func__);
62 return HDF_ERR_INVALID_PARAM;
63 }
64 liteNdPri = (struct NetDeviceAdapterLite *)OsalMemCalloc(sizeof(struct NetDeviceAdapterLite));
65 if (liteNdPri == NULL) {
66 HDF_LOGE("%s fail : malloc fail!", __func__);
67 return HDF_ERR_MALLOC_FAIL;
68 }
69 (void)memset_s(liteNdPri, sizeof(struct NetDeviceAdapterLite), 0, sizeof(struct NetDeviceAdapterLite));
70 netDeviceImpl->osPrivate = (void *)liteNdPri;
71 HDF_LOGI("%s Success!", __func__);
72 return HDF_SUCCESS;
73 }
LiteNetDevDeInit(struct NetDeviceImpl * netDeviceImpl)74 static int32_t LiteNetDevDeInit(struct NetDeviceImpl *netDeviceImpl)
75 {
76 if (netDeviceImpl == NULL) {
77 HDF_LOGE("netdevice lite deinit already free.");
78 return HDF_SUCCESS;
79 }
80 if (netDeviceImpl->osPrivate != NULL) {
81 OsalMemFree(netDeviceImpl->osPrivate);
82 netDeviceImpl->osPrivate = NULL;
83 }
84 HDF_LOGI("net device lite deinit success!");
85 return HDF_SUCCESS;
86 }
ConverPbuffToNetBuf(const NetDevice * netDev,struct pbuf * lwipBuf)87 static struct NetBuf *ConverPbuffToNetBuf(const NetDevice *netDev, struct pbuf *lwipBuf)
88 {
89 if (netDev == NULL || lwipBuf == NULL) {
90 HDF_LOGE("Convert tot net buff fail : netdev = NULL or lwipBuf = NULL!");
91 return NULL;
92 }
93 return Pbuf2NetBuf(netDev, lwipBuf);
94 }
95
ConverNetBufToPBuf(const struct NetBuf * netBuff)96 static struct pbuf *ConverNetBufToPBuf(const struct NetBuf *netBuff)
97 {
98 if (netBuff == NULL) {
99 HDF_LOGE("Conver to pbufff fail : netBuff = NULL!");
100 return NULL;
101 }
102 return NetBuf2Pbuf(netBuff);
103 }
104
LwipSendCheck(struct netif * netif,struct pbuf * lwipBuf)105 static int32_t LwipSendCheck(struct netif *netif, struct pbuf *lwipBuf)
106 {
107 if (netif == NULL || lwipBuf == NULL) {
108 HDF_LOGE("%s : netif = NUll or lwipBuf = NULL!", __func__);
109 return HDF_ERR_INVALID_PARAM;
110 }
111 struct NetDeviceImpl *ndImpl = (struct NetDeviceImpl *)netif->state;
112 if (ndImpl == NULL || ndImpl->netDevice == NULL) {
113 HDF_LOGE("%s fail : state = NUll or netdevice = NULL!", __func__);
114 return HDF_ERR_INVALID_PARAM;
115 }
116 return HDF_SUCCESS;
117 }
118
LwipSend(struct netif * netif,struct pbuf * lwipBuf)119 void LwipSend(struct netif *netif, struct pbuf *lwipBuf)
120 {
121 if (LwipSendCheck(netif, lwipBuf) != HDF_SUCCESS) {
122 return;
123 }
124 struct NetDeviceImpl *ndImpl = (struct NetDeviceImpl *)netif->state;
125 struct NetDevice *netDev = ndImpl->netDevice;
126 struct NetDeviceInterFace *netDevIf = NULL;
127
128 struct NetBuf *netBuff = ConverPbuffToNetBuf(netDev, lwipBuf);
129 if (netBuff == NULL) {
130 return;
131 }
132 netDevIf = netDev->netDeviceIf;
133 if (netDevIf != NULL && netDevIf->xmit != NULL) {
134 netDevIf->xmit(netDev, netBuff);
135 } else {
136 HDF_LOGE("%s fail : netdevIf = null or xmit = null!", __func__);
137 NetBufFree(netBuff);
138 }
139 return;
140 }
141
LwipSetHwaddr(struct netif * netif,uint8_t * addr,uint8_t len)142 uint8_t LwipSetHwaddr(struct netif *netif, uint8_t *addr, uint8_t len)
143 {
144 if (netif == NULL || addr == NULL) {
145 HDF_LOGE("%s fail : netif = null or addr = null!", __func__);
146 return (uint8_t)COMMON_ERROR;
147 }
148 if (len != MAC_ADDR_SIZE) {
149 HDF_LOGE("%s fail : len not right", __func__);
150 return (uint8_t)COMMON_ERROR;
151 }
152 struct NetDeviceImpl *ndImpl = (struct NetDeviceImpl *)netif->state;
153 struct NetDevice *netDev = NULL;
154 struct NetDeviceInterFace *netDevIf = NULL;
155 if (ndImpl == NULL || ndImpl->netDevice == NULL) {
156 HDF_LOGE("%s fail : state = null or netdevice = null!", __func__);
157 return (uint8_t)COMMON_ERROR;
158 }
159 netDev = ndImpl->netDevice;
160 netDevIf = netDev->netDeviceIf;
161 if (netDevIf == NULL || netDevIf->setMacAddr == NULL) {
162 HDF_LOGE("%s fail : netdevice if = null or setMacAddr = null!", __func__);
163 return (uint8_t)COMMON_ERROR;
164 }
165 return netDevIf->setMacAddr(netDev, addr);
166 }
167
LwipDrvConfig(struct netif * netif,uint32_t conFlags,uint8_t bit)168 void LwipDrvConfig(struct netif *netif, uint32_t conFlags, uint8_t bit)
169 {
170 (void)netif;
171 (void)conFlags;
172 (void)bit;
173 return;
174 }
175
CreateLwipNetIf(const struct NetDeviceImpl * netDeviceImpl,const struct NetDevice * netDev)176 static struct netif *CreateLwipNetIf(const struct NetDeviceImpl *netDeviceImpl, const struct NetDevice *netDev)
177 {
178 if (netDev == NULL) {
179 HDF_LOGE("%s : netDev = null!", __func__);
180 return NULL;
181 }
182 struct netif *lwipNf = NULL;
183 lwipNf = (struct netif *)OsalMemCalloc(sizeof(struct netif));
184 if (lwipNf == NULL) {
185 HDF_LOGE("%s fail : netif malloc fail!", __func__);
186 return NULL;
187 }
188 (void)memset_s(lwipNf, sizeof(struct netif), 0, sizeof(struct netif));
189
190 /* register netif to lwip */
191 lwipNf->state = (void *)netDeviceImpl;
192 lwipNf->drv_send = LwipSend;
193 lwipNf->drv_set_hwaddr = LwipSetHwaddr;
194 lwipNf->link_layer_type = netDev->LinkLayerType;
195 lwipNf->hwaddr_len = MAC_ADDR_SIZE;
196 lwipNf->drv_config = LwipDrvConfig;
197 #if LOSCFG_NET_LWIP_SACK_2_0
198 if (strncpy_s(lwipNf->name, IFNAMSIZ, netDev->name, IFNAMSIZ - FREE_SPACE_SIZE) != EOK) {
199 HDF_LOGE("lite netif add fail : strncpy_s fail!");
200 OsalMemFree(lwipNf);
201 return NULL;
202 }
203 lwipNf->name[IFNAMSIZ - FREE_SPACE_SIZE] = '\0';
204 #endif
205 return lwipNf;
206 }
207
DestroyLwipNetIf(struct netif * lwipNf)208 static void DestroyLwipNetIf(struct netif *lwipNf)
209 {
210 if (lwipNf != NULL) {
211 OsalMemFree(lwipNf);
212 }
213 }
214
IpV6SpecialProc(struct NetDevice * lwipNd,struct netif * lwipNf)215 static void IpV6SpecialProc(struct NetDevice *lwipNd, struct netif *lwipNf)
216 {
217 Protocol80211IfType ifType = lwipNd->funType.wlanType;
218
219 if (lwipNd->LinkLayerType != WIFI_LINK) {
220 return;
221 }
222
223 if ((ifType == PROTOCOL_80211_IFTYPE_AP) || (ifType == PROTOCOL_80211_IFTYPE_MESH_POINT)) {
224 #ifdef _PRE_WLAN_FEATURE_LWIP_IPV6_AUTOCONFIG
225 (hi_void)netifapi_set_ip6_autoconfig_disabled(lwipNf);
226 (hi_void)netifapi_set_accept_ra(lwipNf, false);
227 (hi_void)netifapi_set_ipv6_forwarding(lwipNf, true);
228 (hi_void)netifapi_set_ra_enable(lwipNf, true);
229 #endif
230 }
231 return;
232 }
233
LiteNetifLinkChangeCallback(struct netif * netif)234 static void LiteNetifLinkChangeCallback(struct netif *netif)
235 {
236 if (netif == NULL || netif->state == NULL) {
237 return;
238 }
239 struct NetDeviceImpl *netDeviceImpl = (struct NetDeviceImpl *)netif->state;
240 if (netDeviceImpl->netDevice != NULL && netDeviceImpl->netDevice->netDeviceIf != NULL &&
241 netDeviceImpl->netDevice->netDeviceIf->linkStatusChanged != NULL) {
242 netDeviceImpl->netDevice->netDeviceIf->linkStatusChanged(netDeviceImpl->netDevice);
243 }
244 }
245
LiteNetDevAdd(struct NetDeviceImpl * netDeviceImpl)246 static int32_t LiteNetDevAdd(struct NetDeviceImpl *netDeviceImpl)
247 {
248 if (netDeviceImpl == NULL || netDeviceImpl->osPrivate == NULL || netDeviceImpl->netDevice == NULL) {
249 HDF_LOGE("Lite netif add fail : impl = null , osPrivate = null, or netdevice = null!");
250 return HDF_ERR_INVALID_PARAM;
251 }
252
253 struct NetDeviceAdapterLite *liteNdPri = (struct NetDeviceAdapterLite *)netDeviceImpl->osPrivate;
254 struct NetDevice *lwipNd = netDeviceImpl->netDevice;
255 struct netif *lwipNf = NULL;
256 ip4_addr_t gw, ipaddr, netmask;
257 IP4_ADDR(&gw, 0, 0, 0, 0);
258 IP4_ADDR(&ipaddr, 0, 0, 0, 0);
259 IP4_ADDR(&netmask, 0, 0, 0, 0);
260 int ret = 0;
261 lwipNf = CreateLwipNetIf(netDeviceImpl, lwipNd);
262 if (lwipNf == NULL) {
263 HDF_LOGE("%s fail : CreateLwipNetIf fail!", __func__);
264 return HDF_FAILURE;
265 }
266 if ((ret = netifapi_netif_add(lwipNf, &ipaddr, &netmask, &gw, (void *)liteNdPri,
267 driverif_input, tcpip_input)) != ERR_OK) {
268 HDF_LOGE("%s : netifapi_netif_add fail!,ret=%d", __func__, ret);
269 DestroyLwipNetIf(lwipNf);
270 return HDF_FAILURE;
271 }
272 HDF_LOGI("netifapi_netif_add success!");
273 #if LOSCFG_NET_LWIP_SACK_2_0
274 /* update netdevice name - wipNf->name + lwipNf->num */
275 int32_t num = snprintf_s(lwipNd->name, IFNAMSIZ, (IFNAMSIZ - 1), "%s%" U16_F, lwipNf->name, lwipNf->num);
276 if (num < 0) {
277 HDF_LOGI("Lite netif add fail : snprintf_s fail!");
278 netifapi_netif_remove(lwipNf);
279 DestroyLwipNetIf(lwipNf);
280 return HDF_FAILURE;
281 }
282 #endif
283 /* copy MAC ADDR TO LWIP */
284 if (memcpy_s(lwipNf->hwaddr, NETIF_MAX_HWADDR_LEN, lwipNd->macAddr, MAC_ADDR_SIZE) != EOK) {
285 HDF_LOGI("%s fail : memcpy_s fail!", __func__);
286 netifapi_netif_remove(lwipNf);
287 DestroyLwipNetIf(lwipNf);
288 return HDF_FAILURE;
289 }
290
291 liteNdPri->lwipNetif = lwipNf;
292 IpV6SpecialProc(lwipNd, lwipNf);
293 /* set netif default status */
294 netifapi_netif_set_default(lwipNf);
295 netif_set_link_callback(lwipNf, LiteNetifLinkChangeCallback);
296 HDF_LOGI("%s success!!", __func__);
297 return HDF_SUCCESS;
298 }
299
GetNetIfFromDevImpl(const struct NetDeviceImpl * netDeviceImpl)300 static struct netif *GetNetIfFromDevImpl(const struct NetDeviceImpl *netDeviceImpl)
301 {
302 if (netDeviceImpl == NULL || netDeviceImpl->osPrivate == NULL) {
303 HDF_LOGE("%s fail : impl = null or osPrivate = null!", __func__);
304 return NULL;
305 }
306 struct NetDeviceAdapterLite *liteNdPri = (struct NetDeviceAdapterLite *)netDeviceImpl->osPrivate;
307 return liteNdPri->lwipNetif;
308 }
309
LiteNetDevDelete(struct NetDeviceImpl * netDeviceImpl)310 static int32_t LiteNetDevDelete(struct NetDeviceImpl *netDeviceImpl)
311 {
312 if (netDeviceImpl == NULL || netDeviceImpl->osPrivate == NULL) {
313 HDF_LOGE("%s fail : impl = null or osPrivate = null!", __func__);
314 return HDF_ERR_INVALID_PARAM;
315 }
316 struct NetDeviceAdapterLite *liteNdPri = (struct NetDeviceAdapterLite *)netDeviceImpl->osPrivate;
317 struct netif *lwipNf = liteNdPri->lwipNetif;
318 if (lwipNf != NULL) {
319 netifapi_netif_remove(lwipNf);
320 OsalMemFree(lwipNf);
321 liteNdPri->lwipNetif = NULL;
322 }
323 HDF_LOGI("%s success!", __func__);
324 return HDF_SUCCESS;
325 }
326
LiteNetDevSetStatus(struct NetDeviceImpl * netDeviceImpl,NetIfStatus status)327 static int32_t LiteNetDevSetStatus(struct NetDeviceImpl *netDeviceImpl, NetIfStatus status)
328 {
329 struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
330 if (lwipNf == NULL) {
331 HDF_LOGE("%s fail : netif = null!", __func__);
332 return HDF_ERR_INVALID_PARAM;
333 }
334 int32_t ret = 0;
335 if (status == NETIF_DOWN) {
336 ret = netifapi_netif_set_down(lwipNf);
337 } else if (status == NETIF_UP) {
338 ret = netifapi_netif_set_up(lwipNf);
339 } else {
340 HDF_LOGE("%s fail : status error!", __func__);
341 return HDF_ERR_INVALID_PARAM;
342 }
343 if (ret == ERR_OK) {
344 return HDF_SUCCESS;
345 }
346 HDF_LOGE("%s fail ret = %d!", __func__, ret);
347 return HDF_FAILURE;
348 }
349
LiteNetDevSetLinkStatus(struct NetDeviceImpl * netDeviceImpl,NetIfLinkStatus status)350 static int32_t LiteNetDevSetLinkStatus(struct NetDeviceImpl *netDeviceImpl, NetIfLinkStatus status)
351 {
352 struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
353 if (lwipNf == NULL) {
354 HDF_LOGE("%s fail : netif = null!", __func__);
355 return HDF_ERR_INVALID_PARAM;
356 }
357 return SetNetIfLinkStatus(lwipNf, status);
358 }
359
LiteNetDevGetLinkStatus(struct NetDeviceImpl * netDeviceImpl,NetIfLinkStatus * status)360 int32_t LiteNetDevGetLinkStatus(struct NetDeviceImpl *netDeviceImpl, NetIfLinkStatus *status)
361 {
362 struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
363 if (lwipNf == NULL) {
364 HDF_LOGE("%s fail : netif = null!", __func__);
365 return HDF_ERR_INVALID_PARAM;
366 }
367 if (status == NULL) {
368 HDF_LOGE("%s fail : status is null", __func__);
369 return HDF_ERR_INVALID_PARAM;
370 }
371 if (netif_is_link_up(lwipNf)) {
372 *status = NETIF_LINK_UP;
373 } else {
374 *status = NETIF_LINK_DOWN;
375 }
376 return HDF_SUCCESS;
377 }
378
LiteNetDevDataFilter(struct NetDeviceImpl * netDeviceImpl,struct NetBuf * buff)379 static ProcessingResult LiteNetDevDataFilter(struct NetDeviceImpl *netDeviceImpl, struct NetBuf *buff)
380 {
381 struct NetDevice *lwipNd = NULL;
382
383 if (netDeviceImpl == NULL || buff == NULL) {
384 HDF_LOGE("%s fail : buff = null!", __func__);
385 return PROCESSING_ERROR;
386 }
387 lwipNd = netDeviceImpl->netDevice;
388
389 struct EtherHeader *header = (struct EtherHeader *)NetBufGetAddress(buff, E_DATA_BUF);
390 uint16_t etherType = ntohs(header->etherType);
391 if ((lwipNd->LinkLayerType == ETHERNET_LINK) || (etherType == ETHER_TYPE_IP) || (etherType == ETHER_TYPE_ARP) ||
392 (etherType == ETHER_TYPE_RARP) || (etherType == ETHER_TYPE_IPV6) || (etherType == ETHER_TYPE_6LO) ||
393 (etherType == ETHER_TYPE_PAE)) {
394 return PROCESSING_CONTINUE;
395 } else {
396 NetBufFree(buff);
397 return PROCESSING_COMPLETE;
398 }
399 }
400
LiteNetDevDataReceive(struct NetDeviceImpl * netDeviceImpl,struct NetBuf * buff)401 static int32_t LiteNetDevDataReceive(struct NetDeviceImpl *netDeviceImpl, struct NetBuf *buff)
402 {
403 struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
404 if (lwipNf == NULL) {
405 NetBufFree(buff);
406 HDF_LOGE("%s fail : lwipnf = null!", __func__);
407 return HDF_ERR_INVALID_PARAM;
408 }
409 struct pbuf *pBuff = ConverNetBufToPBuf(buff);
410 if (pBuff == NULL) {
411 NetBufFree(buff);
412 HDF_LOGE("%s fail : pBuff = null!", __func__);
413 return HDF_FAILURE;
414 }
415 driverif_input(lwipNf, pBuff);
416 NetBufFree(buff);
417 return HDF_SUCCESS;
418 }
419
LiteNetDevReceive(struct NetDeviceImpl * netDeviceImpl,struct NetBuf * buff,ReceiveFlag flag)420 static int32_t LiteNetDevReceive(struct NetDeviceImpl *netDeviceImpl, struct NetBuf *buff, ReceiveFlag flag)
421 {
422 if (flag >= MAX_RECEIVE_FLAG) {
423 HDF_LOGE("%s fail : flag = %d.", __func__, flag);
424 return HDF_ERR_INVALID_PARAM;
425 }
426 struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
427 if (lwipNf == NULL || buff == NULL) {
428 HDF_LOGE("%s fail : lwipnf = null or buff = null!", __func__);
429 return HDF_ERR_INVALID_PARAM;
430 }
431 ProcessingResult ret = LiteNetDevDataFilter(netDeviceImpl, buff);
432 if (ret == PROCESSING_CONTINUE) {
433 return LiteNetDevDataReceive(netDeviceImpl, buff);
434 } else if (ret == PROCESSING_COMPLETE) {
435 return HDF_SUCCESS;
436 } else {
437 return HDF_FAILURE;
438 }
439 }
440
LiteNetSetIpAddr(struct NetDeviceImpl * netDeviceImpl,const IpV4Addr * ipAddr,const IpV4Addr * netMask,const IpV4Addr * gw)441 int32_t LiteNetSetIpAddr(struct NetDeviceImpl *netDeviceImpl, const IpV4Addr *ipAddr, const IpV4Addr *netMask,
442 const IpV4Addr *gw)
443 {
444 struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
445 if (lwipNf == NULL) {
446 HDF_LOGE("%s fail : lwipNf = null!", __func__);
447 return HDF_ERR_INVALID_PARAM;
448 }
449 netifapi_netif_set_addr(lwipNf, (ip4_addr_t *)ipAddr, (ip4_addr_t *)netMask, (ip4_addr_t *)gw);
450 return HDF_SUCCESS;
451 }
452
LiteNetDhcpsStart(struct NetDeviceImpl * netDeviceImpl,char * ip,uint16_t ipNum)453 int32_t LiteNetDhcpsStart(struct NetDeviceImpl *netDeviceImpl, char *ip, uint16_t ipNum)
454 {
455 struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
456 if (lwipNf == NULL) {
457 HDF_LOGE("%s fail : lwipNf = null!", __func__);
458 return HDF_ERR_INVALID_PARAM;
459 }
460 int32_t ret = 0;
461 if ((ret = netifapi_dhcps_start(lwipNf, ip, ipNum)) == ERR_OK) {
462 HDF_LOGI("%s success!", __func__);
463 return HDF_SUCCESS;
464 }
465 HDF_LOGE("%s fail, ret = %d!", __func__, ret);
466 return HDF_FAILURE;
467 }
468
LiteNetDhcpsStop(struct NetDeviceImpl * netDeviceImpl)469 int32_t LiteNetDhcpsStop(struct NetDeviceImpl *netDeviceImpl)
470 {
471 struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
472 if (lwipNf == NULL) {
473 HDF_LOGE("%s fail : lwipNf = null!", __func__);
474 return HDF_ERR_INVALID_PARAM;
475 }
476 int32_t ret = 0;
477 if (ERR_OK == (ret = netifapi_dhcps_stop(lwipNf))) {
478 HDF_LOGI("%s success!", __func__);
479 return HDF_SUCCESS;
480 }
481 HDF_LOGE("%s fail, ret = %d!", __func__, ret);
482 return HDF_FAILURE;
483 }
484
LiteNetDhcpStart(struct NetDeviceImpl * netDeviceImpl)485 int32_t LiteNetDhcpStart(struct NetDeviceImpl *netDeviceImpl)
486 {
487 struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
488 if (lwipNf == NULL) {
489 HDF_LOGE("%s fail : lwipNf = null!", __func__);
490 return HDF_ERR_INVALID_PARAM;
491 }
492 int32_t ret = 0;
493 if ((ret = netifapi_dhcp_start(lwipNf)) == ERR_OK) {
494 HDF_LOGI("%s success!", __func__);
495 return HDF_SUCCESS;
496 }
497 HDF_LOGE("%s fail, ret = %d!", __func__, ret);
498 return HDF_FAILURE;
499 }
500
LiteNetDhcpStop(struct NetDeviceImpl * netDeviceImpl)501 int32_t LiteNetDhcpStop(struct NetDeviceImpl *netDeviceImpl)
502 {
503 struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
504 if (lwipNf == NULL) {
505 HDF_LOGE("%s fail : lwipNf = null!", __func__);
506 return HDF_ERR_INVALID_PARAM;
507 }
508 int32_t ret = 0;
509 if ((ret = netifapi_dhcp_stop(lwipNf)) == ERR_OK) {
510 dhcp_cleanup(lwipNf);
511 HDF_LOGI("%s success!", __func__);
512 return HDF_SUCCESS;
513 }
514 HDF_LOGE("%s fail, ret = %d!", __func__, ret);
515 return HDF_FAILURE;
516 }
517
LiteNetDhcpIsBound(struct NetDeviceImpl * netDeviceImpl)518 int32_t LiteNetDhcpIsBound(struct NetDeviceImpl *netDeviceImpl)
519 {
520 struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
521 if (lwipNf == NULL) {
522 HDF_LOGE("%s fail : lwipNf = null!", __func__);
523 return HDF_ERR_INVALID_PARAM;
524 }
525
526 if (dhcp_is_bound(lwipNf) == ERR_OK) {
527 HDF_LOGI("%s success!", __func__);
528 return HDF_SUCCESS;
529 }
530 return HDF_FAILURE;
531 }
532
LiteNetChangeMacAddr(struct NetDeviceImpl * netDeviceImpl)533 int32_t LiteNetChangeMacAddr(struct NetDeviceImpl *netDeviceImpl)
534 {
535 struct NetDevice *lwipNd = NULL;
536 struct netif *lwipNf = GetNetIfFromDevImpl(netDeviceImpl);
537 if (lwipNf == NULL) {
538 HDF_LOGE("%s fail : lwipNf = null!", __func__);
539 return HDF_ERR_INVALID_PARAM;
540 }
541 lwipNd = netDeviceImpl->netDevice;
542 if (memcpy_s(lwipNf->hwaddr, NETIF_MAX_HWADDR_LEN, lwipNd->macAddr, MAC_ADDR_SIZE) != EOK) {
543 HDF_LOGE("%s fail : memcpy_s fail!", __func__);
544 return HDF_FAILURE;
545 }
546 return HDF_SUCCESS;
547 }
548
549 static struct NetDeviceImplOp g_liteNdImplOps = {
550 .init = LiteNetDevInit,
551 .deInit = LiteNetDevDeInit,
552 .add = LiteNetDevAdd,
553 .delete = LiteNetDevDelete,
554 .setStatus = LiteNetDevSetStatus,
555 .setLinkStatus = LiteNetDevSetLinkStatus,
556 .getLinkStatus = LiteNetDevGetLinkStatus,
557 .receive = LiteNetDevReceive,
558 .setIpAddr = LiteNetSetIpAddr,
559 .dhcpsStart = LiteNetDhcpsStart,
560 .dhcpsStop = LiteNetDhcpsStop,
561 .dhcpStart = LiteNetDhcpStart,
562 .dhcpStop = LiteNetDhcpStop,
563 .dhcpIsBound = LiteNetDhcpIsBound,
564 .changeMacAddr = LiteNetChangeMacAddr,
565 };
566
RegisterNetDeviceImpl(struct NetDeviceImpl * ndImpl)567 int32_t RegisterNetDeviceImpl(struct NetDeviceImpl *ndImpl)
568 {
569 if (ndImpl == NULL) {
570 HDF_LOGE("%s fail : impl = null!", __func__);
571 return HDF_ERR_INVALID_PARAM;
572 }
573
574 ndImpl->interFace = &g_liteNdImplOps;
575 HDF_LOGI("register lite netdevice impl success.");
576 return HDF_SUCCESS;
577 }
UnRegisterNetDeviceImpl(struct NetDeviceImpl * ndImpl)578 int32_t UnRegisterNetDeviceImpl(struct NetDeviceImpl *ndImpl)
579 {
580 if (ndImpl == NULL) {
581 HDF_LOGI("%s already unregister.", __func__);
582 return HDF_SUCCESS;
583 }
584
585 ndImpl->interFace = NULL;
586 HDF_LOGI("%s success.", __func__);
587 return HDF_SUCCESS;
588 }
589