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