From c9775a599023891eda1e06756c285f3ac323e12e Mon Sep 17 00:00:00 2001 From: Igor Abdrakhimov Date: Thu, 11 Jul 2024 17:48:18 -0700 Subject: [PATCH] fixup --- .../fleet_provisioning/main.cpp | 24 ++++++--------- .../mqtt5_fleet_provisioning/main.cpp | 29 ++++++++++--------- 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/samples/fleet_provisioning/fleet_provisioning/main.cpp b/samples/fleet_provisioning/fleet_provisioning/main.cpp index 8e5579ca4..f4f5fdfd5 100644 --- a/samples/fleet_provisioning/fleet_provisioning/main.cpp +++ b/samples/fleet_provisioning/fleet_provisioning/main.cpp @@ -6,8 +6,8 @@ /** * A sample application demonstrating usage of AWS IoT Fleet provisioning. * - * In a real world application you probably don't want to enforce synchronous behavior. But this is a sample console - * application, so all actions, like creating a certificate or registering a thing, are performed in synchronous manner. + * It's easier to follow a synchronous workflow, when events happen one after another. For that reason, this sample + * performs all actions, like connecting to a server or registering a thing, in synchronous manner. */ #include @@ -34,12 +34,12 @@ using namespace Aws::Crt; using namespace Aws::Iotidentity; -static std::string getFileData(std::string const &fileName) +static String getFileData(const String &fileName) { - std::ifstream ifs(fileName); + std::ifstream ifs(fileName.c_str()); std::string str; getline(ifs, str, (char)ifs.eof()); - return str; + return str.c_str(); } /** @@ -437,8 +437,6 @@ int main(int argc, char *argv[]) // Do the global initialization for the API ApiHandle apiHandle; - // Variables for the sample - String csrFile; /** * cmdData is the arguments/input from the command line placed into a single struct for @@ -447,11 +445,6 @@ int main(int argc, char *argv[]) */ Utils::cmdData cmdData = Utils::parseSampleInputFleetProvisioning(argc, argv, &apiHandle); - if (cmdData.input_csrPath != "") - { - csrFile = getFileData(cmdData.input_csrPath.c_str()).c_str(); - } - ConnectionContext connectionContext; auto connection = createConnection(cmdData, connectionContext); @@ -474,13 +467,14 @@ int main(int argc, char *argv[]) // Create certificate. CreateCertificateContext certificateContext; - if (csrFile.empty()) + if (cmdData.input_csrPath != "") { - createKeysAndCertificate(identityClient, certificateContext); + auto csrFile = getFileData(cmdData.input_csrPath); + createCertificateFromCsr(identityClient, certificateContext, csrFile); } else { - createCertificateFromCsr(identityClient, certificateContext, csrFile); + createKeysAndCertificate(identityClient, certificateContext); } // After certificate is obtained, it's time to register a thing. diff --git a/samples/fleet_provisioning/mqtt5_fleet_provisioning/main.cpp b/samples/fleet_provisioning/mqtt5_fleet_provisioning/main.cpp index 4389636f2..c84b8b3b9 100644 --- a/samples/fleet_provisioning/mqtt5_fleet_provisioning/main.cpp +++ b/samples/fleet_provisioning/mqtt5_fleet_provisioning/main.cpp @@ -2,6 +2,14 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0. */ + +/** + * A sample application demonstrating usage of AWS IoT Fleet provisioning with MQTT5 client. + * + * It's easier to follow a synchronous workflow, when events happen one after another. For that reason, this sample + * performs all actions, like connecting to a server or registering a thing, in synchronous manner. + */ + #include #include #include @@ -27,12 +35,12 @@ using namespace Aws::Crt; using namespace Aws::Iotidentity; -static std::string getFileData(std::string const &fileName) +static String getFileData(const String &fileName) { - std::ifstream ifs(fileName); + std::ifstream ifs(fileName.c_str()); std::string str; getline(ifs, str, (char)ifs.eof()); - return str; + return str.c_str(); } /** @@ -421,9 +429,6 @@ int main(int argc, char *argv[]) // Do the global initialization for the API ApiHandle apiHandle; - // Variables for the sample - String csrFile; - RegisterThingResponse registerThingResponse; /** * cmdData is the arguments/input from the command line placed into a single struct for @@ -432,11 +437,6 @@ int main(int argc, char *argv[]) */ Utils::cmdData cmdData = Utils::parseSampleInputFleetProvisioning(argc, argv, &apiHandle); - if (cmdData.input_csrPath != "") - { - csrFile = getFileData(cmdData.input_csrPath.c_str()).c_str(); - } - Mqtt5ClientContext mqtt5ClientContext; auto client = createMqtt5Client(mqtt5ClientContext, cmdData); @@ -459,13 +459,14 @@ int main(int argc, char *argv[]) // Create certificate. CreateCertificateContext certificateContext; - if (csrFile.empty()) + if (cmdData.input_csrPath != "") { - createKeysAndCertificate(identityClient, certificateContext); + auto csrFile = getFileData(cmdData.input_csrPath); + createCertificateFromCsr(identityClient, certificateContext, csrFile); } else { - createCertificateFromCsr(identityClient, certificateContext, csrFile); + createKeysAndCertificate(identityClient, certificateContext); } // After certificate is obtained, it's time to register a thing.