net/mqtt: Convert mqtt lib to use net app API

Use net app API since we want to enable MQTT with TLS.
mqtt_connect() and mqtt_close() are added to build and close the
connection to the broker. The caller doesn't need to deal with
the net context anymore and the most of network setup code in
mqtt_publisher is removed.

Signed-off-by: Aska Wu <aska.wu@linaro.org>
This commit is contained in:
Aska Wu 2017-06-30 17:22:19 +08:00 committed by Jukka Rissanen
commit fb79837862
9 changed files with 183 additions and 372 deletions

View file

@ -9,6 +9,7 @@
#include <net/mqtt_types.h>
#include <net/net_context.h>
#include <net/net_app.h>
#ifdef __cplusplus
extern "C" {
@ -61,11 +62,15 @@ enum mqtt_app {
* the state of the received and sent messages.</b>
*/
struct mqtt_ctx {
/** IP stack context structure */
struct net_context *net_ctx;
/** Network timeout for tx and rx routines */
/** Net app context structure */
struct net_app_ctx net_app_ctx;
s32_t net_init_timeout;
s32_t net_timeout;
/** Connectivity */
char *peer_addr_str;
u16_t peer_port;
/** Callback executed when a MQTT CONNACK msg is received and validated.
* If this function pointer is not used, must be set to NULL.
*/
@ -176,6 +181,23 @@ struct mqtt_ctx {
*/
int mqtt_init(struct mqtt_ctx *ctx, enum mqtt_app app_type);
/**
* Release the MQTT context structure
*
* @param ctx MQTT context structure
* @retval 0 on success, and <0 if error
*/
int mqtt_close(struct mqtt_ctx *ctx);
/**
* Connect to an MQTT broker
*
* @param ctx MQTT context structure
* @retval 0 on success, and <0 if error
*/
int mqtt_connect(struct mqtt_ctx *ctx);
/**
* Sends the MQTT CONNECT message
*