int PmcBase::Set()

in src/modules/pmc/src/lib/PmcBase.cpp [128:225]


int PmcBase::Set(const char* componentName, const char* objectName, const MMI_JSON_STRING payload, const int payloadSizeBytes)
{
    if (!CanRunOnThisPlatform())
    {
        return ENODEV;
    }

    size_t payloadHash = HashString(payload);
    if (m_lastReachedStateHash == payloadHash)
    {
        if (IsDebugLoggingEnabled())
        {
            OsConfigLogInfo(PmcLog::Get(), "Ignoring update, desired state equals current state.");
        }
        return MMI_OK;
    }

    int status = PMC_0K;
    m_executionState.SetExecutionState(StateComponent::Running, SubstateComponent::DeserializingJsonPayload);

    int maxPayloadSizeBytes = static_cast<int>(GetMaxPayloadSizeBytes());
    if ((0 != maxPayloadSizeBytes) && (payloadSizeBytes > maxPayloadSizeBytes))
    {
        OsConfigLogError(PmcLog::Get(), "%s %s payload too large. Max payload expected %d, actual payload size %d", componentName, objectName, maxPayloadSizeBytes, payloadSizeBytes);
        m_executionState.SetExecutionState(StateComponent::Failed, SubstateComponent::DeserializingJsonPayload);
        return E2BIG;
    }

    rapidjson::Document document;

    if (document.Parse(payload, payloadSizeBytes).HasParseError())
    {
        OsConfigLogError(PmcLog::Get(), "Unabled to parse JSON payload: %s", payload);
        m_executionState.SetExecutionState(StateComponent::Failed, SubstateComponent::DeserializingJsonPayload);
        status = EINVAL;
    }
    else
    {
        if (0 == g_componentName.compare(componentName))
        {
            if (0 == g_desiredObjectName.compare(objectName))
            {
                if (document.IsObject())
                {
                    DesiredState desiredState;
                    m_executionState.SetExecutionState(StateComponent::Running, SubstateComponent::DeserializingDesiredState);

                    status = DeserializeDesiredState(document, desiredState);
                    if (m_executionState.IsSuccessful())
                    {
                        status = ValidateAndGetPackagesNames(desiredState.Packages);
                        if (m_executionState.IsSuccessful())
                        {
                            status = DownloadGpgKeys(desiredState.GpgKeys);
                            if (m_executionState.IsSuccessful())
                            {
                                status = ConfigureSources(desiredState.Sources, desiredState.GpgKeys);
                                if (m_executionState.IsSuccessful())
                                {
                                    status = ExecuteUpdates(desiredState.Packages);
                                }
                            }
                        }
                    }
                    else
                    {
                        OsConfigLogError(PmcLog::Get(), "Failed to deserialize %s", g_desiredObjectName.c_str());
                        m_executionState.SetExecutionState(StateComponent::Failed, SubstateComponent::DeserializingDesiredState);
                        status = EINVAL;
                    }
                }
                else
                {
                    OsConfigLogError(PmcLog::Get(), "JSON payload is not a %s object", g_desiredObjectName.c_str());
                    m_executionState.SetExecutionState(StateComponent::Failed, SubstateComponent::DeserializingDesiredState);
                    status = EINVAL;
                }
            }
            else
            {
                OsConfigLogError(PmcLog::Get(), "Invalid objectName: %s", objectName);
                m_executionState.SetExecutionState(StateComponent::Failed, SubstateComponent::DeserializingDesiredState);
                status = EINVAL;
            }
        }
        else
        {
            OsConfigLogError(PmcLog::Get(), "Invalid componentName: %s", componentName);
            m_executionState.SetExecutionState(StateComponent::Failed, SubstateComponent::DeserializingJsonPayload);
            status = EINVAL;
        }
    }

    // If anything goes wrong, reset the last reached state since the current state is undefined.
    m_lastReachedStateHash = m_executionState.IsSuccessful() ? payloadHash : 0;

    return m_executionState.IsSuccessful() ? MMI_OK : status;
}