Greenbone Vulnerability Management Libraries 22.12.2
mqtt.h File Reference

Protos for MQTT handling. More...

#include <MQTTClient.h>
#include <glib.h>

Go to the source code of this file.

Macros

#define AUTH_MQTT   1
 

Functions

int mqtt_init (const char *)
 Init MQTT communication.
 
int mqtt_init_auth (const char *, const char *, const char *)
 Init MQTT communication.
 
gboolean mqtt_is_initialized (void)
 Get the global init status.
 
void mqtt_reset (void)
 Destroy MQTTClient handle and free mqtt_t.
 
int mqtt_publish (const char *, const char *)
 Publish a message on topic using the global client.
 
int mqtt_publish_single_message_auth (const char *, const char *, const char *, const char *, const char *)
 Send a single message with credentials.
 
int mqtt_publish_single_message (const char *, const char *, const char *)
 Send a single message.
 
int mqtt_subscribe (const char *)
 subscribes to a single topic.
 
int mqtt_retrieve_message (char **, int *, char **, int *, const unsigned int)
 wait for a given timeout in ms to retrieve any message of subscribed topics
 
int mqtt_unsubscribe (const char *)
 unsubscribe a single topic.
 

Detailed Description

Protos for MQTT handling.

Macro Definition Documentation

◆ AUTH_MQTT

#define AUTH_MQTT   1

Function Documentation

◆ mqtt_init()

int mqtt_init ( const char * server_uri)

Init MQTT communication.

Parameters
server_uriServer URI
Returns
0 on success, <0 on error.

◆ mqtt_init_auth()

int mqtt_init_auth ( const char * server_uri,
const char * username,
const char * password )

Init MQTT communication.

Parameters
server_uriServer URI
usernameUsername
passwordPassword
Returns
0 on success, <0 on error.

◆ mqtt_is_initialized()

gboolean mqtt_is_initialized ( void )

Get the global init status.

Returns
Initialization status of mqtt handling.

◆ mqtt_publish()

int mqtt_publish ( const char * topic,
const char * msg )

Publish a message on topic using the global client.

Parameters
topictopic
msgmessage
Returns
0 on success, <0 on error.

◆ mqtt_publish_single_message()

int mqtt_publish_single_message ( const char * server_uri_in,
const char * topic,
const char * msg )

Send a single message.

This functions creates a mqtt handle, connects, sends the message, closes the connection and destroys the handler. This function should not be chosen for repeated and frequent messaging. Its meant for error messages and the likes emitted by openvas.

Parameters
server_uri_inServer URI
topicTopic to publish to
msgMessage to publish
Returns
0 on success, <0 on failure.

◆ mqtt_publish_single_message_auth()

int mqtt_publish_single_message_auth ( const char * server_uri_in,
const char * username_in,
const char * passwd_in,
const char * topic,
const char * msg )

Send a single message with credentials.

This functions creates a mqtt handle, connects, sends the message, closes the connection and destroys the handler. This function should not be chosen for repeated and frequent messaging. Its meant for error messages and the likes emitted by openvas.

Parameters
server_uri_inServer URI
username_inUsername
passwd_inPassword
topicTopic to publish to
msgMessage to publish
Returns
0 on success, <0 on failure.

◆ mqtt_reset()

void mqtt_reset ( void )

Destroy MQTTClient handle and free mqtt_t.

◆ mqtt_retrieve_message()

int mqtt_retrieve_message ( char ** topic,
int * topic_len,
char ** payload,
int * payload_len,
const unsigned int timeout )

wait for a given timeout in ms to retrieve any message of subscribed topics

This function performs a synchronous receive of incoming messages. Using this function allows a single-threaded client subscriber application to be written. When called, this function blocks until the next message arrives or the specified timeout expires.

Important note: The application must free() the memory allocated to the topic and payload when processing is complete.

Parameters
[out]topicThe address of a pointer to a topic. This function allocates the memory for the topic and returns it to the application by setting topic to point to the topic.
[out]topic_lenThe length of the topic.
[out]payloadThe address of a pointer to the received message. This function allocates the memory for the payload and returns it to the application by setting payload to point to the received message. The pointer is set to NULL if the timeout expires.
[out]payload_lenThe length of the payload.
timeoutThe length of time to wait for a message in milliseconds.
Returns
0 on message retrieved, 1 on timeout and -1 on an error.

◆ mqtt_subscribe()

int mqtt_subscribe ( const char * topic)

subscribes to a single topic.

mqtt_subscribe uses global mqtt_t to subscribe with global qos to given topic.

To be able to subscribe to a topic the client needs to be connected to a broker. To do that call mqtt_init before mqtt_subscribe.

Parameters
topicTopic to subscribe to
Returns
0 on success, -1 when mqtt is not initialized, -2 when subscription failed.

◆ mqtt_unsubscribe()

int mqtt_unsubscribe ( const char * topic)

unsubscribe a single topic.

This function unsubscribes global client from a given topic.

Parameters
topicTopic to unsubscribe from
Returns
0 on success, -1 when given mqtt is not useable, -2 when unsubscribe failed.