S2OPC OPCUA Toolkit
Loading...
Searching...
No Matches
sopc_mqtt_transport_layer.h File Reference
#include <stdio.h>
#include <string.h>
#include "sopc_atomic.h"
#include "sopc_mem_alloc.h"
#include "sopc_mutexes.h"
#include "sopc_pubsub_conf.h"
#include "sopc_pubsub_constants.h"
#include "sopc_threads.h"
#include "sopc_time_reference.h"
#include "sopc_types.h"

Go to the source code of this file.

Macros

#define MQTT_LIB_QOS   (2) /* QOS of publish, subscribe set to 2*/
 
#define MQTT_LIB_MAX_SIZE_TOPIC_NAME   (256) /* Maximum length of a topic */
 
#define MQTT_LIB_MAX_NB_TOPIC_NAME   (256) /* Maximum subscriber topics that can be handle by client */
 
#define MQTT_LIB_CONNECTION_TIMEOUT   (4) /* Connection lib timeout = 4 s*/
 
#define MQTT_LIB_KEEPALIVE   (4) /* Connection lost detection set to 4 s*/
 

Typedefs

typedef struct MQTT_CONTEXT_CLIENT MqttContextClient
 
typedef enum MQTT_CLIENT_STATE MqttClientState
 
typedef void FctMessageReceived(uint8_t *data, uint16_t size, void *pUser)
 

Enumerations

enum  MQTT_CLIENT_STATE {
  SOPC_MQTT_CLIENT_UNITIALIZED , SOPC_MQTT_CLIENT_INITIALIZED = 1 , SOPC_MQTT_CLIENT_DISCONNECTED = 2 , SOPC_MQTT_CLIENT_CONNECTED = 3 ,
  SOPC_MQTT_CLIENT_FAILED_CONNECT = 4 , SOPC_MQTT_CLIENT_LOST_CONNECTION = 5
}
 

Functions

SOPC_ReturnStatus SOPC_MQTT_Send_Message (MqttContextClient *contextClient, const char *topic, SOPC_Buffer message)
 Send message to topic destination with MQTT client, contextClient must be successfully initialized with SOPC_MQTT_InitializeAndConnect_Client before using this function.
 
SOPC_ReturnStatus SOPC_MQTT_InitializeAndConnect_Client (MqttContextClient *contextClient, const char *uri, const char *username, const char *password, const char **subTopic, uint16_t nbSubTopic, FctMessageReceived *cbMessageReceived, SOPC_PubSub_OnFatalError *cbFatalError, void *userContext)
 Initialize MQTT client and connection, Shall be called after SOPC_MQTT_Create_Client To use MQTT client as a subscriber, number of sub topic must be superior to 0 and Topics to subscribe can't be NULL.
 
SOPC_ReturnStatus SOPC_MQTT_Create_Client (MqttContextClient **contextClient)
 Allocate memory for MQTT context client. Must be freed with SOPC_MQTT_Release_Client.
 
void SOPC_MQTT_Release_Client (MqttContextClient *contextClient)
 Disconnect from server and free MQTT client context pointer.
 
bool SOPC_MQTT_Client_Is_Connected (MqttContextClient *contextClient)
 Return if client is connected or not.
 
MqttClientState SOPC_MQTT_Client_Get_State (MqttContextClient *contextClient)
 Return state of the client.
 

Macro Definition Documentation

◆ MQTT_LIB_QOS

#define MQTT_LIB_QOS   (2) /* QOS of publish, subscribe set to 2*/

◆ MQTT_LIB_MAX_SIZE_TOPIC_NAME

#define MQTT_LIB_MAX_SIZE_TOPIC_NAME   (256) /* Maximum length of a topic */

◆ MQTT_LIB_MAX_NB_TOPIC_NAME

#define MQTT_LIB_MAX_NB_TOPIC_NAME   (256) /* Maximum subscriber topics that can be handle by client */

◆ MQTT_LIB_CONNECTION_TIMEOUT

#define MQTT_LIB_CONNECTION_TIMEOUT   (4) /* Connection lib timeout = 4 s*/

◆ MQTT_LIB_KEEPALIVE

#define MQTT_LIB_KEEPALIVE   (4) /* Connection lost detection set to 4 s*/

