int HostNameBase::Get()

in src/modules/hostname/src/lib/HostNameBase.cpp [128:249]


int HostNameBase::Get(
    MMI_HANDLE clientSession,
    const char* componentName,
    const char* objectName,
    MMI_JSON_STRING* payload,
    int* payloadSizeBytes)
{
    if (!IsValidClientSession(clientSession))
    {
        OsConfigLogError(HostNameLog::Get(), ERROR_INVALID_SESSION, "Get", clientSession);
        return EINVAL;
    }

    if (!IsValidComponentName(componentName))
    {
        if (IsDebugLoggingEnabled())
        {
            OsConfigLogError(HostNameLog::Get(), ERROR_INVALID_COMPONENT, "Get", componentName, m_componentName);
        }
        return EINVAL;
    }

    if (!IsValidObjectName(objectName, false))
    {
        if (IsDebugLoggingEnabled())
        {
            OsConfigLogError(HostNameLog::Get(), ERROR_INVALID_OBJECT, "Get", objectName ? objectName : "-", m_propertyName, m_propertyHosts);
        }
        return EINVAL;
    }

    if (!payload || !payloadSizeBytes)
    {
        if (IsDebugLoggingEnabled())
        {
            OsConfigLogError(HostNameLog::Get(), ERROR_INVALID_PAYLOAD, "Get");
        }
        return EINVAL;
    }

    std::string data;
    if (std::strcmp(objectName, m_propertyName) == 0)
    {
        data = GetName();
    }
    else if (std::strcmp(objectName, m_propertyHosts) == 0)
    {
        data = GetHosts();
    }

    // Serialize data.
    rapidjson::Document document;
    document.SetString(data.c_str(), document.GetAllocator());
    rapidjson::StringBuffer buffer;
    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
    document.Accept(writer);

    // Validate payload size.
    int status = MMI_OK;
    *payloadSizeBytes = buffer.GetSize();
    if ((0 != m_maxPayloadSizeBytes) && (*payloadSizeBytes > static_cast<int>(m_maxPayloadSizeBytes)))
    {
        if (IsDebugLoggingEnabled())
        {
            OsConfigLogError(HostNameLog::Get(), ERROR_PAYLOAD_TOO_LARGE, "Get", *payloadSizeBytes, static_cast<int>(m_maxPayloadSizeBytes));
        }
        status = E2BIG;
    }
    else
    {
        // Allocate payload.
        *payload = new char[buffer.GetSize()];
        if (!payload)
        {
            if (IsDebugLoggingEnabled())
            {
                OsConfigLogError(HostNameLog::Get(), ERROR_MEMORY_ALLOCATION, "Get", static_cast<uint>(buffer.GetSize()));
            }
            status = ENOMEM;
        }
        else
        {
            // Copy data to payload.
            std::fill(*payload, *payload + *payloadSizeBytes, 0);
            std::memcpy(*payload, buffer.GetString(), *payloadSizeBytes);

            // Validate payload.
            if (!HostNameBase::IsValidJsonString(*payload, *payloadSizeBytes))
            {
                if (IsDebugLoggingEnabled())
                {
                    OsConfigLogError(HostNameLog::Get(), ERROR_INVALID_PAYLOAD, "Get");
                }
                status = EINVAL;
                delete *payload;
            }
        }
    }

    // If an error occurred, reset the status and return an empty payload.
    if (status != MMI_OK)
    {
        status = MMI_OK;
        std::size_t len = sizeof(g_emptyPayload) - 1;
        *payloadSizeBytes = len;
        *payload = new char[len];
        if (!payload)
        {
            // Unable to allocate an empty payload, cannot return MMI_OK at this point.
            if (IsDebugLoggingEnabled())
            {
                OsConfigLogError(HostNameLog::Get(), ERROR_MEMORY_ALLOCATION, "Get", static_cast<uint>(buffer.GetSize()));
            }
            status = ENOMEM;
        }
        else
        {
            std::memcpy(*payload, g_emptyPayload, len);
        }
    }
    return status;
}