XFusion API v1.3.0
载入中...
搜索中...
未找到
ex_easy_wifi.c
浏览该文件的文档.
1
12/* ==================== [Includes] ========================================== */
13
14#include "ex_easy_wifi.h"
15
16#include "xf_osal.h"
17
18/* ==================== [Defines] =========================================== */
19
20#define EXAMPLE_WIFI_AUTHMODE XF_WIFI_AUTH_WPA_WPA2_PSK
21#define EXAMPLE_WIFI_CHANNEL 6
22#define EXAMPLE_WIFI_SSID_HIDDEN_FLAG 0
24/*
25 TODO Kconfig 如果 string 类型输入 "\xaa\xbb\xcc\xdd\xee\xff"
26 要么转义成 "\\xaa\\xbb\\xcc\\xdd\\xee\\xff",
27 要么变成 "xaaxbbxccxddxeexff"
28 */
29#if !defined(EX_EASY_WIFI_AP_MAC)
30# define EX_EASY_WIFI_AP_MAC "\x11\x22\x33\x44\x55\x66"
31#endif
32#if !defined(EX_EASY_WIFI_STA_MAC)
33# define EX_EASY_WIFI_STA_MAC "\xaa\xbb\xcc\xdd\xee\xff"
34#endif
35
36/* ==================== [Typedefs] ========================================== */
37
38/* ==================== [Static Prototypes] ================================= */
39
40static void _wifi_ap_event_handler(
41 xf_wifi_event_id_t event_id, void *event_data, void *user_args);
42static void _wifi_ip_event_handler(
43 xf_ip_event_id_t event_id, void *event_data, void *user_args);
44
45static void _wifi_sta_event_handler(
46 xf_wifi_event_id_t event_id, void *event_data, void *user_args);
47static void _ip_event_handler(
48 xf_ip_event_id_t event_id, void *event_data, void *user_args);
49
50static void _sta_task(void *argument);
51
52/* ==================== [Static Variables] ================================== */
53
54static const char *TAG = "ex_easy_wifi";
55
56/* AP */
57
59 .b_set_mac = true,
61};
63 .ssid = CONFIG_EX_EASY_WIFI_SSID,
64 .password = CONFIG_EX_EASY_WIFI_PASSWORD,
65 .authmode = EXAMPLE_WIFI_AUTHMODE,
66 .channel = EXAMPLE_WIFI_CHANNEL,
67 .ssid_hidden = EXAMPLE_WIFI_SSID_HIDDEN_FLAG,
68 .p_cfg_ext = &s_ap_cfg_ext,
69 .p_static_ip = NULL,
70};
71static uint8_t s_ap_ip_assigned = false;
72
73/* STA */
74
75static uint8_t s_sta_connected = false;
76static uint8_t s_sta_got_ip_flag = false;
83 .ssid = CONFIG_EX_EASY_WIFI_SSID,
84 .password = CONFIG_EX_EASY_WIFI_PASSWORD,
85 .bssid_set = false,
86 .bssid = {0},
87 .authmode = EXAMPLE_WIFI_AUTHMODE,
88 .channel = EXAMPLE_WIFI_CHANNEL,
89 .p_cfg_ext = &s_sta_cfg_ext,
90 .p_static_ip = NULL,
91};
92
94#define EX_THREAD_NAME "ex_sta"
95#define EX_THREAD_PRIORITY XF_OSAL_PRIORITY_NORMOL
96#define EX_THREAD_STACK_SIZE (1024 * 4)
99 .priority = EX_THREAD_PRIORITY,
100 .stack_size = EX_THREAD_STACK_SIZE,
101};
102
103/* ==================== [Macros] ============================================ */
104
105/* ==================== [Global Functions] ================================== */
106
126
128{
129 xf_ip4_addr_t ip4_addr = {0};
130 xf_wifi_sta_info_t sta_array[8] = {0};
131 uint32_t sta_num = 0;
132 xf_err_t xf_ret = XF_OK;
133 xf_ret = xf_wifi_ap_get_sta_list(sta_array, ARRAY_SIZE(sta_array), &sta_num);
134 if (XF_OK != xf_ret) {
135 XF_LOGE(TAG, "ap_get_sta_list-%s", xf_err_to_name(xf_ret));
136 goto l_err;
137 }
138 if (sta_num > ARRAY_SIZE(sta_array) || (0 == sta_num)) {
139 XF_LOGE(TAG, "sta_num:%d-%s", sta_num, xf_err_to_name(XF_ERR_INVALID_ARG));
140 goto l_err;
141 }
142 xf_netif_t netif_hdl = NULL;
143 xf_ret = xf_wifi_ap_get_netif(&netif_hdl);
144 if (XF_OK != xf_ret) {
145 XF_LOGE(TAG, "ap_get_netif-%s", xf_err_to_name(xf_ret));
146 goto l_err;
147 }
148 xf_netif_pair_mac_ip_t pair_mac_ip[1] = {0};
149 xf_memcpy(pair_mac_ip[0].mac, sta_array[sta_num - 1].mac, ARRAY_SIZE(sta_array[0].mac));
151 netif_hdl, pair_mac_ip, ARRAY_SIZE(pair_mac_ip));
152 if (XF_OK != xf_ret) {
153 XF_LOGE(TAG, "xf_netif_dhcps_get_clients_by_mac-%s", xf_err_to_name(xf_ret));
154 goto l_err;
155 }
156 ip4_addr.addr = pair_mac_ip[0].ip.addr;
157 return ip4_addr;
158
159l_err:;
160 return (xf_ip4_addr_t) {
161 0
162 };
163}
164
166{
167 xf_ip4_addr_t ip4_addr = {0};
168 xf_err_t xf_ret = XF_OK;
169 xf_netif_t netif_hdl = NULL;
170 xf_ret = xf_wifi_ap_get_netif(&netif_hdl);
171 if (XF_OK != xf_ret) {
172 XF_LOGE(TAG, "ap_get_netif-%s", xf_err_to_name(xf_ret));
173 goto l_err;
174 }
175 xf_netif_ip_info_t ip_info = {0};
176 xf_ret = xf_netif_get_ip_info(netif_hdl, &ip_info);
177 if (XF_OK != xf_ret) {
178 XF_LOGE(TAG, "get_ip_info-%s", xf_err_to_name(xf_ret));
179 goto l_err;
180 }
181
182 ip4_addr.addr = ip_info.ip.addr;
183 return ip4_addr;
184
185l_err:;
186 return (xf_ip4_addr_t) {
187 0
188 };
189}
190
192{
196
198 if (strnlen((const char *)s_sta_cfg.password, XF_WIFI_PASSWORD_LEN_MAX) == 0) {
200 }
201
203
205 if (s_sta_thread_hdl == NULL) {
206 XF_LOGE(TAG, "xf_osal_thread_create error");
207 return XF_FAIL;
208 }
209
210 for (;;) {
211 if (true == s_sta_got_ip_flag) {
212 break;
213 }
214 xf_osal_delay_ms(100);
215 }
216
217 return XF_OK;
218}
219
221{
222 return (bool)s_sta_connected;
223}
224
226{
227 return (bool)s_sta_got_ip_flag;
228}
229
231{
232 xf_ip4_addr_t ip4_addr = {0};
233 xf_err_t xf_ret = XF_OK;
234 xf_netif_t netif_hdl = NULL;
235 xf_ret = xf_wifi_sta_get_netif(&netif_hdl);
236 if (XF_OK != xf_ret) {
237 XF_LOGE(TAG, "ap_get_netif-%s", xf_err_to_name(xf_ret));
238 goto l_err;
239 }
240 xf_netif_ip_info_t ip_info = {0};
241 xf_ret = xf_netif_get_ip_info(netif_hdl, &ip_info);
242 if (XF_OK != xf_ret) {
243 XF_LOGE(TAG, "get_ip_info-%s", xf_err_to_name(xf_ret));
244 goto l_err;
245 }
246
247 ip4_addr.addr = ip_info.gw.addr;
248 return ip4_addr;
249
250l_err:;
251 return (xf_ip4_addr_t) {
252 0
253 };
254}
255
257{
258 xf_ip4_addr_t ip4_addr = {0};
259 xf_err_t xf_ret = XF_OK;
260 xf_netif_t netif_hdl = NULL;
261 xf_ret = xf_wifi_sta_get_netif(&netif_hdl);
262 if (XF_OK != xf_ret) {
263 XF_LOGE(TAG, "ap_get_netif-%s", xf_err_to_name(xf_ret));
264 goto l_err;
265 }
266 xf_netif_ip_info_t ip_info = {0};
267 xf_ret = xf_netif_get_ip_info(netif_hdl, &ip_info);
268 if (XF_OK != xf_ret) {
269 XF_LOGE(TAG, "get_ip_info-%s", xf_err_to_name(xf_ret));
270 goto l_err;
271 }
272
273 ip4_addr.addr = ip_info.ip.addr;
274 return ip4_addr;
275l_err:;
276 return (xf_ip4_addr_t) {
277 0
278 };
279}
280
281/* ==================== [Static Functions] ================================== */
282
283static void _sta_task(void *argument)
284{
285 for (;;) {
286 if (false == xf_wifi_sta_is_connected()) {
287 /* 如果填 NULL,则使用 xf_wifi_sta_init() 设置的配置连接 */
289 xf_osal_delay_ms(3000);
290 }
291 xf_osal_delay_ms(100);
292 }
294}
295
297 xf_wifi_event_id_t event_id, void *event_data, void *user_args)
298{
299 UNUSED(user_args);
300 switch (event_id) {
302 XF_LOGI(TAG, "AP has started.");
303 } break;
305 XF_LOGI(TAG, "AP has stopped.");
306 } break;
309 XF_LOGI(TAG, "station "XF_MACSTR" join", XF_MAC2STR(e->mac));
310 } break;
313 XF_LOGI(TAG, "station "XF_MACSTR" leave", XF_MAC2STR(e->mac));
314 } break;
315 default:
316 break;
317 }
318}
319
321 xf_ip_event_id_t event_id, void *event_data, void *user_args)
322{
323 UNUSED(user_args);
324 switch (event_id) {
327 XF_LOGI(TAG, "Assign ip " XF_IPSTR " to site " XF_MACSTR,
328 XF_IP2STR(&e->ip), XF_MAC2STR(e->mac));
329 s_ap_ip_assigned = true;
330 } break;
331 default:
332 break;
333 }
334}
335
337 xf_wifi_event_id_t event_id, void *event_data, void *user_args)
338{
339 UNUSED(user_args);
340
341 switch (event_id) {
343 XF_LOGI(TAG, "STA has started.");
344 } break;
346 s_sta_got_ip_flag = false;
347 XF_LOGI(TAG, "STA has stopped.");
348 } break;
350 s_sta_connected = true;
351 XF_LOGI(TAG, "The STA is connected to the AP.");
353 XF_LOGI(TAG,
354 "\nssid: %s\n"
355 "mac: "XF_MACSTR"\n"
356 "channel: %d\n"
357 "authmode: %d",
358 e->ssid, XF_MAC2STR(e->bssid), (int)e->channel, (int)e->authmode);
359 } break;
361 /* 如果之前的状态是已连接 */
362 if (true == s_sta_connected) {
363 s_sta_got_ip_flag = false;
364 s_sta_connected = false;
365 XF_LOGI(TAG, "The STA has been disconnected from the AP.");
367 XF_LOGI(TAG,
368 "\nssid: %s\n"
369 "mac: "XF_MACSTR"\n"
370 "rssi: %d",
371 e->ssid, XF_MAC2STR(e->bssid), (int)e->rssi);
372 }
373 } break;
374 default:
375 break;
376 }
377}
378
380 xf_ip_event_id_t event_id, void *event_data, void *user_args)
381{
382 switch (event_id) {
383 case XF_IP_EVENT_GOT_IP: {
385 XF_LOGI(TAG, "\n"
386 "got ip: " XF_IPSTR "\n"
387 "got gw: " XF_IPSTR "\n"
388 "got netmask: " XF_IPSTR,
389 XF_IP2STR(&e->ip_info.ip),
390 XF_IP2STR(&e->ip_info.gw),
392 s_sta_got_ip_flag = true;
393 if (0 == e->ip_info.gw.addr) {
394 /* TODO 对于 ws63 的默认 at ap(1.10.101),存在不会分配 gw 的现象 */
395 xf_netif_ip_info_t ip_info = {0};
396 ip_info.ip.addr = e->ip_info.ip.addr;
397 ip_info.netmask.addr = e->ip_info.netmask.addr;
401 1);
402 xf_netif_set_ip_info(e->netif_hdl, &ip_info);
403 }
404 } break;
405 default:
406 break;
407 }
408}
xf_ip4_addr_t ex_easy_wifi_ap_get_onw_ip(void)
xf_ip4_addr_t ex_easy_wifi_ap_get_last_sta_ip(void)
#define EX_THREAD_PRIORITY
#define EX_THREAD_STACK_SIZE
#define EX_EASY_WIFI_STA_MAC
bool ex_easy_wifi_sta_got_ip(void)
static xf_wifi_ap_cfg_ext_t s_ap_cfg_ext
xf_ip4_addr_t ex_easy_wifi_sta_get_onw_ip(void)
bool ex_easy_wifi_sta_is_connected(void)
xf_err_t ex_easy_wifi_sta(void)
#define EX_THREAD_NAME
#define EX_EASY_WIFI_AP_MAC
static const char * TAG
xf_err_t ex_easy_wifi_ap(void)
static void _sta_task(void *argument)
static uint8_t s_sta_got_ip_flag
static const xf_osal_thread_attr_t s_sta_attr
static void _wifi_ap_event_handler(xf_wifi_event_id_t event_id, void *event_data, void *user_args)
static xf_wifi_sta_cfg_ext_t s_sta_cfg_ext
static uint8_t s_ap_ip_assigned
static void _wifi_sta_event_handler(xf_wifi_event_id_t event_id, void *event_data, void *user_args)
static uint8_t s_sta_connected
#define EXAMPLE_WIFI_CHANNEL
xf_ip4_addr_t ex_easy_wifi_sta_get_gw_ip(void)
#define EXAMPLE_WIFI_SSID_HIDDEN_FLAG
#define EXAMPLE_WIFI_AUTHMODE
static xf_osal_thread_t s_sta_thread_hdl
static xf_wifi_sta_cfg_t s_sta_cfg
static void _wifi_ip_event_handler(xf_ip_event_id_t event_id, void *event_data, void *user_args)
static xf_wifi_ap_cfg_t s_ap_cfg
static void _ip_event_handler(xf_ip_event_id_t event_id, void *event_data, void *user_args)
#define xf_ip4_addr1_16(ipaddr)
int32_t xf_ip_event_id_t
ip 事件 id。见 xf_ip_event_code_t.
#define XF_IPSTR
#define xf_ip4_addr3_16(ipaddr)
#define XF_MACSTR
Definition xf_mac.h:47
#define XF_IP4TOADDR(a, b, c, d)
#define XF_MAC2STR(a)
Definition xf_mac.h:44
void * xf_netif_t
xfusion netif 句柄。
#define XF_IP2STR(ipaddr)
#define xf_ip4_addr2_16(ipaddr)
@ XF_IP_EVENT_IP_ASSIGNED
@ XF_IP_EVENT_GOT_IP
xf_err_t xf_netif_dhcps_get_clients_by_mac(xf_netif_t netif_hdl, xf_netif_pair_mac_ip_t mac_ip_pair_array[], uint32_t pair_array_size)
根据 MAC 地址获取对应的 IP(对于 DHCP 服务器)。
xf_err_t xf_netif_set_ip_info(xf_netif_t netif_hdl, const xf_netif_ip_info_t *ip_info)
设置指定 netif 句柄的 IPv4 信息(ip地址、网关、子网掩码)。
xf_err_t xf_netif_get_ip_info(xf_netif_t netif_hdl, xf_netif_ip_info_t *ip_info)
获取指定 netif 句柄的 IPv4 信息(ip地址、网关、子网掩码)。
xf_err_t xf_osal_thread_delete(xf_osal_thread_t thread)
终止线程的执行。
xf_osal_thread_t xf_osal_thread_create(xf_osal_thread_func_t func, void *argument, const xf_osal_thread_attr_t *attr)
创建一个线程并将其添加到活动线程中。
void * xf_osal_thread_t
线程句柄。
xf_err_t xf_osal_delay_ms(uint32_t ms)
(睡眠)等待超时,以 ms 为单位。
#define XF_ERROR_CHECK(expression)
xfusion 错误检查宏(表达式 不等于 XF_OK 时则调用 XF_CHECK_ERROR_HANDLER)。
Definition xf_check.h:182
int32_t xf_err_t
整形错误类型。 错误码具体值见 xf_err_code_t.
Definition xf_err.h:69
const char * xf_err_to_name(xf_err_t code)
返回 xf_err_code_t 错误代码对应的错误信息字符串。
@ XF_FAIL
Definition xf_err.h:42
@ XF_ERR_INVALID_ARG
Definition xf_err.h:46
@ XF_OK
Definition xf_err.h:43
#define ARRAY_SIZE(arr)
ARRAY_SIZE - 获取数组 arr 中的元素数量。
Definition xf_predef.h:173
#define xf_memcpy(dest, src, n)
Definition xf_string.h:44
xf_err_t xf_wifi_ap_init(const xf_wifi_ap_cfg_t *p_cfg)
启用 wifi AP 模式.
xf_err_t xf_wifi_ap_set_cb(xf_wifi_cb_t cb_func, void *user_args)
AP 设置 wifi 事件回调函数.
xf_err_t xf_wifi_ap_get_netif(xf_netif_t *p_netif_hdl)
获取 AP 的 netif 句柄.
xf_err_t xf_wifi_ap_set_ip_cb(xf_ip_cb_t cb_func, void *user_args)
AP 设置 ip 事件回调函数.
xf_err_t xf_wifi_ap_get_sta_list(xf_wifi_sta_info_t sta_array[], uint32_t sta_array_size, uint32_t *p_sta_num)
获取 AP 已连接的 STA 列表.
xf_err_t xf_wifi_sta_set_ip_cb(xf_ip_cb_t cb_func, void *user_args)
STA 设置 ip 事件回调函数.
xf_err_t xf_wifi_sta_get_netif(xf_netif_t *p_netif_hdl)
获取 STA 的 netif 句柄.
xf_err_t xf_wifi_sta_connect(xf_wifi_sta_cfg_t *p_cfg)
STA 连接到指定 AP.
bool xf_wifi_sta_is_connected(void)
STA 检查是否已经连接到 AP.
xf_err_t xf_wifi_sta_set_cb(xf_wifi_cb_t cb_func, void *user_args)
wifi STA 设置回调函数.
xf_err_t xf_wifi_sta_init(const xf_wifi_sta_cfg_t *p_cfg)
启用 wifi STA 模式.
xf_err_t xf_wifi_enable(void)
启用 wifi 协议栈.
int32_t xf_wifi_event_id_t
wifi 事件 id。见 xf_wifi_event_code_t.
#define XF_WIFI_PASSWORD_LEN_MAX
wifi 密码最大长度。 64 字节有效字符 + 一个 '\0'。
@ XF_WIFI_AUTH_OPEN
@ XF_WIFI_EVENT_AP_STOP
@ XF_WIFI_EVENT_STA_START
@ XF_WIFI_EVENT_AP_STA_DISCONNECTED
@ XF_WIFI_EVENT_AP_STA_CONNECTED
@ XF_WIFI_EVENT_STA_DISCONNECTED
@ XF_WIFI_EVENT_STA_STOP
@ XF_WIFI_EVENT_STA_CONNECTED
@ XF_WIFI_EVENT_AP_START
XF_IP_EVENT_STA_GOT_IP 事件的事件结构。
xf_netif_ip_info_t ip_info
XF_IP_EVENT_AP_STAIPASSIGNED 事件的事件结构。
uint8_t mac[XF_MAC_LEN_MAX]
DHCP 客户端的地址信息(MAC 和 IP 地址对)。
线程的属性结构。
AP 事件消息类型: STA 连接。 事件 id 见 XF_WIFI_EVENT_AP_STA_CONNECTED.
uint8_t mac[XF_MAC_LEN_MAX]
AP 事件消息类型: STA 断开连接。 事件 id 见 XF_WIFI_EVENT_AP_STA_DISCONNECTED.
STA 事件消息类型: 已连接到 AP。 事件 id 见 XF_WIFI_EVENT_STA_CONNECTED.
uint8_t bssid[XF_MAC_LEN_MAX]
uint8_t ssid[XF_WIFI_SSID_LEN_MAX]
STA 事件消息类型: 已从 AP 断开连接。 事件 id 见 XF_WIFI_EVENT_STA_DISCONNECTED.
uint8_t bssid[XF_MAC_LEN_MAX]
uint8_t ssid[XF_WIFI_SSID_LEN_MAX]
Soft-AP 扩展配置结构体。 谨慎使用,不同平台对这些配置的处理不一定相同。
Soft-AP 基础配置结构体.
uint8_t password[XF_WIFI_PASSWORD_LEN_MAX]
uint8_t ssid[XF_WIFI_SSID_LEN_MAX]
STA 基础配置结构体。
uint8_t password[XF_WIFI_PASSWORD_LEN_MAX]
uint8_t ssid[XF_WIFI_SSID_LEN_MAX]
连接到当前 AP 的 STA 的信息。
#define XF_LOGI(tag, format,...)
#define XF_LOGE(tag, format,...)
#define UNUSED(_x)