-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt.h
268 lines (233 loc) · 8.04 KB
/
mqtt.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* libemqtt.h */
/*
* This file was forked from libemqtt,
* developed by Vicente Ruiz Rodríguez.
* https://github.com/menudoproblema/libemqtt
*
*/
#ifndef __LIBEMQTT_H__
#define __LIBEMQTT_H__
#include <stdint.h>
#ifndef MQTT_CONF_USERNAME_LENGTH
#define MQTT_CONF_USERNAME_LENGTH 50
#endif
#ifndef MQTT_CONF_PASSWORD_LENGTH
#define MQTT_CONF_PASSWORD_LENGTH 40
#endif
#define MQTT_MSG_CONNECT 1<<4
#define MQTT_MSG_CONNACK 2<<4
#define MQTT_MSG_PUBLISH 3<<4
#define MQTT_MSG_PUBACK 4<<4
#define MQTT_MSG_PUBREC 5<<4
#define MQTT_MSG_PUBREL 6<<4
#define MQTT_MSG_PUBCOMP 7<<4
#define MQTT_MSG_SUBSCRIBE 8<<4
#define MQTT_MSG_SUBACK 9<<4
#define MQTT_MSG_UNSUBSCRIBE 10<<4
#define MQTT_MSG_UNSUBACK 11<<4
#define MQTT_MSG_PINGREQ 12<<4
#define MQTT_MSG_PINGRESP 13<<4
#define MQTT_MSG_DISCONNECT 14<<4
/** Extract the message type from buffer.
* @param buffer Pointer to the packet.
*
* @return Message Type byte.
*/
#define MQTTParseMessageType(buffer) ( *buffer & 0xF0 )
/** Indicate if it is a duplicate packet.
* @param buffer Pointer to the packet.
*
* @retval 0 Not duplicate.
* @retval !=0 Duplicate.
*/
#define MQTTParseMessageDuplicate(buffer) ( *buffer & 0x08 )
/** Extract the message QoS level.
* @param buffer Pointer to the packet.
*
* @return QoS Level (0, 1 or 2).
*/
#define MQTTParseMessageQos(buffer) ( (*buffer & 0x06) >> 1 )
/** Indicate if this packet has a retain flag.
* @param buffer Pointer to the packet.
*
* @retval 0 Not duplicate.
* @retval !=0 Duplicate.
*/
#define MQTTParseMessageRetain(buffer) ( *buffer & 0x01 )
/** Parse packet buffer for number of bytes in remaining length field.
*
* Given a packet, return number of bytes in remaining length
* field in MQTT fixed header. Can be from 1 - 4 bytes depending
* on length of message.
*
* @param buf Pointer to the packet.
*
* @retval number of bytes
*/
uint8_t mqtt_num_rem_len_bytes(const uint8_t* buf);
/** Parse packet buffer for remaning length value.
*
* Given a packet, return remaining length value (in fixed header).
*
* @param buf Pointer to the packet.
*
* @retval remaining length
*/
uint16_t mqtt_parse_rem_len(const uint8_t* buf);
/** Parse packet buffer for message id.
*
* @param buf Pointer to the packet.
*
* @retval message id
*/
uint8_t mqtt_parse_msg_id(const uint8_t* buf);
/** Parse a packet buffer for the publish topic.
*
* Given a packet containing an MQTT publish message,
* return the message topic.
*
* @param buf Pointer to the packet.
* @param topic Pointer destination buffer for topic
*
* @retval size in bytes of topic (0 = no publish message in buffer)
*/
uint16_t mqtt_parse_pub_topic(const uint8_t* buf, uint8_t* topic);
/** Parse a packet buffer for a pointer to the publish topic.
*
* Not called directly - called by mqtt_parse_pub_topic
*/
uint16_t mqtt_parse_pub_topic_ptr(const uint8_t* buf, const uint8_t** topic_ptr);
/** Parse a packet buffer for the publish message.
*
* Given a packet containing an MQTT publish message,
* return the message.
*
* @param buf Pointer to the packet.
* @param msg Pointer destination buffer for message
*
* @retval size in bytes of topic (0 = no publish message in buffer)
*/
uint16_t mqtt_parse_publish_msg(const uint8_t* buf, uint8_t* msg);
/** Parse a packet buffer for a pointer to the publish message.
*
* Not called directly - called by mqtt_parse_pub_msg
*/
uint16_t mqtt_parse_pub_msg_ptr(const uint8_t* buf, const uint8_t** msg_ptr);
typedef struct {
void* socket_info;
int (*send)(void* socket_info, const void* buf, unsigned int count);
// Connection info
char clientid[50];
// Auth fields
char username[MQTT_CONF_USERNAME_LENGTH];
char password[MQTT_CONF_PASSWORD_LENGTH];
// Will topic
uint8_t will_retain;
uint8_t will_qos;
uint8_t clean_session;
// Management fields
uint16_t seq;
uint16_t alive;
} mqtt_broker_handle_t;
/** Initialize the information to connect to the broker.
* @param broker Data structure that contains the connection information with the broker.
* @param clientid A string that identifies the client id.
*
* @note Only has effect before to call mqtt_connect
*/
void mqtt_init(mqtt_broker_handle_t* broker, const char* clientid);
/** Enable the authentication to connect to the broker.
* @param broker Data structure that contains the connection information with the broker.
* @param username A string that contains the username.
* @param password A string that contains the password.
*
* @note Only has effect before to call mqtt_connect
*/
void mqtt_init_auth(mqtt_broker_handle_t* broker, const char* username, const char* password);
/** Set the keep alive timer.
* @param broker Data structure that contains the connection information with the broker.
* @param alive Keep aliver timer value (in seconds).
*
* @note Only has effect before to call mqtt_connect
*/
void mqtt_set_alive(mqtt_broker_handle_t* broker, uint16_t alive);
/** Connect to the broker.
* @param broker Data structure that contains the connection information with the broker.
*
* @retval 1 On success.
* @retval 0 On connection error.
* @retval -1 On IO error.
*/
int mqtt_connect(mqtt_broker_handle_t* broker);
/** Disconnect to the broker.
* @param broker Data structure that contains the connection information with the broker.
*
* @note The socket must also be closed.
*
* @retval 1 On success.
* @retval 0 On connection error.
* @retval -1 On IO error.
*/
int mqtt_disconnect(mqtt_broker_handle_t* broker);
/** Publish a message on a topic. This message will be published with 0 Qos level.
* @param broker Data structure that contains the connection information with the broker.
* @param topic The topic name.
* @param msg The message.
* @param retain Enable or disable the Retain flag (values: 0 or 1).
*
* @retval 1 On success.
* @retval 0 On connection error.
* @retval -1 On IO error.
*/
int mqtt_publish(mqtt_broker_handle_t* broker, const char* topic, const char* msg, uint8_t retain);
/** Publish a message on a topic.
* @param broker Data structure that contains the connection information with the broker.
* @param topic The topic name.
* @param msg The message.
* @param retain Enable or disable the Retain flag (values: 0 or 1).
* @param qos Quality of Service (values: 0, 1 or 2)
* @param message_id Variable that will store the Message ID, if the pointer is not NULL.
*
* @retval 1 On success.
* @retval 0 On connection error.
* @retval -1 On IO error.
*/
int mqtt_publish_with_qos(mqtt_broker_handle_t* broker, const char* topic, const char* msg, uint8_t retain, uint8_t qos, uint16_t* message_id);
/** Send a PUBREL message. It's used for PUBLISH message with 2 QoS level.
* @param broker Data structure that contains the connection information with the broker.
* @param message_id Message ID
*
* @retval 1 On success.
* @retval 0 On connection error.
* @retval -1 On IO error.
*/
int mqtt_pubrel(mqtt_broker_handle_t* broker, uint16_t message_id);
/** Subscribe to a topic.
* @param broker Data structure that contains the connection information with the broker.
* @param topic The topic name.
* @param message_id Variable that will store the Message ID, if the pointer is not NULL.
*
* @retval 1 On success.
* @retval 0 On connection error.
* @retval -1 On IO error.
*/
int mqtt_subscribe(mqtt_broker_handle_t* broker, const char* topic, uint16_t* message_id);
/** Unsubscribe from a topic.
* @param broker Data structure that contains the connection information with the broker.
* @param topic The topic name.
* @param message_id Variable that will store the Message ID, if the pointer is not NULL.
*
* @retval 1 On success.
* @retval 0 On connection error.
* @retval -1 On IO error.
*/
int mqtt_unsubscribe(mqtt_broker_handle_t* broker, const char* topic, uint16_t* message_id);
/** Make a ping.
* @param broker Data structure that contains the connection information with the broker.
*
* @retval 1 On success.
* @retval 0 On connection error.
* @retval -1 On IO error.
*/
int mqtt_ping(mqtt_broker_handle_t* broker);
#endif // __LIBEMQTT_H__