diff --git a/include/net/mqtt.h b/include/net/mqtt.h
index 3163c2d5547..9ea6fbfeadd 100644
--- a/include/net/mqtt.h
+++ b/include/net/mqtt.h
@@ -43,9 +43,9 @@ enum mqtt_app {
* Internally, the #publish_rx callback must store the #mqtt_publish_msg message
* when a MQTT PUBLISH msg is received. When a MQTT PUBREL message is received,
* the application must evaluate if the PUBREL's Packet Identifier matches a
- * previous received MQTT PUBLISH message. In this case, the #publish_rx_data
- * may be used to refer to a collection of #mqtt_publish_msg structs (array of
- * structs).
+ * previous received MQTT PUBLISH message. In this case, the user may provide a
+ * collection of #mqtt_publish_msg structs (array of structs) to store those
+ * messages.
*
* NOTE: The application (and not the API) is in charge of keeping track of
* the state of the received and sent messages.
@@ -58,31 +58,20 @@ struct mqtt_ctx {
/** Callback executed when a MQTT CONNACK msg is received and validated.
* If this function pointer is not used, must be set to NULL.
- * The argument for the routine pointed to by this pointer
- * is represented by #connect_data.
*/
- void (*connect)(void *data);
-
- /** Data passed to the #connect callback. */
- void *connect_data;
+ void (*connect)(struct mqtt_ctx *ctx);
/** Callback executed when a MQTT DISCONNECT msg is sent.
* If this function pointer is not used, must be set to NULL.
- * The argument for the routine pointed to by this pointer
- * is represented by #disconnect_data.
*/
- void (*disconnect)(void *data);
-
- /** Data passed to the #disconnect callback. */
- void *disconnect_data;
+ void (*disconnect)(struct mqtt_ctx *ctx);
/** Callback executed when a #MQTT_APP_PUBLISHER application receives
* a MQTT PUBxxxx msg.
*
* Note: this callback must be not NULL
*
- * @param [in] data User provided data, represented by
- * #publish_tx_data
+ * @param [in] ctx MQTT context
* @param [in] pkt_id Packet Identifier for the input MQTT msg
* @param [in] type Packet type
* @return If this callback returns 0, the caller will
@@ -99,10 +88,8 @@ struct mqtt_ctx {
* @return Any other value will stop the QoS handshake
* and the caller will return -EINVAL
*/
- int (*publish_tx)(void *data, uint16_t pkt_id, enum mqtt_packet type);
-
- /** Data passed to the #publish_tx callback */
- void *publish_tx_data;
+ int (*publish_tx)(struct mqtt_ctx *ctx, uint16_t pkt_id,
+ enum mqtt_packet type);
/** Callback executed when a MQTT_APP_SUBSCRIBER,
* MQTT_APP_PUBLISHER_SUBSCRIBER or MQTT_APP_SERVER application receive
@@ -110,8 +97,7 @@ struct mqtt_ctx {
*
* Note: this callback must be not NULL
*
- * @param [in] data User provided data, represented by
- * #publish_rx_data
+ * @param [in] ctx MQTT context
* @param [in] msg Publish message, this parameter is only used
* when the type is MQTT_PUBLISH
* @param [in] pkt_id Packet Identifier for the input msg
@@ -129,19 +115,15 @@ struct mqtt_ctx {
* @return Any other value will stop the QoS handshake
* and the caller will return -EINVAL
*/
- int (*publish_rx)(void *data, struct mqtt_publish_msg *msg,
+ int (*publish_rx)(struct mqtt_ctx *ctx, struct mqtt_publish_msg *msg,
uint16_t pkt_id, enum mqtt_packet type);
- /** Data passed to the #publish_rx callback */
- void *publish_rx_data;
-
/** Callback executed when a MQTT_APP_SUBSCRIBER or
* MQTT_APP_PUBLISHER_SUBSCRIBER receives the MQTT SUBACK message
*
* Note: this callback must be not NULL
*
- * @param [in] data User provided data, represented by
- * #subscribe_data
+ * @param [in] ctx MQTT context
* @param [in] pkt_id Packet Identifier for the MQTT SUBACK msg
* @param [in] items Number of elements in the qos array
* @param [in] qos Array of QoS values
@@ -150,31 +132,24 @@ struct mqtt_ctx {
* @return Any other value will make the caller return
* -EINVAL
*/
- int (*subscribe)(void *data, uint16_t pkt_id,
+ int (*subscribe)(struct mqtt_ctx *ctx, uint16_t pkt_id,
uint8_t items, enum mqtt_qos qos[]);
- /** Data passed to the #subscribe callback */
- void *subscribe_data;
-
/** Callback executed when a MQTT_APP_SUBSCRIBER or
* MQTT_APP_PUBLISHER_SUBSCRIBER receives the MQTT UNSUBACK message
*
* Note: this callback must be not NULL
- * @param [in] data User provided data, represented by
- * #unsubscribe_data
+ * @param [in] ctx MQTT context
* @param [in] pkt_id Packet Identifier for the MQTT SUBACK msg
* @return If this callback returns 0, the caller will
* continue
* @return Any other value will make the caller return
* -EINVAL
*/
- int (*unsubscribe)(void *data, uint16_t pkt_id);
-
- /** Data passed to the #unsubscribe callback */
- void *unsubscribe_data;
+ int (*unsubscribe)(struct mqtt_ctx *ctx, uint16_t pkt_id);
/* Internal use only */
- int (*rcv)(struct mqtt_ctx *, struct net_buf *);
+ int (*rcv)(struct mqtt_ctx *ctx, struct net_buf *);
/** Application type, see: enum mqtt_app */
uint8_t app_type;
diff --git a/subsys/net/lib/mqtt/mqtt.c b/subsys/net/lib/mqtt/mqtt.c
index e025d599acb..9b947931dd6 100644
--- a/subsys/net/lib/mqtt/mqtt.c
+++ b/subsys/net/lib/mqtt/mqtt.c
@@ -103,7 +103,7 @@ int mqtt_tx_disconnect(struct mqtt_ctx *ctx)
tx = NULL;
if (ctx->disconnect) {
- ctx->disconnect(ctx->disconnect_data);
+ ctx->disconnect(ctx);
}
exit_disconnect:
@@ -414,7 +414,7 @@ int mqtt_rx_connack(struct mqtt_ctx *ctx, struct net_buf *rx, int clean_session)
ctx->connected = 1;
if (ctx->connect) {
- ctx->connect(ctx->connect_data);
+ ctx->connect(ctx);
}
exit_connect:
@@ -477,13 +477,12 @@ int mqtt_rx_pub_msgs(struct mqtt_ctx *ctx, struct net_buf *rx,
*/
if (type == MQTT_PUBREL) {
if (ctx->app_type != MQTT_APP_PUBLISHER) {
- rc = ctx->publish_rx(ctx->publish_rx_data, NULL, pkt_id,
- MQTT_PUBREL);
+ rc = ctx->publish_rx(ctx, NULL, pkt_id, MQTT_PUBREL);
} else {
rc = -EINVAL;
}
} else {
- rc = ctx->publish_tx(ctx->publish_tx_data, pkt_id, type);
+ rc = ctx->publish_tx(ctx, pkt_id, type);
}
if (rc != 0) {
@@ -560,7 +559,7 @@ int mqtt_rx_suback(struct mqtt_ctx *ctx, struct net_buf *rx)
return -EINVAL;
}
- rc = ctx->subscribe(ctx->subscribe_data, pkt_id, items, suback_qos);
+ rc = ctx->subscribe(ctx, pkt_id, items, suback_qos);
if (rc != 0) {
return -EINVAL;
}
@@ -588,7 +587,7 @@ int mqtt_rx_unsuback(struct mqtt_ctx *ctx, struct net_buf *rx)
return -EINVAL;
}
- rc = ctx->unsubscribe(ctx->subscribe_data, pkt_id);
+ rc = ctx->unsubscribe(ctx, pkt_id);
if (rc != 0) {
return -EINVAL;
}
@@ -606,8 +605,7 @@ int mqtt_rx_publish(struct mqtt_ctx *ctx, struct net_buf *rx)
return -EINVAL;
}
- rc = ctx->publish_rx(ctx->publish_rx_data, &msg, msg.pkt_id,
- MQTT_PUBLISH);
+ rc = ctx->publish_rx(ctx, &msg, msg.pkt_id, MQTT_PUBLISH);
if (rc != 0) {
return -EINVAL;
}