1 /*
2 * Copyright (c) 2021 FnLink (hunan) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "cmsis_os2.h"
17 #include "console.h"
18 #include "hal_trace.h"
19 #include "lwip/netifapi.h"
20 #include "lwip/sockets.h"
21 #include "lwip/api_shell.h"
22 #include "lwip/tcpip.h"
23 #include "ohos_init.h"
24 #include "wifi_hotspot.h"
25 #include <string.h>
26 #include <stdint.h>
27 #include <unistd.h>
28 #include "bwifi_interface.h"
29 #include "wifi_device.h"
30 #include "wifi_error_code.h"
31
at_param_null_check(int * _argc,char ** argv)32 static int at_param_null_check(int *_argc, char **argv)
33 {
34 int argc = *_argc;
35 printf("argc = %d\r\n", argc);
36 for (int i = 0; i < argc; i++) {
37 if (argv[i] == NULL) {
38 return -1;
39 }
40 printf("argv[%d] = %s\r\n", i, argv[i]);
41 }
42 argc = argc - 1;
43 for (int i = 0; i < argc; i++) {
44 argv[i] = argv[i+1];
45 }
46 *_argc = argc;
47 return 0;
48 }
49
lwip_ifconfig_check(int argc,const char ** argv)50 unsigned int lwip_ifconfig_check(int argc, const char **argv)
51 {
52 if ((argc == 0)) {
53 return 0;
54 } else if (argc == 2) { /* 2 Parameters */
55 if ((strcmp("up", argv[1]) == 0) || (strcmp("down", argv[1]) == 0)) {
56 return 0;
57 } else {
58 return -1;
59 }
60 } else if (argc == 6) { /* 6 Parameter */
61 if ((strcmp("netmask", argv[2]) == 0) && (strcmp("gw", argv[4]) == 0) && /* 2/4:Members of parameters */
62 (strcmp("inet", argv[1]) != 0) && (strcmp("inet6", argv[1]) != 0)) {
63 return 0;
64 } else {
65 return -1;
66 }
67 } else {
68 return -1;
69 }
70 }
71
at_lwip_ifconfig(cmd_tbl_t * cmd,int argc,char * argv[])72 static int at_lwip_ifconfig(cmd_tbl_t *cmd, int argc, char *argv[])
73 {
74 if (at_param_null_check(&argc, argv) == -1) {
75 return -1;
76 }
77
78 int ret = lwip_ifconfig_check(argc, argv);
79 if (ret != 0) {
80 return -1;
81 }
82 struct netif *netif = NULL;
83 if (argc != 0) {
84 if ((netif = netif_find(argv[0])) == NULL) {
85 netif = netifapi_netif_find_by_name(argv[0]);
86 }
87 }
88
89 if (argc == 2) { /* 2:Number of parameters */
90 if (netif == NULL) {
91 return -1;
92 }
93 if (strcmp(argv[1], "down") == 0) {
94 (void)netifapi_netif_set_link_down(netif);
95 (void)netifapi_netif_set_down(netif);
96 // (void)netif_l2_netdev_status_callback(netif);
97 (void)netifapi_netif_set_addr(netif, NULL, NULL, NULL);
98 #if LWIP_IPV6
99 for (hi_u8 index = 0; index < LWIP_IPV6_NUM_ADDRESSES; index++) {
100 (void)netifapi_netif_rmv_ip6_address(netif, &netif->ip6_addr[index]);
101 }
102 #endif
103 } else if (strcmp(argv[1], "up") == 0) {
104 (void)netifapi_netif_set_up(netif);
105 // (void)netif_l2_netdev_status_callback(netif);
106 #if LWIP_IPV6
107 (hi_void)netifapi_netif_add_ip6_linklocal_address(netif, HI_TRUE);
108 #endif
109 (void)netifapi_netif_set_link_up(netif);
110
111 (void)netifapi_netif_set_up(netif);
112 }
113 } else if (argc == 6){
114 if (netif == NULL || !netif_is_up(netif)) {
115 return -1;
116 }
117 ip4_addr_t ipaddr, netmask, gw;
118 ip4addr_aton(argv[1],&ipaddr);
119 ip4addr_aton(argv[3],&netmask);
120 ip4addr_aton(argv[5],&gw);
121 (void)netifapi_netif_set_addr(netif, &ipaddr, &netmask, &gw);
122 } else {
123 ret = lwip_ifconfig(argc, (const char **)argv);
124 if (ret == 0) {
125 return 0;
126 } else if (ret == 3) { /* 3:up down execution succeeded. */
127 printf("OK\r\n");
128 return 0;
129 }
130 return -1;
131 }
132 printf("OK\r\n");
133 return 0;
134 }
135 static int g_apEnableSuccess = 0;
136 static WifiEvent g_wifiEventHandler = {0};
137 static WifiErrorCode error;
138
OnHotspotStaJoinHandler(StationInfo * info)139 static void OnHotspotStaJoinHandler(StationInfo *info)
140 {
141 if (info == NULL) {
142 printf("HotspotStaJoin:info is null.\r\n");
143 return;
144 }
145 StationInfo stainfo[WIFI_MAX_STA_NUM] = {0};
146 StationInfo *sta_list_node = NULL;
147 unsigned int size = WIFI_MAX_STA_NUM;
148
149 error = GetStationList(stainfo, &size);
150 if (error != WIFI_SUCCESS) {
151 printf("HotspotStaJoin:get list fail, error is %d.\r\n", error);
152 return;
153 }
154 sta_list_node = stainfo;
155 for (uint32_t i = 0; i < size; i++, sta_list_node++) {
156 unsigned char *mac = sta_list_node->macAddress;
157 printf("HotspotSta[%u]: macAddress=%02X:%02X:%02X:%02X:%02X:%02X\r\n", i,
158 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
159 }
160 g_apEnableSuccess++;
161 }
162
OnHotspotStaLeaveHandler(StationInfo * info)163 static void OnHotspotStaLeaveHandler(StationInfo *info)
164 {
165 if (info == NULL) {
166 printf("HotspotStaLeave:info is null.\r\n");
167 return;
168 }
169 unsigned char *mac = info->macAddress;
170 printf("HotspotStaLeave: macAddress=%02X:%02X:%02X:%02X:%02X:%02X, reason=%d.\r\n",
171 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
172 info->disconnectedReason);
173 g_apEnableSuccess--;
174 }
175
OnHotspotStateChangedHandler(int state)176 static void OnHotspotStateChangedHandler(int state)
177 {
178 printf("HotspotStateChanged:state is %d.\r\n", state);
179 if (state == WIFI_HOTSPOT_ACTIVE) {
180 printf("wifi hotspot active.\r\n");
181 } else {
182 printf("wifi hotspot noactive.\r\n");
183 }
184 }
185
net_intf_status_change_cb(struct netif * netif)186 static void net_intf_status_change_cb(struct netif *netif)
187 {
188 if (netif_is_up(netif) && !ip_addr_isany(&netif->ip_addr)) {
189 struct ip4_addr_t * ip = (ip4_addr_t *)&netif->ip_addr;
190 struct ip4_addr_t * gw = (ip4_addr_t *)&netif->gw;
191 struct ip4_addr_t * mask = (ip4_addr_t *)&netif->netmask;
192 printf("net_intf_status_change_cb **ip = %s\n", ip4addr_ntoa(ip));
193 printf("net_intf_status_change_cb **netmask = %s\n", ip4addr_ntoa(mask));
194 printf("net_intf_status_change_cb **gw = %s\n", ip4addr_ntoa(gw));
195 }
196 }
197
WifiAPStart(char * ap_ssid,char * ap_psk,int sec_type,int band_type,int channel)198 static int WifiAPStart(char* ap_ssid, char* ap_psk, int sec_type, int band_type, int channel)
199 {
200 osDelay(2000);
201
202 //注册wifi事件的回调函数
203 g_wifiEventHandler.OnHotspotStaJoin = OnHotspotStaJoinHandler;
204 g_wifiEventHandler.OnHotspotStaLeave = OnHotspotStaLeaveHandler;
205 g_wifiEventHandler.OnHotspotStateChanged = OnHotspotStateChangedHandler;
206 error = RegisterWifiEvent(&g_wifiEventHandler);
207 if (error != WIFI_SUCCESS) {
208 printf("RegisterWifiEvent failed, error = %d.\r\n", error);
209 return -1;
210 }
211 printf("RegisterWifiEvent succeed!\r\n");
212
213 HotspotConfig config = {0};
214 strcpy(config.ssid, ap_ssid);
215 strcpy(config.preSharedKey, ap_psk);
216 config.securityType = sec_type;
217 config.band = band_type;
218 config.channelNum = channel;
219
220 error = SetHotspotConfig(&config);
221 printf("SetHotspotConfig succeed!\r\n");
222
223 if (error != WIFI_SUCCESS) {
224 printf("SetHotspotConfig failed, error = %d.\r\n", error);
225 return -1;
226 }
227
228 error = EnableHotspot();
229 if (error != WIFI_SUCCESS) {
230 printf("EnableHotspot failed, error = %d.\r\n", error);
231 return -1;
232 }
233 printf("EnableHotspot succeed!\r\n");
234
235 if (IsHotspotActive() == WIFI_HOTSPOT_NOT_ACTIVE) {
236 printf("Wifi Hotspot is not actived.\r\n");
237 return -1;
238 }
239 printf("Wifi Hotspot is actived!\r\n");
240 return 0;
241 }
242
at_start_softap(cmd_tbl_t * cmd,int argc,char * argv[])243 static int at_start_softap(cmd_tbl_t *cmd, int argc, char *argv[])
244 {
245 if (at_param_null_check(&argc, argv) == -1 || argc != 5) {
246 return -1;
247 }
248
249 char* ap_ssid = argv[0];
250 char* ap_psk = argv[1];
251 int sec_type = atoi(argv[2]);
252 int band_type = atoi(argv[3]);
253 int channel = atoi(argv[4]);
254
255 if(WifiAPStart(ap_ssid, ap_psk, sec_type, band_type, channel) != 0){
256 return -1;
257 }
258 printf("OK\r\n");
259 return 0;
260 }
261
at_stop_softap(cmd_tbl_t * cmd,int argc,char * argv[])262 static int at_stop_softap(cmd_tbl_t *cmd, int argc, char *argv[])
263 {
264 int ret = 0;
265
266 if(DisableHotspot() != WIFI_SUCCESS) {
267 return -1;
268 }
269 printf("OK\r\n");
270 return 0;
271 }
at_start_wifista(cmd_tbl_t * cmd,int argc,char * argv[])272 static int at_start_wifista(cmd_tbl_t *cmd, int argc, char *argv[])
273 {
274 int ret = 0;
275 ret = EnableWifi();
276
277 printf("OK\r\n");
278 return ret;
279 }
280
at_stop_wifista(cmd_tbl_t * cmd,int argc,char * argv[])281 static int at_stop_wifista(cmd_tbl_t *cmd, int argc, char *argv[])
282 {
283 if (IsWifiActive() == WIFI_STA_NOT_ACTIVE) {
284 printf("Wifi station is not actived.\n");
285 return -1;
286 }
287 if(DisableWifi() != WIFI_SUCCESS) {
288 return -1;
289 }
290
291 printf("OK\r\n");
292 return 0;
293 }
294
at_setup_dhcp(cmd_tbl_t * cmd,int argc,char * argv[])295 static int at_setup_dhcp(cmd_tbl_t *cmd, int argc, char *argv[])
296 {
297 int ret = 0;
298
299 if (at_param_null_check(&argc, argv) == -1) {
300 return -1;
301 }
302 if (argc != 2) {
303 return -1;
304 }
305
306 struct netif *netif_p = NULL;
307 if (argc != 0) {
308 if ((netif_p = netif_find(argv[0])) == NULL) {
309 netif_p = netifapi_netif_find_by_name(argv[0]);
310 }
311 }
312 if (netif_p == NULL) {
313 return -1;
314 }
315
316 if (strcmp(argv[1], "1") == 0) {
317 printf("netifapi_dhcp_start\r\n");
318 ret = netifapi_dhcp_start(netif_p);
319 } else if (strcmp(argv[1], "0") == 0) {
320 printf("netifapi_dhcp_stop\r\n");
321 ret = netifapi_dhcp_stop(netif_p);
322 } else {
323 printf("error param\r\n");
324 return -1;
325 }
326 if (ret == LOS_OK) {
327 printf("OK\r\n");
328 }
329
330 return ret;
331 }
332
at_setup_dhcps(cmd_tbl_t * cmd,int argc,char * argv[])333 static int at_setup_dhcps(cmd_tbl_t *cmd, int argc, char *argv[])
334 {
335 int ret = 0;
336
337 if (at_param_null_check(&argc, argv) == -1) {
338 return -1;
339 }
340 if (argc != 2) {
341 return -1;
342 }
343
344 struct netif *netif_p = NULL;
345 if (argc != 0) {
346 if ((netif_p = netif_find(argv[0])) == NULL) {
347 netif_p = netifapi_netif_find_by_name(argv[0]);
348 }
349 }
350 if (netif_p == NULL) {
351 return -1;
352 }
353
354 if (ip_addr_isany_val(netif_p->ip_addr)) {
355 printf("Please set ip address for dhcp server\r\n");
356 return -1;
357 }
358 if (strcmp(argv[1], "1") == 0) {
359 printf("netifapi_dhcps_start\r\n");
360 ret = netifapi_dhcps_start(netif_p, NULL, 0);
361 } else if (strcmp(argv[1], "0") == 0) {
362 printf("netifapi_dhcps_stop\r\n");
363 ret = netifapi_dhcps_stop(netif_p);
364 } else {
365 printf("error param\r\n");
366 return -1;
367 }
368 if (ret == LOS_OK) {
369 printf("OK\r\n");
370 }
371
372 return ret;
373 }
RegisterCustomATCmd()374 static void RegisterCustomATCmd()
375 {
376 cmd_tbl_t cmd_list[] = {
377 {"AT+IFCFG", 8, at_lwip_ifconfig, "AT+IFCFG - ifconfig\n"},
378 {"AT+STARTAP", 7, at_start_softap, "AT+STARTAP - start wifi softap\n"},
379 {"AT+STOPAP", 1, at_stop_softap, "AT+STOPAP - stop wifi softap\n"},
380 {"AT+STARTSTA", 1, at_start_wifista, "AT+STARTSTA - start wifi sta\n"},
381 {"AT+STOPSTA", 1, at_stop_wifista, "AT+STOPSTA - stop wifi sta\n"},
382 {"AT+DHCP", 3, at_setup_dhcp, "AT+DHCP - dhcp\n"},
383 {"AT+DHCPS", 3, at_setup_dhcps, "AT+DHCPS - dhcps\n"},
384 };
385 for (int i = 0; i < sizeof(cmd_list) / sizeof(cmd_tbl_t); i++) {
386 console_cmd_add(&cmd_list[i]);
387 }
388 }
389 SYS_SERVICE_INIT(RegisterCustomATCmd);