Annotation of mqtt/inc/aitmqtt.h, revision 1.1.1.1.2.15

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>