MQTTAsync_publish_time.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  * Frank Pagliughi - loop to repeatedly read and sent time values.
16  *******************************************************************************/
17 
18 // This is a somewhat contrived example to show an application that publishes
19 // continuously, like a data acquisition app might do. In this case, though,
20 // we don't have a sensor to read, so we use the system time as the number
21 // of milliseconds since the epoch to simulate a data input.
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdint.h>
27 #include <time.h>
28 #include "MQTTAsync.h"
29 
30 #if !defined(_WIN32)
31 #include <unistd.h>
32 #else
33 #include <windows.h>
34 #include <Minwinbase.h>
35 #endif
36 
37 #if defined(_WRS_KERNEL)
38 #include <OsWrapper.h>
39 #endif
40 
41 #if defined(_WIN32) || defined(_WIN64)
42 #define snprintf _snprintf
43 #endif
44 
45 
46 // Better not to flood a public broker. Test against localhost.
47 #define ADDRESS "tcp://localhost:1883"
48 
49 #define CLIENTID "ExampleClientTimePub"
50 #define TOPIC "data/time"
51 #define QOS 1
52 #define TIMEOUT 10000L
53 #define SAMPLE_PERIOD 10L // in ms
54 
55 volatile int finished = 0;
56 volatile int connected = 0;
57 
58 void connlost(void *context, char *cause)
59 {
60  MQTTAsync client = (MQTTAsync)context;
62  int rc;
63 
64  printf("\nConnection lost\n");
65  printf(" cause: %s\n", cause);
66 
67  printf("Reconnecting\n");
68  conn_opts.keepAliveInterval = 20;
69  conn_opts.cleansession = 1;
70  if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
71  {
72  printf("Failed to start connect, return code %d\n", rc);
73  finished = 1;
74  }
75 }
76 
78 {
79  printf("Disconnect failed\n");
80  finished = 1;
81 }
82 
84 {
85  printf("Successful disconnection\n");
86  finished = 1;
87 }
88 
90 {
91  MQTTAsync client = (MQTTAsync)context;
93  int rc;
94 
95  printf("Message send failed token %d error code %d\n", response->token, response->code);
96  opts.onSuccess = onDisconnect;
98  opts.context = client;
99  if ((rc = MQTTAsync_disconnect(client, &opts)) != MQTTASYNC_SUCCESS)
100  {
101  printf("Failed to start disconnect, return code %d\n", rc);
102  exit(EXIT_FAILURE);
103  }
104 }
105 
106 void onSend(void* context, MQTTAsync_successData* response)
107 {
108  // This gets called when a message is acknowledged successfully.
109 }
110 
111 
113 {
114  printf("Connect failed, rc %d\n", response ? response->code : 0);
115  finished = 1;
116 }
117 
118 
120 {
121  printf("Successful connection\n");
122  connected = 1;
123 }
124 
125 int messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* m)
126 {
127  /* not expecting any messages */
128  return 1;
129 }
130 
131 int64_t getTime(void)
132 {
133  #if defined(_WIN32)
134  FILETIME ft;
135  GetSystemTimeAsFileTime(&ft);
136  return ((((int64_t) ft.dwHighDateTime) << 8) + ft.dwLowDateTime) / 10000;
137  #else
138  struct timespec ts;
139  clock_gettime(CLOCK_REALTIME, &ts);
140  return ((int64_t) ts.tv_sec * 1000) + ((int64_t) ts.tv_nsec / 1000000);
141  #endif
142 }
143 
144 int main(int argc, char* argv[])
145 {
148 
151 
152  int rc;
153 
155  {
156  printf("Failed to create client object, return code %d\n", rc);
157  exit(EXIT_FAILURE);
158  }
159 
160  if ((rc = MQTTAsync_setCallbacks(client, NULL, connlost, messageArrived, NULL)) != MQTTASYNC_SUCCESS)
161  {
162  printf("Failed to set callback, return code %d\n", rc);
163  exit(EXIT_FAILURE);
164  }
165 
166  conn_opts.keepAliveInterval = 20;
167  conn_opts.cleansession = 1;
168  conn_opts.onSuccess = onConnect;
169  conn_opts.onFailure = onConnectFailure;
170  conn_opts.context = client;
171  if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
172  {
173  printf("Failed to start connect, return code %d\n", rc);
174  exit(EXIT_FAILURE);
175  }
176 
177  while (!connected) {
178  #if defined(_WIN32)
179  Sleep(100);
180  #else
181  usleep(100000L);
182  #endif
183  }
184 
185  while (!finished) {
186  int64_t t = getTime();
187 
188  char buf[256];
189  int n = snprintf(buf, sizeof(buf), "%lld", (long long) t);
190  printf("%s\n", buf);
191 
192  pub_opts.onSuccess = onSend;
193  pub_opts.onFailure = onSendFailure;
194  pub_opts.context = client;
195 
196  pubmsg.payload = buf;
197  pubmsg.payloadlen = n;
198  pubmsg.qos = QOS;
199  pubmsg.retained = 0;
200 
201  if ((rc = MQTTAsync_sendMessage(client, TOPIC, &pubmsg, &pub_opts)) != MQTTASYNC_SUCCESS)
202  {
203  printf("Failed to start sendMessage, return code %d\n", rc);
204  exit(EXIT_FAILURE);
205  }
206 
207  #if defined(_WIN32)
208  Sleep(SAMPLE_PERIOD);
209  #else
210  usleep(SAMPLE_PERIOD * 1000);
211  #endif
212  }
213 
214  MQTTAsync_destroy(&client);
215  return rc;
216 }
217 
MQTTAsync_onFailure * onFailure
Definition: MQTTAsync.h:1255
void onSend(void *context, MQTTAsync_successData *response)
#define MQTTAsync_responseOptions_initializer
Definition: MQTTAsync.h:746
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
int main(int argc, char *argv[])
struct pubsub_opts opts
Definition: paho_c_pub.c:42
void * MQTTAsync
Definition: MQTTAsync.h:239
MQTTAsync_responseOptions pub_opts
Definition: paho_c_pub.c:52
int messageArrived(void *context, char *topicName, int topicLen, MQTTAsync_message *m)
int MQTTAsync_connect(MQTTAsync handle, const MQTTAsync_connectOptions *options)
Definition: MQTTAsync.c:3480
MQTTAsync_connectOptions conn_opts
Definition: paho_c_sub.c:191
#define QOS
int MQTTAsync_create(MQTTAsync *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context)
Definition: MQTTAsync.c:737
#define TOPIC
#define MQTTAsync_disconnectOptions_initializer
Definition: MQTTAsync.h:1422
volatile int finished
MQTTAsync_onFailure * onFailure
Definition: MQTTAsync.h:702
MQTTAsync_onFailure * onFailure
Definition: MQTTAsync.h:1393
#define CLIENTID
#define MQTTAsync_connectOptions_initializer
Definition: MQTTAsync.h:1335
MQTTAsync_onSuccess * onSuccess
Definition: MQTTAsync.h:696
void connlost(void *context, char *cause)
#define ADDRESS
MQTTAsync client
Definition: test6.c:276
void onConnect(void *context, MQTTAsync_successData *response)
void MQTTAsync_destroy(MQTTAsync *handle)
Definition: MQTTAsync.c:2554
#define MQTTASYNC_SUCCESS
Definition: MQTTAsync.h:113
int MQTTAsync_sendMessage(MQTTAsync handle, const char *destinationName, const MQTTAsync_message *message, MQTTAsync_responseOptions *response)
Definition: MQTTAsync.c:4328
dictionary context
Definition: test2.py:57
int64_t getTime(void)
#define MQTTCLIENT_PERSISTENCE_NONE
#define SAMPLE_PERIOD
void onSendFailure(void *context, MQTTAsync_failureData *response)
MQTTAsync_token token
Definition: MQTTAsync.h:514
void onDisconnectFailure(void *context, MQTTAsync_failureData *response)
volatile int connected
void onDisconnect(void *context, MQTTAsync_successData *response)
#define MQTTAsync_message_initializer
Definition: MQTTAsync.h:319
enum MQTTReasonCodes rc
Definition: test10.c:1112
void onConnectFailure(void *context, MQTTAsync_failureData *response)
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