static int UpdateConfigurationFile()

in src/modules/configuration/src/lib/Configuration.c [134:300]


static int UpdateConfigurationFile(void)
{
    const char* debugLoggingEnabledName = "DebugLogging";
    const char* localManagementEnabledName = "LocalManagement";
    const char* modelVersionName = "ModelVersion";
    const char* iotHubtManagementEnabledName = "IotHubManagement";
    const char* iotHubProtocolName = "IotHubProtocol";
    const char* refreshIntervalName = "ReportingIntervalSeconds";
    const char* gitManagementEnabledName = "GitManagement";
    const char* gitBranchName = "GitBranch";

    int status = MMI_OK;

    JSON_Value* jsonValue = NULL;
    JSON_Object* jsonObject = NULL;

    int modelVersion = g_modelVersion;
    int refreshInterval = g_refreshInterval;
    bool localManagementEnabled = g_localManagementEnabled;
    bool debugLoggingEnabled = g_debugLoggingEnabled;
    bool iotHubManagementEnabled = g_iotHubManagementEnabled;
    int iotHubProtocol = g_iotHubProtocol;
    bool gitManagementEnabled = g_gitManagementEnabled;
    char* gitBranch = DuplicateString(g_gitBranch);

    char* existingConfiguration = LoadConfigurationFromFile(g_configurationFile);
    char* newConfiguration = NULL;

    if (!existingConfiguration)
    {
        OsConfigLogError(ConfigurationGetLog(), "No configuration file, cannot update configuration");
        return ENOENT;
    }

    if ((modelVersion != g_modelVersion) || (refreshInterval != g_refreshInterval) || (localManagementEnabled != g_localManagementEnabled) ||
        (debugLoggingEnabled != g_debugLoggingEnabled) || (iotHubManagementEnabled != g_iotHubManagementEnabled) || (iotHubProtocol != g_iotHubProtocol) ||
        (gitManagementEnabled != g_gitManagementEnabled) || strcmp(gitBranch, g_gitBranch))
    {
        if (NULL == (jsonValue = json_parse_string(existingConfiguration)))
        {
            OsConfigLogError(ConfigurationGetLog(), "json_parse_string(%s) failed, UpdateConfigurationFile failed", existingConfiguration);
            status = EINVAL;
        }
        else if (NULL == (jsonObject = json_value_get_object(jsonValue)))
        {
            OsConfigLogError(ConfigurationGetLog(), "json_value_get_object(%s) failed, UpdateConfigurationFile failed", existingConfiguration);
            status = EINVAL;
        }

        if (MMI_OK == status)
        {
            if (JSONSuccess == json_object_set_number(jsonObject, modelVersionName, (double)modelVersion))
            {
                g_modelVersion = modelVersion;
            }
            else
            {
                OsConfigLogError(ConfigurationGetLog(), "json_object_set_number(%s, %d) failed", g_modelVersionObject, modelVersion);
            }

            if (JSONSuccess == json_object_set_number(jsonObject, refreshIntervalName, (double)refreshInterval))
            {
                g_refreshInterval = refreshInterval;
            }
            else
            {
                OsConfigLogError(ConfigurationGetLog(), "json_object_set_number(%s, %d) failed", g_refreshIntervalObject, refreshInterval);
            }

            if (JSONSuccess == json_object_set_number(jsonObject, localManagementEnabledName, (double)(localManagementEnabled ? 1 : 0)))
            {
                g_localManagementEnabled = localManagementEnabled;
            }
            else
            {
                OsConfigLogError(ConfigurationGetLog(), "json_object_set_boolean(%s, %s) failed", g_localManagementEnabledObject, localManagementEnabled ? "true" : "false");
            }

            if (JSONSuccess == json_object_set_number(jsonObject, debugLoggingEnabledName, (double)(debugLoggingEnabled ? 1: 0)))
            {
                g_debugLoggingEnabled = debugLoggingEnabled;
            }
            else
            {
                OsConfigLogError(ConfigurationGetLog(), "json_object_set_boolean(%s, %s) failed", g_debugLoggingEnabledObject, debugLoggingEnabled ? "true" : "false");
            }

            if (JSONSuccess == json_object_set_number(jsonObject, iotHubtManagementEnabledName, (double)(iotHubManagementEnabled ? 1 : 0)))
            {
                g_iotHubManagementEnabled = iotHubManagementEnabled;
            }
            else
            {
                OsConfigLogError(ConfigurationGetLog(), "json_object_set_boolean(%s, %s) failed", g_iotHubManagementEnabledObject, iotHubManagementEnabled ? "true" : "false");
            }

            if (JSONSuccess == json_object_set_number(jsonObject, iotHubProtocolName, (double)iotHubProtocol))
            {
                g_iotHubProtocol = iotHubProtocol;
            }
            else
            {
                OsConfigLogError(ConfigurationGetLog(), "json_object_set_number(%s, %d) failed", g_iotHubProtocolObject, iotHubProtocol);
            }

            if (JSONSuccess == json_object_set_number(jsonObject, gitManagementEnabledName, (double)(gitManagementEnabled ? 1 : 0)))
            {
                g_gitManagementEnabled = gitManagementEnabled;
            }
            else
            {
                OsConfigLogError(ConfigurationGetLog(), "json_object_set_boolean(%s, %s) failed", g_gitManagementEnabledObject, gitManagementEnabled ? "true" : "false");
            }

            if (JSONSuccess == json_object_set_string(jsonObject, gitBranchName, gitBranch))
            {
                FREE_MEMORY(g_gitBranch);
                g_gitBranch = DuplicateString(gitBranch);
            }
            else
            {
                OsConfigLogError(ConfigurationGetLog(), "json_object_set_string(%s, %s) failed", g_gitBranchObject, gitBranch);
            }
        }

        if (MMI_OK == status)
        {
            if (NULL != (newConfiguration = json_serialize_to_string_pretty(jsonValue)))
            {
                if (false == SavePayloadToFile(g_configurationFile, newConfiguration, strlen(newConfiguration), ConfigurationGetLog()))
                {
                    OsConfigLogError(ConfigurationGetLog(), "Failed saving configuration to %s", g_osConfigConfigurationFile);
                    status = ENOENT;
                }
            }
            else
            {
                OsConfigLogError(ConfigurationGetLog(), "json_serialize_to_string_pretty failed");
                status = EIO;
            }
        }
    }

    if (MMI_OK == status)
    {
        OsConfigLogInfo(ConfigurationGetLog(), "New configuration successfully applied: %s", IsDebugLoggingEnabled() ? newConfiguration : "-");
    }
    else
    {
        OsConfigLogError(ConfigurationGetLog(), "Failed to apply new configuration: %s", IsDebugLoggingEnabled() ? newConfiguration : "-");
    }

    if (jsonValue)
    {
        json_value_free(jsonValue);
    }

    if (newConfiguration)
    {
        json_free_serialized_string(newConfiguration);
    }

    FREE_MEMORY(gitBranch);
    FREE_MEMORY(existingConfiguration);

    return status;
}