Skip to content

Commit

Permalink
safely set return codes
Browse files Browse the repository at this point in the history
  • Loading branch information
256dpi committed Oct 18, 2017
1 parent b6f1c81 commit 113614f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
13 changes: 7 additions & 6 deletions include/lwmqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,13 @@ typedef struct {
* The available return codes transported by the connack packet.
*/
typedef enum {
LWMQTT_CONNACK_CONNECTION_ACCEPTED = 0,
LWMQTT_CONNACK_UNACCEPTABLE_PROTOCOL = 1,
LWMQTT_CONNACK_IDENTIFIER_REJECTED = 2,
LWMQTT_CONNACK_SERVER_UNAVAILABLE = 3,
LWMQTT_CONNACK_BAD_USERNAME_OR_PASSWORD = 4,
LWMQTT_CONNACK_NOT_AUTHORIZED = 5
LWMQTT_CONNECTION_ACCEPTED = 0,
LWMQTT_UNACCEPTABLE_PROTOCOL = 1,
LWMQTT_IDENTIFIER_REJECTED = 2,
LWMQTT_SERVER_UNAVAILABLE = 3,
LWMQTT_BAD_USERNAME_OR_PASSWORD = 4,
LWMQTT_NOT_AUTHORIZED = 5,
LWMQTT_UNKNOWN_RETURN_CODE = 6
} lwmqtt_return_code_t;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ lwmqtt_err_t lwmqtt_connect(lwmqtt_client_t *client, lwmqtt_options_t options, l
}

// return error if connection was not accepted
if (*return_code != LWMQTT_CONNACK_CONNECTION_ACCEPTED) {
if (*return_code != LWMQTT_CONNECTION_ACCEPTED) {
return LWMQTT_CONNECTION_DENIED;
}

Expand Down
27 changes: 25 additions & 2 deletions src/packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,32 @@ lwmqtt_err_t lwmqtt_decode_connack(uint8_t *buf, size_t buf_len, bool *session_p
return err;
}

// set variables
// set session present
*session_present = flags.bits.session_present == 1;
*return_code = (lwmqtt_return_code_t)raw_return_code;

// set return code
switch (raw_return_code) {
case 0:
*return_code = LWMQTT_CONNECTION_ACCEPTED;
break;
case 1:
*return_code = LWMQTT_UNACCEPTABLE_PROTOCOL;
break;
case 2:
*return_code = LWMQTT_IDENTIFIER_REJECTED;
break;
case 3:
*return_code = LWMQTT_SERVER_UNAVAILABLE;
break;
case 4:
*return_code = LWMQTT_BAD_USERNAME_OR_PASSWORD;
break;
case 5:
*return_code = LWMQTT_NOT_AUTHORIZED;
break;
default:
*return_code = LWMQTT_UNKNOWN_RETURN_CODE;
}

return LWMQTT_SUCCESS;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ TEST(ConnackTest, Decode1) {
lwmqtt_err_t err = lwmqtt_decode_connack(pkt, 4, &session_present, &return_code);

EXPECT_EQ(err, LWMQTT_SUCCESS);
EXPECT_EQ(session_present, 0);
EXPECT_EQ(return_code, 0);
EXPECT_EQ(session_present, false);
EXPECT_EQ(return_code, LWMQTT_CONNECTION_ACCEPTED);
}

TEST(ConnackTest, DecodeError1) {
Expand Down

0 comments on commit 113614f

Please sign in to comment.