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 * Description: Application core main function for standard \n
16 *
17 * History: \n
18 * 2022-07-27, Create file. \n
19 */
20
21 #include "lwip/netifapi.h"
22 #include "wifi_hotspot.h"
23 #include "wifi_hotspot_config.h"
24 #include "td_base.h"
25 #include "td_type.h"
26 #include "stdlib.h"
27 #include "uart.h"
28 #include "cmsis_os2.h"
29 #include "app_init.h"
30 #include "soc_osal.h"
31
32 #define WIFI_IFNAME_MAX_SIZE 16
33 #define WIFI_MAX_KEY_LEN 65
34 #define WIFI_MAX_SSID_LEN 33
35 #define WIFI_SOFTAP_SAMPLE_LOG "[WIFI_SOFTAP_SAMPLE]"
36
37 #define WIFI_TASK_PRIO (osPriority_t)(13)
38 #define WIFI_TASK_DURATION_MS 2000
39 #define WIFI_TASK_STACK_SIZE 0x1000
40 /*****************************************************************************
41 SoftAP sample用例
42 *****************************************************************************/
example_softap_function(td_void)43 td_s32 example_softap_function(td_void)
44 {
45 /* SoftAp接口的信息 */
46 td_char ssid[WIFI_MAX_SSID_LEN] = "my_softAP";
47 td_char pre_shared_key[WIFI_MAX_KEY_LEN] = "my_password";
48 softap_config_stru hapd_conf = {0};
49 softap_config_advance_stru config = {0};
50 td_char ifname[WIFI_IFNAME_MAX_SIZE + 1] = "ap0"; /* 创建的SoftAp接口名 */
51 struct netif *netif_p = TD_NULL;
52 ip4_addr_t st_gw;
53 ip4_addr_t st_ipaddr;
54 ip4_addr_t st_netmask;
55 IP4_ADDR(&st_ipaddr, 192, 168, 43, 1); /* IP地址设置:192.168.43.1 */
56 IP4_ADDR(&st_netmask, 255, 255, 255, 0); /* 子网掩码设置:255.255.255.0 */
57 IP4_ADDR(&st_gw, 192, 168, 43, 2); /* 网关地址设置:192.168.43.2 */
58
59 /* 配置SoftAp基本参数 */
60 (td_void)memcpy_s(hapd_conf.ssid, sizeof(hapd_conf.ssid), ssid, sizeof(ssid));
61 (td_void)memcpy_s(hapd_conf.pre_shared_key, WIFI_MAX_KEY_LEN, pre_shared_key, WIFI_MAX_KEY_LEN);
62 hapd_conf.security_type = 3; /* 3: 加密方式设置为WPA_WPA2_PSK */
63 hapd_conf.channel_num = 13; /* 13:工作信道设置为13信道 */
64 hapd_conf.wifi_psk_type = 0;
65
66 /* 配置SoftAp网络参数 */
67 config.beacon_interval = 100; /* 100:Beacon周期设置为100ms */
68 config.dtim_period = 2; /* 2:DTIM周期设置为2 */
69 config.gi = 0; /* 0:short GI默认关闭 */
70 config.group_rekey = 86400; /* 86400:组播秘钥更新时间设置为1天 */
71 config.protocol_mode = 4; /* 4:协议类型设置为802.11b + 802.11g + 802.11n + 802.11ax */
72 config.hidden_ssid_flag = 1; /* 1:不隐藏SSID */
73 if (wifi_set_softap_config_advance(&config) != 0) {
74 return -1;
75 }
76 /* 启动SoftAp接口 */
77 if (wifi_softap_enable(&hapd_conf) != 0) {
78 return -1;
79 }
80 /* 配置DHCP服务器 */
81 netif_p = netif_find(ifname);
82 if (netif_p == TD_NULL) {
83 (td_void)wifi_softap_disable();
84 return -1;
85 }
86 if (netifapi_netif_set_addr(netif_p, &st_ipaddr, &st_netmask, &st_gw) != 0) {
87 (td_void)wifi_softap_disable();
88 return -1;
89 }
90 if (netifapi_dhcps_start(netif_p, NULL, 0) != 0) {
91 (td_void)wifi_softap_disable();
92 return -1;
93 }
94 PRINT("%s::SoftAp start success.\r\n", WIFI_SOFTAP_SAMPLE_LOG);
95 return 0;
96 }
97
softap_sample_init(void * param)98 int softap_sample_init(void *param)
99 {
100 param = param;
101
102 /* 等待wifi初始化完成 */
103 while (wifi_is_wifi_inited() == 0) {
104 (void)osDelay(10); /* 1: 等待100ms后判断状态 */
105 }
106 PRINT("%s::wifi init succ.\r\n", WIFI_SOFTAP_SAMPLE_LOG);
107
108 if (example_softap_function() != 0) {
109 PRINT("%s::example_softap_function fail.\r\n", WIFI_SOFTAP_SAMPLE_LOG);
110 return -1;
111 }
112 return 0;
113 }
114
softap_sample_entry(void)115 static void softap_sample_entry(void)
116 {
117 osThreadAttr_t attr;
118 attr.name = "softap_sample_task";
119 attr.attr_bits = 0U;
120 attr.cb_mem = NULL;
121 attr.cb_size = 0U;
122 attr.stack_mem = NULL;
123 attr.stack_size = WIFI_TASK_STACK_SIZE;
124 attr.priority = WIFI_TASK_PRIO;
125 if (osThreadNew((osThreadFunc_t)softap_sample_init, NULL, &attr) == NULL) {
126 PRINT("%s::Create softap_sample_task fail.\r\n", WIFI_SOFTAP_SAMPLE_LOG);
127 }
128 PRINT("%s::Create softap_sample_task succ.\r\n", WIFI_SOFTAP_SAMPLE_LOG);
129 }
130
131 /* Run the sta_sample_task. */
132 app_run(softap_sample_entry);