libwebsockets
Lightweight C library for HTML5 websockets
lws-mqtt.h
1 /*
2  * libwebsockets - protocol - mqtt
3  *
4  * Copyright (C) 2010 - 2021 Andy Green <andy@warmcat.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  *
24  * included from libwebsockets.h
25  */
26 
27 #ifndef _LWS_MQTT_H
28 #define _LWS_MQTT_H 1
29 
30 struct _lws_mqtt_related;
31 typedef struct _lws_mqtt_related lws_mqtt_related_t;
32 struct lws_mqtt_str_st;
33 typedef struct lws_mqtt_str_st lws_mqtt_str_t;
34 
35 #define MQTT_VER_3_1_1 4
36 
37 #define LWS_MQTT_FINAL_PART 1
38 
39 #define LWS_MQTT_MAX_AWSIOT_TOPICLEN 256
40 #define LWS_MQTT_MAX_TOPICLEN 65535
41 #define LWS_MQTT_MAX_CIDLEN 128
42 #define LWS_MQTT_RANDOM_CIDLEN 23 /* 3.1.3.1-5: Server MUST... between
43  1 and 23 chars... */
44 
45 typedef enum {
46  QOS0,
47  QOS1,
48  QOS2, /* not supported */
49  RESERVED_QOS_LEVEL,
50  FAILURE_QOS_LEVEL = 0x80
51 } lws_mqtt_qos_levels_t;
52 
53 typedef union {
54  struct {
55  uint8_t retain:1;
56  uint8_t qos:2;
57  uint8_t dup:1;
58  uint8_t ctrl_pkt_type:4;
59  } flags;
60  uint8_t bits;
62 
63 /*
64  * MQTT connection parameters, passed into struct
65  * lws_client_connect_info to establish a connection using
66  * lws_client_connect_via_info().
67 */
68 typedef struct lws_mqtt_client_connect_param_s {
69  const char *client_id; /* Client ID */
70  uint16_t keep_alive; /* MQTT keep alive
71  interval in
72  seconds */
73  uint8_t clean_start; /* MQTT clean
74  session */
75  struct {
76  const char *topic;
77  const char *message;
78  lws_mqtt_qos_levels_t qos;
79  uint8_t retain;
80  } will_param; /* MQTT LWT
81  parameters */
82  const char *username;
83  const char *password;
84  uint8_t aws_iot;
86 
87 /*
88  * MQTT publish parameters
89 */
90 typedef struct lws_mqtt_publish_param_s {
91  char *topic; /* Topic Name */
92  uint16_t topic_len;
93  const void *payload; /* Publish Payload */
94  uint32_t payload_len; /* Size of the
95  complete payload */
96  uint32_t payload_pos; /* where we are in payload */
97  lws_mqtt_qos_levels_t qos;
98 
99  /*--v-Following will be used by LWS-v--*/
100  uint16_t packet_id; /* Packet ID for QoS >
101  0 */
102  uint8_t dup:1; /* Retried PUBLISH,
103  for QoS > 0 */
105 
106 typedef struct topic_elem {
107  const char *name; /* Topic Name */
108  lws_mqtt_qos_levels_t qos; /* Requested QoS */
109 
110  /*--v-Following will be used by LWS-v--*/
111  uint8_t acked;
113 
114 /*
115  * MQTT publish parameters
116 */
117 typedef struct lws_mqtt_subscribe_param_s {
118  uint32_t num_topics; /* Number of topics */
119  lws_mqtt_topic_elem_t *topic; /* Array of topic elements */
120 
121  /*--v-Following will be used by LWS-v--*/
122  uint16_t packet_id;
124 
125 typedef enum {
126  LMQCP_RESERVED,
127  LMQCP_CTOS_CONNECT, /* Connection request */
128  LMQCP_STOC_CONNACK, /* Connection acknowledgment */
129  LMQCP_PUBLISH, /* Publish Message */
130  LMQCP_PUBACK, /* QoS 1: Publish acknowledgment */
131  LMQCP_PUBREC, /* QoS 2.1: Publish received */
132  LMQCP_PUBREL, /* QoS 2.2: Publish release */
133  LMQCP_PUBCOMP, /* QoS 2.3: Publish complete */
134  LMQCP_CTOS_SUBSCRIBE, /* Subscribe request */
135  LMQCP_STOC_SUBACK, /* Subscribe acknowledgment */
136  LMQCP_CTOS_UNSUBSCRIBE, /* Unsubscribe request */
137  LMQCP_STOC_UNSUBACK, /* Unsubscribe acknowledgment */
138  LMQCP_CTOS_PINGREQ, /* PING request */
139  LMQCP_STOC_PINGRESP, /* PONG response */
140  LMQCP_DISCONNECT, /* Disconnect notification */
141  LMQCP_AUTH /* Authentication exchange */
142 } lws_mqtt_control_packet_t;
143 
144 /* flags from byte 8 of C_TO_S CONNECT */
145 typedef enum {
146  LMQCFT_USERNAME = (1 << 7),
147  LMQCFT_PASSWORD = (1 << 6),
148  LMQCFT_WILL_RETAIN = (1 << 5),
149  LMQCFT_WILL_QOS = (1 << 3),
150  LMQCFT_WILL_FLAG = (1 << 2),
151  LMQCFT_CLEAN_START = (1 << 1),
152  LMQCFT_RESERVED = (1 << 0),
153 
154  LMQCFT_WILL_QOS_MASK = (3 << 3),
155 } lws_mqtt_connect_flags_t;
156 
157 /* flags for S_TO_C CONNACK */
158 typedef enum {
159  LMQCFT_SESSION_PRESENT = (1 << 0),
160 } lws_mqtt_connack_flags_t;
161 
162 typedef enum {
163  LMQCP_REASON_SUCCESS = 0x00,
164  LMQCP_REASON_NORMAL_DISCONNECTION = 0x00,
165  LMQCP_REASON_GRANTED_QOS0 = 0x00,
166  LMQCP_REASON_GRANTED_QOS1 = 0x01,
167  LMQCP_REASON_GRANTED_QOS2 = 0x02,
168  LMQCP_REASON_DISCONNECT_WILL = 0x04,
169  LMQCP_REASON_NO_MATCHING_SUBSCRIBER = 0x10,
170  LMQCP_REASON_NO_SUBSCRIPTION_EXISTED = 0x11,
171  LMQCP_REASON_CONTINUE_AUTHENTICATION = 0x18,
172  LMQCP_REASON_RE_AUTHENTICATE = 0x19,
173 
174  LMQCP_REASON_UNSPECIFIED_ERROR = 0x80,
175  LMQCP_REASON_MALFORMED_PACKET = 0x81,
176  LMQCP_REASON_PROTOCOL_ERROR = 0x82,
177  LMQCP_REASON_IMPLEMENTATION_SPECIFIC_ERROR = 0x83,
178 
179  /* Begin - Error codes for CONNACK */
180  LMQCP_REASON_UNSUPPORTED_PROTOCOL = 0x84,
181  LMQCP_REASON_CLIENT_ID_INVALID = 0x85,
182  LMQCP_REASON_BAD_CREDENTIALS = 0x86,
183  LMQCP_REASON_NOT_AUTHORIZED = 0x87,
184  /* End - Error codes for CONNACK */
185 
186  LMQCP_REASON_SERVER_UNAVAILABLE = 0x88,
187  LMQCP_REASON_SERVER_BUSY = 0x89,
188  LMQCP_REASON_BANNED = 0x8a,
189  LMQCP_REASON_SERVER_SHUTTING_DOWN = 0x8b,
190  LMQCP_REASON_BAD_AUTHENTICATION_METHOD = 0x8c,
191  LMQCP_REASON_KEEPALIVE_TIMEOUT = 0x8d,
192  LMQCP_REASON_SESSION_TAKEN_OVER = 0x8e,
193  LMQCP_REASON_TOPIC_FILTER_INVALID = 0x8f,
194  LMQCP_REASON_TOPIC_NAME_INVALID = 0x90,
195  LMQCP_REASON_PACKET_ID_IN_USE = 0x91,
196  LMQCP_REASON_PACKET_ID_NOT_FOUND = 0x92,
197  LMQCP_REASON_MAX_RX_EXCEEDED = 0x93,
198  LMQCP_REASON_TOPIC_ALIAS_INVALID = 0x94,
199  LMQCP_REASON_PACKET_TOO_LARGE = 0x95,
200  LMQCP_REASON_RATELIMIT = 0x96,
201  LMQCP_REASON_QUOTA_EXCEEDED = 0x97,
202  LMQCP_REASON_ADMINISTRATIVE_ACTION = 0x98,
203  LMQCP_REASON_PAYLOAD_FORMAT_INVALID = 0x99,
204  LMQCP_REASON_RETAIN_NOT_SUPPORTED = 0x9a,
205  LMQCP_REASON_QOS_NOT_SUPPORTED = 0x9b,
206  LMQCP_REASON_USE_ANOTHER_SERVER = 0x9c,
207  LMQCP_REASON_SERVER_MOVED = 0x9d,
208  LMQCP_REASON_SHARED_SUBSCRIPTIONS_NOT_SUPPORTED = 0x9e,
209  LMQCP_REASON_CONNECTION_RATE_EXCEEDED = 0x9f,
210  LMQCP_REASON_MAXIMUM_CONNECT_TIME = 0xa0,
211  LMQCP_REASON_SUBSCRIPTION_IDS_NOT_SUPPORTED = 0xa1,
212  LMQCP_REASON_WILDCARD_SUBSCRIPTIONS_NOT_SUPPORTED = 0xa2,
213 } lws_mqtt_reason_t;
214 
215 typedef enum {
216  LMQPROP_INVALID,
217  LMQPROP_PAYLOAD_FORMAT_INDICATOR = 0x01,
218  LMQPROP_MESSAGE_EXPIRY_INTERVAL = 0x02,
219  LMQPROP_CONTENT_TYPE = 0x03,
220  LMQPROP_RESPONSE_TOPIC = 0x08,
221  LMQPROP_CORRELATION_DATA = 0x09,
222  LMQPROP_SUBSCRIPTION_IDENTIFIER = 0x0b,
223  LMQPROP_SESSION_EXPIRY_INTERVAL = 0x11,
224  LMQPROP_ASSIGNED_CLIENT_IDENTIFIER = 0x12,
225  LMQPROP_SERVER_KEEP_ALIVE = 0x13,
226  LMQPROP_AUTHENTICATION_METHOD = 0x15,
227  LMQPROP_AUTHENTICATION_DATA = 0x16,
228  LMQPROP_REQUEST_PROBLEM_INFORMATION = 0x17,
229  LMQPROP_WILL_DELAY_INTERVAL = 0x18,
230  LMQPROP_REQUEST_RESPONSE_INFORMATION = 0x19,
231  LMQPROP_RESPONSE_INFORMATION = 0x1a,
232  LMQPROP_SERVER_REFERENCE = 0x1c,
233  LMQPROP_REASON_STRING = 0x1f,
234  LMQPROP_RECEIVE_MAXIMUM = 0x21,
235  LMQPROP_TOPIC_ALIAS_MAXIMUM = 0x22,
236  LMQPROP_TOPIC_ALIAS = 0x23,
237  LMQPROP_MAXIMUM_QOS = 0x24,
238  LMQPROP_RETAIN_AVAILABLE = 0x25,
239  LMQPROP_USER_PROPERTY = 0x26,
240  LMQPROP_MAXIMUM_PACKET_SIZE = 0x27,
241  LMQPROP_WILDCARD_SUBSCRIPTION_AVAIL = 0x28,
242  LMQPROP_SUBSCRIPTION_IDENTIFIER_AVAIL = 0x29,
243  LMQPROP_SHARED_SUBSCRIPTION_AVAIL = 0x2a
244 } lws_mqtt_property;
245 
246 int
247 lws_read_mqtt(struct lws *wsi, unsigned char *buf, lws_filepos_t len);
248 
249 /* returns 0 if bd1 and bd2 are "the same", that includes empty, else nonzero */
250 LWS_VISIBLE LWS_EXTERN int
251 lws_mqtt_bindata_cmp(const lws_mqtt_str_t *bd1, const lws_mqtt_str_t *bd2);
252 
253 LWS_VISIBLE LWS_EXTERN void
254 lws_mqtt_str_init(lws_mqtt_str_t *s, uint8_t *buf, uint16_t lim, char nf);
255 
256 LWS_VISIBLE LWS_EXTERN lws_mqtt_str_t *
257 lws_mqtt_str_create(uint16_t lim);
258 
259 LWS_VISIBLE LWS_EXTERN lws_mqtt_str_t *
260 lws_mqtt_str_create_init(uint8_t *buf, uint16_t len, uint16_t lim);
261 
262 LWS_VISIBLE LWS_EXTERN lws_mqtt_str_t *
263 lws_mqtt_str_create_cstr_dup(const char *buf, uint16_t lim);
264 
265 LWS_VISIBLE LWS_EXTERN uint8_t *
266 lws_mqtt_str_next(lws_mqtt_str_t *s, uint16_t *budget);
267 
268 LWS_VISIBLE LWS_EXTERN int
269 lws_mqtt_str_advance(lws_mqtt_str_t *s, int n);
270 
271 LWS_VISIBLE LWS_EXTERN void
272 lws_mqtt_str_free(lws_mqtt_str_t **s);
273 
274 
302 LWS_VISIBLE LWS_EXTERN int
303 lws_mqtt_client_send_publish(struct lws *wsi, lws_mqtt_publish_param_t *pub,
304  const void *buf, uint32_t len, int final);
305 
319 LWS_VISIBLE LWS_EXTERN int
320 lws_mqtt_client_send_subcribe(struct lws *wsi, lws_mqtt_subscribe_param_t *sub);
321 
336 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
337 lws_mqtt_client_send_unsubcribe(struct lws *wsi,
338  const lws_mqtt_subscribe_param_t *unsub);
339 
340 #endif /* _LWS_MQTT_H */
Definition: lws-mqtt.h:67
Definition: lws-mqtt.h:89
Definition: lws-mqtt.h:116
Definition: lws-mqtt.h:105
Definition: lws-mqtt.h:52