|
| 1 | +/** |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <aws/crt/Api.h> |
| 7 | +#include <aws/crt/UUID.h> |
| 8 | +#include <aws/crt/io/HostResolver.h> |
| 9 | + |
| 10 | +#include <aws/iot/MqttClient.h> |
| 11 | + |
| 12 | +#include <aws/iotsecuretunneling/IotSecureTunnelingClient.h> |
| 13 | +#include <aws/iotsecuretunneling/SecureTunnelingNotifyResponse.h> |
| 14 | +#include <aws/iotsecuretunneling/SubscribeToTunnelsNotifyRequest.h> |
| 15 | +#include <iostream> |
| 16 | + |
| 17 | +#include <algorithm> |
| 18 | +#include <condition_variable> |
| 19 | +#include <iostream> |
| 20 | +#include <mutex> |
| 21 | + |
| 22 | +using namespace std; |
| 23 | +using namespace Aws::Iot; |
| 24 | +using namespace Aws::Crt; |
| 25 | +using namespace Aws::Crt::Mqtt; |
| 26 | +using namespace Aws::Iotsecuretunneling; |
| 27 | + |
| 28 | +static void s_printHelp() |
| 29 | +{ |
| 30 | + fprintf(stdout, "Usage:\n"); |
| 31 | + fprintf( |
| 32 | + stdout, |
| 33 | + "tunnel-notification --endpoint <endpoint> --cert <path to cert>" |
| 34 | + " --key <path to key> --ca_file <optional: path to custom ca>" |
| 35 | + " --thing_name <thing name> \n\n"); |
| 36 | + fprintf(stdout, "endpoint: the endpoint of the mqtt server not including a port\n"); |
| 37 | + fprintf(stdout, "cert: path to your client certificate in PEM format\n"); |
| 38 | + fprintf(stdout, "key: path to your key in PEM format\n"); |
| 39 | + fprintf( |
| 40 | + stdout, |
| 41 | + "ca_file: Optional, if the mqtt server uses a certificate that's not already" |
| 42 | + " in your trust store, set this.\n"); |
| 43 | + fprintf(stdout, "\tIt's the path to a CA file in PEM format\n"); |
| 44 | + fprintf(stdout, "thing_name: the name of your IOT thing\n"); |
| 45 | +} |
| 46 | + |
| 47 | +bool s_cmdOptionExists(char **begin, char **end, const String &option) |
| 48 | +{ |
| 49 | + return std::find(begin, end, option) != end; |
| 50 | +} |
| 51 | + |
| 52 | +char *s_getCmdOption(char **begin, char **end, const String &option) |
| 53 | +{ |
| 54 | + char **itr = std::find(begin, end, option); |
| 55 | + if (itr != end && ++itr != end) |
| 56 | + { |
| 57 | + return *itr; |
| 58 | + } |
| 59 | + return 0; |
| 60 | +} |
| 61 | + |
| 62 | +int main(int argc, char *argv[]) |
| 63 | +{ |
| 64 | + /************************ Setup the Lib ****************************/ |
| 65 | + /* |
| 66 | + * Do the global initialization for the API. |
| 67 | + */ |
| 68 | + ApiHandle apiHandle; |
| 69 | + |
| 70 | + String endpoint; |
| 71 | + String certificatePath; |
| 72 | + String keyPath; |
| 73 | + String caFile; |
| 74 | + String clientId(String("test-") + Aws::Crt::UUID().ToString()); |
| 75 | + String thingName; |
| 76 | + |
| 77 | + /*********************** Parse Arguments ***************************/ |
| 78 | + if (!(s_cmdOptionExists(argv, argv + argc, "--endpoint") && s_cmdOptionExists(argv, argv + argc, "--cert") && |
| 79 | + s_cmdOptionExists(argv, argv + argc, "--key") && s_cmdOptionExists(argv, argv + argc, "--thing_name"))) |
| 80 | + { |
| 81 | + s_printHelp(); |
| 82 | + return 0; |
| 83 | + } |
| 84 | + |
| 85 | + endpoint = s_getCmdOption(argv, argv + argc, "--endpoint"); |
| 86 | + certificatePath = s_getCmdOption(argv, argv + argc, "--cert"); |
| 87 | + keyPath = s_getCmdOption(argv, argv + argc, "--key"); |
| 88 | + thingName = s_getCmdOption(argv, argv + argc, "--thing_name"); |
| 89 | + |
| 90 | + if (s_cmdOptionExists(argv, argv + argc, "--ca_file")) |
| 91 | + { |
| 92 | + caFile = s_getCmdOption(argv, argv + argc, "--ca_file"); |
| 93 | + } |
| 94 | + |
| 95 | + /********************** Now Setup an Mqtt Client ******************/ |
| 96 | + /* |
| 97 | + * You need an event loop group to process IO events. |
| 98 | + * If you only have a few connections, 1 thread is ideal |
| 99 | + */ |
| 100 | + Io::EventLoopGroup eventLoopGroup(1); |
| 101 | + if (!eventLoopGroup) |
| 102 | + { |
| 103 | + fprintf( |
| 104 | + stderr, "Event Loop Group Creation failed with error %s\n", ErrorDebugString(eventLoopGroup.LastError())); |
| 105 | + exit(-1); |
| 106 | + } |
| 107 | + |
| 108 | + Io::DefaultHostResolver defaultHostResolver(eventLoopGroup, 2, 30); |
| 109 | + Io::ClientBootstrap bootstrap(eventLoopGroup, defaultHostResolver); |
| 110 | + |
| 111 | + if (!bootstrap) |
| 112 | + { |
| 113 | + fprintf(stderr, "ClientBootstrap failed with error %s\n", ErrorDebugString(bootstrap.LastError())); |
| 114 | + exit(-1); |
| 115 | + } |
| 116 | + |
| 117 | + auto clientConfigBuilder = Aws::Iot::MqttClientConnectionConfigBuilder(certificatePath.c_str(), keyPath.c_str()); |
| 118 | + clientConfigBuilder.WithEndpoint(endpoint); |
| 119 | + if (!caFile.empty()) |
| 120 | + { |
| 121 | + clientConfigBuilder.WithCertificateAuthority(caFile.c_str()); |
| 122 | + } |
| 123 | + auto clientConfig = clientConfigBuilder.Build(); |
| 124 | + |
| 125 | + if (!clientConfig) |
| 126 | + { |
| 127 | + fprintf( |
| 128 | + stderr, |
| 129 | + "Client Configuration initialization failed with error %s\n", |
| 130 | + ErrorDebugString(clientConfig.LastError())); |
| 131 | + exit(-1); |
| 132 | + } |
| 133 | + |
| 134 | + /* |
| 135 | + * Now Create a client. This can not throw. |
| 136 | + * An instance of a client must outlive its connections. |
| 137 | + * It is the users responsibility to make sure of this. |
| 138 | + */ |
| 139 | + Aws::Iot::MqttClient mqttClient(bootstrap); |
| 140 | + |
| 141 | + /* |
| 142 | + * Since no exceptions are used, always check the bool operator |
| 143 | + * when an error could have occurred. |
| 144 | + */ |
| 145 | + if (!mqttClient) |
| 146 | + { |
| 147 | + fprintf(stderr, "MQTT Client Creation failed with error %s\n", ErrorDebugString(mqttClient.LastError())); |
| 148 | + exit(-1); |
| 149 | + } |
| 150 | + |
| 151 | + /* |
| 152 | + * Now create a connection object. Note: This type is move only |
| 153 | + * and its underlying memory is managed by the client. |
| 154 | + */ |
| 155 | + auto connection = mqttClient.NewConnection(clientConfig); |
| 156 | + |
| 157 | + if (!*connection) |
| 158 | + { |
| 159 | + fprintf(stderr, "MQTT Connection Creation failed with error %s\n", ErrorDebugString(connection->LastError())); |
| 160 | + exit(-1); |
| 161 | + } |
| 162 | + |
| 163 | + /* |
| 164 | + * In a real world application you probably don't want to enforce synchronous behavior |
| 165 | + * but this is a sample console application, so we'll just do that with a condition variable. |
| 166 | + */ |
| 167 | + std::promise<bool> connectionCompletedPromise; |
| 168 | + std::promise<void> connectionClosedPromise; |
| 169 | + |
| 170 | + /* |
| 171 | + * This will execute when an mqtt connect has completed or failed. |
| 172 | + */ |
| 173 | + auto onConnectionCompleted = [&](Mqtt::MqttConnection &, int errorCode, Mqtt::ReturnCode returnCode, bool) { |
| 174 | + if (errorCode) |
| 175 | + { |
| 176 | + fprintf(stdout, "Connection failed with error %s\n", ErrorDebugString(errorCode)); |
| 177 | + connectionCompletedPromise.set_value(false); |
| 178 | + } |
| 179 | + else |
| 180 | + { |
| 181 | + fprintf(stdout, "Connection completed with return code %d\n", returnCode); |
| 182 | + connectionCompletedPromise.set_value(true); |
| 183 | + } |
| 184 | + }; |
| 185 | + |
| 186 | + /* |
| 187 | + * Invoked when a disconnect message has completed. |
| 188 | + */ |
| 189 | + auto onDisconnect = [&](Mqtt::MqttConnection & /*conn*/) { |
| 190 | + { |
| 191 | + fprintf(stdout, "Disconnect completed\n"); |
| 192 | + connectionClosedPromise.set_value(); |
| 193 | + } |
| 194 | + }; |
| 195 | + |
| 196 | + connection->OnConnectionCompleted = std::move(onConnectionCompleted); |
| 197 | + connection->OnDisconnect = std::move(onDisconnect); |
| 198 | + |
| 199 | + /* |
| 200 | + * Actually perform the connect dance. |
| 201 | + */ |
| 202 | + fprintf(stdout, "Connecting...\n"); |
| 203 | + if (!connection->Connect(clientId.c_str(), true, 0)) |
| 204 | + { |
| 205 | + fprintf(stderr, "MQTT Connection failed with error %s\n", ErrorDebugString(connection->LastError())); |
| 206 | + exit(-1); |
| 207 | + } |
| 208 | + |
| 209 | + auto onSubscribeToTunnelsNotifyResponse = [&](Aws::Iotsecuretunneling::SecureTunnelingNotifyResponse *response, |
| 210 | + int ioErr) -> void { |
| 211 | + if (ioErr == 0) |
| 212 | + { |
| 213 | + fprintf(stdout, "Received MQTT Tunnel Notification\n"); |
| 214 | + |
| 215 | + std::string clientAccessToken = response->ClientAccessToken->c_str(); |
| 216 | + std::string clientMode = response->ClientMode->c_str(); |
| 217 | + std::string region = response->Region->c_str(); |
| 218 | + |
| 219 | + fprintf( |
| 220 | + stdout, |
| 221 | + "Recv: Token:%s, Mode:%s, Region:%s\n", |
| 222 | + clientAccessToken.c_str(), |
| 223 | + clientMode.c_str(), |
| 224 | + region.c_str()); |
| 225 | + |
| 226 | + size_t nServices = response->Services->size(); |
| 227 | + if (nServices <= 0) |
| 228 | + { |
| 229 | + fprintf(stdout, "No service requested\n"); |
| 230 | + } |
| 231 | + else |
| 232 | + { |
| 233 | + std::string service = response->Services->at(0).c_str(); |
| 234 | + fprintf(stdout, "Requested service=%s\n", service.c_str()); |
| 235 | + |
| 236 | + if (nServices > 1) |
| 237 | + { |
| 238 | + fprintf(stdout, "Multiple services not supported yet\n"); |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + else |
| 243 | + { |
| 244 | + fprintf(stderr, "MQTT Connection failed with error %d\n", ioErr); |
| 245 | + } |
| 246 | + /* Disconnect */ |
| 247 | + if (connection->Disconnect()) |
| 248 | + { |
| 249 | + connectionClosedPromise.get_future().wait(); |
| 250 | + } |
| 251 | + }; |
| 252 | + |
| 253 | + auto OnSubscribeComplete = [&](int ioErr) -> void { |
| 254 | + if (ioErr) |
| 255 | + { |
| 256 | + fprintf(stderr, "MQTT Connection failed with error %d\n", ioErr); |
| 257 | + } |
| 258 | + /* Disconnect */ |
| 259 | + if (connection->Disconnect()) |
| 260 | + { |
| 261 | + connectionClosedPromise.get_future().wait(); |
| 262 | + } |
| 263 | + }; |
| 264 | + |
| 265 | + if (connectionCompletedPromise.get_future().get()) |
| 266 | + { |
| 267 | + SubscribeToTunnelsNotifyRequest request; |
| 268 | + request.ThingName = thingName; |
| 269 | + |
| 270 | + IotSecureTunnelingClient client(connection); |
| 271 | + client.SubscribeToTunnelsNotify( |
| 272 | + request, AWS_MQTT_QOS_AT_LEAST_ONCE, onSubscribeToTunnelsNotifyResponse, OnSubscribeComplete); |
| 273 | + } |
| 274 | + while (1) |
| 275 | + { |
| 276 | + continue; |
| 277 | + } |
| 278 | +} |
0 commit comments