Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mqtt.h Source File

mqtt.h

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * MQTT client
00004  */
00005 
00006 /*
00007  * Copyright (c) 2016 Erik Andersson
00008  * All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without modification,
00011  * are permitted provided that the following conditions are met:
00012  *
00013  * 1. Redistributions of source code must retain the above copyright notice,
00014  *    this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright notice,
00016  *    this list of conditions and the following disclaimer in the documentation
00017  *    and/or other materials provided with the distribution.
00018  * 3. The name of the author may not be used to endorse or promote products
00019  *    derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00023  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00024  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00026  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00029  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00030  * OF SUCH DAMAGE.
00031  *
00032  * This file is part of the lwIP TCP/IP stack.
00033  *
00034  * Author: Erik Andersson
00035  *
00036  */
00037 #ifndef LWIP_HDR_APPS_MQTT_CLIENT_H
00038 #define LWIP_HDR_APPS_MQTT_CLIENT_H
00039 
00040 #include "lwip/apps/mqtt_opts.h"
00041 #include "lwip/err.h"
00042 #include "lwip/ip_addr.h"
00043 #include "lwip/prot/iana.h"
00044 
00045 #ifdef __cplusplus
00046 extern "C" {
00047 #endif
00048 
00049 typedef struct mqtt_client_s mqtt_client_t;
00050 
00051 #if LWIP_ALTCP && LWIP_ALTCP_TLS
00052 struct altcp_tls_config;
00053 #endif
00054 
00055 /** @ingroup mqtt
00056  * Default MQTT port (non-TLS) */
00057 #define MQTT_PORT     LWIP_IANA_PORT_MQTT
00058 /** @ingroup mqtt
00059  * Default MQTT TLS port */
00060 #define MQTT_TLS_PORT LWIP_IANA_PORT_SECURE_MQTT
00061 
00062 /*---------------------------------------------------------------------------------------------- */
00063 /* Connection with server */
00064 
00065 /**
00066  * @ingroup mqtt
00067  * Client information and connection parameters */
00068 struct mqtt_connect_client_info_t {
00069   /** Client identifier, must be set by caller */
00070   const char *client_id;
00071   /** User name, set to NULL if not used */
00072   const char* client_user;
00073   /** Password, set to NULL if not used */
00074   const char* client_pass;
00075   /** keep alive time in seconds, 0 to disable keep alive functionality*/
00076   u16_t keep_alive;
00077   /** will topic, set to NULL if will is not to be used,
00078       will_msg, will_qos and will retain are then ignored */
00079   const char* will_topic;
00080   /** will_msg, see will_topic */
00081   const char* will_msg;
00082   /** will_qos, see will_topic */
00083   u8_t will_qos;
00084   /** will_retain, see will_topic */
00085   u8_t will_retain;
00086 #if LWIP_ALTCP && LWIP_ALTCP_TLS
00087   /** TLS configuration for secure connections */
00088   struct altcp_tls_config *tls_config;
00089 #endif
00090 };
00091 
00092 /**
00093  * @ingroup mqtt
00094  * Connection status codes */
00095 typedef enum
00096 {
00097   /** Accepted */
00098   MQTT_CONNECT_ACCEPTED                 = 0,
00099   /** Refused protocol version */
00100   MQTT_CONNECT_REFUSED_PROTOCOL_VERSION = 1,
00101   /** Refused identifier */
00102   MQTT_CONNECT_REFUSED_IDENTIFIER       = 2,
00103   /** Refused server */
00104   MQTT_CONNECT_REFUSED_SERVER           = 3,
00105   /** Refused user credentials */
00106   MQTT_CONNECT_REFUSED_USERNAME_PASS    = 4,
00107   /** Refused not authorized */
00108   MQTT_CONNECT_REFUSED_NOT_AUTHORIZED_  = 5,
00109   /** Disconnected */
00110   MQTT_CONNECT_DISCONNECTED             = 256,
00111   /** Timeout */
00112   MQTT_CONNECT_TIMEOUT                  = 257
00113 } mqtt_connection_status_t;
00114 
00115 /**
00116  * @ingroup mqtt
00117  * Function prototype for mqtt connection status callback. Called when
00118  * client has connected to the server after initiating a mqtt connection attempt by
00119  * calling mqtt_client_connect() or when connection is closed by server or an error
00120  *
00121  * @param client MQTT client itself
00122  * @param arg Additional argument to pass to the callback function
00123  * @param status Connect result code or disconnection notification @see mqtt_connection_status_t
00124  *
00125  */
00126 typedef void (*mqtt_connection_cb_t)(mqtt_client_t *client, void *arg, mqtt_connection_status_t status);
00127 
00128 
00129 /**
00130  * @ingroup mqtt
00131  * Data callback flags */
00132 enum {
00133   /** Flag set when last fragment of data arrives in data callback */
00134   MQTT_DATA_FLAG_LAST = 1
00135 };
00136 
00137 /** 
00138  * @ingroup mqtt
00139  * Function prototype for MQTT incoming publish data callback function. Called when data
00140  * arrives to a subscribed topic @see mqtt_subscribe
00141  *
00142  * @param arg Additional argument to pass to the callback function
00143  * @param data User data, pointed object, data may not be referenced after callback return,
00144           NULL is passed when all publish data are delivered
00145  * @param len Length of publish data fragment
00146  * @param flags MQTT_DATA_FLAG_LAST set when this call contains the last part of data from publish message
00147  *
00148  */
00149 typedef void (*mqtt_incoming_data_cb_t)(void *arg, const u8_t *data, u16_t len, u8_t flags);
00150 
00151 
00152 /** 
00153  * @ingroup mqtt
00154  * Function prototype for MQTT incoming publish function. Called when an incoming publish
00155  * arrives to a subscribed topic @see mqtt_subscribe
00156  *
00157  * @param arg Additional argument to pass to the callback function
00158  * @param topic Zero terminated Topic text string, topic may not be referenced after callback return
00159  * @param tot_len Total length of publish data, if set to 0 (no publish payload) data callback will not be invoked
00160  */
00161 typedef void (*mqtt_incoming_publish_cb_t)(void *arg, const char *topic, u32_t tot_len);
00162 
00163 
00164 /**
00165  * @ingroup mqtt
00166  * Function prototype for mqtt request callback. Called when a subscribe, unsubscribe
00167  * or publish request has completed
00168  * @param arg Pointer to user data supplied when invoking request
00169  * @param err ERR_OK on success
00170  *            ERR_TIMEOUT if no response was received within timeout,
00171  *            ERR_ABRT if (un)subscribe was denied
00172  */
00173 typedef void (*mqtt_request_cb_t)(void *arg, err_t err);
00174 
00175 
00176 err_t mqtt_client_connect(mqtt_client_t *client, const ip_addr_t *ipaddr, u16_t port, mqtt_connection_cb_t cb, void *arg,
00177                    const struct mqtt_connect_client_info_t *client_info);
00178 
00179 void mqtt_disconnect(mqtt_client_t *client);
00180 
00181 mqtt_client_t *mqtt_client_new(void);
00182 void mqtt_client_free(mqtt_client_t* client);
00183 
00184 u8_t mqtt_client_is_connected(mqtt_client_t *client);
00185 
00186 void mqtt_set_inpub_callback(mqtt_client_t *client, mqtt_incoming_publish_cb_t,
00187                              mqtt_incoming_data_cb_t data_cb, void *arg);
00188 
00189 err_t mqtt_sub_unsub(mqtt_client_t *client, const char *topic, u8_t qos, mqtt_request_cb_t cb, void *arg, u8_t sub);
00190 
00191 /** @ingroup mqtt
00192  *Subscribe to topic */
00193 #define mqtt_subscribe(client, topic, qos, cb, arg) mqtt_sub_unsub(client, topic, qos, cb, arg, 1)
00194 /** @ingroup mqtt
00195  *  Unsubscribe to topic */
00196 #define mqtt_unsubscribe(client, topic, cb, arg) mqtt_sub_unsub(client, topic, 0, cb, arg, 0)
00197 
00198 err_t mqtt_publish(mqtt_client_t *client, const char *topic, const void *payload, u16_t payload_length, u8_t qos, u8_t retain,
00199                                     mqtt_request_cb_t cb, void *arg);
00200 
00201 #ifdef __cplusplus
00202 }
00203 #endif
00204 
00205 #endif /* LWIP_HDR_APPS_MQTT_CLIENT_H */