parent
9e41ff3497
commit
eaecdbe36c
2 changed files with 32 additions and 3 deletions
10
src/packet.c
10
src/packet.c
|
@ -427,7 +427,9 @@ Buffer *encode_connack(ConnAckPayload *payload) {
|
||||||
Buffer *encode_publish(PublishPayload *payload) {
|
Buffer *encode_publish(PublishPayload *payload) {
|
||||||
size_t sz = 0;
|
size_t sz = 0;
|
||||||
sz += strlen(payload->topic) + 2; // topic
|
sz += strlen(payload->topic) + 2; // topic
|
||||||
sz += 2; // packet id
|
if (payload->qos != MQTT_QOS_0) {
|
||||||
|
sz += 2; // packet id
|
||||||
|
}
|
||||||
if (payload->message) {
|
if (payload->message) {
|
||||||
sz += strlen(payload->message);
|
sz += strlen(payload->message);
|
||||||
}
|
}
|
||||||
|
@ -450,8 +452,10 @@ Buffer *encode_publish(PublishPayload *payload) {
|
||||||
|
|
||||||
// Variable header
|
// Variable header
|
||||||
utf8_string_encode(payload->topic, buffer);
|
utf8_string_encode(payload->topic, buffer);
|
||||||
buffer->data[buffer->position++] = (payload->packet_id & 0xff00) >> 8;
|
if (payload->qos != MQTT_QOS_0) {
|
||||||
buffer->data[buffer->position++] = (payload->packet_id & 0xff);
|
buffer->data[buffer->position++] = (payload->packet_id & 0xff00) >> 8;
|
||||||
|
buffer->data[buffer->position++] = (payload->packet_id & 0xff);
|
||||||
|
}
|
||||||
|
|
||||||
// Payload
|
// Payload
|
||||||
if (payload->message) {
|
if (payload->message) {
|
||||||
|
|
|
@ -314,6 +314,30 @@ TestResult test_encode_publish_with_msg(void) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TestResult test_encode_publish_with_msg_qos0(void) {
|
||||||
|
char data[] = {
|
||||||
|
0x31, 0x13, // header, qos1, retain
|
||||||
|
0x00, 0x0a, 't', 'e', 's', 't', '/', 't', 'o', 'p', 'i', 'c',
|
||||||
|
'p', 'a', 'y', 'l', 'o', 'a', 'd'
|
||||||
|
};
|
||||||
|
PublishPayload *payload = calloc(1, sizeof(PublishPayload));
|
||||||
|
|
||||||
|
payload->qos = MQTT_QOS_0;
|
||||||
|
payload->retain = true;
|
||||||
|
payload->duplicate = false;
|
||||||
|
payload->topic = "test/topic";
|
||||||
|
payload->packet_id = 10;
|
||||||
|
payload->message = "payload";
|
||||||
|
|
||||||
|
Buffer *encoded = mqtt_packet_encode(&(MQTTPacket){ PacketTypePublish, payload });
|
||||||
|
free(payload);
|
||||||
|
|
||||||
|
return TESTMEMCMP(
|
||||||
|
buffer_from_data_copy(data, sizeof(data)),
|
||||||
|
encoded
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
TestResult test_encode_puback(void) {
|
TestResult test_encode_puback(void) {
|
||||||
char data[] = {
|
char data[] = {
|
||||||
0x40, 0x02, // header
|
0x40, 0x02, // header
|
||||||
|
@ -528,6 +552,7 @@ TESTS(
|
||||||
TEST("Encode Publish with no message", test_encode_publish_no_msg),
|
TEST("Encode Publish with no message", test_encode_publish_no_msg),
|
||||||
TEST("Encode Publish with invalid flags", test_encode_publish_dup_qos0),
|
TEST("Encode Publish with invalid flags", test_encode_publish_dup_qos0),
|
||||||
TEST("Encode Publish with message", test_encode_publish_with_msg),
|
TEST("Encode Publish with message", test_encode_publish_with_msg),
|
||||||
|
TEST("Encode Publish with message on QoS 0", test_encode_publish_with_msg_qos0),
|
||||||
TEST("Encode PubAck", test_encode_puback),
|
TEST("Encode PubAck", test_encode_puback),
|
||||||
TEST("Encode PubRec", test_encode_pubrec),
|
TEST("Encode PubRec", test_encode_pubrec),
|
||||||
TEST("Encode PubRel", test_encode_pubrel),
|
TEST("Encode PubRel", test_encode_pubrel),
|
||||||
|
|
Loading…
Reference in a new issue