Sometimes legacy DFU does not work #1477

This commit is contained in:
rusefi 2020-06-01 19:52:56 -04:00
parent 5beca0c48c
commit 9d8537d434
4 changed files with 56 additions and 15 deletions

View File

@ -1,6 +1,7 @@
package com.rusefi.autodetect; package com.rusefi.autodetect;
import com.rusefi.FileLog; import com.rusefi.FileLog;
import com.rusefi.io.IoStream;
import jssc.SerialPortList; import jssc.SerialPortList;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -11,6 +12,7 @@ import java.util.List;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
/** /**
* (c) Andrey Belomutskiy 2013-2019 * (c) Andrey Belomutskiy 2013-2019
@ -18,8 +20,9 @@ import java.util.concurrent.atomic.AtomicReference;
public class PortDetector { public class PortDetector {
/** /**
* Connect to all serial ports and find out which one respond first * Connect to all serial ports and find out which one respond first
* @param callback
*/ */
public static String autoDetectSerial() { public static String autoDetectSerial(Function<IoStream, Void> callback) {
String[] serialPorts = getPortNames(); String[] serialPorts = getPortNames();
if (serialPorts.length == 0) { if (serialPorts.length == 0) {
System.err.println("No serial ports detected"); System.err.println("No serial ports detected");
@ -30,7 +33,7 @@ public class PortDetector {
CountDownLatch portFound = new CountDownLatch(1); CountDownLatch portFound = new CountDownLatch(1);
AtomicReference<String> result = new AtomicReference<>(); AtomicReference<String> result = new AtomicReference<>();
for (String serialPort : serialPorts) { for (String serialPort : serialPorts) {
Thread thread = new Thread(new SerialAutoChecker(serialPort, portFound, result)); Thread thread = new Thread(new SerialAutoChecker(serialPort, portFound, result, callback));
serialFinder.add(thread); serialFinder.add(thread);
thread.start(); thread.start();
} }
@ -55,7 +58,7 @@ public class PortDetector {
@Nullable @Nullable
public static String autoDetectPort(JFrame parent) { public static String autoDetectPort(JFrame parent) {
String autoDetectedPort = autoDetectSerial(); String autoDetectedPort = autoDetectSerial(null);
if (autoDetectedPort == null) { if (autoDetectedPort == null) {
JOptionPane.showMessageDialog(parent, "Failed to located device"); JOptionPane.showMessageDialog(parent, "Failed to located device");
return null; return null;
@ -64,8 +67,12 @@ public class PortDetector {
} }
public static String autoDetectSerialIfNeeded(String port) { public static String autoDetectSerialIfNeeded(String port) {
if (!port.toLowerCase().startsWith("auto")) if (!isAutoPort(port))
return port; return port;
return autoDetectSerial(); return autoDetectSerial(null);
}
public static boolean isAutoPort(String port) {
return port.toLowerCase().startsWith("auto");
} }
} }

View File

@ -12,6 +12,7 @@ import com.rusefi.io.serial.SerialIoStreamJSerialComm;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import static com.rusefi.binaryprotocol.IoHelper.checkResponseCode; import static com.rusefi.binaryprotocol.IoHelper.checkResponseCode;
@ -19,12 +20,18 @@ public class SerialAutoChecker implements Runnable {
private final String serialPort; private final String serialPort;
private final CountDownLatch portFound; private final CountDownLatch portFound;
private final AtomicReference<String> result; private final AtomicReference<String> result;
private final Function<IoStream, Void> callback;
public static String SIGNATURE; public static String SIGNATURE;
public SerialAutoChecker(String serialPort, CountDownLatch portFound, AtomicReference<String> result) { public SerialAutoChecker(String serialPort, CountDownLatch portFound, AtomicReference<String> result, Function<IoStream, Void> callback) {
this.serialPort = serialPort; this.serialPort = serialPort;
this.portFound = portFound; this.portFound = portFound;
this.result = result; this.result = result;
this.callback = callback;
}
public SerialAutoChecker(String serialPort, CountDownLatch portFound, AtomicReference<String> result) {
this(serialPort, portFound, result, null);
} }
@Override @Override
@ -42,6 +49,9 @@ public class SerialAutoChecker implements Runnable {
System.out.println("Got " + signature + " from " + serialPort); System.out.println("Got " + signature + " from " + serialPort);
String signatureWithoutMinorVersion = Fields.TS_SIGNATURE.substring(0, Fields.TS_SIGNATURE.length() - 2); String signatureWithoutMinorVersion = Fields.TS_SIGNATURE.substring(0, Fields.TS_SIGNATURE.length() - 2);
if (signature.startsWith(signatureWithoutMinorVersion)) { if (signature.startsWith(signatureWithoutMinorVersion)) {
if (callback != null) {
callback.apply(stream);
}
result.set(serialPort); result.set(serialPort);
portFound.countDown(); portFound.countDown();
} }

View File

@ -18,6 +18,7 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.function.Function;
/** /**
* @see FirmwareFlasher * @see FirmwareFlasher
@ -42,19 +43,42 @@ public class DfuFlasher {
public static void doAutoDfu(JComboBox<String> comboPorts) { public static void doAutoDfu(JComboBox<String> comboPorts) {
String port = comboPorts.getSelectedItem().toString(); String port = comboPorts.getSelectedItem().toString();
port = PortDetector.autoDetectSerialIfNeeded(port); StringBuilder messages = new StringBuilder();
if (port == null) {
JOptionPane.showMessageDialog(Launcher.getFrame(), "rusEfi serial port not detected"); if (!PortDetector.isAutoPort(port)) {
return; messages.append("Using selected " + port + "\n");
IoStream stream = SerialIoStreamJSerialComm.openPort(port);
sendDfuRebootCommand(stream, messages);
} else {
messages.append("Auto-detecting port...\n");
// instead of opening the just-detected port we execute the command using the same stream we used to discover port
// it's more reliable this way
port = PortDetector.autoDetectSerial(stream -> {
sendDfuRebootCommand(stream, messages);
return null;
});
if (port == null) {
JOptionPane.showMessageDialog(Launcher.getFrame(), "rusEfi serial port not detected");
return;
} else {
messages.append("Detected rusEFI on " + port + "\n");
}
} }
IoStream stream = SerialIoStreamJSerialComm.openPort(port); StatusWindow wnd = new StatusWindow();
wnd.showFrame("DFU status " + Launcher.CONSOLE_VERSION);
wnd.appendMsg(messages.toString());
ExecHelper.submitAction(() -> executeDFU(wnd), DfuFlasher.class + " thread");
}
private static void sendDfuRebootCommand(IoStream stream, StringBuilder messages) {
byte[] command = BinaryProtocol.getTextCommandBytes(Fields.CMD_REBOOT_DFU); byte[] command = BinaryProtocol.getTextCommandBytes(Fields.CMD_REBOOT_DFU);
try { try {
stream.sendPacket(command, FileLog.LOGGER); stream.sendPacket(command, FileLog.LOGGER);
stream.close(); stream.close();
} catch (IOException ignored) { messages.append("Reboot command sent!\n");
} catch (IOException e) {
messages.append("Error " + e);
} }
runDfuProgramming();
} }
public static void runDfuProgramming() { public static void runDfuProgramming() {

View File

@ -142,7 +142,7 @@ public class ConsoleTools {
} }
}); });
String autoDetectedPort = PortDetector.autoDetectSerial(); String autoDetectedPort = PortDetector.autoDetectSerial(null);
if (autoDetectedPort == null) { if (autoDetectedPort == null) {
System.err.println("rusEFI not detected"); System.err.println("rusEFI not detected");
return; return;
@ -206,7 +206,7 @@ public class ConsoleTools {
@Nullable @Nullable
private static String autoDetectPort() { private static String autoDetectPort() {
String autoDetectedPort = PortDetector.autoDetectSerial(); String autoDetectedPort = PortDetector.autoDetectSerial(null);
if (autoDetectedPort == null) { if (autoDetectedPort == null) {
System.err.println("rusEFI not detected"); System.err.println("rusEFI not detected");
return null; return null;