XFusion API v1.3.0
载入中...
搜索中...
未找到
xf_main.c
浏览该文件的文档.
1
17/* ==================== [Includes] ========================================== */
18
19#include "lwip/err.h"
20#include "lwip/sockets.h"
21#include "lwip/sys.h"
22#include "lwip/netdb.h"
23#include "lwip/inet.h"
24
25#include "xf_utils.h"
26#include "xf_wifi.h"
27#include "xf_netif.h"
28#include "xf_osal.h"
29
30#include "ex_easy_wifi.h"
31
32/* ==================== [Defines] =========================================== */
33
34#define PORT CONFIG_EXAMPLE_PORT
35#define EXAMPLE_DELAY_MS 1000
36
37/* ==================== [Typedefs] ========================================== */
38
39/* ==================== [Static Prototypes] ================================= */
40
41static void _example_thread(void *argument);
42
43/* ==================== [Static Variables] ================================== */
44
45static const char *TAG = "sta";
46static const char *payload = "This is a message from client.";
47
49#define EX_THREAD_NAME "ex_thread"
50#define EX_THREAD_PRIORITY XF_OSAL_PRIORITY_NORMOL
51#define EX_THREAD_STACK_SIZE (1024 * 4)
54 .priority = EX_THREAD_PRIORITY,
55 .stack_size = EX_THREAD_STACK_SIZE,
56};
57
58/* ==================== [Macros] ============================================ */
59
60/* ==================== [Global Functions] ================================== */
61
62void xf_main(void)
63{
65 if (s_thread_hdl == NULL) {
66 XF_LOGE(TAG, "xf_osal_thread_create error");
67 return;
68 }
69}
70
71/* ==================== [Static Functions] ================================== */
72
73static void _example_thread(void *argument)
74{
75 UNUSED(argument);
76
78
79 /* 默认网关为主机 */
80 xf_ip4_addr_t host_ip4 = {0};
81 host_ip4 = ex_easy_wifi_sta_get_gw_ip();
82
83 int err = 0;
84 char rx_buffer[128];
85 int addr_family = 0;
86 int ip_protocol = 0;
87 int sockfd = 0;
88
89 struct sockaddr_in dest_addr = {0};
90 struct timeval timeout = {0};
91
92 dest_addr.sin_addr.s_addr = host_ip4.addr;
93 dest_addr.sin_family = AF_INET;
94 dest_addr.sin_port = htons(PORT);
95 addr_family = AF_INET;
96 ip_protocol = IPPROTO_IP;
97
98 sockfd = socket(addr_family, SOCK_STREAM, ip_protocol);
99 if (sockfd < 0) {
100 XF_LOGE(TAG, "Unable to create socket: errno %d", errno);
101 goto l_err;
102 }
103
104 err = connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr_in));
105 if (err != 0) {
106 XF_LOGE(TAG, "Socket unable to connect: errno %d", errno);
107 goto l_err;
108 }
109
110 XF_LOGI(TAG, "Successfully connected to " XF_IPSTR ":%d",
111 XF_IP2STR(&host_ip4), PORT);
112
113 /* 设置超时时间 */
114 timeout.tv_sec = 10;
115 timeout.tv_usec = 0;
116 setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
117
118 for (;;) {
120
121 /* 发送 */
122 int payload_len = strlen(payload);
123 err = send(sockfd, payload, payload_len, 0);
124 if (err < 0) {
125 XF_LOGE(TAG, "Error occurred during sending: errno %d", errno);
126 continue;
127 }
128 XF_LOGI(TAG, "sent %d bytes: \"%s\"", payload_len, payload);
129
130 /* 接收 */
131 int len = recv(sockfd, rx_buffer, sizeof(rx_buffer) - 1, 0);
132 if (len < 0) {
133 XF_LOGE(TAG, "recvfrom failed: errno %d", errno);
134 continue;
135 }
136
137 /* 收到数据 */
138 rx_buffer[len] = 0;
139 XF_LOGI(TAG, "Received %d bytes: \"%s\"", len, rx_buffer);
140 }
141
142l_err:;
143 if (sockfd != -1) {
144 XF_LOGE(TAG, "Shutting down socket...");
145 shutdown(sockfd, 0);
146 closesocket(sockfd);
147 }
149}
#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
#define XF_IP2STR(ipaddr)
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 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
#define EXAMPLE_DELAY_MS
Definition xf_main.c:35
#define PORT
Definition xf_main.c:34
static const char * payload
Definition xf_main.c:46
线程的属性结构。
#define XF_LOGI(tag, format,...)
#define XF_LOGE(tag, format,...)
#define UNUSED(_x)
xf_wifi 包含 STA、AP 接口。