Annotation of mqtt/inc/aitmqtt.h, revision 1.2
1.1 misho 1: #ifndef __AITMQTT_H
2: #define __AITMQTT_H
3:
4:
1.2 ! misho 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:
1.1 misho 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:
1.2 ! misho 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: * @kasec = Keep alive timeout
! 238: * @csUser = Username if !=NULL
! 239: * @csPass = Password for Username, only if csUser is set
! 240: * @csWillTopic = Will Topic if !=NULL Will Flags set into message
! 241: * @csWillMessage = Will Message, may be NULL
! 242: * @ClrSess = Clear Session subscriptions after disconnect
! 243: * @WillQOS = Will QOS if csWillTopic is set
! 244: * @WillRetain = Will Retain Will Message if csWillTopic is set
! 245: * return: -1 error or >-1 message size for send
! 246: */
! 247: int mqtt_msgCONNECT(mqtt_msg_t * __restrict buf, const char *csConnID,
! 248: unsigned short kasec, const char *csUser, const char *csPass,
! 249: const char *csWillTopic, const char *csWillMessage,
! 250: unsigned char ClrSess, unsigned char WillQOS, unsigned char WillRetain);
! 251: /*
! 252: * mqtt_msgCONNACK() Create CONNACK message
! 253: *
! 254: * @buf = Message buffer
! 255: * @retcode = Return code
! 256: * return: -1 error or >-1 message size for send
! 257: */
! 258: int mqtt_msgCONNACK(mqtt_msg_t * __restrict buf, unsigned char retcode);
! 259: /*
! 260: * mqtt_msgDISCONNECT() Create DISCONNECT message
! 261: *
! 262: * @buf = Message buffer
! 263: * return: -1 error or >-1 message size for send
! 264: */
! 265: int mqtt_msgDISCONNECT(mqtt_msg_t * __restrict buf);
! 266: /*
! 267: * mqtt_msgPINGREQ() Create PINGREQ message
! 268: *
! 269: * @buf = Message buffer
! 270: * return: -1 error or >-1 message size for send
! 271: */
! 272: int mqtt_msgPINGREQ(mqtt_msg_t * __restrict buf);
! 273: /*
! 274: * mqtt_msgPINGRESP() Create PINGRESP message
! 275: *
! 276: * @buf = Message buffer
! 277: * return: -1 error or >-1 message size for send
! 278: */
! 279: int mqtt_msgPINGRESP(mqtt_msg_t * __restrict buf);
! 280:
! 281: /*
! 282: * mqtt_msgPUBLISH() Create PUBLISH message
! 283: *
! 284: * @buf = Message buffer
! 285: * @csTopic = Publish topic
! 286: * @msgID = MessageID >0, if QOS != MQTT_QOS_ONCE
! 287: * @Dup = Duplicate message
! 288: * @QOS = QoS
! 289: * @Retain = Retain message
! 290: * @pData = Publish data into topic
! 291: * @datlen = Publish data length
! 292: * return: -1 error or >-1 message size for send
! 293: */
! 294: int mqtt_msgPUBLISH(mqtt_msg_t * __restrict buf, const char *csTopic,
! 295: unsigned short msgID, unsigned char Dup, unsigned char QOS,
! 296: unsigned char Retain, const void *pData, int datlen);
! 297: /*
! 298: * mqtt_msgPUBACK() Create PUBACK message
! 299: *
! 300: * @buf = Message buffer
! 301: * @msgID = MessageID
! 302: * return: -1 error or >-1 message size for send
! 303: */
! 304: inline int mqtt_msgPUBACK(mqtt_msg_t * __restrict buf, unsigned short msgID);
! 305: /*
! 306: * mqtt_msgPUBREC() Create PUBREC message
! 307: *
! 308: * @buf = Message buffer
! 309: * @msgID = MessageID
! 310: * return: -1 error or >-1 message size for send
! 311: */
! 312: inline int mqtt_msgPUBREC(mqtt_msg_t * __restrict buf, unsigned short msgID);
! 313: /*
! 314: * mqtt_msgPUBREL() Create PUBREL message
! 315: *
! 316: * @buf = Message buffer
! 317: * @msgID = MessageID
! 318: * return: -1 error or >-1 message size for send
! 319: */
! 320: inline int mqtt_msgPUBREL(mqtt_msg_t * __restrict buf, unsigned short msgID);
! 321: /*
! 322: * mqtt_msgPUBCOMP() Create PUBCOMP message
! 323: *
! 324: * @buf = Message buffer
! 325: * @msgID = MessageID
! 326: * return: -1 error or >-1 message size for send
! 327: */
! 328: inline int mqtt_msgPUBCOMP(mqtt_msg_t * __restrict buf, unsigned short msgID);
! 329:
! 330: /*
! 331: * mqtt_msgSUBSCRIBE() Create SUBSCRIBE message
! 332: *
! 333: * @buf = Message buffer
! 334: * @Topics = MQTT subscription topics
! 335: * @msgID = MessageID
! 336: * @Dup = Duplicate message
! 337: * @QOS = QoS
! 338: * return: -1 error or >-1 message size for send
! 339: */
! 340: int
! 341: mqtt_msgSUBSCRIBE(mqtt_msg_t * __restrict buf, mqtt_subscr_t * __restrict Topics,
! 342: unsigned short msgID, unsigned char Dup, unsigned char QOS);
! 343: /*
! 344: * mqtt_msgSUBACK() Create SUBACK message
! 345: *
! 346: * @buf = Message buffer
! 347: * @Topics = MQTT subscription topics
! 348: * @msgID = MessageID
! 349: * return: -1 error or >-1 message size for send
! 350: */
! 351: int mqtt_msgSUBACK(mqtt_msg_t * __restrict buf, mqtt_subscr_t * __restrict Topics,
! 352: unsigned short msgID);
! 353: /*
! 354: * mqtt_msgUNSUBSCRIBE() Create UNSUBSCRIBE message
! 355: *
! 356: * @buf = Message buffer
! 357: * @Topics = MQTT subscription topics
! 358: * @msgID = MessageID
! 359: * @Dup = Duplicate message
! 360: * @QOS = QoS
! 361: * return: -1 error or >-1 message size for send
! 362: */
! 363: int
! 364: mqtt_msgUNSUBSCRIBE(mqtt_msg_t * __restrict buf, mqtt_subscr_t * __restrict Topics,
! 365: unsigned short msgID, unsigned char Dup, unsigned char QOS);
! 366: /*
! 367: * mqtt_msgUNSUBACK() Create UNSUBACK message
! 368: *
! 369: * @buf = Message buffer
! 370: * @msgID = MessageID
! 371: * return: -1 error or >-1 message size for send
! 372: */
! 373: int mqtt_msgUNSUBACK(mqtt_msg_t * __restrict buf, unsigned short msgID);
! 374:
! 375:
! 376: /*** RECEIVER FUNCTIONS ***/
! 377:
! 378: /*
! 379: * mqtt_readCONNECT() Read elements from CONNECT message
! 380: *
! 381: * @buf = Message buffer
! 382: * @kasec = Keep Alive in seconds for current connection
! 383: * @psConnID = ConnectID
! 384: * @connLen = ConnectID length
! 385: * @psUser = Username if !=NULL
! 386: * @userLen = Username length
! 387: * @psPass = Password for Username, only if csUser is set
! 388: * @passLen = Password length
! 389: * @psWillTopic = Will Topic if !=NULL Will Flags set into message and must be free()
! 390: * @psWillMessage = Will Message, may be NULL if !NULL must be free() after use!
! 391: * return: .reserved == 1 is error or == 0 connection flags & msg ok
! 392: */
! 393: mqtthdr_connack_t mqtt_readCONNECT(mqtt_msg_t * __restrict buf, unsigned short *kasec,
! 394: char * __restrict psConnID, int connLen,
! 395: char * __restrict psUser, int userLen, char * __restrict psPass, int passLen,
! 396: char ** __restrict psWillTopic, char ** __restrict psWillMessage);
! 397: /*
! 398: * mqtt_readCONNACK() Read CONNACK message
! 399: *
! 400: * @buf = Message buffer
! 401: * return: -1 error or >-1 CONNECT message return code
! 402: */
! 403: unsigned char mqtt_readCONNACK(mqtt_msg_t * __restrict buf);
! 404: /*
! 405: * mqtt_readDISCONNECT() Read DISCONNECT message
! 406: *
! 407: * @buf = Message buffer
! 408: * return: -1 error, 0 ok, >0 undefined result
! 409: */
! 410: int mqtt_readDISCONNECT(mqtt_msg_t * __restrict buf);
! 411: /*
! 412: * mqtt_readPINGREQ() Read PINGREQ message
! 413: *
! 414: * @buf = Message buffer
! 415: * return: -1 error, 0 ok, >0 undefined result
! 416: */
! 417: int mqtt_readPINGREQ(mqtt_msg_t * __restrict buf);
! 418: /*
! 419: * mqtt_readPINGRESP() Read PINGRESP message
! 420: *
! 421: * @buf = Message buffer
! 422: * return: -1 error, 0 ok, >0 undefined result
! 423: */
! 424: int mqtt_readPINGRESP(mqtt_msg_t * __restrict buf);
! 425:
! 426: /*
! 427: * mqtt_readPUBLISH() Read PUBLISH message
! 428: *
! 429: * @buf = Message buffer
! 430: * @psTopic = Topic
! 431: * @topicLen = Topic length
! 432: * @msgID = MessageID
! 433: * @pData = Data buffer
! 434: * @datLen = Data buffer length, if *datLen == 0 allocate memory for pData
! 435: * return: NULL error or !=NULL MQTT fixed header
! 436: */
! 437: struct mqtthdr *mqtt_readPUBLISH(mqtt_msg_t * __restrict buf, char * __restrict psTopic,
! 438: int topicLen, unsigned short *msgID, void * __restrict pData, int *datLen);
! 439: /*
! 440: * mqtt_readPUBACK() Read PUBACK message
! 441: *
! 442: * @buf = Message buffer
! 443: * return: -1 error or MessageID
! 444: */
! 445: u_short mqtt_readPUBACK(mqtt_msg_t * __restrict buf);
! 446: /*
! 447: * mqtt_readPUBREC() Read PUBREC message
! 448: *
! 449: * @buf = Message buffer
! 450: * return: -1 error or MessageID
! 451: */
! 452: u_short mqtt_readPUBREC(mqtt_msg_t * __restrict buf);
! 453: /*
! 454: * mqtt_readPUBREL() Read PUBREL message
! 455: *
! 456: * @buf = Message buffer
! 457: * return: -1 error or MessageID
! 458: */
! 459: u_short mqtt_readPUBREL(mqtt_msg_t * __restrict buf);
! 460: /*
! 461: * mqtt_readPUBCOMP() Read PUBCOMP message
! 462: *
! 463: * @buf = Message buffer
! 464: * return: -1 error or MessageID
! 465: */
! 466: u_short mqtt_readPUBCOMP(mqtt_msg_t * __restrict buf);
! 467:
! 468: /*
! 469: * mqtt_readSUBSCRIBE() Read SUBSCRIBE message
! 470: *
! 471: * @buf = Message buffer
! 472: * @msgID = MessageID
! 473: * @subscr = Subscriptions, must be free after use with mqtt_subFree()
! 474: * return: NULL error or !=NULL MQTT fixed header
! 475: */
! 476: struct mqtthdr *mqtt_readSUBSCRIBE(mqtt_msg_t * __restrict buf, unsigned short *msgID,
! 477: mqtt_subscr_t **subscr);
! 478: /*
! 479: * mqtt_readSUBACK() Read SUBACK message
! 480: *
! 481: * @buf = Message buffer
! 482: * @msgID = MessageID
! 483: * @subqos = Subscribes QoS, must be free after use with free()
! 484: * return: -1 error or >-1 readed subscribes QoS elements
! 485: */
! 486: int mqtt_readSUBACK(mqtt_msg_t * __restrict buf, u_short *msgID, unsigned char **subqos);
! 487: /*
! 488: * mqtt_readUNSUBSCRIBE() Read UNSUBSCRIBE message
! 489: *
! 490: * @buf = Message buffer
! 491: * @msgID = MessageID
! 492: * @subscr = Subscriptions, must be free after use with mqtt_subFree()
! 493: * return: NULL error or !=NULL MQTT fixed header
! 494: */
! 495: struct mqtthdr *mqtt_readUNSUBSCRIBE(mqtt_msg_t * __restrict buf, unsigned short *msgID,
! 496: mqtt_subscr_t **subscr);
! 497: /*
! 498: * mqtt_readUNSUBACK() Read UNSUBACK message
! 499: *
! 500: * @buf = Message buffer
! 501: * return: -1 error or MessageID
! 502: */
! 503: u_short mqtt_readUNSUBACK(mqtt_msg_t * __restrict buf);
! 504:
! 505: /*** ENGINE FUNCTIONS ***/
! 506:
! 507: /*
! 508: * mqttInitCallbacks() Init callback array for dispatcher
! 509: *
! 510: * return: NULL error or !=NULL allocated callback array, after use free with mqttFiniCallbacks()
! 511: */
! 512: mqtt_cb_t *mqttInitCallbacks(void);
! 513: /*
! 514: * mqttFiniCallbacks() Free callback array
! 515: *
! 516: * @cb = Callback array
! 517: * return: none
! 518: */
! 519: void mqttFiniCallbacks(mqtt_cb_t ** __restrict cb);
! 520: /*
! 521: * MQTT_CALLBACK() Assign function to callback array for MQTT dispatcher
! 522: *
! 523: * @_cbs = Callback array
! 524: * @_x = MQTT Message type, like MQTT_TYPE_* ...
! 525: * @_func = Function
! 526: * return: none
! 527: */
! 528: #define MQTT_CALLBACK(_cbs, _x, _func) (assert((_cbs)), (_cbs)[(_x)] = (_func))
! 529: /*
! 530: * mqttDispatcher() MQTT Message type dispatcher
! 531: *
! 532: * @cb = Callback array
! 533: * @buf = Received MQTT message
! 534: * return: -1 error or >-1 return value from executed callback
! 535: */
! 536: inline int mqttDispatcher(mqtt_cb_t * __restrict cb, mqtt_msg_t * __restrict buf);
! 537:
! 538:
1.1 misho 539: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>