#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, u_short datlen) { int siz = 0; struct mqtthdr *hdr; mqtthdr_var_t *topic; mqtt_v_t *mid; void *data; if (!buf || !csTopic) return -1; if (QOS > MQTT_QOS_EXACTLY) { mqtt_SetErr(EINVAL, "Error:: invalid QoS parameter"); return -1; } if (!msgID && QOS != MQTT_QOS_ONCE) { mqtt_SetErr(EINVAL, "Error:: invalid MessageID parameter must be >0"); return -1; } if (mqtt_msgRealloc(buf, MQTTMSG_MAX) == -1) return -1; else { hdr = (struct mqtthdr *) (buf->msg_base + siz); siz += sizeof(struct mqtthdr); } /* variable header */ topic = (mqtthdr_var_t*) (buf->msg_base + siz); topic->var_sb.val = htons(strlen(csTopic)); memcpy(topic->var_data, csTopic, ntohs(topic->var_sb.val)); siz += MQTTHDR_VAR_SIZEOF(topic); mid = (mqtt_v_t*) (buf->msg_base + siz); mid->val = htons(msgID); siz += sizeof(mqtt_v_t); /* load with data */ if (pData && datlen) { data = buf->msg_base + siz; memcpy(data, pData, datlen); siz += datlen; } /* 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; *hdr->mqtt_len = mqtt_encodeLen(siz - sizeof(struct mqtthdr)); mqtt_msgRealloc(buf, siz); return siz; } static int _mqtt_msgPUB_(mqtt_msg_t * __restrict buf, u_char cmd, u_short msgID) { int siz = 0; struct mqtthdr *hdr; mqtt_v_t *v; if (!buf) return -1; if (mqtt_msgRealloc(buf, sizeof(struct mqtthdr) + sizeof(mqtt_v_t)) == -1) return -1; else { hdr = (struct mqtthdr *) (buf->msg_base + siz); siz += sizeof(struct mqtthdr); v = (mqtt_v_t*) (buf->msg_base + siz); siz += sizeof(mqtt_v_t); } /* fixed header */ hdr->mqtt_msg.type = cmd; *hdr->mqtt_len = sizeof(mqtt_v_t); /* MessageID */ v->val = htons(msgID); return siz; } /* * mqtt_msgPUBACK() Create PUBACK message * * @buf = Message buffer * @msgID = MessageID * return: -1 error or >-1 message size for send */ inline 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 */ inline 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 */ inline 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 */ inline int mqtt_msgPUBCOMP(mqtt_msg_t * __restrict buf, u_short msgID) { return _mqtt_msgPUB_(buf, MQTT_TYPE_PUBCOMP, msgID); }