35#define G_LOG_DOMAIN "libgvm util"
168 rc = MQTTClient_disconnect5 (mqtt->
client, 200,
169 MQTTREASONCODE_NORMAL_DISCONNECTION, NULL);
170 if (rc != MQTTCLIENT_SUCCESS)
172 g_warning (
"Failed to disconnect: %s", MQTTClient_strerror (rc));
192 client = (MQTTClient) mqtt->
client;
196 MQTTClient_destroy (&client);
211 g_free ((*mqtt)->client_id);
222 g_debug (
"%s: start", __func__);
233 g_debug (
"%s: end", __func__);
249 MQTTClient_createOptions create_opts = MQTTClient_createOptions_initializer;
250 create_opts.MQTTVersion = MQTTVERSION_5;
252 if (mqtt == NULL || mqtt->
client_id == NULL)
255 int rc = MQTTClient_createWithOptions (&client, address, mqtt->
client_id,
256 MQTTCLIENT_PERSISTENCE_NONE, NULL,
259 if (rc != MQTTCLIENT_SUCCESS)
261 g_warning (
"%s: Error creating MQTTClient: %s", __func__,
262 MQTTClient_strerror (rc));
263 MQTTClient_destroy (&client);
318 const char *password)
322 MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer5;
323 MQTTProperties connect_properties = MQTTProperties_initializer;
333 conn_opts.keepAliveInterval = 0;
334 conn_opts.cleanstart = 1;
335 conn_opts.MQTTVersion = MQTTVERSION_5;
337 if (username != NULL && password != NULL)
339 conn_opts.username = username;
340 conn_opts.password = password;
343 resp = MQTTClient_connect5 (client, &conn_opts, &connect_properties, NULL);
344 rc = resp.reasonCode;
345 MQTTProperties_free (&connect_properties);
346 if (rc != MQTTCLIENT_SUCCESS)
349 "%s: mqtt connection error to %s: %s", __func__, server_uri,
350 MQTTClient_strerror (rc));
351 MQTTResponse_free (resp);
383 const char *password)
386 const char *g_server_uri;
387 const char *g_username;
388 const char *g_password;
390 g_debug (
"%s: start", __func__);
392 mqtt = g_malloc0 (
sizeof (
mqtt_t));
396 g_warning (
"%s: Could not set client id.", __func__);
401 g_debug (
"%s: client id set: %s", __func__, mqtt->
client_id);
403 if (g_server_uri == NULL)
407 if (g_username == NULL)
411 if (g_password == NULL)
414 if (
mqtt_connect (mqtt, server_uri, username, password))
416 g_warning (
"%s: Unable to connect to MQTT broker.", __func__);
425 g_debug (
"%s: end", __func__);
436 const char *server_uri;
437 const char *username;
438 const char *password;
441 if (server_uri == NULL)
443 g_warning (
"%s: mqtt_init() has to be called once at program start "
444 "else the server URI is not set. ",
465 MQTTClient_message pubmsg = MQTTClient_message_initializer;
466 MQTTClient_deliveryToken token;
476 pubmsg.payload = (
char *) msg;
477 pubmsg.payloadlen = (int) strlen (msg);
481 resp = MQTTClient_publishMessage5 (client, topic, &pubmsg, &token);
482 rc = resp.reasonCode;
483 if (rc != MQTTCLIENT_SUCCESS)
485 g_warning (
"Failed to connect: %s", MQTTClient_strerror (rc));
486 MQTTResponse_free (resp);
490 if ((rc = MQTTClient_waitForCompletion (client, token,
TIMEOUT))
491 != MQTTCLIENT_SUCCESS)
493 g_debug (
"Message '%s' with delivery token %d could not be "
494 "published on topic %s",
563 const char *username_in,
564 const char *passwd_in,
const char *topic,
567 const char *server_uri;
568 const char *username = NULL;
569 const char *password = NULL;
574 if (server_uri_in == NULL)
577 if (server_uri == NULL)
580 "%s: No server URI provided and no global server URI available.",
587 server_uri = server_uri_in;
590 if (username_in == NULL || passwd_in == NULL)
597 username = username_in;
598 password = passwd_in;
601 mqtt = g_malloc0 (
sizeof (
mqtt_t));
605 g_warning (
"%s: Could not set client id.", __func__);
639 if (mqtt == NULL || mqtt->
client == NULL)
643 MQTTSubscribe_options opts = MQTTSubscribe_options_initializer;
644 MQTTProperties props = MQTTProperties_initializer;
646 MQTTClient_subscribe5 (mqtt->
client, topic, qos, &opts, &props);
647 if (resp.reasonCode != MQTTREASONCODE_GRANTED_QOS_1)
691 if (mqtt == NULL || mqtt->
client == NULL)
696 if (MQTTClient_unsubscribe (mqtt->
client, topic) != MQTTCLIENT_SUCCESS)
746 char **payload,
int *payload_len,
747 const unsigned int timeout)
751 MQTTClient_message *message = NULL;
752 if (mqtt == NULL || mqtt->
client == NULL)
754 g_warning (
"mqtt is not initialized.");
759 rc = MQTTClient_receive (mqtt->
client, &tmp, topic_len, &message, timeout);
760 if (rc == MQTTCLIENT_SUCCESS || rc == MQTTCLIENT_TOPICNAME_TRUNCATED)
764 g_debug (
"%s: got message %s (%d) on topic %s (%d) \n", __func__,
765 (
char *) message->payload, message->payloadlen, tmp,
768 if ((*topic = calloc (1, *topic_len)) == NULL)
773 if ((memcpy (*topic, tmp, *topic_len)) == NULL)
775 g_warning (
"unable to copy topic");
780 *payload_len = message->payloadlen;
781 *payload = calloc (1, message->payloadlen);
782 if ((memcpy (*payload, (
char *) message->payload,
783 message->payloadlen))
786 g_warning (
"unable to copy payload");
807 MQTTClient_freeMessage (&message);
809 MQTTClient_free (tmp);
839 int *payload_len,
const unsigned int timeout)
842 payload, payload_len, timeout);
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.
static int mqtt_connect(mqtt_t *mqtt, const char *server_uri, const char *username, const char *password)
Make new client and connect to mqtt broker.
static const char * mqtt_get_global_server_uri()
Get global server URI.
static int mqtt_unsubscribe_r(mqtt_t *mqtt, const char *topic)
unsubscribe a single topic.
static void mqtt_set_global_password(const char *password)
Set the global mqtt password.
static void mqtt_set_global_client(mqtt_t *mqtt)
Set global client.
static const char * global_password
static int mqtt_set_client(mqtt_t *mqtt, MQTTClient client)
Set MQTTClient of mqtt_t.
static int mqtt_disconnect(mqtt_t *mqtt)
Disconnect from the Broker.
static void mqtt_set_initialized_status(gboolean status)
Set the global init status.
int mqtt_init_auth(const char *server_uri, const char *username, const char *password)
Init MQTT communication.
int mqtt_publish_single_message(const char *server_uri_in, const char *topic, const char *msg)
Send a single message.
static const char * mqtt_get_global_password()
Get global password.
gboolean mqtt_is_initialized()
Get the global init status.
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
int mqtt_unsubscribe(const char *topic)
unsubscribe a single topic.
static const char * global_server_uri
int mqtt_publish(const char *topic, const char *msg)
Publish a message on topic using the global client.
void mqtt_reset()
Destroy MQTTClient handle and free mqtt_t.
static void mqtt_reinit()
Reinitializes communication after mqtt_reset was used.
static void mqtt_client_destroy(mqtt_t *mqtt)
Destroy the MQTTClient client of the mqtt_t.
static void mqtt_set_global_username(const char *username)
Set the global mqtt username.
static mqtt_t * global_mqtt_client
int mqtt_init(const char *server_uri)
Init MQTT communication.
static mqtt_t * mqtt_get_global_client()
static int mqtt_retrieve_message_r(mqtt_t *mqtt, 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
static MQTTClient mqtt_create(mqtt_t *mqtt, const char *address)
Create a new mqtt client.
static char * mqtt_set_client_id(mqtt_t *mqtt)
Set a random client ID.
static int mqtt_subscribe_r(mqtt_t *mqtt, int qos, const char *topic)
subscribes to a single topic.
static void mqtt_set_global_server_uri(const char *server_uri_in)
Set the global mqtt server URI.
int mqtt_subscribe(const char *topic)
subscribes to a single topic.
static gboolean mqtt_initialized
static int mqtt_client_publish(mqtt_t *mqtt, const char *topic, const char *msg)
Use the provided client to publish message on a topic.
static const char * mqtt_get_global_username()
Get global username.
static const char * global_username
static void mqtt_client_data_destroy(mqtt_t **mqtt)
Destroy the mqtt_t data.
Protos for MQTT handling.
char * gvm_uuid_make(void)
Make a new universal identifier.