int DeliveryOptimizationMmiGet()

in src/modules/deliveryoptimization/src/lib/DeliveryOptimization.c [132:297]


int DeliveryOptimizationMmiGet(MMI_HANDLE clientSession, const char* componentName, const char* objectName, MMI_JSON_STRING* payload, int* payloadSizeBytes)
{
    int status = MMI_OK;
    const char* jsonPropertyName = NULL;
    int jsonPropertyType = JSONNull;
    JSON_Value *rootValue = NULL;
    JSON_Object *rootObject = NULL;
    JSON_Value *value = NULL;
    char *json = NULL;
    const char* emptyJsonPayload = NULL;

    if ((NULL == componentName) || (NULL == objectName) || (NULL == payload) || (NULL == payloadSizeBytes))
    {
        OsConfigLogError(DeliveryOptimizationGetLog(), "MmiGet(%s, %s, %p, %p) called with invalid arguments", componentName, objectName, payload, payloadSizeBytes);
        status = EINVAL;
        return status;
    }

    *payload = NULL;
    *payloadSizeBytes = 0;

    if (!IsValidSession(clientSession))
    {
        OsConfigLogError(DeliveryOptimizationGetLog(), "MmiGet(%s, %s) called outside of a valid session", componentName, objectName);
        status = EINVAL;
    }
    else if (0 != strcmp(componentName, g_deliveryOptimizationComponentName))
    {
        OsConfigLogError(DeliveryOptimizationGetLog(), "MmiGet called for an unsupported component name '%s'", componentName);
        status = EINVAL;
    }
    else
    {
        if (0 == strcmp(objectName, g_reportedCacheHostObjectName))
        {
            jsonPropertyName = g_cacheHostConfigName;
            jsonPropertyType = JSONString;
        }
        else if (0 == strcmp(objectName, g_reportedCacheHostSourceObjectName))
        {
            jsonPropertyName = g_cacheHostSourceConfigName;
            jsonPropertyType = JSONNumber;
        }
        else if (0 == strcmp(objectName, g_reportedCacheHostFallbackObjectName))
        {
            jsonPropertyName = g_cacheHostFallbackConfigName;
            jsonPropertyType = JSONNumber;
        }
        else if (0 == strcmp(objectName, g_reportedPercentageDownloadThrottleObjectName))
        {
            jsonPropertyName = g_percentageDownloadThrottleConfigName;
            jsonPropertyType = JSONNumber;
        }
        else
        {
            OsConfigLogError(DeliveryOptimizationGetLog(), "MmiGet called for an unsupported object name '%s'", objectName);
            status = EINVAL;
        }
    }

    if ((MMI_OK == status) && (NULL != jsonPropertyName) && (JSONNull != jsonPropertyType))
    {
        rootValue = json_parse_file(g_deliveryOptimizationConfigFile);

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

            if (json_object_has_value_of_type(rootObject, jsonPropertyName, jsonPropertyType))
            {
                value = json_object_get_value(rootObject, jsonPropertyName);
                json = json_serialize_to_string(value);

                if (NULL != json)
                {
                    *payloadSizeBytes = strlen(json);
                    if ((g_maxPayloadSizeBytes > 0) && ((unsigned)*payloadSizeBytes > g_maxPayloadSizeBytes))
                    {
                        OsConfigLogError(DeliveryOptimizationGetLog(), "MmiGet(%s, %s) insufficient maxmimum size (%d bytes) versus data size (%d bytes), reported value will be truncated", componentName, objectName, g_maxPayloadSizeBytes, *payloadSizeBytes);
                        *payloadSizeBytes = g_maxPayloadSizeBytes;
                    }

                    *payload = (MMI_JSON_STRING)malloc(*payloadSizeBytes);
                    if (NULL != *payload)
                    {
                        memset(*payload, 0, *payloadSizeBytes);
                        memcpy(*payload, json, *payloadSizeBytes);
                    }
                    else
                    {
                        OsConfigLogError(DeliveryOptimizationGetLog(), "MmiGet failed to allocate %d bytes", *payloadSizeBytes + 1);
                        *payloadSizeBytes = 0;
                        status = ENOMEM;
                    }

                    json_free_serialized_string(json);
                }
                else
                {
                    if (IsDebugLoggingEnabled())
                    {
                        OsConfigLogError(DeliveryOptimizationGetLog(), "MmiGet failed to serialize JSON property '%s'", jsonPropertyName);
                    }
                    status = EINVAL;
                }
            }
            else
            {
                if (IsDebugLoggingEnabled())
                {
                    OsConfigLogError(DeliveryOptimizationGetLog(), "MmiGet failed to find JSON property '%s'", jsonPropertyName);
                }
                status = EINVAL;
            }
        }
        else
        {
            if (IsDebugLoggingEnabled())
            {
                OsConfigLogError(DeliveryOptimizationGetLog(), "MmiGet failed to parse JSON file '%s'", g_deliveryOptimizationConfigFile);
            }
            status = EINVAL;
        }

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

        // Reset status and payload if JSON file could not be parsed or property not found, as it may yet have to be configured.
        if (MMI_OK != status)
        {
            if (JSONNumber == jsonPropertyType)
            {
                emptyJsonPayload = "0";
            }
            else if (JSONString == jsonPropertyType)
            {
                emptyJsonPayload = "\"\"";
            }

            *payloadSizeBytes = strlen(emptyJsonPayload);
            *payload = (MMI_JSON_STRING)malloc(*payloadSizeBytes);

            if (NULL != payload)
            {
                memset(*payload, 0, *payloadSizeBytes);
                strncpy(*payload, emptyJsonPayload, *payloadSizeBytes);
                status = MMI_OK;
            }
            else
            {
                OsConfigLogError(DeliveryOptimizationGetLog(), "MmiGet failed to allocate %d bytes", *payloadSizeBytes + 1);
                *payloadSizeBytes = 0;
                status = ENOMEM;
            }
        }
    }

    if (IsDebugLoggingEnabled())
    {
        OsConfigLogInfo(DeliveryOptimizationGetLog(), "MmiGet(%p, %s, %s, %.*s, %d) returning %d", clientSession, componentName, objectName, *payloadSizeBytes, *payload, *payloadSizeBytes, status);
    }

    return status;
}