Paho Asynchronous MQTT C Client Library: Subscription example

| Paho Asynchronous MQTT C Client Library

Subscription example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTAsync.h"
#if !defined(_WIN32)
#include <unistd.h>
#else
#include <windows.h>
#endif
#if defined(_WRS_KERNEL)
#include <OsWrapper.h>
#endif
#define ADDRESS "tcp://mqtt.eclipseprojects.io:1883"
#define CLIENTID "ExampleClientSub"
#define TOPIC "MQTT Examples"
#define PAYLOAD "Hello World!"
#define QOS 1
#define TIMEOUT 10000L
int disc_finished = 0;
int subscribed = 0;
int finished = 0;
void connlost(void *context, char *cause)
{
MQTTAsync client = (MQTTAsync)context;
MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
int rc;
printf("\nConnection lost\n");
if (cause)
printf(" cause: %s\n", cause);
printf("Reconnecting\n");
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
{
printf("Failed to start connect, return code %d\n", rc);
finished = 1;
}
}
int msgarrvd(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
{
printf("Message arrived\n");
printf(" topic: %s\n", topicName);
printf(" message: %.*s\n", message->payloadlen, (char*)message->payload);
MQTTAsync_freeMessage(&message);
MQTTAsync_free(topicName);
return 1;
}
void onDisconnectFailure(void* context, MQTTAsync_failureData* response)
{
printf("Disconnect failed, rc %d\n", response->code);
disc_finished = 1;
}
void onDisconnect(void* context, MQTTAsync_successData* response)
{
printf("Successful disconnection\n");
disc_finished = 1;
}
void onSubscribe(void* context, MQTTAsync_successData* response)
{
printf("Subscribe succeeded\n");
subscribed = 1;
}
void onSubscribeFailure(void* context, MQTTAsync_failureData* response)
{
printf("Subscribe failed, rc %d\n", response->code);
finished = 1;
}
void onConnectFailure(void* context, MQTTAsync_failureData* response)
{
printf("Connect failed, rc %d\n", response->code);
finished = 1;
}
void onConnect(void* context, MQTTAsync_successData* response)
{
MQTTAsync client = (MQTTAsync)context;
MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
int rc;
printf("Successful connection\n");
printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
"Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);
opts.onSuccess = onSubscribe;
opts.onFailure = onSubscribeFailure;
opts.context = client;
if ((rc = MQTTAsync_subscribe(client, TOPIC, QOS, &opts)) != MQTTASYNC_SUCCESS)
{
printf("Failed to start subscribe, return code %d\n", rc);
finished = 1;
}
}
int main(int argc, char* argv[])
{
MQTTAsync client;
MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
MQTTAsync_disconnectOptions disc_opts = MQTTAsync_disconnectOptions_initializer;
int rc;
int ch;
if ((rc = MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL))
!= MQTTASYNC_SUCCESS)
{
printf("Failed to create client, return code %d\n", rc);
rc = EXIT_FAILURE;
goto exit;
}
if ((rc = MQTTAsync_setCallbacks(client, client, connlost, msgarrvd, NULL)) != MQTTASYNC_SUCCESS)
{
printf("Failed to set callbacks, return code %d\n", rc);
rc = EXIT_FAILURE;
goto destroy_exit;
}
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
conn_opts.onSuccess = onConnect;
conn_opts.onFailure = onConnectFailure;
conn_opts.context = client;
if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
{
printf("Failed to start connect, return code %d\n", rc);
rc = EXIT_FAILURE;
goto destroy_exit;
}
while (!subscribed && !finished)
#if defined(_WIN32)
Sleep(100);
#else
usleep(10000L);
#endif
if (finished)
goto exit;
do
{
ch = getchar();
} while (ch!='Q' && ch != 'q');
disc_opts.onSuccess = onDisconnect;
disc_opts.onFailure = onDisconnectFailure;
if ((rc = MQTTAsync_disconnect(client, &disc_opts)) != MQTTASYNC_SUCCESS)
{
printf("Failed to start disconnect, return code %d\n", rc);
rc = EXIT_FAILURE;
goto destroy_exit;
}
while (!disc_finished)
{
#if defined(_WIN32)
Sleep(100);
#else
usleep(10000L);
#endif
}
destroy_exit:
MQTTAsync_destroy(&client);
exit:
return rc;
}
MQTTAsync.h
MQTTAsync_connect
int MQTTAsync_connect(MQTTAsync handle, const MQTTAsync_connectOptions *options)
MQTTAsync
void * MQTTAsync
Definition: MQTTAsync.h:253
MQTTAsync_free
void MQTTAsync_free(void *ptr)
MQTTAsync_disconnectOptions_initializer
#define MQTTAsync_disconnectOptions_initializer
Definition: MQTTAsync.h:1478
MQTTAsync_responseOptions_initializer
#define MQTTAsync_responseOptions_initializer
Definition: MQTTAsync.h:771
MQTTAsync_create
int MQTTAsync_create(MQTTAsync *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context)
MQTTAsync_freeMessage
void MQTTAsync_freeMessage(MQTTAsync_message **msg)
MQTTAsync_destroy
void MQTTAsync_destroy(MQTTAsync *handle)
MQTTAsync_disconnect
int MQTTAsync_disconnect(MQTTAsync handle, const MQTTAsync_disconnectOptions *options)
MQTTAsync_subscribe
int MQTTAsync_subscribe(MQTTAsync handle, const char *topic, int qos, MQTTAsync_responseOptions *response)
MQTTAsync_connectOptions_initializer
#define MQTTAsync_connectOptions_initializer
Definition: MQTTAsync.h:1384
MQTTAsync_setCallbacks
int MQTTAsync_setCallbacks(MQTTAsync handle, void *context, MQTTAsync_connectionLost *cl, MQTTAsync_messageArrived *ma, MQTTAsync_deliveryComplete *dc)
MQTTASYNC_SUCCESS
#define MQTTASYNC_SUCCESS
Definition: MQTTAsync.h:113
MQTTCLIENT_PERSISTENCE_NONE
#define MQTTCLIENT_PERSISTENCE_NONE
Definition: MQTTClientPersistence.h:74
MQTTAsync_connectOptions
Definition: MQTTAsync.h:1199
MQTTAsync_connectOptions::cleansession
int cleansession
Definition: MQTTAsync.h:1245
MQTTAsync_connectOptions::onFailure
MQTTAsync_onFailure * onFailure
Definition: MQTTAsync.h:1296
MQTTAsync_connectOptions::onSuccess
MQTTAsync_onSuccess * onSuccess
Definition: MQTTAsync.h:1290
MQTTAsync_connectOptions::keepAliveInterval
int keepAliveInterval
Definition: MQTTAsync.h:1223
MQTTAsync_connectOptions::context
void * context
Definition: MQTTAsync.h:1302
MQTTAsync_disconnectOptions
Definition: MQTTAsync.h:1428
MQTTAsync_disconnectOptions::onFailure
MQTTAsync_onFailure * onFailure
Definition: MQTTAsync.h:1449
MQTTAsync_disconnectOptions::onSuccess
MQTTAsync_onSuccess * onSuccess
Definition: MQTTAsync.h:1443
MQTTAsync_failureData
Definition: MQTTAsync.h:536
MQTTAsync_failureData::code
int code
Definition: MQTTAsync.h:540
MQTTAsync_message
Definition: MQTTAsync.h:272
MQTTAsync_message::payload
void * payload
Definition: MQTTAsync.h:281
MQTTAsync_message::payloadlen
int payloadlen
Definition: MQTTAsync.h:279
MQTTAsync_responseOptions
Definition: MQTTAsync.h:709
MQTTAsync_responseOptions::onFailure
MQTTAsync_onFailure * onFailure
Definition: MQTTAsync.h:726
MQTTAsync_responseOptions::onSuccess
MQTTAsync_onSuccess * onSuccess
Definition: MQTTAsync.h:720
MQTTAsync_responseOptions::context
void * context
Definition: MQTTAsync.h:732
MQTTAsync_successData
Definition: MQTTAsync.h:571