master
h 155 lines 5.78 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #ifndef MQTT_WSS_CLIENT_H
4 #define MQTT_WSS_CLIENT_H
5
6 #include "common_public.h"
7
8 #define MQTT_WSS_OK 0 // All OK call me at your earliest convinience
9 #define MQTT_WSS_OK_TO 1 // All OK, poll timeout you requested when calling mqtt_wss_service expired
10 //you might want to know if timeout
11 //happened or we got some data or handle same as MQTT_WSS_OK
12 #define MQTT_WSS_ERR_CONN_DROP -1 // Connection was closed by remote
13 #define MQTT_WSS_ERR_PROTO_MQTT -2 // Error in MQTT protocol (e.g. malformed packet)
14 #define MQTT_WSS_ERR_PROTO_WS -3 // Error in WebSocket protocol (e.g. malformed packet)
15 #define MQTT_WSS_ERR_MSG_TOO_BIG -6 // Message size too big for server
16 #define MQTT_WSS_ERR_CANT_DO -8 // if client was initialized with MQTT 3 but MQTT 5 feature
17 // was requested by user of library
18 #define MQTT_WSS_ERR_POLL_FAILED -9
19 #define MQTT_WSS_ERR_REMOTE_CLOSED -10
20
21 typedef struct mqtt_wss_client_struct *mqtt_wss_client;
22
23 typedef void (*msg_callback_fnc_t)(const char *topic, const void *msg, size_t msglen, int qos);
24
25 /* Creates new instance of MQTT over WSS. Doesn't start connection.
26 * @param msg_callback is function pointer to function which will be called
27 * when application level message arrives from broker (for subscribed topics).
28 * Can be NULL if you are not interested about incoming messages.
29 * @param puback_callback is function pointer to function to be called when QOS1 Publish
30 * is acknowledged by server
31 */
32 mqtt_wss_client mqtt_wss_new(
33 msg_callback_fnc_t msg_callback,
34 void (*puback_callback)(uint16_t packet_id));
35
36 void mqtt_wss_set_max_buf_size(mqtt_wss_client client, size_t size);
37
38 void mqtt_wss_destroy(mqtt_wss_client client);
39
40 struct mqtt_connect_params;
41 struct mqtt_wss_proxy;
42
43 #define MQTT_WSS_SSL_CERT_CHECK_FULL 0x00
44 #define MQTT_WSS_SSL_ALLOW_SELF_SIGNED 0x01
45 #define MQTT_WSS_SSL_DONT_CHECK_CERTS 0x08
46
47 /* Will block until the MQTT over WSS connection is established or return error
48 * @param client mqtt_wss_client which should connect
49 * @param host to connect to (where MQTT over WSS server is listening)
50 * @param port to connect to (where MQTT over WSS server is listening)
51 * @param mqtt_params pointer to mqtt_connect_params structure which contains MQTT credentials and settings
52 * @param ssl_flags parameters for OpenSSL, 0=MQTT_WSS_SSL_CERT_CHECK_FULL
53 */
54 int mqtt_wss_connect(
55 mqtt_wss_client client,
56 char *host,
57 int port,
58 struct mqtt_connect_params *mqtt_params,
59 int ssl_flags,
60 const struct mqtt_wss_proxy *proxy,
61 bool *fallback_ipv4);
62 int mqtt_wss_service(mqtt_wss_client client, int t_ms);
63 void mqtt_wss_disconnect(mqtt_wss_client client, int timeout_ms);
64
65 // we redefine this instead of using MQTT-C flags as in future
66 // we want to support different MQTT implementations if needed
67 enum mqtt_wss_publish_flags {
68 MQTT_WSS_PUB_QOS0 = 0x0,
69 MQTT_WSS_PUB_QOS1 = 0x1,
70 MQTT_WSS_PUB_QOS2 = 0x2,
71 MQTT_WSS_PUB_QOSMASK = 0x3,
72 MQTT_WSS_PUB_RETAIN = 0x4
73 };
74
75 struct mqtt_connect_params {
76 const char *clientid;
77 const char *username;
78 const char *password;
79 const char *will_topic;
80 const void *will_msg;
81 enum mqtt_wss_publish_flags will_flags;
82 size_t will_msg_len;
83 int keep_alive;
84 int drop_on_publish_fail;
85 };
86
87 enum mqtt_wss_proxy_type {
88 MQTT_WSS_DIRECT = 0,
89 MQTT_WSS_PROXY_HTTP,
90 MQTT_WSS_PROXY_SOCKS5,
91 MQTT_WSS_PROXY_SOCKS5H
92 };
93
94 struct mqtt_wss_proxy {
95 enum mqtt_wss_proxy_type type;
96 const char *host;
97 int port;
98 const char *username;
99 const char *password;
100 const char *proxy_destination;
101 };
102
103 /* TODO!!! update the description
104 * Publishes MQTT message and gets message id
105 * @param client mqtt_wss_client which should transfer the message
106 * @param topic MQTT topic to publish message to (0 terminated C string)
107 * @param msg Message to be published (no need for 0 termination)
108 * @param msg_len Length of the message to be published
109 * @param publish_flags see enum mqtt_wss_publish_flags e.g. (MQTT_WSS_PUB_QOS1 | MQTT_WSS_PUB_RETAIN)
110 * @param packet_id is 16 bit unsigned int representing ID that can be used to pair with PUBACK callback
111 * for usages where application layer wants to know which messages are delivered when
112 * @return Returns 0 on success
113 */
114 int mqtt_wss_publish5(mqtt_wss_client client,
115 char *topic,
116 free_fnc_t topic_free,
117 void *msg,
118 free_fnc_t msg_free,
119 size_t msg_len,
120 uint8_t publish_flags,
121 uint16_t *packet_id);
122
123 int mqtt_wss_set_topic_alias(mqtt_wss_client client, const char *topic);
124
125 /* Subscribes to MQTT topic
126 * @param client mqtt_wss_client which should do the subscription
127 * @param topic MQTT topic to subscribe to
128 * @param max_qos_level maximum QOS level that broker can send to us on this subscription
129 * @return Returns 0 on success
130 */
131 int mqtt_wss_subscribe(mqtt_wss_client client, char *topic, int max_qos_level);
132
133
134 struct mqtt_wss_stats {
135 uint64_t bytes_tx;
136 uint64_t bytes_rx;
137 #ifdef MQTT_WSS_CPUSTATS
138 uint64_t time_keepalive;
139 uint64_t time_read_socket;
140 uint64_t time_write_socket;
141 uint64_t time_process_websocket;
142 uint64_t time_process_mqtt;
143 #endif
144 struct mqtt_ng_stats mqtt;
145 };
146
147 struct mqtt_wss_stats mqtt_wss_get_stats(mqtt_wss_client client);
148 void mqtt_wss_reset_stats(mqtt_wss_client client);
149
150 #ifdef MQTT_WSS_DEBUG
151 #include <openssl/ssl.h>
152 void mqtt_wss_set_SSL_CTX_keylog_cb(mqtt_wss_client client, void (*ssl_ctx_keylog_cb)(const SSL *ssl, const char *line));
153 #endif
154
155 #endif /* MQTT_WSS_CLIENT_H */