bool StartupAgent()

in src/agent/src/main.c [695:808]


bool StartupAgent(const ADUC_LaunchArguments* launchArgs)
{
    bool succeeded = false;

    ADUC_ConnectionInfo info;
    memset(&info, 0, sizeof(info));

    if (!ADUC_D2C_Messaging_Init())
    {
        goto done;
    }

    if (launchArgs->connectionString != NULL)
    {
        ADUC_ConnType connType = GetConnTypeFromConnectionString(launchArgs->connectionString);

        if (connType == ADUC_ConnType_NotSet)
        {
            Log_Error("Connection string is invalid");
            goto done;
        }

        ADUC_ConnectionInfo connInfo = {
            ADUC_AuthType_NotSet, connType, launchArgs->connectionString, NULL, NULL, NULL
        };

        if (!ADUC_SetDiagnosticsDeviceNameFromConnectionString(connInfo.connectionString))
        {
            Log_Error("Setting DiagnosticsDeviceName failed");
            goto done;
        }

        if (!IoTHub_CommunicationManager_Init(
                &g_iotHubClientHandle,
                ADUC_PnPDeviceTwin_Callback,
                ADUC_PnP_Components_HandleRefresh,
                &g_iotHubInitiatedPnPPropertyChangeContext))
        {
            Log_Error("IoTHub_CommunicationManager_Init failed");
            goto done;
        }
    }
    else
    {
        if (!GetAgentConfigInfo(&info))
        {
            goto done;
        }

        if (!ADUC_SetDiagnosticsDeviceNameFromConnectionString(info.connectionString))
        {
            Log_Error("Setting DiagnosticsDeviceName failed");
            goto done;
        }

        if (!IoTHub_CommunicationManager_Init(
                &g_iotHubClientHandle,
                ADUC_PnPDeviceTwin_Callback,
                ADUC_PnP_Components_HandleRefresh,
                &g_iotHubInitiatedPnPPropertyChangeContext))
        {
            Log_Error("IoTHub_CommunicationManager_Init failed");
            goto done;
        }
    }

    if (!ADUC_PnP_Components_Create(g_iotHubClientHandle, launchArgs->argc, launchArgs->argv))
    {
        Log_Error("ADUC_PnP_Components_Create failed");
        goto done;
    }

    ADUC_Result result;

    // The connection string is valid (IoT hub connection successful) and we are ready for further processing.
    // Send connection string to DO SDK for it to discover the Edge gateway if present.
    if (ConnectionStringUtils_IsNestedEdge(info.connectionString))
    {
        result = ExtensionManager_InitializeContentDownloader(info.connectionString);
    }
    else
    {
        result = ExtensionManager_InitializeContentDownloader(NULL /*initializeData*/);
    }

#ifdef ADUC_COMMAND_HELPER_H
    if (InitializeCommandListenerThread())
    {
        RegisterCommand(&redoUpdateCommand);
    }
    else
    {
        Log_Error(
            "Cannot initialize the command listener thread. Running another instance of DU Agent with --command will not work correctly.");
        // Note: even though we can't create command listener here, we need to ensure that
        // the agent stay alive and connected to the IoT hub.
    }
#endif // #ifdef ADUC_COMMAND_HELPER_H

    if (IsAducResultCodeFailure(result.ResultCode))
    {
        // Since it is nested edge and if DO fails to accept the connection string, then we go ahead and
        // fail the startup.
        Log_Error("Failed to set DO connection string in Nested Edge scenario, result: 0x%08x", result.ResultCode);
        goto done;
    }

    succeeded = true;

done:

    ADUC_ConnectionInfo_DeAlloc(&info);
    return succeeded;
}