-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmainwindow.cpp
2246 lines (1937 loc) · 94.7 KB
/
mainwindow.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
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSplashScreen>
const QColor MainWindow::RED_LIGHT_OFF = QColor(96, 32, 32);
const QColor MainWindow::YELLOW_LIGHT_OFF = QColor(96, 96, 32);
const QColor MainWindow::GREEN_LIGHT_OFF = QColor(32, 96, 32);
const QColor MainWindow::RED_LIGHT_ON = QColor(255, 64, 64);
const QColor MainWindow::YELLOW_LIGHT_ON = QColor(223, 223, 64);
const QColor MainWindow::GREEN_LIGHT_ON = QColor(64, 255, 64);
MainWindow::MainWindow(QString peerAddress, QString peerPassword, QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, peerAddress(peerAddress)
, peerPassword(peerPassword)
{
ui->setupUi(this);
qApp->installEventFilter(this);
qRegisterMetaType<QVector<int> >("QVector<int>");
QPixmap startUpSplashImage(":/images/startup_splash.jpg");
int startUpSplashProgressBarValue = 0;
startUpSplash = new QSplashScreen(startUpSplashImage);
QVBoxLayout *startUpSplashLayout = new QVBoxLayout(startUpSplash);
//startUpSplashLayout->setMargin(0);
startUpSplashLayout->setSpacing(0);
startUpSplashLayout->setAlignment(Qt::AlignBottom);
startUpSplashLabel = new QLabel(QString("Starting FastECU..."));
startUpSplashLabel->setStyleSheet("QLabel { background-color : black; color : white; }");
startUpSplashLayout->addWidget(startUpSplashLabel);
startUpSplashProgressBar = new QProgressBar();
startUpSplashProgressBar->setMinimum(0);
startUpSplashProgressBar->setMaximum(100);
startUpSplashProgressBar->setValue(startUpSplashProgressBarValue);
startUpSplashProgressBar->setFixedHeight(16);
startUpSplashLayout->addWidget(startUpSplashProgressBar);
//startUpSplash->setEnabled(false);
startUpSplash->show();
// Move splashscreen to the center of the screen
QScreen *screen = QGuiApplication::primaryScreen();
QRect screenGeometry = screen->geometry();
startUpSplash->move(screenGeometry.center() - startUpSplash->rect().center());
QCoreApplication::processEvents(QEventLoop::AllEvents, 10);
#if defined Q_OS_UNIX
//send_log_window_message("Running on Linux Desktop ", true, false);
//serialPort = serialPortLinux;
serial_port_prefix = "/dev/";
#elif defined Q_OS_WIN32
//serialPort = serialPortWindows;
serial_port_prefix = "";
#endif
#if Q_PROCESSOR_WORDSIZE == 4
//qDebug() << "32-bit executable";
//send_log_window_message("32-bit executable", false, true);
#elif Q_PROCESSOR_WORDSIZE == 8
//qDebug() << "64-bit executable";
//send_log_window_message("64-bit executable", false, true);
#endif
setSplashScreenProgress("Reading config files...", 10);
fileActions = new FileActions();
configValues = &fileActions->ConfigValuesStruct;
software_name = configValues->software_name;
software_title = configValues->software_title;
software_version = configValues->software_version;
this->setWindowTitle(software_title + " " + software_version);
//configValues->base_directory.append("/" + configValues->software_version);
fileActions->check_config_dir(configValues);
QThread *syslog_thread = new QThread();
syslogger = new SystemLogger(configValues->syslog_files_directory, software_name, software_version);
syslogger->moveToThread(syslog_thread);
QObject::connect(this, &MainWindow::LOG_E, syslogger, &SystemLogger::log_messages);
QObject::connect(this, &MainWindow::LOG_W, syslogger, &SystemLogger::log_messages);
QObject::connect(this, &MainWindow::LOG_I, syslogger, &SystemLogger::log_messages);
QObject::connect(this, &MainWindow::LOG_D, syslogger, &SystemLogger::log_messages);
QObject::connect(this, &MainWindow::enable_log_write_to_file, syslogger, &SystemLogger::enable_log_write_to_file);
QObject::connect(syslogger, &SystemLogger::send_message_to_log_window, this, &MainWindow::send_message_to_log_window);
QObject::connect(syslogger, &SystemLogger::finished, syslog_thread, &QThread::quit);
QObject::connect(syslogger, &SystemLogger::finished, syslogger, &SystemLogger::deleteLater);
QObject::connect(syslog_thread, &QThread::finished, syslog_thread, &QThread::deleteLater);
QObject::connect(syslog_thread, &QThread::started, syslogger, &SystemLogger::run);
syslog_thread->start();
QObject::connect(fileActions, &FileActions::LOG_E, syslogger, &SystemLogger::log_messages);
QObject::connect(fileActions, &FileActions::LOG_W, syslogger, &SystemLogger::log_messages);
QObject::connect(fileActions, &FileActions::LOG_I, syslogger, &SystemLogger::log_messages);
QObject::connect(fileActions, &FileActions::LOG_D, syslogger, &SystemLogger::log_messages);
emit enable_log_write_to_file(true);
//fileActions->check_config_dir(configValues);
configValues = fileActions->read_config_file(configValues);
fileActions->read_protocols_file(configValues);
qDebug() << "ECU protocols read";
qDebug() << "Protocols ID:" << configValues->flash_protocol_selected_id + "/" + QString::number(configValues->flash_protocol_id.length());
if (configValues->flash_protocol_selected_id.toInt() > configValues->flash_protocol_id.length())
configValues->flash_protocol_selected_id = "0";
configValues->flash_protocol_selected_make = configValues->flash_protocol_make.at(configValues->flash_protocol_selected_id.toInt());
configValues->flash_protocol_selected_mcu = configValues->flash_protocol_mcu.at(configValues->flash_protocol_selected_id.toInt());
configValues->flash_protocol_selected_checksum = configValues->flash_protocol_checksum.at(configValues->flash_protocol_selected_id.toInt());
configValues->flash_protocol_selected_model = configValues->flash_protocol_model.at(configValues->flash_protocol_selected_id.toInt());
configValues->flash_protocol_selected_version = configValues->flash_protocol_version.at(configValues->flash_protocol_selected_id.toInt());
configValues->flash_protocol_selected_protocol_name = configValues->flash_protocol_protocol_name.at(configValues->flash_protocol_selected_id.toInt());
configValues->flash_protocol_selected_description = configValues->flash_protocol_description.at(configValues->flash_protocol_selected_id.toInt());
//configValues->flash_protocol_selected_flash_transport = configValues->flash_protocol_flash_transport.at(configValues->flash_protocol_selected_id.toInt());
//configValues->flash_protocol_selected_log_transport = configValues->flash_protocol_log_transport.at(configValues->flash_protocol_selected_id.toInt());
//configValues->flash_protocol_selected_log_protocol = configValues->flash_protocol_log_protocol.at(configValues->flash_protocol_selected_id.toInt());
qDebug() << configValues->flash_protocol_selected_make;
qDebug() << configValues->flash_protocol_selected_mcu;
qDebug() << configValues->flash_protocol_selected_checksum;
qDebug() << configValues->flash_protocol_selected_model;
qDebug() << configValues->flash_protocol_selected_version;
qDebug() << configValues->flash_protocol_selected_protocol_name;
qDebug() << configValues->flash_protocol_selected_description;
qDebug() << configValues->flash_protocol_selected_flash_transport;
qDebug() << configValues->flash_protocol_selected_log_transport;
qDebug() << configValues->flash_protocol_selected_log_protocol;
qDebug() << "ECU protocols set";
QRect qrect = MainWindow::geometry();
if (configValues->window_width != "maximized" && configValues->window_height != "maximized")
this->setGeometry(qrect.x(), qrect.y(), configValues->window_width.toInt(), configValues->window_height.toInt());
else
this->setWindowState(Qt::WindowMaximized);
setSplashScreenProgress("Preparing ROM definitions...", 10);
if (!configValues->romraider_definition_files.length() && !configValues->ecuflash_definition_files_directory.length())
QMessageBox::warning(this, tr("Ecu definition file"), "No definition file(s), use 'Settings' in 'Edit' menu to choose file(s)");
setSplashScreenProgress("Preparing EcuFlash ROM definitions...", 10);
fileActions->create_ecuflash_def_id_list(configValues);
setSplashScreenProgress("Preparing RomRaider ROM definitions...", 10);
fileActions->create_romraider_def_id_list(configValues);
if (QDir(configValues->kernel_files_directory).exists()){
QDir dir(configValues->kernel_files_directory);
QStringList nameFilter("*.bin");
QStringList txtFilesAndDirectories = dir.entryList(nameFilter);
//qDebug() << txtFilesAndDirectories;
}
setSplashScreenProgress("Setting up menus...", 10);
QSignalMapper *mapper = fileActions->read_menu_file(ui->menubar, ui->toolBar);
#if QT_VERSION >=0x060000
connect(mapper, SIGNAL(mappedString(QString)), this, SLOT(menu_action_triggered(QString)));
#elif QT_VERSION >=0x050000
connect(mapper, SIGNAL(mapped(QString)), this, SLOT(menu_action_triggered(QString)));
#endif
/*
for (int i = 0; i < configValues->calibration_files.count(); i++)
{
QString filename = configValues->calibration_files.at(i);
bool result = false;
//qDebug() << "Open file" << filename;
//ecuCalDef[ecuCalDefIndex] = new FileActions::EcuCalDefStructure;
result = open_calibration_file(filename);
if (result)
{
configValues->calibration_files.removeAt(i);
fileActions->saveConfigFile();
i--;
}
}
if(ecuCalDefIndex > 0)
{
const QModelIndex index = ui->calibrationFilesTreeWidget->selectionModel()->currentIndex();
emit ui->calibrationFilesTreeWidget->clicked(index);
}
*/
connect(ui->switchBoxWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(change_switch_values()));
connect(ui->logBoxWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(change_digital_values()));
connect(ui->mdiArea, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(change_gauge_values()));
//connect(ui->action_Preferences, SIGNAL(triggered()), this, SLOT(openPreferences()));
connect(ui->calibrationFilesTreeWidget, SIGNAL(itemClicked(QTreeWidgetItem *,int)), this, SLOT(calibration_files_treewidget_item_selected(QTreeWidgetItem*)));
connect(ui->calibrationDataTreeWidget, SIGNAL(itemClicked(QTreeWidgetItem *,int)), this, SLOT(calibration_data_treewidget_item_selected(QTreeWidgetItem*)));
connect(ui->calibrationDataTreeWidget, SIGNAL(itemExpanded(QTreeWidgetItem*)), this, SLOT(calibration_data_treewidget_item_expanded(QTreeWidgetItem*)));
connect(ui->calibrationDataTreeWidget, SIGNAL(itemCollapsed(QTreeWidgetItem*)), this, SLOT(calibration_data_treewidget_item_collapsed(QTreeWidgetItem*)));
connect(calibrationTreeWidget, SIGNAL(closeRom()), this, SLOT(close_calibration()));
setSplashScreenProgress("Setting up statusbar...", 10);
status_bar_connection_label->setMargin(5);
//status_bar_connection_label->setStyleSheet("QLabel { background-color : red; color : white; }");
set_status_bar_label(false, false, "");
status_bar_ecu_label->setText("");
status_bar_ecu_label->setMargin(5);
status_bar_ecu_label->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
//status_bar_ecu_label->setStyleSheet("QLabel { background-color : red; color : white; }");
QWidget* status_bar_spacer = new QWidget();
status_bar_spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
statusBar()->addWidget(status_bar_connection_label);
statusBar()->addWidget(status_bar_spacer);
statusBar()->addPermanentWidget(status_bar_ecu_label);
statusBar()->setSizeGripEnabled(true);
setSplashScreenProgress("Preparing up treewidget...", 10);
ui->calibrationFilesTreeWidget->setHeaderLabel("Calibration Files");
ui->calibrationDataTreeWidget->setHeaderLabel("Calibration Data");
ui->calibrationDataTreeWidget->resizeColumnToContents(0);
ui->calibrationDataTreeWidget->resizeColumnToContents(1);
ui->calibrationFilesTreeWidget->setMinimumHeight(125);
ui->calibrationFilesTreeWidget->minimumHeight();
ui->calibrationFilesTreeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->calibrationFilesTreeWidget, SIGNAL(customContextMenuRequested(QPoint)), SLOT(custom_menu_requested(QPoint)));
//ui->splitter->setStretchFactor(2, 1);
ui->splitter->setSizes(QList<int>({125, INT_MAX}));
setSplashScreenProgress("Preparing remote connection...", 10);
//Splash screen
netSplash = new QSplashScreen();
QVBoxLayout *netSplashLayout = new QVBoxLayout(netSplash);
netSplashLayout->setAlignment(Qt::AlignCenter);
QLabel *netSplashLabel = new QLabel(QString("Waiting for peer "+peerAddress+"..."), netSplash);
netSplashLabel->setAlignment(Qt::AlignCenter);
QProgressBar *netSplashProgressBar = new QProgressBar(netSplash);
netSplashProgressBar->setAlignment(Qt::AlignCenter);
netSplashProgressBar->setMinimum(0);
//Number of network connection stages
netSplashProgressBar->setMaximum(2);
netSplashProgressBar->setValue(0);
QPushButton *btnCloseApp = new QPushButton("Close app", netSplash);
netSplashLayout->addWidget(netSplashLabel);
netSplashLayout->addWidget(netSplashProgressBar);
netSplashLayout->addWidget(btnCloseApp);
//splash->setLayout(layout);
netSplash->resize(350,50);
//Show it in remote mode only
//Prepare for remote mode
if (!peerAddress.isEmpty())
{
netSplash->show();
// Move splashscreen to the center of the screen
QScreen *screen = QGuiApplication::primaryScreen();
QRect screenGeometry = screen->geometry();
netSplash->move(screenGeometry.center() - netSplash->rect().center());
QCoreApplication::processEvents(QEventLoop::AllEvents, 10);
QString wt = this->windowTitle();
if (peerAddress.length() > 0)
wt += " - Remote Connection to " + peerAddress;
this->setWindowTitle(wt);
}
//Add option to close app while waiting for network connection
QObject::connect(btnCloseApp, &QPushButton::released, this, [&]()
{
netSplashLabel->setText("Closing app, please wait...");
exit(1);
});
//Init may take a long time due to network
//Do it in a non-blocking way
//This timer will timeout as fast as possible
//processing events
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, [&]()
{
QApplication::processEvents();
});
timer->start();
serial = new SerialPortActions(peerAddress, peerPassword, nullptr, this);
remote_utility = new RemoteUtility(peerAddress, peerPassword, nullptr, this);
if (!serial->isDirectConnection())
{
netSplashProgressBar->setValue(0);
netSplashProgressBar->setFormat("Connecting to J2534 and serial devices...");
serial->waitForSource();
netSplashProgressBar->setValue(1);
netSplashProgressBar->setFormat("Connecting to utility functions...");
remote_utility->waitForSource();
netSplashProgressBar->setValue(2);
}
external_logger("Connection successfull.");
timer->stop();
netSplash->close();
timer->deleteLater();
connect(serial, &SerialPortActions::stateChanged,
this, &MainWindow::network_state_changed, Qt::DirectConnection);
connect(remote_utility, &RemoteUtility::stateChanged,
this, &MainWindow::network_state_changed, Qt::DirectConnection);
setSplashScreenProgress("Setting up toolbar...", 10);
toolbar_item_size.setWidth(configValues->toolbar_iconsize.toInt());
toolbar_item_size.setHeight(configValues->toolbar_iconsize.toInt());
ui->toolBar->setIconSize(toolbar_item_size);
QWidget* spacer = new QWidget();
spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
ui->toolBar->addWidget(spacer);
ui->toolBar->addSeparator();
QPushButton *select_protocol_button = new QPushButton();
select_protocol_button->setText("Select protocol");
//car_make_button->setMargin(10);
select_protocol_button->setFixedHeight(toolbar_item_size.height());
ui->toolBar->addWidget(select_protocol_button);
connect(select_protocol_button, SIGNAL(clicked(bool)), this, SLOT(select_protocol()));
QPushButton *select_vehicle_button = new QPushButton();
select_vehicle_button->setText("Select vehicle");
//car_make_button->setMargin(10);
select_vehicle_button->setFixedHeight(toolbar_item_size.height());
ui->toolBar->addWidget(select_vehicle_button);
connect(select_vehicle_button, SIGNAL(clicked(bool)), this, SLOT(select_vehicle()));
flash_transport_list = new QComboBox();
flash_transport_list->setFixedHeight(toolbar_item_size.height());
flash_transport_list->setFixedWidth(90);
flash_transport_list->setObjectName("flash_transport_list");
ui->toolBar->addWidget(flash_transport_list);
ui->toolBar->addSeparator();
QLabel *log_transport = new QLabel("Log:");
log_transport->setMargin(10);
ui->toolBar->addWidget(log_transport);
log_transport_list = new QComboBox();
log_transport_list->setFixedHeight(toolbar_item_size.height());
log_transport_list->setFixedWidth(90);
log_transport_list->setObjectName("log_transport_list");
ui->toolBar->addWidget(log_transport_list);
flash_transports = create_flash_transports_list();
log_transports = create_log_transports_list();
connect(flash_transport_list, SIGNAL(currentIndexChanged(int)), this, SLOT(flash_transport_changed()));
connect(log_transport_list, SIGNAL(currentIndexChanged(int)), this, SLOT(log_transport_changed()));
//ui->toolBar->addSeparator();
QLabel *log_select = new QLabel();
log_select->setMargin(5);
ui->toolBar->addWidget(log_select);
ecu_radio_button = new QRadioButton("ECU");
ecu_radio_button->setChecked(true);
ui->toolBar->addWidget(ecu_radio_button);
tcu_radio_button = new QRadioButton("TCU");
ui->toolBar->addWidget(tcu_radio_button);
ui->toolBar->addSeparator();
QLabel *serial_port_select = new QLabel("Port:");
serial_port_select->setMargin(10);
ui->toolBar->addWidget(serial_port_select);
serial_port_list = new QComboBox();
serial_port_list->setFixedHeight(toolbar_item_size.height());
serial_port_list->setFixedWidth(180);
serial_port_list->setObjectName("serial_port_list");
serial_ports = serial->check_serial_ports();
for (int i = 0; i < serial_ports.length(); i++)
{
serial_port_list->addItem(serial_ports.at(i));
if (configValues->serial_port == serial_ports.at(i).split(" - ").at(0))
serial_port_list->setCurrentIndex(i);
}
ui->toolBar->addWidget(serial_port_list);
refresh_serial_port_list = new QPushButton();
refresh_serial_port_list->setIcon(QIcon(":/icons/view-refresh.png"));
refresh_serial_port_list->setFixedHeight(toolbar_item_size.height());
refresh_serial_port_list->setFixedWidth(toolbar_item_size.height());
//refresh_serial_port_list->setIconSize(toolbar_item_size);
connect(refresh_serial_port_list, SIGNAL(clicked(bool)), this, SLOT(check_serial_ports()));
ui->toolBar->addWidget(refresh_serial_port_list);
logValues = &fileActions->LogValuesStruct;
logValues = fileActions->read_logger_definition_file();
logBoxes = new LogBox();
if (logValues != NULL)
{
int switchBoxCount = 20;
int logBoxCount = 12;
//update_logboxes(log_protocol);
update_logboxes(configValues->flash_protocol_selected_log_protocol);
}
serial_port = serial_port_prefix + configValues->serial_port;
serial_port_baudrate = default_serial_port_baudrate;
serial->set_serial_port_baudrate(serial_port_baudrate);
serial->set_serial_port(serial_port);
/*
serial_poll_timer = new QTimer(this);
serial_poll_timer->setInterval(serial_poll_timer_timeout);
connect(serial_poll_timer, SIGNAL(timeout()), this, SLOT(open_serial_port()));
serial_poll_timer->start();
ssm_init_poll_timer = new QTimer(this);
ssm_init_poll_timer->setInterval(ssm_init_poll_timer_timeout);
connect(ssm_init_poll_timer, SIGNAL(timeout()), this, SLOT(ecu_init()));
ssm_init_poll_timer->start();
*/
logging_poll_timer = new QTimer(this);
logging_poll_timer->setInterval(logging_poll_timer_timeout);
connect(logging_poll_timer, SIGNAL(timeout()), this, SLOT(log_ssm_values()));
logparams_poll_timer = new QTimer(this);
logparams_poll_timer->setInterval(logparams_poll_timer_timeout);
connect(logparams_poll_timer, SIGNAL(timeout()), this, SLOT(read_log_serial_data()));
log_speed_timer = new QElapsedTimer();
log_file_timer = new QElapsedTimer();
if(ecuCalDefIndex > 0)
{
const QModelIndex index = ui->calibrationFilesTreeWidget->selectionModel()->currentIndex();
emit ui->calibrationFilesTreeWidget->clicked(index);
}
emit log_transport_list->currentIndexChanged(log_transport_list->currentIndex());
status_bar_ecu_label->setText(configValues->flash_protocol_selected_description + " ");
set_flash_arrow_state();
startUpSplashLabel->setText("Starting FastECU GUI...");
startUpSplashProgressBarValue = startUpSplashProgressBar->value();
while (startUpSplashProgressBarValue < 100)
{
startUpSplashProgressBar->setValue(startUpSplashProgressBarValue += 1);
delay(10);
QCoreApplication::processEvents(QEventLoop::AllEvents, 10);
}
startUpSplash->close();
netSplash->deleteLater();
/*
// AES-128 ECB examples start
qDebug() << "Solving challenge...";
//QByteArray key = { "\x46\x9a\x20\xab\x30\x8d\x5c\xa6\x4b\xcd\x5b\xbe\x53\x5b\xd8\x5f" };
QByteArray key = { "\x7D\x89\xDD\xE1\xC9\x5A\x22\x4E\xD7\x23\xE0\x44\x96\xF4\xC0\xAE" };
//QByteArray challenge = { "\x5f\x75\x8c\x11\x92\xdc\x56\xfb\x69\xe3\x40\x2d\x83\xfb\x75\xe4" };
QByteArray challenge = { "\x3D\xA9\x19\x57\x6E\x88\xD3\xBF\x25\x2C\x02\xC4\x4F\x70\x0B\x63" };
QByteArray response;
qDebug() << "*********";
response.append(aes_ecb_test(challenge, key));
key = "\x5C\x9F\x97\xAD\xE5\x1D\xA6\xD0\x60\x9D\x49\xBB\x05\xA4\x17\xE9";
response.append(aes_ecb_test(challenge, key));
key = "\xC9\x22\x70\xE6\x64\x63\x39\x54\x07\xF6\xC3\x01\x4D\xC8\x90\xB0";
response.append(aes_ecb_test(challenge, key));
key = "\x66\xD5\x36\x4A\x0D\x2D\x45\xC6\x7E\x8D\xB1\x20\xED\x47\x14\x38";
response.append(aes_ecb_test(challenge, key));
key = "\x37\x49\x0E\x2C\x46\xC5\x7F\x8C\xB2\x2F\xEC\x48\x13\x39\x65\xD6"; qDebug() << "*********";
response.append(aes_ecb_test(challenge, key));
//aes_ecb_example();
// AES-128 ECB examples end
*/
emit LOG_I("FastECU initialized", true, true);
}
MainWindow::~MainWindow()
{
if (logging_state)
{
logging_state = false;
log_params_request_started = false;
log_ssm_values();
delay(200);
}
//ssm_init_poll_timer->stop();
//serial_poll_timer->stop();
delete ui;
}
QByteArray MainWindow::aes_ecb_test(QByteArray challenge, QByteArray key)
{
// Clean: "5f758c1192dc56fb69e3402d83fb75e4"
// Key : "469a20ab308d5ca64bcd5bbe535bd85f"
// Enc : "b8f73be3b1dcc30e93d88f0af5a860ca"
Cipher cipher;
unsigned char encrypted[64];
unsigned char decrypted[64];
char *data = challenge.data();
char *cKey = key.data();
int decrypted_len, encrypted_len;
// Encrypt the original data
encrypted_len = cipher.encrypt_aes128_ecb((unsigned char*)data, strlen(data), (unsigned char*)cKey, encrypted);
// Decrypt the encrypted data
decrypted_len = cipher.decrypt_aes128_ecb(encrypted, encrypted_len, (unsigned char*)cKey, decrypted);
QByteArray challengeReply(QByteArray::fromRawData((const char*)encrypted, 16));
qDebug() << "Key:";
qDebug() << parse_message_to_hex(key);
qDebug() << "Challenge:";
qDebug() << parse_message_to_hex(challenge);
qDebug() << "Decrypted:";
qDebug() << parse_message_to_hex(challengeReply);
qDebug() << "Length:" << encrypted_len;
QByteArray challengeDecrypt(QByteArray::fromRawData((const char*)decrypted, 16));
return challengeReply;
}
void MainWindow::network_state_changed(QRemoteObjectReplica::State state, QRemoteObjectReplica::State oldState)
{
if (state == QRemoteObjectReplica::Valid)
{
qDebug() << "Network connection established";
}
else if (oldState == QRemoteObjectReplica::Valid)
{
if (!restartQuestionActive.tryLock())
return;
qDebug() << "Network connection lost, reconnecting...";
QMessageBox msgBox;
msgBox.setText("Network connection lost.");
msgBox.setInformativeText("Do you want to restart application and try to connect again?");
QPushButton *restartButton = msgBox.addButton(tr("Restart"), QMessageBox::YesRole);
QPushButton *quitButton = msgBox.addButton(tr("Quit"), QMessageBox::NoRole);
msgBox.addButton(tr("Continue work"), QMessageBox::NoRole);
msgBox.setIcon(QMessageBox::Warning);
msgBox.setDetailedText("Press Restart to restart application. All your unsaved progress will be lost.\n\n"
"Press Quit to quit application. All your unsaved progress will be lost.\n\n"
"Press Continue work to return to application and save your progress. "
"In this case all ECU operations will be unavailable until you restart application.");
msgBox.exec();
if (msgBox.clickedButton() == restartButton)
qApp->exit(RESTART_CODE);
else if (msgBox.clickedButton() == quitButton)
qApp->exit(1);
restartQuestionActive.unlock();
}
}
void MainWindow::aes_ecb_example()
{
// Clean: "5f758c1192dc56fb69e3402d83fb75e4"
// Key : "469a20ab308d5ca64bcd5bbe535bd85f"
// Enc : "b8f73be3b1dcc30e93d88f0af5a860ca"
qDebug() << "aes_ecb_example:";
Cipher cipher;
// A 128 bit key
unsigned char key[] = { 0x46, 0x9a, 0x20, 0xab, 0x30, 0x8d, 0x5c, 0xa6, 0x4b, 0xcd, 0x5b, 0xbe, 0x53, 0x5b, 0xd8, 0x5f };
QByteArray engine_key_1((const char*)key, ARRAYSIZE(key));
// Message to be encrypted
unsigned char data[] = { 0x5f, 0x75, 0x8c, 0x11, 0x92, 0xdc, 0x56, 0xfb, 0x69, 0xe3, 0x40, 0x2d, 0x83, 0xfb, 0x75, 0xe4 };
QByteArray ch_data((const char*)data, ARRAYSIZE(data));
qDebug() << "Received challenge:";
qDebug() << parse_message_to_hex(ch_data);
QByteArray output;
qDebug() << "Reply to challenge:";
output = cipher.encrypt_aes128_ecb(ch_data, engine_key_1);
qDebug() << parse_message_to_hex(output);
qDebug() << "Decrypted data is:";
output = cipher.decrypt_aes128_ecb(output, engine_key_1);
qDebug() << parse_message_to_hex(output);
}
void MainWindow::SetComboBoxItemEnabled(QComboBox * comboBox, int index, bool enabled)
{
auto * model = qobject_cast<QStandardItemModel*>(comboBox->model());
assert(model);
if(!model) return;
auto * item = model->item(index);
assert(item);
if(!item) return;
item->setEnabled(enabled);
}
QStringList MainWindow::create_flash_transports_list()
{
QStringList flash_protocols;
flash_protocols.append(configValues->flash_protocol_flash_transport.at(configValues->flash_protocol_selected_id.toInt()).split(","));
flash_transport_list->clear();
for (int i = 0; i < flash_protocols.length(); i++){
flash_transport_list->addItem(flash_protocols.at(i));
if (configValues->flash_protocol_selected_flash_transport == flash_protocols.at(i))
flash_transport_list->setCurrentIndex(i);
}
return flash_protocols;
}
QStringList MainWindow::create_log_transports_list()
{
QStringList log_transports;
log_transports.append(configValues->flash_protocol_log_transport.at(configValues->flash_protocol_selected_id.toInt()).split(","));
log_transport_list->clear();
for (int i = 0; i < log_transports.length(); i++){
log_transport_list->addItem(log_transports.at(i));
if (configValues->flash_protocol_selected_flash_transport == log_transports.at(i))
{
log_transport_list->setCurrentIndex(i);
protocol = "SSM";
}
}
//if (car_model_list->currentText() == "Subaru")
//protocol = "SSM";
return log_transports;
}
void MainWindow::select_protocol()
{
//qDebug() << "Select protocol";
ProtocolSelect protocolSelect(configValues);
connect(&protocolSelect, SIGNAL(finished (int)), this, SLOT(select_protocol_finished(int)));
protocolSelect.exec();
//QRect screenGeometry = this->geometry();
//protocolSelect.move(screenGeometry.center() - protocolSelect.rect().center());
//QCoreApplication::processEvents(QEventLoop::AllEvents, 10);
qDebug() << "Selected protocol:" << configValues->flash_protocol_selected_id;
//status_bar_ecu_label->setText(configValues->flash_protocol_selected_description + " ");
}
void MainWindow::select_protocol_finished(int result)
{
if(result == QDialog::Accepted)
{
create_flash_transports_list();
create_log_transports_list();
fileActions->save_config_file(configValues);
set_flash_arrow_state();
}
else
{
//qDebug() << "Dialog is rejected";
}
status_bar_ecu_label->setText(configValues->flash_protocol_selected_description + " ");
}
void MainWindow::select_vehicle()
{
//qDebug() << "Select protocol";
VehicleSelect vehicleSelect(configValues);
connect(&vehicleSelect, SIGNAL(finished (int)), this, SLOT(select_vehicle_finished(int)));
vehicleSelect.exec();
//QRect screenGeometry = this->geometry();
//vehicleSelect.move(screenGeometry.center() - vehicleSelect.rect().center());
//QCoreApplication::processEvents(QEventLoop::AllEvents, 10);
qDebug() << "Selected protocol:" << configValues->flash_protocol_selected_id;
//status_bar_ecu_label->setText(configValues->flash_protocol_selected_description + " ");
}
void MainWindow::select_vehicle_finished(int result)
{
if(result == QDialog::Accepted)
{
create_flash_transports_list();
create_log_transports_list();
fileActions->save_config_file(configValues);
set_flash_arrow_state();
}
else
{
//qDebug() << "Dialog is rejected";
}
status_bar_ecu_label->setText(configValues->flash_protocol_selected_description + " ");
}
void MainWindow::update_protocol_info(int rom_number)
{
bool info_updated = false;
emit LOG_D("Update protocol info by selected ROM with FlashMethod: " + ecuCalDef[rom_number]->RomInfo.at(fileActions->FlashMethod), true, true);
for (int i = 0; i < configValues->flash_protocol_id.length(); i++)
{
if (configValues->flash_protocol_protocol_name.at(i) == ecuCalDef[rom_number]->RomInfo.at(fileActions->FlashMethod))
{
info_updated = true;
qDebug() << "Protocol name for selected ROM found:" << ecuCalDef[rom_number]->RomInfo.at(fileActions->FlashMethod) << "==" << configValues->flash_protocol_protocol_name.at(i);
configValues->flash_protocol_selected_id = configValues->flash_protocol_id.at(i);
configValues->flash_protocol_selected_make = configValues->flash_protocol_make.at(i);
configValues->flash_protocol_selected_model = configValues->flash_protocol_model.at(i);
configValues->flash_protocol_selected_version = configValues->flash_protocol_version.at(i);
configValues->flash_protocol_selected_protocol_name = configValues->flash_protocol_protocol_name.at(i);
configValues->flash_protocol_selected_description = configValues->flash_protocol_description.at(i);
configValues->flash_protocol_selected_log_protocol = configValues->flash_protocol_log_protocol.at(i);
configValues->flash_protocol_selected_mcu = configValues->flash_protocol_mcu.at(i);
configValues->flash_protocol_selected_checksum = configValues->flash_protocol_checksum.at(i);
}
}
if (info_updated)
emit LOG_D("Protocol info for selected ROM updated", true, true);
else
emit LOG_D("Could not find protocol for selected ROM!", true, true);
status_bar_ecu_label->setText(configValues->flash_protocol_selected_description + " ");
}
void MainWindow::set_flash_arrow_state()
{
QList<QMenu*> menus = ui->menubar->findChildren<QMenu*>();
foreach (QMenu *menu, menus) {
foreach (QAction *action, menu->actions()) {
if (action->isSeparator()) {
} else if (action->menu()) {
} else {
if (action->text() == "Read from ecu")
{
if (configValues->flash_protocol_read.at(configValues->flash_protocol_selected_id.toInt()) == "yes")
action->setEnabled(true);
else
action->setEnabled(false);
}
if (action->text() == "Test write to ecu")
{
if (configValues->flash_protocol_test_write.at(configValues->flash_protocol_selected_id.toInt()) == "yes")
action->setEnabled(true);
else
action->setEnabled(false);
}
if (action->text() == "Write to ecu")
{
if (configValues->flash_protocol_write.at(configValues->flash_protocol_selected_id.toInt()) == "yes")
action->setEnabled(true);
else
action->setEnabled(false);
}
}
}
}
}
void MainWindow::log_transport_changed()
{
//qDebug() << "Change log transport";
QComboBox *log_transport_list = ui->toolBar->findChild<QComboBox*>("log_transport_list");
serial->set_is_can_connection(false);
serial->set_is_iso15765_connection(false);
if (log_transport_list->currentText() == "CAN")
{
serial->set_is_can_connection(true);
serial->set_is_iso15765_connection(false);
serial->set_is_29_bit_id(false);
serial->set_can_speed("500000");
}
else if (log_transport_list->currentText() == "iso15765")
{
serial->set_is_can_connection(false);
serial->set_is_iso15765_connection(true);
serial->set_is_29_bit_id(true);
serial->set_can_speed("500000");
}
else if (log_transport_list->currentText() == "K-Line")
{
if (configValues->flash_protocol_selected_log_protocol == "SSM")
serial->change_port_speed("4800");
}
protocol = configValues->flash_protocol_selected_log_protocol;
configValues->flash_protocol_selected_log_transport = log_transport_list->currentText();
fileActions->save_config_file(configValues);
serial->reset_connection();
ecuid.clear();
ecu_init_complete = false;
//ssm_init_poll_timer->start();
}
void MainWindow::flash_transport_changed()
{
//qDebug() << "Change flash transport";
QComboBox *flash_transport_list = ui->toolBar->findChild<QComboBox*>("flash_transport_list");
configValues->flash_protocol_selected_flash_transport = flash_transport_list->currentText();
fileActions->save_config_file(configValues);
}
void MainWindow::check_serial_ports()
{
QComboBox *serial_port_list = ui->toolBar->findChild<QComboBox*>("serial_port_list");
QString prev_serial_port = serial_port_list->currentText();
int index = 0;
//serial_poll_timer->stop();
//ssm_init_poll_timer->stop();
serial->reset_connection();
ecuid.clear();
ecu_init_complete = false;
serial->set_is_iso14230_connection(false);
serial->set_is_29_bit_id(false);
serial->set_add_iso14230_header(false);
serial->set_is_can_connection(false);
serial->set_is_iso15765_connection(false);
serial->set_serial_port_baudrate("4800");
emit log_transport_list->currentIndexChanged(log_transport_list->currentIndex());
//if(configValues->flash_method != "subarucan" && configValues->flash_method != "subarucan_iso")
//QStringList j2534_list = serial->getAvailableJ2534Libs();
//qDebug() << "J2534 Vehicle PassThru Interfaces:" << j2534_list;
serial_ports = serial->check_serial_ports();
serial_port_list->clear();
for (int i = 0; i < serial_ports.length(); i++)
{
serial_port_list->addItem(serial_ports.at(i));
if (prev_serial_port == serial_ports.at(i))
serial_port_list->setCurrentIndex(index);
index++;
}
//qDebug() << "Start serial and ssm poll timers";
//serial_poll_timer->start();
//ssm_init_poll_timer->start();
}
void MainWindow::open_serial_port()
{
if (serial_ports.length() > 0)
{
//QStringList serial_port = serial_ports.at(serial_port_list->currentIndex()).split(" - ");
QStringList serial_port;
serial_port.append(serial_ports.at(serial_port_list->currentIndex()));
//qDebug() << "Serial ports" << serial_ports;
//if (serial_port.length() < 2)
// serial_port.append("Unknown");
//qDebug() << "Serial port" << serial_port;
serial->set_serial_port_list(serial_port);
QString opened_serial_port = serial->open_serial_port();
if (opened_serial_port != "")
{
if (opened_serial_port != previous_serial_port)
{
ecuid.clear();
ecu_init_complete = false;
}
//qDebug() << "Serial port" << opened_serial_port << "opened" << previous_serial_port;
previous_serial_port = opened_serial_port;
configValues->serial_port = serial_port.at(0);
fileActions->save_config_file(configValues);
if (ecuid == "")
set_status_bar_label(true, false, "");
else
set_status_bar_label(true, true, ecuid);
}
else
{
set_status_bar_label(false, false, "");
ecu_init_complete = false;
}
}
}
int MainWindow::can_listener()
{
QByteArray received;
QByteArray output;
qDebug() << "Listen CANbus";
QComboBox *serial_port_list = ui->toolBar->findChild<QComboBox*>("serial_port_list");
if (serial_port_list->currentText() == "")
{
qDebug() << "No serial port selected!";
QMessageBox::warning(this, tr("Serial port"), "No serial port selected!");
return 0;
}
// Stop serial timers
//serial_poll_timer->stop();
//ssm_init_poll_timer->stop();
logging_poll_timer->stop();
//serial->serial_port_list.clear();
//serial->serial_port_list.append(serial_ports.at(serial_port_list->currentIndex()).split(" - ").at(0));
//serial->serial_port_list.append(serial_ports.at(serial_port_list->currentIndex()).split(" - ").at(1));
//QStringList spl = serial->get_serial_port_list();
//spl.clear();
//spl.append(serial_ports.at(serial_port_list->currentIndex()).split(" - ").at(0));
//spl.append(serial_ports.at(serial_port_list->currentIndex()).split(" - ").at(1));
QStringList spl;
spl.append(serial_ports.at(serial_port_list->currentIndex()));
serial->set_serial_port_list(spl);
if (flash_transport_list->currentText() == "CAN")
{
serial->set_is_can_connection(true);
serial->set_is_iso15765_connection(false);
serial->set_is_29_bit_id(true);
serial->set_can_speed("500000");
}
else if (flash_transport_list->currentText() == "iso15765")
{
serial->set_is_can_connection(false);
serial->set_is_iso15765_connection(true);
serial->set_is_29_bit_id(false);
serial->set_can_speed("500000");
}
serial->set_iso15765_source_address(0x00);//0xFFFFFFFF;
serial->set_iso15765_destination_address(0x00);//0xFFFFFFFF;
serial->set_can_source_address(serial->get_iso15765_source_address());
serial->set_can_destination_address(serial->get_iso15765_destination_address());
// Open serial port
serial->reset_connection();
ecuid.clear();
ecu_init_complete = false;
serial->set_add_iso14230_header(false);
open_serial_port();
uint8_t id = 0xE0;
output.clear();
output.append((uint8_t)0x00);
output.append((uint8_t)0x00);
output.append((uint8_t)0x07);
output.append((uint8_t)id);
output.append((uint8_t)0x01);
output.append((uint8_t)0x00);
while (can_listener_on)
{
qDebug() << "Send msg:" << parse_message_to_hex(output);
serial->write_serial_data_echo_check(output);
delay(200);
received = serial->read_serial_data(100, 500);
qDebug() << parse_message_to_hex(received);
id++;
if (id > 0xE8)
id = 0xE0;
output[3] = (uint8_t)id;
}
return 1;
}
int MainWindow::start_ecu_operations(QString cmd_type)
{
set_realtime_state(false);
toggle_realtime();
int rom_number = 0;
QTreeWidgetItem *selectedItem = NULL;
int item_count = ui->calibrationFilesTreeWidget->selectedItems().count();
QComboBox *serial_port_list = ui->toolBar->findChild<QComboBox*>("serial_port_list");
if (serial_port_list->currentText() == "")
{
qDebug() << "No serial port selected!";
QMessageBox::warning(this, tr("Serial port"), "No serial port selected!");
return 0;
}
// Stop serial timers
logging_poll_timer->stop();
QStringList spl;
spl.append(serial_ports.at(serial_port_list->currentIndex()));