Skip to content

Commit

Permalink
fixed wrong connect flags
Browse files Browse the repository at this point in the history
  • Loading branch information
256dpi committed Jun 7, 2018
1 parent ec70559 commit b371fc9
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ lwmqtt_err_t lwmqtt_encode_connect(uint8_t *buf, size_t buf_len, size_t *len, lw

// set username flag if present
if (options.username.len > 0) {
lwmqtt_write_bits(&flags, 1, 6, 1);
lwmqtt_write_bits(&flags, 1, 7, 1);

// set password flag if present
if (options.password.len > 0) {
lwmqtt_write_bits(&flags, 1, 7, 1);
lwmqtt_write_bits(&flags, 1, 6, 1);
}
}

Expand Down
74 changes: 74 additions & 0 deletions tests/packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,80 @@ TEST(ConnectTest, Encode2) {
EXPECT_ARRAY_EQ(pkt, buf, len);
}

TEST(ConnectTest, Encode3) {
uint8_t pkt[50] = {
LWMQTT_CONNECT_PACKET << 4,
48,
0, // Protocol String MSB
4, // Protocol String LSB
'M',
'Q',
'T',
'T',
4, // Protocol level 4
140, // Connect Flags
0, // Keep Alive MSB
10, // Keep Alive LSB
0, // Client ID MSB
7, // Client ID LSB
's',
'u',
'r',
'g',
'e',
'm',
'q',
0, // Will Topic MSB
4, // Will Topic LSB
'w',
'i',
'l',
'l',
0, // Will Message MSB
12, // Will Message LSB
's',
'e',
'n',
'd',
' ',
'm',
'e',
' ',
'h',
'o',
'm',
'e',
0, // Username ID MSB
7, // Username ID LSB
's',
'u',
'r',
'g',
'e',
'm',
'q',
};

uint8_t buf[62];

lwmqtt_will_t will = lwmqtt_default_will;
will.topic = lwmqtt_string("will");
will.payload = lwmqtt_string("send me home");
will.qos = LWMQTT_QOS1;

lwmqtt_options_t opts = lwmqtt_default_options;
opts.clean_session = false;
opts.keep_alive = 10;
opts.client_id = lwmqtt_string("surgemq");
opts.username = lwmqtt_string("surgemq");

size_t len;
lwmqtt_err_t err = lwmqtt_encode_connect(buf, 62, &len, opts, &will);

EXPECT_EQ(err, LWMQTT_SUCCESS);
EXPECT_ARRAY_EQ(pkt, buf, len);
}

TEST(ConnectTest, EncodeError1) {
uint8_t buf[4]; // <- too small buffer

Expand Down

0 comments on commit b371fc9

Please sign in to comment.