WE310F5  39.00.000
M2MB MQTT

This section describes the M2MB APIs to perform various MQTT client operations. More...

#define M2MB_MQTT_MAX_SUBSCRIBE_TOPICS   32
 
enum  M2MB_MQTT_SUBSCRIBE_REASON_E {
  M2MB_MQTT_SUBSCRIPTION_DENIED = 0,
  M2MB_MQTT_SUBSCRIPTION_GRANTED,
  M2MB_MQTT_SUBSCRIPTION_MSG
}
 MQTT client subscribe reason. More...
 
enum  M2MB_MQTT_CONNECT_STATUS {
  M2MB_MQTT_CONNECT_STATUS_UNKNOWN = -1,
  M2MB_MQTT_CONNECT_STATUS_ACCEPTED = 0,
  M2MB_MQTT_CONNECT_STATUS_REFUSED_PROTOCOL_VERSION = 1,
  M2MB_MQTT_CONNECT_STATUS_REFUSED_IDENTIFIER = 2,
  M2MB_MQTT_CONNECT_STATUS_REFUSED_SERVER = 3,
  M2MB_MQTT_CONNECT_STATUS_REFUSED_USERNAME_PASS = 4,
  M2MB_MQTT_CONNECT_STATUS_REFUSED_NOT_AUTHORIZED_ = 5,
  M2MB_MQTT_CONNECT_STATUS_DISCONNECTED = 256,
  M2MB_MQTT_CONNECT_STATUS_TIMEOUT = 257
}
 
typedef VOIDM2MB_MQTT_CTXT_HANDLE
 MQTT context handle.
This is obtained from m2mb_mqtt_open API and then used in subsequent MQTT APIs. More...
 
typedef enum M2MB_MQTT_SUBSCRIBE_REASON_E M2MB_MQTT_SUBSCRIBE_REASON_E
 MQTT client subscribe reason. More...
 
typedef enum M2MB_MQTT_CONNECT_STATUS M2MB_MQTT_CONNECT_STATUS_E
 
typedef VOID(* M2MB_MQTT_CONNECT_CB_T) (M2MB_MQTT_CTXT_HANDLE handle, INT32 status)
 User registered callback for MQTT connection status, registered during m2mb_mqtt_connect call. More...
 
typedef VOID(* M2MB_MQTT_MSG_HNDLR_CB_T) (M2MB_MQTT_CTXT_HANDLE handle, void *arg, INT32 reason, const UINT8 *topic, UINT16 topic_Length, const UINT8 *msg, UINT32 msg_Length, UINT32 QOS)
 User registered callback for incoming messages for subscribed topics. More...
 
typedef struct M2MB_MQTT_OPEN_PARAMS M2MB_MQTT_OPEN_PARAMS_T
 MQTT client Open parameters. More...
 
typedef struct M2MB_MQTT_CONN_PARAMS M2MB_MQTT_CONN_PARAMS_T
 MQTT connect parameters. More...
 
M2MB_STATUS_T m2mb_mqtt_init (VOID)
 initializes MQTT module. More...
 
M2MB_STATUS_T m2mb_mqtt_deinit (VOID)
 deinitializes MQTT module. More...
 
M2MB_STATUS_T m2mb_mqtt_open (M2MB_MQTT_CTXT_HANDLE *handle, M2MB_MQTT_OPEN_PARAMS_T *p_open_params)
 Creates MQTT instance for the MQTT client. More...
 
M2MB_STATUS_T m2mb_mqtt_close (M2MB_MQTT_CTXT_HANDLE mqtt_handle)
 closes MQTT instance. More...
 
M2MB_STATUS_T m2mb_mqtt_connect (M2MB_MQTT_CTXT_HANDLE mqtt_handle, M2MB_MQTT_CONN_PARAMS_T *p_conn_params)
 performs MQTT connection to the server. More...
 
M2MB_STATUS_T m2mb_mqtt_disconnect (M2MB_MQTT_CTXT_HANDLE mqtt_handle)
 Disconnect from the server(broker). More...
 
M2MB_STATUS_T m2mb_mqtt_publish (M2MB_MQTT_CTXT_HANDLE mqtt_handle, INT32 qos, INT32 retain, UINT16 message_id, const UINT8 *topic, const UINT8 *data, INT32 data_len)
 Publishes a message on a given topic. More...
 
M2MB_STATUS_T m2mb_mqtt_subscribe (M2MB_MQTT_CTXT_HANDLE mqtt_handle, UINT16 msg_id, const UINT8 *topic, INT32 qos, M2MB_MQTT_MSG_HNDLR_CB_T cbk, VOID *cbk_arg)
 m2mb_mqtt_subscribe is called to Subscribe. More...
 
M2MB_STATUS_T m2mb_mqtt_unsubscribe (M2MB_MQTT_CTXT_HANDLE mqtt_handle, UINT16 msg_id, const UINT8 *topic)
 unsubscribe to a perticular topic. More...
 

Detailed Description

This section describes the M2MB APIs to perform various MQTT client operations.

WE310F5_MQTT.png
MQTT

Macro Definition Documentation

◆ M2MB_MQTT_MAX_SUBSCRIBE_TOPICS

#define M2MB_MQTT_MAX_SUBSCRIBE_TOPICS   32

Maximum number of topics can be subsribed per connection

Definition at line 66 of file m2mb_mqtt.h.

Typedef Documentation

◆ M2MB_MQTT_CONN_PARAMS_T

MQTT connect parameters.

MQTT client connect parameters used in m2mb_mqtt_connect API.

◆ M2MB_MQTT_CONNECT_CB_T

typedef VOID(* M2MB_MQTT_CONNECT_CB_T) (M2MB_MQTT_CTXT_HANDLE handle, INT32 status)

User registered callback for MQTT connection status, registered during m2mb_mqtt_connect call.

