• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * Description: SoftAP适配实现 (此文件为DEMO,需集成方适配修改)
15  */
16 #include "hilink_softap_adapter.h"
17 
18 #include <stddef.h>
19 #include "securec.h"
20 #include "wifi_device.h"
21 #include "wifi_hotspot.h"
22 #include "hilink_sal_defines.h"
23 #include "lwip/netifapi.h"
24 #include "lwip/nettool/ifconfig.h"
25 #include "osal_task.h"
26 
27 typedef struct {
28     const char *ifname;
29     const char *ip_str;
30     const char *netmask;
31     const char *netmask_value;
32     const char *gateway;
33     const char *gateway_value;
34 } ifcfg_args_t;
35 
CheckWifiStateBeforeStartSoftap(void)36 static void CheckWifiStateBeforeStartSoftap(void)
37 {
38     /* 套餐2存在hilink重入,重入后起softap需要保证sta与ap已关闭 */
39     if (wifi_is_sta_enabled() == 1) {
40         HILINK_SAL_NOTICE("sta active, try stop!\r\n");
41         if (wifi_sta_disable() != ERRCODE_SUCC) {
42             HILINK_SAL_WARN("stop sta fail\r\n");
43         }
44     }
45 
46     if (wifi_is_softap_enabled() == 1) {
47         HILINK_SAL_NOTICE("ap active, try stop!\r\n");
48         if (wifi_softap_disable() != ERRCODE_SUCC) {
49             HILINK_SAL_WARN("stop ap fail\r\n");
50         }
51     }
52 }
53 
softap_start_dhcps(void)54 static int softap_start_dhcps(void)
55 {
56     int ret;
57     struct netif *netif_p = netifapi_netif_find("ap0");
58     if (netif_p == NULL) {
59         HILINK_SAL_WARN("netif_p is null\r\n");
60         return HILINK_SAL_NOK;
61     }
62     if (ip_addr_isany_val(netif_p->ip_addr)) {
63         HILINK_SAL_WARN("Please set ip address for dhcp server\r\n");
64         return HILINK_SAL_NOK;
65     }
66 
67     ret = netifapi_dhcps_start(netif_p, NULL, 0);
68     if (ret == 0) {
69         HILINK_SAL_WARN("softap dhcps set OK\r\n");
70         return HILINK_SAL_OK;
71     }
72 
73     return HILINK_SAL_NOK;
74 }
75 
softap_config_static_ip(void)76 static int softap_config_static_ip(void)
77 {
78     int ret = 0;
79     ifcfg_args_t ifconfig_param;
80     memset_s(&ifconfig_param, sizeof(ifconfig_param), 0, sizeof(ifconfig_param));
81 
82     ifconfig_param.ifname = "ap0";
83     ifconfig_param.ip_str = "192.168.3.1";
84     ifconfig_param.netmask = "netmask";
85     ifconfig_param.netmask_value = "255.255.255.0";
86     ifconfig_param.gateway = "gateway";
87     ifconfig_param.gateway_value = "192.168.3.1";
88 
89     lwip_ifconfig(6, &ifconfig_param.ifname); // 6: ifconfig param number
90 
91     return HILINK_SAL_OK;
92 }
93 
HILINK_StartSoftAp(const char * ssid,unsigned int ssidLen)94 int HILINK_StartSoftAp(const char *ssid, unsigned int ssidLen)
95 {
96     if ((ssid == NULL) || (ssidLen == 0)) {
97         HILINK_SAL_WARN("invalid param\r\n");
98         return HILINK_SAL_PARAM_INVALID;
99     }
100 
101     CheckWifiStateBeforeStartSoftap();
102 
103     softap_config_stru config;
104     (void)memset_s(&config, sizeof(softap_config_stru), 0, sizeof(softap_config_stru));
105 
106     if (strcpy_s((char *)config.ssid, sizeof(config.ssid), ssid) != EOK) {
107         HILINK_SAL_WARN("strcpy error\r\n");
108         return HILINK_SAL_STRCPY_ERR;
109     }
110 
111     config.security_type = WIFI_SEC_TYPE_OPEN;
112     config.channel_num = 6; /* 6: channel num */
113 
114     if (wifi_softap_enable(&config) != ERRCODE_SUCC) {
115         HILINK_SAL_ERROR("enable hotspot fail\r\n");
116         return HILINK_SAL_SET_SOFTAP_ERR;
117     }
118 
119     if (softap_config_static_ip() != HILINK_SAL_OK) {
120         HILINK_SAL_ERROR("set softap ip failed\r\n");
121         return HILINK_SAL_SET_SOFTAP_ERR;
122     }
123     osal_msleep(4000); /* 4000:配置静态ip之后要睡眠4秒之后再开启dhcps */
124 
125     // dhcps
126     if (softap_start_dhcps() != 0) {
127         HILINK_SAL_ERROR("set softap dhcps failed\r\n");
128         return HILINK_SAL_SET_SOFTAP_ERR;
129     }
130 
131     return HILINK_SAL_OK;
132 }
133 
HILINK_StopSoftAp(void)134 int HILINK_StopSoftAp(void)
135 {
136     if (wifi_is_softap_enabled() == 1) {
137         if (wifi_softap_disable() != ERRCODE_SUCC) {
138             HILINK_SAL_ERROR("Stop softAp disable hotspot fail.\r\n");
139             return HILINK_SAL_SET_SOFTAP_ERR;
140         }
141     }
142     return HILINK_SAL_OK;
143 }