forked from vedderb/vesc_tool
-
Notifications
You must be signed in to change notification settings - Fork 1
/
codeloader.cpp
951 lines (781 loc) · 28.4 KB
/
codeloader.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
/*
Copyright 2022 Benjamin Vedder benjamin@vedder.se
This file is part of VESC Tool.
VESC Tool is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
VESC Tool is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "codeloader.h"
#include "utility.h"
#include <QEventLoop>
#include <QFileDialog>
#include <QMessageBox>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QEventLoop>
CodeLoader::CodeLoader(QObject *parent) : QObject(parent)
{
mVesc = nullptr;
}
VescInterface *CodeLoader::vesc() const
{
return mVesc;
}
void CodeLoader::setVesc(VescInterface *vesc)
{
mVesc = vesc;
}
bool CodeLoader::lispErase(int size)
{
if (!mVesc) {
return false;
}
if (!mVesc->isPortConnected()) {
mVesc->emitMessageDialog(tr("Erase old code"), tr("Not Connected"), false);
return false;
}
auto waitEraseRes = [this]() {
int res = -10;
QEventLoop loop;
QTimer timeoutTimer;
timeoutTimer.setSingleShot(true);
timeoutTimer.start(8000);
auto conn = connect(mVesc->commands(), &Commands::lispEraseCodeRx,
[&res,&loop](bool erRes) {
res = erRes ? 1 : -1;
loop.quit();
});
connect(&timeoutTimer, SIGNAL(timeout()), &loop, SLOT(quit()));
loop.exec();
disconnect(conn);
return res;
};
mVesc->commands()->lispEraseCode(size);
int erRes = waitEraseRes();
if (erRes != 1) {
QString msg = tr("Unknown failure");
if (erRes == -10) {
msg = tr("Erase timed out");
} else if (erRes == -1) {
msg = tr("Erasing Lisp Code failed");
}
mVesc->emitMessageDialog(tr("Erase Lisp"), msg, false);
return false;
}
return true;
}
QByteArray CodeLoader::lispPackImports(QString codeStr, QString editorPath)
{
VByteArray vb;
vb.vbAppendUint16(0); // Flags: 0
vb.append(codeStr.toLocal8Bit());
if (vb.at(vb.size() - 1) != '\0') {
vb.append('\0');
}
auto dbg = [this](QString title, QString msg, bool isGood) {
if (mVesc) {
mVesc->emitMessageDialog(title, msg, isGood);
} else {
if (isGood) {
qDebug() << title << ":" << msg;
} else {
qWarning() << title << ":" << msg;
}
}
};
// Create and append data table
{
QList<QPair<QString, QByteArray> > files;
auto lines = codeStr.split("\n");
int line_num = 0;
foreach (auto line, lines) {
line_num++;
QString path;
QString tag;
bool isInvalid;
if (getImportFromLine(line, path, tag, isInvalid)) {
if (isInvalid) {
dbg(tr("Append Imports"),
tr("Line: %1: Invalid import tag.").arg(line_num),
false);
return QByteArray();
}
auto pkgErrorMsg = "If you are importing from a package in the git repository you might "
"need to update the package archive. That can be done from the the "
"VESC Packages page.";
bool isPkgImport = false;
QString pkgImportName;
if (path.startsWith("pkg::")) {
path.remove(0, 5);
auto atInd = path.indexOf("@");
if (atInd > 0) {
pkgImportName = path.mid(0, atInd);
path.remove(0, atInd + 1);
} else {
dbg(tr("Append Imports"),
tr("Line: %1: Invalid import tag.").arg(line_num),
false);
return QByteArray();
}
isPkgImport = true;
} else if (path.startsWith("pkg@")) {
path.remove(0, 4);
isPkgImport = true;
}
QFileInfo fi(editorPath + "/" + path);
if (!fi.exists()) {
fi = QFileInfo(path);
}
if (fi.exists()) {
QFile f(fi.absoluteFilePath());
if (f.open(QIODevice::ReadOnly)) {
auto fileData = f.readAll();
if (isPkgImport) {
auto pkg = unpackVescPackage(fileData);
auto imports = lispUnpackImports(pkg.lispData);
if (pkgImportName.isEmpty()) {
auto importData = imports.first.toLocal8Bit();
importData.append('\0'); // Pad with 0 in case it is a text file
files.append(qMakePair(tag, importData));
} else {
bool found = false;
for (auto i: imports.second) {
if (i.first == pkgImportName) {
auto importData = i.second;
importData.append('\0'); // Pad with 0 in case it is a text file
files.append(qMakePair(tag, importData));
found = true;
break;
}
}
if (!found) {
mVesc->emitMessageDialog(tr("Append Imports"),
tr("Tag %1 not found in package %2. %3").
arg(pkgImportName).arg(path).arg(pkgErrorMsg),
false);
return QByteArray();
}
}
} else {
fileData.append('\0'); // Pad with 0 in case it is a text file
files.append(qMakePair(tag, fileData));
}
} else {
dbg(tr("Append Imports"),
tr("Line: %1: Imported file cannot be opened.").arg(line_num),
false);
return QByteArray();
}
} else {
dbg(tr("Append Imports"),
tr("Line: %1: Imported file not found: %2. %3").
arg(line_num).arg(fi.absoluteFilePath()).arg(pkgErrorMsg),
false);
return QByteArray();
}
}
}
int file_table_size = 0;
for (auto f: files) {
file_table_size += f.first.length() + 9;
}
vb.vbAppendInt16(files.size());
int file_offset = vb.size() + file_table_size - 2;
for (auto f: files) {
// Align on 4 bytes in case this is loaded as code
while (file_offset % 4 != 0) {
file_offset++;
}
vb.vbAppendString(f.first);
vb.vbAppendInt32(file_offset);
vb.vbAppendInt32(f.second.size());
file_offset += f.second.size();
}
for (auto f: files) {
while ((vb.size() - 2) % 4 != 0) {
vb.append('\0');
}
vb.append(f.second);
}
}
return std::move(vb);
}
QPair<QString, QList<QPair<QString, QByteArray> > > CodeLoader::lispUnpackImports(QByteArray data)
{
// In case the import starts with a flag it is removed here. Note: The flag is a
// bit confusing and there is a slightly more consistent way of handling it, but
// I left it as is to not break compatibility.
if (data.size() > 2 && data[0] == '\0' && data[1] == '\0') {
data.remove(0, 2);
}
QList<QPair<QString, QByteArray> > imports;
int end = data.indexOf(QByteArray("\0", 1));
if (end > 0) {
VByteArray vb = data.mid(end + 1);
if (vb.size() > 3) {
auto num_imports = vb.vbPopFrontInt16();
if (num_imports > 0 && num_imports < 500) {
for (int i = 0;i < num_imports;i++) {
auto name = vb.vbPopFrontString();
auto offset = vb.vbPopFrontInt32();
auto len = vb.vbPopFrontInt32() - 1; // Remove 1 as the files are padded with one 0
imports.append(qMakePair(name, data.mid(offset, len)));
}
}
}
data = data.left(end);
}
return qMakePair(QString::fromLocal8Bit(data), imports);
}
bool CodeLoader::lispUpload(VByteArray vb)
{
quint16 crc = Packet::crc16((const unsigned char*)vb.constData(), uint32_t(vb.size()));
VByteArray data;
data.vbAppendUint32(vb.size() - 2);
data.vbAppendUint16(crc);
data.append(vb);
// The ESP32 partition table has 512k space for lisp scripts. The STM32
// has one 128k flash page.
auto fwParams = mVesc->getLastFwRxParams();
int max_size = 1024 * 500;
if (fwParams.hwType == HW_TYPE_VESC) {
max_size = 1024 * 120;
}
if (data.size() > max_size) {
mVesc->emitMessageDialog(tr("Upload Code"), tr("Not enough space"), false);
return false;
}
auto waitWriteRes = [this]() {
int res = -10;
QEventLoop loop;
QTimer timeoutTimer;
timeoutTimer.setSingleShot(true);
timeoutTimer.start(1000);
auto conn = connect(mVesc->commands(), &Commands::lispWriteCodeRx,
[&res,&loop](bool erRes, quint32 offset) {
(void)offset;
res = erRes ? 1 : -1;
loop.quit();
});
connect(&timeoutTimer, SIGNAL(timeout()), &loop, SLOT(quit()));
loop.exec();
disconnect(conn);
return res;
};
auto writeChunk = [this, waitWriteRes](QByteArray data, quint32 offset) {
int tries = 5;
int res = -10;
while (tries > 0) {
mVesc->commands()->lispWriteCode(data, offset);
if ((res = waitWriteRes()) >= 0) {
return res;
}
tries--;
}
return res;
};
quint32 offset = 0;
bool ok = true;
while (data.size() > 0) {
const int chunkSize = 384;
int sz = data.size() > chunkSize ? chunkSize : data.size();
if (writeChunk(data.mid(0, sz), offset) < 0) {
mVesc->emitMessageDialog(tr("Upload Code"), tr("Write failed"), false);
ok = false;
break;
}
offset += sz;
data.remove(0, sz);
}
return ok;
}
bool CodeLoader::lispUpload(QString codeStr, QString editorPath)
{
VByteArray vb = lispPackImports(codeStr, editorPath);
if (vb.isEmpty()) {
return false;
}
return lispUpload(vb);
}
bool CodeLoader::lispStream(VByteArray vb, qint8 mode)
{
if (!mVesc->isPortConnected()) {
mVesc->emitMessageDialog(tr("Stream code"), tr("Not Connected"), false);
return false;
}
auto waitWriteRes = [this]() {
int res = -10;
QEventLoop loop;
QTimer timeoutTimer;
timeoutTimer.setSingleShot(true);
timeoutTimer.start(4000);
auto conn = connect(mVesc->commands(), &Commands::lispStreamCodeRx,
[&res,&loop](qint32 offset, qint16 result) {
(void)offset;
res = result;
loop.quit();
});
connect(&timeoutTimer, SIGNAL(timeout()), &loop, SLOT(quit()));
loop.exec();
disconnect(conn);
return res;
};
qint32 offset = 0;
qint32 size_tot = vb.size();
bool ok = true;
while (vb.size() > 0) {
const int chunkSize = 384;
int sz = vb.size() > chunkSize ? chunkSize : vb.size();
mVesc->commands()->lispStreamCode(vb.mid(0, sz), offset, size_tot, mode);
auto writeRes = waitWriteRes();
if (writeRes != 0) {
mVesc->emitMessageDialog(tr("Stream Code"), tr("Stream failed. Result: %1").arg(writeRes), false);
ok = false;
break;
}
offset += sz;
vb.remove(0, sz);
}
return ok;
}
QString CodeLoader::lispRead(QWidget *parent, QString &lispPath)
{
if (!mVesc->isPortConnected()) {
mVesc->emitMessageDialog(tr("Read code"), tr("Not Connected"), false);
return "";
}
QByteArray lispData;
int lenLispLast = 0;
auto conn = connect(mVesc->commands(), &Commands::lispReadCodeRx,
[&](int lenLisp, int ofsLisp, QByteArray data) {
if (lispData.size() <= ofsLisp) {
lispData.append(data);
}
lenLispLast = lenLisp;
});
auto getLispChunk = [&](int size, int offset, int tries, int timeout) {
bool res = false;
for (int j = 0;j < tries;j++) {
mVesc->commands()->lispReadCode(size, offset);
res = Utility::waitSignal(mVesc->commands(), SIGNAL(lispReadCodeRx(int,int,QByteArray)), timeout);
if (res) {
break;
}
}
return res;
};
QString res = "";
if (getLispChunk(10, 0, 5, 1500)) {
while (lispData.size() < lenLispLast) {
int dataLeft = lenLispLast - lispData.size();
if (!getLispChunk(dataLeft > 400 ? 400 : dataLeft, lispData.size(), 5, 1500)) {
break;
}
}
if (lispData.size() == lenLispLast) {
auto unpacked = lispUnpackImports(lispData);
res = unpacked.first;
auto num_imports = unpacked.second.length();
if (num_imports > 0) {
auto reply = QMessageBox::warning(parent,
tr("Imports"),
tr("%1 imports found. Do you want to save them as files?").arg(num_imports),
QMessageBox::Yes | QMessageBox::No);
QMap<QString, QString> importPaths;
foreach (auto line, res.split('\n')) {
QString path;
QString tag;
bool isInvalid;
if (getImportFromLine(line, path, tag, isInvalid)) {
if (!isInvalid) {
importPaths.insert(tag, path);
}
}
}
if (reply == QMessageBox::Yes) {
QString dirName = QFileDialog::getExistingDirectory(parent, tr("Choose Directory"));
if (!dirName.isEmpty()) {
QFile fileLisp(dirName + "/From VESC.lisp");
if (!fileLisp.exists()) {
if (fileLisp.open(QIODevice::WriteOnly)) {
fileLisp.write(res.toUtf8());
fileLisp.close();
lispPath = QFileInfo(fileLisp).canonicalFilePath();
}
}
foreach (auto i, unpacked.second) {
QString fileName = dirName + "/" + i.first + ".bin";
if (importPaths.contains(i.first)) {
const auto &path = importPaths[i.first];
if (!path.startsWith("/") &&
!path.startsWith("\\") &&
!path.startsWith("pkg::") &&
!path.startsWith("pkg@")) {
fileName = dirName + "/" + path;
}
}
QFile file(fileName);
QFileInfo fi(file);
QDir().mkpath(fi.path());
if (file.exists()) {
auto reply = QMessageBox::question(parent,
tr("Replace File"),
tr("File %1 exists. Do you want to replace it?").arg(i.first));
if (reply != QMessageBox::Yes) {
continue;
}
}
if (!file.open(QIODevice::WriteOnly)) {
QMessageBox::critical(parent, tr("Save Import"),
"Could not open\n" + file.fileName() + "\nfor writing");
return QByteArray();
}
file.write(i.second);
file.close();
}
}
}
}
return res;
}
}
mVesc->emitMessageDialog(tr("Get Lisp"),
tr("Could not read Lisp code"),
false);
disconnect(conn);
return "";
}
bool CodeLoader::qmlErase(int size)
{
if (!mVesc) {
return false;
}
if (!mVesc->isPortConnected()) {
mVesc->emitMessageDialog(tr("Erase Qml"), tr("Not Connected"), false);
return false;
}
auto waitEraseRes = [this]() {
int res = -10;
QEventLoop loop;
QTimer timeoutTimer;
timeoutTimer.setSingleShot(true);
timeoutTimer.start(6000);
auto conn = connect(mVesc->commands(), &Commands::eraseQmluiResReceived,
[&res,&loop](bool erRes) {
res = erRes ? 1 : -1;
loop.quit();
});
connect(&timeoutTimer, SIGNAL(timeout()), &loop, SLOT(quit()));
loop.exec();
disconnect(conn);
return res;
};
mVesc->commands()->qmlUiErase(size);
int erRes = waitEraseRes();
if (erRes != 1) {
QString msg = "Unknown failure";
if (erRes == -10) {
msg = "Erase timed out";
} else if (erRes == -1) {
msg = "Erasing QMLUI failed";
}
mVesc->emitMessageDialog(tr("Erase Qml"), msg, false);
return false;
}
return true;
}
QByteArray CodeLoader::qmlCompress(QString script)
{
script.prepend("import \"qrc:/mobile\";");
script.prepend("import Vedder.vesc.vescinterface 1.0;");
return qCompress(script.toUtf8(), 9);
}
bool CodeLoader::qmlUpload(QByteArray script, bool isFullscreen)
{
auto waitWriteRes = [this]() {
int res = -10;
QEventLoop loop;
QTimer timeoutTimer;
timeoutTimer.setSingleShot(true);
timeoutTimer.start(1000);
auto conn = connect(mVesc->commands(), &Commands::writeQmluiResReceived,
[&res,&loop](bool erRes, quint32 offset) {
(void)offset;
res = erRes ? 1 : -1;
loop.quit();
});
connect(&timeoutTimer, SIGNAL(timeout()), &loop, SLOT(quit()));
loop.exec();
disconnect(conn);
return res;
};
auto writeChunk = [this, waitWriteRes](QByteArray data, quint32 offset) {
int tries = 5;
int res = -10;
while (tries > 0) {
mVesc->commands()->qmlUiWrite(data, offset);
if ((res = waitWriteRes()) >= 0) {
return res;
}
tries--;
}
return res;
};
VByteArray vb;
vb.vbAppendUint16(isFullscreen ? 2 : 1);
vb.append(script);
quint16 crc = Packet::crc16((const unsigned char*)vb.constData(),
uint32_t(vb.size()));
VByteArray data;
data.vbAppendUint32(vb.size() - 2);
data.vbAppendUint16(crc);
data.append(vb);
if (data.size() > (1024 * 120)) {
mVesc->emitMessageDialog(tr("Upload Qml"), tr("Not enough space"), false);
return false;
}
quint32 offset = 0;
bool ok = true;
while (data.size() > 0) {
const int chunkSize = 384;
int sz = data.size() > chunkSize ? chunkSize : data.size();
if (writeChunk(data.mid(0, sz), offset) < 0) {
mVesc->emitMessageDialog(tr("Upload Qml"), tr("Qml write failed"), false);
ok = false;
break;
}
offset += sz;
data.remove(0, sz);
}
return ok;
}
QByteArray CodeLoader::packVescPackage(VescPackage pkg)
{
VByteArray data;
data.vbAppendString("VESC Packet");
if (!pkg.name.isEmpty()) {
auto dataRaw = pkg.name.toUtf8();
data.vbAppendString("name");
data.vbAppendInt32(dataRaw.size());
data.append(dataRaw);
}
if (!pkg.description.isEmpty()) {
auto dataRaw = pkg.description.toUtf8();
data.vbAppendString("description");
data.vbAppendInt32(dataRaw.size());
data.append(dataRaw);
}
if (!pkg.lispData.isEmpty()) {
data.vbAppendString("lispData");
data.vbAppendInt32(pkg.lispData.size());
data.append(pkg.lispData);
}
if (!pkg.qmlFile.isEmpty()) {
auto dataRaw = pkg.qmlFile.toUtf8();
data.vbAppendString("qmlFile");
data.vbAppendInt32(dataRaw.size());
data.append(dataRaw);
}
data.vbAppendString("qmlIsFullscreen");
data.vbAppendInt32(1);
data.vbAppendInt8(pkg.qmlIsFullscreen);
return qCompress(data, 9);
}
VescPackage CodeLoader::unpackVescPackage(QByteArray data)
{
VescPackage pkg;
VByteArray vb(qUncompress(data));
// Yes, packet is a typo and should say package...
// That does not matter in practice and changing that now breaks
// compatibility.
if (vb.vbPopFrontString() != "VESC Packet") {
qWarning() << "Invalid VESC Packet";
return pkg;
}
pkg.compressedData = data;
while (!vb.isEmpty()) {
QString name = vb.vbPopFrontString();
if (name.isEmpty()) {
qWarning() << "Empty name";
break;
}
if (name == "name") {
auto len = vb.vbPopFrontInt32();
auto dataRaw = vb.left(len);
vb.remove(0, len);
pkg.name = QString::fromUtf8(dataRaw);
pkg.loadOk = true;
} else if (name == "description") {
auto len = vb.vbPopFrontInt32();
auto dataRaw = vb.left(len);
vb.remove(0, len);
pkg.description = QString::fromUtf8(dataRaw);
} else if (name == "lispData") {
auto len = vb.vbPopFrontInt32();
auto dataRaw = vb.left(len);
vb.remove(0, len);
pkg.lispData = dataRaw;
} else if (name == "qmlFile") {
auto len = vb.vbPopFrontInt32();
auto dataRaw = vb.left(len);
vb.remove(0, len);
pkg.qmlFile = QString::fromUtf8(dataRaw);
} else if (name == "qmlIsFullscreen") {
vb.vbPopFrontInt32(); // Discard length
pkg.qmlIsFullscreen = vb.vbPopFrontInt8();
} else {
// Unknown identifier, skip
auto len = vb.vbPopFrontInt32();
vb.remove(0, len);
}
}
return pkg;
}
bool CodeLoader::installVescPackage(VescPackage pkg)
{
bool res = true;
QByteArray qml;
if (res && !pkg.qmlFile.isEmpty()) {
qml = qmlCompress(pkg.qmlFile);
res = qmlErase(qml.size() + 100);
}
if (res && !pkg.qmlFile.isEmpty()) {
res = qmlUpload(qml, pkg.qmlIsFullscreen);
}
if (res && !pkg.lispData.isEmpty()) {
res = lispErase(pkg.lispData.size() + 100);
}
if (res && !pkg.lispData.isEmpty()) {
res = lispUpload(VByteArray(pkg.lispData));
}
if (res && !pkg.lispData.isEmpty()) {
mVesc->commands()->lispSetRunning(1);
}
Utility::sleepWithEventLoop(500);
mVesc->reloadFirmware();
return res;
}
bool CodeLoader::installVescPackage(QByteArray data)
{
return installVescPackage(unpackVescPackage(data));
}
bool CodeLoader::installVescPackageFromPath(QString path)
{
if (path.startsWith("file:/")) {
path.remove(0, 6);
}
QFile f(path);
if (!f.open(QIODevice::ReadOnly)) {
mVesc->emitMessageDialog(tr("Write Package"),
tr("Could not open package file for reading."),
false, false);
return false;
}
return installVescPackage(f.readAll());
}
QVariantList CodeLoader::reloadPackageArchive()
{
QVariantList res;
QString appDataLoc = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
if(!QDir(appDataLoc).exists()) {
QDir().mkpath(appDataLoc);
}
QString path = appDataLoc + "/vesc_pkg_all.rcc";
QFile file(path);
if (file.exists()) {
QResource::unregisterResource(path);
QResource::registerResource(path);
QString pkgDir = "://vesc_packages";
QDirIterator it(pkgDir);
while (it.hasNext()) {
QFileInfo fi(it.next());
QDirIterator it2(fi.absoluteFilePath());
while (it2.hasNext()) {
QFileInfo fi2(it2.next());
if (fi2.absoluteFilePath().toLower().endsWith(".vescpkg")) {
QString name = fi2.fileName();
QFile f(fi2.absoluteFilePath());
if (f.open(QIODevice::ReadOnly)) {
auto pkg = unpackVescPackage(f.readAll());
name = pkg.name;
pkg.isLibrary = fi2.absoluteFilePath().startsWith("://vesc_packages/lib_");
res.append(QVariant::fromValue(pkg));
}
}
}
}
}
return res;
}
bool CodeLoader::downloadPackageArchive()
{
bool res = false;
QUrl url("http://home.vedder.se/vesc_pkg/vesc_pkg_all.rcc");
QNetworkAccessManager manager;
QNetworkRequest request(url);
QNetworkReply *reply = manager.get(request);
connect(reply, &QNetworkReply::downloadProgress, [this](qint64 bytesReceived, qint64 bytesTotal) {
emit downloadProgress(bytesReceived, bytesTotal);
});
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
if (reply->error() == QNetworkReply::NoError) {
QString appDataLoc = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
if(!QDir(appDataLoc).exists()) {
QDir().mkpath(appDataLoc);
}
QString path = appDataLoc + "/vesc_pkg_all.rcc";
QResource::unregisterResource(path);
QFile file(path);
if (file.open(QIODevice::WriteOnly)) {
file.write(reply->readAll());
file.close();
res = true;
}
// Remove image cache
QString cacheLoc = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
QDir(cacheLoc + "/img/").removeRecursively();
}
reply->abort();
reply->deleteLater();
return res;
}
bool CodeLoader::getImportFromLine(QString line, QString &path, QString &tag, bool &isInvalid)
{
bool res = false;
isInvalid = false;
while (line.startsWith(" ")) {
line.remove(0, 1);
}
while (line.startsWith("( ")) {
line.remove(1, 1);
}
if (line.startsWith("(import ", Qt::CaseInsensitive)) {
int start = line.indexOf("\"");
int end = line.lastIndexOf("\"");
if (start > 0 && end > start) {
path = line.mid(start + 1, end - start - 1);
tag = line.mid(end + 1).replace("\r", "").replace(" ", "").replace(")", "").replace("'", "");
if (tag.indexOf(";") >= 0) {
tag = tag.mid(0, tag.indexOf(";"));
}
} else {
isInvalid = true;
}
res = true;
}
if (path.isEmpty() || tag.isEmpty()) {
isInvalid = true;
}
return res;
}