This callback function is called to notify the user about the connection request status. This callback function is called to notify the user if the connection status changes(asynchronous disconnect) after connection done. m2mb_mqtt_connect API is non blocking API, so after m2mb_mqtt_connect call returns success, user has to wait till the connect callback gets called to do further operations on perticular MQTT client.

Parameters
[out]handleArgument provided to the user during m2mb_mqtt_open()
[out]statusConnection status, status values are explained in m2mb_net_status.h file

Example

void m2m_mqtt_connect_callback( M2MB_MQTT_CTXT_HANDLE handle, INT32 status)
{
//connection success, here onwards user can do subscribe and publish.
else
//connection failed(status values are described in m2mb_net_status.h file
return;
}

Definition at line 140 of file m2mb_mqtt.h.

◆ M2MB_MQTT_CONNECT_STATUS_E

Connection status codes

◆ M2MB_MQTT_CTXT_HANDLE

MQTT context handle.
This is obtained from m2mb_mqtt_open API and then used in subsequent MQTT APIs.

Definition at line 70 of file m2mb_mqtt.h.

◆ M2MB_MQTT_MSG_HNDLR_CB_T

typedef VOID(* M2MB_MQTT_MSG_HNDLR_CB_T) (M2MB_MQTT_CTXT_HANDLE handle, void *arg, INT32 reason, const UINT8 *topic, UINT16 topic_Length, const UINT8 *msg, UINT32 msg_Length, UINT32 QOS)

User registered callback for incoming messages for subscribed topics.

This callback function is called to notify user about the incoming messages.

Parameters
[out]handleArgument provided to the user during m2mb_mqtt_open()
[out]arguser provided subscribe call back arguments.
[out]reasonreason of incoming topic(M2MB_MQTT_SUBSCRIBE_REASON_E)
[out]topicincoming topic name.
[out]topic_Lengthincoming topic length.
[out]msgincoming message.
[out]msg_Lengthincoming message length.
[out]QoSGranted QOS for this subscription when 'reason' is M2MB_MQTT_SUBSCRIPTION_GRANTED or QOS for 'msg' delivered by the broker when 'reason' is M2MB_MQTT_SUBSCRIPTION_MSG.

Example

VOID m2m_s2w_mqtt_message_handler_cbk (void *handle, void* arg, INT32 reason, const char* topic,
UINT16 topic_Length, const char* msg, UINT32 msg_Length, UINT32 QOS )
{
//there is an incoming message
}

Definition at line 183 of file m2mb_mqtt.h.

◆ M2MB_MQTT_OPEN_PARAMS_T

MQTT client Open parameters.

MQTT client open parameters used in m2mb_mqtt_open API.

◆ M2MB_MQTT_SUBSCRIBE_REASON_E

MQTT client subscribe reason.

MQTT client subscribe reason for use in M2MB_MQTT_CONNECT_CB_T callback.

Enumeration Type Documentation

◆ M2MB_MQTT_CONNECT_STATUS

Connection status codes

Enumerator
M2MB_MQTT_CONNECT_STATUS_UNKNOWN 

Accepted

M2MB_MQTT_CONNECT_STATUS_ACCEPTED 

Accepted

M2MB_MQTT_CONNECT_STATUS_REFUSED_PROTOCOL_VERSION 

Refused protocol version

M2MB_MQTT_CONNECT_STATUS_REFUSED_IDENTIFIER 

Refused identifier

M2MB_MQTT_CONNECT_STATUS_REFUSED_SERVER 

Refused server

M2MB_MQTT_CONNECT_STATUS_REFUSED_USERNAME_PASS 

Refused user credentials

M2MB_MQTT_CONNECT_STATUS_REFUSED_NOT_AUTHORIZED_ 

Refused not authorized

M2MB_MQTT_CONNECT_STATUS_DISCONNECTED 

Disconnected

M2MB_MQTT_CONNECT_STATUS_TIMEOUT 

Timeout

Definition at line 88 of file m2mb_mqtt.h.

◆ M2MB_MQTT_SUBSCRIBE_REASON_E

MQTT client subscribe reason.

MQTT client subscribe reason for use in M2MB_MQTT_CONNECT_CB_T callback.

Enumerator
M2MB_MQTT_SUBSCRIPTION_DENIED 

Subscription is denied by the broker.

M2MB_MQTT_SUBSCRIPTION_GRANTED 

subscription is granted by the broker.

M2MB_MQTT_SUBSCRIPTION_MSG 

Application message is received from the broker

Definition at line 79 of file m2mb_mqtt.h.

Function Documentation

◆ m2mb_mqtt_close()

M2MB_STATUS_T m2mb_mqtt_close ( M2MB_MQTT_CTXT_HANDLE  mqtt_handle)

closes MQTT instance.

m2mb_mqtt_close frees all resources allocated by m2mb_mqtt_open.

Parameters
[in]mqtt_handlemqtt handle obtained from m2mb_mqtt_open API.
Returns
On success, M2MB_OK is returned. On error, non zero error code is returned.
Note

Example

