int MpiGetReported()

in src/platform/ModulesManager.c [718:841]


int MpiGetReported(MPI_HANDLE handle, MPI_JSON_STRING* payload, int* payloadSizeBytes)
{
    int status = MPI_OK;
    const char* uuid = (const char*)handle;
    SESSION* session = NULL;
    MODULE_SESSION* moduleSession = NULL;
    JSON_Value* rootValue = NULL;
    JSON_Object* rootObject = NULL;
    JSON_Value* componentValue = NULL;
    JSON_Object* componentObject = NULL;
    JSON_Value* objectValue = NULL;
    MMI_JSON_STRING mmiPayload = NULL;
    int mmiPayloadSizeBytes = 0;
    int mmiStatus = MMI_OK;
    char* payloadJson = NULL;
    int i = 0;

    if ((NULL == handle) || (NULL == payload) || (NULL == payloadSizeBytes))
    {
        OsConfigLogError(GetPlatformLog(), "MpiGetReported(%p, %p, %p) called with invalid arguments", handle, payload, payloadSizeBytes);
        status = EINVAL;
    }
    else if (NULL == (session = FindSession(uuid)))
    {
        OsConfigLogError(GetPlatformLog(), "MpiGetReported: no session exists with UUID '%s'", uuid);
        status = EINVAL;
    }
    else if (NULL == (rootValue = json_value_init_object()))
    {
        OsConfigLogError(GetPlatformLog(), "MpiGetReported: failed to initialize json object");
        status = ENOMEM;
    }
    else if (NULL == (rootObject = json_value_get_object(rootValue)))
    {
        OsConfigLogError(GetPlatformLog(), "MpiGetReported: failed to get root json object from value");
        status = ENOMEM;
    }
    else
    {
        for (i = 0; i < g_reportedTotal; i++)
        {
            if (NULL == (moduleSession = FindModuleSession(session->modules, g_reported[i].component)))
            {
                OsConfigLogError(GetPlatformLog(), "MpiGetReported: no module exists with component '%s'", g_reported[i].component);
            }
            else if (NULL == moduleSession->module)
            {
                OsConfigLogError(GetPlatformLog(), "MpiGetReported: no module is loaded for session '%s'", uuid);
            }
            else
            {
                mmiStatus = moduleSession->module->get(moduleSession->handle, g_reported[i].component, g_reported[i].object, &mmiPayload, &mmiPayloadSizeBytes);

                OsConfigLogDebug(GetPlatformLog(), "MmiGet(%s, %s) returned %d (%.*s)", g_reported[i].component, g_reported[i].object, mmiStatus, mmiPayloadSizeBytes, mmiPayload);

                if (MMI_OK != mmiStatus)
                {
                    OsConfigLogError(GetPlatformLog(), "MmiGet(%s, %s), returned %d", g_reported[i].component, g_reported[i].object, mmiStatus);
                }
                else if (NULL == (payloadJson = (char*)malloc(mmiPayloadSizeBytes + 1)))
                {
                    OsConfigLogError(GetPlatformLog(), "MpiGetReported: failed to allocate memory for JSON");
                }
                else
                {
                    memcpy(payloadJson, mmiPayload, mmiPayloadSizeBytes);
                    payloadJson[mmiPayloadSizeBytes] = '\0';

                    if (NULL == (objectValue = json_parse_string(payloadJson)))
                    {
                        if (IsDebugLoggingEnabled())
                        {
                            OsConfigLogError(GetPlatformLog(), "MmiGet(%s, %s) returned an invalid payload '%s'", g_reported[i].component, g_reported[i].object, payloadJson);
                        }
                        else
                        {
                            OsConfigLogError(GetPlatformLog(), "MmiGet(%s, %s) returned an invalid payload", g_reported[i].component, g_reported[i].object);
                        }
                    }
                    else
                    {
                        if (NULL == (componentValue = json_object_get_value(rootObject, g_reported[i].component)))
                        {
                            componentValue = json_value_init_object();
                            json_object_set_value(rootObject, g_reported[i].component, componentValue);
                        }

                        if (NULL == (componentObject = json_value_get_object(componentValue)))
                        {
                            OsConfigLogError(GetPlatformLog(), "MpiGetReported: failed to get JSON object for component '%s'", g_reported[i].component);
                        }
                        else
                        {
                            json_object_set_value(componentObject, g_reported[i].object, objectValue);
                        }
                    }

                    FREE_MEMORY(payloadJson);
                }

                moduleSession->module->free(mmiPayload);
                mmiPayload = NULL;
            }
        }

        *payload = json_serialize_to_string_pretty(rootValue);
        *payloadSizeBytes = (int)strlen(*payload);
        json_value_free(rootValue);
    }

    if (IsDebugLoggingEnabled())
    {
        if (MMI_OK == status)
        {
            OsConfigLogDebug(GetPlatformLog(), "MpiGetDesired(%p, %p) succeeded", handle, payload);
        }
        else
        {
            OsConfigLogError(GetPlatformLog(), "MpiGetDesired(%p, %p) failed with %d", handle, payload, status);
        }
    }

    return status;
}