diff --git a/samples/fleet_provisioning/mqtt5_fleet_provisioning/main.cpp b/samples/fleet_provisioning/mqtt5_fleet_provisioning/main.cpp index 994d83a56..1fecd4c9d 100644 --- a/samples/fleet_provisioning/mqtt5_fleet_provisioning/main.cpp +++ b/samples/fleet_provisioning/mqtt5_fleet_provisioning/main.cpp @@ -22,29 +22,12 @@ #include #include -#include -#include -#include #include -#include -#include -#include -#include -#include -#include #include "../../utils/CommandLineUtils.h" using namespace Aws::Crt; using namespace Aws::Iotidentity; -using namespace std::this_thread; // sleep_for, sleep_until -using namespace std::chrono; // nanoseconds, system_clock, seconds - -static void sleep(int sleeptime) -{ - std::cout << "Sleeping for " << sleeptime << " seconds..." << std::endl; - sleep_until(system_clock::now() + seconds(sleeptime)); -} static std::string getFileData(std::string const &fileName) { @@ -141,321 +124,332 @@ int main(int argc, char *argv[]) exit(-1); } - if (connectionPromise.get_future().get()) + if (!connectionPromise.get_future().get()) { - IotIdentityClient identityClient(client); - - std::promise csrPublishCompletedPromise; - std::promise csrAcceptedCompletedPromise; - std::promise csrRejectedCompletedPromise; - - std::promise keysPublishCompletedPromise; - std::promise keysAcceptedCompletedPromise; - std::promise keysRejectedCompletedPromise; - - std::promise registerPublishCompletedPromise; - std::promise registerAcceptedCompletedPromise; - std::promise registerRejectedCompletedPromise; - - auto onCsrPublishSubAck = [&](int ioErr) { - if (ioErr != AWS_OP_SUCCESS) - { - fprintf(stderr, "Error publishing to CreateCertificateFromCsr: %s\n", ErrorDebugString(ioErr)); - exit(-1); - } - - csrPublishCompletedPromise.set_value(); - }; - - auto onCsrAcceptedSubAck = [&](int ioErr) { - if (ioErr != AWS_OP_SUCCESS) - { - fprintf( - stderr, "Error subscribing to CreateCertificateFromCsr accepted: %s\n", ErrorDebugString(ioErr)); - exit(-1); - } - - csrAcceptedCompletedPromise.set_value(); - }; - - auto onCsrRejectedSubAck = [&](int ioErr) { - if (ioErr != AWS_OP_SUCCESS) - { - fprintf( - stderr, "Error subscribing to CreateCertificateFromCsr rejected: %s\n", ErrorDebugString(ioErr)); - exit(-1); - } - csrRejectedCompletedPromise.set_value(); - }; - - auto onCsrAccepted = [&](CreateCertificateFromCsrResponse *response, int ioErr) { - if (ioErr == AWS_OP_SUCCESS) - { - fprintf( - stdout, "CreateCertificateFromCsrResponse certificateId: %s.\n", response->CertificateId->c_str()); - token = *response->CertificateOwnershipToken; - } - else - { - fprintf(stderr, "Error on subscription: %s.\n", ErrorDebugString(ioErr)); - exit(-1); - } - }; - - auto onCsrRejected = [&](ErrorResponse *error, int ioErr) { - if (ioErr == AWS_OP_SUCCESS) - { - fprintf( - stdout, - "CreateCertificateFromCsr failed with statusCode %d, errorMessage %s and errorCode %s.", - *error->StatusCode, - error->ErrorMessage->c_str(), - error->ErrorCode->c_str()); - exit(-1); - } - else - { - fprintf(stderr, "Error on subscription: %s.\n", ErrorDebugString(ioErr)); - exit(-1); - } - }; - - auto onKeysPublishSubAck = [&](int ioErr) { - if (ioErr != AWS_OP_SUCCESS) - { - fprintf(stderr, "Error publishing to CreateKeysAndCertificate: %s\n", ErrorDebugString(ioErr)); - exit(-1); - } - - keysPublishCompletedPromise.set_value(); - }; - - auto onKeysAcceptedSubAck = [&](int ioErr) { - if (ioErr != AWS_OP_SUCCESS) - { - fprintf( - stderr, "Error subscribing to CreateKeysAndCertificate accepted: %s\n", ErrorDebugString(ioErr)); - exit(-1); - } - - keysAcceptedCompletedPromise.set_value(); - }; - - auto onKeysRejectedSubAck = [&](int ioErr) { - if (ioErr != AWS_OP_SUCCESS) - { - fprintf( - stderr, "Error subscribing to CreateKeysAndCertificate rejected: %s\n", ErrorDebugString(ioErr)); - exit(-1); - } - keysRejectedCompletedPromise.set_value(); - }; - - auto onKeysAccepted = [&](CreateKeysAndCertificateResponse *response, int ioErr) { - if (ioErr == AWS_OP_SUCCESS) - { - fprintf( - stdout, "CreateKeysAndCertificateResponse certificateId: %s.\n", response->CertificateId->c_str()); - token = *response->CertificateOwnershipToken; - } - else - { - fprintf(stderr, "Error on subscription: %s.\n", ErrorDebugString(ioErr)); - exit(-1); - } - }; - - auto onKeysRejected = [&](ErrorResponse *error, int ioErr) { - if (ioErr == AWS_OP_SUCCESS) - { - fprintf( - stdout, - "CreateKeysAndCertificate failed with statusCode %d, errorMessage %s and errorCode %s.", - *error->StatusCode, - error->ErrorMessage->c_str(), - error->ErrorCode->c_str()); - exit(-1); - } - else - { - fprintf(stderr, "Error on subscription: %s.\n", ErrorDebugString(ioErr)); - exit(-1); - } - }; - - auto onRegisterAcceptedSubAck = [&](int ioErr) { - if (ioErr != AWS_OP_SUCCESS) - { - fprintf(stderr, "Error subscribing to RegisterThing accepted: %s\n", ErrorDebugString(ioErr)); - exit(-1); - } - - registerAcceptedCompletedPromise.set_value(); - }; - - auto onRegisterRejectedSubAck = [&](int ioErr) { - if (ioErr != AWS_OP_SUCCESS) - { - fprintf(stderr, "Error subscribing to RegisterThing rejected: %s\n", ErrorDebugString(ioErr)); - exit(-1); - } - registerRejectedCompletedPromise.set_value(); - }; - - auto onRegisterAccepted = [&](RegisterThingResponse *response, int ioErr) { - if (ioErr == AWS_OP_SUCCESS) - { - fprintf(stdout, "RegisterThingResponse ThingName: %s.\n", response->ThingName->c_str()); - } - else - { - fprintf(stderr, "Error on subscription: %s.\n", ErrorDebugString(ioErr)); - exit(-1); - } - }; - - auto onRegisterRejected = [&](ErrorResponse *error, int ioErr) { - if (ioErr == AWS_OP_SUCCESS) - { - fprintf( - stdout, - "RegisterThing failed with statusCode %d, errorMessage %s and errorCode %s.", - *error->StatusCode, - error->ErrorMessage->c_str(), - error->ErrorCode->c_str()); - } - else - { - fprintf(stderr, "Error on subscription: %s.\n", ErrorDebugString(ioErr)); - exit(-1); - } - }; - - auto onRegisterPublishSubAck = [&](int ioErr) { - if (ioErr != AWS_OP_SUCCESS) - { - fprintf(stderr, "Error publishing to RegisterThing: %s\n", ErrorDebugString(ioErr)); - exit(-1); - } - - registerPublishCompletedPromise.set_value(); - }; - - if (csrFile.empty()) + return -1; + } + IotIdentityClient identityClient(client); + + /* + * Use promises to enforce synchronous behavior, so it's easier to follow the workflow. + */ + std::promise csrPublishPubAckCompletedPromise; + std::promise csrAcceptedSubAckCompletedPromise; + std::promise csrRejectedSubAckCompletedPromise; + std::promise csrAcceptedCompletedPromise; + + std::promise keysPublishPubAckCompletedPromise; + std::promise keysAcceptedSubAckCompletedPromise; + std::promise keysRejectedSubAckCompletedPromise; + std::promise keysAcceptedCompletedPromise; + + std::promise registerPublishPubAckCompletedPromise; + std::promise registerAcceptedSubAckCompletedPromise; + std::promise registerRejectedSubAckCompletedPromise; + std::promise registerAcceptedCompletedPromise; + + auto onCsrPublishPubAck = [&](int ioErr) { + if (ioErr != AWS_OP_SUCCESS) + { + fprintf(stderr, "Error publishing to CreateCertificateFromCsr: %s\n", ErrorDebugString(ioErr)); + exit(-1); + } + + csrPublishPubAckCompletedPromise.set_value(); + }; + + auto onCsrAcceptedSubAck = [&](int ioErr) { + if (ioErr != AWS_OP_SUCCESS) + { + fprintf(stderr, "Error subscribing to CreateCertificateFromCsr accepted: %s\n", ErrorDebugString(ioErr)); + exit(-1); + } + + csrAcceptedSubAckCompletedPromise.set_value(); + }; + + auto onCsrRejectedSubAck = [&](int ioErr) { + if (ioErr != AWS_OP_SUCCESS) + { + fprintf(stderr, "Error subscribing to CreateCertificateFromCsr rejected: %s\n", ErrorDebugString(ioErr)); + exit(-1); + } + csrRejectedSubAckCompletedPromise.set_value(); + }; + + auto onCsrAccepted = [&](CreateCertificateFromCsrResponse *response, int ioErr) { + if (ioErr == AWS_OP_SUCCESS) + { + fprintf(stdout, "CreateCertificateFromCsrResponse certificateId: %s.\n", response->CertificateId->c_str()); + token = *response->CertificateOwnershipToken; + } + else + { + fprintf(stderr, "Error on subscription: %s.\n", ErrorDebugString(ioErr)); + exit(-1); + } + }; + + auto onCsrRejected = [&](ErrorResponse *error, int ioErr) { + if (ioErr == AWS_OP_SUCCESS) { - // CreateKeysAndCertificate workflow - std::cout << "Subscribing to CreateKeysAndCertificate Accepted and Rejected topics" << std::endl; - CreateKeysAndCertificateSubscriptionRequest keySubscriptionRequest; - identityClient.SubscribeToCreateKeysAndCertificateAccepted( - keySubscriptionRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onKeysAccepted, onKeysAcceptedSubAck); - - identityClient.SubscribeToCreateKeysAndCertificateRejected( - keySubscriptionRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onKeysRejected, onKeysRejectedSubAck); - - std::cout << "Publishing to CreateKeysAndCertificate topic" << std::endl; - CreateKeysAndCertificateRequest createKeysAndCertificateRequest; - identityClient.PublishCreateKeysAndCertificate( - createKeysAndCertificateRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onKeysPublishSubAck); - - std::cout << "Subscribing to RegisterThing Accepted and Rejected topics" << std::endl; - RegisterThingSubscriptionRequest registerSubscriptionRequest; - registerSubscriptionRequest.TemplateName = cmdData.input_templateName; - - identityClient.SubscribeToRegisterThingAccepted( - registerSubscriptionRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onRegisterAccepted, onRegisterAcceptedSubAck); - - identityClient.SubscribeToRegisterThingRejected( - registerSubscriptionRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onRegisterRejected, onRegisterRejectedSubAck); - - sleep(1); - - std::cout << "Publishing to RegisterThing topic" << std::endl; - RegisterThingRequest registerThingRequest; - registerThingRequest.TemplateName = cmdData.input_templateName; - - const Aws::Crt::String jsonValue = cmdData.input_templateParameters; - Aws::Crt::JsonObject value(jsonValue); - Map pm = value.View().GetAllObjects(); - Aws::Crt::Map params = - Aws::Crt::Map(); - - for (const auto &x : pm) - { - params.emplace(x.first, x.second.AsString()); - } - - registerThingRequest.Parameters = params; - registerThingRequest.CertificateOwnershipToken = token; - - identityClient.PublishRegisterThing( - registerThingRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onRegisterPublishSubAck); - sleep(1); - - keysPublishCompletedPromise.get_future().wait(); - keysAcceptedCompletedPromise.get_future().wait(); - keysRejectedCompletedPromise.get_future().wait(); - registerPublishCompletedPromise.get_future().wait(); - registerAcceptedCompletedPromise.get_future().wait(); - registerRejectedCompletedPromise.get_future().wait(); + fprintf( + stdout, + "CreateCertificateFromCsr failed with statusCode %d, errorMessage %s and errorCode %s.", + *error->StatusCode, + error->ErrorMessage->c_str(), + error->ErrorCode->c_str()); + exit(-1); } else { - // CreateCertificateFromCsr workflow - std::cout << "Subscribing to CreateCertificateFromCsr Accepted and Rejected topics" << std::endl; - CreateCertificateFromCsrSubscriptionRequest csrSubscriptionRequest; - identityClient.SubscribeToCreateCertificateFromCsrAccepted( - csrSubscriptionRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onCsrAccepted, onCsrAcceptedSubAck); - - identityClient.SubscribeToCreateCertificateFromCsrRejected( - csrSubscriptionRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onCsrRejected, onCsrRejectedSubAck); - - std::cout << "Publishing to CreateCertificateFromCsr topic" << std::endl; - CreateCertificateFromCsrRequest createCertificateFromCsrRequest; - createCertificateFromCsrRequest.CertificateSigningRequest = csrFile; - identityClient.PublishCreateCertificateFromCsr( - createCertificateFromCsrRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onCsrPublishSubAck); - - std::cout << "Subscribing to RegisterThing Accepted and Rejected topics" << std::endl; - RegisterThingSubscriptionRequest registerSubscriptionRequest; - registerSubscriptionRequest.TemplateName = cmdData.input_templateName; - - identityClient.SubscribeToRegisterThingAccepted( - registerSubscriptionRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onRegisterAccepted, onRegisterAcceptedSubAck); - - identityClient.SubscribeToRegisterThingRejected( - registerSubscriptionRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onRegisterRejected, onRegisterRejectedSubAck); - - sleep(2); - - std::cout << "Publishing to RegisterThing topic" << std::endl; - RegisterThingRequest registerThingRequest; - registerThingRequest.TemplateName = cmdData.input_templateName; - - const Aws::Crt::String jsonValue = cmdData.input_templateParameters; - Aws::Crt::JsonObject value(jsonValue); - Map pm = value.View().GetAllObjects(); - Aws::Crt::Map params = - Aws::Crt::Map(); - - for (const auto &x : pm) - { - params.emplace(x.first, x.second.AsString()); - } - - registerThingRequest.Parameters = params; - registerThingRequest.CertificateOwnershipToken = token; - - identityClient.PublishRegisterThing( - registerThingRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onRegisterPublishSubAck); - sleep(2); - - csrPublishCompletedPromise.get_future().wait(); - csrAcceptedCompletedPromise.get_future().wait(); - csrRejectedCompletedPromise.get_future().wait(); - registerPublishCompletedPromise.get_future().wait(); - registerAcceptedCompletedPromise.get_future().wait(); - registerRejectedCompletedPromise.get_future().wait(); + fprintf(stderr, "Error on subscription: %s.\n", ErrorDebugString(ioErr)); + exit(-1); + } + }; + + auto onKeysPublishPubAck = [&](int ioErr) { + if (ioErr != AWS_OP_SUCCESS) + { + fprintf(stderr, "Error publishing to CreateKeysAndCertificate: %s\n", ErrorDebugString(ioErr)); + exit(-1); } + + keysPublishPubAckCompletedPromise.set_value(); + }; + + auto onKeysAcceptedSubAck = [&](int ioErr) { + if (ioErr != AWS_OP_SUCCESS) + { + fprintf(stderr, "Error subscribing to CreateKeysAndCertificate accepted: %s\n", ErrorDebugString(ioErr)); + exit(-1); + } + + keysAcceptedSubAckCompletedPromise.set_value(); + }; + + auto onKeysRejectedSubAck = [&](int ioErr) { + if (ioErr != AWS_OP_SUCCESS) + { + fprintf(stderr, "Error subscribing to CreateKeysAndCertificate rejected: %s\n", ErrorDebugString(ioErr)); + exit(-1); + } + keysRejectedSubAckCompletedPromise.set_value(); + }; + + auto onKeysAccepted = [&](CreateKeysAndCertificateResponse *response, int ioErr) { + if (ioErr == AWS_OP_SUCCESS) + { + fprintf(stdout, "CreateKeysAndCertificateResponse certificateId: %s.\n", response->CertificateId->c_str()); + token = *response->CertificateOwnershipToken; + } + else + { + fprintf(stderr, "Error on subscription: %s.\n", ErrorDebugString(ioErr)); + exit(-1); + } + }; + + auto onKeysRejected = [&](ErrorResponse *error, int ioErr) { + if (ioErr == AWS_OP_SUCCESS) + { + fprintf( + stdout, + "CreateKeysAndCertificate failed with statusCode %d, errorMessage %s and errorCode %s.", + *error->StatusCode, + error->ErrorMessage->c_str(), + error->ErrorCode->c_str()); + exit(-1); + } + else + { + fprintf(stderr, "Error on subscription: %s.\n", ErrorDebugString(ioErr)); + exit(-1); + } + }; + + auto onRegisterAcceptedSubAck = [&](int ioErr) { + if (ioErr != AWS_OP_SUCCESS) + { + fprintf(stderr, "Error subscribing to RegisterThing accepted: %s\n", ErrorDebugString(ioErr)); + exit(-1); + } + + registerAcceptedSubAckCompletedPromise.set_value(); + }; + + auto onRegisterRejectedSubAck = [&](int ioErr) { + if (ioErr != AWS_OP_SUCCESS) + { + fprintf(stderr, "Error subscribing to RegisterThing rejected: %s\n", ErrorDebugString(ioErr)); + exit(-1); + } + registerRejectedSubAckCompletedPromise.set_value(); + }; + + auto onRegisterAccepted = [&](RegisterThingResponse *response, int ioErr) { + if (ioErr == AWS_OP_SUCCESS) + { + fprintf(stdout, "RegisterThingResponse ThingName: %s.\n", response->ThingName->c_str()); + } + else + { + fprintf(stderr, "Error on subscription: %s.\n", ErrorDebugString(ioErr)); + exit(-1); + } + }; + + auto onRegisterRejected = [&](ErrorResponse *error, int ioErr) { + if (ioErr == AWS_OP_SUCCESS) + { + fprintf( + stdout, + "RegisterThing failed with statusCode %d, errorMessage %s and errorCode %s.", + *error->StatusCode, + error->ErrorMessage->c_str(), + error->ErrorCode->c_str()); + } + else + { + fprintf(stderr, "Error on subscription: %s.\n", ErrorDebugString(ioErr)); + exit(-1); + } + }; + + auto onRegisterPublishPubAck = [&](int ioErr) { + if (ioErr != AWS_OP_SUCCESS) + { + fprintf(stderr, "Error publishing to RegisterThing: %s\n", ErrorDebugString(ioErr)); + exit(-1); + } + + registerPublishPubAckCompletedPromise.set_value(); + }; + + if (csrFile.empty()) + { + // CreateKeysAndCertificate workflow + fprintf(stdout, "Subscribing to CreateKeysAndCertificate Accepted and Rejected topics\n"); + CreateKeysAndCertificateSubscriptionRequest keySubscriptionRequest; + identityClient.SubscribeToCreateKeysAndCertificateAccepted( + keySubscriptionRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onKeysAccepted, onKeysAcceptedSubAck); + identityClient.SubscribeToCreateKeysAndCertificateRejected( + keySubscriptionRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onKeysRejected, onKeysRejectedSubAck); + + // Wait for the subscriptions to the accept and reject key/certificates topics to be established. + keysAcceptedSubAckCompletedPromise.get_future().wait(); + keysRejectedSubAckCompletedPromise.get_future().wait(); + + // Now, when we subscribed to the keys and certificates topics, we can make a request for a certificate. + fprintf(stdout, "Publishing to CreateKeysAndCertificate topic\n"); + CreateKeysAndCertificateRequest createKeysAndCertificateRequest; + identityClient.PublishCreateKeysAndCertificate( + createKeysAndCertificateRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onKeysPublishPubAck); + keysPublishPubAckCompletedPromise.get_future().wait(); + + // Wait for a certificate token. + keysAcceptedCompletedPromise.get_future().wait(); + + fprintf(stdout, "Subscribing to RegisterThing Accepted and Rejected topics\n"); + RegisterThingSubscriptionRequest registerSubscriptionRequest; + registerSubscriptionRequest.TemplateName = cmdData.input_templateName; + + identityClient.SubscribeToRegisterThingAccepted( + registerSubscriptionRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onRegisterAccepted, onRegisterAcceptedSubAck); + + identityClient.SubscribeToRegisterThingRejected( + registerSubscriptionRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onRegisterRejected, onRegisterRejectedSubAck); + + // Wait for the subscriptions to the accept and reject RegisterThing topics to be established. + registerAcceptedSubAckCompletedPromise.get_future().wait(); + registerRejectedSubAckCompletedPromise.get_future().wait(); + + fprintf(stdout, "Publishing to RegisterThing topic\n"); + RegisterThingRequest registerThingRequest; + registerThingRequest.TemplateName = cmdData.input_templateName; + + const Aws::Crt::String jsonValue = cmdData.input_templateParameters; + Aws::Crt::JsonObject value(jsonValue); + Map pm = value.View().GetAllObjects(); + Aws::Crt::Map params = Aws::Crt::Map(); + + for (const auto &x : pm) + { + params.emplace(x.first, x.second.AsString()); + } + + registerThingRequest.Parameters = params; + registerThingRequest.CertificateOwnershipToken = token; + + identityClient.PublishRegisterThing(registerThingRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onRegisterPublishPubAck); + registerPublishPubAckCompletedPromise.get_future().wait(); + + // Wait for registering a thing to succeed. + registerAcceptedCompletedPromise.get_future().wait(); + } + else + { + // CreateCertificateFromCsr workflow + fprintf(stdout, "Subscribing to CreateCertificateFromCsr Accepted and Rejected topics\n"); + CreateCertificateFromCsrSubscriptionRequest csrSubscriptionRequest; + identityClient.SubscribeToCreateCertificateFromCsrAccepted( + csrSubscriptionRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onCsrAccepted, onCsrAcceptedSubAck); + + identityClient.SubscribeToCreateCertificateFromCsrRejected( + csrSubscriptionRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onCsrRejected, onCsrRejectedSubAck); + + // Wait for the subscriptions to the accept and reject certificates topics to be established. + csrAcceptedSubAckCompletedPromise.get_future().wait(); + csrRejectedSubAckCompletedPromise.get_future().wait(); + + // Now, when we subscribed to the certificates topics, we can make a request for a certificate. + fprintf(stdout, "Publishing to CreateCertificateFromCsr topic\n"); + CreateCertificateFromCsrRequest createCertificateFromCsrRequest; + createCertificateFromCsrRequest.CertificateSigningRequest = csrFile; + identityClient.PublishCreateCertificateFromCsr( + createCertificateFromCsrRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onCsrPublishPubAck); + csrPublishPubAckCompletedPromise.get_future().wait(); + + // Wait for a certificate token. + csrAcceptedCompletedPromise.get_future().wait(); + + fprintf(stdout, "Subscribing to RegisterThing Accepted and Rejected topics\n"); + RegisterThingSubscriptionRequest registerSubscriptionRequest; + registerSubscriptionRequest.TemplateName = cmdData.input_templateName; + + identityClient.SubscribeToRegisterThingAccepted( + registerSubscriptionRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onRegisterAccepted, onRegisterAcceptedSubAck); + + identityClient.SubscribeToRegisterThingRejected( + registerSubscriptionRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onRegisterRejected, onRegisterRejectedSubAck); + + // Wait for the subscriptions to the accept and reject RegisterThing topics to be established. + registerAcceptedSubAckCompletedPromise.get_future().wait(); + registerRejectedSubAckCompletedPromise.get_future().wait(); + + fprintf(stdout, "Publishing to RegisterThing topic\n"); + RegisterThingRequest registerThingRequest; + registerThingRequest.TemplateName = cmdData.input_templateName; + + const Aws::Crt::String jsonValue = cmdData.input_templateParameters; + Aws::Crt::JsonObject value(jsonValue); + Map pm = value.View().GetAllObjects(); + Aws::Crt::Map params = Aws::Crt::Map(); + + for (const auto &x : pm) + { + params.emplace(x.first, x.second.AsString()); + } + + registerThingRequest.Parameters = params; + registerThingRequest.CertificateOwnershipToken = token; + + identityClient.PublishRegisterThing(registerThingRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onRegisterPublishPubAck); + + registerPublishPubAckCompletedPromise.get_future().wait(); + + // Wait for registering a thing to succeed. + registerAcceptedCompletedPromise.get_future().wait(); } // Disconnect