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