/************************************************************************* * (C) 2011 AITNET ltd - Sofia/Bulgaria - * by Michael Pounov * * $Author: misho $ * $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 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); } /* ============= decode ============ */ #if 0 /* * mqtt_readPUBLISH() Read PUBLISH message * * @buf = Message buffer * @psTopic = Topic * @topicLen = Topic length * @msgID = MessageID * @pData = Data buffer, may be NULL * return: -1 error or !=-1 allocated data buffer length */ int mqtt_readPUBLISH(mqtt_msg_t * __restrict buf, char * __restrict psTopic, int topicLen, u_short *msgID, void ** __restrict pData) { int len, ret; struct mqtthdr *hdr; mqtthdr_var_t *var; mqtt_len_t *v; caddr_t pos; if (!buf || !psTopic || !msgID) return -1; hdr = _mqtt_readHEADER(buf, MQTT_TYPE_PUBLISH, &ret, &len); if (!hdr) return -1; pos = buf->msg_base + ret + 1; var = (mqtthdr_var_t*) pos; /* topic */ len -= MQTTHDR_VAR_SIZEOF(var); if (len < 0) { mqtt_SetErr(EINVAL, "Short message length %d", len); return -1; } else { memset(psTopic, 0, topicLen--); memcpy(psTopic, var->var_data, ntohs(var->var_sb.val) > topicLen ? topicLen : ntohs(var->var_sb.val)); pos += MQTTHDR_VAR_SIZEOF(var); v = (mqtt_len_t*) pos; } len -= sizeof(mqtt_len_t); if (len < 0) { mqtt_SetErr(EINVAL, "Short message length %d", len); return -1; } else { *msgID = ntohs(v->val); pos += sizeof(mqtt_len_t); } /* data */ if (len < 0) { mqtt_SetErr(EINVAL, "Short message length %d", len); return -1; } else if (pData) { if (!(*pData = malloc(len + 1))) { LOGERR; return -1; } else ((char*) (*pData))[len] = 0; memcpy(*pData, pos, len); } return len; } /* * mqtt_readPUBACK() Read PUBACK message * * @buf = Message buffer * return: -1 error or MessageID */ u_short mqtt_readPUBACK(mqtt_msg_t * __restrict buf) { int len, ret; struct mqtthdr *hdr; mqtt_len_t *v; caddr_t pos; hdr = _mqtt_readHEADER(buf, MQTT_TYPE_PUBACK, &ret, &len); if (!hdr) return (u_short) -1; if (len < sizeof(mqtt_len_t)) { mqtt_SetErr(EINVAL, "Short message length %d", len); return (u_short) -1; } else { pos = buf->msg_base + ret + 1; v = (mqtt_len_t*) pos; } return ntohs(v->val); } /* * mqtt_readPUBREC() Read PUBREC message * * @buf = Message buffer * return: -1 error or MessageID */ u_short mqtt_readPUBREC(mqtt_msg_t * __restrict buf) { int len, ret; struct mqtthdr *hdr; mqtt_len_t *v; caddr_t pos; hdr = _mqtt_readHEADER(buf, MQTT_TYPE_PUBREC, &ret, &len); if (!hdr) return (u_short) -1; if (len < sizeof(mqtt_len_t)) { mqtt_SetErr(EINVAL, "Short message length %d", len); return (u_short) -1; } else { pos = buf->msg_base + ret + 1; v = (mqtt_len_t*) pos; } return ntohs(v->val); } /* * mqtt_readPUBREL() Read PUBREL message * * @buf = Message buffer * return: -1 error or MessageID */ u_short mqtt_readPUBREL(mqtt_msg_t * __restrict buf) { int len, ret; struct mqtthdr *hdr; mqtt_len_t *v; caddr_t pos; hdr = _mqtt_readHEADER(buf, MQTT_TYPE_PUBREL, &ret, &len); if (!hdr) return (u_short) -1; if (len < sizeof(mqtt_len_t)) { mqtt_SetErr(EINVAL, "Short message length %d", len); return (u_short) -1; } else { pos = buf->msg_base + ret + 1; v = (mqtt_len_t*) pos; } return ntohs(v->val); } /* * mqtt_readPUBCOMP() Read PUBCOMP message * * @buf = Message buffer * return: -1 error or MessageID */ u_short mqtt_readPUBCOMP(mqtt_msg_t * __restrict buf) { int len, ret; struct mqtthdr *hdr; mqtt_len_t *v; caddr_t pos; hdr = _mqtt_readHEADER(buf, MQTT_TYPE_PUBCOMP, &ret, &len); if (!hdr) return (u_short) -1; if (len < sizeof(mqtt_len_t)) { mqtt_SetErr(EINVAL, "Short message length %d", len); return (u_short) -1; } else { pos = buf->msg_base + ret + 1; v = (mqtt_len_t*) pos; } return ntohs(v->val); } #endif