--- libaitmqtt/src/pub.c 2013/05/30 09:18:33 1.4 +++ libaitmqtt/src/pub.c 2022/09/14 17:37:13 1.4.4.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: pub.c,v 1.4 2013/05/30 09:18:33 misho Exp $ +* $Id: pub.c,v 1.4.4.1 2022/09/14 17:37:13 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 +Copyright 2004 - 2022 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -49,7 +49,6 @@ SUCH DAMAGE. /* * mqtt_msgPUBLISH() Create PUBLISH message * - * @buf = Message buffer * @csTopic = Publish topic * @msgID = MessageID >0, if QOS != MQTT_QOS_ONCE * @Dup = Duplicate message @@ -57,11 +56,11 @@ SUCH DAMAGE. * @Retain = Retain message * @pData = Publish data into topic * @datlen = Publish data length - * return: -1 error or >-1 message size for send + * return: NULL error or allocated PUBLISH message */ -int -mqtt_msgPUBLISH(mqtt_msg_t * __restrict buf, const char *csTopic, u_short msgID, - u_char Dup, u_char QOS, u_char Retain, const void *pData, int datlen) +mqtt_msg_t * +mqtt_msgPUBLISH(const char *csTopic, u_short msgID, u_char Dup, + u_char QOS, u_char Retain, const void *pData, int datlen) { int len, siz; u_int n, *l; @@ -69,16 +68,17 @@ mqtt_msgPUBLISH(mqtt_msg_t * __restrict buf, const cha mqtthdr_var_t *topic; mqtt_len_t *mid; void *data; + mqtt_msg_t *msg = NULL; - if (!buf || !csTopic) - return -1; + if (!csTopic) + return NULL; if (QOS > MQTT_QOS_EXACTLY) { mqtt_SetErr(EINVAL, "Invalid QoS parameter"); - return -1; + return NULL; } if (!msgID && QOS != MQTT_QOS_ONCE) { mqtt_SetErr(EINVAL, "Invalid MessageID parameter must be >0"); - return -1; + return NULL; } /* calculate message size */ @@ -91,15 +91,14 @@ mqtt_msgPUBLISH(mqtt_msg_t * __restrict buf, const cha n = mqtt_encodeLen(len); /* message size */ siz += mqtt_sizeLen(n) - 1; /* length size */ - if (mqtt_msgRealloc(buf, siz + len) == -1) - return -1; + if (!(msg = mqtt_msgAlloc(siz + len))) + return NULL; else { - data = buf->msg_base; + data = msg->msg_base; hdr = (struct mqtthdr *) data; } /* fixed header */ - MQTTHDR_MSGINIT(hdr); hdr->mqtt_msg.type = MQTT_TYPE_PUBLISH; hdr->mqtt_msg.qos = QOS; hdr->mqtt_msg.dup = Dup ? 1 : 0; @@ -122,92 +121,85 @@ mqtt_msgPUBLISH(mqtt_msg_t * __restrict buf, const cha if (pData && datlen) memcpy(data, pData, datlen); - return siz + len; + return msg; } -static int -_mqtt_msgPUB_(mqtt_msg_t * __restrict buf, u_char cmd, u_short msgID) +static mqtt_msg_t * +_mqtt_msgPUB_(u_char cmd, u_short msgID) { - int siz = sizeof(struct mqtthdr); struct mqtthdr *hdr; mqtt_len_t *v; + mqtt_msg_t *msg = NULL; - if (!buf) - return -1; + if (!(msg = mqtt_msgAlloc(sizeof(struct mqtthdr) + sizeof(mqtt_len_t)))) + return NULL; + else { + hdr = (struct mqtthdr *) msg->msg_base; + v = (mqtt_len_t*) (msg->msg_base + sizeof(struct mqtthdr)); + } - if (mqtt_msgRealloc(buf, sizeof(struct mqtthdr) + sizeof(mqtt_len_t)) == -1) - return -1; - else - hdr = (struct mqtthdr *) buf->msg_base; - /* fixed header */ - MQTTHDR_MSGINIT(hdr); hdr->mqtt_msg.type = cmd; *hdr->mqtt_len = sizeof(mqtt_len_t); /* MessageID */ - v = (mqtt_len_t*) (buf->msg_base + siz); v->val = htons(msgID); - siz += sizeof(mqtt_len_t); - return siz; + return msg; } /* * mqtt_msgPUBACK() Create PUBACK message * - * @buf = Message buffer * @msgID = MessageID - * return: -1 error or >-1 message size for send + * return: NULL error or allocated PUBACK message */ -int -mqtt_msgPUBACK(mqtt_msg_t * __restrict buf, u_short msgID) +mqtt_msg_t * +mqtt_msgPUBACK(u_short msgID) { - return _mqtt_msgPUB_(buf, MQTT_TYPE_PUBACK, msgID); + return _mqtt_msgPUB_(MQTT_TYPE_PUBACK, msgID); } /* * mqtt_msgPUBREC() Create PUBREC message * - * @buf = Message buffer * @msgID = MessageID - * return: -1 error or >-1 message size for send + * return: NULL error or allocated PUBREC message */ -int -mqtt_msgPUBREC(mqtt_msg_t * __restrict buf, u_short msgID) +mqtt_msg_t * +mqtt_msgPUBREC(u_short msgID) { - return _mqtt_msgPUB_(buf, MQTT_TYPE_PUBREC, msgID); + return _mqtt_msgPUB_(MQTT_TYPE_PUBREC, msgID); } /* * mqtt_msgPUBREL() Create PUBREL message * - * @buf = Message buffer * @msgID = MessageID - * return: -1 error or >-1 message size for send + * return: NULL error or allocated PUBREL message */ -int -mqtt_msgPUBREL(mqtt_msg_t * __restrict buf, u_short msgID) +mqtt_msg_t * +mqtt_msgPUBREL(u_short msgID) { - return _mqtt_msgPUB_(buf, MQTT_TYPE_PUBREL, msgID); + return _mqtt_msgPUB_(MQTT_TYPE_PUBREL, msgID); } /* * mqtt_msgPUBCOMP() Create PUBCOMP message * - * @buf = Message buffer * @msgID = MessageID - * return: -1 error or >-1 message size for send + * return: NULL error or allocated PUBCOMP message */ -int -mqtt_msgPUBCOMP(mqtt_msg_t * __restrict buf, u_short msgID) +mqtt_msg_t * +mqtt_msgPUBCOMP(u_short msgID) { - return _mqtt_msgPUB_(buf, MQTT_TYPE_PUBCOMP, msgID); + return _mqtt_msgPUB_(MQTT_TYPE_PUBCOMP, msgID); } /* ============= decode ============ */ +#if 0 /* * mqtt_readPUBLISH() Read PUBLISH message * @@ -387,3 +379,4 @@ mqtt_readPUBCOMP(mqtt_msg_t * __restrict buf) return ntohs(v->val); } +#endif