|
| 1 | +/** |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0. |
| 4 | + */ |
| 5 | +#include <aws/crt/Api.h> |
| 6 | +#include <aws/crt/UUID.h> |
| 7 | +#include <aws/crt/io/Pkcs11.h> |
| 8 | + |
| 9 | +#include "../../utils/CommandLineUtils.h" |
| 10 | + |
| 11 | +using namespace Aws::Crt; |
| 12 | + |
| 13 | +int main(int argc, char *argv[]) |
| 14 | +{ |
| 15 | + |
| 16 | + /************************ Setup ****************************/ |
| 17 | + |
| 18 | + // Do the global initialization for the API. |
| 19 | + ApiHandle apiHandle; |
| 20 | + |
| 21 | + /** |
| 22 | + * cmdData is the arguments/input from the command line placed into a single struct for |
| 23 | + * use in this sample. This handles all of the command line parsing, validating, etc. |
| 24 | + * See the Utils/CommandLineUtils for more information. |
| 25 | + */ |
| 26 | + Utils::cmdData cmdData = Utils::parseSampleInputPKCS12Connect(argc, argv, &apiHandle); |
| 27 | + |
| 28 | + // Create the MQTT builder and populate it with data from cmdData. |
| 29 | + Aws::Iot::MqttClient client; |
| 30 | + struct Aws::Iot::Pkcs12Options options; |
| 31 | + options.pkcs12_file = cmdData.input_pkcs12File; |
| 32 | + options.pkcs12_password = cmdData.input_pkcs12Password; |
| 33 | + Aws::Iot::MqttClientConnectionConfigBuilder clientConfigBuilder(options); |
| 34 | + if (!clientConfigBuilder) |
| 35 | + { |
| 36 | + fprintf( |
| 37 | + stderr, |
| 38 | + "MqttClientConnectionConfigBuilder failed: %s\n", |
| 39 | + Aws::Crt::ErrorDebugString(Aws::Crt::LastError())); |
| 40 | + exit(-1); |
| 41 | + } |
| 42 | + if (cmdData.input_ca != "") |
| 43 | + { |
| 44 | + clientConfigBuilder.WithCertificateAuthority(cmdData.input_ca.c_str()); |
| 45 | + } |
| 46 | + clientConfigBuilder.WithEndpoint(cmdData.input_endpoint); |
| 47 | + |
| 48 | + // Create the MQTT connection from the MQTT builder |
| 49 | + auto clientConfig = clientConfigBuilder.Build(); |
| 50 | + if (!clientConfig) |
| 51 | + { |
| 52 | + fprintf( |
| 53 | + stderr, |
| 54 | + "Client Configuration initialization failed with error %s\n", |
| 55 | + Aws::Crt::ErrorDebugString(clientConfig.LastError())); |
| 56 | + exit(-1); |
| 57 | + } |
| 58 | + auto connection = client.NewConnection(clientConfig); |
| 59 | + if (!*connection) |
| 60 | + { |
| 61 | + fprintf( |
| 62 | + stderr, |
| 63 | + "MQTT Connection Creation failed with error %s\n", |
| 64 | + Aws::Crt::ErrorDebugString(connection->LastError())); |
| 65 | + exit(-1); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * In a real world application you probably don't want to enforce synchronous behavior |
| 70 | + * but this is a sample console application, so we'll just do that with a condition variable. |
| 71 | + */ |
| 72 | + std::promise<bool> connectionCompletedPromise; |
| 73 | + std::promise<void> connectionClosedPromise; |
| 74 | + |
| 75 | + // Invoked when a MQTT connect has completed or failed |
| 76 | + auto onConnectionCompleted = |
| 77 | + [&](Aws::Crt::Mqtt::MqttConnection &, int errorCode, Aws::Crt::Mqtt::ReturnCode returnCode, bool) { |
| 78 | + if (errorCode) |
| 79 | + { |
| 80 | + fprintf(stdout, "Connection failed with error %s\n", Aws::Crt::ErrorDebugString(errorCode)); |
| 81 | + connectionCompletedPromise.set_value(false); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + fprintf(stdout, "Connection completed with return code %d\n", returnCode); |
| 86 | + connectionCompletedPromise.set_value(true); |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + // Invoked when a MQTT connection was interrupted/lost |
| 91 | + auto onInterrupted = [&](Aws::Crt::Mqtt::MqttConnection &, int error) { |
| 92 | + fprintf(stdout, "Connection interrupted with error %s\n", Aws::Crt::ErrorDebugString(error)); |
| 93 | + }; |
| 94 | + |
| 95 | + // Invoked when a MQTT connection was interrupted/lost, but then reconnected successfully |
| 96 | + auto onResumed = [&](Aws::Crt::Mqtt::MqttConnection &, Aws::Crt::Mqtt::ReturnCode, bool) { |
| 97 | + fprintf(stdout, "Connection resumed\n"); |
| 98 | + }; |
| 99 | + |
| 100 | + // Invoked when a disconnect message has completed. |
| 101 | + auto onDisconnect = [&](Aws::Crt::Mqtt::MqttConnection &) { |
| 102 | + fprintf(stdout, "Disconnect completed\n"); |
| 103 | + connectionClosedPromise.set_value(); |
| 104 | + }; |
| 105 | + |
| 106 | + // Assign callbacks |
| 107 | + connection->OnConnectionCompleted = std::move(onConnectionCompleted); |
| 108 | + connection->OnDisconnect = std::move(onDisconnect); |
| 109 | + connection->OnConnectionInterrupted = std::move(onInterrupted); |
| 110 | + connection->OnConnectionResumed = std::move(onResumed); |
| 111 | + |
| 112 | + /************************ Run the sample ****************************/ |
| 113 | + |
| 114 | + // Connect |
| 115 | + fprintf(stdout, "Connecting...\n"); |
| 116 | + if (!connection->Connect(cmdData.input_clientId.c_str(), false /*cleanSession*/, 1000 /*keepAliveTimeSecs*/)) |
| 117 | + { |
| 118 | + fprintf(stderr, "MQTT Connection failed with error %s\n", Aws::Crt::ErrorDebugString(connection->LastError())); |
| 119 | + exit(-1); |
| 120 | + } |
| 121 | + |
| 122 | + // wait for the OnConnectionCompleted callback to fire, which sets connectionCompletedPromise... |
| 123 | + if (connectionCompletedPromise.get_future().get() == false) |
| 124 | + { |
| 125 | + fprintf(stderr, "Connection failed\n"); |
| 126 | + exit(-1); |
| 127 | + } |
| 128 | + |
| 129 | + // Disconnect |
| 130 | + if (connection->Disconnect()) |
| 131 | + { |
| 132 | + connectionClosedPromise.get_future().wait(); |
| 133 | + } |
| 134 | + return 0; |
| 135 | +} |
0 commit comments