/************************************************************************* * (C) 2011 AITNET ltd - Sofia/Bulgaria - * by Michael Pounov * * $Author: misho $ * $Id: pub.c,v 1.4.4.2 2022/09/14 18:36:23 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria Copyright 2004 - 2022 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. All advertising materials mentioning features or use of this software must display the following acknowledgement: This product includes software developed by Michael Pounov ELWIX - Embedded LightWeight unIX and its contributors. 4. Neither the name of AITNET nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "global.h" /* * mqtt_msgPUBLISH() Create PUBLISH message * * @csTopic = Publish topic * @msgID = MessageID >0, if QOS != MQTT_QOS_ONCE * @Dup = Duplicate message * @QOS = QoS * @Retain = Retain message * @pData = Publish data into topic * @datlen = Publish data length * return: NULL error or allocated PUBLISH message */ 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; struct mqtthdr *hdr; mqtthdr_var_t *topic; mqtt_len_t *mid; void *data; mqtt_msg_t *msg = NULL; if (!csTopic) return NULL; if (QOS > MQTT_QOS_EXACTLY) { mqtt_SetErr(EINVAL, "Invalid QoS parameter"); return NULL; } if (!msgID && QOS != MQTT_QOS_ONCE) { mqtt_SetErr(EINVAL, "Invalid MessageID parameter must be >0"); return NULL; } /* calculate message size */ len = sizeof(mqtt_len_t) + strlen(csTopic); /* topic */ len += sizeof(mqtt_len_t); /* msgid */ len += datlen; /* data len */ /* calculate header size */ siz = sizeof(struct mqtthdr); /* mqtt fixed header */ n = mqtt_encodeLen(len); /* message size */ siz += mqtt_sizeLen(n) - 1; /* length size */ if (!(msg = mqtt_msgAlloc(siz + len))) return NULL; else { data = msg->msg_base; hdr = (struct mqtthdr *) data; } /* fixed header */ hdr->mqtt_msg.type = MQTT_TYPE_PUBLISH; hdr->mqtt_msg.qos = QOS; hdr->mqtt_msg.dup = Dup ? 1 : 0; hdr->mqtt_msg.retain = Retain ? 1 : 0; l = (u_int*) hdr->mqtt_len; *l = n; data += siz; /* variable header */ topic = (mqtthdr_var_t*) data; topic->var_sb.val = htons(strlen(csTopic)); memcpy(topic->var_data, csTopic, ntohs(topic->var_sb.val)); data += MQTTHDR_VAR_SIZEOF(topic); mid = (mqtt_len_t*) data; mid->val = htons(msgID); data += sizeof(mqtt_len_t); /* load with data */ if (pData && datlen) memcpy(data, pData, datlen); return msg; } static mqtt_msg_t * _mqtt_msgPUB_(u_char cmd, u_short msgID) { struct mqtthdr *hdr; mqtt_len_t *v; mqtt_msg_t *msg = NULL; 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)); } /* fixed header */ hdr->mqtt_msg.type = cmd; *hdr->mqtt_len = sizeof(mqtt_len_t); /* MessageID */ v->val = htons(msgID); return msg; } /* * mqtt_msgPUBACK() Create PUBACK message * * @msgID = MessageID * return: NULL error or allocated PUBACK message */ mqtt_msg_t * mqtt_msgPUBACK(u_short msgID) { return _mqtt_msgPUB_(MQTT_TYPE_PUBACK, msgID); } /* * mqtt_msgPUBREC() Create PUBREC message * * @msgID = MessageID * return: NULL error or allocated PUBREC message */ mqtt_msg_t * mqtt_msgPUBREC(u_short msgID) { return _mqtt_msgPUB_(MQTT_TYPE_PUBREC, msgID); } /* * mqtt_msgPUBREL() Create PUBREL message * * @msgID = MessageID * return: NULL error or allocated PUBREL message */ mqtt_msg_t * mqtt_msgPUBREL(u_short msgID) { return _mqtt_msgPUB_(MQTT_TYPE_PUBREL, msgID); } /* * mqtt_msgPUBCOMP() Create PUBCOMP message * * @msgID = MessageID * return: NULL error or allocated PUBCOMP message */ mqtt_msg_t * mqtt_msgPUBCOMP(u_short msgID) { return _mqtt_msgPUB_(MQTT_TYPE_PUBCOMP, msgID); }