typedef struct M2M_S2W_MQTT_S
{
UINT8 use_ssl;
M2M_S2W_MQTT_MESSAGE_T *msg_ptr_head;
M2M_S2W_MQTT_MESSAGE_T *msg_ptr_tail;
M2MB_SSL_CTXT_HANDLE pM2mbSslHandle;
}M2M_S2W_MQTT_T;
void m2m_mqtt_connect_callback( M2MB_MQTT_CTXT_HANDLE handle, INT32 status)
{
//connection success, here onwards user can do subscribe and publish.
else
//connection failed(status values are described in m2mb_net_status.h file
return;
}
void main(void)
{
M2MB_MQTT_OPEN_PARAMS_T open_params = {0};
M2MB_MQTT_CONN_PARAMS_T connect_params = {0};
INT8* HOST = "192.168.1.120";
INT32 PORT = 1883;
INT8* client_id = "mqttclient";
INT8* username = "username";
INT8* password = "password";
UINT32 keepalive_in_sec = 180;
UINT32 timeout_in_sec = 175;
M2M_S2W_MQTT_T *pS2wMqttHandle = NULL;
INT32 api_ret_code;
VOID* pCid = NULL;
char *cert_file = NULL;
pS2wMqttHandle = (M2M_S2W_MQTT_T *)MALLOC(sizeof(M2M_S2W_MQTT_T));
memset(pS2wMqttHandle, 0, sizeof(M2M_S2W_MQTT_T));
pS2wMqttHandle->use_ssl = 1;
{
return M2MB_ERROR;
}
open_params.client_id = client_id;
open_params.keepalive_in_sec = keepalive_in_sec;
open_params.username = username;
open_params.password = password;
open_params.timeout_in_sec = timeout_in_sec;
if(pS2wMqttHandle->use_ssl == TRUE)
{
M2MB_SSL_OPEN_PARAMS_T params = {0};
M2MB_STATUS_T ret_status = m2mb_ssl_open(&pS2wMqttHandle->pM2mbSslHandle, params );
if( (ret_status != M2MB_OK) || (pS2wMqttHandle->pM2mbSslHandle == NULL))
{
FREE(pS2wMqttHandle);
return M2MB_ERROR;
}
open_params.pSslCfg = pS2wMqttHandle->pM2mbSslHandle;
}
if (M2MB_OK != m2mb_mqtt_open(&s2w_mqtt_handle->handle, &open_params)
{
return M2MB_ERROR;
}
connect_params.host = HOST;
connect_params.port = PORT;
connect_params.connectcb = m2m_mqtt_connect_callback;
connect_params.connectcb_args = pCid;
api_ret_code = m2mb_mqtt_connect(s2w_mqtt_handle->handle, &connect_params);
if(api_ret_code != M2MB_OK)
{
return M2MB_ERROR;
}
if((pS2wMqttHandle->handle != NULL) && ( M2MB_OK != m2mb_mqtt_disconnect(pS2wMqttHandle->handle) ))
{
return M2MB_ERROR;
}
if(pS2wMqttHandle->use_ssl == 1)
{
if(pS2wMqttHandle->pM2mbSslHandle)
m2mb_ssl_close(pS2wMqttHandle->pM2mbSslHandle);
}
if((pS2wMqttHandle->handle != NULL) && ( M2MB_OK != m2mb_mqtt_close(pS2wMqttHandle->handle) ))
{
return M2MB_ERROR;
}
}

◆ m2mb_mqtt_connect()

M2MB_STATUS_T m2mb_mqtt_connect ( M2MB_MQTT_CTXT_HANDLE  mqtt_handle,
M2MB_MQTT_CONN_PARAMS_T p_conn_params 
)

performs MQTT connection to the server.

m2mb_mqtt_connect API connects to the server.

Parameters
[in]mqtt_handlemqtt handle obtained from m2mb_mqtt_open API.
[in]p_conn_paramspointer to connection parameters of type M2MB_MQTT_CONN_PARAMS_T.
Returns
On success, M2MB_OK is returned. On error, non zero error code is returned.
Note

Example

typedef struct M2M_S2W_MQTT_S
{
UINT8 use_ssl;
M2M_S2W_MQTT_MESSAGE_T *msg_ptr_head;
M2M_S2W_MQTT_MESSAGE_T *msg_ptr_tail;
M2MB_SSL_CTXT_HANDLE pM2mbSslHandle;
}M2M_S2W_MQTT_T;
void m2m_mqtt_connect_callback( M2MB_MQTT_CTXT_HANDLE handle, INT32 status)
{
//connection success, here onwards user can do subscribe and publish.
else
//connection failed(status values are described in m2mb_net_status.h file
return;
}
void main(void)
{
M2MB_MQTT_OPEN_PARAMS_T open_params = {0};
M2MB_MQTT_CONN_PARAMS_T connect_params = {0};
INT8* HOST = "192.168.1.120";
INT32 PORT = 1883;
INT8* client_id = "mqttclient";
INT8* username = "username";
INT8* password = "password";
UINT32 keepalive_in_sec = 180;
UINT32 timeout_in_sec = 175;
M2M_S2W_MQTT_T *pS2wMqttHandle = NULL;
INT32 api_ret_code;
VOID* pCid = NULL;
char *cert_file = NULL;
pS2wMqttHandle = (M2M_S2W_MQTT_T *)MALLOC(sizeof(M2M_S2W_MQTT_T));
memset(pS2wMqttHandle, 0, sizeof(M2M_S2W_MQTT_T));
pS2wMqttHandle->use_ssl = 1;
{
return M2MB_ERROR;
}
open_params.client_id = client_id;
open_params.keepalive_in_sec = keepalive_in_sec;
open_params.username = username;
open_params.password = password;
open_params.timeout_in_sec = timeout_in_sec;
if(pS2wMqttHandle->use_ssl == TRUE)
{
M2MB_SSL_OPEN_PARAMS_T params = {0};
M2MB_STATUS_T ret_status = m2mb_ssl_open(&pS2wMqttHandle->pM2mbSslHandle, params );
if( (ret_status != M2MB_OK) || (pS2wMqttHandle->pM2mbSslHandle == NULL))
{
FREE(pS2wMqttHandle);
return M2MB_ERROR;
}
open_params.pSslCfg = pS2wMqttHandle->pM2mbSslHandle;
}
if (M2MB_OK != m2mb_mqtt_open(&s2w_mqtt_handle->handle, &open_params)
{
return M2MB_ERROR;
}
connect_params.host = HOST;
connect_params.port = PORT;
connect_params.connectcb = m2m_mqtt_connect_callback;
connect_params.connectcb_args = pCid;
api_ret_code = m2mb_mqtt_connect(s2w_mqtt_handle->handle, &connect_params);
if(api_ret_code != M2MB_OK)
{
return M2MB_ERROR;
}
}

◆ m2mb_mqtt_deinit()

M2MB_STATUS_T m2mb_mqtt_deinit ( VOID  )

deinitializes MQTT module.

deintializes mqtt module and frees all resources allocated by m2mb_mqtt_init API.

Returns
returns M2MB_OK on success, M2MB_ERROR on error.
Note
this is the last API to call after finishing all MQTT operations. Example
typedef struct M2M_S2W_MQTT_S
{
UINT8 use_ssl;
M2M_S2W_MQTT_MESSAGE_T *msg_ptr_head;
M2M_S2W_MQTT_MESSAGE_T *msg_ptr_tail;
M2MB_SSL_CTXT_HANDLE pM2mbSslHandle;
}M2M_S2W_MQTT_T;
void m2m_mqtt_connect_callback( M2MB_MQTT_CTXT_HANDLE handle, INT32 status)
{
//connection success, here onwards user can do subscribe and publish.
else
//connection failed(status values are described in m2mb_net_status.h file
return;
}
void main(void)
{
M2MB_MQTT_OPEN_PARAMS_T open_params = {0};
M2MB_MQTT_CONN_PARAMS_T connect_params = {0};
INT8* HOST = "192.168.1.120";
INT32 PORT = 1883;
INT8* client_id = "mqttclient";
INT8* username = "username";
INT8* password = "password";
UINT32 keepalive_in_sec = 180;
UINT32 timeout_in_sec = 175;
M2M_S2W_MQTT_T *pS2wMqttHandle = NULL;
INT32 api_ret_code;
VOID* pCid = NULL;
char *cert_file = NULL;
pS2wMqttHandle = (M2M_S2W_MQTT_T *)MALLOC(sizeof(M2M_S2W_MQTT_T));
memset(pS2wMqttHandle, 0, sizeof(M2M_S2W_MQTT_T));
pS2wMqttHandle->use_ssl = 1;
{
return M2MB_ERROR;
}
open_params.client_id = client_id;
open_params.keepalive_in_sec = keepalive_in_sec;
open_params.username = username;
open_params.password = password;
open_params.timeout_in_sec = timeout_in_sec;
if(pS2wMqttHandle->use_ssl == TRUE)
{
M2MB_SSL_OPEN_PARAMS_T params = {0};
M2MB_STATUS_T ret_status = m2mb_ssl_open(&pS2wMqttHandle->pM2mbSslHandle, params );
if( (ret_status != M2MB_OK) || (pS2wMqttHandle->pM2mbSslHandle == NULL))
{
FREE(pS2wMqttHandle);
return M2MB_ERROR;
}
open_params.pSslCfg = pS2wMqttHandle->pM2mbSslHandle;
}
if (M2MB_OK != m2mb_mqtt_open(&s2w_mqtt_handle->handle, &open_params)
{
return M2MB_ERROR;
}
connect_params.host = HOST;
connect_params.port = PORT;
connect_params.connectcb = m2m_mqtt_connect_callback;
connect_params.connectcb_args = pCid;
api_ret_code = m2mb_mqtt_connect(s2w_mqtt_handle->handle, &connect_params);
if(api_ret_code != M2MB_OK)
{
return M2MB_ERROR;
}
if((pS2wMqttHandle->handle != NULL) && ( M2MB_OK != m2mb_mqtt_disconnect(pS2wMqttHandle->handle) ))
{
return M2MB_ERROR;
}
if(pS2wMqttHandle->use_ssl == 1)
{
if(pS2wMqttHandle->pM2mbSslHandle)
m2mb_ssl_close(pS2wMqttHandle->pM2mbSslHandle);
}
if((pS2wMqttHandle->handle != NULL) && ( M2MB_OK != m2mb_mqtt_close(pS2wMqttHandle->handle) ))
{
return M2MB_ERROR;
}
{
return M2MB_ERROR;
}
}

◆ m2mb_mqtt_disconnect()

M2MB_STATUS_T m2mb_mqtt_disconnect ( M2MB_MQTT_CTXT_HANDLE  mqtt_handle)

Disconnect from the server(broker).

m2mb_mqtt_disconnect API sends disconnect packet to the broker.

Parameters
[in]mqtt_handlemqtt handle obtained from m2mb_mqtt_open API.
Returns
On success, M2MB_OK is returned. On error, non zero error code is returned.
Note

Example

typedef struct M2M_S2W_MQTT_S
{
UINT8 use_ssl;
M2M_S2W_MQTT_MESSAGE_T *msg_ptr_head;
M2M_S2W_MQTT_MESSAGE_T *msg_ptr_tail;
M2MB_SSL_CTXT_HANDLE pM2mbSslHandle;
}M2M_S2W_MQTT_T;
void m2m_mqtt_connect_callback( M2MB_MQTT_CTXT_HANDLE handle, INT32 status)
{
//connection success, here onwards user can do subscribe and publish.
else
//connection failed(status values are described in m2mb_net_status.h file
return;
}
void main(void)
{
M2MB_MQTT_OPEN_PARAMS_T open_params = {0};
M2MB_MQTT_CONN_PARAMS_T connect_params = {0};
INT8* HOST = "192.168.1.120";
INT32 PORT = 1883;
INT8* client_id = "mqttclient";
INT8* username = "username";
INT8* password = "password";
UINT32 keepalive_in_sec = 180;
UINT32 timeout_in_sec = 175;
M2M_S2W_MQTT_T *pS2wMqttHandle = NULL;
INT32 api_ret_code;
VOID* pCid = NULL;
char *cert_file = NULL;
pS2wMqttHandle = (M2M_S2W_MQTT_T *)MALLOC(sizeof(M2M_S2W_MQTT_T));
memset(pS2wMqttHandle, 0, sizeof(M2M_S2W_MQTT_T));
pS2wMqttHandle->use_ssl = 1;
{
return M2MB_ERROR;
}
open_params.client_id = client_id;
open_params.keepalive_in_sec = keepalive_in_sec;
open_params.username = username;
open_params.password = password;
open_params.timeout_in_sec = timeout_in_sec;
if(pS2wMqttHandle->use_ssl == TRUE)
{
M2MB_SSL_OPEN_PARAMS_T params = {0};
M2MB_STATUS_T ret_status = m2mb_ssl_open(&pS2wMqttHandle->pM2mbSslHandle, params );
if( (ret_status != M2MB_OK) || (pS2wMqttHandle->pM2mbSslHandle == NULL))
{
FREE(pS2wMqttHandle);
return M2MB_ERROR;
}
open_params.pSslCfg = pS2wMqttHandle->pM2mbSslHandle;
}
if (M2MB_OK != m2mb_mqtt_open(&s2w_mqtt_handle->handle, &open_params)
{
return M2MB_ERROR;
}
connect_params.host = HOST;
connect_params.port = PORT;
connect_params.connectcb = m2m_mqtt_connect_callback;
connect_params.connectcb_args = pCid;
api_ret_code = m2mb_mqtt_connect(s2w_mqtt_handle->handle, &connect_params);
if(api_ret_code != M2MB_OK)
{
return M2MB_ERROR;
}
if((pS2wMqttHandle->handle != NULL) && ( M2MB_OK != m2mb_mqtt_disconnect(pS2wMqttHandle->handle) ))
{
return M2MB_ERROR;
}
if(pS2wMqttHandle->use_ssl == 1)
{
if(pS2wMqttHandle->pM2mbSslHandle)
m2mb_ssl_close(pS2wMqttHandle->pM2mbSslHandle);
}
if((pS2wMqttHandle->handle != NULL) && ( M2MB_OK != m2mb_mqtt_close(pS2wMqttHandle->handle) ))
{
return M2MB_ERROR;
}
}

◆ m2mb_mqtt_init()

M2MB_STATUS_T m2mb_mqtt_init ( VOID  )

initializes MQTT module.

m2mb_mqtt_init intializes mqtt module resources required for MQTT.

Returns
returns M2MB_OK on success, M2MB_ERROR on error.
Note
this is the first API to call before calling any other MQTT APIs, this is one time call.

Example

typedef struct M2M_S2W_MQTT_S
{
UINT8 use_ssl;
M2M_S2W_MQTT_MESSAGE_T *msg_ptr_head;
M2M_S2W_MQTT_MESSAGE_T *msg_ptr_tail;
M2MB_SSL_CTXT_HANDLE pM2mbSslHandle;
}M2M_S2W_MQTT_T;
void m2m_mqtt_connect_callback( M2MB_MQTT_CTXT_HANDLE handle, INT32 status)
{
//connection success, here onwards user can do subscribe and publish.
else
//connection failed(status values are described in m2mb_net_status.h file
return;
}
void main(void)
{
M2MB_MQTT_OPEN_PARAMS_T open_params = {0};
M2MB_MQTT_CONN_PARAMS_T connect_params = {0};
INT8* HOST = "192.168.1.120";
INT32 PORT = 1883;
INT8* client_id = "mqttclient";
INT8* username = "username";
INT8* password = "password";
UINT32 keepalive_in_sec = 180;
UINT32 timeout_in_sec = 175;
M2M_S2W_MQTT_T *pS2wMqttHandle = NULL;
INT32 api_ret_code;
VOID* pCid = NULL;
char *cert_file = NULL;
pS2wMqttHandle = (M2M_S2W_MQTT_T *)MALLOC(sizeof(M2M_S2W_MQTT_T));
memset(pS2wMqttHandle, 0, sizeof(M2M_S2W_MQTT_T));
pS2wMqttHandle->use_ssl = 1;
{
return M2MB_ERROR;
}
open_params.client_id = client_id;
open_params.keepalive_in_sec = keepalive_in_sec;
open_params.username = username;
open_params.password = password;
open_params.timeout_in_sec = timeout_in_sec;
if(pS2wMqttHandle->use_ssl == TRUE)
{
M2MB_SSL_OPEN_PARAMS_T params = {0};
M2MB_STATUS_T ret_status = m2mb_ssl_open(&pS2wMqttHandle->pM2mbSslHandle, params );
if( (ret_status != M2MB_OK) || (pS2wMqttHandle->pM2mbSslHandle == NULL))
{
FREE(pS2wMqttHandle);
return M2MB_ERROR;
}
open_params.pSslCfg = pS2wMqttHandle->pM2mbSslHandle;
}
if (M2MB_OK != m2mb_mqtt_open(&s2w_mqtt_handle->handle, &open_params)
{
return M2MB_ERROR;
}
connect_params.host = HOST;
connect_params.port = PORT;
connect_params.connectcb = m2m_mqtt_connect_callback;
connect_params.connectcb_args = pCid;
api_ret_code = m2mb_mqtt_connect(s2w_mqtt_handle->handle, &connect_params);
if(api_ret_code != M2MB_OK)
{
return M2MB_ERROR;
}
if((pS2wMqttHandle->handle != NULL) && ( M2MB_OK != m2mb_mqtt_disconnect(pS2wMqttHandle->handle) ))
{
return M2MB_ERROR;
}
if(pS2wMqttHandle->use_ssl == 1)
{
if(pS2wMqttHandle->pM2mbSslHandle)
m2mb_ssl_close(pS2wMqttHandle->pM2mbSslHandle);
}
if((pS2wMqttHandle->handle != NULL) && ( M2MB_OK != m2mb_mqtt_close(pS2wMqttHandle->handle) ))
{
return M2MB_ERROR;
}
{
return M2MB_ERROR;
}
}

◆ m2mb_mqtt_open()

M2MB_STATUS_T m2mb_mqtt_open ( M2MB_MQTT_CTXT_HANDLE handle,
M2MB_MQTT_OPEN_PARAMS_T p_open_params 
)

Creates MQTT instance for the MQTT client.

m2mb_mqtt_open creates a new MQTT client instance.

Parameters
[out]mqtt_handlemqtt handle for a new MQTT client instance.
[in]p_open_paramspointer to open parameters of type M2MB_MQTT_OPEN_PARAMS_T.
Returns
On success, M2MB_OK is returned. On error, non zero error code is returned.
Note

Example

typedef struct M2M_S2W_MQTT_S
{
UINT8 use_ssl;
M2M_S2W_MQTT_MESSAGE_T *msg_ptr_head;
M2M_S2W_MQTT_MESSAGE_T *msg_ptr_tail;
M2MB_SSL_CTXT_HANDLE pM2mbSslHandle;
}M2M_S2W_MQTT_T;
void m2m_mqtt_connect_callback( M2MB_MQTT_CTXT_HANDLE handle, INT32 status)
{
//connection success, here onwards user can do subscribe and publish.
else
//connection failed(status values are described in m2mb_net_status.h file
return;
}
void main(void)
{
M2MB_MQTT_OPEN_PARAMS_T open_params = {0};
M2MB_MQTT_CONN_PARAMS_T connect_params = {0};
INT8* HOST = "192.168.1.120";
INT32 PORT = 1883;
INT8* client_id = "mqttclient";
INT8* username = "username";
INT8* password = "password";
UINT32 keepalive_in_sec = 180;
UINT32 timeout_in_sec = 175;
M2M_S2W_MQTT_T *pS2wMqttHandle = NULL;
INT32 api_ret_code;
VOID* pCid = NULL;
char *cert_file = NULL;
pS2wMqttHandle = (M2M_S2W_MQTT_T *)MALLOC(sizeof(M2M_S2W_MQTT_T));
memset(pS2wMqttHandle, 0, sizeof(M2M_S2W_MQTT_T));
pS2wMqttHandle->use_ssl = 1;
{
return M2MB_ERROR;
}
open_params.client_id = client_id;
open_params.keepalive_in_sec = keepalive_in_sec;
open_params.username = username;
open_params.password = password;
open_params.timeout_in_sec = timeout_in_sec;
if(pS2wMqttHandle->use_ssl == TRUE)
{
M2MB_SSL_OPEN_PARAMS_T params = {0};
M2MB_STATUS_T ret_status = m2mb_ssl_open(&pS2wMqttHandle->pM2mbSslHandle, params );
if( (ret_status != M2MB_OK) || (pS2wMqttHandle->pM2mbSslHandle == NULL))
{
FREE(pS2wMqttHandle);
return M2MB_ERROR;
}
open_params.pSslCfg = pS2wMqttHandle->pM2mbSslHandle;
}
if (M2MB_OK != m2mb_mqtt_open(&s2w_mqtt_handle->handle, &open_params)
{
return M2MB_ERROR;
}
connect_params.host = HOST;
connect_params.port = PORT;
connect_params.connectcb = m2m_mqtt_connect_callback;
connect_params.connectcb_args = pCid;
api_ret_code = m2mb_mqtt_connect(s2w_mqtt_handle->handle, &connect_params);
if(api_ret_code != M2MB_OK)
{
return M2MB_ERROR;
}
}

◆ m2mb_mqtt_publish()

M2MB_STATUS_T m2mb_mqtt_publish ( M2MB_MQTT_CTXT_HANDLE  mqtt_handle,
INT32  qos,
INT32  retain,
UINT16  message_id,
const UINT8 topic,
const UINT8 data,
INT32  data_len 
)

Publishes a message on a given topic.

m2mb_mqtt_publish Publishes a message on a given topic.

Parameters
[out]mqtt_handlemqtt handle obtained from m2mb_mqtt_open API.
[in]qosInteger value 0, 1, or 2 indicating the QOS to be used for msg.
[in]retainSet to TRUE to make the message retained.
[in]message_idMessage ID
[in]topicTopic name
[in]dataData to publish
[in]data_lenData length.
Returns
On success, M2MB_OK is returned. On error, non zero error code is returned.
Note

Example

typedef struct M2M_S2W_MQTT_S
{
UINT8 use_ssl;
M2M_S2W_MQTT_MESSAGE_T *msg_ptr_head;
M2M_S2W_MQTT_MESSAGE_T *msg_ptr_tail;
M2MB_SSL_CTXT_HANDLE pM2mbSslHandle;
}M2M_S2W_MQTT_T;
void m2m_mqtt_connect_callback( M2MB_MQTT_CTXT_HANDLE handle, INT32 status)
{
//connection success, here onwards user can do subscribe and publish.
else
//connection failed(status values are described in m2mb_net_status.h file
return;
}
void main(void)
{
M2MB_MQTT_OPEN_PARAMS_T open_params = {0};
M2MB_MQTT_CONN_PARAMS_T connect_params = {0};
INT8* HOST = "192.168.1.120";
INT32 PORT = 1883;
INT8* client_id = "mqttclient";
INT8* username = "username";
INT8* password = "password";
UINT32 keepalive_in_sec = 180;
UINT32 timeout_in_sec = 175;
M2M_S2W_MQTT_T *pS2wMqttHandle = NULL;
INT32 api_ret_code;
VOID* pCid = NULL;
char *cert_file = NULL;
pS2wMqttHandle = (M2M_S2W_MQTT_T *)MALLOC(sizeof(M2M_S2W_MQTT_T));
memset(pS2wMqttHandle, 0, sizeof(M2M_S2W_MQTT_T));
pS2wMqttHandle->use_ssl = 1;
{
return M2MB_ERROR;
}
open_params.client_id = client_id;
open_params.keepalive_in_sec = keepalive_in_sec;
open_params.username = username;
open_params.password = password;
open_params.timeout_in_sec = timeout_in_sec;
if(pS2wMqttHandle->use_ssl == TRUE)
{
M2MB_SSL_OPEN_PARAMS_T params = {0};
M2MB_STATUS_T ret_status = m2mb_ssl_open(&pS2wMqttHandle->pM2mbSslHandle, params );
if( (ret_status != M2MB_OK) || (pS2wMqttHandle->pM2mbSslHandle == NULL))
{
FREE(pS2wMqttHandle);
return M2MB_ERROR;
}
open_params.pSslCfg = pS2wMqttHandle->pM2mbSslHandle;
}
if (M2MB_OK != m2mb_mqtt_open(&s2w_mqtt_handle->handle, &open_params)
{
return M2MB_ERROR;
}
connect_params.host = HOST;
connect_params.port = PORT;
connect_params.connectcb = m2m_mqtt_connect_callback;
connect_params.connectcb_args = pCid;
api_ret_code = m2mb_mqtt_connect(s2w_mqtt_handle->handle, &connect_params);
if(api_ret_code != M2MB_OK)
{
return M2MB_ERROR;
}
if((pS2wMqttHandle->handle != NULL) && ( M2MB_OK != m2mb_mqtt_disconnect(pS2wMqttHandle->handle) ))
{
return M2MB_ERROR;
}
if(pS2wMqttHandle->use_ssl == 1)
{
if(pS2wMqttHandle->pM2mbSslHandle)
m2mb_ssl_close(pS2wMqttHandle->pM2mbSslHandle);
}
if((pS2wMqttHandle->handle != NULL) && ( M2MB_OK != m2mb_mqtt_close(pS2wMqttHandle->handle) ))
{
return M2MB_ERROR;
}
publish_st = ( UINT32 )m2mb_mqtt_publish(pS2wMqttHandle->handle, QOS, retainFlag, message_id, topic,
buf, data_tlen);
if( M2MB_OK == publish_st)
return M2MB_OK;
else
return M2MB_ERROR;
}

◆ m2mb_mqtt_subscribe()

M2MB_STATUS_T m2mb_mqtt_subscribe ( M2MB_MQTT_CTXT_HANDLE  mqtt_handle,
UINT16  msg_id,
const UINT8 topic,
INT32  qos,
M2MB_MQTT_MSG_HNDLR_CB_T  cbk,
VOID cbk_arg 
)

m2mb_mqtt_subscribe is called to Subscribe.

m2mb_mqtt_subscribe Subscribes to one topic.

Parameters
[out]mqtt_handlemqtt handle obtained from m2mb_mqtt_open API.
[in]msg_idmessage id
[in]topictopic name
[in]qosInteger value 0, 1, or 2 indicating the QOS to be used for msg.
[in]cbkUser callback for incoming messages
[in]cbk_argUser callback arguments for incoming messages.
Returns
On success, M2MB_OK is returned. On error, non zero error code is returned.
Note

Example

VOID m2m_s2w_mqtt_message_handler_cbk (INT32 handle, void* arg, INT32 reason, const char* topic,
UINT16 topic_Length, const char* msg, UINT32 msg_Length, UINT32 QOS )
{
UINT32 data_len;
M2M_S2W_MQTT_T * s2w_mqtt_handle_ptr = (M2M_S2W_MQTT_T *)arg;
if(0 == s2w_mqtt_handle_ptr->receive_request_present)
{
return;
}
m2m_s2w_mqtt_message_set(s2w_mqtt_handle_ptr,
reason, topic,
topic_Length,
msg,
msg_Length,
QOS );
data_len = MSG_TO_HOST_LEN(topic_Length, msg_Length);
//indicate to the user.
}
typedef struct M2M_S2W_MQTT_S
{
UINT8 use_ssl;
M2M_S2W_MQTT_MESSAGE_T *msg_ptr_head;
M2M_S2W_MQTT_MESSAGE_T *msg_ptr_tail;
M2MB_SSL_CTXT_HANDLE pM2mbSslHandle;
}M2M_S2W_MQTT_T;
void m2m_mqtt_connect_callback( M2MB_MQTT_CTXT_HANDLE handle, INT32 status)
{
//connection success, here onwards user can do subscribe and publish.
else
//connection failed(status values are described in m2mb_net_status.h file
return;
}
void main(void)
{
M2MB_MQTT_OPEN_PARAMS_T open_params = {0};
M2MB_MQTT_CONN_PARAMS_T connect_params = {0};
INT8* HOST = "192.168.1.120";
INT32 PORT = 1883;
INT8* client_id = "mqttclient";
INT8* username = "username";
INT8* password = "password";
UINT32 keepalive_in_sec = 180;
UINT32 timeout_in_sec = 175;
M2M_S2W_MQTT_T *pS2wMqttHandle = NULL;
INT32 api_ret_code;
VOID* pCid = NULL;
char *cert_file = NULL;
pS2wMqttHandle = (M2M_S2W_MQTT_T *)MALLOC(sizeof(M2M_S2W_MQTT_T));
memset(pS2wMqttHandle, 0, sizeof(M2M_S2W_MQTT_T));
pS2wMqttHandle->use_ssl = 1;
{
return M2MB_ERROR;
}
open_params.client_id = client_id;
open_params.keepalive_in_sec = keepalive_in_sec;
open_params.username = username;
open_params.password = password;
open_params.timeout_in_sec = timeout_in_sec;
if(pS2wMqttHandle->use_ssl == TRUE)
{
M2MB_SSL_OPEN_PARAMS_T params = {0};
M2MB_STATUS_T ret_status = m2mb_ssl_open(&pS2wMqttHandle->pM2mbSslHandle, params );
if( (ret_status != M2MB_OK) || (pS2wMqttHandle->pM2mbSslHandle == NULL))
{
FREE(pS2wMqttHandle);
return M2MB_ERROR;
}
open_params.pSslCfg = pS2wMqttHandle->pM2mbSslHandle;
}
if (M2MB_OK != m2mb_mqtt_open(&s2w_mqtt_handle->handle, &open_params)
{
return M2MB_ERROR;
}
connect_params.host = HOST;
connect_params.port = PORT;
connect_params.connectcb = m2m_mqtt_connect_callback;
connect_params.connectcb_args = pCid;
api_ret_code = m2mb_mqtt_connect(s2w_mqtt_handle->handle, &connect_params);
if(api_ret_code != M2MB_OK)
{
return M2MB_ERROR;
}
ret_val = m2mb_mqtt_subscribe(s2w_mqtt_handle->handle, message_id, topic, QOS, m2m_s2w_mqtt_message_handler_cbk, s2w_mqtt_handle);
if(ret_val < 0)
{
return M2MB_ERROR;
}
m2mb_os_taskSleep(100);
}

◆ m2mb_mqtt_unsubscribe()

M2MB_STATUS_T m2mb_mqtt_unsubscribe ( M2MB_MQTT_CTXT_HANDLE  mqtt_handle,
UINT16  msg_id,
const UINT8 topic 
)

unsubscribe to a perticular topic.

unsubscribe to a given topic.

Parameters
[out]mqtt_handlemqtt handle obtained from m2mb_mqtt_open API.
[in]msg_idmessage id
[in]topictopic name
Returns
On success, M2MB_OK is returned. On error, non zero error code is returned.
Note

Example

VOID m2m_s2w_mqtt_message_handler_cbk (INT32 handle, void* arg, INT32 reason, const char* topic,
UINT16 topic_Length, const char* msg, UINT32 msg_Length, UINT32 QOS )
{
UINT32 data_len;
M2M_S2W_MQTT_T * s2w_mqtt_handle_ptr = (M2M_S2W_MQTT_T *)arg;
if(0 == s2w_mqtt_handle_ptr->receive_request_present)
{
return;
}
m2m_s2w_mqtt_message_set(s2w_mqtt_handle_ptr,
reason, topic,
topic_Length,
msg,
msg_Length,
QOS );
data_len = MSG_TO_HOST_LEN(topic_Length, msg_Length);
//indicate to the user.
}
typedef struct M2M_S2W_MQTT_S
{
UINT8 use_ssl;
M2M_S2W_MQTT_MESSAGE_T *msg_ptr_head;
M2M_S2W_MQTT_MESSAGE_T *msg_ptr_tail;
M2MB_SSL_CTXT_HANDLE pM2mbSslHandle;
}M2M_S2W_MQTT_T;
void m2m_mqtt_connect_callback( M2MB_MQTT_CTXT_HANDLE handle, INT32 status)
{
//connection success, here onwards user can do subscribe and publish.
else
//connection failed(status values are described in m2mb_net_status.h file
return;
}
void main(void)
{
M2MB_MQTT_OPEN_PARAMS_T open_params = {0};
M2MB_MQTT_CONN_PARAMS_T connect_params = {0};
INT8* HOST = "192.168.1.120";
INT32 PORT = 1883;
INT8* client_id = "mqttclient";
INT8* username = "username";
INT8* password = "password";
UINT32 keepalive_in_sec = 180;
UINT32 timeout_in_sec = 175;
M2M_S2W_MQTT_T *pS2wMqttHandle = NULL;
INT32 api_ret_code;
VOID* pCid = NULL;
char *cert_file = NULL;
pS2wMqttHandle = (M2M_S2W_MQTT_T *)MALLOC(sizeof(M2M_S2W_MQTT_T));
memset(pS2wMqttHandle, 0, sizeof(M2M_S2W_MQTT_T));
pS2wMqttHandle->use_ssl = 1;
{
return M2MB_ERROR;
}
open_params.client_id = client_id;
open_params.keepalive_in_sec = keepalive_in_sec;
open_params.username = username;
open_params.password = password;
open_params.timeout_in_sec = timeout_in_sec;
if(pS2wMqttHandle->use_ssl == TRUE)
{
M2MB_SSL_OPEN_PARAMS_T params = {0};
M2MB_STATUS_T ret_status = m2mb_ssl_open(&pS2wMqttHandle->pM2mbSslHandle, params );
if( (ret_status != M2MB_OK) || (pS2wMqttHandle->pM2mbSslHandle == NULL))
{
FREE(pS2wMqttHandle);
return M2MB_ERROR;
}
open_params.pSslCfg = pS2wMqttHandle->pM2mbSslHandle;
}
if (M2MB_OK != m2mb_mqtt_open(&s2w_mqtt_handle->handle, &open_params)
{
return M2MB_ERROR;
}
connect_params.host = HOST;
connect_params.port = PORT;
connect_params.connectcb = m2m_mqtt_connect_callback;
connect_params.connectcb_args = pCid;
api_ret_code = m2mb_mqtt_connect(s2w_mqtt_handle->handle, &connect_params);
if(api_ret_code != M2MB_OK)
{
return M2MB_ERROR;
}
ret_val = m2mb_mqtt_subscribe(s2w_mqtt_handle->handle, message_id, topic, QOS, m2m_s2w_mqtt_message_handler_cbk, s2w_mqtt_handle);
if(ret_val < 0)
{
return M2MB_ERROR;
}
m2mb_os_taskSleep(100);
ret_val = m2mb_mqtt_unsubscribe(s2w_mqtt_handle->handle, message_id, topic);
if(ret_val < 0)
{
return M2MB_ERROR;
}
}
}