Skip to content

Commit 05cd342

Browse files
hakonfamjukkar
authored andcommitted
net: mqtt: add mqtt_readall_publish_payload()
This function uses mqtt_read_publish_payload_blocking to perform a blocking read of the specified number of bytes. When reading out a payload, the normal use case is to read the entire payload. This function facilitates that use case. Signed-off-by: Håkon Øye Amundsen <[email protected]>
1 parent e1f0b61 commit 05cd342

File tree

3 files changed

+48
-19
lines changed

3 files changed

+48
-19
lines changed

include/net/mqtt.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,21 @@ int mqtt_read_publish_payload(struct mqtt_client *client, void *buffer,
715715
int mqtt_read_publish_payload_blocking(struct mqtt_client *client, void *buffer,
716716
size_t length);
717717

718+
/**
719+
* @brief Blocking version of @ref mqtt_read_publish_payload function which
720+
* runs until the required number of bytes are read.
721+
*
722+
* @param[in] client Client instance for which the procedure is requested.
723+
* Shall not be NULL.
724+
* @param[out] buffer Buffer where payload should be stored.
725+
* @param[in] length Number of bytes to read.
726+
*
727+
* @return 0 if success, otherwise a negative error code (errno.h) indicating
728+
* reason of failure.
729+
*/
730+
int mqtt_readall_publish_payload(struct mqtt_client *client, u8_t *buffer,
731+
size_t length);
732+
718733
#ifdef __cplusplus
719734
}
720735
#endif

subsys/net/lib/mqtt/mqtt.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,3 +647,25 @@ int mqtt_read_publish_payload_blocking(struct mqtt_client *client, void *buffer,
647647
{
648648
return read_publish_payload(client, buffer, length, true);
649649
}
650+
651+
int mqtt_readall_publish_payload(struct mqtt_client *client, u8_t *buffer,
652+
size_t length)
653+
{
654+
u8_t *end = buffer + length;
655+
656+
while (buffer < end) {
657+
int ret = mqtt_read_publish_payload_blocking(client, buffer,
658+
end - buffer);
659+
660+
if (ret < 0) {
661+
return ret;
662+
} else if (ret == 0) {
663+
return -EIO;
664+
}
665+
666+
buffer += ret;
667+
}
668+
669+
return 0;
670+
}
671+

tests/net/lib/mqtt_pubsub/src/test_mqtt_pubsub.c

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ void publish_handler(struct mqtt_client *const client,
101101
const struct mqtt_evt *evt)
102102
{
103103
int rc;
104-
u8_t buf[16];
105-
u32_t offset = 0U;
104+
static u8_t buf[sizeof(payload_long)];
106105

107106
if (evt->result != 0) {
108107
TC_PRINT("MQTT PUBLISH error: %d\n", evt->result);
@@ -115,26 +114,19 @@ void publish_handler(struct mqtt_client *const client,
115114
goto error;
116115
}
117116

118-
while (payload_left > 0) {
119-
rc = mqtt_read_publish_payload_blocking(client, buf,
120-
sizeof(buf));
121-
if (rc <= 0) {
122-
TC_PRINT("Failed to receive payload, err: %d\n", -rc);
123-
if (rc == -EAGAIN) {
124-
continue;
125-
}
126-
goto error;
127-
}
128-
129-
if (memcmp(payload + offset, buf, rc) != 0) {
130-
TC_PRINT("Invalid payload content\n");
131-
goto error;
132-
}
117+
rc = mqtt_readall_publish_payload(client, buf, payload_left);
118+
if (rc != 0) {
119+
TC_PRINT("Error while reading publish payload\n");
120+
goto error;
121+
}
133122

134-
payload_left -= rc;
135-
offset += rc;
123+
if (memcmp(payload, buf, evt->param.publish.message.payload.len != 0)) {
124+
TC_PRINT("Invalid payload content\n");
125+
goto error;
136126
}
137127

128+
payload_left = 0;
129+
138130
return;
139131

140132
error:

0 commit comments

Comments
 (0)