int DeliveryOptimizationMmiSet()

in src/modules/deliveryoptimization/src/lib/DeliveryOptimization.c [299:425]


int DeliveryOptimizationMmiSet(MMI_HANDLE clientSession, const char* componentName, const char* objectName, const MMI_JSON_STRING payload, const int payloadSizeBytes)
{
    int status = MMI_OK;
    char *buffer = NULL;
    JSON_Value* rootValue = NULL;
    JSON_Object* rootObject = NULL;
    JSON_Value* newValue = NULL;
    JSON_Object* newObject = NULL;
    unsigned int count = 0;
    const char* name = NULL;
    JSON_Value *currentValue = NULL;
    const char* cacheHost = NULL;
    int cacheHostSource = 0;
    int cacheHostFallback = 0;
    int percentageDownloadThrottle = 0;

    if ((NULL == componentName) || (NULL == objectName) || (NULL == payload) || (payloadSizeBytes <= 0))
    {
        OsConfigLogError(DeliveryOptimizationGetLog(), "MmiSet(%s, %s, %p, %d) called with invalid arguments", componentName, objectName, payload, payloadSizeBytes);
        status = EINVAL;
    }
    else if (!IsValidSession(clientSession))
    {
        OsConfigLogError(DeliveryOptimizationGetLog(), "MmiSet(%s, %s) called outside of a valid session", componentName, objectName);
        status = EINVAL;
    }
    else if (0 != strcmp(componentName, g_deliveryOptimizationComponentName))
    {
        OsConfigLogError(DeliveryOptimizationGetLog(), "MmiSet called for an unsupported component name '%s'", componentName);
        status = EINVAL;
    }
    else if (0 != strcmp(objectName, g_desiredDeliveryOptimizationPoliciesObjectName))
    {
        OsConfigLogError(DeliveryOptimizationGetLog(), "MmiSet called for an unsupported object name '%s'", objectName);
        status = EINVAL;
    }
    else
    {
        // Copy payload to local buffer with null terminator for json_parse_string.
        buffer = malloc(payloadSizeBytes + 1);
        if (NULL != buffer)
        {
            memset(buffer, 0, payloadSizeBytes + 1);
            memcpy(buffer, payload, payloadSizeBytes);

            rootValue = json_parse_string(buffer);
            if (json_value_get_type(rootValue) == JSONObject)
            {
                rootObject = json_value_get_object(rootValue);

                // Create new JSON_Value with validated output.
                newValue = json_value_init_object();
                newObject = json_value_get_object(newValue);

                count = json_object_get_count(rootObject);
                for (unsigned int i = 0; i < count; i++)
                {
                    name = json_object_get_name(rootObject, i);
                    currentValue = json_object_get_value(rootObject, name);

                    if ((0 == strcmp(name, g_desiredCacheHostSettingName)) && (JSONString == json_value_get_type(currentValue)))
                    {
                        cacheHost = json_value_get_string(currentValue);
                        json_object_set_string(newObject, g_cacheHostConfigName, cacheHost);
                    }
                    else if ((0 == strcmp(name, g_desiredCacheHostSourceSettingName)) && (JSONNumber == json_value_get_type(currentValue)))
                    {
                        cacheHostSource = (int)json_value_get_number(currentValue);
                        if ((cacheHostSource >= 0) && (cacheHostSource <= 3))
                        {
                            json_object_set_number(newObject, g_cacheHostSourceConfigName, cacheHostSource);
                        }
                        else
                        {
                            OsConfigLogError(DeliveryOptimizationGetLog(), "MmiSet called with invalid cacheHostSource %d", cacheHostSource);
                            status = EINVAL;
                        }
                    }
                    else if ((0 == strcmp(name, g_desiredCacheHostFallbackSettingName)) && (JSONNumber == json_value_get_type(currentValue)))
                    {
                        cacheHostFallback = (int)json_value_get_number(currentValue);
                        json_object_set_number(newObject, g_cacheHostFallbackConfigName, cacheHostFallback);
                    }
                    else if ((0 == strcmp(name, g_desiredPercentageDownloadThrottleSettingName)) && (JSONNumber == json_value_get_type(currentValue)))
                    {
                        percentageDownloadThrottle = (int)json_value_get_number(currentValue);
                        if ((percentageDownloadThrottle >= 0) && (percentageDownloadThrottle <= 100))
                        {
                            json_object_set_number(newObject, g_percentageDownloadThrottleConfigName, percentageDownloadThrottle);
                        }
                        else
                        {
                            OsConfigLogError(DeliveryOptimizationGetLog(), "MmiSet called with invalid percentageDownloadThrottle %d", percentageDownloadThrottle);
                            status = EINVAL;
                        }
                    }
                }

                if (JSONSuccess != json_serialize_to_file_pretty(newValue, g_deliveryOptimizationConfigFile))
                {
                    OsConfigLogError(DeliveryOptimizationGetLog(), "MmiSet failed to write JSON file '%s'", g_deliveryOptimizationConfigFile);
                    status = EIO;
                }

                json_value_free(newValue);
            }
            else
            {
                OsConfigLogError(DeliveryOptimizationGetLog(), "MmiSet failed to parse JSON '%.*s'", payloadSizeBytes, payload);
                status = EINVAL;
            }

            json_value_free(rootValue);
        }
        else
        {
            OsConfigLogError(DeliveryOptimizationGetLog(), "MmiSet failed to allocate %d bytes", payloadSizeBytes + 1);
            status = ENOMEM;
        }
    }

    OsConfigLogInfo(DeliveryOptimizationGetLog(), "MmiSet(%p, %s, %s, %.*s, %d) returning %d", clientSession, componentName, objectName, payloadSizeBytes, payload, payloadSizeBytes, status);

    FREE_MEMORY(buffer);

    return status;
}