-
Notifications
You must be signed in to change notification settings - Fork 70
/
FirmwareActivity.java
514 lines (449 loc) · 19.3 KB
/
FirmwareActivity.java
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
/*
CanZE
Take a closer look at your ZE car
Copyright (C) 2015 - The CanZE Team
http://canze.fisch.lu
This program 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 any
later version.
This program 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/>.
*/
package lu.fisch.canze.activities;
import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Button;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import lu.fisch.canze.BuildConfig;
import lu.fisch.canze.R;
import lu.fisch.canze.actors.Ecu;
import lu.fisch.canze.actors.Ecus;
import lu.fisch.canze.actors.Field;
import lu.fisch.canze.actors.Fields;
import lu.fisch.canze.actors.Frame;
import lu.fisch.canze.actors.Frames;
import lu.fisch.canze.actors.Message;
import lu.fisch.canze.actors.StoppableThread;
import lu.fisch.canze.interfaces.DebugListener;
import lu.fisch.canze.interfaces.FieldListener;
import static lu.fisch.canze.activities.MainActivity.debug;
// Jeroen
public class FirmwareActivity extends CanzeActivity implements FieldListener, DebugListener {
// displays firmware version of an eCU, or
// writes firmware version of all ECUs to a CSv
private StoppableThread queryThread;
private BufferedWriter bufferedDumpWriter = null;
private boolean dumpInProgress = false;
private long ticker = 0;
@SuppressLint("StringFormatMatches")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_firmware);
int index = 0;
for (Ecu ecu : Ecus.getInstance().getAllEcus()) {
// ensure we are only selecting true (as in physical boxes) and reachable (as in, i.e. skipping R-LINK) ECU's
if (ecu.getFromId() > 0 && ecu.getFromId() != 0x800 && ecu.getFromId() != 0x801 &&
(!BuildConfig.BRANCH.equals("master") || (
!ecu.getAliases().contains("AIRBAG") &&
!ecu.getAliases().contains("ESC"))
))
{
TextView tv;
tv = findViewById(getResources().getIdentifier("lEcu" + index, "id", getPackageName()));
if (tv != null) {
final Ecu thisEcu = ecu;
tv.setText(ecu.getMnemonic() + " (" + ecu.getName() + ")");
tv.setOnClickListener(v -> {
if(dumpInProgress)
return;
showSelected (v); // clear out previous values an the selection bar, and set the new bar
doGetOneFirmware(thisEcu); // get the new values and populate
});
index++;
} else {
MainActivity.toast(MainActivity.TOAST_NONE, MainActivity.getStringSingle(R.string.format_NoView), "lEcu", Integer.toString (index));
}
}
}
final Button btnCsvSave = findViewById(R.id.csvFirmware);
btnCsvSave.setOnClickListener(v -> doGetAllFirmware());
TextView textView = findViewById(R.id.link);
textView.setText(Html.fromHtml(MainActivity.getStringSingle(R.string.help_Ecus)));
textView.setMovementMethod(LinkMovementMethod.getInstance());
setDoRestartQueueOnResume(false);
new Thread(() -> {
if (MainActivity.device != null) {
// stop the poller thread
// note tht for the ZE50, the gateway is now not triggered
MainActivity.device.stopAndJoin();
}
}).start();
}
private void showSelected (View v) {
View tv;
int bgColor = 0xfff3f3f3;
TypedValue a = new TypedValue();
getTheme().resolveAttribute(android.R.attr.windowBackground, a, true);
if (a.type >= TypedValue.TYPE_FIRST_COLOR_INT && a.type <= TypedValue.TYPE_LAST_COLOR_INT) {
bgColor = a.data;
}
// deselect bg color ecu list
for (int index = 0; (tv = findViewById(getResources().getIdentifier("lEcu" + index, "id", getPackageName())))!= null ; index++) {
tv.setBackgroundColor(bgColor);
}
/*
for (Ecu ecu : Ecus.getInstance().getAllEcus()) {
if (ecu.getFromId() > 0 && ecu.getFromId() < 0x800) {
tv = findViewById(getResources().getIdentifier("lEcu" + Integer.toHexString(ecu.getFromId()).toLowerCase(), "id", getPackageName()));
if (tv != null) {
tv.setBackgroundColor(bgColor);
}
}
}
*/
// set bg selected ecu
v.setBackgroundColor(0xff808080); // selected color
// clear out old values
setSoftwareValue(R.id.textDiagVersion, null, "");
setSoftwareValue(R.id.textSupplier, null, "");
setSoftwareValue(R.id.textSoft, null, "");
setSoftwareValue(R.id.textVersion, null, "");
}
private void doGetOneFirmware (final Ecu ecu) {
// try to stop previous thread
if(queryThread!=null) {
if (queryThread.isAlive()) {
queryThread.tryToStop();
try {
queryThread.join();
} catch (Exception e) {
MainActivity.debug(e.getMessage());
}
}
}
// clear out the result fields from previous results
setSoftwareValue(R.id.textDiagVersion, null, null);
setSoftwareValue(R.id.textSupplier, null, null);
setSoftwareValue(R.id.textSoft, null, null);
setSoftwareValue(R.id.textVersion, null, null);
queryThread = new StoppableThread(() -> {
if (MainActivity.isPh2() && MainActivity.pokeSecureGateway) {
// open the gateway, as the poller is stopped
queryFrame(getFrame(0x18daf1d2, "5003"));
}
if (ecu.getSessionRequired()) {
// open the ecu, as the poller is stopped
queryFrame(getFrame(ecu.getFromId(), ecu.getStartDiag()));
}
// get the info
processOneEcu(ecu, R.id.textDiagVersion, R.id.textSupplier, R.id.textSoft, R.id.textVersion);
});
queryThread.start();
}
private void doGetAllFirmware(){
// try to stop previous thread
if(queryThread!=null) {
if (queryThread.isAlive()) {
queryThread.tryToStop();
try {
queryThread.join();
} catch (Exception e) {
MainActivity.debug(e.getMessage());
}
}
}
queryThread = new StoppableThread(() -> {
freezeOrientation();
Frame frame;
displayProgress(true, R.id.progressBar_cyclic3, R.id.csvFirmware);
// re-initialize the device
if (!MainActivity.device.initDevice(1)) {
return;
}
createDump();
ticker = Calendar.getInstance().getTimeInMillis();
log("ECU, Version Type, Version data");
if (MainActivity.isPh2() && MainActivity.pokeSecureGateway) {
frame = getFrame(0x18daf1d2, "5003"); // open the gateway, as the poller is stopped
queryFrame(frame);
}
for (Ecu ecu : Ecus.getInstance().getAllEcus()) {
// see if we need to stop right now
if (((StoppableThread) Thread.currentThread()).isStopped()) return;
if (ecu.getFromId() > 0 && (ecu.getFromId() < 0x800 || ecu.getFromId() >= 0x900)) {
keepAlive();
if (ecu.getSessionRequired()) {
frame = getFrame(ecu.getFromId(), ecu.getStartDiag()); // open the ecu, as the poller is stopped
queryFrame(frame);
}
processOneEcu(ecu, 0, 0, 0, 0);
}
}
closeDump();
displayProgress(false, R.id.progressBar_cyclic3, R.id.csvFirmware);
});
queryThread.start();
}
private void processOneEcu (Ecu ecu, int idDiag, int idSupplier, int idSoft, int idVersion) {
// query the Frame
Frame frame = getFrame(ecu.getFromId(), "6180");
if (frame != null) {
frame = queryFrame(frame);
if (frame != null) {
for (Field field : frame.getAllFields()) {
switch (field.getFrom()) {
case 56:
setSoftwareValue(idDiag, field, ecu.getMnemonic() + ", diagVersion");
break;
case 64:
setSoftwareValue(idSupplier, field, ecu.getMnemonic() + ", supplier");
break;
case 128:
setSoftwareValue(idSoft, field, ecu.getMnemonic() + ", soft");
break;
case 144:
setSoftwareValue(idVersion, field, ecu.getMnemonic() + ", version");
break;
}
}
}
return;
}
// else 2nd approach
frame = getFrame(ecu.getFromId(), "62f1a0");
if (frame != null) {
frame = queryFrame(frame);
if (frame != null) {
Field field = frame.getAllFields().get(0);
setSoftwareValue(idDiag, field, ecu.getMnemonic() + ", diagVersion");
}
}
frame = getFrame(ecu.getFromId(), "62f18a");
if (frame != null) {
frame = queryFrame(frame);
if (frame != null) {
Field field = frame.getAllFields().get(0);
setSoftwareValue(idSupplier, field, ecu.getMnemonic() + ", supplier");
}
}
frame = getFrame(ecu.getFromId(), "62f194");
if (frame != null) {
frame = queryFrame(frame);
if (frame != null) {
Field field = frame.getAllFields().get(0);
setSoftwareValue(idSoft, field, ecu.getMnemonic() + ", soft:");
}
}
frame = getFrame(ecu.getFromId(), "62f195");
if (frame != null) {
frame = queryFrame(frame);
if (frame != null) {
Field field = frame.getAllFields().get(0);
setSoftwareValue(idVersion, field, ecu.getMnemonic() + ", version:");
}
}
}
private Frame getFrame (int fromId, String responseId) {
Frame frame = Frames.getInstance().getById(fromId, responseId);
if (frame == null) {
MainActivity.getInstance().dropDebugMessage(String.format(Locale.getDefault(), "Frame for this ECU %X.%s not found", fromId, responseId));
return null;
}
MainActivity.getInstance().dropDebugMessage(frame.getFromIdHex() + "." + frame.getResponseId());
return frame;
}
private Frame queryFrame (Frame frame) {
if (frame == null) return null;
Message message = MainActivity.device.requestFrame(frame); // field.getFrame());
if (message.isError()) {
MainActivity.getInstance().dropDebugMessage(message.getError());
return null;
}
MainActivity.debug("msg:" + message.getData());
message.onMessageCompleteEvent(); // set the value of all fields in the frame of this message
return frame;
}
private void setSoftwareValue(final int id, final Field field, final String label) {
final String toDisplay;
if (field == null) {
toDisplay = "";
} else if (field.isString()) {
toDisplay = label + ":" + field.getStringValue();
} else if ((field.getTo() - field.getFrom()) < 8) {
toDisplay = label + String.format(Locale.getDefault(), ":%02X", (int)field.getValue());
} else {
toDisplay = label + String.format(Locale.getDefault(), ":%04X", (int)field.getValue());
}
if (id == 0) { //log
log (toDisplay);
return;
}
runOnUiThread(() -> {
TextView tv = findViewById(id);
if (tv != null) {
tv.setText(toDisplay);
}
});
}
private void log(String text) {
if (!dumpInProgress) return;
try {
bufferedDumpWriter.append(text);
bufferedDumpWriter.append(System.getProperty("line.separator"));
} catch (IOException e) {
e.printStackTrace();
}
}
private void createDump() {
dumpInProgress = false;
if (!MainActivity.storageIsAvailable) {
debug("FirmwareActivity.createDump: SDcard not available");
return;
}
// ensure that there is a CanZE Folder in SDcard
if (!MainActivity.getInstance().isExternalStorageWritable()) {
debug("FirmwareActivity.createDump: SDcard not writeable");
return;
}
String file_path = MainActivity.getInstance().getExternalFolder();
File dir = new File(file_path);
if (!dir.exists()) {
if (!dir.mkdirs()) {
debug("DiagDump: Can't create directory:" + file_path);
return;
}
}
debug("DiagDump: file_path:" + file_path);
SimpleDateFormat sdf = new SimpleDateFormat(MainActivity.getStringSingle(R.string.format_YMDHMS), Locale.getDefault());
String exportdataFileName = file_path + "Firmwares-" + sdf.format(Calendar.getInstance().getTime()) + ".csv";
File logFile = new File(exportdataFileName);
if (!logFile.exists()) {
try {
if (!logFile.createNewFile()) {
debug("DiagDump: Can't create file:" + exportdataFileName);
return;
}
debug("DiagDump: NewFile:" + exportdataFileName);
} catch (IOException e) {
e.printStackTrace();
return;
}
}
try {
//BufferedWriter for performance, true to set append to file flag
bufferedDumpWriter = new BufferedWriter(new FileWriter(logFile, true));
dumpInProgress = true;
if (!isFinishing())
MainActivity.toast(MainActivity.TOAST_NONE, MainActivity.getStringSingle(R.string.format_DumpWriting), exportdataFileName);
} catch (IOException e) {
e.printStackTrace();
}
}
private void keepAlive() {
if (!MainActivity.isPh2()) return; // quit ticker if no gateway and no session
if (Calendar.getInstance().getTimeInMillis() < ticker) return; // then, quit if no timeout
if (MainActivity.isPh2() && MainActivity.pokeSecureGateway) {
// open the gateway
MainActivity.device.requestFrame(Frames.getInstance().getById(0x18daf1d2, "5003"));
}
ticker = ticker + 3000;
}
private void closeDump() {
try {
if (dumpInProgress) {
bufferedDumpWriter.close();
if (!isFinishing())
MainActivity.toast(MainActivity.TOAST_NONE, "Done.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
// UI elements
private void displayProgress(final boolean on, final int id_spinner, final int id_button) {
// remove progress spinners
runOnUiThread(() -> {
ProgressBar pb = findViewById(id_spinner);
if (pb != null){ pb.setVisibility(on ? View.VISIBLE : View.GONE);}
Button btn = findViewById(id_button);
if (btn != null){btn.setEnabled(!on);}
});
}
@SuppressLint("SourceLockedOrientationActivity")
// this is done to avoid restarting the long running activity
private void freezeOrientation () {
int rotation = getWindowManager().getDefaultDisplay().getRotation();
switch (getResources().getConfiguration().orientation) {
case Configuration.ORIENTATION_PORTRAIT:
if (rotation == android.view.Surface.ROTATION_90 || rotation == android.view.Surface.ROTATION_180) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
break;
case Configuration.ORIENTATION_LANDSCAPE:
if (rotation == android.view.Surface.ROTATION_0 || rotation == android.view.Surface.ROTATION_90) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}
break;
}
}
@Override
protected void onDestroy() {
// stop the query thread if still running
if(queryThread!=null)
if(queryThread.isAlive()) {
queryThread.tryToStop();
try {
queryThread.join();
}
catch(Exception e)
{
MainActivity.debug(e.getMessage());
}
}
closeDump();
// Reload the frame & timings
Frames.getInstance().load();
Fields.getInstance().load();
// restart the poller
if (MainActivity.device != null) {
MainActivity.device.initConnection();
// register application wide fields
MainActivity.getInstance().registerApplicationFields();
}
super.onDestroy();
}
protected void initListeners () {
MainActivity.getInstance().setDebugListener(this);
}
@Override
public void onFieldUpdateEvent(final Field field) {
// do nothing, no fields will ne updated
}
}