Skip to content

Commit

Permalink
Merge pull request #17 from metratec/binary-will
Browse files Browse the repository at this point in the history
Support for binary will and important bug in handling PUBLISH messages
  • Loading branch information
zhaojh329 authored Aug 29, 2024
2 parents 6ba5bc2 + 0018d8e commit e64db61
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/umqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ static void handle_unsuback(struct umqtt_client *cl)
static void handle_publish(struct umqtt_client *cl)
{
struct umqtt_packet *pkt = &cl->pkt;
uint8_t qos = (pkt->flags > 1) & 0x03;
bool dup = (pkt->flags > 3) & 0x1;
uint8_t qos = (pkt->flags >> 1) & 0x03;
bool dup = (pkt->flags >> 3) & 0x1;
struct buffer *rb = &cl->rb;
uint16_t mid = 0;
int payloadlen;
Expand Down Expand Up @@ -345,7 +345,7 @@ static int umqtt_connect(struct umqtt_client *cl, struct umqtt_connect_opts *opt
will = true;

remlen += strlen(opts->will_topic) + 2;
remlen += strlen(opts->will_message) + 2;
remlen += (opts->will_message_len ? : strlen(opts->will_message)) + 2;

flags |= 1 << 2;
flags |= (opts->will_qos & 0x3) << 3;
Expand Down Expand Up @@ -378,8 +378,10 @@ static int umqtt_connect(struct umqtt_client *cl, struct umqtt_connect_opts *opt
umqtt_put_string(wb, client_id);

if (will) {
size_t will_message_len = opts->will_message_len ? : strlen(opts->will_message);
umqtt_put_string(wb, opts->will_topic);
umqtt_put_string(wb, opts->will_message);
buffer_put_u16(wb, htons(will_message_len));
buffer_put_data(wb, opts->will_message, will_message_len);
}

if (opts->username) {
Expand Down
3 changes: 2 additions & 1 deletion src/umqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ struct umqtt_connect_opts {
const char *password;

const char *will_topic;
const char *will_message;
const void *will_message;
size_t will_message_len;
uint8_t will_qos;
bool will_retain;
};
Expand Down

0 comments on commit e64db61

Please sign in to comment.