diff --git a/samples/net/paho_mqtt_clients/publisher/src/mqtt_pack.c b/samples/net/paho_mqtt_clients/publisher/src/mqtt_pack.c index df302fad372..dc73977cc00 100644 --- a/samples/net/paho_mqtt_clients/publisher/src/mqtt_pack.c +++ b/samples/net/paho_mqtt_clients/publisher/src/mqtt_pack.c @@ -25,6 +25,10 @@ #include #include +/* From MQTTConnectClient.c */ +extern int MQTTSerialize_zero(unsigned char *buf, int buflen, + unsigned char packettype); + static void str_to_buf(struct app_buf_t *buf, char *str); int mqtt_msg_topic(struct mqtt_msg_t *msg, char *str) @@ -103,6 +107,24 @@ int mqtt_pack_subscribe(struct app_buf_t *buf, int dup, uint16_t pkt_id, return 0; } +int mqtt_pack_unsubscribe(struct app_buf_t *buf, int dup, uint16_t pkt_id, + char *topic) +{ + MQTTString topic_str = MQTTString_initializer; + int rc; + + topic_str.cstring = topic; + rc = MQTTSerialize_unsubscribe(buf->buf, buf->size, dup, pkt_id, 1, + &topic_str); + if (rc <= 0) { + return -EINVAL; + } + + buf->length = rc; + + return 0; +} + int mqtt_unpack_suback(struct app_buf_t *buf, uint16_t *pkt_id, int *granted_qos) { @@ -119,6 +141,19 @@ int mqtt_unpack_suback(struct app_buf_t *buf, uint16_t *pkt_id, return 0; } +int mqtt_unpack_unsuback(struct app_buf_t *buf, uint16_t *pkt_id) +{ + int rc; + + rc = MQTTDeserialize_unsuback(pkt_id, buf->buf, buf->length); + + if (rc != 1) { + return -EINVAL; + } + + return 0; +} + static int paho_mqtt_ctx(MQTTPacket_connectData *paho, struct mqtt_client_ctx_t *mqtt_ctx) { @@ -279,6 +314,20 @@ int mqtt_pack_pingreq(struct app_buf_t *buf) return 0; } +int mqtt_pack_pingresp(struct app_buf_t *buf) +{ + int rc; + + rc = MQTTSerialize_zero(buf->buf, buf->size, MQTT_PINGRESP); + if (rc <= 0) { + return -EINVAL; + } + + buf->length = rc; + + return 0; +} + static void str_to_buf(struct app_buf_t *buf, char *str) { buf->buf = str; diff --git a/samples/net/paho_mqtt_clients/publisher/src/mqtt_pack.h b/samples/net/paho_mqtt_clients/publisher/src/mqtt_pack.h index f2fe191fe4c..acd8139952e 100644 --- a/samples/net/paho_mqtt_clients/publisher/src/mqtt_pack.h +++ b/samples/net/paho_mqtt_clients/publisher/src/mqtt_pack.h @@ -176,6 +176,19 @@ int mqtt_pack_msg(struct app_buf_t *buf, enum mqtt_packet type, uint16_t pkt_id, int mqtt_pack_subscribe(struct app_buf_t *buf, int dup, uint16_t pkt_id, char *topic, enum mqtt_qos qos); +/** + * @brief mqtt_pack_unsubscribe + Packs a UNSUBSCRIBE message + * @param buf Buffer where the message is stored + * @param dup DUP flag + * @param pkt_id Packet Identifier + * @param topic Topic to unsubscribe from + * @return 0 on success + * -EINVAL on error + */ +int mqtt_pack_unsubscribe(struct app_buf_t *buf, int dup, uint16_t pkt_id, + char *topic); + /** * @brief mqtt_unpack_suback Unpacks a SUBACK message * @param buf Buffer where the message is stored @@ -187,6 +200,16 @@ int mqtt_pack_subscribe(struct app_buf_t *buf, int dup, uint16_t pkt_id, int mqtt_unpack_suback(struct app_buf_t *buf, uint16_t *pkt_id, int *granted_qos); + +/** + * @brief mqtt_unpack_unsuback Unpacks a UNSUBACK message + * @param buf Buffer where the message is stored + * @param pkt_id Packet Identifier + * @return 0 on success + * -EINVAL on error + */ +int mqtt_unpack_unsuback(struct app_buf_t *buf, uint16_t *pkt_id); + /** * @brief mqtt_pack_connect Packs a CONNECT message * @param buf Buffer where the message is stored @@ -264,4 +287,13 @@ int mqtt_pack_pubrel(struct app_buf_t *buf, int dup, uint16_t pkt_id); */ int mqtt_pack_pingreq(struct app_buf_t *buf); + +/** + * @brief mqtt_pack_pingresp Packs a PINGRESP message + * @param buf Buffer where the message is stored + * @return 0 on success + * -EINVAL on error + */ +int mqtt_pack_pingresp(struct app_buf_t *buf); + #endif