Annotation of mqtt/inc/aitmqtt.h, revision 1.1.1.1.2.18
1.1 misho 1: #ifndef __AITMQTT_H
2: #define __AITMQTT_H
3:
4:
1.1.1.1.2.3 misho 5: #define MQTT_DATA_MAX 268435455
1.1.1.1.2.4 misho 6: #define MQTT_CONN_STR "MQIsdp"
7: #define MQTT_PROTO_VER 3
8: #define MQTT_KEEPALIVE 10
9:
10: /* FIXED HEADER */
1.1.1.1.2.3 misho 11:
1.1.1.1.2.1 misho 12: struct mqtthdr {
1.1.1.1.2.17 misho 13: union {
14: struct {
15: unsigned char retain:1,
16: qos:2,
17: dup:1,
18: type:4;
19: };
20: unsigned char val;
1.1.1.1.2.4 misho 21: } mqtt_msg;
22: unsigned char mqtt_len[1]; /* may be grow to 4 bytes */
23: } __packed;
1.1.1.1.2.17 misho 24: #define MQTTHDR_MSGINIT(x) (assert((x)), (x)->mqtt_msg.val ^= (x)->mqtt_msg.val)
1.1.1.1.2.1 misho 25:
26: #define MQTT_TYPE_UNKNOWN 0 /* reserved */
27: #define MQTT_TYPE_CONNECT 1 /* client request to connect to server */
28: #define MQTT_TYPE_CONNACK 2 /* connect acknowledgment */
29: #define MQTT_TYPE_PUBLISH 3 /* publish message */
30: #define MQTT_TYPE_PUBACK 4 /* publish acknowledgment */
31: #define MQTT_TYPE_PUBREC 5 /* publish received (assured delivery part 1) */
32: #define MQTT_TYPE_PUBREL 6 /* publish release (assured delivery part 2) */
33: #define MQTT_TYPE_PUBCOMP 7 /* publish complete (assured delivery part 3) */
34: #define MQTT_TYPE_SUBSCRIBE 8 /* client subscribe request */
35: #define MQTT_TYPE_SUBACK 9 /* subscribe acknowledgment */
36: #define MQTT_TYPE_UNSUBSCRIBE 10 /* client unsubscribe request */
37: #define MQTT_TYPE_UNSUBACK 11 /* unsubscribe acknowledgment */
38: #define MQTT_TYPE_PINGREQ 12 /* PING request */
39: #define MQTT_TYPE_PINGRESP 13 /* PING response */
40: #define MQTT_TYPE_DISCONNECT 14 /* client is disconnecting */
41: #define MQTT_TYPE_RESERVED 15 /* reserved */
42:
43: #define MQTT_FLAG_DUP 1 /* This flag is set when the client or server attempts to re-deliver
44: a PUBLISH, PUBREL, SUBSCRIBE or UNSUBSCRIBE message.
45: This applies to messages where the value of QoS is greater than
46: zero (0), and an acknowledgment is required.
47: When the DUP bit is set, the variable header includes a Message ID.
48:
49: The recipient should treat this flag as a hint as to whether
50: the message may have been previously received.
51: It should not be relied on to detect duplicates. */
52:
53: #define MQTT_QOS_ONCE 0 /* At most once, Fire and Forget, <=1 */
54: #define MQTT_QOS_ACK 1 /* At least once, Acknowledged delivery, >=1 */
55: #define MQTT_QOS_EXACTLY 2 /* Exactly once, Assured delivery, =1 */
56: #define MQTT_QOS_RESERVED 3 /* reserved */
57:
58: #define MQTT_FLAG_RETAIN 1 /* This flag is only used on PUBLISH messages.
59:
60: When a client sends a PUBLISH to a server,
61: if the Retain flag is set (1),
62: the server should hold on to the message after it has been
63: delivered to the current subscribers.
64: When a new subscription is established on a topic,
65: the last retained message on that topic should be sent to
66: the subscriber with the Retain flag set.
67: If there is no retained message, nothing is sent
68: This is useful where publishers send messages on a
69: "report by exception" basis, where it might be some time between messages.
70: This allows new subscribers to instantly receive data with the retained,
71: or Last Known Good, value.
72:
73: When a server sends a PUBLISH to a client as a result of
74: a subscription that already existed when the original PUBLISH arrived,
75: the Retain flag should not be set, regardless of the Retain flag
76: of the original PUBLISH. This allows a client to distinguish messages
77: that are being received because they were retained and those
78: that are being received "live".
79:
80: Retained messages should be kept over restarts of the server.
81: A server may delete a retained message if it receives a message
82: with a zero-length payload and the Retain flag set on the same topic. */
83:
1.1.1.1.2.4 misho 84: /* VARIABLE HEADERS */
85:
86: #define MQTT_RETCODE_ACCEPTED 0
87: #define MQTT_RETCODE_REFUSE_VER 1
88: #define MQTT_RETCODE_REFUSE_ID 2
89: #define MQTT_RETCODE_REFUSE_UNAVAIL 3
90: #define MQTT_RETCODE_REFUSE_USERPASS 4
91: #define MQTT_RETCODE_DENIED 5
92:
93:
94: typedef union {
95: struct {
96: unsigned short m:8,
97: l:8;
98: } sb;
99: unsigned short val;
1.1.1.1.2.5 misho 100: } mqtt_v_t;
1.1.1.1.2.4 misho 101:
102: typedef struct {
1.1.1.1.2.12 misho 103: mqtt_v_t sub_sb;
104: char *sub_data;
105: unsigned char sub_qos;
106: } mqtt_subscr_t;
107:
108: typedef struct {
1.1.1.1.2.5 misho 109: mqtt_v_t var_sb;
1.1.1.1.2.4 misho 110: unsigned char var_data[0];
111: } __packed mqtthdr_var_t;
1.1.1.1.2.5 misho 112: #define MQTTHDR_VAR_SIZEOF(x) (assert((x)), sizeof(mqtt_v_t) + ntohs((x)->var_sb.val))
1.1.1.1.2.4 misho 113:
114: typedef unsigned char mqtthdr_protover_t;
115:
116: typedef struct {
117: unsigned char reserved:1,
118: clean_sess:1,
119: will_flg:1,
120: will_qos:2,
121: will_retain:1,
122: password:1,
123: username:1;
124: } __packed mqtthdr_connflgs_t;
125:
1.1.1.1.2.7 misho 126: typedef struct {
127: unsigned char reserved;
128: unsigned char retcode;
129: } __packed mqtthdr_connack_t;
130:
1.1.1.1.2.1 misho 131:
1.1.1.1.2.5 misho 132: /* MQTT Message buffer */
133:
134: typedef struct {
135: void *msg_base;
136: unsigned short msg_len;
137: } mqtt_msg_t;
138:
139:
1.1 misho 140: // -------------------------------------------------------
141: // mqtt_GetErrno() Get error code of last operation
142: inline int mqtt_GetErrno();
143: // mqtt_GetError() Get error text of last operation
144: inline const char *mqtt_GetError();
145: // -------------------------------------------------------
146:
147:
1.1.1.1.2.1 misho 148: /*
1.1.1.1.2.5 misho 149: * mqtt_msgAlloc() Allocate memory for MQTT Message
1.1.1.1.2.15 misho 150: *
1.1.1.1.2.5 misho 151: * @len = >0 Allocate buffer with length
152: * return: NULL error or Message, after use must call mqtt_msgFree() with all!=0
153: */
154: inline mqtt_msg_t *mqtt_msgAlloc(unsigned short len);
155: /*
156: * mqtt_msgFree() Free MQTT message
1.1.1.1.2.15 misho 157: *
1.1.1.1.2.5 misho 158: * @msg = Message buffer
159: * @all = !=0 Destroy entire message, if MQTT Message allocated with mqtt_msgAlloc()
160: * return: none
161: */
162: inline void mqtt_msgFree(mqtt_msg_t ** __restrict msg, int all);
163: /*
164: * mqtt_msgRealloc() Reallocate MQTT message buffer
1.1.1.1.2.15 misho 165: *
1.1.1.1.2.5 misho 166: * @msg = MQTT message
167: * @len = new length
168: * return: -1 error or >-1 old buffer length
169: */
170: inline int mqtt_msgRealloc(mqtt_msg_t * __restrict msg, unsigned short len);
171:
172: /*
1.1.1.1.2.1 misho 173: * mqtt_encodeLen() Encode number to MQTT length field
1.1.1.1.2.15 misho 174: *
1.1.1.1.2.1 misho 175: * @num = number for encode
176: * return: -1 error or >-1 length
177: */
178: inline unsigned int mqtt_encodeLen(unsigned int num);
179: /*
180: * mqtt_decodeLen() Decode length from MQTT packet
1.1.1.1.2.15 misho 181: *
1.1.1.1.2.1 misho 182: * @len = length
1.1.1.1.2.2 misho 183: * @n = sizeof bytes, if !=NULL
1.1.1.1.2.1 misho 184: * return: -1 error, >-1 length of message
185: */
1.1.1.1.2.2 misho 186: inline unsigned int mqtt_decodeLen(unsigned int len, char *n);
187: /*
188: * mqtt_sizeLen Return sizeof len field
1.1.1.1.2.15 misho 189: *
1.1.1.1.2.2 misho 190: * @len = length
191: * return: -1 error, >-1 sizeof len in bytes
192: */
193: inline char mqtt_sizeLen(unsigned int len);
1.1.1.1.2.4 misho 194: /*
1.1.1.1.2.12 misho 195: * mqtt_str2sub Create MQTT subscribe variable from string(s)
1.1.1.1.2.15 misho 196: *
1.1.1.1.2.12 misho 197: * @csStr = strings
198: * @strnum = number of strings elements
199: * @qoses = QoS elements applied to subscribe variable,
200: * count of elements must be equal with csStr elements
201: * return: NULL error or != subscribe variables array, must be free after use with mqtt_freeSub()
202: */
203: inline mqtt_subscr_t *mqtt_str2sub(const char **csStr, unsigned short strnum, unsigned char *qoses);
204: /*
1.1.1.1.2.15 misho 205: * mqtt_subFree() Free array from subscribe variables
1.1.1.1.2.12 misho 206: *
207: * @subs = Subscribe variables
208: * return: none
1.1.1.1.2.4 misho 209: */
1.1.1.1.2.16 misho 210: inline void mqtt_subFree(mqtt_subscr_t ** __restrict subs);
1.1.1.1.2.15 misho 211: /*
212: * mqtt_subAlloc() Create array from subscribe variables
213: *
214: * @num = Number of elements
1.1.1.1.2.16 misho 215: * return: NULL error or subscribe array, after use must call mqtt_subFree()
1.1.1.1.2.15 misho 216: */
217: inline mqtt_subscr_t *mqtt_subAlloc(unsigned short num);
1.1.1.1.2.1 misho 218:
1.1.1.1.2.6 misho 219: /*
220: * mqtt_msgCONNECT() Create CONNECT message
221: *
222: * @buf = Message buffer
223: * @csConnID = ConnectID
224: * @csUser = Username if !=NULL
225: * @csPass = Password for Username, only if csUser is set
226: * @csWillTopic = Will Topic if !=NULL Will Flags set into message
227: * @csWillMessage = Will Message, may be NULL
228: * @ClrSess = Clear Session subscriptions after disconnect
229: * @WillQOS = Will QOS if csWillTopic is set
230: * @WillRetain = Will Retain Will Message if csWillTopic is set
231: * return: -1 error or >-1 message size for send
232: */
233: int mqtt_msgCONNECT(mqtt_msg_t * __restrict buf, const char *csConnID,
234: const char *csUser, const char *csPass,
235: const char *csWillTopic, const char *csWillMessage,
1.1.1.1.2.8 misho 236: unsigned char ClrSess, unsigned char WillQOS, unsigned char WillRetain);
1.1.1.1.2.7 misho 237: /*
238: * mqtt_msgCONNACK() Create CONNACK message
239: *
240: * @buf = Message buffer
241: * @retcode = Return code
242: * return: -1 error or >-1 message size for send
243: */
1.1.1.1.2.8 misho 244: int mqtt_msgCONNACK(mqtt_msg_t * __restrict buf, unsigned char retcode);
1.1.1.1.2.18! misho 245: /*
! 246: * mqtt_msgDISCONNECT() Create DISCONNECT message
! 247: *
! 248: * @buf = Message buffer
! 249: * return: -1 error or >-1 message size for send
! 250: */
! 251: int mqtt_msgDISCONNECT(mqtt_msg_t * __restrict buf);
! 252: /*
! 253: * mqtt_msgPINGREQ() Create PINGREQ message
! 254: *
! 255: * @buf = Message buffer
! 256: * return: -1 error or >-1 message size for send
! 257: */
! 258: int mqtt_msgPINGREQ(mqtt_msg_t * __restrict buf);
! 259: /*
! 260: * mqtt_msgPINGRESP() Create PINGRESP message
! 261: *
! 262: * @buf = Message buffer
! 263: * return: -1 error or >-1 message size for send
! 264: */
! 265: int mqtt_msgPINGRESP(mqtt_msg_t * __restrict buf);
1.1.1.1.2.13 misho 266:
1.1.1.1.2.8 misho 267: /*
268: * mqtt_msgPUBLISH() Create PUBLISH message
269: *
270: * @buf = Message buffer
271: * @csTopic = Publish topic
272: * @msgID = MessageID >0, if QOS != MQTT_QOS_ONCE
273: * @Dup = Duplicate message
274: * @QOS = QoS
1.1.1.1.2.10 misho 275: * @Retain = Retain message
1.1.1.1.2.11 misho 276: * @pData = Publish data into topic
277: * @datlen = Publish data length
1.1.1.1.2.8 misho 278: * return: -1 error or >-1 message size for send
279: */
1.1.1.1.2.10 misho 280: int mqtt_msgPUBLISH(mqtt_msg_t * __restrict buf, const char *csTopic, unsigned short msgID,
1.1.1.1.2.11 misho 281: unsigned char Dup, unsigned char QOS, unsigned char Retain,
282: const void *pData, unsigned short datlen);
1.1.1.1.2.8 misho 283: /*
284: * mqtt_msgPUBACK() Create PUBACK message
285: *
286: * @buf = Message buffer
287: * @msgID = MessageID
288: * return: -1 error or >-1 message size for send
289: */
290: inline int mqtt_msgPUBACK(mqtt_msg_t * __restrict buf, unsigned short msgID);
291: /*
292: * mqtt_msgPUBREC() Create PUBREC message
293: *
294: * @buf = Message buffer
295: * @msgID = MessageID
296: * return: -1 error or >-1 message size for send
297: */
298: inline int mqtt_msgPUBREC(mqtt_msg_t * __restrict buf, unsigned short msgID);
299: /*
300: * mqtt_msgPUBREL() Create PUBREL message
301: *
302: * @buf = Message buffer
303: * @msgID = MessageID
304: * return: -1 error or >-1 message size for send
305: */
306: inline int mqtt_msgPUBREL(mqtt_msg_t * __restrict buf, unsigned short msgID);
307: /*
308: * mqtt_msgPUBCOMP() Create PUBCOMP message
309: *
310: * @buf = Message buffer
311: * @msgID = MessageID
312: * return: -1 error or >-1 message size for send
313: */
314: inline int mqtt_msgPUBCOMP(mqtt_msg_t * __restrict buf, unsigned short msgID);
1.1.1.1.2.6 misho 315:
1.1.1.1.2.13 misho 316: /*
317: * mqtt_msgSUBSCRIBE() Create SUBSCRIBE message
318: *
319: * @buf = Message buffer
320: * @Topics = MQTT subscription topics
321: * @msgID = MessageID
322: * @Dup = Duplicate message
323: * @QOS = QoS
324: * return: -1 error or >-1 message size for send
325: */
326: int
327: mqtt_msgSUBSCRIBE(mqtt_msg_t * __restrict buf, mqtt_subscr_t * __restrict Topics,
328: unsigned short msgID, unsigned char Dup, unsigned char QOS);
1.1.1.1.2.15 misho 329: /*
330: * mqtt_msgSUBACK() Create SUBACK message
331: *
332: * @buf = Message buffer
333: * @Topics = MQTT subscription topics
334: * @msgID = MessageID
335: * return: -1 error or >-1 message size for send
336: */
337: int mqtt_msgSUBACK(mqtt_msg_t * __restrict buf, mqtt_subscr_t * __restrict Topics,
338: unsigned short msgID);
1.1.1.1.2.17 misho 339: /*
340: * mqtt_msgUNSUBSCRIBE() Create UNSUBSCRIBE message
341: *
342: * @buf = Message buffer
343: * @Topics = MQTT subscription topics
344: * @msgID = MessageID
345: * @Dup = Duplicate message
346: * @QOS = QoS
347: * return: -1 error or >-1 message size for send
348: */
349: int
350: mqtt_msgUNSUBSCRIBE(mqtt_msg_t * __restrict buf, mqtt_subscr_t * __restrict Topics,
351: unsigned short msgID, unsigned char Dup, unsigned char QOS);
352: /*
353: * mqtt_msgUNSUBACK() Create UNSUBACK message
354: *
355: * @buf = Message buffer
356: * @msgID = MessageID
357: * return: -1 error or >-1 message size for send
358: */
359: int mqtt_msgUNSUBACK(mqtt_msg_t * __restrict buf, unsigned short msgID);
1.1.1.1.2.13 misho 360:
1.1.1.1.2.1 misho 361:
1.1 misho 362: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>