1 /*
2 * Copyright (c) 2022 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 "wifi_softap.h"
17 #include "hi_wifi_api.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_lwipNetif = NULL;
25
26 /* clear netif's ip, gateway and netmask */
HiSoftapResetAddr(const struct netif * pstLwipNetif)27 void HiSoftapResetAddr(const struct netif *pstLwipNetif)
28 {
29 ip4_addr_t st_gw;
30 ip4_addr_t st_ipaddr;
31 ip4_addr_t st_netmask;
32
33 if (pstLwipNetif == 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(pstLwipNetif, &st_ipaddr, &st_netmask, &st_gw);
43 }
44
HiWifiStartSoftap(void)45 int HiWifiStartSoftap(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 wifiVapResNum = APP_INIT_VAP_NUM;
53 const unsigned char wifiUserResNum = APP_INIT_USR_NUM;
54 ip4_addr_t st_gw;
55 ip4_addr_t st_ipaddr;
56 ip4_addr_t st_netmask;
57 ip4_addr_t st_port;
58
59 rc = memcpy_s(hapd_conf.ssid, HI_WIFI_MAX_SSID_LEN + 1, "XXXXXXXX", 9); /* 9:ssid length */
60 if (rc != EOK) {
61 return -1;
62 }
63 hapd_conf.authmode = HI_WIFI_SECURITY_OPEN;
64 hapd_conf.channel_num = 1;
65
66 ret = hi_wifi_softap_start(&hapd_conf, ifname, &len);
67 if (ret != HISI_OK) {
68 printf("hi_wifi_softap_start\n");
69 return -1;
70 }
71
72 /* acquire netif for IP operation */
73 g_lwipNetif = netifapi_netif_find(ifname);
74 if (g_lwipNetif == NULL) {
75 printf("%s: get netif failed\n", __FUNCTION__);
76 return -1;
77 }
78
79 IP4_ADDR(&st_gw, 192, 168, 1, 1); /* input your IP for example: 192.168.1.1 */
80 IP4_ADDR(&st_ipaddr, 192, 168, 1, 4); /* input your netmask for example: 192.168.4.1 */
81 IP4_ADDR(&st_netmask, 255, 255, 255, 0); /* input your gateway for example: 255.255.255.0 */
82 netifapi_netif_set_addr(g_lwipNetif, &st_ipaddr, &st_netmask, &st_gw);
83
84 netifapi_dhcps_start(g_lwipNetif, 0, 0);
85
86 return 0;
87 }
88
HiWifiStopSoftap(void)89 void HiWifiStopSoftap(void)
90 {
91 int ret;
92
93 netifapi_dhcps_stop(g_lwipNetif);
94 HiSoftapResetAddr(g_lwipNetif);
95
96 ret = hi_wifi_softap_stop();
97 if (ret != HISI_OK) {
98 printf("failed to stop softap\n");
99 }
100
101 ret = hi_wifi_deinit();
102 if (ret != HISI_OK) {
103 printf("failed to deinit wifi\n");
104 }
105
106 g_lwipNetif = NULL;
107 }
108
109