Skip to content

Commit

Permalink
net: mqtt: improve encoder 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 349bf81 commit 45282a4
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions subsys/net/lib/mqtt/mqtt_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,18 @@ static const uint8_t disc_packet[MQTT_FIXED_HEADER_MIN_SIZE] = {
*/
static int pack_uint8(uint8_t val, struct buf_ctx *buf)
{
if ((buf->end - buf->cur) < sizeof(uint8_t)) {
uint8_t *cur = buf->cur;
uint8_t *end = buf->end;

if ((end - cur) < sizeof(uint8_t)) {
return -ENOMEM;
}

NET_DBG(">> val:%02x cur:%p, end:%p", val, (void *)buf->cur, (void *)buf->end);
NET_DBG(">> val:%02x cur:%p, end:%p", val, (void *)cur, (void *)end);

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

return 0;
}
Expand All @@ -69,15 +73,18 @@ static int pack_uint8(uint8_t val, struct buf_ctx *buf)
*/
static int pack_uint16(uint16_t val, struct buf_ctx *buf)
{
if ((buf->end - buf->cur) < sizeof(uint16_t)) {
uint8_t *cur = buf->cur;
uint8_t *end = buf->end;

if ((end - cur) < sizeof(uint16_t)) {
return -ENOMEM;
}

NET_DBG(">> val:%04x cur:%p, end:%p", val, (void *)buf->cur, (void *)buf->end);
NET_DBG(">> val:%04x cur:%p, end:%p", val, (void *)cur, (void *)end);

/* Pack value. */
*(buf->cur++) = (val >> 8) & 0xFF;
*(buf->cur++) = val & 0xFF;
sys_put_be16(val, cur);
buf->cur = (cur + sizeof(uint16_t));

return 0;
}
Expand Down Expand Up @@ -470,12 +477,15 @@ int publish_complete_encode(const struct mqtt_pubcomp_param *param,

int disconnect_encode(struct buf_ctx *buf)
{
if (buf->end - buf->cur < sizeof(disc_packet)) {
uint8_t *cur = buf->cur;
uint8_t *end = buf->end;

if ((end - cur) < sizeof(disc_packet)) {
return -ENOMEM;
}

memcpy(buf->cur, disc_packet, sizeof(disc_packet));
buf->end = buf->cur + sizeof(disc_packet);
memcpy(cur, disc_packet, sizeof(disc_packet));
buf->end = (cur + sizeof(disc_packet));

return 0;
}
Expand Down Expand Up @@ -546,12 +556,15 @@ int unsubscribe_encode(const struct mqtt_subscription_list *param,

int ping_request_encode(struct buf_ctx *buf)
{
if (buf->end - buf->cur < sizeof(ping_packet)) {
uint8_t *cur = buf->cur;
uint8_t *end = buf->end;

if ((end - cur) < sizeof(ping_packet)) {
return -ENOMEM;
}

memcpy(buf->cur, ping_packet, sizeof(ping_packet));
buf->end = buf->cur + sizeof(ping_packet);
memcpy(cur, ping_packet, sizeof(ping_packet));
buf->end = (cur + sizeof(ping_packet));

return 0;
}

0 comments on commit 45282a4

Please sign in to comment.