int ConfigurationMmiSet()

in src/modules/configuration/src/lib/Configuration.c [495:640]


int ConfigurationMmiSet(MMI_HANDLE clientSession, const char* componentName, const char* objectName, const MMI_JSON_STRING payload, const int payloadSizeBytes)
{
    JSON_Value* jsonValue = NULL;
    const char* jsonString = NULL;
    char* payloadString = NULL;

    int status = MMI_OK;

    if ((NULL == componentName) || (NULL == objectName) || (NULL == payload) || (0 >= payloadSizeBytes))
    {
        OsConfigLogError(ConfigurationGetLog(), "MmiSet(%s, %s, %s, %d) called with invalid arguments", componentName, objectName, payload, payloadSizeBytes);
        status = EINVAL;
        return status;
    }

    if (!IsValidSession(clientSession))
    {
        OsConfigLogError(ConfigurationGetLog(), "MmiSet(%s, %s) called outside of a valid session", componentName, objectName);
        status = EINVAL;
    }
    else if (0 != strcmp(componentName, g_configurationComponentName))
    {
        OsConfigLogError(ConfigurationGetLog(), "MmiSet called for an unsupported component name (%s)", componentName);
        status = EINVAL;
    }

    if (MMI_OK == status)
    {
        if (NULL != (payloadString = malloc(payloadSizeBytes + 1)))
        {
            memset(payloadString, 0, payloadSizeBytes + 1);
            memcpy(payloadString, payload, payloadSizeBytes);

            if (NULL != (jsonValue = json_parse_string(payloadString)))
            {
                if (0 == strcmp(objectName, g_desiredRefreshIntervalObject))
                {
                    g_refreshInterval = (unsigned int)json_value_get_number(jsonValue);
                }
                else if (0 == strcmp(objectName, g_desiredLocalManagementEnabledObject))
                {
                    g_localManagementEnabled = (0 == json_value_get_boolean(jsonValue)) ? false : true;
                }
                else if (0 == strcmp(objectName, g_desiredDebugLoggingEnabledObject))
                {
                    g_debugLoggingEnabled = (0 == json_value_get_boolean(jsonValue)) ? false : true;
                }
                else if (0 == strcmp(objectName, g_desiredIotHubManagementEnabledObject))
                {
                    g_iotHubManagementEnabled = (0 == json_value_get_boolean(jsonValue)) ? false : true;
                }
                else if (0 == strcmp(objectName, g_desiredIotHubProtocolObject))
                {
                    if (NULL != (jsonString = (char*)json_value_get_string(jsonValue)))
                    {
                        if (0 == strcmp(g_auto, jsonString))
                        {
                            g_iotHubProtocol = 0;
                        }
                        else if (0 == strcmp(g_mqtt, jsonString))
                        {
                            g_iotHubProtocol = 1;
                        }
                        else if (0 == strcmp(g_mqttWebSocket, jsonString))
                        {
                            g_iotHubProtocol = 2;
                        }
                        else
                        {
                            OsConfigLogError(ConfigurationGetLog(), "MmiSet(%s, %s): unsupported value: '%s'", componentName, objectName, payloadString);
                            status = EINVAL;
                        }
                    }
                    else
                    {
                        OsConfigLogError(ConfigurationGetLog(), "MmiSet(%s, %s): json_value_get_string('%s') failed", componentName, objectName, payloadString);
                        status = EINVAL;
                    }
                }
                else if (0 == strcmp(objectName, g_desiredGitManagementEnabledObject))
                {
                    g_gitManagementEnabled = (0 == json_value_get_boolean(jsonValue)) ? false : true;
                }
                else if (0 == strcmp(objectName, g_desiredGitBranchObject))
                {
                    if (NULL != (jsonString = json_value_get_string(jsonValue)))
                    {
                        if (jsonString)
                        {
                            FREE_MEMORY(g_gitBranch);
                            g_gitBranch = DuplicateString(jsonString);
                        }
                        else
                        {
                            OsConfigLogError(ConfigurationGetLog(), "Bad string value for %s (json_value_get_string failed)", g_desiredGitBranchObject);
                            status = EINVAL;
                        }
                    }
                    else
                    {
                        OsConfigLogError(ConfigurationGetLog(), "MmiSet(%s, %s): json_value_get_string('%s') failed", componentName, objectName, payloadString);
                        status = EINVAL;
                    }
                }
                else
                {
                    OsConfigLogError(ConfigurationGetLog(), "MmiSet called for an unsupported object name: %s", objectName);
                    status = EINVAL;
                }
            }
            else
            {
                status = EINVAL;
                OsConfigLogError(ConfigurationGetLog(), "MmiSet(%s, %s): json_parse_string(%s) failed", componentName, objectName, payloadString);
            }
        }
        else
        {
            OsConfigLogError(ConfigurationGetLog(), "Failed to allocate %d bytes of memory, MmiSet failed", payloadSizeBytes + 1);
            status = ENOMEM;
        }
    }

    if (MMI_OK == status)
    {
        status = UpdateConfigurationFile();
    }

    if (NULL != jsonValue)
    {
        json_value_free(jsonValue);
    }

    FREE_MEMORY(payloadString);

    if (IsDebugLoggingEnabled())
    {
        OsConfigLogDebug(ConfigurationGetLog(), "MmiSet(%p, %s, %s, %.*s, %d) returning %d", clientSession, componentName, objectName, payloadSizeBytes, payload, payloadSizeBytes, status);
    }
    else
    {
        OsConfigLogInfo(ConfigurationGetLog(), "MmiSet(%p, %s, %s) returning %d", clientSession, componentName, objectName, status);
    }

    return status;
}