libwebsockets
Lightweight C library for HTML5 websockets
lws-mqtt.h
Go to the documentation of this file.
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 #define LWS_MQTT_SHADOW_MAX_THING_LEN 128
46 #define LWS_MQTT_SHADOW_MAX_SHADOW_LEN 64
47 #define LWS_MQTT_SHADOW_UPDATE_STR "/update"
48 #define LWS_MQTT_SHADOW_DELETE_STR "/delete"
49 #define LWS_MQTT_SHADOW_GET_STR "/get"
50 #define LWS_MQTT_SHADOW_RESP_ACCEPTED_STR "/accepted"
51 #define LWS_MQTT_SHADOW_RESP_REJECTED_STR "/rejected"
52 #define LWS_MQTT_SHADOW_RESP_DELTA_STR "/delta"
53 #define LWS_MQTT_SHADOW_RESP_DOCUMENT_STR "/documents"
54 #define LWS_MQTT_SHADOW_UPDATE_ACCEPTED_STR LWS_MQTT_SHADOW_UPDATE_STR LWS_MQTT_SHADOW_RESP_ACCEPTED_STR
55 #define LWS_MQTT_SHADOW_UPDATE_REJECTED_STR LWS_MQTT_SHADOW_UPDATE_STR LWS_MQTT_SHADOW_RESP_REJECTED_STR
56 #define LWS_MQTT_SHADOW_UPDATE_DELTA_STR LWS_MQTT_SHADOW_UPDATE_STR LWS_MQTT_SHADOW_RESP_DELTA_STR
57 #define LWS_MQTT_SHADOW_UPDATE_DOCUMENT_STR LWS_MQTT_SHADOW_UPDATE_STR LWS_MQTT_SHADOW_RESP_DOCUMENT_STR
58 #define LWS_MQTT_SHADOW_DELETE_ACCEPTED_STR LWS_MQTT_SHADOW_DELETE_STR LWS_MQTT_SHADOW_RESP_ACCEPTED_STR
59 #define LWS_MQTT_SHADOW_DELETE_REJECTED_STR LWS_MQTT_SHADOW_DELETE_STR LWS_MQTT_SHADOW_RESP_REJECTED_STR
60 #define LWS_MQTT_SHADOW_GET_ACCEPTED_STR LWS_MQTT_SHADOW_GET_STR LWS_MQTT_SHADOW_RESP_ACCEPTED_STR
61 #define LWS_MQTT_SHADOW_GET_REJECTED_STR LWS_MQTT_SHADOW_GET_STR LWS_MQTT_SHADOW_RESP_REJECTED_STR
62 #define LWS_MQTT_SHADOW_PREFIX_FORMAT "$aws/things/%s"
63 #define LWS_MQTT_SHADOW_NAMED_SHADOW_TOPIC_FORMAT LWS_MQTT_SHADOW_PREFIX_FORMAT "/shadow/name/%s%s"
64 #define LWS_MQTT_SHADOW_UNNAMED_SHADOW_TOPIC_FORMAT LWS_MQTT_SHADOW_PREFIX_FORMAT "/shadow%s"
65 #define LWS_MQTT_SHADOW_UNNAMED_TOPIC_MATCH "$aws/things/+/shadow/+"
66 #define LWS_MQTT_SHADOW_NAMED_TOPIC_MATCH "$aws/things/+/shadow/name/+/+"
67 
68 typedef enum {
71  QOS2, /* not supported */
75 
76 typedef union {
77  struct {
78  uint8_t retain:1;
79  uint8_t qos:2;
80  uint8_t dup:1;
81  uint8_t ctrl_pkt_type:4;
82  } flags;
83  uint8_t bits;
85 
86 /*
87  * MQTT connection parameters, passed into struct
88  * lws_client_connect_info to establish a connection using
89  * lws_client_connect_via_info().
90 */
92  const char *client_id; /* Client ID */
93  uint16_t keep_alive; /* MQTT keep alive
94  interval in
95  seconds */
96  uint8_t clean_start:1; /* MQTT clean
97  session */
104  struct {
105  const char *topic;
106  const char *message;
108  uint8_t retain;
109  } will_param; /* MQTT LWT
110  parameters */
111  struct {
112  const char *topic;
113  const char *message;
115  uint8_t retain;
116  } birth_param; /* MQTT Birth
117  parameters */
118  const char *username;
119  const char *password;
122 
123 /*
124  * MQTT publish parameters
125 */
126 typedef struct lws_mqtt_publish_param_s {
127  char *topic; /* Topic Name */
129  const void *payload; /* Publish Payload */
130  uint32_t payload_len; /* Size of the
131  complete payload */
132  uint32_t payload_pos; /* where we are in payload */
134 
135  /*--v-Following will be used by LWS-v--*/
136  uint16_t packet_id; /* Packet ID for QoS >
137  0 */
138  uint8_t dup:1; /* Retried PUBLISH,
139  for QoS > 0 */
140  uint8_t retain:1; /* Retained message */
142 
143 typedef struct topic_elem {
144  const char *name; /* Topic Name */
145  lws_mqtt_qos_levels_t qos; /* Requested QoS */
146 
147  /*--v-Following will be used by LWS-v--*/
150 
151 /*
152  * MQTT publish parameters
153 */
155  uint32_t num_topics; /* Number of topics */
156  lws_mqtt_topic_elem_t *topic; /* Array of topic elements */
157 
158  /*--v-Following will be used by LWS-v--*/
161 
162 typedef enum {
164  LMQCP_CTOS_CONNECT, /* Connection request */
165  LMQCP_STOC_CONNACK, /* Connection acknowledgment */
166  LMQCP_PUBLISH, /* Publish Message */
167  LMQCP_PUBACK, /* QoS 1: Publish acknowledgment */
168  LMQCP_PUBREC, /* QoS 2.1: Publish received */
169  LMQCP_PUBREL, /* QoS 2.2: Publish release */
170  LMQCP_PUBCOMP, /* QoS 2.3: Publish complete */
171  LMQCP_CTOS_SUBSCRIBE, /* Subscribe request */
172  LMQCP_STOC_SUBACK, /* Subscribe acknowledgment */
173  LMQCP_CTOS_UNSUBSCRIBE, /* Unsubscribe request */
174  LMQCP_STOC_UNSUBACK, /* Unsubscribe acknowledgment */
175  LMQCP_CTOS_PINGREQ, /* PING request */
176  LMQCP_STOC_PINGRESP, /* PONG response */
177  LMQCP_DISCONNECT, /* Disconnect notification */
178  LMQCP_AUTH /* Authentication exchange */
180 
181 /* flags from byte 8 of C_TO_S CONNECT */
182 typedef enum {
185  LMQCFT_CLIENT_ID_NOFREE = (1 << 8),
186  /* only the low 8 are standardized and go out in the protocol */
187  LMQCFT_USERNAME = (1 << 7),
188  LMQCFT_PASSWORD = (1 << 6),
189  LMQCFT_WILL_RETAIN = (1 << 5),
190  LMQCFT_WILL_QOS = (1 << 3),
191  LMQCFT_WILL_FLAG = (1 << 2),
192  LMQCFT_CLEAN_START = (1 << 1),
193  LMQCFT_RESERVED = (1 << 0),
194 
195  LMQCFT_WILL_QOS_MASK = (3 << 3),
197 
198 /* flags for S_TO_C CONNACK */
199 typedef enum {
200  LMQCFT_SESSION_PRESENT = (1 << 0),
202 
203 typedef enum {
214 
219 
220  /* Begin - Error codes for CONNACK */
225  /* End - Error codes for CONNACK */
226 
255 
256 typedef enum {
286 
287 int
288 lws_read_mqtt(struct lws *wsi, unsigned char *buf, lws_filepos_t len);
289 
290 /* returns 0 if bd1 and bd2 are "the same", that includes empty, else nonzero */
292 lws_mqtt_bindata_cmp(const lws_mqtt_str_t *bd1, const lws_mqtt_str_t *bd2);
293 
295 lws_mqtt_str_init(lws_mqtt_str_t *s, uint8_t *buf, uint16_t lim, char nf);
296 
299 
302 
304 lws_mqtt_str_create_cstr_dup(const char *buf, uint16_t lim);
305 
308 
311 
314 
315 
345  const void *buf, uint32_t len, int final);
346 
362 
378 lws_mqtt_client_send_unsubcribe(struct lws *wsi,
379  const lws_mqtt_subscribe_param_t *unsub);
380 
381 #endif /* _LWS_MQTT_H */
unsigned short uint16_t
unsigned int uint32_t
#define LWS_EXTERN
unsigned char uint8_t
#define LWS_WARN_UNUSED_RESULT
#define LWS_VISIBLE
unsigned long long lws_filepos_t
struct lws_mqtt_str_st lws_mqtt_str_t
Definition: lws-mqtt.h:33
struct lws_mqtt_client_connect_param_s::@31 birth_param
lws_mqtt_connect_flags_t
Definition: lws-mqtt.h:181
@ LMQCFT_USERNAME
Definition: lws-mqtt.h:186
@ LMQCFT_WILL_QOS_MASK
Definition: lws-mqtt.h:194
@ LMQCFT_PASSWORD
Definition: lws-mqtt.h:187
@ LMQCFT_WILL_FLAG
Definition: lws-mqtt.h:190
@ LMQCFT_CLIENT_ID_NOFREE
Definition: lws-mqtt.h:184
@ LMQCFT_WILL_RETAIN
Definition: lws-mqtt.h:188
@ LMQCFT_RESERVED
Definition: lws-mqtt.h:192
@ LMQCFT_PASSWORD_NOFREE
Definition: lws-mqtt.h:183
@ LMQCFT_WILL_QOS
Definition: lws-mqtt.h:189
@ LMQCFT_CLEAN_START
Definition: lws-mqtt.h:191
@ LMQCFT_USERNAME_NOFREE
Definition: lws-mqtt.h:182
int lws_read_mqtt(struct lws *wsi, unsigned char *buf, lws_filepos_t len)
const char * name
Definition: lws-mqtt.h:143
LWS_VISIBLE LWS_EXTERN lws_mqtt_str_t * lws_mqtt_str_create_init(uint8_t *buf, uint16_t len, uint16_t lim)
struct lws_mqtt_publish_param_s lws_mqtt_publish_param_t
lws_mqtt_topic_elem_t * topic
Definition: lws-mqtt.h:155
lws_mqtt_control_packet_t
Definition: lws-mqtt.h:161
@ LMQCP_PUBREC
Definition: lws-mqtt.h:167
@ LMQCP_STOC_SUBACK
Definition: lws-mqtt.h:171
@ LMQCP_PUBACK
Definition: lws-mqtt.h:166
@ LMQCP_CTOS_CONNECT
Definition: lws-mqtt.h:163
@ LMQCP_STOC_UNSUBACK
Definition: lws-mqtt.h:173
@ LMQCP_CTOS_UNSUBSCRIBE
Definition: lws-mqtt.h:172
@ LMQCP_PUBCOMP
Definition: lws-mqtt.h:169
@ LMQCP_DISCONNECT
Definition: lws-mqtt.h:176
@ LMQCP_STOC_CONNACK
Definition: lws-mqtt.h:164
@ LMQCP_STOC_PINGRESP
Definition: lws-mqtt.h:175
@ LMQCP_RESERVED
Definition: lws-mqtt.h:162
@ LMQCP_AUTH
Definition: lws-mqtt.h:177
@ LMQCP_PUBREL
Definition: lws-mqtt.h:168
@ LMQCP_CTOS_PINGREQ
Definition: lws-mqtt.h:174
@ LMQCP_PUBLISH
Definition: lws-mqtt.h:165
@ LMQCP_CTOS_SUBSCRIBE
Definition: lws-mqtt.h:170
LWS_VISIBLE LWS_EXTERN void lws_mqtt_str_free(lws_mqtt_str_t **s)
LWS_VISIBLE LWS_EXTERN int lws_mqtt_str_advance(lws_mqtt_str_t *s, int n)
struct _lws_mqtt_related lws_mqtt_related_t
Definition: lws-mqtt.h:31
LWS_VISIBLE LWS_EXTERN void lws_mqtt_str_init(lws_mqtt_str_t *s, uint8_t *buf, uint16_t lim, char nf)
lws_mqtt_reason_t
Definition: lws-mqtt.h:202
@ LMQCP_REASON_USE_ANOTHER_SERVER
Definition: lws-mqtt.h:246
@ LMQCP_REASON_TOPIC_NAME_INVALID
Definition: lws-mqtt.h:234
@ LMQCP_REASON_RE_AUTHENTICATE
Definition: lws-mqtt.h:212
@ LMQCP_REASON_SERVER_BUSY
Definition: lws-mqtt.h:227
@ LMQCP_REASON_CONNECTION_RATE_EXCEEDED
Definition: lws-mqtt.h:249
@ LMQCP_REASON_NOT_AUTHORIZED
Definition: lws-mqtt.h:223
@ LMQCP_REASON_RETAIN_NOT_SUPPORTED
Definition: lws-mqtt.h:244
@ LMQCP_REASON_SUBSCRIPTION_IDS_NOT_SUPPORTED
Definition: lws-mqtt.h:251
@ LMQCP_REASON_TOPIC_FILTER_INVALID
Definition: lws-mqtt.h:233
@ LMQCP_REASON_NORMAL_DISCONNECTION
Definition: lws-mqtt.h:204
@ LMQCP_REASON_BAD_CREDENTIALS
Definition: lws-mqtt.h:222
@ LMQCP_REASON_UNSPECIFIED_ERROR
Definition: lws-mqtt.h:214
@ LMQCP_REASON_GRANTED_QOS0
Definition: lws-mqtt.h:205
@ LMQCP_REASON_PACKET_TOO_LARGE
Definition: lws-mqtt.h:239
@ LMQCP_REASON_QUOTA_EXCEEDED
Definition: lws-mqtt.h:241
@ LMQCP_REASON_SESSION_TAKEN_OVER
Definition: lws-mqtt.h:232
@ LMQCP_REASON_PACKET_ID_NOT_FOUND
Definition: lws-mqtt.h:236
@ LMQCP_REASON_PAYLOAD_FORMAT_INVALID
Definition: lws-mqtt.h:243
@ LMQCP_REASON_PACKET_ID_IN_USE
Definition: lws-mqtt.h:235
@ LMQCP_REASON_UNSUPPORTED_PROTOCOL
Definition: lws-mqtt.h:220
@ LMQCP_REASON_NO_MATCHING_SUBSCRIBER
Definition: lws-mqtt.h:209
@ LMQCP_REASON_SERVER_MOVED
Definition: lws-mqtt.h:247
@ LMQCP_REASON_BAD_AUTHENTICATION_METHOD
Definition: lws-mqtt.h:230
@ LMQCP_REASON_DISCONNECT_WILL
Definition: lws-mqtt.h:208
@ LMQCP_REASON_QOS_NOT_SUPPORTED
Definition: lws-mqtt.h:245
@ LMQCP_REASON_TOPIC_ALIAS_INVALID
Definition: lws-mqtt.h:238
@ LMQCP_REASON_KEEPALIVE_TIMEOUT
Definition: lws-mqtt.h:231
@ LMQCP_REASON_MAX_RX_EXCEEDED
Definition: lws-mqtt.h:237
@ LMQCP_REASON_MAXIMUM_CONNECT_TIME
Definition: lws-mqtt.h:250
@ LMQCP_REASON_MALFORMED_PACKET
Definition: lws-mqtt.h:215
@ LMQCP_REASON_NO_SUBSCRIPTION_EXISTED
Definition: lws-mqtt.h:210
@ LMQCP_REASON_CONTINUE_AUTHENTICATION
Definition: lws-mqtt.h:211
@ LMQCP_REASON_SERVER_SHUTTING_DOWN
Definition: lws-mqtt.h:229
@ LMQCP_REASON_GRANTED_QOS1
Definition: lws-mqtt.h:206
@ LMQCP_REASON_SHARED_SUBSCRIPTIONS_NOT_SUPPORTED
Definition: lws-mqtt.h:248
@ LMQCP_REASON_PROTOCOL_ERROR
Definition: lws-mqtt.h:216
@ LMQCP_REASON_SERVER_UNAVAILABLE
Definition: lws-mqtt.h:226
@ LMQCP_REASON_IMPLEMENTATION_SPECIFIC_ERROR
Definition: lws-mqtt.h:217
@ LMQCP_REASON_BANNED
Definition: lws-mqtt.h:228
@ LMQCP_REASON_ADMINISTRATIVE_ACTION
Definition: lws-mqtt.h:242
@ LMQCP_REASON_RATELIMIT
Definition: lws-mqtt.h:240
@ LMQCP_REASON_GRANTED_QOS2
Definition: lws-mqtt.h:207
@ LMQCP_REASON_SUCCESS
Definition: lws-mqtt.h:203
@ LMQCP_REASON_CLIENT_ID_INVALID
Definition: lws-mqtt.h:221
@ LMQCP_REASON_WILDCARD_SUBSCRIPTIONS_NOT_SUPPORTED
Definition: lws-mqtt.h:252
uint8_t acked
Definition: lws-mqtt.h:147
struct topic_elem lws_mqtt_topic_elem_t
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT lws_mqtt_client_send_unsubcribe(struct lws *wsi, const lws_mqtt_subscribe_param_t *unsub)
LWS_VISIBLE LWS_EXTERN int lws_mqtt_client_send_publish(struct lws *wsi, lws_mqtt_publish_param_t *pub, const void *buf, uint32_t len, int final)
LWS_VISIBLE LWS_EXTERN uint8_t * lws_mqtt_str_next(lws_mqtt_str_t *s, uint16_t *budget)
lws_mqtt_qos_levels_t qos
Definition: lws-mqtt.h:144
lws_mqtt_connack_flags_t
Definition: lws-mqtt.h:198
@ LMQCFT_SESSION_PRESENT
Definition: lws-mqtt.h:199
struct lws_mqtt_client_connect_param_s::@30 will_param
lws_mqtt_property
Definition: lws-mqtt.h:255
@ LMQPROP_RESPONSE_INFORMATION
Definition: lws-mqtt.h:271
@ LMQPROP_RETAIN_AVAILABLE
Definition: lws-mqtt.h:278
@ LMQPROP_RECEIVE_MAXIMUM
Definition: lws-mqtt.h:274
@ LMQPROP_AUTHENTICATION_DATA
Definition: lws-mqtt.h:267
@ LMQPROP_RESPONSE_TOPIC
Definition: lws-mqtt.h:260
@ LMQPROP_SERVER_KEEP_ALIVE
Definition: lws-mqtt.h:265
@ LMQPROP_PAYLOAD_FORMAT_INDICATOR
Definition: lws-mqtt.h:257
@ LMQPROP_REQUEST_RESPONSE_INFORMATION
Definition: lws-mqtt.h:270
@ LMQPROP_REQUEST_PROBLEM_INFORMATION
Definition: lws-mqtt.h:268
@ LMQPROP_CORRELATION_DATA
Definition: lws-mqtt.h:261
@ LMQPROP_ASSIGNED_CLIENT_IDENTIFIER
Definition: lws-mqtt.h:264
@ LMQPROP_MESSAGE_EXPIRY_INTERVAL
Definition: lws-mqtt.h:258
@ LMQPROP_INVALID
Definition: lws-mqtt.h:256
@ LMQPROP_MAXIMUM_QOS
Definition: lws-mqtt.h:277
@ LMQPROP_SUBSCRIPTION_IDENTIFIER
Definition: lws-mqtt.h:262
@ LMQPROP_SESSION_EXPIRY_INTERVAL
Definition: lws-mqtt.h:263
@ LMQPROP_SERVER_REFERENCE
Definition: lws-mqtt.h:272
@ LMQPROP_MAXIMUM_PACKET_SIZE
Definition: lws-mqtt.h:280
@ LMQPROP_WILDCARD_SUBSCRIPTION_AVAIL
Definition: lws-mqtt.h:281
@ LMQPROP_AUTHENTICATION_METHOD
Definition: lws-mqtt.h:266
@ LMQPROP_CONTENT_TYPE
Definition: lws-mqtt.h:259
@ LMQPROP_SUBSCRIPTION_IDENTIFIER_AVAIL
Definition: lws-mqtt.h:282
@ LMQPROP_TOPIC_ALIAS_MAXIMUM
Definition: lws-mqtt.h:275
@ LMQPROP_SHARED_SUBSCRIPTION_AVAIL
Definition: lws-mqtt.h:283
@ LMQPROP_TOPIC_ALIAS
Definition: lws-mqtt.h:276
@ LMQPROP_WILL_DELAY_INTERVAL
Definition: lws-mqtt.h:269
@ LMQPROP_REASON_STRING
Definition: lws-mqtt.h:273
@ LMQPROP_USER_PROPERTY
Definition: lws-mqtt.h:279
LWS_VISIBLE LWS_EXTERN lws_mqtt_str_t * lws_mqtt_str_create(uint16_t lim)
LWS_VISIBLE LWS_EXTERN int lws_mqtt_client_send_subcribe(struct lws *wsi, lws_mqtt_subscribe_param_t *sub)
LWS_VISIBLE LWS_EXTERN lws_mqtt_str_t * lws_mqtt_str_create_cstr_dup(const char *buf, uint16_t lim)
LWS_VISIBLE LWS_EXTERN int lws_mqtt_bindata_cmp(const lws_mqtt_str_t *bd1, const lws_mqtt_str_t *bd2)
lws_mqtt_qos_levels_t
Definition: lws-mqtt.h:67
@ QOS2
Definition: lws-mqtt.h:70
@ QOS1
Definition: lws-mqtt.h:69
@ RESERVED_QOS_LEVEL
Definition: lws-mqtt.h:71
@ QOS0
Definition: lws-mqtt.h:68
@ FAILURE_QOS_LEVEL
Definition: lws-mqtt.h:72
struct lws_mqtt_client_connect_param_s lws_mqtt_client_connect_param_t
const void * payload
Definition: lws-mqtt.h:128
lws_mqtt_qos_levels_t qos
Definition: lws-mqtt.h:132
struct lws_mqtt_subscribe_param_s lws_mqtt_subscribe_param_t