modbus rtu 1.1 plugins
This commit is contained in:
107
plugins/protocols/modbus_rtu/parser/ModbusRtuProtocolPlugin.cpp
Normal file
107
plugins/protocols/modbus_rtu/parser/ModbusRtuProtocolPlugin.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
#include "plugins/protocols/modbus_rtu/parser/ModbusRtuProtocolPlugin.h"
|
||||
|
||||
#include "core/plugin_system/IProtocolPlugin.h"
|
||||
#include "message_bus/pipeline/PipelineContext.h"
|
||||
#include "message_bus/pipeline/protocol/ProtocolEnvelope.h"
|
||||
#include "plugins/protocols/modbus_rtu/codec/ModbusRtuCodec.h"
|
||||
#include "plugins/protocols/modbus_rtu/framer/ModbusRtuFraming.h"
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <variant>
|
||||
|
||||
namespace softbus::core::plugin_system
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
using softbus::message_bus::pipeline::protocol::ModbusExceptionPdu;
|
||||
using softbus::message_bus::pipeline::protocol::ModbusRtuPdu;
|
||||
using softbus::message_bus::pipeline::protocol::ModbusRtuUnsupportedPdu;
|
||||
using softbus::message_bus::pipeline::protocol::ProtocolFamily;
|
||||
using softbus::message_bus::pipeline::protocol::ReadCoilsRequest;
|
||||
using softbus::message_bus::pipeline::protocol::ReadCoilsResponse;
|
||||
using softbus::message_bus::pipeline::protocol::ReadDiscreteInputsRequest;
|
||||
using softbus::message_bus::pipeline::protocol::ReadDiscreteInputsResponse;
|
||||
using softbus::message_bus::pipeline::protocol::ReadHoldingRegistersRequest;
|
||||
using softbus::message_bus::pipeline::protocol::ReadHoldingRegistersResponse;
|
||||
using softbus::message_bus::pipeline::protocol::ReadInputRegistersRequest;
|
||||
using softbus::message_bus::pipeline::protocol::ReadInputRegistersResponse;
|
||||
using softbus::message_bus::pipeline::protocol::WriteSingleCoilRequest;
|
||||
using softbus::message_bus::pipeline::protocol::WriteSingleCoilResponse;
|
||||
using softbus::message_bus::pipeline::protocol::WriteSingleRegisterRequest;
|
||||
using softbus::message_bus::pipeline::protocol::WriteSingleRegisterResponse;
|
||||
using softbus::message_bus::pipeline::protocol::WriteMultipleCoilsRequest;
|
||||
using softbus::message_bus::pipeline::protocol::WriteMultipleCoilsResponse;
|
||||
using softbus::message_bus::pipeline::protocol::WriteMultipleRegistersRequest;
|
||||
using softbus::message_bus::pipeline::protocol::WriteMultipleRegistersResponse;
|
||||
using softbus::message_bus::pipeline::protocol::decodeModbusRtu;
|
||||
|
||||
template <class... Ts>
|
||||
struct overloaded : Ts...
|
||||
{
|
||||
using Ts::operator()...;
|
||||
};
|
||||
template <class... Ts>
|
||||
overloaded(Ts...) -> overloaded<Ts...>;
|
||||
|
||||
class ModbusRtuSession final : public IProtocolSession
|
||||
{
|
||||
public:
|
||||
bool parse(const softbus::message_bus::pipeline::PipelineContext& ctx,
|
||||
softbus::message_bus::pipeline::protocol::ProtocolEnvelope& out) override
|
||||
{
|
||||
using softbus::message_bus::pipeline::FrameKind;
|
||||
if (ctx.frameKind != FrameKind::CompleteFrame || !ctx.payload.valid()) {
|
||||
return false;
|
||||
}
|
||||
const std::size_t n = ctx.payloadSize ? ctx.payloadSize : ctx.payload.length;
|
||||
const std::uint8_t* p = ctx.payload.bytes();
|
||||
if (!p || n < 4) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ModbusRtuPdu pdu;
|
||||
if (!decodeModbusRtu(ctx.direction, p, n, pdu, nullptr)) {
|
||||
out.family = ProtocolFamily::None;
|
||||
out.pdu = {};
|
||||
return false;
|
||||
}
|
||||
out.family = ProtocolFamily::ModbusRtu;
|
||||
out.pdu = pdu;
|
||||
return true;
|
||||
}
|
||||
|
||||
void reset() override {}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
class ModbusRtuProtocolPlugin final : public IProtocolPlugin
|
||||
{
|
||||
public:
|
||||
QString pluginId() const override { return QStringLiteral("modbus_rtu"); }
|
||||
|
||||
bool supports(const QString& protocolHint) const override
|
||||
{
|
||||
return softbus::message_bus::pipeline::protocol::modbusrtu::isModbusRtuHint(protocolHint);
|
||||
}
|
||||
|
||||
std::unique_ptr<IProtocolSession> createSession() override
|
||||
{
|
||||
return std::make_unique<ModbusRtuSession>();
|
||||
}
|
||||
};
|
||||
|
||||
std::shared_ptr<IProtocolPlugin> makeModbusRtuProtocolPlugin()
|
||||
{
|
||||
return std::make_shared<ModbusRtuProtocolPlugin>();
|
||||
}
|
||||
|
||||
IProtocolPlugin* createModbusRtuProtocolPluginRaw()
|
||||
{
|
||||
return new ModbusRtuProtocolPlugin();
|
||||
}
|
||||
|
||||
} // namespace softbus::core::plugin_system
|
||||
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
// Modbus RTU 协议解析器插件
|
||||
#include <memory>
|
||||
|
||||
#include "core/plugin_system/IProtocolPlugin.h"
|
||||
|
||||
namespace softbus::core::plugin_system
|
||||
{
|
||||
|
||||
std::shared_ptr<IProtocolPlugin> makeModbusRtuProtocolPlugin();
|
||||
IProtocolPlugin* createModbusRtuProtocolPluginRaw();
|
||||
|
||||
} // namespace softbus::core::plugin_system
|
||||
Reference in New Issue
Block a user