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 "net_device_test.h"
10 #include "hdf_log.h"
11 #include "net_device.h"
12 #include <securec.h>
13
14 static struct NetDevice *g_netDevice = NULL;
15
16 static uint8_t g_filterData[] = {
17 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x88, 0x11, 0x31, 0xc6, 0xda, 0xdc, 0xff, 0xff
18 };
19
WiFiNetDeviceTestEnv(void)20 static bool WiFiNetDeviceTestEnv(void)
21 {
22 if (g_netDevice == NULL) {
23 char devName[IFNAMSIZ] = {0};
24 if (strncpy_s(devName, IFNAMSIZ, "wlan_test_0", strlen("wlan_test_0") + 1) != EOK) {
25 HDF_LOGE("%s: strcpy_s fail", __func__);
26 return false;
27 }
28 g_netDevice = NetDeviceInit(devName, strlen(devName), WIFI_LINK, LITE_OS);
29 if (g_netDevice == NULL) {
30 HDF_LOGE("%s fail ", __func__);
31 return false;
32 }
33 g_netDevice->funType.wlanType = PROTOCOL_80211_IFTYPE_STATION;
34 HDF_LOGE("%s success ", __func__);
35 return true;
36 }
37 return true;
38 }
39
WiFiNetDviceTestInit(void)40 int32_t WiFiNetDviceTestInit(void)
41 {
42 if (g_netDevice != NULL) {
43 return HDF_SUCCESS;
44 }
45 if (!WiFiNetDeviceTestEnv()) {
46 return HDF_FAILURE;
47 }
48 return HDF_SUCCESS;
49 }
50
WiFiNetDviceTestDeInit(void)51 int32_t WiFiNetDviceTestDeInit(void)
52 {
53 NetDeviceDeInit(g_netDevice);
54 g_netDevice = NULL;
55 return HDF_SUCCESS;
56 }
57
WiFiNetDviceTestAdd(void)58 int32_t WiFiNetDviceTestAdd(void)
59 {
60 if (g_netDevice == NULL) {
61 if (!WiFiNetDeviceTestEnv()) {
62 HDF_LOGE("%s WiFiNetDeviceTestEnv fail!", __func__);
63 return HDF_FAILURE;
64 }
65 }
66 if (NetDeviceAdd(g_netDevice) != HDF_SUCCESS) {
67 HDF_LOGE("%s add fail!", __func__);
68 return HDF_FAILURE;
69 }
70 return HDF_SUCCESS;
71 }
72
WiFiNetDviceTestDelete(void)73 int32_t WiFiNetDviceTestDelete(void)
74 {
75 if (NetDeviceDelete(g_netDevice) != HDF_SUCCESS) {
76 HDF_LOGE("%s NetDeviceDelete fail!", __func__);
77 return HDF_FAILURE;
78 }
79 return HDF_SUCCESS;
80 }
81
WiFiNetDviceTestGetDevice(void)82 int32_t WiFiNetDviceTestGetDevice(void)
83 {
84 struct NetDevice *temp = NetDeviceGetInstByName(g_netDevice->name);
85 if (temp == NULL) {
86 return HDF_FAILURE;
87 }
88 return HDF_SUCCESS;
89 }
90
WiFiNetDviceTestGetCount(void)91 int32_t WiFiNetDviceTestGetCount(void)
92 {
93 uint32_t count = NetDevGetRegisterCount();
94 if (count == 0) {
95 return HDF_FAILURE;
96 }
97 return HDF_SUCCESS;
98 }
99
WiFiNetDviceTestGetCap(void)100 int32_t WiFiNetDviceTestGetCap(void)
101 {
102 uint32_t count = NetDeviceGetCap();
103 if (count > 0) {
104 return HDF_SUCCESS;
105 }
106 return HDF_FAILURE;
107 }
108
WiFiNetDviceTestSetAddr(void)109 int32_t WiFiNetDviceTestSetAddr(void)
110 {
111 #ifdef __LITEOS__
112 IpV4Addr ip = { 0x010ca8c0UL }; // 192, 168, 12, 1
113 IpV4Addr netmask = { 0x00ffffffUL }; // 255, 255, 255, 0
114 IpV4Addr gw = { 0x010ca8c0UL }; // 192, 168, 12, 1
115 return NetIfSetAddr(g_netDevice, &ip, &netmask, &gw);
116 #else
117 return HDF_SUCCESS;
118 #endif
119 }
120
WiFiNetDviceTestRx(void)121 int32_t WiFiNetDviceTestRx(void)
122 {
123 NetBuf *buff = NULL;
124
125 int count = sizeof(g_filterData);
126 buff = NetBufAlloc(count);
127 if (buff == NULL) {
128 HDF_LOGE("%s fail : NetBufAlloc = null!", __func__);
129 return HDF_FAILURE;
130 }
131 NetBufPush(buff, E_DATA_BUF, count);
132 if (memcpy_s(NetBufGetAddress(buff, E_DATA_BUF), count, g_filterData, count) != EOK) {
133 NetBufFree(buff);
134 buff = NULL;
135 HDF_LOGE("%s fail : memcpy_s fail", __func__);
136 return HDF_FAILURE;
137 }
138 return NetIfRx(g_netDevice, buff);
139 }
140
WiFiNetDviceTestSetStatus(void)141 int32_t WiFiNetDviceTestSetStatus(void)
142 {
143 return NetIfSetStatus(g_netDevice, NETIF_DOWN);
144 }
145
WiFiNetDviceTestSetLinkStatus(void)146 int32_t WiFiNetDviceTestSetLinkStatus(void)
147 {
148 #ifdef __LITEOS__
149 return NetIfSetLinkStatus(g_netDevice, NETIF_LINK_DOWN);
150 #else
151 return HDF_SUCCESS;
152 #endif
153 }
154
WifiNetDeviceDhcpClient(void)155 int32_t WifiNetDeviceDhcpClient(void)
156 {
157 #ifdef __LITEOS__
158 int32_t expectedRet = HDF_SUCCESS;
159 #else
160 int32_t expectedRet = HDF_ERR_INVALID_PARAM;
161 #endif
162
163 NetIfSetStatus(g_netDevice, NETIF_UP);
164 NetIfDhcpStop(g_netDevice);
165 if (NetIfDhcpStart(g_netDevice) != expectedRet) {
166 return HDF_FAILURE;
167 }
168 if (NetIfDhcpStop(g_netDevice) != expectedRet) {
169 return HDF_FAILURE;
170 }
171 return HDF_SUCCESS;
172 }
173
WifiNetDeviceDhcpServer(void)174 int32_t WifiNetDeviceDhcpServer(void)
175 {
176 #ifdef __LITEOS__
177 int32_t expectedRet = HDF_SUCCESS;
178 #else
179 int32_t expectedRet = HDF_ERR_INVALID_PARAM;
180 #endif
181
182 NetIfSetStatus(g_netDevice, NETIF_UP);
183 NetIfDhcpsStop(g_netDevice);
184 if (NetIfDhcpsStart(g_netDevice, NULL, 0) != expectedRet) {
185 return HDF_FAILURE;
186 }
187 if (NetIfDhcpsStop(g_netDevice) != expectedRet) {
188 return HDF_FAILURE;
189 }
190 return HDF_SUCCESS;
191 }