1 /*
2 * Copyright (c) 2020 HiSilicon (Shanghai) 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 "hi_wifi_api.h"
17 #include "wifi_softap.h"
18 #include "lwip/ip_addr.h"
19 #include "lwip/netifapi.h"
20
21 #define APP_INIT_VAP_NUM 2
22 #define APP_INIT_USR_NUM 2
23
24 static struct netif *g_lwip_netif = NULL;
25
26 /* clear netif's ip, gateway and netmask */
hi_softap_reset_addr(struct netif * pst_lwip_netif)27 void hi_softap_reset_addr(struct netif *pst_lwip_netif)
28 {
29 ip4_addr_t st_gw;
30 ip4_addr_t st_ipaddr;
31 ip4_addr_t st_netmask;
32
33 if (pst_lwip_netif == NULL) {
34 printf("hisi_reset_addr::Null param of netdev\r\n");
35 return;
36 }
37
38 IP4_ADDR(&st_ipaddr, 0, 0, 0, 0);
39 IP4_ADDR(&st_gw, 0, 0, 0, 0);
40 IP4_ADDR(&st_netmask, 0, 0, 0, 0);
41
42 netifapi_netif_set_addr(pst_lwip_netif, &st_ipaddr, &st_netmask, &st_gw);
43 }
44
hi_wifi_start_softap(void)45 int hi_wifi_start_softap(void)
46 {
47 int ret;
48 errno_t rc;
49 char ifname[WIFI_IFNAME_MAX_SIZE + 1] = {0};
50 int len = sizeof(ifname);
51 hi_wifi_softap_config hapd_conf = {0};
52 const unsigned char wifi_vap_res_num = APP_INIT_VAP_NUM;
53 const unsigned char wifi_user_res_num = APP_INIT_USR_NUM;
54 ip4_addr_t st_gw;
55 ip4_addr_t st_ipaddr;
56 ip4_addr_t st_netmask;
57
58 ret = hi_wifi_init(wifi_vap_res_num, wifi_user_res_num);
59 if (ret != HISI_OK) {
60 printf("hi_wifi_init\n");
61 return -1;
62 }
63
64 rc = memcpy_s(hapd_conf.ssid, HI_WIFI_MAX_SSID_LEN + 1, "Hisilicon", 9); /* 9:ssid length */
65 if (rc != EOK) {
66 return -1;
67 }
68
69 hapd_conf.authmode = HI_WIFI_SECURITY_OPEN;
70 hapd_conf.channel_num = 1;
71
72 ret = hi_wifi_softap_start(&hapd_conf, ifname, &len);
73 if (ret != HISI_OK) {
74 printf("hi_wifi_softap_start\n");
75 return -1;
76 }
77
78 /* acquire netif for IP operation */
79 g_lwip_netif = netifapi_netif_find(ifname);
80 if (g_lwip_netif == NULL) {
81 printf("%s: get netif failed\n", __FUNCTION__);
82 return -1;
83 }
84
85 IP4_ADDR(&st_gw, 0, 0, 0, 0); /* input your IP for example: 192.168.1.1 */
86 IP4_ADDR(&st_ipaddr, 0, 0, 0, 0); /* input your netmask for example: 192.168.1.1 */
87 IP4_ADDR(&st_netmask, 0, 0, 0, 0); /* input your gateway for example: 255.255.255.0 */
88 netifapi_netif_set_addr(g_lwip_netif, &st_ipaddr, &st_netmask, &st_gw);
89
90 netifapi_dhcps_start(g_lwip_netif, 0, 0);
91
92 return 0;
93 }
94
hi_wifi_stop_softap(void)95 void hi_wifi_stop_softap(void)
96 {
97 int ret;
98
99 netifapi_dhcps_stop(g_lwip_netif);
100 hi_softap_reset_addr(g_lwip_netif);
101
102 ret = hi_wifi_softap_stop();
103 if (ret != HISI_OK) {
104 printf("failed to stop softap\n");
105 }
106
107 ret = hi_wifi_deinit();
108 if (ret != HISI_OK) {
109 printf("failed to deinit wifi\n");
110 }
111
112 g_lwip_netif = NULL;
113 }
114
115