--- libaitmqtt/inc/aitmqtt.h 2022/09/12 23:15:37 1.3.4.5 +++ libaitmqtt/inc/aitmqtt.h 2022/09/13 22:20:59 1.3.4.8 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitmqtt.h,v 1.3.4.5 2022/09/12 23:15:37 misho Exp $ +* $Id: aitmqtt.h,v 1.3.4.8 2022/09/13 22:20:59 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -47,16 +47,16 @@ SUCH DAMAGE. #define __AITMQTT_H -#define MAX_CONNID 24 -#define MAX_CRED 13 +#define MQTT_CONNID_MAX 24 +#define MQTT_CRED_MAX 13 #define MQTTMSG_MAX 65535 #define MQTTMSG_BIN_MAX 65535 #define MQTT_DATA_MAX 268435455 -#define MQTT_PROTO_VER 3 -#define MQTT_PROTO_DEFVER 5 -#define MQTT_KEEPALIVE 10 -#define MQTT_DEFAULT_MSGID 0xDEBA +#define MQTT_PROTO_VER_3 3 +#define MQTT_PROTO_VER_311 4 +#define MQTT_PROTO_VER_5 5 +#define MQTT_KEEPALIVE 60 /* FIXED HEADER */ @@ -72,9 +72,41 @@ struct mqtthdr { } mqtt_msg; unsigned char mqtt_len[1]; /* may be grow to 4 bytes */ } __attribute__((packed)); -#define MQTTHDR_MSGINIT(x) (assert((x)), (x)->mqtt_msg.val ^= (x)->mqtt_msg.val, *mqtt_len = 0) +#define MQTTHDR_MSGINIT(x) (assert((x)), (x)->mqtt_msg.val ^= (x)->mqtt_msg.val, *(x)->mqtt_len = 0) #define MQTTHDR_DATA_SIZEOF(x) (assert((x)), mqtt_decodeLen((x)->mqtt_len, NULL)) +typedef union { + struct { + unsigned short m:8, + l:8; + } sb; + unsigned short val; +} __attribute__((packed)) mqtt_len_t; + +typedef struct { + mqtt_len_t var_sb; + unsigned char var_data[0]; +} __attribute__((packed)) mqtthdr_var_t; +#define MQTTHDR_VAR_SIZEOF(x) (assert((x)), sizeof(mqtt_len_t) + ntohs((x)->var_sb.val)) + +typedef union { + struct { + unsigned char reserved:1, + clean_sess:1, + will_flg:1, + will_qos:2, + will_retain:1, + password:1, + username:1; + }; + unsigned char flags; +} __attribute__((packed)) mqtthdr_connflgs_t; + +typedef struct { + unsigned char reserved; + unsigned char retcode; +} __attribute__((packed)) mqtthdr_connack_t; + #define MQTT_TYPE_UNKNOWN 0 /* reserved */ #define MQTT_TYPE_CONNECT 1 /* client request to connect to server (CLI) */ #define MQTT_TYPE_CONNACK 2 /* connect acknowledgment (SRV) [ret_no_data] */ @@ -200,52 +232,18 @@ struct mqtthdr { typedef struct { void *msg_base; - unsigned short msg_len; + unsigned int msg_len; } mqtt_msg_t; /* MQTT structures */ -typedef union { - struct { - unsigned short m:8, - l:8; - } sb; - unsigned short val; -} mqtt_len_t; - typedef struct { unsigned char sub_ret; mqtt_msg_t sub_topic; mqtt_msg_t sub_value; } mqtt_subscr_t; -typedef struct { - mqtt_len_t var_sb; - unsigned char var_data[0]; -} __attribute__((packed)) mqtthdr_var_t; -#define MQTTHDR_VAR_SIZEOF(x) (assert((x)), sizeof(mqtt_len_t) + ntohs((x)->var_sb.val)) -typedef unsigned char mqtthdr_protover_t; - -typedef union { - struct { - unsigned char reserved:1, - clean_sess:1, - will_flg:1, - will_qos:2, - will_retain:1, - password:1, - username:1; - }; - unsigned char flags; -} __attribute__((packed)) mqtthdr_connflgs_t; - -typedef struct { - unsigned char reserved; - unsigned char retcode; -} __attribute__((packed)) mqtthdr_connack_t; - - // ------------------------------------------------------- // mqtt_GetErrno() Get error code of last operation int mqtt_GetErrno(); @@ -260,15 +258,15 @@ const char *mqtt_GetError(); * @len = >0 Allocate buffer with length * return: NULL error or Message, after use must call mqtt_msgFree() with all!=0 */ -mqtt_msg_t *mqtt_msgAlloc(unsigned short len); +mqtt_msg_t *mqtt_msgAlloc(unsigned int len); /* * mqtt_msgFree() Free MQTT message * * @msg = Message buffer - * @all = !=0 Destroy entire message, if MQTT Message allocated with mqtt_msgAlloc() + * @keepmsg = !=0 just free message content * return: none */ -void mqtt_msgFree(mqtt_msg_t ** __restrict msg, int all); +void mqtt_msgFree(mqtt_msg_t ** __restrict msg, int keepmsg); /* * mqtt_msgRealloc() Reallocate MQTT message buffer * @@ -276,7 +274,7 @@ void mqtt_msgFree(mqtt_msg_t ** __restrict msg, int al * @len = new length * return: -1 error or >-1 old buffer length */ -int mqtt_msgRealloc(mqtt_msg_t * __restrict msg, unsigned short len); +int mqtt_msgRealloc(mqtt_msg_t * __restrict msg, unsigned int len); /* * mqtt_msgDup() - Duplicate message buffer * @@ -337,16 +335,15 @@ char mqtt_sizeLen(unsigned int len); */ unsigned int mqtt_pktLen(struct mqtthdr * __restrict hdr); /* - * mqtt_str2subs Create MQTT subscribe variable from string(s) + * mqtt_strs2subs Create MQTT subscribe variable from string(s) * * @csStr = null terminated string array - * @strnum = copy at most number of strings elements + * @strnum = copy at most number of strings elements, ==0 till NULL element * @qoses = QoS elements applied to subscribe variable, * count of elements must be equal with csStr elements * return: NULL error or != subscribe variables array, must be free after use with mqtt_freeSub() */ -mqtt_subscr_t *mqtt_str2subs(const char **csStr, unsigned short strnum, - unsigned char *qoses); +mqtt_subscr_t *mqtt_strs2subs(const char **csStr, unsigned short strnum, unsigned char *qoses); /* * mqtt_subFree() Free array from subscribe variables * @@ -384,9 +381,9 @@ mqtt_subscr_t *mqtt_subCopy(mqtt_subscr_t * __restrict /* * mqtt_msgCONNECT() Create CONNECT message * - * @buf = Message buffer * @csConnID = ConnectID - * @kasec = Keep alive timeout, if =0 default timeout for MQTT + * @Version = MQTT version + * @KASec = Keep alive timeout, if =0 default timeout for MQTT * @csUser = Username if !=NULL * @csPass = Password for Username, only if csUser is set * @csWillTopic = Will Topic if !=NULL Will Flags set into message @@ -394,10 +391,10 @@ mqtt_subscr_t *mqtt_subCopy(mqtt_subscr_t * __restrict * @ClrSess = Clear Session subscriptions after disconnect * @WillQOS = Will QOS if csWillTopic is set * @WillRetain = Will Retain Will Message if csWillTopic is set - * return: -1 error or >-1 message size for send + * return: NULL error or allocated connect message */ -int mqtt_msgCONNECT(mqtt_msg_t * __restrict buf, const char *csConnID, - unsigned short kasec, const char *csUser, const char *csPass, +mqtt_msg_t *mqtt_msgCONNECT(const char *csConnID, unsigned char Version, + unsigned short KASec, const char *csUser, const char *csPass, const char *csWillTopic, const char *csWillMessage, unsigned char ClrSess, unsigned char WillQOS, unsigned char WillRetain); /*