1: #ifndef __AITMQTT_H
2: #define __AITMQTT_H
3:
4:
5: /* FIXED HEADER */
6:
7: struct mqtthdr {
8: union {
9: struct {
10: unsigned char retain:1,
11: qos:2,
12: dup:1,
13: type:4;
14: };
15: unsigned char val;
16: } mqtt_msg;
17: unsigned char mqtt_len[1]; /* may be grow to 4 bytes */
18: } __packed;
19: #define MQTTHDR_MSGINIT(x) (assert((x)), (x)->mqtt_msg.val ^= (x)->mqtt_msg.val)
20:
21: #define MQTT_TYPE_UNKNOWN 0 /* reserved */
22: #define MQTT_TYPE_CONNECT 1 /* client request to connect to server */
23: #define MQTT_TYPE_CONNACK 2 /* connect acknowledgment */
24: #define MQTT_TYPE_PUBLISH 3 /* publish message */
25: #define MQTT_TYPE_PUBACK 4 /* publish acknowledgment */
26: #define MQTT_TYPE_PUBREC 5 /* publish received (assured delivery part 1) */
27: #define MQTT_TYPE_PUBREL 6 /* publish release (assured delivery part 2) */
28: #define MQTT_TYPE_PUBCOMP 7 /* publish complete (assured delivery part 3) */
29: #define MQTT_TYPE_SUBSCRIBE 8 /* client subscribe request */
30: #define MQTT_TYPE_SUBACK 9 /* subscribe acknowledgment */
31: #define MQTT_TYPE_UNSUBSCRIBE 10 /* client unsubscribe request */
32: #define MQTT_TYPE_UNSUBACK 11 /* unsubscribe acknowledgment */
33: #define MQTT_TYPE_PINGREQ 12 /* PING request */
34: #define MQTT_TYPE_PINGRESP 13 /* PING response */
35: #define MQTT_TYPE_DISCONNECT 14 /* client is disconnecting */
36: #define MQTT_TYPE_MAX 15 /* reserved */
37:
38: #define MQTT_FLAG_DUP 1 /* This flag is set when the client or server attempts to re-deliver
39: a PUBLISH, PUBREL, SUBSCRIBE or UNSUBSCRIBE message.
40: This applies to messages where the value of QoS is greater than
41: zero (0), and an acknowledgment is required.
42: When the DUP bit is set, the variable header includes a Message ID.
43:
44: The recipient should treat this flag as a hint as to whether
45: the message may have been previously received.
46: It should not be relied on to detect duplicates. */
47:
48: #define MQTT_QOS_ONCE 0 /* At most once, Fire and Forget, <=1 */
49: #define MQTT_QOS_ACK 1 /* At least once, Acknowledged delivery, >=1 */
50: #define MQTT_QOS_EXACTLY 2 /* Exactly once, Assured delivery, =1 */
51: #define MQTT_QOS_RESERVED 3 /* reserved */
52:
53: #define MQTT_FLAG_RETAIN 1 /* This flag is only used on PUBLISH messages.
54:
55: When a client sends a PUBLISH to a server,
56: if the Retain flag is set (1),
57: the server should hold on to the message after it has been
58: delivered to the current subscribers.
59: When a new subscription is established on a topic,
60: the last retained message on that topic should be sent to
61: the subscriber with the Retain flag set.
62: If there is no retained message, nothing is sent
63: This is useful where publishers send messages on a
64: "report by exception" basis, where it might be some time between messages.
65: This allows new subscribers to instantly receive data with the retained,
66: or Last Known Good, value.
67:
68: When a server sends a PUBLISH to a client as a result of
69: a subscription that already existed when the original PUBLISH arrived,
70: the Retain flag should not be set, regardless of the Retain flag
71: of the original PUBLISH. This allows a client to distinguish messages
72: that are being received because they were retained and those
73: that are being received "live".
74:
75: Retained messages should be kept over restarts of the server.
76: A server may delete a retained message if it receives a message
77: with a zero-length payload and the Retain flag set on the same topic. */
78:
79: /* VARIABLE HEADERS */
80:
81: #define MQTT_RETCODE_ACCEPTED 0
82: #define MQTT_RETCODE_REFUSE_VER 1
83: #define MQTT_RETCODE_REFUSE_ID 2
84: #define MQTT_RETCODE_REFUSE_UNAVAIL 3
85: #define MQTT_RETCODE_REFUSE_USERPASS 4
86: #define MQTT_RETCODE_DENIED 5
87:
88:
89: typedef union {
90: struct {
91: unsigned short m:8,
92: l:8;
93: } sb;
94: unsigned short val;
95: } mqtt_v_t;
96:
97: typedef struct {
98: unsigned char sub_ret;
99: struct __sbuf sub_topic;
100: struct __sbuf sub_value;
101: } mqtt_subscr_t;
102:
103: typedef struct {
104: mqtt_v_t var_sb;
105: unsigned char var_data[0];
106: } __packed mqtthdr_var_t;
107: #define MQTTHDR_VAR_SIZEOF(x) (assert((x)), sizeof(mqtt_v_t) + ntohs((x)->var_sb.val))
108:
109: typedef unsigned char mqtthdr_protover_t;
110:
111: typedef union {
112: struct {
113: unsigned char reserved:1,
114: clean_sess:1,
115: will_flg:1,
116: will_qos:2,
117: will_retain:1,
118: password:1,
119: username:1;
120: };
121: unsigned char flags;
122: } __packed mqtthdr_connflgs_t;
123:
124: typedef struct {
125: unsigned char reserved;
126: unsigned char retcode;
127: } __packed mqtthdr_connack_t;
128:
129:
130: /* MQTT Message buffer */
131:
132: typedef struct {
133: void *msg_base;
134: unsigned short msg_len;
135: } mqtt_msg_t;
136:
137: /* MQTT dispatcher callbacks */
138:
139: typedef int (*mqtt_cb_t)(void *);
140:
141:
142: // -------------------------------------------------------
143: // mqtt_GetErrno() Get error code of last operation
144: inline int mqtt_GetErrno();
145: // mqtt_GetError() Get error text of last operation
146: inline const char *mqtt_GetError();
147: // -------------------------------------------------------
148:
149:
150: /*
151: * mqtt_msgAlloc() Allocate memory for MQTT Message
152: *
153: * @len = >0 Allocate buffer with length
154: * return: NULL error or Message, after use must call mqtt_msgFree() with all!=0
155: */
156: inline mqtt_msg_t *mqtt_msgAlloc(unsigned short len);
157: /*
158: * mqtt_msgFree() Free MQTT message
159: *
160: * @msg = Message buffer
161: * @all = !=0 Destroy entire message, if MQTT Message allocated with mqtt_msgAlloc()
162: * return: none
163: */
164: inline void mqtt_msgFree(mqtt_msg_t ** __restrict msg, int all);
165: /*
166: * mqtt_msgRealloc() Reallocate MQTT message buffer
167: *
168: * @msg = MQTT message
169: * @len = new length
170: * return: -1 error or >-1 old buffer length
171: */
172: inline int mqtt_msgRealloc(mqtt_msg_t * __restrict msg, unsigned short len);
173:
174: /*
175: * mqtt_encodeLen() Encode number to MQTT length field
176: *
177: * @num = number for encode
178: * return: -1 error or >-1 length
179: */
180: inline unsigned int mqtt_encodeLen(unsigned int num);
181: /*
182: * mqtt_decodeLen() Decode length from MQTT packet
183: *
184: * @len = length from MQTT header
185: * @n = sizeof bytes, if !=NULL
186: * return: -1 error, >-1 length of message
187: */
188: inline unsigned int mqtt_decodeLen(void * __restrict len, int * __restrict n);
189: /*
190: * mqtt_sizeLen Return sizeof len field
191: *
192: * @len = length
193: * return: -1 error, >-1 sizeof len in bytes
194: */
195: inline char mqtt_sizeLen(unsigned int len);
196: /*
197: * mqtt_str2sub Create MQTT subscribe variable from string(s)
198: *
199: * @csStr = strings
200: * @strnum = number of strings elements
201: * @qoses = QoS elements applied to subscribe variable,
202: * count of elements must be equal with csStr elements
203: * return: NULL error or != subscribe variables array, must be free after use with mqtt_freeSub()
204: */
205: inline mqtt_subscr_t *mqtt_str2sub(const char **csStr, unsigned short strnum, unsigned char *qoses);
206: /*
207: * mqtt_subFree() Free array from subscribe variables
208: *
209: * @subs = Subscribe variables
210: * return: none
211: */
212: inline void mqtt_subFree(mqtt_subscr_t ** __restrict subs);
213: /*
214: * mqtt_subAlloc() Create array from subscribe variables
215: *
216: * @num = Number of elements
217: * return: NULL error or subscribe array, after use must call mqtt_subFree()
218: */
219: inline mqtt_subscr_t *mqtt_subAlloc(unsigned short num);
220: /*
221: * mqtt_subRealloc() Reallocate array from subscribe variables
222: *
223: * @subs = Subscribe array
224: * @num = Number of elements
225: * return: NULL error or subscribe array, after use must call mqtt_subFree()
226: */
227: inline mqtt_subscr_t *mqtt_subRealloc(mqtt_subscr_t * __restrict subs, unsigned short num);
228:
229:
230: /*** SENDER FUNCTIONS ***/
231:
232: /*
233: * mqtt_msgCONNECT() Create CONNECT message
234: *
235: * @buf = Message buffer
236: * @csConnID = ConnectID
237: * @csUser = Username if !=NULL
238: * @csPass = Password for Username, only if csUser is set
239: * @csWillTopic = Will Topic if !=NULL Will Flags set into message
240: * @csWillMessage = Will Message, may be NULL
241: * @ClrSess = Clear Session subscriptions after disconnect
242: * @WillQOS = Will QOS if csWillTopic is set
243: * @WillRetain = Will Retain Will Message if csWillTopic is set
244: * return: -1 error or >-1 message size for send
245: */
246: int mqtt_msgCONNECT(mqtt_msg_t * __restrict buf, const char *csConnID,
247: const char *csUser, const char *csPass,
248: const char *csWillTopic, const char *csWillMessage,
249: unsigned char ClrSess, unsigned char WillQOS, unsigned char WillRetain);
250: /*
251: * mqtt_msgCONNACK() Create CONNACK message
252: *
253: * @buf = Message buffer
254: * @retcode = Return code
255: * return: -1 error or >-1 message size for send
256: */
257: int mqtt_msgCONNACK(mqtt_msg_t * __restrict buf, unsigned char retcode);
258: /*
259: * mqtt_msgDISCONNECT() Create DISCONNECT message
260: *
261: * @buf = Message buffer
262: * return: -1 error or >-1 message size for send
263: */
264: int mqtt_msgDISCONNECT(mqtt_msg_t * __restrict buf);
265: /*
266: * mqtt_msgPINGREQ() Create PINGREQ message
267: *
268: * @buf = Message buffer
269: * return: -1 error or >-1 message size for send
270: */
271: int mqtt_msgPINGREQ(mqtt_msg_t * __restrict buf);
272: /*
273: * mqtt_msgPINGRESP() Create PINGRESP message
274: *
275: * @buf = Message buffer
276: * return: -1 error or >-1 message size for send
277: */
278: int mqtt_msgPINGRESP(mqtt_msg_t * __restrict buf);
279:
280: /*
281: * mqtt_msgPUBLISH() Create PUBLISH message
282: *
283: * @buf = Message buffer
284: * @csTopic = Publish topic
285: * @msgID = MessageID >0, if QOS != MQTT_QOS_ONCE
286: * @Dup = Duplicate message
287: * @QOS = QoS
288: * @Retain = Retain message
289: * @pData = Publish data into topic
290: * @datlen = Publish data length
291: * return: -1 error or >-1 message size for send
292: */
293: int mqtt_msgPUBLISH(mqtt_msg_t * __restrict buf, const char *csTopic,
294: unsigned short msgID, unsigned char Dup, unsigned char QOS,
295: unsigned char Retain, const void *pData, int datlen);
296: /*
297: * mqtt_msgPUBACK() Create PUBACK message
298: *
299: * @buf = Message buffer
300: * @msgID = MessageID
301: * return: -1 error or >-1 message size for send
302: */
303: inline int mqtt_msgPUBACK(mqtt_msg_t * __restrict buf, unsigned short msgID);
304: /*
305: * mqtt_msgPUBREC() Create PUBREC message
306: *
307: * @buf = Message buffer
308: * @msgID = MessageID
309: * return: -1 error or >-1 message size for send
310: */
311: inline int mqtt_msgPUBREC(mqtt_msg_t * __restrict buf, unsigned short msgID);
312: /*
313: * mqtt_msgPUBREL() Create PUBREL message
314: *
315: * @buf = Message buffer
316: * @msgID = MessageID
317: * return: -1 error or >-1 message size for send
318: */
319: inline int mqtt_msgPUBREL(mqtt_msg_t * __restrict buf, unsigned short msgID);
320: /*
321: * mqtt_msgPUBCOMP() Create PUBCOMP message
322: *
323: * @buf = Message buffer
324: * @msgID = MessageID
325: * return: -1 error or >-1 message size for send
326: */
327: inline int mqtt_msgPUBCOMP(mqtt_msg_t * __restrict buf, unsigned short msgID);
328:
329: /*
330: * mqtt_msgSUBSCRIBE() Create SUBSCRIBE message
331: *
332: * @buf = Message buffer
333: * @Topics = MQTT subscription topics
334: * @msgID = MessageID
335: * @Dup = Duplicate message
336: * @QOS = QoS
337: * return: -1 error or >-1 message size for send
338: */
339: int
340: mqtt_msgSUBSCRIBE(mqtt_msg_t * __restrict buf, mqtt_subscr_t * __restrict Topics,
341: unsigned short msgID, unsigned char Dup, unsigned char QOS);
342: /*
343: * mqtt_msgSUBACK() Create SUBACK message
344: *
345: * @buf = Message buffer
346: * @Topics = MQTT subscription topics
347: * @msgID = MessageID
348: * return: -1 error or >-1 message size for send
349: */
350: int mqtt_msgSUBACK(mqtt_msg_t * __restrict buf, mqtt_subscr_t * __restrict Topics,
351: unsigned short msgID);
352: /*
353: * mqtt_msgUNSUBSCRIBE() Create UNSUBSCRIBE message
354: *
355: * @buf = Message buffer
356: * @Topics = MQTT subscription topics
357: * @msgID = MessageID
358: * @Dup = Duplicate message
359: * @QOS = QoS
360: * return: -1 error or >-1 message size for send
361: */
362: int
363: mqtt_msgUNSUBSCRIBE(mqtt_msg_t * __restrict buf, mqtt_subscr_t * __restrict Topics,
364: unsigned short msgID, unsigned char Dup, unsigned char QOS);
365: /*
366: * mqtt_msgUNSUBACK() Create UNSUBACK message
367: *
368: * @buf = Message buffer
369: * @msgID = MessageID
370: * return: -1 error or >-1 message size for send
371: */
372: int mqtt_msgUNSUBACK(mqtt_msg_t * __restrict buf, unsigned short msgID);
373:
374:
375: /*** RECEIVER FUNCTIONS ***/
376:
377: /*
378: * mqtt_readCONNECT() Read elements from CONNECT message
379: *
380: * @buf = Message buffer
381: * @kasec = Keep Alive in seconds for current connection
382: * @psConnID = ConnectID
383: * @connLen = ConnectID length
384: * @psUser = Username if !=NULL
385: * @userLen = Username length
386: * @psPass = Password for Username, only if csUser is set
387: * @passLen = Password length
388: * @psWillTopic = Will Topic if !=NULL Will Flags set into message and must be free()
389: * @psWillMessage = Will Message, may be NULL if !NULL must be free() after use!
390: * return: .reserved == 1 is error or == 0 connection flags & msg ok
391: */
392: mqtthdr_connack_t mqtt_readCONNECT(mqtt_msg_t * __restrict buf, unsigned short *kasec,
393: char * __restrict psConnID, int connLen,
394: char * __restrict psUser, int userLen, char * __restrict psPass, int passLen,
395: char ** __restrict psWillTopic, char ** __restrict psWillMessage);
396: /*
397: * mqtt_readCONNACK() Read CONNACK message
398: *
399: * @buf = Message buffer
400: * return: -1 error or >-1 CONNECT message return code
401: */
402: unsigned char mqtt_readCONNACK(mqtt_msg_t * __restrict buf);
403: /*
404: * mqtt_readDISCONNECT() Read DISCONNECT message
405: *
406: * @buf = Message buffer
407: * return: -1 error, 0 ok, >0 undefined result
408: */
409: int mqtt_readDISCONNECT(mqtt_msg_t * __restrict buf);
410: /*
411: * mqtt_readPINGREQ() Read PINGREQ message
412: *
413: * @buf = Message buffer
414: * return: -1 error, 0 ok, >0 undefined result
415: */
416: int mqtt_readPINGREQ(mqtt_msg_t * __restrict buf);
417: /*
418: * mqtt_readPINGRESP() Read PINGRESP message
419: *
420: * @buf = Message buffer
421: * return: -1 error, 0 ok, >0 undefined result
422: */
423: int mqtt_readPINGRESP(mqtt_msg_t * __restrict buf);
424:
425: /*
426: * mqtt_readPUBLISH() Read PUBLISH message
427: *
428: * @buf = Message buffer
429: * @psTopic = Topic
430: * @topicLen = Topic length
431: * @msgID = MessageID
432: * @pData = Data buffer
433: * @datLen = Data buffer length, if *datLen == 0 allocate memory for pData
434: * return: NULL error or !=NULL MQTT fixed header
435: */
436: struct mqtthdr *mqtt_readPUBLISH(mqtt_msg_t * __restrict buf, char * __restrict psTopic,
437: int topicLen, unsigned short *msgID, void * __restrict pData, int *datLen);
438: /*
439: * mqtt_readPUBACK() Read PUBACK message
440: *
441: * @buf = Message buffer
442: * return: -1 error or MessageID
443: */
444: u_short mqtt_readPUBACK(mqtt_msg_t * __restrict buf);
445: /*
446: * mqtt_readPUBREC() Read PUBREC message
447: *
448: * @buf = Message buffer
449: * return: -1 error or MessageID
450: */
451: u_short mqtt_readPUBREC(mqtt_msg_t * __restrict buf);
452: /*
453: * mqtt_readPUBREL() Read PUBREL message
454: *
455: * @buf = Message buffer
456: * return: -1 error or MessageID
457: */
458: u_short mqtt_readPUBREL(mqtt_msg_t * __restrict buf);
459: /*
460: * mqtt_readPUBCOMP() Read PUBCOMP message
461: *
462: * @buf = Message buffer
463: * return: -1 error or MessageID
464: */
465: u_short mqtt_readPUBCOMP(mqtt_msg_t * __restrict buf);
466:
467: /*
468: * mqtt_readSUBSCRIBE() Read SUBSCRIBE message
469: *
470: * @buf = Message buffer
471: * @msgID = MessageID
472: * @subscr = Subscriptions, must be free after use with mqtt_subFree()
473: * return: NULL error or !=NULL MQTT fixed header
474: */
475: struct mqtthdr *mqtt_readSUBSCRIBE(mqtt_msg_t * __restrict buf, unsigned short *msgID,
476: mqtt_subscr_t **subscr);
477: /*
478: * mqtt_readSUBACK() Read SUBACK message
479: *
480: * @buf = Message buffer
481: * @msgID = MessageID
482: * @subqos = Subscribes QoS, must be free after use with free()
483: * return: -1 error or >-1 readed subscribes QoS elements
484: */
485: int mqtt_readSUBACK(mqtt_msg_t * __restrict buf, u_short *msgID, unsigned char **subqos);
486: /*
487: * mqtt_readUNSUBSCRIBE() Read UNSUBSCRIBE message
488: *
489: * @buf = Message buffer
490: * @msgID = MessageID
491: * @subscr = Subscriptions, must be free after use with mqtt_subFree()
492: * return: NULL error or !=NULL MQTT fixed header
493: */
494: struct mqtthdr *mqtt_readUNSUBSCRIBE(mqtt_msg_t * __restrict buf, unsigned short *msgID,
495: mqtt_subscr_t **subscr);
496: /*
497: * mqtt_readUNSUBACK() Read UNSUBACK message
498: *
499: * @buf = Message buffer
500: * return: -1 error or MessageID
501: */
502: u_short mqtt_readUNSUBACK(mqtt_msg_t * __restrict buf);
503:
504: /*** ENGINE FUNCTIONS ***/
505:
506: /*
507: * mqttInitCallbacks() Init callback array for dispatcher
508: *
509: * return: NULL error or !=NULL allocated callback array, after use free with mqttFiniCallbacks()
510: */
511: mqtt_cb_t *mqttInitCallbacks(void);
512: /*
513: * mqttFiniCallbacks() Free callback array
514: *
515: * @cb = Callback array
516: * return: none
517: */
518: void mqttFiniCallbacks(mqtt_cb_t ** __restrict cb);
519: /*
520: * MQTT_CALLBACK() Assign function to callback array for MQTT dispatcher
521: *
522: * @_cbs = Callback array
523: * @_x = MQTT Message type, like MQTT_TYPE_* ...
524: * @_func = Function
525: * return: none
526: */
527: #define MQTT_CALLBACK(_cbs, _x, _func) (assert((_cbs)), (_cbs)[(_x)] = (_func))
528: /*
529: * mqttDispatcher() MQTT Message type dispatcher
530: *
531: * @cb = Callback array
532: * @buf = Received MQTT message
533: * return: -1 error or >-1 return value from executed callback
534: */
535: inline int mqttDispatcher(mqtt_cb_t * __restrict cb, mqtt_msg_t * __restrict buf);
536:
537:
538: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>