context bind
This commit is contained in:
@@ -3,18 +3,128 @@
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QSaveFile>
|
||||
#include <QSet>
|
||||
// #include <algorithm>
|
||||
|
||||
#include "device_bus/DeviceBus.h"
|
||||
#include "message_bus/egress/EgressRouter.h"
|
||||
#include "message_bus/ingress/DriverIngressAdapter.h"
|
||||
#include "message_bus/pipeline/PipelineEngine.h"
|
||||
#include "message_bus/pipeline/rules/DynamicRuleRegistry.h"
|
||||
#include "core/plugin_system/PluginManager.h"
|
||||
#include "device_bus/manager/SerialDeviceManager.h"
|
||||
#include "devices/DeviceTypes.h"
|
||||
#include "utils/logs/logging.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
softbus::message_bus::pipeline::RuleScope parseRuleScope(const QJsonObject& obj)
|
||||
{
|
||||
softbus::message_bus::pipeline::RuleScope scope;
|
||||
scope.endpointHash = static_cast<std::uint32_t>(obj.value(QStringLiteral("endpointHash")).toInteger(0));
|
||||
scope.deviceId = obj.value(QStringLiteral("deviceId")).toInt(-1);
|
||||
scope.stableKey = obj.value(QStringLiteral("stableKey")).toString();
|
||||
return scope;
|
||||
}
|
||||
|
||||
QString rulesConfigPath()
|
||||
{
|
||||
const QString daemonConfigPath = softbus::device_bus::DeviceBus::instance().configPath();
|
||||
if (!daemonConfigPath.isEmpty()) {
|
||||
const QFileInfo fi(daemonConfigPath);
|
||||
return fi.dir().filePath(QStringLiteral("runtime_rules.json"));
|
||||
}
|
||||
return QDir::homePath() + QStringLiteral("/softbus/config/runtime_rules.json");
|
||||
}
|
||||
|
||||
void loadDynamicRulesFromDedicatedConfig()
|
||||
{
|
||||
auto& reg = softbus::message_bus::pipeline::DynamicRuleRegistry::instance();
|
||||
const QString path = rulesConfigPath();
|
||||
QFile file(path);
|
||||
if (!file.exists()) {
|
||||
const QFileInfo fi(path);
|
||||
if (!QDir().mkpath(fi.dir().absolutePath())) {
|
||||
LOG_WARNING() << "DeviceBusManager: failed to create rules config directory:" << fi.dir().absolutePath();
|
||||
return;
|
||||
}
|
||||
QSaveFile initFile(path);
|
||||
if (!initFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||
LOG_WARNING() << "DeviceBusManager: failed to create rules config file:" << path;
|
||||
return;
|
||||
}
|
||||
const QJsonObject emptyRules{
|
||||
{QStringLiteral("filter"), QJsonArray{}},
|
||||
{QStringLiteral("transform"), QJsonArray{}},
|
||||
};
|
||||
const QByteArray initBytes = QJsonDocument(emptyRules).toJson(QJsonDocument::Indented);
|
||||
if (initFile.write(initBytes) != initBytes.size()) {
|
||||
LOG_WARNING() << "DeviceBusManager: failed to initialize rules config file:" << path;
|
||||
initFile.cancelWriting();
|
||||
return;
|
||||
}
|
||||
if (!initFile.commit()) {
|
||||
LOG_WARNING() << "DeviceBusManager: failed to commit initialized rules config file:" << path;
|
||||
return;
|
||||
}
|
||||
LOG_INFO() << "DeviceBusManager: created empty rules config file:" << path;
|
||||
return;
|
||||
}
|
||||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
LOG_WARNING() << "DeviceBusManager: failed to open rules config:" << path;
|
||||
return;
|
||||
}
|
||||
QJsonParseError pe;
|
||||
const QJsonDocument doc = QJsonDocument::fromJson(file.readAll(), &pe);
|
||||
if (pe.error != QJsonParseError::NoError || !doc.isObject()) {
|
||||
LOG_WARNING() << "DeviceBusManager: invalid rules config json:" << path << "err=" << pe.errorString();
|
||||
return;
|
||||
}
|
||||
|
||||
const QJsonObject rulesObj = doc.object();
|
||||
const QJsonArray filterRules = rulesObj.value(QStringLiteral("filter")).toArray();
|
||||
const QJsonArray transformRules = rulesObj.value(QStringLiteral("transform")).toArray();
|
||||
int loadedFilterCount = 0;
|
||||
for (const auto& item : filterRules) {
|
||||
const QJsonObject obj = item.toObject();
|
||||
softbus::message_bus::pipeline::FilterRule rule;
|
||||
rule.id = obj.value(QStringLiteral("id")).toString();
|
||||
rule.scope = parseRuleScope(obj.value(QStringLiteral("scope")).toObject());
|
||||
rule.condition = obj.value(QStringLiteral("condition")).toObject();
|
||||
if (!rule.id.isEmpty()) {
|
||||
(void)reg.addFilterRule(rule);
|
||||
++loadedFilterCount;
|
||||
}
|
||||
}
|
||||
|
||||
int loadedTransformCount = 0;
|
||||
for (const auto& item : transformRules) {
|
||||
const QJsonObject obj = item.toObject();
|
||||
softbus::message_bus::pipeline::TransformRule rule;
|
||||
rule.id = obj.value(QStringLiteral("id")).toString();
|
||||
rule.scope = parseRuleScope(obj.value(QStringLiteral("scope")).toObject());
|
||||
rule.scale = obj.value(QStringLiteral("scale")).toDouble(1.0);
|
||||
rule.offset = obj.value(QStringLiteral("offset")).toDouble(0.0);
|
||||
rule.unit = obj.value(QStringLiteral("unit")).toString();
|
||||
if (!rule.id.isEmpty()) {
|
||||
(void)reg.addTransformRule(rule);
|
||||
++loadedTransformCount;
|
||||
}
|
||||
}
|
||||
LOG_INFO() << "DeviceBusManager: loaded dynamic rules from" << path
|
||||
<< "filter=" << loadedFilterCount
|
||||
<< "transform=" << loadedTransformCount;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
DeviceBusManager::~DeviceBusManager()
|
||||
{
|
||||
shutdown();
|
||||
@@ -97,6 +207,7 @@ bool DeviceBusManager::initialize(const QJsonObject& rootConfig)
|
||||
m_pipelineEngine->setMemoryPool(m_memoryPool);
|
||||
m_pipelineEngine->loadRuntimeConfigFromJson(rootConfig.value(QStringLiteral("pipeline")).toObject());
|
||||
m_pipelineEngine->setPluginManager(m_pluginManager);
|
||||
loadDynamicRulesFromDedicatedConfig();
|
||||
m_pipelineEngine->start();
|
||||
}
|
||||
if (!m_ingressAdapter) {
|
||||
@@ -198,6 +309,32 @@ void DeviceBusManager::shutdown()
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<softbus::devices::DeviceInfo> DeviceBusManager::listDevices() const
|
||||
{
|
||||
if (!m_registry) {
|
||||
return {};
|
||||
}
|
||||
return m_registry->listDevices();
|
||||
}
|
||||
|
||||
std::optional<softbus::devices::DeviceInfo> DeviceBusManager::findDeviceById(int id) const
|
||||
{
|
||||
if (!m_registry) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return m_registry->findById(id);
|
||||
}
|
||||
|
||||
softbus::message_bus::pipeline::PipelineEngine* DeviceBusManager::pipelineEngine() const
|
||||
{
|
||||
return m_pipelineEngine.get();
|
||||
}
|
||||
|
||||
std::shared_ptr<softbus::core::plugin_system::PluginManager> DeviceBusManager::pluginManager() const
|
||||
{
|
||||
return m_pluginManager;
|
||||
}
|
||||
|
||||
void DeviceBusManager::initializeSerialDevice(const QString& endpoint, const QJsonObject& params)
|
||||
{
|
||||
softbus::device_bus::manager::SerialInitDeps deps;
|
||||
|
||||
Reference in New Issue
Block a user