XFusion API v1.3.0
载入中...
搜索中...
未找到
examples/protocols/icmp_echo/main/xf_main.c

ping 示例。

1
17/* ==================== [Includes] ========================================== */
18
19#include <string.h>
20
21#include "lwip/err.h"
22#include "lwip/sockets.h"
23#include "lwip/inet.h"
24#include "lwip/sys.h"
25#include "lwip/netdb.h"
26
27#include "xf_utils.h"
28#include "xf_wifi.h"
29#include "xf_netif.h"
30#include "xf_osal.h"
31
32#include "xf_ping.h"
33
34#include "ex_easy_wifi.h"
35
36/* ==================== [Defines] =========================================== */
37
38/* ==================== [Typedefs] ========================================== */
39
40/* ==================== [Static Prototypes] ================================= */
41
42static void _example_thread(void *argument);
43static void xf_ping_cb(
44 xf_ping_event_id_t event_id, xf_ping_t hdl, void *user_args);
45
46/* ==================== [Static Variables] ================================== */
47
48static const char *TAG = "sta";
49
50static xf_osal_thread_t s_thread_hdl = NULL;
51#define EX_THREAD_NAME "ex_thread"
52#define EX_THREAD_PRIORITY XF_OSAL_PRIORITY_NORMOL
53#define EX_THREAD_STACK_SIZE (1024 * 4)
56 .priority = EX_THREAD_PRIORITY,
57 .stack_size = EX_THREAD_STACK_SIZE,
58};
59
60static uint8_t s_ping_done = false;
61static const char *const s_hostname_arr[] = {
62 "example.com",
63 "www.baidu.com",
64 "www.qq.com",
65 "www.360.com",
66};
67
68/* ==================== [Macros] ============================================ */
69
70#define ERROR_CHECK(e) \
71 do { \
72 if (unlikely((e) != XF_OK)) { \
73 XF_LOGE(TAG, "An error occurred: %s", XSTR(e != XF_OK)); \
74 while(1); \
75 } \
76 } while (0)
77#define TEST_XF_OK(e) ERROR_CHECK(e)
78
79/* ==================== [Global Functions] ================================== */
80
81void xf_main(void)
82{
84 if (s_thread_hdl == NULL) {
85 XF_LOGE(TAG, "xf_osal_thread_create error");
86 return;
87 }
88}
89
90/* ==================== [Static Functions] ================================== */
91
92static void xf_ping_cb(
93 xf_ping_event_id_t event_id, xf_ping_t hdl, void *user_args)
94{
95 UNUSED(user_args);
96 switch (event_id) {
97 case XF_PING_EVENT_SUCC: {
98 xf_log_printf("%d bytes from " XF_IPSTR " icmp_seq=%d ttl=%d time=%d ms" "\n",
99 hdl->recv_len, XF_IP2STR(&hdl->recv_addr.u_addr.ip4),
100 hdl->packet_hdr->seqno, hdl->ttl, hdl->elapsed_time_ms);
101 } break;
103 xf_log_printf("From " XF_IPSTR " icmp_seq=%d timeout" "\n",
104 XF_IP2STR(&hdl->recv_addr.u_addr.ip4), hdl->packet_hdr->seqno);
105 } break;
106 case XF_PING_EVENT_END: {
107 uint32_t loss = (uint32_t)((1 - ((float)hdl->received) / hdl->transmitted) * 100);
108 if (IP_IS_V4(&hdl->recv_addr)) {
109 struct in_addr ip4;
110 ip4.s_addr = hdl->recv_addr.u_addr.ip4.addr;
111 xf_log_printf("\n--- %s ping statistics ---\n", inet_ntoa(ip4));
112 } else {
113 xf_log_printf("\n--- %s ping statistics ---\n", inet6_ntoa(*ip_2_ip6(&hdl->recv_addr)));
114 }
115 xf_log_printf("%d packets transmitted, %d received, %d%% packet loss, time %dms" "\n",
116 hdl->transmitted, hdl->received, loss, hdl->total_time_ms);
117 s_ping_done = true;
118 } break;
119 default:
120 break;
121 }
122}
123
124static void _example_thread(void *argument)
125{
126 UNUSED(argument);
127
129
130 xf_netif_t netif_hdl = NULL;
131 xf_wifi_sta_get_netif(&netif_hdl);
132
133 xf_netif_dns_info_t dns_info = {0};
134 xf_netif_get_dns_info(netif_hdl, XF_NETIF_DNS_MAIN, &dns_info);
135 XF_LOGI(TAG, "dns main: " XF_IPSTR, XF_IP2STR(&dns_info.ip.u_addr.ip4));
136 xf_netif_get_dns_info(netif_hdl, XF_NETIF_DNS_BACKUP, &dns_info);
137 XF_LOGI(TAG, "dns backup: " XF_IPSTR, XF_IP2STR(&dns_info.ip.u_addr.ip4));
138
139 for (size_t hostname_idx = 0;
140 hostname_idx < ARRAY_SIZE(s_hostname_arr);
141 hostname_idx++) {
142
143 xf_ip4_addr_t dest_ip4 = {0};
144 struct hostent *host;
145 ip_addr_t target_addr = {0};
146
147 /* 解析域名 */
148 if ((host = gethostbyname(s_hostname_arr[hostname_idx])) == NULL) {
149 XF_LOGE(TAG, "gethostbyname failed");
150 dest_ip4 = ex_easy_wifi_sta_get_gw_ip();
151 *(uint32_t *)(host->h_addr) = dest_ip4.addr;
152 }
153 dest_ip4.addr = *(uint32_t *)(host->h_addr);
154
155 target_addr.u_addr.ip4.addr = dest_ip4.addr;
156 target_addr.type = IPADDR_TYPE_V4;
157 XF_LOGI(TAG, "target_addr: " XF_IPSTR,
158 XF_IP2STR(&target_addr.u_addr.ip4));
159
161 ping_cfg.timeout_ms = 2000;
162 ping_cfg.target_addr = target_addr;
163 ping_cfg.count = 5;
164 ping_cfg.interval_ms = 1000;
165
166 xf_ping_t ping_hdl = NULL;
168 &ping_cfg, xf_ping_cb, NULL,
169 &ping_hdl));
170 TEST_XF_OK(xf_ping_start(ping_hdl));
171
172 while (s_ping_done == false) {
173 xf_osal_delay_ms(100);
174 }
175 s_ping_done = false;
176
178
179 xf_osal_delay_ms(1000);
180 } /* for 0 ~ ARRAY_SIZE(s_hostname_arr) */
181
183}
#define EX_THREAD_PRIORITY
#define EX_THREAD_STACK_SIZE
xf_err_t ex_easy_wifi_sta(void)
#define EX_THREAD_NAME
xf_ip4_addr_t ex_easy_wifi_sta_get_gw_ip(void)
void xf_main(void)
Definition xf_main.c:28
#define XF_IPSTR
void * xf_netif_t
xfusion netif 句柄。
#define XF_IP2STR(ipaddr)
@ XF_NETIF_DNS_MAIN
@ XF_NETIF_DNS_BACKUP
xf_err_t xf_netif_get_dns_info(xf_netif_t netif_hdl, xf_netif_dns_type_t type, xf_netif_dns_info_t *dns)
从指定 netif 句柄获取的 DNS 服务端信息。
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 为单位。
xf_err_t xf_ping_new_session(const xf_ping_cfg_t *p_cfg, xf_ping_cb_t cb_func, void *user_args, xf_ping_t *hdl_out)
创建 ping 会话。
Definition xf_ping.c:70
xf_err_t xf_ping_start(xf_ping_t hdl)
启动 ping 会话。
Definition xf_ping.c:224
#define XF_PING_DEFAULT_CONFIG()
默认 ping 配置
Definition xf_ping.h:93
int32_t xf_ping_event_id_t
ping 事件 id。见 xf_ping_event_code_t.
Definition xf_ping.h:134
xf_err_t xf_ping_delete_session(xf_ping_t hdl)
删除 ping 会话。
Definition xf_ping.c:196
@ XF_PING_EVENT_TIMEOUT
Definition xf_ping.h:124
@ XF_PING_EVENT_END
Definition xf_ping.h:125
@ XF_PING_EVENT_SUCC
Definition xf_ping.h:123
#define ARRAY_SIZE(arr)
ARRAY_SIZE - 获取数组 arr 中的元素数量。
Definition xf_predef.h:173
xf_err_t xf_wifi_sta_get_netif(xf_netif_t *p_netif_hdl)
获取 STA 的 netif 句柄.
#define TAG
Definition xf_main.c:24
static const xf_osal_thread_attr_t s_thread_attr
Definition xf_main.c:57
static void _example_thread(void *argument)
Definition xf_main.c:78
static xf_osal_thread_t s_thread_hdl
Definition xf_main.c:53
static const char *const s_hostname_arr[]
Definition xf_main.c:61
#define TEST_XF_OK(e)
Definition xf_main.c:77
static void xf_ping_cb(xf_ping_event_id_t event_id, xf_ping_t hdl, void *user_args)
Definition xf_main.c:92
static uint8_t s_ping_done
Definition xf_main.c:60
union _xf_ip_addr::@10 u_addr
DNS 服务器信息。
线程的属性结构。
ping 配置类型。
Definition xf_ping.h:61
uint32_t count
Definition xf_ping.h:62
uint32_t interval_ms
Definition xf_ping.h:63
uint32_t timeout_ms
Definition xf_ping.h:64
ip_addr_t target_addr
Definition xf_ping.h:68
ping 会话的上下文。
Definition xf_ping.h:151
uint32_t elapsed_time_ms
Definition xf_ping.h:157
uint32_t transmitted
Definition xf_ping.h:161
uint32_t received
Definition xf_ping.h:162
uint8_t ttl
Definition xf_ping.h:163
uint32_t total_time_ms
Definition xf_ping.h:158
struct icmp_echo_hdr * packet_hdr
Definition xf_ping.h:154
ip_addr_t recv_addr
Definition xf_ping.h:155
uint32_t recv_len
Definition xf_ping.h:156
#define XF_LOGI(tag, format,...)
#define XF_LOGE(tag, format,...)
#define UNUSED(_x)
#define xf_log_printf(format,...)
xf_wifi 包含 STA、AP 接口。