2026-03-12 14:56:53 +08:00
|
|
|
|
#include "device_bus/manager/DeviceBusManager.h"
|
|
|
|
|
|
|
2026-03-18 16:31:32 +08:00
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
2026-03-12 14:56:53 +08:00
|
|
|
|
#include <QJsonDocument>
|
2026-03-18 16:31:32 +08:00
|
|
|
|
#include <QSerialPort>
|
2026-03-12 14:56:53 +08:00
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
2026-03-18 16:31:32 +08:00
|
|
|
|
#include "hardware/drivers/serial/SerialQtDriver.h"
|
2026-03-12 14:56:53 +08:00
|
|
|
|
#include "hardware/drivers/serial/SerialCapabilities.h"
|
2026-03-18 16:31:32 +08:00
|
|
|
|
#include "devices/DeviceTypes.h"
|
2026-03-12 14:56:53 +08:00
|
|
|
|
#include "utils/logs/logging.h"
|
|
|
|
|
|
|
|
|
|
|
|
using softbus::hardware::drivers::serial::SerialQtDriver;
|
|
|
|
|
|
namespace serial_cap = softbus::hardware::drivers::serial::capabilities;
|
|
|
|
|
|
|
2026-03-18 16:31:32 +08:00
|
|
|
|
void DeviceBusManager::setDeviceTreeModel(std::shared_ptr<DeviceTreeModel> model)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_treeModel = std::move(model);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-12 14:56:53 +08:00
|
|
|
|
bool DeviceBusManager::initialize(const QJsonObject& rootConfig)
|
|
|
|
|
|
{
|
2026-03-18 16:31:32 +08:00
|
|
|
|
if (!m_treeModel) {
|
|
|
|
|
|
m_treeModel = std::make_shared<DeviceTreeModel>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-12 14:56:53 +08:00
|
|
|
|
// 加载设备树
|
|
|
|
|
|
QString treeError;
|
2026-03-18 16:31:32 +08:00
|
|
|
|
if (!m_treeModel->loadFromDaemonConfig(rootConfig, &treeError)) {
|
2026-03-12 14:56:53 +08:00
|
|
|
|
LOG_ERROR() << "DeviceBusManager: failed to load device_tree:" << treeError;
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-18 16:31:32 +08:00
|
|
|
|
const auto nodes = m_treeModel->nodes();
|
2026-03-12 14:56:53 +08:00
|
|
|
|
for (const auto& n : nodes) {
|
|
|
|
|
|
if (!n.enabled) continue;
|
2026-03-18 16:31:32 +08:00
|
|
|
|
|
|
|
|
|
|
auto kind = softbus::devices::kindFromType(n.type);
|
|
|
|
|
|
if (kind == softbus::devices::DeviceKind::Unknown) {
|
|
|
|
|
|
kind = softbus::devices::inferDeviceKindFromEndpoint(n.endpoint);
|
2026-03-12 14:56:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-18 16:31:32 +08:00
|
|
|
|
if (kind == softbus::devices::DeviceKind::Serial) {
|
|
|
|
|
|
initializeSerialDevice(n.endpoint, n.params);
|
|
|
|
|
|
m_deviceEndpointsMap.insert(n.endpoint, kind);
|
2026-03-12 14:56:53 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
LOG_WARNING() << "DeviceBusManager: skip unsupported kind for endpoint=" << n.endpoint
|
|
|
|
|
|
<< "type=" << n.type;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DeviceBusManager::shutdown()
|
|
|
|
|
|
{
|
2026-03-18 16:31:32 +08:00
|
|
|
|
// 关闭并销毁所有串口设备
|
|
|
|
|
|
const auto keys = m_serialDevicesByEndpoint.keys();
|
2026-03-12 14:56:53 +08:00
|
|
|
|
for (const auto& endpoint : keys) {
|
2026-03-18 16:31:32 +08:00
|
|
|
|
shutdownSerialDevice(endpoint);
|
2026-03-12 14:56:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-18 16:31:32 +08:00
|
|
|
|
void DeviceBusManager::initializeSerialDevice(const QString& endpoint, const QJsonObject& params)
|
2026-03-12 14:56:53 +08:00
|
|
|
|
{
|
2026-03-18 16:31:32 +08:00
|
|
|
|
if (m_serialDevicesByEndpoint.contains(endpoint)) {
|
|
|
|
|
|
LOG_WARNING() << "DeviceBusManager: serial device already exists for" << endpoint;
|
2026-03-12 14:56:53 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 合并参数(defaults + enum 校验)
|
|
|
|
|
|
serial_cap::SerialDefaults def = serial_cap::kDefaults;
|
|
|
|
|
|
|
|
|
|
|
|
int baud = params.value(QStringLiteral("baudRate")).toInt(def.baudRate);
|
|
|
|
|
|
if (!serial_cap::isValidBaud(baud)) baud = def.baudRate;
|
|
|
|
|
|
|
|
|
|
|
|
int dataBits = params.value(QStringLiteral("dataBits")).toInt(def.dataBits);
|
|
|
|
|
|
if (!serial_cap::isValidDataBits(dataBits)) dataBits = def.dataBits;
|
|
|
|
|
|
|
|
|
|
|
|
QString parityStr = params.value(QStringLiteral("parity")).toString(QLatin1String(def.parity));
|
|
|
|
|
|
if (!serial_cap::isValidParity(parityStr)) parityStr = QLatin1String(def.parity);
|
|
|
|
|
|
|
|
|
|
|
|
int stopBitsVal = params.value(QStringLiteral("stopBits")).toInt(def.stopBits);
|
|
|
|
|
|
if (!serial_cap::isValidStopBits(stopBitsVal)) stopBitsVal = def.stopBits;
|
|
|
|
|
|
|
|
|
|
|
|
QString flowStr = params.value(QStringLiteral("flowControl")).toString(QLatin1String(def.flowControl));
|
|
|
|
|
|
if (!serial_cap::isValidFlowControl(flowStr)) flowStr = QLatin1String(def.flowControl);
|
|
|
|
|
|
|
|
|
|
|
|
QJsonObject merged;
|
|
|
|
|
|
merged.insert(QStringLiteral("baudRate"), baud);
|
|
|
|
|
|
merged.insert(QStringLiteral("dataBits"), dataBits);
|
|
|
|
|
|
merged.insert(QStringLiteral("parity"), parityStr);
|
|
|
|
|
|
merged.insert(QStringLiteral("stopBits"), stopBitsVal);
|
|
|
|
|
|
merged.insert(QStringLiteral("flowControl"), flowStr);
|
|
|
|
|
|
|
|
|
|
|
|
LOG_INFO() << "DeviceBusManager: init serial endpoint=" << endpoint
|
|
|
|
|
|
<< "params=" << QJsonDocument(merged).toJson(QJsonDocument::Compact);
|
|
|
|
|
|
|
2026-03-18 16:31:32 +08:00
|
|
|
|
auto driver = std::make_unique<SerialQtDriver>(nullptr);
|
2026-03-12 14:56:53 +08:00
|
|
|
|
driver->SetBaudRate(static_cast<QSerialPort::BaudRate>(baud));
|
|
|
|
|
|
driver->SetDataBits(static_cast<QSerialPort::DataBits>(dataBits));
|
|
|
|
|
|
|
|
|
|
|
|
QSerialPort::Parity parity = QSerialPort::NoParity;
|
|
|
|
|
|
if (parityStr == QStringLiteral("even")) {
|
|
|
|
|
|
parity = QSerialPort::EvenParity;
|
|
|
|
|
|
} else if (parityStr == QStringLiteral("odd")) {
|
|
|
|
|
|
parity = QSerialPort::OddParity;
|
|
|
|
|
|
}
|
|
|
|
|
|
driver->SetParity(parity);
|
|
|
|
|
|
|
|
|
|
|
|
QSerialPort::StopBits stopBits = (stopBitsVal == 2) ? QSerialPort::TwoStop : QSerialPort::OneStop;
|
|
|
|
|
|
driver->SetStopBits(stopBits);
|
|
|
|
|
|
|
|
|
|
|
|
QSerialPort::FlowControl flow = QSerialPort::NoFlowControl;
|
|
|
|
|
|
if (flowStr == QStringLiteral("hardware")) {
|
|
|
|
|
|
flow = QSerialPort::HardwareControl;
|
|
|
|
|
|
} else if (flowStr == QStringLiteral("software")) {
|
|
|
|
|
|
flow = QSerialPort::SoftwareControl;
|
|
|
|
|
|
}
|
|
|
|
|
|
driver->SetFlowControl(flow);
|
|
|
|
|
|
|
2026-03-18 16:31:32 +08:00
|
|
|
|
softbus::devices::DeviceInfo info;
|
|
|
|
|
|
info.id = -1;
|
|
|
|
|
|
info.endpoint = endpoint;
|
|
|
|
|
|
info.type = QStringLiteral("serial");
|
|
|
|
|
|
info.kind = softbus::devices::DeviceKind::Serial;
|
|
|
|
|
|
|
|
|
|
|
|
softbus::devices::physical::SerialDevice::Config cfg;
|
|
|
|
|
|
cfg.endpoint = endpoint;
|
|
|
|
|
|
cfg.baudRate = baud;
|
|
|
|
|
|
cfg.dataBits = dataBits;
|
|
|
|
|
|
cfg.parity = parityStr;
|
|
|
|
|
|
cfg.stopBits = stopBitsVal;
|
|
|
|
|
|
cfg.flowControl = flowStr;
|
|
|
|
|
|
|
|
|
|
|
|
QSharedPointer<softbus::devices::physical::SerialDevice> device(
|
|
|
|
|
|
new softbus::devices::physical::SerialDevice(std::move(driver), cfg, info));
|
|
|
|
|
|
|
|
|
|
|
|
if (m_registry) {
|
|
|
|
|
|
m_registry->registerDevice(info);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 按当前策略:初始化时就打开串口
|
|
|
|
|
|
if (!device->start()) {
|
|
|
|
|
|
LOG_ERROR() << "DeviceBusManager: failed to start serial device for" << endpoint;
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2026-03-12 14:56:53 +08:00
|
|
|
|
|
2026-03-18 16:31:32 +08:00
|
|
|
|
m_serialDevicesByEndpoint.insert(endpoint, device);
|
2026-03-12 14:56:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-18 16:31:32 +08:00
|
|
|
|
void DeviceBusManager::shutdownSerialDevice(const QString& endpoint)
|
2026-03-12 14:56:53 +08:00
|
|
|
|
{
|
2026-03-18 16:31:32 +08:00
|
|
|
|
auto it = m_serialDevicesByEndpoint.find(endpoint);
|
|
|
|
|
|
if (it == m_serialDevicesByEndpoint.end()) {
|
|
|
|
|
|
return;
|
2026-03-12 14:56:53 +08:00
|
|
|
|
}
|
2026-03-18 16:31:32 +08:00
|
|
|
|
|
|
|
|
|
|
auto device = it.value();
|
|
|
|
|
|
if (device) {
|
|
|
|
|
|
auto info = device->deviceInfo();
|
|
|
|
|
|
if (m_registry && info.id >= 0) {
|
|
|
|
|
|
m_registry->unregisterDevice(info.id);
|
|
|
|
|
|
}
|
|
|
|
|
|
device->stop();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
m_serialDevicesByEndpoint.erase(it);
|
2026-03-12 14:56:53 +08:00
|
|
|
|
}
|