--- mqtt/src/Attic/pub.c 2011/11/21 14:40:56 1.1.2.2 +++ mqtt/src/Attic/pub.c 2011/11/22 14:00:09 1.1.2.6 @@ -12,16 +12,19 @@ * @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) + 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; @@ -42,7 +45,6 @@ mqtt_msgPUBLISH(mqtt_msg_t * __restrict buf, const cha } /* 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)); @@ -52,8 +54,15 @@ mqtt_msgPUBLISH(mqtt_msg_t * __restrict buf, const cha mid->val = htons(msgID); siz += sizeof(mqtt_v_t); - /* fixed header */ + /* load with data */ + if (pData && datlen) { + data = buf->msg_base + siz; + memcpy(data, pData, datlen); + siz += datlen; + } + /* fixed header */ + MQTTHDR_MSGINIT(hdr); hdr->mqtt_msg.type = MQTT_TYPE_PUBLISH; hdr->mqtt_msg.qos = QOS; hdr->mqtt_msg.dup = Dup ? 1 : 0; @@ -84,6 +93,7 @@ _mqtt_msgPUB_(mqtt_msg_t * __restrict buf, u_char cmd, } /* fixed header */ + MQTTHDR_MSGINIT(hdr); hdr->mqtt_msg.type = cmd; *hdr->mqtt_len = sizeof(mqtt_v_t);