log0.4
This commit is contained in:
66
src/devices/DeviceTypes.cpp
Normal file
66
src/devices/DeviceTypes.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
#include "devices/DeviceTypes.h"
|
||||
|
||||
namespace softbus::devices {
|
||||
|
||||
DeviceKind kindFromType(const QString &typeStr) {
|
||||
// 使用静态 QHash,仅在首次调用时初始化一次,后续调用直接极速查表
|
||||
static const QHash<QString, DeviceKind> typeMap = {
|
||||
{QStringLiteral("serial"), DeviceKind::Serial},
|
||||
{QStringLiteral("can"), DeviceKind::Can},
|
||||
{QStringLiteral("ethernet"), DeviceKind::Network},
|
||||
{QStringLiteral("network"), DeviceKind::Network},
|
||||
{QStringLiteral("virtual"), DeviceKind::Virtual},
|
||||
{QStringLiteral("distributed"), DeviceKind::Distributed}};
|
||||
|
||||
// value() 方法在找不到键时,会返回传入的第二个默认参数
|
||||
return typeMap.value(typeStr.trimmed().toLower(), DeviceKind::Unknown);
|
||||
}
|
||||
|
||||
DeviceKind inferDeviceKindFromEndpoint(const QString &endpoint) {
|
||||
const QString ep = endpoint.trimmed();
|
||||
// 使用结构体数组来存储设备类型和前缀的映射
|
||||
struct Rule {
|
||||
const QString prefix;
|
||||
DeviceKind kind;
|
||||
};
|
||||
|
||||
static const Rule rules[] = {
|
||||
{QStringLiteral("/dev/tty"), DeviceKind::Serial},
|
||||
{QStringLiteral("can"), DeviceKind::Can},
|
||||
{QStringLiteral("tcp://"), DeviceKind::Tcp},
|
||||
{QStringLiteral("udp://"), DeviceKind::Udp}
|
||||
// 未来在这里添加其他设备类型
|
||||
};
|
||||
|
||||
// 遍历规则表进行前缀匹配
|
||||
for (const auto &rule : rules) {
|
||||
if (ep.startsWith(rule.prefix)) {
|
||||
return rule.kind;
|
||||
}
|
||||
}
|
||||
|
||||
return DeviceKind::Unknown;
|
||||
}
|
||||
|
||||
QString driverKeyFor(DeviceKind kind) {
|
||||
switch (kind) {
|
||||
case DeviceKind::Serial:
|
||||
return QStringLiteral("serial_qt");
|
||||
case DeviceKind::Can:
|
||||
return QStringLiteral("can_socketcan");
|
||||
case DeviceKind::Tcp:
|
||||
return QStringLiteral("tcp_qt");
|
||||
case DeviceKind::Udp:
|
||||
return QStringLiteral("udp_qt");
|
||||
case DeviceKind::Network:
|
||||
return QStringLiteral("network_qt");
|
||||
case DeviceKind::Virtual:
|
||||
return QStringLiteral("virtual_device");
|
||||
case DeviceKind::Distributed:
|
||||
return QStringLiteral("distributed_node");
|
||||
default:
|
||||
return QStringLiteral("unknown");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace softbus::devices
|
||||
78
src/devices/DeviceTypes.h
Normal file
78
src/devices/DeviceTypes.h
Normal file
@@ -0,0 +1,78 @@
|
||||
#pragma once
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QString>
|
||||
|
||||
namespace softbus::devices
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
// 具体设备种类(用于选择 driver / 解析 endpoint)
|
||||
enum class DeviceKind {
|
||||
Unknown,
|
||||
Serial,
|
||||
Can,
|
||||
Tcp,
|
||||
Udp,
|
||||
Network, // 泛指 network(无法区分 tcp/udp 时)
|
||||
Virtual,
|
||||
Distributed,
|
||||
};
|
||||
|
||||
struct DeviceInfo
|
||||
{
|
||||
int id{0};
|
||||
QString endpoint;
|
||||
QString name;
|
||||
QString type; // "serial" / "can" / "network" / ...
|
||||
DeviceKind kind{DeviceKind::Unknown};
|
||||
int address{0};
|
||||
QString protocol;
|
||||
QString protocolDetail;
|
||||
QJsonObject properties;
|
||||
QString status{QStringLiteral("online")};
|
||||
bool isActive{true};
|
||||
QDateTime lastSeen;
|
||||
|
||||
QString toJson() const
|
||||
{
|
||||
QJsonObject obj;
|
||||
obj[QStringLiteral("id")] = id;
|
||||
obj[QStringLiteral("endpoint")] = endpoint;
|
||||
obj[QStringLiteral("name")] = name;
|
||||
obj[QStringLiteral("type")] = type;
|
||||
obj[QStringLiteral("kind")] = static_cast<int>(kind);
|
||||
obj[QStringLiteral("address")] = address;
|
||||
obj[QStringLiteral("protocol")] = protocol;
|
||||
obj[QStringLiteral("protocol_detail")] = protocolDetail;
|
||||
obj[QStringLiteral("properties")] = properties;
|
||||
obj[QStringLiteral("status")] = status;
|
||||
obj[QStringLiteral("isActive")] = isActive;
|
||||
if (lastSeen.isValid()) {
|
||||
obj[QStringLiteral("lastSeen")] = lastSeen.toString(Qt::ISODate);
|
||||
}
|
||||
const QJsonDocument doc(obj);
|
||||
return QString::fromUtf8(doc.toJson(QJsonDocument::Compact));
|
||||
}
|
||||
};
|
||||
|
||||
struct BusMessage
|
||||
{
|
||||
QString id;
|
||||
QString source;
|
||||
QString destination;
|
||||
QJsonObject payload;
|
||||
qint64 timestamp{0};
|
||||
};
|
||||
|
||||
// 纯 helper:不做 IO,仅做字符串/endpoint 到 DeviceKind/driverKey 的映射
|
||||
DeviceKind kindFromType(const QString& typeStr);
|
||||
DeviceKind inferDeviceKindFromEndpoint(const QString& endpoint);
|
||||
QString driverKeyFor(DeviceKind kind);
|
||||
|
||||
} // namespace softbus::devices
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/device/DeviceTypes.h"
|
||||
#include "devices/DeviceTypes.h"
|
||||
|
||||
namespace softbus::devices
|
||||
{
|
||||
@@ -10,7 +10,7 @@ class IDevice
|
||||
public:
|
||||
virtual ~IDevice() = default;
|
||||
|
||||
virtual softbus::core::device::DeviceInfo deviceInfo() const = 0;
|
||||
virtual softbus::devices::DeviceInfo deviceInfo() const = 0;
|
||||
};
|
||||
|
||||
} // namespace softbus::devices
|
||||
|
||||
19
src/devices/base/IPhysicalDevice.h
Normal file
19
src/devices/base/IPhysicalDevice.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "devices/DeviceTypes.h"
|
||||
#include "devices/base/IDevice.h"
|
||||
|
||||
namespace softbus::devices::base
|
||||
{
|
||||
|
||||
class IPhysicalDevice : public IDevice
|
||||
{
|
||||
public:
|
||||
~IPhysicalDevice() override = default;
|
||||
|
||||
virtual bool start() = 0;
|
||||
virtual void stop() = 0;
|
||||
};
|
||||
|
||||
} // namespace softbus::devices::base
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
|
||||
namespace softbus::devices::physical
|
||||
{
|
||||
enum class Type
|
||||
{
|
||||
Serial,
|
||||
Can,
|
||||
Tcp,
|
||||
Udp,
|
||||
};
|
||||
}
|
||||
64
src/devices/physical/SerialDevice.cpp
Normal file
64
src/devices/physical/SerialDevice.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
#include "devices/physical/SerialDevice.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "hardware/drivers/serial/SerialQtDriver.h"
|
||||
#include "utils/logs/logging.h"
|
||||
|
||||
using softbus::hardware::drivers::serial::SerialQtDriver;
|
||||
|
||||
namespace softbus::devices::physical
|
||||
{
|
||||
|
||||
SerialDevice::SerialDevice(std::unique_ptr<SerialQtDriver> driver,
|
||||
const Config& config,
|
||||
const softbus::devices::DeviceInfo& info)
|
||||
: m_driver(std::move(driver))
|
||||
, m_config(config)
|
||||
, m_info(info)
|
||||
{
|
||||
}
|
||||
|
||||
SerialDevice::~SerialDevice()
|
||||
{
|
||||
stop();
|
||||
}
|
||||
|
||||
bool SerialDevice::start()
|
||||
{
|
||||
if (!m_driver) {
|
||||
LOG_ERROR() << "SerialDevice: start called with null driver";
|
||||
return false;
|
||||
}
|
||||
|
||||
LOG_INFO() << "SerialDevice: opening endpoint" << m_config.endpoint;
|
||||
return m_driver->Open(m_config.endpoint.toStdString());
|
||||
}
|
||||
|
||||
void SerialDevice::stop()
|
||||
{
|
||||
if (!m_driver) {
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_INFO() << "SerialDevice: closing endpoint" << m_config.endpoint;
|
||||
m_driver->Close();
|
||||
}
|
||||
|
||||
softbus::devices::DeviceInfo SerialDevice::deviceInfo() const
|
||||
{
|
||||
return m_info;
|
||||
}
|
||||
|
||||
QString SerialDevice::endpoint() const
|
||||
{
|
||||
return m_config.endpoint;
|
||||
}
|
||||
|
||||
softbus::devices::DeviceKind SerialDevice::kind() const
|
||||
{
|
||||
return softbus::devices::DeviceKind::Serial;
|
||||
}
|
||||
|
||||
} // namespace softbus::devices::physical
|
||||
|
||||
47
src/devices/physical/SerialDevice.h
Normal file
47
src/devices/physical/SerialDevice.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <memory>
|
||||
|
||||
#include "devices/DeviceTypes.h"
|
||||
#include "devices/base/IPhysicalDevice.h"
|
||||
namespace softbus::hardware::drivers::serial { class SerialQtDriver; }
|
||||
|
||||
namespace softbus::devices::physical
|
||||
{
|
||||
|
||||
class SerialDevice : public base::IPhysicalDevice
|
||||
{
|
||||
public:
|
||||
struct Config
|
||||
{
|
||||
QString endpoint;
|
||||
int baudRate;
|
||||
int dataBits;
|
||||
QString parity;
|
||||
int stopBits;
|
||||
QString flowControl;
|
||||
};
|
||||
|
||||
SerialDevice(std::unique_ptr<softbus::hardware::drivers::serial::SerialQtDriver> driver,
|
||||
const Config& config,
|
||||
const softbus::devices::DeviceInfo& info);
|
||||
|
||||
~SerialDevice() override;
|
||||
|
||||
bool start() override;
|
||||
void stop() override;
|
||||
|
||||
softbus::devices::DeviceInfo deviceInfo() const override;
|
||||
|
||||
QString endpoint() const;
|
||||
softbus::devices::DeviceKind kind() const;
|
||||
|
||||
private:
|
||||
std::unique_ptr<softbus::hardware::drivers::serial::SerialQtDriver> m_driver;
|
||||
Config m_config;
|
||||
softbus::devices::DeviceInfo m_info;
|
||||
};
|
||||
|
||||
} // namespace softbus::devices::physical
|
||||
|
||||
Reference in New Issue
Block a user