-
Notifications
You must be signed in to change notification settings - Fork 1
/
updatewindow.cpp
235 lines (190 loc) · 5.34 KB
/
updatewindow.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "updatewindow.h"
#include "ui_updatewindow.h"
updateWindow::updateWindow(QWidget* parent) :
QDialog(parent),
ui(new Ui::updateWindow)
{
ui->setupUi(this);
setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint);
setWindowTitle("Update Dialog");
}
updateWindow::~updateWindow()
{
delete ui;
}
void updateWindow::addLine(const QString &text){
time_t currentTime;
struct tm* pstTime;
char timeString[32];
time(¤tTime);
pstTime = localtime(¤tTime);
strftime(timeString, 32, "%H:%M:%S", pstTime);
ui->textEdit->setText(ui->textEdit->toPlainText() + "["+ timeString + "] " + text + "\n");
ui->textEdit->moveCursor(QTextCursor::End);
qApp->processEvents();
}
void updateWindow::clear()
{
ui->textEdit->clear();
}
void updateWindow::refresh() {
qApp->processEvents();
this->show();
}
void updateWindow::setProgressBarVisible(bool isVisible) {
ui->progressBar->setVisible(isVisible);
}
void updateWindow::setProgressBarValue(int n) {
ui->progressBar->setValue(n);
}
bool updateWindow::startUpdate(const QList<int>& work, const QString &OSLoader_path, const QString& System_path, const int& page_OSLoader, const int& page_System)
{
this->clear();
this->setProgressBarVisible(false);
this->show();
this->addLine("Starting update...");
bool isOK = false;
if (link_mode == HOSTLINK_MODE) {
//use sbloader to send OSLoader to calculator RAM.
TCHAR* argv[2];
argv[0] = (TCHAR*)TEXT("-f");
argv[1] = (TCHAR*)reinterpret_cast<const wchar_t*>(OSLoader_path.utf16());
this->addLine("Sending OS Loader to calculator RAM...");
int exitCode = _tmain(2, argv);
this->addLine("Finished with code " + QString::number(exitCode) + ".");
if (exitCode != 0) {
this->addLine("ERROR: Failed to send OS Loader to calculator RAM.");
isOK = false;
goto update_end;
}
this->addLine("Reboot and reconnect... Please wait. ");
//this->addLine("Interval: " + QString::number(REBOOT_INTERVAL) + "ms");
for (int i = 0; i < REBOOT_RETRY_TIME; i++) {
searchForDevices();
if (link_mode == EDB_BIN) break;
Sleep(REBOOT_INTERVAL); //reboot interval
}
if (link_mode != EDB_BIN) {
isOK = false;
goto update_end;
}
this->addLine("Device found.");
}
flashImg item;
errno_t ret;
for (int i = 0; i < work.size(); i++) {
//open edb connection
if (edb.open(EDB_MODE_BIN)) {
this->addLine("Use USB mode to flash.");
}
else {
this->addLine("ERROR: Can not open EDB connection.");
isOK = false;
goto update_end;
}
//set up callback function
shared_ptr<updateWindow> self = make_shared<updateWindow>();
callBack cb = bind(&updateWindow::refreshStatus, self);
switch (work.at(i))
{
case UPDATE_SYSTEM: //update system
memset(&item, 0, sizeof(item));
ret = fopen_s(&item.f, System_path.toLocal8Bit().data(), "rb");
if (ret != 0) {
this->addLine("ERROR: Can not open file.");
isOK = false;
goto update_end;
}
item.filename = System_path.toLocal8Bit().data();
item.toPage = page_System;
item.bootImg = false;
this->addLine("Flashing system to page " + QString::number(page_System) + ".");
break;
case UPDATE_OSLOADER: //update OSLoader
memset(&item, 0, sizeof(item));
ret = fopen_s(&item.f, OSLoader_path.toLocal8Bit().data(), "rb");
if (ret != 0) {
this->addLine("ERROR: Can not open file.");
isOK = false;
goto update_end;
}
item.filename = OSLoader_path.toLocal8Bit().data();
item.toPage = page_OSLoader;
item.bootImg = true;
this->addLine("Flashing OS Loader to page " + QString::number(page_OSLoader) + ".");
break;
default:
isOK = false;
goto update_end;
break;
}
emit sendUpdateStatus(work.at(i) + 1);
if (link_mode == EDB_BIN) edb.reset(EDB_MODE_TEXT);
if (!edb.ping()) {
this->addLine("ERROR: Device no response.");
isOK = false;
goto update_end;
}
edb.vm_suspend();
edb.flash(item, cb);
std::fclose(item.f);
this->addLine("Device rebooting...");
edb.reboot();
edb.close();
for (int j = 0; j < REBOOT_RETRY_TIME; j++) {
searchForDevices();
if (link_mode == EDB_BIN) break;
Sleep(REBOOT_EDB_INTERVAL);
}
}
if (link_mode == EDB_BIN) {
this->addLine("Done.");
isOK = true;
goto update_end;
}
else {
isOK = false;
goto update_end;
}
update_end:
emit sendUpdateStatus(UPDATE_NONE);
return isOK;
}
int updateWindow::searchDevices() {
if (libusb_init(nullptr) != 0) {
QMessageBox::critical(this, " ", "Can not init libusb!");
return UNCONNECT_MODE;
}
int device_count = 0;
libusb_device** device_list;
libusb_device* device_now;
struct libusb_device_descriptor desc;
device_count = libusb_get_device_list(nullptr, &device_list);
if (device_count < 0) return false;
for (int i = 0; i < device_count; i++) {
device_now = device_list[i];
libusb_get_device_descriptor(device_now, &desc);
if (desc.idVendor == HOSTLINK_VID && desc.idProduct == HOSTLINK_PID) return HOSTLINK_MODE;
if (desc.idVendor == EOS_VID && desc.idProduct == EOS_PID) return EDB_BIN;
}
return UNCONNECT_MODE;
}
int updateWindow::searchForDevices() {
link_mode = searchDevices();
return link_mode;
}
void updateWindow::refreshStatus() {
qApp->processEvents();
//callback function
//just for process events :)
}
bool updateWindow::reboot() {
if (edb.open(1)) {
edb.reboot();
edb.close();
return true;
}
else {
return false;
}
}