forked from LazyT/rrcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.cpp
180 lines (128 loc) · 4.43 KB
/
update.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "update.h"
updateDialog::updateDialog(QWidget *parent, QFile *package) : QDialog(parent)
{
setupUi(this);
layout()->setSizeConstraint(QLayout::SetFixedSize);
setWindowFlags(windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
file = package;
label_file->setText(QFileInfo(file->fileName()).fileName());
label_bytes->setText(QString("0 / %1 Byte").arg(file->size()));
connect(&timer, SIGNAL(timeout()), this, SLOT(timer_refreshTime()));
time.start();
timer.start(500);
QTimer::singleShot(1, this, SLOT(startUpdating()));
}
void updateDialog::startUpdating()
{
pkg = file->readAll();
file->close();
size = pkg.size();
server = new QTcpServer(this);
socket = nullptr;
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
if(server->listen(QHostAddress(reinterpret_cast<MainWindow*>(parent())->src_ip), 8080))
{
if(!reinterpret_cast<MainWindow*>(parent())->sendUDP(QString(MIIO_OTA).arg(QString("http://%1:8080/%2").arg(reinterpret_cast<MainWindow*>(parent())->src_ip).arg(QFileInfo(file->fileName()).fileName())).arg(QString(QCryptographicHash::hash(pkg, QCryptographicHash::Md5).toHex())).arg("%1")))
{
timer.stop();
canceled = true;
QMessageBox::warning(this, APPNAME, tr("Could not start firmware update!"));
close();
}
}
else
{
timer.stop();
canceled = true;
QMessageBox::warning(this, APPNAME, tr("Could not start HTTP server!\n\n%1").arg(server->errorString()));
close();
}
}
void updateDialog::timer_refreshTime()
{
QTime elapsed = QTime::fromMSecsSinceStartOfDay(time.elapsed());
label_time->setText(QString("%1h:%2m:%3s").arg(elapsed.hour(), 2, 10, QChar('0')).arg(elapsed.minute(), 2, 10, QChar('0')).arg(elapsed.second(), 2, 10, QChar('0')));
if(!requested && time.elapsed() >= 15000)
{
timer.stop();
canceled = true;
QMessageBox::warning(this, APPNAME, tr("Robot does not send firmware package request!\n\nMake sure your firewall accepts incoming tcp connections for\n\n %1 -> %2:8080\n\nand try again...").arg(reinterpret_cast<MainWindow*>(parent())->cfg.ip).arg(reinterpret_cast<MainWindow*>(parent())->src_ip));
close();
}
}
void updateDialog::newConnection()
{
socket = server->nextPendingConnection();
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(socket, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten(qint64)));
requested = true;
}
void updateDialog::readyRead()
{
socket->readAll();
header = QString("HTTP/1.1 200 OK\r\nDate: %1 GMT\r\nContent-Type: application/octet-stream\r\nContent-Length: %2\r\nConnection: close\r\n\r\n").arg(QLocale("en_US").toString(QDateTime::currentDateTime().toUTC(), "ddd, dd MMM yyyy hh:mm:ss")).arg(size).toUtf8();
total = static_cast<quint64>(header.size() * -1);
socket->write(header);
socket->write(pkg);
}
void updateDialog::bytesWritten(qint64 byte)
{
total += static_cast<quint64>(byte);
progressBar_upload->setValue(static_cast<int>((100 * total) / static_cast<quint64>(size)));
label_bytes->setText(QString("%1 / %2 Byte").arg(total).arg(size));
}
void updateDialog::disconnected()
{
finished = true;
socket->close();
server->close();
if(canceled)
{
timer.stop();
QMessageBox::warning(this, APPNAME, tr("Update aborted by user!"));
close();
return;
}
progressBar_install->setValue(50);
do
{
reinterpret_cast<MainWindow*>(parent())->sendUDP(MIIO_GET_OTA_STATE);
QThread::msleep(1000);
QCoreApplication::processEvents();
}
while(reinterpret_cast<MainWindow*>(parent())->robo.ota.state == "downloading" || reinterpret_cast<MainWindow*>(parent())->robo.ota.state == "installing");
progressBar_install->setValue(100);
timer.stop();
reinterpret_cast<MainWindow*>(parent())->robo.ota.state == "failed" ? QMessageBox::warning(this, APPNAME, tr("Firmware update failed!")) : QMessageBox::information(this, APPNAME, tr("Firmware update successful."));
reinterpret_cast<MainWindow*>(parent())->sendUDP(nullptr);
close();
}
void updateDialog::reject()
{
if(!finished && !canceled)
{
if(QMessageBox::question(this, APPNAME, tr("Really abort update?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes)
{
canceled = true;
if(socket)
{
socket->abort();
reject();
}
else
{
server->close();
accept();
}
}
}
else
{
accept();
}
}
void updateDialog::on_buttonBox_clicked(__attribute__((unused)) QAbstractButton *button)
{
close();
}