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

http 请求示例。

1
17/* ==================== [Includes] ========================================== */
18
19#include <string.h>
20#include <sys/param.h>
21
22#include "lwip/err.h"
23#include "lwip/sockets.h"
24#include "lwip/sys.h"
25#include "lwip/netdb.h"
26
27#include <unistd.h>
28#include <arpa/inet.h>
29
30#include "xf_utils.h"
31#include "xf_wifi.h"
32#include "xf_netif.h"
33#include "xf_osal.h"
34#include "xf_task.h"
35
36#include "ex_easy_wifi.h"
37
38/* ==================== [Defines] =========================================== */
39
40#define HTTP_BUFFER_SIZE (1024 * 2)
41
42/* ==================== [Typedefs] ========================================== */
43
44/* ==================== [Static Prototypes] ================================= */
45
46static void _example_thread(void *argument);
47
48/* ==================== [Static Variables] ================================== */
49
50static const char *TAG = "sta";
51static char s_http_buf[HTTP_BUFFER_SIZE];
52
53static xf_osal_thread_t s_thread_hdl = NULL;
54#define EX_THREAD_NAME "ex_thread"
55#define EX_THREAD_PRIORITY XF_OSAL_PRIORITY_NORMOL
56#define EX_THREAD_STACK_SIZE (1024 * 4)
59 .priority = EX_THREAD_PRIORITY,
60 .stack_size = EX_THREAD_STACK_SIZE,
61};
62
63/* ==================== [Macros] ============================================ */
64
65/* ==================== [Global Functions] ================================== */
66
67void xf_main(void)
68{
70 if (s_thread_hdl == NULL) {
71 XF_LOGE(TAG, "xf_osal_thread_create error");
72 return;
73 }
74}
75
76/* ==================== [Static Functions] ================================== */
77
78static void _example_thread(void *argument)
79{
80 UNUSED(argument);
81
83
84 xf_netif_t netif_hdl = NULL;
85 xf_wifi_sta_get_netif(&netif_hdl);
86
87 xf_netif_dns_info_t dns_info = {0};
88 xf_netif_get_dns_info(netif_hdl, XF_NETIF_DNS_MAIN, &dns_info);
89 XF_LOGI(TAG, "dns main: " XF_IPSTR, XF_IP2STR(&dns_info.ip.u_addr.ip4));
90 xf_netif_get_dns_info(netif_hdl, XF_NETIF_DNS_BACKUP, &dns_info);
91 XF_LOGI(TAG, "dns backup: " XF_IPSTR, XF_IP2STR(&dns_info.ip.u_addr.ip4));
92
93 int sockfd = 0;
94 struct sockaddr_in server_addr;
95 struct hostent *host;
96 char *hostname = "example.com";
97 char *request = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n";
98
99 /* 解析域名 */
100 if ((host = gethostbyname(hostname)) == NULL) {
101 XF_LOGE(TAG, "gethostbyname failed");
102 goto l_err;
103 }
104
105 /* 创建 socket */
106 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
107 XF_LOGE(TAG, "Socket creation failed");
108 goto l_err;
109 }
110
111 /* 设置服务器地址 */
112 server_addr.sin_family = AF_INET;
113 server_addr.sin_port = htons(80);
114 server_addr.sin_addr.s_addr = *(long *)(host->h_addr);
115
116 /* 连接服务器 */
117 if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
118 XF_LOGE(TAG, "Connection failed");
119 closesocket(sockfd);
120 goto l_err;
121 }
122
123 /* 发送 HTTP 请求 */
124 send(sockfd, request, strlen(request), 0);
125
126 /* 接收服务器响应 */
127 int bytes_received = recv(sockfd, s_http_buf, HTTP_BUFFER_SIZE - 1, 0);
128 if (bytes_received < 0) {
129 XF_LOGE(TAG, "Receive failed");
130 } else {
131 s_http_buf[bytes_received] = '\0';
132 XF_LOGI(TAG, "Response:\n%s\n", s_http_buf);
133 }
134
135l_err:;
136 /* 关闭 socket */
137 closesocket(sockfd);
139}
#define EX_THREAD_PRIORITY
#define EX_THREAD_STACK_SIZE
xf_err_t ex_easy_wifi_sta(void)
#define EX_THREAD_NAME
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_wifi_sta_get_netif(xf_netif_t *p_netif_hdl)
获取 STA 的 netif 句柄.
#define TAG
Definition xf_main.c:24
#define HTTP_BUFFER_SIZE
Definition xf_main.c:40
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 char s_http_buf[HTTP_BUFFER_SIZE]
Definition xf_main.c:51
static xf_osal_thread_t s_thread_hdl
Definition xf_main.c:53
union _xf_ip_addr::@10 u_addr
DNS 服务器信息。
线程的属性结构。
#define XF_LOGI(tag, format,...)
#define XF_LOGE(tag, format,...)
#define UNUSED(_x)
xf_wifi 包含 STA、AP 接口。