#include "message_bus/egress/EgressRouter.h"

#include "devices/physical/SerialDevice.h"

namespace softbus::message_bus::egress
{

void EgressRouter::bindSerialDevice(
    const QString& endpoint,
    const QSharedPointer<softbus::devices::physical::SerialDevice>& device)
{
    m_serialByEndpoint.insert(endpoint, device);
}

void EgressRouter::unbindSerialDevice(const QString& endpoint)
{
    m_serialByEndpoint.remove(endpoint);
}

bool EgressRouter::send(const softbus::message_bus::pipeline::PipelineContext& ctx)
{
    auto it = m_serialByEndpoint.constFind(ctx.endpoint);
    if (it == m_serialByEndpoint.constEnd() || !it.value() || !ctx.payload.valid()) {
        return false;
    }

    const auto* p = ctx.payload.bytes();
    if (!p) {
        return false;
    }
    return it.value()->writePayload(p, ctx.payload.length);
}

} // namespace softbus::message_bus::egress
