Skip to content

Commit

Permalink
simplify will
Browse files Browse the repository at this point in the history
  • Loading branch information
256dpi committed May 28, 2017
1 parent 93aa6ef commit 0a80718
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 15 deletions.
7 changes: 2 additions & 5 deletions include/lwmqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,14 @@ void lwmqtt_set_callback(lwmqtt_client_t *client, void *ref, lwmqtt_callback_t c
*/
typedef struct {
lwmqtt_string_t topic;
void *payload;
int payload_len;
bool retained;
lwmqtt_qos_t qos;
lwmqtt_message_t message;
} lwmqtt_will_t;

/**
* The default initializer for the will object.
*/
#define lwmqtt_default_will \
{ lwmqtt_default_string, NULL, 0, false, LWMQTT_QOS0 }
{ lwmqtt_default_string, lwmqtt_default_message }

/**
* The object containing the connections options for a client.
Expand Down
1 change: 0 additions & 1 deletion src/os/unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <errno.h>
#include <memory.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
Expand Down
12 changes: 6 additions & 6 deletions src/packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ lwmqtt_err_t lwmqtt_encode_connect(unsigned char *buf, int buf_len, int *len, lw

// add will if present
if (will != NULL) {
rem_len += will->topic.len + 2 + will->payload_len + 2;
rem_len += will->topic.len + 2 + will->message.payload_len + 2;
}

// add username if present
Expand Down Expand Up @@ -189,8 +189,8 @@ lwmqtt_err_t lwmqtt_encode_connect(unsigned char *buf, int buf_len, int *len, lw
// set will flags if present
if (will != NULL) {
flags.bits.will = 1;
flags.bits.will_qos = (unsigned int)will->qos;
flags.bits.will_retain = will->retained ? 1 : 0;
flags.bits.will_qos = (unsigned int)will->message.qos;
flags.bits.will_retain = will->message.retained ? 1 : 0;
}

// set username flag if present
Expand All @@ -215,9 +215,9 @@ lwmqtt_err_t lwmqtt_encode_connect(unsigned char *buf, int buf_len, int *len, lw
// write will topic and payload if present
if (will != NULL) {
lwmqtt_write_string(&ptr, will->topic);
lwmqtt_write_int(&ptr, will->payload_len);
memcpy(ptr, will->payload, will->payload_len);
ptr += will->payload_len;
lwmqtt_write_int(&ptr, will->message.payload_len);
memcpy(ptr, will->message.payload, will->message.payload_len);
ptr += will->message.payload_len;
}

// write username if present
Expand Down
6 changes: 3 additions & 3 deletions tests/packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ TEST(ConnectTest, Encode1) {

lwmqtt_will_t will = lwmqtt_default_will;
will.topic = lwmqtt_str("will");
will.payload = (void*)"send me home";
will.payload_len = (int)strlen((const char*)will.payload);
will.qos = LWMQTT_QOS1;
will.message.payload = (void*)"send me home";
will.message.payload_len = (int)strlen((const char*)will.message.payload);
will.message.qos = LWMQTT_QOS1;

lwmqtt_options_t opts = lwmqtt_default_options;
opts.clean_session = false;
Expand Down

0 comments on commit 0a80718

Please sign in to comment.