From 5718ba217b4e43bb2adbd0d26931b794937584bb Mon Sep 17 00:00:00 2001 From: Igor Abdrakhimov Date: Fri, 12 Jul 2024 09:40:15 -0700 Subject: [PATCH] Use promise for cert token --- .../fleet_provisioning/main.cpp | 20 +++++++------------ .../mqtt5_fleet_provisioning/main.cpp | 20 +++++++------------ 2 files changed, 14 insertions(+), 26 deletions(-) diff --git a/samples/fleet_provisioning/fleet_provisioning/main.cpp b/samples/fleet_provisioning/fleet_provisioning/main.cpp index f4f5fdfd5..105577ecf 100644 --- a/samples/fleet_provisioning/fleet_provisioning/main.cpp +++ b/samples/fleet_provisioning/fleet_provisioning/main.cpp @@ -59,8 +59,7 @@ struct CreateCertificateContext std::promise pubAckPromise; std::promise acceptedSubAckPromise; std::promise rejectedSubAckPromise; - std::promise tokenReceivedPromise; - String token; + std::promise tokenPromise; }; /** @@ -182,8 +181,7 @@ void createKeysAndCertificate(IotIdentityClient &identityClient, CreateCertifica if (ioErr == AWS_OP_SUCCESS) { fprintf(stdout, "CreateKeysAndCertificateResponse certificateId: %s.\n", response->CertificateId->c_str()); - ctx.token = *response->CertificateOwnershipToken; - ctx.tokenReceivedPromise.set_value(); + ctx.tokenPromise.set_value(*response->CertificateOwnershipToken); } else { @@ -227,9 +225,6 @@ void createKeysAndCertificate(IotIdentityClient &identityClient, CreateCertifica identityClient.PublishCreateKeysAndCertificate( createKeysAndCertificateRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onKeysPublishPubAck); ctx.pubAckPromise.get_future().wait(); - - // Wait for a certificate token. - ctx.tokenReceivedPromise.get_future().wait(); } /** @@ -272,8 +267,7 @@ void createCertificateFromCsr(IotIdentityClient &identityClient, CreateCertifica if (ioErr == AWS_OP_SUCCESS) { fprintf(stdout, "CreateCertificateFromCsrResponse certificateId: %s.\n", response->CertificateId->c_str()); - ctx.token = *response->CertificateOwnershipToken; - ctx.tokenReceivedPromise.set_value(); + ctx.tokenPromise.set_value(*response->CertificateOwnershipToken); } else { @@ -320,9 +314,6 @@ void createCertificateFromCsr(IotIdentityClient &identityClient, CreateCertifica identityClient.PublishCreateCertificateFromCsr( createCertificateFromCsrRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onCsrPublishPubAck); ctx.pubAckPromise.get_future().wait(); - - // Wait for a certificate token. - ctx.tokenReceivedPromise.get_future().wait(); } /** @@ -477,9 +468,12 @@ int main(int argc, char *argv[]) createKeysAndCertificate(identityClient, certificateContext); } + // Wait for a certificate token to be obtained. + auto token = certificateContext.tokenPromise.get_future().get(); + // After certificate is obtained, it's time to register a thing. RegisterThingContext registerThingContext; - registerThing(identityClient, registerThingContext, cmdData, certificateContext.token); + registerThing(identityClient, registerThingContext, cmdData, token); // Disconnect if (connection->Disconnect()) diff --git a/samples/fleet_provisioning/mqtt5_fleet_provisioning/main.cpp b/samples/fleet_provisioning/mqtt5_fleet_provisioning/main.cpp index c84b8b3b9..0670619c2 100644 --- a/samples/fleet_provisioning/mqtt5_fleet_provisioning/main.cpp +++ b/samples/fleet_provisioning/mqtt5_fleet_provisioning/main.cpp @@ -62,8 +62,7 @@ struct CreateCertificateContext std::promise pubAckPromise; std::promise acceptedSubAckPromise; std::promise rejectedSubAckPromise; - std::promise tokenReceivedPromise; - String token; + std::promise tokenPromise; }; /** @@ -174,8 +173,7 @@ void createKeysAndCertificate(IotIdentityClient &identityClient, CreateCertifica if (ioErr == AWS_OP_SUCCESS) { fprintf(stdout, "CreateKeysAndCertificateResponse certificateId: %s.\n", response->CertificateId->c_str()); - ctx.token = *response->CertificateOwnershipToken; - ctx.tokenReceivedPromise.set_value(); + ctx.tokenPromise.set_value(*response->CertificateOwnershipToken); } else { @@ -219,9 +217,6 @@ void createKeysAndCertificate(IotIdentityClient &identityClient, CreateCertifica identityClient.PublishCreateKeysAndCertificate( createKeysAndCertificateRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onKeysPublishPubAck); ctx.pubAckPromise.get_future().wait(); - - // Wait for a certificate token. - ctx.tokenReceivedPromise.get_future().wait(); } /** @@ -264,8 +259,7 @@ void createCertificateFromCsr(IotIdentityClient &identityClient, CreateCertifica if (ioErr == AWS_OP_SUCCESS) { fprintf(stdout, "CreateCertificateFromCsrResponse certificateId: %s.\n", response->CertificateId->c_str()); - ctx.token = *response->CertificateOwnershipToken; - ctx.tokenReceivedPromise.set_value(); + ctx.tokenPromise.set_value(*response->CertificateOwnershipToken); } else { @@ -312,9 +306,6 @@ void createCertificateFromCsr(IotIdentityClient &identityClient, CreateCertifica identityClient.PublishCreateCertificateFromCsr( createCertificateFromCsrRequest, AWS_MQTT_QOS_AT_LEAST_ONCE, onCsrPublishPubAck); ctx.pubAckPromise.get_future().wait(); - - // Wait for a certificate token. - ctx.tokenReceivedPromise.get_future().wait(); } /** @@ -469,9 +460,12 @@ int main(int argc, char *argv[]) createKeysAndCertificate(identityClient, certificateContext); } + // Wait for a certificate token to be obtained. + auto token = certificateContext.tokenPromise.get_future().get(); + // After certificate is obtained, it's time to register a thing. RegisterThingContext registerThingContext; - registerThing(identityClient, registerThingContext, cmdData, certificateContext.token); + registerThing(identityClient, registerThingContext, cmdData, token); // Disconnect if (client->Stop())