AzureSphere/AzureSphereSampleDevice/main.c
static void SetUpAzureIoTHubClient(void)
{
    bool isClientSetupSuccessful = false;

    if (iothubClientHandle != NULL) {
        IoTHubDeviceClient_LL_Destroy(iothubClientHandle);
    }

    if ((connectionType == ConnectionType_Direct) || (connectionType == ConnectionType_IoTEdge)) {
        isClientSetupSuccessful = SetUpAzureIoTHubClientWithDaa();
    } else if (connectionType == ConnectionType_DPS) {
        isClientSetupSuccessful = SetUpAzureIoTHubClientWithDps();
    }

    if (!isClientSetupSuccessful) {
        // If we fail to connect, reduce the polling frequency, starting at
        // AzureIoTMinReconnectPeriodSeconds and with a backoff up to
        // AzureIoTMaxReconnectPeriodSeconds
        if (azureIoTPollPeriodSeconds == AzureIoTDefaultPollPeriodSeconds) {
            azureIoTPollPeriodSeconds = AzureIoTMinReconnectPeriodSeconds;
        } else {
            azureIoTPollPeriodSeconds *= 2;
            if (azureIoTPollPeriodSeconds > AzureIoTMaxReconnectPeriodSeconds) {
                azureIoTPollPeriodSeconds = AzureIoTMaxReconnectPeriodSeconds;
            }
        }

        struct timespec azureTelemetryPeriod = {azureIoTPollPeriodSeconds, 0};
        SetEventLoopTimerPeriod(azureTimer, &azureTelemetryPeriod);

        Log_Debug("ERROR: Failed to create IoTHub Handle - will retry in %i seconds.\n",
                  azureIoTPollPeriodSeconds);
        return;
    }

    // Successfully connected, so make sure the polling frequency is back to the default
    azureIoTPollPeriodSeconds = AzureIoTDefaultPollPeriodSeconds;
    struct timespec azureTelemetryPeriod = {.tv_sec = azureIoTPollPeriodSeconds, .tv_nsec = 0};
    SetEventLoopTimerPeriod(azureTimer, &azureTelemetryPeriod);

    // Set client authentication state to initiated. This is done to indicate that
    // SetUpAzureIoTHubClient() has been called (and so should not be called again) while the
    // client is waiting for a response via the ConnectionStatusCallback().
    iotHubClientAuthenticationState = IoTHubClientAuthenticationState_AuthenticationInitiated;

    IoTHubDeviceClient_LL_SetDeviceTwinCallback(iothubClientHandle, DeviceTwinCallback, NULL);
    IoTHubDeviceClient_LL_SetDeviceMethodCallback(iothubClientHandle, DeviceMethodCallback, NULL);
    IoTHubDeviceClient_LL_SetConnectionStatusCallback(iothubClientHandle, ConnectionStatusCallback,
                                                      NULL);
}
/// <summary>
///     Sets up the Azure IoT Hub connection (creates the iothubClientHandle)
///     When the SAS Token for a device expires the connection needs to be recreated
///     which is why this is not simply a one time call.
/// </summary>

