Skip to content

Commit

Permalink
Use promise for cert token
Browse files Browse the repository at this point in the history
  • Loading branch information
sfodagain committed Jul 12, 2024
1 parent a84bcdc commit 3aee062
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 26 deletions.
20 changes: 7 additions & 13 deletions samples/fleet_provisioning/fleet_provisioning/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ struct CreateCertificateContext
std::promise<void> pubAckPromise;
std::promise<void> acceptedSubAckPromise;
std::promise<void> rejectedSubAckPromise;
std::promise<void> tokenReceivedPromise;
String token;
std::promise<String> tokenPromise;
};

/**
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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())
Expand Down
20 changes: 7 additions & 13 deletions samples/fleet_provisioning/mqtt5_fleet_provisioning/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ struct CreateCertificateContext
std::promise<void> pubAckPromise;
std::promise<void> acceptedSubAckPromise;
std::promise<void> rejectedSubAckPromise;
std::promise<void> tokenReceivedPromise;
String token;
std::promise<String> tokenPromise;
};

/**
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 3aee062

Please sign in to comment.