Typedef Documentation

◆ MqttContextClient

typedef struct MQTT_CONTEXT_CLIENT MqttContextClient

◆ MqttClientState

◆ FctMessageReceived

typedef void FctMessageReceived(uint8_t *data, uint16_t size, void *pUser)

Connection context

Parameters
dataData received
sizeSize of data received in bytes

Enumeration Type Documentation

◆ MQTT_CLIENT_STATE

Enumerator
SOPC_MQTT_CLIENT_UNITIALIZED 

Client isn't initialized with function SOPC_MQTT_InitializeAndConnect_Client

SOPC_MQTT_CLIENT_INITIALIZED 

Client is successfully initialized with function SOPC_MQTT_InitializeAndConnect_Client

SOPC_MQTT_CLIENT_DISCONNECTED 

Client is disconnected

SOPC_MQTT_CLIENT_CONNECTED 

Client is successfully connected

SOPC_MQTT_CLIENT_FAILED_CONNECT 

Client failed to connect

SOPC_MQTT_CLIENT_LOST_CONNECTION 

Client lost connection

Function Documentation

◆ SOPC_MQTT_Send_Message()

SOPC_ReturnStatus SOPC_MQTT_Send_Message ( MqttContextClient * contextClient,
const char * topic,
SOPC_Buffer message )

Send message to topic destination with MQTT client, contextClient must be successfully initialized with SOPC_MQTT_InitializeAndConnect_Client before using this function.

Parameters
contextClientContext for MQTT library containing MQTT client
topicTopic destination
messageBuffer with message information
Returns
SOPC_STATUS_OK if succeed sending message, SOPC_STATUS_NOK otherwise

◆ SOPC_MQTT_InitializeAndConnect_Client()

SOPC_ReturnStatus SOPC_MQTT_InitializeAndConnect_Client ( MqttContextClient * contextClient,
const char * uri,
const char * username,
const char * password,
const char ** subTopic,
uint16_t nbSubTopic,
FctMessageReceived * cbMessageReceived,
SOPC_PubSub_OnFatalError * cbFatalError,
void * userContext )

Initialize MQTT client and connection, Shall be called after SOPC_MQTT_Create_Client To use MQTT client as a subscriber, number of sub topic must be superior to 0 and Topics to subscribe can't be NULL.

Parameters
contextClientContext for MQTT library containing MQTT client
uriUri to broker endpoint
usernameUsername identifier optional. Can be NULL
passwordPassword required if identifier. Can be NULL
subTopicArray of topics to subscribe, can be NULL
nbSubTopicNumber of topics to subscribe, can be NULL
cbMessageReceivedCallback of message reception
cbFatalErrorCallback for broker disconnection
userContextConnection context of user (Passed to cbFatalError and cbMessageReceived)
Returns
SOPC_STATUS_OK if succeed to initialize mqtt contextClient, SOPC_STATUS_NOK if failed to create a mqtt Client or failed to connect

◆ SOPC_MQTT_Create_Client()

SOPC_ReturnStatus SOPC_MQTT_Create_Client ( MqttContextClient ** contextClient)

Allocate memory for MQTT context client. Must be freed with SOPC_MQTT_Release_Client.

Parameters
contextClientMQTT client context
Returns
SOPC_ReturnStatus SOPC_STATUS_OK if succeed to allocate memory, SOPC_STATUS_NOK otherwise

◆ SOPC_MQTT_Release_Client()

void SOPC_MQTT_Release_Client ( MqttContextClient * contextClient)

Disconnect from server and free MQTT client context pointer.

Parameters
contextClientMQTT client context

◆ SOPC_MQTT_Client_Is_Connected()

bool SOPC_MQTT_Client_Is_Connected ( MqttContextClient * contextClient)

Return if client is connected or not.

Parameters
contextClientA valid MQTT client context from a successful SOPC_MQTT_Create_Client
Returns
true if client is connected
false if client is not connected or if parameters are invalid

◆ SOPC_MQTT_Client_Get_State()

MqttClientState SOPC_MQTT_Client_Get_State ( MqttContextClient * contextClient)

Return state of the client.

Parameters
contextClientA valid MQTT client context from a successful SOPC_MQTT_Create_Client
Returns
MqttClientState state of client. Refers to enum description