rusEFI console to compare current bundle against auto-DFU bundle #3266

putting signature into equation
This commit is contained in:
rusefillc 2021-09-25 22:00:52 -04:00
parent b602f1b158
commit 406107830b
3 changed files with 48 additions and 13 deletions

View File

@ -29,7 +29,7 @@ public class PortDetector {
* @return port name on which rusEFI was detected or null if none
*/
@Nullable
public static SerialAutoChecker.AutoDetectResult autoDetectSerial(Function<IoStream, Void> callback) {
public static SerialAutoChecker.AutoDetectResult autoDetectSerial(Function<SerialAutoChecker.CallbackContext, Void> callback) {
String rusEfiAddress = System.getProperty("rusefi.address");
if (rusEfiAddress != null) {
return getSignatureFromPorts(callback, new String[] {rusEfiAddress});
@ -43,12 +43,16 @@ public class PortDetector {
return getSignatureFromPorts(callback, serialPorts);
}
private static SerialAutoChecker.AutoDetectResult getSignatureFromPorts(Function<IoStream, Void> callback, String[] serialPorts) {
public static SerialAutoChecker.AutoDetectResult getSignatureFromPorts(Function<SerialAutoChecker.CallbackContext, Void> callback, String[] serialPorts) {
List<Thread> serialFinder = new ArrayList<>();
CountDownLatch portFound = new CountDownLatch(1);
AtomicReference<SerialAutoChecker.AutoDetectResult> result = new AtomicReference<>();
for (String serialPort : serialPorts) {
Thread thread = AUTO_DETECT_PORT.newThread(new SerialAutoChecker(serialPort, portFound, result, callback));
Thread thread = AUTO_DETECT_PORT.newThread(
() -> {
SerialAutoChecker checker = new SerialAutoChecker(serialPort, portFound, result, callback);
checker.openAndRunLogic();
});
serialFinder.add(thread);
thread.start();
}

View File

@ -15,15 +15,15 @@ import java.util.function.Function;
import static com.rusefi.binaryprotocol.IoHelper.checkResponseCode;
public class SerialAutoChecker implements Runnable {
public class SerialAutoChecker {
private final static Logging log = Logging.getLogging(SerialAutoChecker.class);
private final String serialPort;
private final CountDownLatch portFound;
private final AtomicReference<AutoDetectResult> result;
@Nullable
private final Function<IoStream, Void> callback;
private final Function<CallbackContext, Void> callback;
public SerialAutoChecker(String serialPort, CountDownLatch portFound, AtomicReference<AutoDetectResult> result, Function<IoStream, Void> callback) {
public SerialAutoChecker(String serialPort, CountDownLatch portFound, AtomicReference<AutoDetectResult> result, Function<CallbackContext, Void> callback) {
this.serialPort = serialPort;
this.portFound = portFound;
this.result = result;
@ -34,9 +34,16 @@ public class SerialAutoChecker implements Runnable {
this(serialPort, portFound, result, null);
}
@Override
public void run() {
public void openAndRunLogic() {
IoStream stream = SerialIoStreamJSerialComm.openPort(serialPort);
try {
runLogic(stream);
} finally {
stream.close();
}
}
public void runLogic(IoStream stream) {
IncomingDataBuffer incomingData = stream.getDataBuffer();
boolean isPortFound = false;
String signature;
@ -49,14 +56,12 @@ public class SerialAutoChecker implements Runnable {
log.info("Got signature=" + signature + " from " + serialPort);
if (signature.startsWith(Fields.PROTOCOL_SIGNATURE_PREFIX)) {
if (callback != null) {
callback.apply(stream);
callback.apply(new CallbackContext(stream, signature));
}
isPortFound = true;
}
} catch (IOException ignore) {
return;
} finally {
stream.close();
}
if (isPortFound) {
/**
@ -67,6 +72,24 @@ public class SerialAutoChecker implements Runnable {
}
}
public static class CallbackContext {
private final IoStream stream;
private final String signature;
public CallbackContext(IoStream stream, String signature) {
this.stream = stream;
this.signature = signature;
}
public String getSignature() {
return signature;
}
public IoStream getStream() {
return stream;
}
}
public static class AutoDetectResult {
private final String serialPort;

View File

@ -5,6 +5,7 @@ import com.rusefi.ConsoleUI;
import com.rusefi.Launcher;
import com.rusefi.Timeouts;
import com.rusefi.autodetect.PortDetector;
import com.rusefi.autodetect.SerialAutoChecker;
import com.rusefi.io.DfuHelper;
import com.rusefi.io.IoStream;
import com.rusefi.io.serial.SerialIoStreamJSerialComm;
@ -16,7 +17,10 @@ import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import static com.rusefi.StartupFrame.appendBundleName;
@ -53,14 +57,18 @@ public class DfuFlasher {
if (!PortDetector.isAutoPort(port)) {
messages.append("Using selected " + port + "\n");
Function<SerialAutoChecker.CallbackContext, Void> callback = null; // todo
AtomicReference<SerialAutoChecker.AutoDetectResult> result = new AtomicReference<>();
SerialAutoChecker checker = new SerialAutoChecker(port, new CountDownLatch(1), result, callback);
IoStream stream = SerialIoStreamJSerialComm.openPort(port);
checker.runLogic(stream);
DfuHelper.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 -> {
DfuHelper.sendDfuRebootCommand(stream, messages);
port = PortDetector.autoDetectSerial(callbackContext -> {
DfuHelper.sendDfuRebootCommand(callbackContext.getStream(), messages);
return null;
}).getSerialPort();
if (port == null) {