-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathmain.cpp
More file actions
384 lines (345 loc) · 13.8 KB
/
main.cpp
File metadata and controls
384 lines (345 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/crt/Api.h>
#include <aws/crt/UUID.h>
#include <aws/crt/mqtt/Mqtt5Packets.h>
#include <aws/iot/Mqtt5Client.h>
#include <thread>
using namespace Aws::Crt;
/* --------------------------------- ARGUMENT PARSING ----------------------------------------- */
struct CmdArgs
{
String endpoint;
String cert;
String key;
String clientId;
String topic = "test/topic";
String message = "Hello from mqtt5 sample";
uint32_t count = 5;
};
void printHelp()
{
printf("MQTT5 X509 Sample (mTLS)\n");
printf("options:\n");
printf(" --help show this help message and exit\n");
printf("required arguments:\n");
printf(" --endpoint IoT endpoint hostname\n");
printf(
" --cert Path to the certificate file to use during mTLS connection establishment\n");
printf(
" --key Path to the private key file to use during mTLS connection establishment\n");
printf("optional arguments:\n");
printf(" --client_id Client ID (default: mqtt5-sample-<uuid>)\n");
printf(" --topic Topic (default: test/topic)\n");
printf(" --message Message payload (default: Hello from mqtt5 sample)\n");
printf(" --count Messages to publish (0 = infinite) (default: 5)\n");
}
CmdArgs parseArgs(int argc, char *argv[])
{
CmdArgs args;
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "--help") == 0)
{
printHelp();
exit(0);
}
else if (i < argc - 1)
{
if (strcmp(argv[i], "--endpoint") == 0)
{
args.endpoint = argv[++i];
}
else if (strcmp(argv[i], "--cert") == 0)
{
args.cert = argv[++i];
}
else if (strcmp(argv[i], "--key") == 0)
{
args.key = argv[++i];
}
else if (strcmp(argv[i], "--client_id") == 0)
{
args.clientId = argv[++i];
}
else if (strcmp(argv[i], "--topic") == 0)
{
args.topic = argv[++i];
}
else if (strcmp(argv[i], "--message") == 0)
{
args.message = argv[++i];
}
else if (strcmp(argv[i], "--count") == 0)
{
args.count = atoi(argv[++i]);
}
else
{
fprintf(stderr, "Unknown argument: %s\n", argv[i]);
printHelp();
exit(1);
}
}
}
if (args.endpoint.empty() || args.cert.empty() || args.key.empty())
{
fprintf(stderr, "Error: --endpoint, --cert, and --key are required\n");
printHelp();
exit(1);
}
if (args.clientId.empty())
{
args.clientId = String("mqtt5-sample-") + UUID().ToString();
}
return args;
}
/* --------------------------------- ARGUMENT PARSING END ----------------------------------------- */
int main(int argc, char *argv[])
{
// Parse command line arguments
CmdArgs cmdData = parseArgs(argc, argv);
// Variables needed for the sample
std::mutex receiveMutex;
std::condition_variable receiveSignal;
uint32_t receivedCount = 0;
std::promise<bool> connectionPromise;
std::promise<void> stoppedPromise;
std::promise<void> disconnectPromise;
std::promise<void> subscribeSuccess;
std::promise<void> unsubscribeFinishedPromise;
/* Do the global initialization for the API. */
ApiHandle apiHandle;
/**
* Create MQTT5 client builder using mutual TLS via X509 Certificate and Private Key,
* The builder will be used to create the final client
*/
auto builder = std::unique_ptr<Aws::Iot::Mqtt5ClientBuilder>(
Aws::Iot::Mqtt5ClientBuilder::NewMqtt5ClientBuilderWithMtlsFromPath(
cmdData.endpoint, cmdData.cert.c_str(), cmdData.key.c_str()));
// Check if the builder setup correctly.
if (builder == nullptr)
{
printf(
"Failed to setup Mqtt5 client builder with error code %d: %s\n", LastError(), ErrorDebugString(LastError()));
exit(1);
}
// Setup connection options
std::shared_ptr<Mqtt5::ConnectPacket> connectOptions =
Aws::Crt::MakeShared<Mqtt5::ConnectPacket>(Aws::Crt::DefaultAllocatorImplementation());
connectOptions->WithClientId(cmdData.clientId);
builder->WithConnectOptions(connectOptions);
/* Setup lifecycle callbacks */
// Callback when any publish is received
builder->WithPublishReceivedCallback(
[&receiveMutex, &receivedCount, &receiveSignal](const Mqtt5::PublishReceivedEventData &eventData)
{
if (eventData.publishPacket == nullptr)
return;
std::lock_guard<std::mutex> lock(receiveMutex);
++receivedCount;
fprintf(stdout, "==== Received message from topic '%s': ", eventData.publishPacket->getTopic().c_str());
fwrite(eventData.publishPacket->getPayload().ptr, 1, eventData.publishPacket->getPayload().len, stdout);
fprintf(stdout, " ====\n");
receiveSignal.notify_all();
});
// Callback for the lifecycle event the client Stopped
builder->WithClientStoppedCallback(
[&stoppedPromise](const Mqtt5::OnStoppedEventData &)
{
fprintf(stdout, "Lifecycle Stopped.\n");
stoppedPromise.set_value();
});
// Callback for lifecycle event Attempting Connect
builder->WithClientAttemptingConnectCallback(
[&cmdData](const Mqtt5::OnAttemptingConnectEventData &)
{
fprintf(
stdout,
"Lifecycle Connection Attempt\nConnecting to endpoint:'%s' with client ID '%s'\n",
cmdData.endpoint.c_str(),
cmdData.clientId.c_str());
});
// Callback for the lifecycle event Connection Success
builder->WithClientConnectionSuccessCallback(
[&connectionPromise](const Mqtt5::OnConnectionSuccessEventData &eventData)
{
fprintf(
stdout,
"Lifecycle Connection Success with reason code: %d\n",
eventData.connAckPacket->getReasonCode());
connectionPromise.set_value(true);
});
// Callback for the lifecycle event Connection Failure
builder->WithClientConnectionFailureCallback(
[&connectionPromise](const Mqtt5::OnConnectionFailureEventData &eventData)
{
fprintf(stdout, "Lifecycle Connection Failure with error: %s.\n", aws_error_debug_str(eventData.errorCode));
connectionPromise.set_value(false);
});
// Callback for the lifecycle event Connection get disconnected
builder->WithClientDisconnectionCallback(
[&disconnectPromise](const Mqtt5::OnDisconnectionEventData &eventData)
{
fprintf(stdout, "Lifecycle Disconnected.\n");
if (eventData.disconnectPacket != nullptr)
{
Mqtt5::DisconnectReasonCode reason_code = eventData.disconnectPacket->getReasonCode();
fprintf(stdout, "Disconnection packet code: %d.\n", reason_code);
fprintf(stdout, "Disconnection packet reason: %s.\n", aws_error_debug_str(reason_code));
}
disconnectPromise.set_value();
});
/* Create Mqtt5Client from the builder */
fprintf(stdout, "==== Creating MQTT5 Client ====\n");
std::shared_ptr<Aws::Crt::Mqtt5::Mqtt5Client> client = builder->Build();
if (client == nullptr)
{
fprintf(
stdout, "Failed to init Mqtt5Client with error code %d: %s\n", LastError(), ErrorDebugString(LastError()));
exit(1);
}
/**
* Start the client, instructing the client to desire a connected state. The client will try to
* establish a connection with the provided settings. If the client is disconnected while in this
* state it will attempt to reconnect automatically.
*/
fprintf(stdout, "==== Starting client ====\n");
client->Start();
// We await the `ClientConnectionSuccessCallback` callback to be invoked.
if (connectionPromise.get_future().get())
{
/**
* Subscribe
*/
// Setup the callback that will be triggered on receiveing SUBACK from the server
fprintf(stdout, "==== Subscribing to topic '%s' ====\n", cmdData.topic.c_str());
auto onSubAck = [&subscribeSuccess](int error_code, std::shared_ptr<Mqtt5::SubAckPacket> suback)
{
if (error_code != 0)
{
fprintf(
stdout,
"Subscription failed with error code: (%d)%s\n",
error_code,
aws_error_debug_str(error_code));
}
if (suback != nullptr)
{
for (Mqtt5::SubAckReasonCode reasonCode : suback->getReasonCodes())
{
fprintf(stdout, "Suback received with reason code: %d\n", reasonCode);
}
}
subscribeSuccess.set_value();
};
// Create a subscription object, and add it to a subscribe packet
Mqtt5::Subscription subscription(cmdData.topic, Mqtt5::QOS::AWS_MQTT5_QOS_AT_LEAST_ONCE);
subscription.WithNoLocal(false);
std::shared_ptr<Mqtt5::SubscribePacket> subPacket =
Aws::Crt::MakeShared<Mqtt5::SubscribePacket>(Aws::Crt::DefaultAllocatorImplementation());
subPacket->WithSubscription(std::move(subscription));
// Subscribe & wait for the subscription to complete
if (client->Subscribe(subPacket, onSubAck))
{
subscribeSuccess.get_future().wait();
}
/**
* Publish to the topics
*/
// Setup publish completion callback. The callback will get triggered when the publish completes (when
// the client received the PubAck from the server).
auto onPublishComplete = [](int, std::shared_ptr<Aws::Crt::Mqtt5::PublishResult> result)
{
if (!result->wasSuccessful())
{
fprintf(stdout, "Publish failed with error code: %d\n", result->getErrorCode());
}
else if (result != nullptr)
{
std::shared_ptr<Mqtt5::PubAckPacket> puback =
std::dynamic_pointer_cast<Mqtt5::PubAckPacket>(result->getAck());
fprintf(stdout, "PubAck received with: %d\n", puback->getReasonCode());
}
};
if (cmdData.count == 0)
{
fprintf(stdout, "==== Sending messages until program killed ====\n");
}
else
{
fprintf(stdout, "==== Sending %d message(s) ====\n", cmdData.count);
}
uint32_t publishedCount = 0;
while (publishedCount < cmdData.count || cmdData.count == 0)
{
// Add \" to 'JSON-ify' the message
String message = "\"" + cmdData.message + "[" + std::to_string(publishedCount + 1).c_str() + "]\"";
ByteCursor payload = ByteCursorFromString(message);
fprintf(stdout, "Publishing message to topic '%s': %s\n", cmdData.topic.c_str(), message.c_str());
// Create a publish packet
std::shared_ptr<Mqtt5::PublishPacket> publish = Aws::Crt::MakeShared<Mqtt5::PublishPacket>(
Aws::Crt::DefaultAllocatorImplementation(),
cmdData.topic,
payload,
Mqtt5::QOS::AWS_MQTT5_QOS_AT_LEAST_ONCE);
// Publish
if (client->Publish(publish, onPublishComplete))
{
++publishedCount;
}
// Sleep between publishes to avoid flooding the server
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
// Wait to receive all the messages we sent.
{
std::unique_lock<std::mutex> receivedLock(receiveMutex);
receiveSignal.wait(receivedLock, [&] { return receivedCount >= cmdData.count; });
}
fprintf(stdout, "%d message(s) received.\n", receivedCount);
/**
* Unsubscribe from the topic.
*/
fprintf(stdout, "==== Unsubscribing from topic '%s' ====\n", cmdData.topic.c_str());
// Setup the callback that will be triggered on receiveing UNSUBACK from the server
auto onUnSubAck = [&unsubscribeFinishedPromise](int error_code, std::shared_ptr<Mqtt5::UnSubAckPacket> unsuback)
{
if (error_code != 0)
{
fprintf(
stdout,
"Unsubscription failed with error code: (%d)%s\n",
error_code,
aws_error_debug_str(error_code));
}
if (unsuback != nullptr)
{
for (Mqtt5::UnSubAckReasonCode reasonCode : unsuback->getReasonCodes())
{
fprintf(stdout, "Unsubscribed with reason code: %d\n", reasonCode);
}
}
unsubscribeFinishedPromise.set_value();
};
// Create an unsubscribe packet
std::shared_ptr<Mqtt5::UnsubscribePacket> unsub =
Aws::Crt::MakeShared<Mqtt5::UnsubscribePacket>(Aws::Crt::DefaultAllocatorImplementation());
unsub->WithTopicFilter(cmdData.topic);
// Unsubscribe
if (client->Unsubscribe(unsub, onUnSubAck))
{
// Wait for unsubscription to finish
unsubscribeFinishedPromise.get_future().wait();
}
}
fprintf(stdout, "==== Stopping Client ====\n");
/* Stop the client. Instructs the client to disconnect and remain in a disconnected state. */
if (client->Stop())
{
stoppedPromise.get_future().wait();
fprintf(stdout, "==== Client Stopped! ====\n");
}
exit(0);
}