in src/mqtt_codec.c [226:280]
static int constructPublishVariableHeader(BUFFER_HANDLE ctrlPacket, const PUBLISH_HEADER_INFO* publishHeader, STRING_HANDLE trace_log)
{
int result = 0;
size_t topicLen = 0;
size_t spaceLen = 0;
size_t idLen = 0;
size_t currLen = BUFFER_length(ctrlPacket);
topicLen = strlen(publishHeader->topicName);
spaceLen += 2;
if (publishHeader->qualityOfServiceValue != DELIVER_AT_MOST_ONCE)
{
// Packet Id is only set if the QOS is not 0
idLen = 2;
}
if (topicLen > USHRT_MAX)
{
result = MU_FAILURE;
}
else if (BUFFER_enlarge(ctrlPacket, topicLen + idLen + spaceLen) != 0)
{
result = MU_FAILURE;
}
else
{
uint8_t* iterator = BUFFER_u_char(ctrlPacket);
if (iterator == NULL)
{
result = MU_FAILURE;
}
else
{
iterator += currLen;
/* The Topic Name MUST be present as the first field in the PUBLISH Packet Variable header.It MUST be 792 a UTF-8 encoded string [MQTT-3.3.2-1] as defined in section 1.5.3.*/
byteutil_writeUTF(&iterator, publishHeader->topicName, (uint16_t)topicLen);
if (trace_log != NULL)
{
STRING_sprintf(trace_log, " | TOPIC_NAME: %s", publishHeader->topicName);
}
if (idLen > 0)
{
if (trace_log != NULL)
{
STRING_sprintf(trace_log, " | PACKET_ID: %"PRIu16, publishHeader->packetId);
}
byteutil_writeInt(&iterator, publishHeader->packetId);
}
result = 0;
}
}
return result;
}