/************************************************************************* * (C) 2011 AITNET ltd - Sofia/Bulgaria - * by Michael Pounov * * $Author: misho $ * $Id: pub.c,v 1.4 2013/05/30 09:18:33 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, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 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 * * @buf = Message buffer * @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: -1 error or >-1 message size for send */ 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) { int len, siz; u_int n, *l; struct mqtthdr *hdr; mqtthdr_var_t *topic; mqtt_len_t *mid; void *data; if (!buf || !csTopic) return -1; if (QOS > MQTT_QOS_EXACTLY) { mqtt_SetErr(EINVAL, "Invalid QoS parameter"); return -1; } if (!msgID && QOS != MQTT_QOS_ONCE) { mqtt_SetErr(EINVAL, "Invalid MessageID parameter must be >0"); return -1; } /* 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 (mqtt_msgRealloc(buf, siz + len) == -1) return -1; else { data = buf->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; 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 siz + len; } static int _mqtt_msgPUB_(mqtt_msg_t * __restrict buf, u_char cmd, u_short msgID) { int siz = sizeof(struct mqtthdr); struct mqtthdr *hdr; mqtt_len_t *v; if (!buf) return -1; 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; } /* * mqtt_msgPUBACK() Create PUBACK message * * @buf = Message buffer * @msgID = MessageID * return: -1 error or >-1 message size for send */ int mqtt_msgPUBACK(mqtt_msg_t * __restrict buf, u_short msgID) { return _mqtt_msgPUB_(buf, MQTT_TYPE_PUBACK, msgID); } /* * mqtt_msgPUBREC() Create PUBREC message * * @buf = Message buffer * @msgID = MessageID * return: -1 error or >-1 message size for send */ int mqtt_msgPUBREC(mqtt_msg_t * __restrict buf, u_short msgID) { return _mqtt_msgPUB_(buf, MQTT_TYPE_PUBREC, msgID); } /* * mqtt_msgPUBREL() Create PUBREL message * * @buf = Message buffer * @msgID = MessageID * return: -1 error or >-1 message size for send */ int mqtt_msgPUBREL(mqtt_msg_t * __restrict buf, u_short msgID) { return _mqtt_msgPUB_(buf, MQTT_TYPE_PUBREL, msgID); } /* * mqtt_msgPUBCOMP() Create PUBCOMP message * * @buf = Message buffer * @msgID = MessageID * return: -1 error or >-1 message size for send */ int mqtt_msgPUBCOMP(mqtt_msg_t * __restrict buf, u_short msgID) { return _mqtt_msgPUB_(buf, MQTT_TYPE_PUBCOMP, msgID); } /* ============= decode ============ */ /* * 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); }