第五章 插件:OpenHarmony设备插件实现分析
OpenHarmony设备插件由两个主要类组成:
这两个类共同构成了完整的OpenHarmony设备支持功能,通过HDC(HarmonyOS Device Connector)工具与设备进行交互。
OHOSDevicePlugin类是插件的入口点,继承自QObject和IDevicePlugin接口,主要负责:
classOHOSDevicePlugin :public QObject, public IDevicePlugin
{
Q_OBJECT
Q_INTERFACES(IDevicePlugin)
Q_PLUGIN_METADATA(IID "com.screencast.deviceplugin.ohos" FILE "ohosdeviceplugin.json")
public:
OHOSDevicePlugin(QObject* parent = nullptr);
~OHOSDevicePlugin() override;
// 插件基本信息
QString pluginName()constoverride;
QString pluginVersion()constoverride;
DeviceType deviceType()constoverride;
// 设备代理创建
DeviceProxy* createDeviceProxy(QObject* parent)constoverride;
// HDC驱动管理
boolcheckDriverAvailable()constoverride;
QString getDriverPath()constoverride;
};
OHOSDevice类是实际与OpenHarmony设备交互的组件,继承自DeviceProxy,主要负责:
classOHOSDevice :public DeviceProxy
{
Q_OBJECT
public:
OHOSDevice(QObject* parent = nullptr);
~OHOSDevice() override;
// 设备管理
QVector<DeviceInfo> listDevices()override;
boolqueryDeviceInfo(const QString& serial, DeviceInfo& info)override;
DeviceType deviceType()constoverride;
// 设备信息查询
QString deviceModel(const QString& serial)override;
QString deviceName(const QString& serial)override;
QSize deviceResolution(const QString& serial)override;
intdeviceRotation(const QString& serial)override;
// 镜像服务器管理
boolsetupMirrorServer(const QString& serial, int forwardPort)override;
boolstartMirrorServer(const QString& serial)override;
boolstopMirrorServer(const QString& serial)override;
QString getMirrorServerIp(const QString& serial)override;
// 事件发送
boolsendEvent(const DeviceInfo& dev, DeviceEvent event)override;
boolsendTouchEvent(const DeviceInfo& dev, QPoint pos)override;
boolsendTextEvent(const DeviceInfo& dev, const QString& text)override;
boolsendSwipeEvent(const DeviceInfo& dev, QPoint start, QPoint end, int duration)override;
// 其他功能
boolscreenshot(const QString& serial, QByteArray& imageData)override;
boolsupportEvent(DeviceEvent event)constoverride;
private:
boolpushResourceToDevice(const QString& serial, const QString& resourcePath, const QString& devicePath);
boolisScreenOn(const QString& serial);
private:
bool m_running{false};
QMutex m_mutex;
};
OpenHarmony插件通过HDC(HarmonyOS Device Connector)工具与设备进行通信,实现了以下核心功能:
使用hdc list targets命令获取已连接的OpenHarmony设备列表:
QVector<DeviceInfo> OHOSDevice::listDevices()
{
QMutexLocker locker(&m_mutex);
QVector<DeviceInfo> devices;
QString output;
if (!Shell::executeQuiet("hdc list targets", &output))
{
return devices;
}
QStringList lines = output.split('\n', Qt::SkipEmptyParts);
for (const QString& line : lines)
{
QString serial = line.trimmed();
if (!serial.isEmpty() && serial != "[Empty]")
{
DeviceInfo info;
info.type = DeviceType::OHOS;
info.serial = serial;
devices.append(info);
}
}
return devices;
}
通过HDC命令获取设备的各种信息:
hdc -t <serial> shell param get const.product.modelhdc -t <serial> shell param get const.product.namehdc -t <serial> shell hidumper -s RenderService -a screenhdc -t <serial> shell hidumper -s RenderService -a screen实现了丰富的设备事件发送功能,包括:
事件处理采用映射表设计,使代码结构清晰:
// 定义事件与命令的映射
staticconststd::map<DeviceEvent, QString> eventMap = {
{DeviceEvent::HOME, "uinput -K -d 1 -u 1"},
{DeviceEvent::BACK, "uinput -K -d 2 -u 2"},
{DeviceEvent::MENU, "uinput -K -d 2078 -u 2078"},
{DeviceEvent::WAKEUP, "power-shell wakeup"},
// ... 其他事件映射
};
OpenHarmony插件实现了完整的镜像服务器部署和管理流程:
将镜像服务器程序推送到设备:
boolOHOSDevice::pushResourceToDevice(const QString& serial, const QString& resourcePath, const QString& devicePath)
{
// 读取资源文件
QFile file(resourcePath);
if (!file.open(QIODevice::ReadOnly))
{
returnfalse;
}
// 创建临时文件
QTemporaryFile tempFile;
if (!tempFile.open())
{
returnfalse;
}
tempFile.write(file.readAll());
tempFile.close();
// 推送文件到设备
QString command = QString("hdc -t %1 file send %2 %3").arg(serial, QDir::toNativeSeparators(tempFile.fileName()), devicePath);
// ...
// 设置执行权限
command = QString("hdc -t %1 shell chmod 755 %2").arg(serial, devicePath);
// ...
returntrue;
}
设置本地端口与设备端口之间的转发:
QString forwardCommand = QString("hdc -t %1 fport tcp:%2 tcp:12345").arg(serial).arg(forwardPort);
使用QtConcurrent实现镜像服务器的异步启动和状态监控:
// 启动服务器进程
[[maybe_unused]] auto future1 = QtConcurrent::run([=]() {
QStringList args = {"-t", serial, "shell", "/data/local/tmp/mirror_server"};
QProcess process;
process.start("hdc", args);
while (m_running && !process.waitForFinished(1000))
{
// Keep running
}
process.kill();
});
// 检查服务器状态
[[maybe_unused]] auto future2 = QtConcurrent::run([=]() {
QString command = QString("hdc -t %1 shell netstat -ltn | grep ':12345'").arg(serial);
for (int i = 0; i < 30 && m_running; ++i)
{
QString output;
if (Shell::executeQuiet(command, &output) && output.contains(":12345"))
{
emit serverStarted(serial);
return;
}
QThread::sleep(1);
}
});
截图功能实现了从设备获取屏幕截图并传输到本地:
boolOHOSDevice::screenshot(const QString& serial, QByteArray& imageData)
{
QMutexLocker locker(&m_mutex);
const QString timestamp = QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
const QString tempPath = QString("/data/local/tmp/screenshot_%1.jpeg").arg(timestamp);
const QString localPath = QDir::toNativeSeparators(QDir::tempPath() + QDir::separator() + QString("screenshot_%1.jpeg").arg(timestamp));
// 截取屏幕截图
QString command = QString("hdc -t %1 shell snapshot_display -f %2").arg(serial, tempPath);
if (!Shell::execute(command))
{
returnfalse;
}
// 拉取截图到本地
command = QString("hdc -t %1 file recv %2 %3").arg(serial, tempPath, localPath);
if (!Shell::execute(command))
{
returnfalse;
}
// 读取图像数据并清理
QFile tempFile(localPath);
if (tempFile.open(QIODevice::ReadOnly))
{
imageData = tempFile.readAll();
tempFile.close();
QFile::remove(localPath);
returntrue;
}
returnfalse;
}
OpenHarmony设备插件通过HDC工具实现了与OpenHarmony设备的全面交互,包括设备发现、信息查询、事件发送、镜像服务和截图等功能。插件采用了模块化设计和异步处理机制,确保了良好的性能和用户体验。
与Android插件类似,OpenHarmony插件也遵循了相同的接口规范,但针对OpenHarmony平台的特点进行了专门优化,使用了适合OpenHarmony系统的HDC工具和命令集。
通过这些实现,ScreenCast项目成功支持了OpenHarmony设备的屏幕投影和远程控制功能,为用户提供了统一的多平台投影解决方案。