Skip to content

Commit

Permalink
net: mqtt: improve decoder buffer handling
Browse files Browse the repository at this point in the history
Improve buffer handling logic to use local variables extensively.

This change reduces the number of pointer dereferences, which leads
to more efficient runtime and helps reduce the code size.

Signed-off-by: Pisit Sawangvonganan <[email protected]>
  • Loading branch information
ndrs-pst authored and carlescufi committed Jul 31, 2024
1 parent 45282a4 commit 6211de8
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions subsys/net/lib/mqtt/mqtt_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ LOG_MODULE_REGISTER(net_mqtt_dec, CONFIG_MQTT_LOG_LEVEL);
*/
static int unpack_uint8(struct buf_ctx *buf, uint8_t *val)
{
NET_DBG(">> cur:%p, end:%p", (void *)buf->cur, (void *)buf->end);
uint8_t *cur = buf->cur;
uint8_t *end = buf->end;

NET_DBG(">> cur:%p, end:%p", (void *)cur, (void *)end);

if ((buf->end - buf->cur) < sizeof(uint8_t)) {
if ((end - cur) < sizeof(uint8_t)) {
return -EINVAL;
}

*val = *(buf->cur++);
*val = cur[0];
buf->cur = (cur + sizeof(uint8_t));

NET_DBG("<< val:%02x", *val);

Expand All @@ -55,14 +59,17 @@ static int unpack_uint8(struct buf_ctx *buf, uint8_t *val)
*/
static int unpack_uint16(struct buf_ctx *buf, uint16_t *val)
{
NET_DBG(">> cur:%p, end:%p", (void *)buf->cur, (void *)buf->end);
uint8_t *cur = buf->cur;
uint8_t *end = buf->end;

NET_DBG(">> cur:%p, end:%p", (void *)cur, (void *)end);

if ((buf->end - buf->cur) < sizeof(uint16_t)) {
if ((end - cur) < sizeof(uint16_t)) {
return -EINVAL;
}

*val = *(buf->cur++) << 8; /* MSB */
*val |= *(buf->cur++); /* LSB */
*val = sys_get_be16(cur);
buf->cur = (cur + sizeof(uint16_t));

NET_DBG("<< val:%04x", *val);

Expand Down

0 comments on commit 6211de8

Please sign in to comment.