endpointhash

This commit is contained in:
flower_linux
2026-04-15 20:05:04 +08:00
parent 94eec643de
commit 11bd86063d
21 changed files with 314 additions and 157 deletions

View File

@@ -17,10 +17,10 @@ class ModbusRtuFramerSession final : public softbus::core::plugin_system::IProto
public:
ModbusRtuFramerSession(std::shared_ptr<softbus::core::memory::MemoryPool> pool,
ModbusRtuFramerPluginConfig cfg,
QString endpoint)
std::uint32_t endpointHash)
: m_pool(std::move(pool))
, m_cfg(cfg)
, m_endpoint(std::move(endpoint))
, m_endpointHash(endpointHash)
{
m_buf.resize(m_cfg.maxAdu);
}
@@ -32,7 +32,7 @@ public:
return;
}
const std::uint8_t* p = in.payload.bytes();
const std::size_t n = in.payloadSize ? in.payloadSize : in.payload.length;
const std::size_t n = in.payload.length;
if (!p || n == 0) {
return;
}
@@ -47,7 +47,8 @@ public:
m_lastActivity = m_lastRx;
if (m_len + n > m_cfg.maxAdu) {
LOG_WARNING() << "ModbusRtuFramerPlugin: overflow endpoint=" << m_endpoint;
LOG_WARNING() << "ModbusRtuFramerPlugin: overflow endpointHash=0x"
<< QString::number(m_endpointHash, 16);
m_len = 0;
return;
}
@@ -78,16 +79,22 @@ public:
softbus::core::models::RawBusMessage framed;
framed.deviceId = in.deviceId;
framed.stableKey = in.stableKey;
framed.endpoint = in.endpoint;
framed.timestamp = in.timestamp;
framed.protocolHint = in.protocolHint;
framed.endpointHash = in.endpointHash;
framed.protocol = softbus::core::models::ProtocolType::MODBUS_RTU;
framed.logicalAddress = in.logicalAddress;
framed.routingKey = in.routingKey;
framed.header = in.header;
framed.timestampMs = in.timestampMs;
framed.traceId = in.traceId;
framed.direction = in.direction;
framed.payload.block = std::move(blk);
framed.payload.offset = 0;
framed.payload.length = *pred;
framed.payloadSize = *pred;
if (*pred >= 2 && framed.payload.block && framed.payload.block->data) {
framed.header.modbus.slaveId = framed.payload.block->data[0];
framed.header.modbus.functionCode = framed.payload.block->data[1];
framed.logicalAddress = framed.header.modbus.slaveId;
}
out.push_back(std::move(framed));
const std::size_t rest = m_len - *pred;
@@ -98,7 +105,6 @@ public:
}
in.payload = {};
in.payloadSize = 0;
}
void reset() override { m_len = 0; }
@@ -107,7 +113,7 @@ public:
private:
std::shared_ptr<softbus::core::memory::MemoryPool> m_pool;
ModbusRtuFramerPluginConfig m_cfg;
QString m_endpoint;
std::uint32_t m_endpointHash{0};
std::vector<std::uint8_t> m_buf;
std::size_t m_len{0};
std::chrono::steady_clock::time_point m_lastRx{std::chrono::steady_clock::now()};
@@ -128,9 +134,9 @@ QString ModbusRtuFramerPlugin::pluginId() const
return QStringLiteral("modbus_rtu_framer");
}
bool ModbusRtuFramerPlugin::supports(const QString& protocolHint) const
softbus::core::models::ProtocolType ModbusRtuFramerPlugin::protocolType() const
{
return isModbusRtuHint(protocolHint);
return softbus::core::models::ProtocolType::MODBUS_RTU;
}
void ModbusRtuFramerPlugin::bindMemoryPool(std::shared_ptr<softbus::core::memory::MemoryPool> pool)
@@ -139,7 +145,7 @@ void ModbusRtuFramerPlugin::bindMemoryPool(std::shared_ptr<softbus::core::memory
}
std::unique_ptr<softbus::core::plugin_system::IProtocolFramerSession>
ModbusRtuFramerPlugin::createFramerSession(const QString& endpoint, const QJsonObject& params)
ModbusRtuFramerPlugin::createFramerSession(std::uint32_t endpointHash, const QJsonObject& params)
{
if (!m_pool) {
return {};
@@ -152,7 +158,7 @@ ModbusRtuFramerPlugin::createFramerSession(const QString& endpoint, const QJsonO
if (params.contains(QStringLiteral("modbusRtuInterFrameTimeoutMs"))) {
cfg.interFrameTimeoutMs = params.value(QStringLiteral("modbusRtuInterFrameTimeoutMs")).toInt(cfg.interFrameTimeoutMs);
}
return std::make_unique<ModbusRtuFramerSession>(m_pool, cfg, endpoint);
return std::make_unique<ModbusRtuFramerSession>(m_pool, cfg, endpointHash);
}
std::shared_ptr<softbus::core::plugin_system::IProtocolFramerPlugin> makeModbusRtuFramerPlugin(

View File

@@ -27,10 +27,10 @@ public:
ModbusRtuFramerPluginConfig cfg = {});
QString pluginId() const override;
bool supports(const QString& protocolHint) const override;
softbus::core::models::ProtocolType protocolType() const override;
void bindMemoryPool(std::shared_ptr<softbus::core::memory::MemoryPool> pool) override;
std::unique_ptr<softbus::core::plugin_system::IProtocolFramerSession> createFramerSession(
const QString& endpoint, const QJsonObject& params) override;
std::uint32_t endpointHash, const QJsonObject& params) override;
private:
std::shared_ptr<softbus::core::memory::MemoryPool> m_pool;

View File

@@ -30,13 +30,14 @@ public:
for (int i = 0; i < resp->registers.size(); ++i) {
softbus::core::models::DOMMessage msg;
msg.messageId = QStringLiteral("%1.reg.%2")
.arg(rawContext.stableKey.isEmpty() ? QString::number(rawContext.deviceId)
: rawContext.stableKey)
.arg(rawContext.routingKey == 0
? QString::number(rawContext.deviceId)
: QString::number(rawContext.routingKey))
.arg(i);
msg.timestamp = rawContext.timestamp.toMSecsSinceEpoch();
msg.timestamp = rawContext.timestampMs;
msg.value = static_cast<std::uint32_t>(resp->registers[static_cast<qsizetype>(i)]);
msg.quality = softbus::core::models::DataQuality::GOOD;
msg.attributes.insert(QStringLiteral("sourceEndpoint"), rawContext.endpoint);
msg.attributes.insert(QStringLiteral("sourceEndpoint"), QString::number(rawContext.endpointHash));
out.push_back(std::move(msg));
}
return out;
@@ -46,12 +47,13 @@ public:
std::get_if<softbus::message_bus::pipeline::protocol::ReadHoldingRegistersRequest>(pdu)) {
softbus::core::models::DOMMessage msg;
msg.messageId = QStringLiteral("%1.req.read_holding")
.arg(rawContext.stableKey.isEmpty() ? QString::number(rawContext.deviceId)
: rawContext.stableKey);
msg.timestamp = rawContext.timestamp.toMSecsSinceEpoch();
.arg(rawContext.routingKey == 0
? QString::number(rawContext.deviceId)
: QString::number(rawContext.routingKey));
msg.timestamp = rawContext.timestampMs;
msg.value = static_cast<std::uint32_t>(req->quantity);
msg.quality = softbus::core::models::DataQuality::GOOD;
msg.attributes.insert(QStringLiteral("sourceEndpoint"), rawContext.endpoint);
msg.attributes.insert(QStringLiteral("sourceEndpoint"), QString::number(rawContext.endpointHash));
msg.attributes.insert(QStringLiteral("frameType"), QStringLiteral("request"));
msg.attributes.insert(QStringLiteral("startAddress"), static_cast<int>(req->startAddress));
msg.attributes.insert(QStringLiteral("quantity"), static_cast<int>(req->quantity));
@@ -67,9 +69,9 @@ class ModbusRtuProtocolMapperPlugin final : public softbus::core::plugin_system:
{
public:
QString pluginId() const override { return QStringLiteral("modbus_rtu_mapper"); }
bool supports(const QString& protocolHint) const override
softbus::core::models::ProtocolType protocolType() const override
{
return isModbusRtuHint(protocolHint);
return softbus::core::models::ProtocolType::MODBUS_RTU;
}
std::unique_ptr<softbus::core::plugin_system::IProtocolMapperSession> createMapperSession() override
{

View File

@@ -55,7 +55,7 @@ public:
if (!ctx.payload.valid()) {
return false;
}
const std::size_t n = ctx.payloadSize ? ctx.payloadSize : ctx.payload.length;
const std::size_t n = ctx.payload.length;
const std::uint8_t* p = ctx.payload.bytes();
if (!p || n < 4) {
return false;
@@ -86,9 +86,9 @@ class ModbusRtuProtocolPlugin final : public IProtocolPlugin
public:
QString pluginId() const override { return QStringLiteral("modbus_rtu"); }
bool supports(const QString& protocolHint) const override
softbus::core::models::ProtocolType protocolType() const override
{
return softbus::message_bus::pipeline::protocol::modbusrtu::isModbusRtuHint(protocolHint);
return softbus::core::models::ProtocolType::MODBUS_RTU;
}
std::unique_ptr<IProtocolSession> createSession() override