完善升级功能。

This commit is contained in:
luocai
2024-08-08 17:05:32 +08:00
parent 2610c40189
commit 9d495d487d
7 changed files with 251 additions and 198 deletions

View File

@ -3,7 +3,6 @@
#include "StringUtility.h"
#include <QDebug>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <filesystem>
#include <fstream>
#include <mbedtls/md5.h>
@ -41,10 +40,15 @@ CdcUpdater::CdcUpdater(QObject *parent) : QObject(parent) {
CdcUpdater::~CdcUpdater() {
}
void CdcUpdater::start(const QString &path) {
if (m_timerId < 0) {
m_timerId = startTimer(1000);
void CdcUpdater::start(const QString &path, const QSerialPortInfo &info) {
if (info.isNull()) {
if (m_timerId < 0) {
m_timerId = startTimer(1000);
}
} else {
open(info);
}
m_path = Amass::StringUtility::UTF8ToGBK(path.toStdString());
LOG(info) << "ota file: " << m_path;
emit progressChanged(++m_progress);
@ -84,20 +88,29 @@ bool CdcUpdater::write(Command command, const uint8_t *data, uint32_t size) {
return m_serialPort->write(reinterpret_cast<const char *>(packet.data()), packet.size());
}
void CdcUpdater::timerEvent(QTimerEvent *event) {
std::optional<QSerialPortInfo> CdcUpdater::searchDevice() {
std::optional<QSerialPortInfo> ret = std::nullopt;
const auto serialPortInfos = QSerialPortInfo::availablePorts();
for (const QSerialPortInfo &portInfo : serialPortInfos) {
LOG(info) << "portName:" << portInfo.portName().toStdString()
<< ", vendorIdentifier: " << portInfo.vendorIdentifier()
<< ", productIdentifier: " << portInfo.productIdentifier();
if (portInfo.vendorIdentifier() == 0xffff) {
LOG(info) << "founded device: " << portInfo.portName().toStdString();
emit deviceDiscovered(portInfo);
killTimer(m_timerId);
m_timerId = -1;
ret = portInfo;
break;
}
}
return ret;
}
void CdcUpdater::timerEvent(QTimerEvent *event) {
auto device = searchDevice();
if (device) {
LOG(info) << "founded device: " << device->portName().toStdString();
emit deviceDiscovered(*device);
killTimer(m_timerId);
m_timerId = -1;
}
LOG(info) << "----------";
}

View File

@ -2,9 +2,10 @@
#define __CDCUPDATER_H__
#include <QObject>
#include <QSerialPortInfo>
#include <optional>
class QSerialPort;
class QSerialPortInfo;
class CdcUpdater : public QObject {
Q_OBJECT
@ -35,8 +36,10 @@ public:
};
CdcUpdater(QObject *parent = nullptr);
~CdcUpdater();
void start(const QString &path);
void start(const QString &path, const QSerialPortInfo &info = QSerialPortInfo());
bool open(const QSerialPortInfo &info);
static std::optional<QSerialPortInfo> searchDevice();
signals:
void deviceDiscovered(const QSerialPortInfo &info);
void updateFinished();