#include <stdio.h>
#include <stdlib.h>
#include <string.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)
{
int rc;
printf("\nConnection lost\n");
if (cause)
printf(" cause: %s\n", cause);
printf("Reconnecting\n");
{
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);
return 1;
}
{
printf(
"Disconnect failed, rc %d\n", response->
code);
disc_finished = 1;
}
{
printf("Successful disconnection\n");
disc_finished = 1;
}
{
printf("Subscribe succeeded\n");
subscribed = 1;
}
{
printf(
"Subscribe failed, rc %d\n", response->
code);
finished = 1;
}
{
printf(
"Connect failed, rc %d\n", response->
code);
finished = 1;
}
{
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);
{
printf("Failed to start subscribe, return code %d\n", rc);
finished = 1;
}
}
int main(int argc, char* argv[])
{
int rc;
int ch;
{
printf("Failed to create client, return code %d\n", rc);
rc = EXIT_FAILURE;
goto exit;
}
{
printf("Failed to set callbacks, return code %d\n", rc);
rc = EXIT_FAILURE;
goto destroy_exit;
}
{
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');
{
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:
exit:
return rc;
}
int MQTTAsync_connect(MQTTAsync handle, const MQTTAsync_connectOptions *options)
void * MQTTAsync
Definition: MQTTAsync.h:253
void MQTTAsync_free(void *ptr)
#define MQTTAsync_disconnectOptions_initializer
Definition: MQTTAsync.h:1478
#define MQTTAsync_responseOptions_initializer
Definition: MQTTAsync.h:771
int MQTTAsync_create(MQTTAsync *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context)
void MQTTAsync_freeMessage(MQTTAsync_message **msg)
void MQTTAsync_destroy(MQTTAsync *handle)
int MQTTAsync_disconnect(MQTTAsync handle, const MQTTAsync_disconnectOptions *options)
int MQTTAsync_subscribe(MQTTAsync handle, const char *topic, int qos, MQTTAsync_responseOptions *response)
#define MQTTAsync_connectOptions_initializer
Definition: MQTTAsync.h:1384
int MQTTAsync_setCallbacks(MQTTAsync handle, void *context, MQTTAsync_connectionLost *cl, MQTTAsync_messageArrived *ma, MQTTAsync_deliveryComplete *dc)
#define MQTTASYNC_SUCCESS
Definition: MQTTAsync.h:113
#define MQTTCLIENT_PERSISTENCE_NONE
Definition: MQTTClientPersistence.h:74
Definition: MQTTAsync.h:1199
int cleansession
Definition: MQTTAsync.h:1245
MQTTAsync_onFailure * onFailure
Definition: MQTTAsync.h:1296
MQTTAsync_onSuccess * onSuccess
Definition: MQTTAsync.h:1290
int keepAliveInterval
Definition: MQTTAsync.h:1223
void * context
Definition: MQTTAsync.h:1302
Definition: MQTTAsync.h:1428
MQTTAsync_onFailure * onFailure
Definition: MQTTAsync.h:1449
MQTTAsync_onSuccess * onSuccess
Definition: MQTTAsync.h:1443
Definition: MQTTAsync.h:536
int code
Definition: MQTTAsync.h:540
Definition: MQTTAsync.h:272
void * payload
Definition: MQTTAsync.h:281
int payloadlen
Definition: MQTTAsync.h:279
Definition: MQTTAsync.h:709
MQTTAsync_onFailure * onFailure
Definition: MQTTAsync.h:726
MQTTAsync_onSuccess * onSuccess
Definition: MQTTAsync.h:720
void * context
Definition: MQTTAsync.h:732
Definition: MQTTAsync.h:571