static bool ADUC_DeviceClient_Create()

in src/communication_managers/iothub_communication_manager/src/iothub_communication_manager.c [306:434]


static bool ADUC_DeviceClient_Create(
    ADUC_ClientHandle* outClientHandle, ADUC_ConnectionInfo* connInfo, const bool iotHubTracingEnabled)
{
    IOTHUB_CLIENT_RESULT iothubResult;
    HTTP_PROXY_OPTIONS proxyOptions;
    memset(&proxyOptions, 0, sizeof(proxyOptions));
    bool result = true;
    bool shouldSetProxyOptions = InitializeProxyOptions(&proxyOptions);

    Log_Info("Attempting to create connection to IotHub using type: %s ", ADUC_ConnType_ToString(connInfo->connType));

    IOTHUB_CLIENT_TRANSPORT_PROVIDER transportProvider = GetIotHubProtocolFromConfig();
    if (transportProvider == NULL)
    {
        result = false;
    }
    // Create a connection to IoTHub.
    else if (!ClientHandle_CreateFromConnectionString(
                 outClientHandle, connInfo->connType, connInfo->connectionString, transportProvider))
    {
        Log_Error("Failure creating IotHub device client using MQTT protocol. Check your connection string.");
        result = false;
    }
    // Sets IoTHub tracing verbosity level.
    else if (
        (iothubResult = ClientHandle_SetOption(*outClientHandle, OPTION_LOG_TRACE, &iotHubTracingEnabled))
        != IOTHUB_CLIENT_OK)
    {
        Log_Error("Unable to set IoTHub tracing option, error=%d", iothubResult);
        result = false;
    }
    else if (
        connInfo->certificateString != NULL && connInfo->authType == ADUC_AuthType_SASCert
        && (iothubResult = ClientHandle_SetOption(*outClientHandle, SU_OPTION_X509_CERT, connInfo->certificateString))
            != IOTHUB_CLIENT_OK)
    {
        Log_Error("Unable to set IotHub certificate, error=%d", iothubResult);
        result = false;
    }
    else if (
        shouldSetProxyOptions
        && ((iothubResult = ClientHandle_SetOption(*outClientHandle, OPTION_HTTP_PROXY, &proxyOptions))
            != IOTHUB_CLIENT_OK))
    {
        Log_Error("Could not set http proxy options, error=%d ", iothubResult);
        result = false;
    }
    else if (
        connInfo->certificateString != NULL && connInfo->authType == ADUC_AuthType_NestedEdgeCert
        && (iothubResult = ClientHandle_SetOption(*outClientHandle, OPTION_TRUSTED_CERT, connInfo->certificateString))
            != IOTHUB_CLIENT_OK)
    {
        Log_Error("Could not add trusted certificate, error=%d ", iothubResult);
        result = false;
    }
    else if (
        connInfo->opensslEngine != NULL && connInfo->authType == ADUC_AuthType_SASCert
        && (iothubResult = ClientHandle_SetOption(*outClientHandle, OPTION_OPENSSL_ENGINE, connInfo->opensslEngine))
            != IOTHUB_CLIENT_OK)
    {
        Log_Error("Unable to set IotHub OpenSSL Engine, error=%d", iothubResult);
        result = false;
    }
    else if (
        connInfo->opensslPrivateKey != NULL && connInfo->authType == ADUC_AuthType_SASCert
        && (iothubResult =
                ClientHandle_SetOption(*outClientHandle, SU_OPTION_X509_PRIVATE_KEY, connInfo->opensslPrivateKey))
            != IOTHUB_CLIENT_OK)
    {
        Log_Error("Unable to set IotHub OpenSSL Private Key, error=%d", iothubResult);
        result = false;
    }
    else if (
        connInfo->opensslEngine != NULL && connInfo->opensslPrivateKey != NULL
        && connInfo->authType == ADUC_AuthType_SASCert
        && (iothubResult =
                ClientHandle_SetOption(*outClientHandle, OPTION_OPENSSL_PRIVATE_KEY_TYPE, &x509_key_from_engine))
            != IOTHUB_CLIENT_OK)
    {
        Log_Error("Unable to set IotHub OpenSSL Private Key Type, error=%d", iothubResult);
        result = false;
    }
    // Sets the name of ModelId for this PnP device.
    // This *MUST* be set before the client is connected to IoTHub.  We do not automatically connect when the
    // handle is created, but will implicitly connect to subscribe for device method and device twin callbacks below.
    else if (
        (iothubResult = ClientHandle_SetOption(*outClientHandle, OPTION_MODEL_ID, g_aduModelId)) != IOTHUB_CLIENT_OK)
    {
        Log_Error("Unable to set the Device Twin Model ID, error=%d", iothubResult);
        result = false;
    }
    // Sets the callback function that processes device twin changes from the IoTHub, which is the channel
    // that PnP Properties are transferred over.
    // This will also automatically retrieve the full twin for the application.
    else if (
        (iothubResult =
             ClientHandle_SetClientTwinCallback(*outClientHandle, g_device_twin_callback, g_property_update_context))
        != IOTHUB_CLIENT_OK)
    {
        Log_Error("Unable to set device twin callback, error=%d", iothubResult);
        result = false;
    }
    else if (
        (iothubResult = ClientHandle_SetConnectionStatusCallback(
             *outClientHandle, IoTHub_CommunicationManager_ConnectionStatus_Callback, NULL))
        != IOTHUB_CLIENT_OK)
    {
        Log_Error("Unable to set connection status callback, error=%d", iothubResult);
        result = false;
    }
    else
    {
        Log_Info("IoTHub Device Twin callback registered.");
        result = true;
    }

    if ((result == false) && (*outClientHandle != NULL))
    {
        ClientHandle_Destroy(*outClientHandle);
        *outClientHandle = NULL;
    }

    if (shouldSetProxyOptions)
    {
        UninitializeProxyOptions(&proxyOptions);
    }

    return result;
}