TuyaOS
websocket_client.h
1 /*============================================================================
2 * *
3 * Copyright (C) by Tuya Inc *
4 * All rights reserved *
5 * *
6 * @author : Linch *
7 * @date : 2020-07-07 *
8 * @brief : *
9 * @log : *
10 =============================================================================*/
11
12#ifndef __WEBSOCKET_H__
13#define __WEBSOCKET_H__
14
15#ifdef __cplusplus
16 extern "C" {
17#endif
18
19/*============================ INCLUDES ======================================*/
20#include "tuya_iot_config.h"
21#include "tuya_transporter.h"
22#include "tal_thread.h"
23#include "tuya_tls.h"
24
25/*============================ MACROS ========================================*/
26/*============================ MACROFIED FUNCTIONS ===========================*/
27/*============================ TYPES =========================================*/
28typedef enum {
29 WEBSOCKET_OK,
30 WEBSOCKET_COM_ERROR,
31 WEBSOCKET_MALLOC_FAILED,
32 WEBSOCKET_INVALID_PARM,
33} websocket_error_t;
34
35typedef enum {
36 WEBSOCKET_RECV_DATA_EVENT,
37 WEBSOCKET_CONNECTED_EVENT,
38 WEBSOCKET_DISCONNECT_EVENT,
39} websocket_client_event_t;
40
41typedef struct {
42 websocket_client_event_t event;
43 uint8_t *data;
44 uint32_t len;
46
47
48typedef void (*websocket_client_event_cb_t)(websocket_client_msg_t *msg, void *priv_data);
49
50typedef struct {
51 char *uri;
52 char* scheme;
53 char* host;
54 int port;
55 tuya_tcp_config_t tcpConfig;
56 void *priv_data;
57 websocket_client_event_cb_t event_cb;
59
60typedef enum {
61 WEBSOCKET_INIT,
62 WEBSOCKET_CONNECT,
63 WEBSOCKET_RECEIVE,
64 WEBSOCKET_TIMEOUT,
65 WEBSOCKET_PING,
66 WEBSOCKET_WAIT,
67} websocket_client_state_t;
68
69#define WEBSOCKET_BUFFER_SIZE 1024
70
71typedef struct {
72 websocket_client_state_t state;
73 uint8_t run;
74 uint8_t connected;
75 uint8_t tls_enable;
76 tuya_tls_hander tls_hander;
78 char *hostname;
79 char *path;
80 uint16_t port;
81 uint8_t buffer[WEBSOCKET_BUFFER_SIZE];
82 uint16_t ping;
83 uint16_t ping_tick;
84 THREAD_HANDLE task;
85 tuya_transporter_t transporter;
87
88typedef void *websocket_client_handle_t;
89
90/*============================ PROTOTYPES ====================================*/
91int websocket_client_init (websocket_client_handle_t *handle, websocket_client_cfg_t *cfg);
92int websocket_client_start (websocket_client_handle_t handle, char *name);
93
94
95int websocket_client_set_path(websocket_client_handle_t handle, char *path);
96
97int websocket_client_send_bin (websocket_client_handle_t handle, uint8_t *data, uint32_t data_len);
98int websocket_client_send_text (websocket_client_handle_t handle, uint8_t *data, uint32_t data_len);
99
100int websocket_client_disconnect (websocket_client_handle_t handle);
101
102int websocket_client_open(websocket_client_handle_t client, int connect_timeout_ms);
103
104int websocket_client_poll(websocket_client_handle_t client, int timeout_ms);
105int websocket_client_read(websocket_client_handle_t client, uint8_t *buffer, int len);
106void websocket_client_close(websocket_client_handle_t client);
107
108#ifdef __cplusplus
109} // extern "C"
110#endif
111
112#endif
定义了tcp transporter配置选项
Definition: tuya_transporter.h:57
Definition: tuya_transporter.h:387
Definition: websocket_client.h:50
char * scheme
<ws/wss>://host[:port]/path
Definition: websocket_client.h:52
Definition: websocket_client.h:41
Definition: websocket_client.h:71
Common process - tls include