MQTTAsync_subscribe.c
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (c) 2012, 2020 IBM Corp.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v2.0
6  * and Eclipse Distribution License v1.0 which accompany this distribution.
7  *
8  * The Eclipse Public License is available at
9  * https://www.eclipse.org/legal/epl-2.0/
10  * and the Eclipse Distribution License is available at
11  * http://www.eclipse.org/org/documents/edl-v10.php.
12  *
13  * Contributors:
14  * Ian Craggs - initial contribution
15  *******************************************************************************/
16 
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include "MQTTAsync.h"
21 
22 #if !defined(_WIN32)
23 #include <unistd.h>
24 #else
25 #include <windows.h>
26 #endif
27 
28 #if defined(_WRS_KERNEL)
29 #include <OsWrapper.h>
30 #endif
31 
32 #define ADDRESS "tcp://mqtt.eclipse.org:1883"
33 #define CLIENTID "ExampleClientSub"
34 #define TOPIC "MQTT Examples"
35 #define PAYLOAD "Hello World!"
36 #define QOS 1
37 #define TIMEOUT 10000L
38 
39 int disc_finished = 0;
40 int subscribed = 0;
41 int finished = 0;
42 
43 void connlost(void *context, char *cause)
44 {
45  MQTTAsync client = (MQTTAsync)context;
47  int rc;
48 
49  printf("\nConnection lost\n");
50  if (cause)
51  printf(" cause: %s\n", cause);
52 
53  printf("Reconnecting\n");
54  conn_opts.keepAliveInterval = 20;
55  conn_opts.cleansession = 1;
56  if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
57  {
58  printf("Failed to start connect, return code %d\n", rc);
59  finished = 1;
60  }
61 }
62 
63 
64 int msgarrvd(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
65 {
66  printf("Message arrived\n");
67  printf(" topic: %s\n", topicName);
68  printf(" message: %.*s\n", message->payloadlen, (char*)message->payload);
69  MQTTAsync_freeMessage(&message);
70  MQTTAsync_free(topicName);
71  return 1;
72 }
73 
75 {
76  printf("Disconnect failed, rc %d\n", response->code);
77  disc_finished = 1;
78 }
79 
81 {
82  printf("Successful disconnection\n");
83  disc_finished = 1;
84 }
85 
87 {
88  printf("Subscribe succeeded\n");
89  subscribed = 1;
90 }
91 
93 {
94  printf("Subscribe failed, rc %d\n", response->code);
95  finished = 1;
96 }
97 
98 
100 {
101  printf("Connect failed, rc %d\n", response->code);
102  finished = 1;
103 }
104 
105 
107 {
108  MQTTAsync client = (MQTTAsync)context;
110  int rc;
111 
112  printf("Successful connection\n");
113 
114  printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
115  "Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);
116  opts.onSuccess = onSubscribe;
118  opts.context = client;
119  if ((rc = MQTTAsync_subscribe(client, TOPIC, QOS, &opts)) != MQTTASYNC_SUCCESS)
120  {
121  printf("Failed to start subscribe, return code %d\n", rc);
122  finished = 1;
123  }
124 }
125 
126 
127 int main(int argc, char* argv[])
128 {
132  int rc;
133  int ch;
134 
135  if ((rc = MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL))
137  {
138  printf("Failed to create client, return code %d\n", rc);
139  rc = EXIT_FAILURE;
140  goto exit;
141  }
142 
143  if ((rc = MQTTAsync_setCallbacks(client, client, connlost, msgarrvd, NULL)) != MQTTASYNC_SUCCESS)
144  {
145  printf("Failed to set callbacks, return code %d\n", rc);
146  rc = EXIT_FAILURE;
147  goto destroy_exit;
148  }
149 
150  conn_opts.keepAliveInterval = 20;
151  conn_opts.cleansession = 1;
152  conn_opts.onSuccess = onConnect;
153  conn_opts.onFailure = onConnectFailure;
154  conn_opts.context = client;
155  if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
156  {
157  printf("Failed to start connect, return code %d\n", rc);
158  rc = EXIT_FAILURE;
159  goto destroy_exit;
160  }
161 
162  while (!subscribed && !finished)
163  #if defined(_WIN32)
164  Sleep(100);
165  #else
166  usleep(10000L);
167  #endif
168 
169  if (finished)
170  goto exit;
171 
172  do
173  {
174  ch = getchar();
175  } while (ch!='Q' && ch != 'q');
176 
177  disc_opts.onSuccess = onDisconnect;
178  disc_opts.onFailure = onDisconnectFailure;
179  if ((rc = MQTTAsync_disconnect(client, &disc_opts)) != MQTTASYNC_SUCCESS)
180  {
181  printf("Failed to start disconnect, return code %d\n", rc);
182  rc = EXIT_FAILURE;
183  goto destroy_exit;
184  }
185  while (!disc_finished)
186  {
187  #if defined(_WIN32)
188  Sleep(100);
189  #else
190  usleep(10000L);
191  #endif
192  }
193 
194 destroy_exit:
195  MQTTAsync_destroy(&client);
196 exit:
197  return rc;
198 }
MQTTAsync_onFailure * onFailure
Definition: MQTTAsync.h:1255
#define CLIENTID
void onDisconnect(void *context, MQTTAsync_successData *response)
#define QOS
int disc_finished
#define MQTTAsync_responseOptions_initializer
Definition: MQTTAsync.h:746
void onDisconnectFailure(void *context, MQTTAsync_failureData *response)
int MQTTAsync_disconnect(MQTTAsync handle, const MQTTAsync_disconnectOptions *options)
Definition: MQTTAsync.c:3923
int MQTTAsync_setCallbacks(MQTTAsync handle, void *context, MQTTAsync_connectionLost *cl, MQTTAsync_messageArrived *ma, MQTTAsync_deliveryComplete *dc)
Definition: MQTTAsync.c:3062
struct pubsub_opts opts
Definition: paho_c_pub.c:42
void * MQTTAsync
Definition: MQTTAsync.h:239
void onConnectFailure(void *context, MQTTAsync_failureData *response)
void MQTTAsync_free(void *memory)
Definition: MQTTAsync.c:2626
void MQTTAsync_freeMessage(MQTTAsync_message **message)
Definition: MQTTAsync.c:2615
int MQTTAsync_connect(MQTTAsync handle, const MQTTAsync_connectOptions *options)
Definition: MQTTAsync.c:3480
MQTTAsync_connectOptions conn_opts
Definition: paho_c_sub.c:191
int subscribed
int MQTTAsync_subscribe(MQTTAsync handle, const char *topic, int qos, MQTTAsync_responseOptions *response)
Definition: MQTTAsync.c:4121
int MQTTAsync_create(MQTTAsync *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context)
Definition: MQTTAsync.c:737
int main(int argc, char *argv[])
#define MQTTAsync_disconnectOptions_initializer
Definition: MQTTAsync.h:1422
MQTTAsync_onFailure * onFailure
Definition: MQTTAsync.h:702
void connlost(void *context, char *cause)
MQTTAsync_onFailure * onFailure
Definition: MQTTAsync.h:1393
#define MQTTAsync_connectOptions_initializer
Definition: MQTTAsync.h:1335
MQTTAsync_onSuccess * onSuccess
Definition: MQTTAsync.h:696
void onSubscribe(void *context, MQTTAsync_successData *response)
#define ADDRESS
MQTTAsync client
Definition: test6.c:276
void MQTTAsync_destroy(MQTTAsync *handle)
Definition: MQTTAsync.c:2554
#define MQTTASYNC_SUCCESS
Definition: MQTTAsync.h:113
dictionary context
Definition: test2.py:57
#define TOPIC
#define MQTTCLIENT_PERSISTENCE_NONE
void onSubscribeFailure(void *context, MQTTAsync_failureData *response)
void onConnect(void *context, MQTTAsync_successData *response)
int finished
enum MQTTReasonCodes rc
Definition: test10.c:1112
int msgarrvd(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
MQTTAsync_onSuccess * onSuccess
Definition: MQTTAsync.h:1249
MQTTAsync_onSuccess * onSuccess
Definition: MQTTAsync.h:1387


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:09