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.

(cherry picked from commit 45282a4)

Original-Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
GitOrigin-RevId: 45282a4
Change-Id: I67333b20bdd5716003afb336b2f9f5135dc7f889
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/zephyr/+/5753301
Tested-by: Fabio Baltieri <fabiobaltieri@google.com>
Reviewed-by: Fabio Baltieri <fabiobaltieri@google.com>
Tested-by: ChromeOS Prod (Robot) <chromeos-ci-prod@chromeos-bot.iam.gserviceaccount.com>
Commit-Queue: Fabio Baltieri <fabiobaltieri@google.com>
  • Loading branch information
ndrs-pst authored and Chromeos LUCI committed Jul 31, 2024
1 parent 6b34f01 commit 9ae1038
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 9ae1038

Please sign in to comment.