Search in sources :

Example 1 with ProvisioningDeviceClientStatus

use of com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClientStatus in project azure-iot-sdk-java by Azure.

the class ProvisioningTPMTests method provisioningTpmFlow.

@Test
public void provisioningTpmFlow() throws SecurityProviderException, ProvisioningServiceClientException, ProvisioningDeviceClientException, InterruptedException {
    ProvisioningServiceClient provisioningServiceClient = ProvisioningServiceClient.createFromConnectionString(provisioningServiceConnectionString);
    String registrationId = UUID.randomUUID().toString();
    String provisionedDeviceId = "Some-Provisioned-Device-" + TPM + "-" + UUID.randomUUID().toString();
    SecurityProvider securityProvider = new SecurityProviderTPMEmulator(registrationId);
    Attestation attestation = new TpmAttestation(new String(encodeBase64(((SecurityProviderTpm) securityProvider).getEndorsementKey())));
    IndividualEnrollment individualEnrollment = new IndividualEnrollment(registrationId, attestation);
    individualEnrollment.setDeviceIdFinal(provisionedDeviceId);
    provisioningServiceClient.createOrUpdateIndividualEnrollment(individualEnrollment);
    ProvisioningDeviceClient provisioningDeviceClient = ProvisioningDeviceClient.create(provisioningServiceGlobalEndpoint, provisioningServiceIdScope, ProvisioningDeviceClientTransportProtocol.AMQPS, securityProvider);
    AtomicBoolean registrationCompleted = new AtomicBoolean(false);
    AtomicBoolean registrationCompletedSuccessfully = new AtomicBoolean(false);
    provisioningDeviceClient.registerDevice((provisioningDeviceClientRegistrationResult, e, context) -> {
        log.debug("Provisioning registration callback fired with result {}", provisioningDeviceClientRegistrationResult.getProvisioningDeviceClientStatus());
        if (e != null) {
            log.error("Provisioning registration callback fired with exception {}", e);
        }
        ProvisioningDeviceClientStatus status = provisioningDeviceClientRegistrationResult.getProvisioningDeviceClientStatus();
        if (status == ProvisioningDeviceClientStatus.PROVISIONING_DEVICE_STATUS_ASSIGNED) {
            registrationCompletedSuccessfully.set(true);
        }
        registrationCompleted.set(true);
    }, null);
    long startTime = System.currentTimeMillis();
    while (!registrationCompleted.get()) {
        Thread.sleep(200);
        if (System.currentTimeMillis() - startTime > REGISTRATION_TIMEOUT_MILLISECONDS) {
            fail("Timed out waiting for device registration to complete.");
        }
    }
    assertTrue("Registration completed, but not successfully", registrationCompletedSuccessfully.get());
    provisioningDeviceClient.closeNow();
}
Also used : IndividualEnrollment(com.microsoft.azure.sdk.iot.provisioning.service.configs.IndividualEnrollment) ProvisioningDeviceClient(com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClient) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SecurityProviderTPMEmulator(com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator) ProvisioningDeviceClientStatus(com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClientStatus) TpmAttestation(com.microsoft.azure.sdk.iot.provisioning.service.configs.TpmAttestation) SecurityProvider(com.microsoft.azure.sdk.iot.provisioning.security.SecurityProvider) TpmAttestation(com.microsoft.azure.sdk.iot.provisioning.service.configs.TpmAttestation) Attestation(com.microsoft.azure.sdk.iot.provisioning.service.configs.Attestation) ProvisioningServiceClient(com.microsoft.azure.sdk.iot.provisioning.service.ProvisioningServiceClient) Test(org.junit.Test)

Aggregations

ProvisioningDeviceClient (com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClient)1 ProvisioningDeviceClientStatus (com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClientStatus)1 SecurityProvider (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProvider)1 SecurityProviderTPMEmulator (com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator)1 ProvisioningServiceClient (com.microsoft.azure.sdk.iot.provisioning.service.ProvisioningServiceClient)1 Attestation (com.microsoft.azure.sdk.iot.provisioning.service.configs.Attestation)1 IndividualEnrollment (com.microsoft.azure.sdk.iot.provisioning.service.configs.IndividualEnrollment)1 TpmAttestation (com.microsoft.azure.sdk.iot.provisioning.service.configs.TpmAttestation)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Test (org.junit.Test)1