net/mqtt: Simplify the MQTT high-level API

1) Remove some variables pointing to user-provided data.
2) Pass the context structure instead of those variables.
3) Homogenize the use of "ctx" for all the callbacks receiving the
   struct mqtt_ctx * pointer.

Now users must use the CONTAINER_OF macro to access data required
by the MQTT callbacks.

Change-Id: I871c0bd8601a67b39187683215579f9ed0087cf9
Signed-off-by: Flavio Santes <flavio.santes@intel.com>
This commit is contained in:
Flavio Santes 2017-01-13 23:16:53 -06:00 committed by Jukka Rissanen
commit 4a17932d6a
2 changed files with 22 additions and 49 deletions

View file

@ -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.
*
* <b>NOTE: The application (and not the API) is in charge of keeping track of
* the state of the received and sent messages.</b>
@ -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.
*
* <b>Note: this callback must be not NULL</b>
*
* @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 {
*
* <b>Note: this callback must be not NULL</b>
*
* @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
*
* <b>Note: this callback must be not NULL</b>
*
* @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
*
* <b>Note: this callback must be not NULL</b>
* @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;

View file

@ -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;
}