mqtt: FIX: add MQTT unsubscribe/pingresp handling functions
The UNSUBSCRIBE and UNSUBACK functions are added. These functions allow to create the UNSUBSCRIBE message and to read the UNSUBACK message, repectively. Another function to create the PINGRESP message was also added. Jira: ZEP-623 Change-Id: Ic055e5762051cc9fb5d59a19c6eb974e34014d7e Signed-off-by: Flavio Santes <flavio.santes@intel.com>
This commit is contained in:
parent
6503654e75
commit
5280beffab
2 changed files with 81 additions and 0 deletions
|
@ -25,6 +25,10 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
/* 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);
|
static void str_to_buf(struct app_buf_t *buf, char *str);
|
||||||
|
|
||||||
int mqtt_msg_topic(struct mqtt_msg_t *msg, 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;
|
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 mqtt_unpack_suback(struct app_buf_t *buf, uint16_t *pkt_id,
|
||||||
int *granted_qos)
|
int *granted_qos)
|
||||||
{
|
{
|
||||||
|
@ -119,6 +141,19 @@ int mqtt_unpack_suback(struct app_buf_t *buf, uint16_t *pkt_id,
|
||||||
return 0;
|
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,
|
static int paho_mqtt_ctx(MQTTPacket_connectData *paho,
|
||||||
struct mqtt_client_ctx_t *mqtt_ctx)
|
struct mqtt_client_ctx_t *mqtt_ctx)
|
||||||
{
|
{
|
||||||
|
@ -279,6 +314,20 @@ int mqtt_pack_pingreq(struct app_buf_t *buf)
|
||||||
return 0;
|
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)
|
static void str_to_buf(struct app_buf_t *buf, char *str)
|
||||||
{
|
{
|
||||||
buf->buf = str;
|
buf->buf = str;
|
||||||
|
|
|
@ -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,
|
int mqtt_pack_subscribe(struct app_buf_t *buf, int dup, uint16_t pkt_id,
|
||||||
char *topic, enum mqtt_qos qos);
|
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
|
* @brief mqtt_unpack_suback Unpacks a SUBACK message
|
||||||
* @param buf Buffer where the message is stored
|
* @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 mqtt_unpack_suback(struct app_buf_t *buf, uint16_t *pkt_id,
|
||||||
int *granted_qos);
|
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
|
* @brief mqtt_pack_connect Packs a CONNECT message
|
||||||
* @param buf Buffer where the message is stored
|
* @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);
|
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
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue