Skip to content

Commit 7c91c08

Browse files
committed
updated lwmqtt
1 parent ae5ce21 commit 7c91c08

File tree

4 files changed

+81
-7
lines changed

4 files changed

+81
-7
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fmt:
55

66
update:
77
rm -rf ./lwmqtt
8-
git clone --branch v0.6.4 https://github.com/256dpi/lwmqtt.git ./lwmqtt
8+
git clone --branch v0.7.0 https://github.com/256dpi/lwmqtt.git ./lwmqtt
99
mkdir -p ./src/lwmqtt
1010
cp -r ./lwmqtt/src/*.c ./src/lwmqtt/
1111
cp -r ./lwmqtt/src/*.h ./src/lwmqtt/

src/lwmqtt/client.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ void lwmqtt_init(lwmqtt_client_t *client, uint8_t *write_buf, size_t write_buf_s
2222
client->command_timer = NULL;
2323
client->timer_set = NULL;
2424
client->timer_get = NULL;
25+
26+
client->drop_overflow = false;
27+
client->overflow_counter = NULL;
2528
}
2629

2730
void lwmqtt_set_network(lwmqtt_client_t *client, void *ref, lwmqtt_network_read_t read, lwmqtt_network_write_t write) {
@@ -46,6 +49,11 @@ void lwmqtt_set_callback(lwmqtt_client_t *client, void *ref, lwmqtt_callback_t c
4649
client->callback = cb;
4750
}
4851

52+
void lwmqtt_drop_overflow(lwmqtt_client_t *client, bool enabled, uint32_t *counter) {
53+
client->drop_overflow = enabled;
54+
client->overflow_counter = counter;
55+
}
56+
4957
static uint16_t lwmqtt_get_next_packet_id(lwmqtt_client_t *client) {
5058
// check overflow
5159
if (client->last_packet_id == 65535) {
@@ -91,6 +99,36 @@ static lwmqtt_err_t lwmqtt_read_from_network(lwmqtt_client_t *client, size_t off
9199
return LWMQTT_SUCCESS;
92100
}
93101

102+
static lwmqtt_err_t lwmqtt_drain_network(lwmqtt_client_t *client, size_t amount) {
103+
// read while data is left
104+
while (amount > 0) {
105+
// check remaining time
106+
int32_t remaining_time = client->timer_get(client->command_timer);
107+
if (remaining_time <= 0) {
108+
return LWMQTT_NETWORK_TIMEOUT;
109+
}
110+
111+
// get max read
112+
size_t max_read = amount;
113+
if (max_read > client->read_buf_size) {
114+
max_read = client->read_buf_size;
115+
}
116+
117+
// read
118+
size_t partial_read = 0;
119+
lwmqtt_err_t err =
120+
client->network_read(client->network, client->read_buf, max_read, &partial_read, (uint32_t)remaining_time);
121+
if (err != LWMQTT_SUCCESS) {
122+
return err;
123+
}
124+
125+
// decrement counter
126+
amount -= partial_read;
127+
}
128+
129+
return LWMQTT_SUCCESS;
130+
}
131+
94132
static lwmqtt_err_t lwmqtt_write_to_network(lwmqtt_client_t *client, size_t offset, size_t len) {
95133
// prepare counter
96134
size_t written = 0;
@@ -161,6 +199,26 @@ static lwmqtt_err_t lwmqtt_read_packet_in_buffer(lwmqtt_client_t *client, size_t
161199
return err;
162200
}
163201

202+
// handle overflow
203+
if (client->drop_overflow && 1 + len + rem_len > client->read_buf_size) {
204+
// drain network
205+
err = lwmqtt_drain_network(client, rem_len);
206+
if (err != LWMQTT_SUCCESS) {
207+
return err;
208+
}
209+
210+
// unset packet
211+
*packet_type = LWMQTT_NO_PACKET;
212+
*read = 0;
213+
214+
// increment if counter is available
215+
if (client->overflow_counter != NULL) {
216+
*client->overflow_counter += 1;
217+
}
218+
219+
return LWMQTT_SUCCESS;
220+
}
221+
164222
// read the rest of the buffer if needed
165223
if (rem_len > 0) {
166224
err = lwmqtt_read_from_network(client, 1 + len, rem_len);

src/lwmqtt/helpers.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
#include "helpers.h"
44

5-
uint8_t lwmqtt_read_bits(uint8_t byte, int pos, int num) { return (byte & (uint8_t)((~(0xFF << num)) << pos)) >> pos; }
5+
uint8_t lwmqtt_read_bits(uint8_t byte, int pos, int num) {
6+
return (byte & (uint8_t)((~(0xFF << (uint8_t)num)) << (uint8_t)pos)) >> (uint8_t)pos;
7+
}
68

79
void lwmqtt_write_bits(uint8_t *byte, uint8_t value, int pos, int num) {
8-
*byte = (*byte & ~(uint8_t)((~(0xFF << num)) << pos)) | (value << pos);
10+
*byte = (*byte & ~(uint8_t)((~(0xFFu << (uint8_t)num)) << (uint8_t)pos)) | (value << (uint8_t)pos);
911
}
1012

1113
lwmqtt_err_t lwmqtt_read_data(uint8_t **buf, const uint8_t *buf_end, uint8_t **data, size_t len) {
@@ -199,11 +201,11 @@ lwmqtt_err_t lwmqtt_read_varnum(uint8_t **buf, const uint8_t *buf_end, uint32_t
199201
byte = (*buf)[len - 1];
200202

201203
// add byte to number
202-
*varnum += (byte & 127) * multiplier;
204+
*varnum += (byte & 127u) * multiplier;
203205

204206
// increase multiplier
205207
multiplier *= 128;
206-
} while ((byte & 128) != 0);
208+
} while ((byte & 128u) != 0);
207209

208210
// adjust pointer
209211
*buf += len;
@@ -235,7 +237,7 @@ lwmqtt_err_t lwmqtt_write_varnum(uint8_t **buf, const uint8_t *buf_end, uint32_t
235237

236238
// set the top bit of this byte if there are more to encode
237239
if (varnum > 0) {
238-
byte |= 0x80;
240+
byte |= 0x80u;
239241
}
240242

241243
// write byte

src/lwmqtt/lwmqtt.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ struct lwmqtt_client_t {
163163
void *command_timer;
164164
lwmqtt_timer_set_t timer_set;
165165
lwmqtt_timer_get_t timer_get;
166+
167+
bool drop_overflow;
168+
uint32_t *overflow_counter;
166169
};
167170

168171
/**
@@ -208,6 +211,16 @@ void lwmqtt_set_timers(lwmqtt_client_t *client, void *keep_alive_timer, void *co
208211
*/
209212
void lwmqtt_set_callback(lwmqtt_client_t *client, void *ref, lwmqtt_callback_t cb);
210213

214+
/**
215+
* Will configure the client to drop packets that overflow the read buffer. If a counter is provided it will be
216+
* incremented with each dropped packet.
217+
*
218+
* @param client - The client.
219+
* @param enabled - Whether dropping is enabled.
220+
* @param counter - The dropped packet counter.
221+
*/
222+
void lwmqtt_drop_overflow(lwmqtt_client_t *client, bool enabled, uint32_t *counter);
223+
211224
/**
212225
* The object defining the last will of a client.
213226
*/
@@ -271,7 +284,8 @@ lwmqtt_err_t lwmqtt_connect(lwmqtt_client_t *client, lwmqtt_options_t options, l
271284
lwmqtt_return_code_t *return_code, uint32_t timeout);
272285

273286
/**
274-
* Will send a publish packet and wait for all acks to complete.
287+
* Will send a publish packet and wait for all acks to complete. If the encoded packet is bigger than the write buffer
288+
* the function will return LWMQTT_BUFFER_TOO_SHORT without attempting to send the packet.
275289
*
276290
* Note: The message callback might be called with incoming messages as part of this call.
277291
*

0 commit comments

Comments
 (0)