tune via CAN #3361

This commit is contained in:
rusefillc 2021-12-04 01:36:06 -05:00
parent 34174fc7ee
commit 37ef7302d4
7 changed files with 77 additions and 18 deletions

View File

@ -24,16 +24,21 @@ public class PortDetector {
private static final NamedThreadFactory AUTO_DETECT_PORT = new NamedThreadFactory("AutoDetectPort"); private static final NamedThreadFactory AUTO_DETECT_PORT = new NamedThreadFactory("AutoDetectPort");
public static final String AUTO = "auto"; public static final String AUTO = "auto";
public enum DetectorMode {
DETECT_TS,
DETECT_ELM327,
}
/** /**
* 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 * @param callback
* @return port name on which rusEFI was detected or null if none * @return port name on which rusEFI was detected or null if none
*/ */
@NotNull @NotNull
public static SerialAutoChecker.AutoDetectResult autoDetectSerial(Function<SerialAutoChecker.CallbackContext, Void> callback) { public static SerialAutoChecker.AutoDetectResult autoDetectSerial(Function<SerialAutoChecker.CallbackContext, Void> callback, PortDetector.DetectorMode mode) {
String rusEfiAddress = System.getProperty("rusefi.address"); String rusEfiAddress = System.getProperty("rusefi.address");
if (rusEfiAddress != null) { if (rusEfiAddress != null) {
return getSignatureFromPorts(callback, new String[] {rusEfiAddress}); return getSignatureFromPorts(mode, callback, new String[] {rusEfiAddress});
} }
String[] serialPorts = getPortNames(); String[] serialPorts = getPortNames();
if (serialPorts.length == 0) { if (serialPorts.length == 0) {
@ -41,11 +46,11 @@ public class PortDetector {
return new SerialAutoChecker.AutoDetectResult(null, null); return new SerialAutoChecker.AutoDetectResult(null, null);
} }
log.info("Trying " + Arrays.toString(serialPorts)); log.info("Trying " + Arrays.toString(serialPorts));
return getSignatureFromPorts(callback, serialPorts); return getSignatureFromPorts(mode, callback, serialPorts);
} }
@NotNull @NotNull
private static SerialAutoChecker.AutoDetectResult getSignatureFromPorts(Function<SerialAutoChecker.CallbackContext, Void> callback, String[] serialPorts) { private static SerialAutoChecker.AutoDetectResult getSignatureFromPorts(DetectorMode mode, Function<SerialAutoChecker.CallbackContext, Void> callback, String[] serialPorts) {
List<Thread> serialFinder = new ArrayList<>(); List<Thread> serialFinder = new ArrayList<>();
CountDownLatch portFound = new CountDownLatch(1); CountDownLatch portFound = new CountDownLatch(1);
AtomicReference<SerialAutoChecker.AutoDetectResult> result = new AtomicReference<>(); AtomicReference<SerialAutoChecker.AutoDetectResult> result = new AtomicReference<>();
@ -53,7 +58,7 @@ public class PortDetector {
Thread thread = AUTO_DETECT_PORT.newThread(new Runnable() { Thread thread = AUTO_DETECT_PORT.newThread(new Runnable() {
@Override @Override
public void run() { public void run() {
new SerialAutoChecker(serialPort, portFound).openAndCheckResponse(result, callback); new SerialAutoChecker(mode, serialPort, portFound).openAndCheckResponse(result, callback);
} }
@Override @Override
@ -88,6 +93,10 @@ public class PortDetector {
return autoDetectResult; return autoDetectResult;
} }
public static SerialAutoChecker.AutoDetectResult autoDetectSerial(Function<SerialAutoChecker.CallbackContext, Void> callback) {
return autoDetectSerial(callback, PortDetector.DetectorMode.DETECT_TS);
}
private static String[] getPortNames() { private static String[] getPortNames() {
// long now = System.currentTimeMillis(); // long now = System.currentTimeMillis();
String[] portNames = LinkManager.getCommPorts(); String[] portNames = LinkManager.getCommPorts();

View File

@ -4,6 +4,7 @@ import com.devexperts.logging.Logging;
import com.rusefi.binaryprotocol.IncomingDataBuffer; import com.rusefi.binaryprotocol.IncomingDataBuffer;
import com.rusefi.config.generated.Fields; import com.rusefi.config.generated.Fields;
import com.rusefi.io.IoStream; import com.rusefi.io.IoStream;
import com.rusefi.io.can.Elm327Connector;
import com.rusefi.io.commands.HelloCommand; import com.rusefi.io.commands.HelloCommand;
import com.rusefi.io.serial.SerialIoStreamJSerialComm; import com.rusefi.io.serial.SerialIoStreamJSerialComm;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -17,15 +18,23 @@ import static com.rusefi.binaryprotocol.IoHelper.checkResponseCode;
public class SerialAutoChecker { public class SerialAutoChecker {
private final static Logging log = Logging.getLogging(SerialAutoChecker.class); private final static Logging log = Logging.getLogging(SerialAutoChecker.class);
private final PortDetector.DetectorMode mode;
private final String serialPort; private final String serialPort;
private final CountDownLatch portFound; private final CountDownLatch portFound;
public SerialAutoChecker(String serialPort, CountDownLatch portFound) { public SerialAutoChecker(PortDetector.DetectorMode mode, String serialPort, CountDownLatch portFound) {
this.mode = mode;
this.serialPort = serialPort; this.serialPort = serialPort;
this.portFound = portFound; this.portFound = portFound;
} }
public String checkResponse(IoStream stream, Function<CallbackContext, Void> callback) { public String checkResponse(IoStream stream, Function<CallbackContext, Void> callback) {
if (mode == PortDetector.DetectorMode.DETECT_ELM327) {
if (Elm327Connector.checkConnection(serialPort, SerialIoStreamJSerialComm.openPort(serialPort))) {
return serialPort;
}
return null;
}
IncomingDataBuffer incomingData = stream.getDataBuffer(); IncomingDataBuffer incomingData = stream.getDataBuffer();
try { try {
HelloCommand.send(stream); HelloCommand.send(stream);

View File

@ -2,7 +2,6 @@ package com.rusefi.io.can;
import com.devexperts.logging.Logging; import com.devexperts.logging.Logging;
import com.opensr5.io.DataListener; import com.opensr5.io.DataListener;
import com.romraider.util.HexUtil;
import com.rusefi.io.IoStream; import com.rusefi.io.IoStream;
import com.rusefi.io.serial.BaudRateHolder; import com.rusefi.io.serial.BaudRateHolder;
import com.rusefi.io.serial.SerialIoStreamJSerialComm; import com.rusefi.io.serial.SerialIoStreamJSerialComm;
@ -12,11 +11,9 @@ import com.rusefi.io.tcp.TcpConnector;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.lang.Thread;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -87,9 +84,9 @@ public class Elm327Connector implements Closeable, DataListener {
} }
public static boolean checkConnection(String serialPort) { public static boolean checkConnection(String serialPort, IoStream stream) {
Elm327Connector con = new Elm327Connector(); Elm327Connector con = new Elm327Connector();
boolean found = con.initConnection(serialPort); boolean found = con.initConnection(serialPort, stream);
con.close(); con.close();
return found; return found;
} }
@ -97,7 +94,7 @@ public class Elm327Connector implements Closeable, DataListener {
public void start(String serialPort) { public void start(String serialPort) {
log.info("* Elm327.start()"); log.info("* Elm327.start()");
if (initConnection(serialPort)) { if (initConnection(serialPort, SerialIoStreamJSerialComm.openPort(serialPort))) {
// reset to defaults // reset to defaults
sendCommand("ATD", "OK"); sendCommand("ATD", "OK");
@ -210,18 +207,18 @@ public class Elm327Connector implements Closeable, DataListener {
/////////////////////////////////////////////////////// ///////////////////////////////////////////////////////
private boolean initConnection(String serialPort) { private boolean initConnection(String msg, IoStream stream) {
// todo: this seems like a hack-ish way? Shouldn't be openPort(port, baudrate)? // todo: this seems like a hack-ish way? Shouldn't be openPort(port, baudrate)?
BaudRateHolder.INSTANCE.baudRate = ELM327_DEFAULT_BAUDRATE; BaudRateHolder.INSTANCE.baudRate = ELM327_DEFAULT_BAUDRATE;
this.stream = SerialIoStreamJSerialComm.openPort(serialPort); this.stream = stream;
this.stream.setInputListener(this); this.stream.setInputListener(this);
if (sendCommand("ATZ", "ELM327 v[0-9]+\\.[0-9]+", BIG_TIMEOUT) != null) { if (sendCommand("ATZ", "ELM327 v[0-9]+\\.[0-9]+", BIG_TIMEOUT) != null) {
log.info("ELM DETECTED on " + serialPort + "!"); log.info("ELM DETECTED on " + msg + "!");
return true; return true;
} }
log.info("ELM NOT FOUND on " + serialPort + "!"); log.info("ELM NOT FOUND on " + msg + "!");
return false; return false;
} }

View File

@ -0,0 +1,43 @@
package com.rusefi.io.can;
public class HexUtil {
public static byte[] asBytes(String hex) {
if (hex.indexOf(' ') >= 0) {
hex = hex.replaceAll(" ", "");
}
if (hex.startsWith("0x")) {
hex = hex.substring(2);
}
return hexToBytes(hex);
}
private static byte[] hexToBytes(String s) {
return hexToBytes(s, 0);
}
private static byte[] hexToBytes(String s, int off) {
byte[] bs = new byte[off + (1 + s.length()) / 2];
hexToBytes(s, bs, off);
return bs;
}
private static void hexToBytes(String s, byte[] out, int off) throws NumberFormatException, IndexOutOfBoundsException {
int slen = s.length();
if ((slen % 2) != 0) {
s = '0' + s;
}
if (out.length < off + slen / 2) {
throw new IndexOutOfBoundsException("Output buffer too small for input (" + out.length + "<" + off + slen / 2 + ")");
}
// Safe to assume the string is even length
byte b1, b2;
for (int i = 0; i < slen; i += 2) {
b1 = (byte) Character.digit(s.charAt(i), 16);
b2 = (byte) Character.digit(s.charAt(i + 1), 16);
if ((b1 < 0) || (b2 < 0)) {
throw new NumberFormatException();
}
out[off + i / 2] = (byte) (b1 << 4 | b2);
}
}
}

View File

@ -72,7 +72,7 @@ public class DfuFlasher {
wnd.append("Using selected " + port + "\n"); wnd.append("Using selected " + port + "\n");
IoStream stream = SerialIoStreamJSerialComm.openPort(port); IoStream stream = SerialIoStreamJSerialComm.openPort(port);
AtomicReference<String> signature = new AtomicReference<>(); AtomicReference<String> signature = new AtomicReference<>();
new SerialAutoChecker(port, new CountDownLatch(1)).checkResponse(stream, new Function<SerialAutoChecker.CallbackContext, Void>() { new SerialAutoChecker(PortDetector.DetectorMode.DETECT_TS, port, new CountDownLatch(1)).checkResponse(stream, new Function<SerialAutoChecker.CallbackContext, Void>() {
@Override @Override
public Void apply(SerialAutoChecker.CallbackContext callbackContext) { public Void apply(SerialAutoChecker.CallbackContext callbackContext) {
signature.set(callbackContext.getSignature()); signature.set(callbackContext.getSignature());

View File

@ -65,6 +65,7 @@ public class ConsoleTools {
registerTool("network_connector", strings -> NetworkConnectorStartup.start(), "Connect your rusEFI ECU to rusEFI Online"); registerTool("network_connector", strings -> NetworkConnectorStartup.start(), "Connect your rusEFI ECU to rusEFI Online");
registerTool("network_authenticator", strings -> LocalApplicationProxy.start(), "rusEFI Online Authenticator"); registerTool("network_authenticator", strings -> LocalApplicationProxy.start(), "rusEFI Online Authenticator");
registerTool("elm327_connector", strings -> Elm327ConnectorStartup.start(), "Connect your rusEFI ECU using ELM327 CAN-bus adapter");
registerTool("print_auth_token", args -> printAuthToken(), "Print current rusEFI Online authentication token."); registerTool("print_auth_token", args -> printAuthToken(), "Print current rusEFI Online authentication token.");
registerTool("print_vehicle_token", args -> printVehicleToken(), "Prints vehicle access token."); registerTool("print_vehicle_token", args -> printVehicleToken(), "Prints vehicle access token.");

View File

@ -15,7 +15,7 @@ public class Elm327ConnectorStartup {
return; return;
} }
(new Elm327Connector()).start(autoDetectedPort); new Elm327Connector().start(autoDetectedPort);
log.info("Running Elm327 connector for " + autoDetectedPort); log.info("Running Elm327 connector for " + autoDetectedPort);
} }
} }