Sets up the Azure IoT Hub connection (creates the iothubClientHandle)
When the SAS Token for a device expires the connection needs to be recreated
which is why this is not simply a one time call.
Sets up the Azure IoT Hub connection ( creates the iothubClientHandle ) When the SAS Token for a device expires the connection needs to be recreated which is why this is not simply a one time call .
static void SetUpAzureIoTHubClient(void)
{
    bool isClientSetupSuccessful = false;
    if (iothubClientHandle != NULL) {
        IoTHubDeviceClient_LL_Destroy(iothubClientHandle);
    }
    if ((connectionType == ConnectionType_Direct) || (connectionType == ConnectionType_IoTEdge)) {
        isClientSetupSuccessful = SetUpAzureIoTHubClientWithDaa();
    } else if (connectionType == ConnectionType_DPS) {
        isClientSetupSuccessful = SetUpAzureIoTHubClientWithDps();
    }
    if (!isClientSetupSuccessful) {
        if (azureIoTPollPeriodSeconds == AzureIoTDefaultPollPeriodSeconds) {
            azureIoTPollPeriodSeconds = AzureIoTMinReconnectPeriodSeconds;
        } else {
            azureIoTPollPeriodSeconds *= 2;
            if (azureIoTPollPeriodSeconds > AzureIoTMaxReconnectPeriodSeconds) {
                azureIoTPollPeriodSeconds = AzureIoTMaxReconnectPeriodSeconds;
            }
        }
        struct timespec azureTelemetryPeriod = {azureIoTPollPeriodSeconds, 0};
        SetEventLoopTimerPeriod(azureTimer, &azureTelemetryPeriod);
        Log_Debug("ERROR: Failed to create IoTHub Handle - will retry in %i seconds.\n",
                  azureIoTPollPeriodSeconds);
        return;
    }
    azureIoTPollPeriodSeconds = AzureIoTDefaultPollPeriodSeconds;
    struct timespec azureTelemetryPeriod = {.tv_sec = azureIoTPollPeriodSeconds, .tv_nsec = 0};
    SetEventLoopTimerPeriod(azureTimer, &azureTelemetryPeriod);
    iotHubClientAuthenticationState = IoTHubClientAuthenticationState_AuthenticationInitiated;
    IoTHubDeviceClient_LL_SetDeviceTwinCallback(iothubClientHandle, DeviceTwinCallback, NULL);
    IoTHubDeviceClient_LL_SetDeviceMethodCallback(iothubClientHandle, DeviceMethodCallback, NULL);
    IoTHubDeviceClient_LL_SetConnectionStatusCallback(iothubClientHandle, ConnectionStatusCallback,
                                                      NULL);
}
static void SetUpAzureIoTHubClient ( void ) { bool isClientSetupSuccessful = false ; if ( iothubClientHandle != NULL ) { IoTHubDeviceClient_LL_Destroy ( iothubClientHandle ) ; } if ( ( connectionType == ConnectionType_Direct ) || ( connectionType == ConnectionType_IoTEdge ) ) { isClientSetupSuccessful = SetUpAzureIoTHubClientWithDaa ( ) ; } else if ( connectionType == ConnectionType_DPS ) { isClientSetupSuccessful = SetUpAzureIoTHubClientWithDps ( ) ; } if ( ! isClientSetupSuccessful ) { if ( azureIoTPollPeriodSeconds == AzureIoTDefaultPollPeriodSeconds ) { azureIoTPollPeriodSeconds = AzureIoTMinReconnectPeriodSeconds ; } else { azureIoTPollPeriodSeconds *= 2 ; if ( azureIoTPollPeriodSeconds > AzureIoTMaxReconnectPeriodSeconds ) { azureIoTPollPeriodSeconds = AzureIoTMaxReconnectPeriodSeconds ; } } struct timespec azureTelemetryPeriod = { azureIoTPollPeriodSeconds , 0 } ; SetEventLoopTimerPeriod ( azureTimer , & azureTelemetryPeriod ) ; Log_Debug ( " \n " , azureIoTPollPeriodSeconds ) ; return ; } azureIoTPollPeriodSeconds = AzureIoTDefaultPollPeriodSeconds ; struct timespec azureTelemetryPeriod = { . tv_sec = azureIoTPollPeriodSeconds , . tv_nsec = 0 } ; SetEventLoopTimerPeriod ( azureTimer , & azureTelemetryPeriod ) ; iotHubClientAuthenticationState = IoTHubClientAuthenticationState_AuthenticationInitiated ; IoTHubDeviceClient_LL_SetDeviceTwinCallback ( iothubClientHandle , DeviceTwinCallback , NULL ) ; IoTHubDeviceClient_LL_SetDeviceMethodCallback ( iothubClientHandle , DeviceMethodCallback , NULL ) ; IoTHubDeviceClient_LL_SetConnectionStatusCallback ( iothubClientHandle , ConnectionStatusCallback , NULL ) ; }
<summary>
Sets up the Azure IoT Hub connection (creates the iothubClientHandle)
When the SAS Token for a device expires the connection needs to be recreated
which is why this is not simply a one time call.
<summary > Sets up the Azure IoT Hub connection ( creates the iothubClientHandle ) When the SAS Token for a device expires the connection needs to be recreated which is why this is not simply a one time call .
// If we fail to connect, reduce the polling frequency, starting at // AzureIoTMinReconnectPeriodSeconds and with a backoff up to // AzureIoTMaxReconnectPeriodSeconds // Successfully connected, so make sure the polling frequency is back to the default // Set client authentication state to initiated. This is done to indicate that // SetUpAzureIoTHubClient() has been called (and so should not be called again) while the // client is waiting for a response via the ConnectionStatusCallback().