diff --git a/src/com/romraider/io/connection/ConnectionManager.java b/src/com/romraider/io/connection/ConnectionManager.java new file mode 100644 index 00000000..46403e9b --- /dev/null +++ b/src/com/romraider/io/connection/ConnectionManager.java @@ -0,0 +1,31 @@ +/* + * + * RomRaider Open-Source Tuning, Logging and Reflashing + * Copyright (C) 2006-2008 RomRaider.com + * + * 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 2 of the License, or + * (at your option) 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, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +package com.romraider.io.connection; + +public interface ConnectionManager { + + void send(byte[] request, byte[] response, long sendTimeout); + + byte[] send(byte[] bytes, long maxWait); + + void close(); +} diff --git a/src/com/romraider/io/j2534/api/J2534ConnectionManager.java b/src/com/romraider/io/j2534/api/J2534ConnectionManager.java new file mode 100644 index 00000000..d42ed2dc --- /dev/null +++ b/src/com/romraider/io/j2534/api/J2534ConnectionManager.java @@ -0,0 +1,89 @@ +/* + * + * RomRaider Open-Source Tuning, Logging and Reflashing + * Copyright (C) 2006-2008 RomRaider.com + * + * 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 2 of the License, or + * (at your option) 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, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +package com.romraider.io.j2534.api; + +import com.romraider.io.connection.ConnectionManager; +import com.romraider.io.connection.ConnectionProperties; +import com.romraider.io.j2534.op20.J2534OpenPort20; +import static com.romraider.io.j2534.op20.OpenPort20.CONFIG_P1_MAX; +import static com.romraider.io.j2534.op20.OpenPort20.CONFIG_P3_MIN; +import static com.romraider.io.j2534.op20.OpenPort20.CONFIG_P4_MIN; +import static com.romraider.io.j2534.op20.OpenPort20.FLAG_ISO9141_NO_CHECKSUM; +import static com.romraider.io.j2534.op20.OpenPort20.PROTOCOL_ISO9141; +import static com.romraider.util.ParamChecker.checkNotNull; +import org.apache.log4j.Logger; +import static org.apache.log4j.Logger.getLogger; + +public final class J2534ConnectionManager implements ConnectionManager { + private static final Logger LOGGER = getLogger(J2534ConnectionManager.class); + private final J2534 api = new J2534OpenPort20(PROTOCOL_ISO9141); + private final ConnectionProperties connectionProperties; + private int channelId; + private int deviceId; + private int msgId; + + public J2534ConnectionManager(ConnectionProperties connectionProperties) { + checkNotNull(connectionProperties, "connectionProperties"); + this.connectionProperties = connectionProperties; + initJ2534(connectionProperties); + } + + private void initJ2534(ConnectionProperties connectionProperties) { + this.deviceId = api.open(); + version(deviceId); + this.channelId = api.connect(deviceId, FLAG_ISO9141_NO_CHECKSUM, connectionProperties.getBaudRate()); + setConfig(channelId); + this.msgId = api.startPassMsgFilter(channelId, (byte) 0x00, (byte) 0x00); + } + + // Send request and wait for response with known length + public void send(byte[] request, byte[] response, long timeout) { + checkNotNull(request, "request"); + checkNotNull(request, "response"); + // FIX - Complete! + } + + // Send request and wait specified time for response with unknown length + public byte[] send(byte[] bytes, long maxWait) { + checkNotNull(bytes, "bytes"); + api.writeMsg(channelId, bytes, connectionProperties.getSendTimeout()); + return api.readMsg(channelId, maxWait); + } + + public void close() { + api.stopMsgFilter(channelId, msgId); + api.disconnect(channelId); + api.close(deviceId); + } + + private void version(int deviceId) { + Version version = api.readVersion(deviceId); + System.out.println("Version => firmware: " + version.firmware + ", dll: " + version.dll + ", api: " + version.api); + } + + private void setConfig(int channelId) { + ConfigItem p1Max = new ConfigItem(CONFIG_P1_MAX, 2); + ConfigItem p3Min = new ConfigItem(CONFIG_P3_MIN, 0); + ConfigItem p4Min = new ConfigItem(CONFIG_P4_MIN, 0); + api.setConfig(channelId, p1Max, p3Min, p4Min); + } +} \ No newline at end of file diff --git a/src/com/romraider/io/j2534/op20/J2534OpenPort20.java b/src/com/romraider/io/j2534/op20/J2534OpenPort20.java index 45e458a0..a5c222eb 100644 --- a/src/com/romraider/io/j2534/op20/J2534OpenPort20.java +++ b/src/com/romraider/io/j2534/op20/J2534OpenPort20.java @@ -131,7 +131,7 @@ public final class J2534OpenPort20 implements J2534 { private boolean isResponse(PassThruMessage msg) { if (msg.RxStatus != 0x00) return false; - return msg.Timestamp > 0; + return msg.Timestamp != 0; } private PassThruMessage doReadMsg(int channelId) { diff --git a/src/com/romraider/io/serial/connection/SerialConnectionManager.java b/src/com/romraider/io/serial/connection/SerialConnectionManager.java index 50c5a4d7..8d9400fc 100644 --- a/src/com/romraider/io/serial/connection/SerialConnectionManager.java +++ b/src/com/romraider/io/serial/connection/SerialConnectionManager.java @@ -21,11 +21,67 @@ package com.romraider.io.serial.connection; -public interface SerialConnectionManager { +import com.romraider.io.connection.ConnectionManager; +import com.romraider.io.connection.ConnectionProperties; +import static com.romraider.util.HexUtil.asHex; +import static com.romraider.util.ParamChecker.checkNotNull; +import static com.romraider.util.ParamChecker.checkNotNullOrEmpty; +import static com.romraider.util.ThreadUtil.sleep; +import org.apache.log4j.Logger; +import static org.apache.log4j.Logger.getLogger; +import static java.lang.System.currentTimeMillis; - void send(byte[] request, byte[] response, long sendTimeout); +public final class SerialConnectionManager implements ConnectionManager { + private static final Logger LOGGER = getLogger(SerialConnectionManager.class); + private final SerialConnection connection; - byte[] send(byte[] bytes, long maxWait); + public SerialConnectionManager(String portName, ConnectionProperties connectionProperties) { + checkNotNullOrEmpty(portName, "portName"); + checkNotNull(connectionProperties, "connectionProperties"); + // Use TestSerialConnection for testing!! + connection = new SerialConnectionImpl(portName, connectionProperties); +// connection = new TestSerialConnection(portName, connectionProperties); + } - void close(); + // Send request and wait for response with known length + public void send(byte[] request, byte[] response, long timeout) { + checkNotNull(request, "request"); + checkNotNull(request, "response"); + connection.readStaleData(); + connection.write(request); + while (connection.available() < response.length) { + sleep(1); + timeout -= 1; + if (timeout <= 0) { + byte[] badBytes = new byte[connection.available()]; + connection.read(badBytes); + LOGGER.debug("Bad response (read timeout): " + asHex(badBytes)); + break; + } + } + connection.read(response); + } + + // Send request and wait specified time for response with unknown length + public byte[] send(byte[] bytes, long maxWait) { + checkNotNull(bytes, "bytes"); + connection.readStaleData(); + connection.write(bytes); + int available = 0; + boolean keepLooking = true; + long lastChange = currentTimeMillis(); + while (keepLooking) { + sleep(2); + if (connection.available() != available) { + available = connection.available(); + lastChange = currentTimeMillis(); + } + keepLooking = (currentTimeMillis() - lastChange) < maxWait; + } + return connection.readAvailable(); + } + + public void close() { + connection.close(); + } } diff --git a/src/com/romraider/io/serial/connection/SerialConnectionManagerImpl.java b/src/com/romraider/io/serial/connection/SerialConnectionManagerImpl.java deleted file mode 100644 index b2882a8f..00000000 --- a/src/com/romraider/io/serial/connection/SerialConnectionManagerImpl.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * - * RomRaider Open-Source Tuning, Logging and Reflashing - * Copyright (C) 2006-2008 RomRaider.com - * - * 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 2 of the License, or - * (at your option) 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, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - */ - -package com.romraider.io.serial.connection; - -import com.romraider.io.connection.ConnectionProperties; -import static com.romraider.util.HexUtil.asHex; -import static com.romraider.util.ParamChecker.checkNotNull; -import static com.romraider.util.ParamChecker.checkNotNullOrEmpty; -import static com.romraider.util.ThreadUtil.sleep; -import org.apache.log4j.Logger; -import static org.apache.log4j.Logger.getLogger; -import static java.lang.System.currentTimeMillis; - -public final class SerialConnectionManagerImpl implements SerialConnectionManager { - private static final Logger LOGGER = getLogger(SerialConnectionManagerImpl.class); - private final SerialConnection connection; - - public SerialConnectionManagerImpl(String portName, ConnectionProperties connectionProperties) { - checkNotNullOrEmpty(portName, "portName"); - checkNotNull(connectionProperties, "connectionProperties"); - // Use TestSerialConnection for testing!! - connection = new SerialConnectionImpl(portName, connectionProperties); -// connection = new TestSerialConnection(portName, connectionProperties); - } - - // Send request and wait for response with known length - public void send(byte[] request, byte[] response, long timeout) { - checkNotNull(request, "request"); - checkNotNull(request, "response"); - connection.readStaleData(); - connection.write(request); - while (connection.available() < response.length) { - sleep(1); - timeout -= 1; - if (timeout <= 0) { - byte[] badBytes = new byte[connection.available()]; - connection.read(badBytes); - LOGGER.debug("Bad response (read timeout): " + asHex(badBytes)); - break; - } - } - connection.read(response); - } - - // Send request and wait specified time for response with unknown length - public byte[] send(byte[] bytes, long maxWait) { - checkNotNull(bytes, "bytes"); - connection.readStaleData(); - connection.write(bytes); - int available = 0; - boolean keepLooking = true; - long lastChange = currentTimeMillis(); - while (keepLooking) { - sleep(2); - if (connection.available() != available) { - available = connection.available(); - lastChange = currentTimeMillis(); - } - keepLooking = (currentTimeMillis() - lastChange) < maxWait; - } - return connection.readAvailable(); - } - - public void close() { - connection.close(); - } -} diff --git a/src/com/romraider/logger/ecu/comms/io/connection/SSMLoggerConnection.java b/src/com/romraider/logger/ecu/comms/io/connection/SSMLoggerConnection.java index e1144bfe..a57f29a4 100644 --- a/src/com/romraider/logger/ecu/comms/io/connection/SSMLoggerConnection.java +++ b/src/com/romraider/logger/ecu/comms/io/connection/SSMLoggerConnection.java @@ -21,9 +21,9 @@ package com.romraider.logger.ecu.comms.io.connection; +import com.romraider.io.connection.ConnectionManager; import com.romraider.io.connection.ConnectionProperties; import com.romraider.io.serial.connection.SerialConnectionManager; -import com.romraider.io.serial.connection.SerialConnectionManagerImpl; import com.romraider.logger.ecu.comms.io.protocol.LoggerProtocol; import com.romraider.logger.ecu.comms.io.protocol.SSMLoggerProtocol; import com.romraider.logger.ecu.comms.query.EcuQuery; @@ -34,12 +34,12 @@ import java.util.Collection; public final class SSMLoggerConnection implements LoggerConnection { private static final long SEND_TIMEOUT = 2000L; private final LoggerProtocol protocol = new SSMLoggerProtocol(); - private final SerialConnectionManager manager; + private final ConnectionManager manager; public SSMLoggerConnection(String portName, ConnectionProperties connectionProperties) { checkNotNullOrEmpty(portName, "portName"); checkNotNull(connectionProperties); - this.manager = new SerialConnectionManagerImpl(portName, connectionProperties); + this.manager = new SerialConnectionManager(portName, connectionProperties); } public void sendAddressReads(Collection queries) { diff --git a/src/com/romraider/logger/ecu/comms/manager/QueryManagerImpl.java b/src/com/romraider/logger/ecu/comms/manager/QueryManagerImpl.java index cca4e7a6..d3f93a06 100644 --- a/src/com/romraider/logger/ecu/comms/manager/QueryManagerImpl.java +++ b/src/com/romraider/logger/ecu/comms/manager/QueryManagerImpl.java @@ -22,11 +22,11 @@ package com.romraider.logger.ecu.comms.manager; import com.romraider.Settings; +import com.romraider.io.connection.ConnectionManager; import com.romraider.io.connection.ConnectionProperties; +import com.romraider.io.j2534.api.J2534ConnectionManager; import com.romraider.io.protocol.Protocol; import static com.romraider.io.protocol.ProtocolFactory.getProtocol; -import com.romraider.io.serial.connection.SerialConnectionManager; -import com.romraider.io.serial.connection.SerialConnectionManagerImpl; import com.romraider.logger.ecu.comms.query.EcuInitCallback; import com.romraider.logger.ecu.comms.query.EcuQuery; import com.romraider.logger.ecu.comms.query.EcuQueryImpl; @@ -143,12 +143,13 @@ public final class QueryManagerImpl implements QueryManager { try { Protocol protocol = getProtocol(settings.getLoggerProtocol()); ConnectionProperties connectionProperties = settings.getLoggerConnectionProperties(); - SerialConnectionManager connectionManager = new SerialConnectionManagerImpl(settings.getLoggerPort(), connectionProperties); +// ConnectionManager connectionManager = new SerialConnectionManager(settings.getLoggerPort(), connectionProperties); + ConnectionManager connectionManager = new J2534ConnectionManager(connectionProperties); try { messageListener.reportMessage("Sending ECU Init..."); byte[] request = protocol.constructEcuInitRequest(); LOGGER.debug("Ecu Init Request ---> " + asHex(request)); - byte[] response = connectionManager.send(request, connectionProperties.getSendTimeout()); + byte[] response = connectionManager.send(request, 500L); byte[] processedResponse = protocol.preprocessResponse(request, response); protocol.checkValidEcuInitResponse(processedResponse); LOGGER.debug("Ecu Init Response <--- " + asHex(processedResponse)); diff --git a/src/com/romraider/logger/ecu/comms/reset/ResetManagerImpl.java b/src/com/romraider/logger/ecu/comms/reset/ResetManagerImpl.java index 74c1c9ba..0604eefb 100644 --- a/src/com/romraider/logger/ecu/comms/reset/ResetManagerImpl.java +++ b/src/com/romraider/logger/ecu/comms/reset/ResetManagerImpl.java @@ -22,11 +22,11 @@ package com.romraider.logger.ecu.comms.reset; import com.romraider.Settings; +import com.romraider.io.connection.ConnectionManager; import com.romraider.io.connection.ConnectionProperties; import com.romraider.io.protocol.Protocol; import static com.romraider.io.protocol.ProtocolFactory.getProtocol; import com.romraider.io.serial.connection.SerialConnectionManager; -import com.romraider.io.serial.connection.SerialConnectionManagerImpl; import com.romraider.logger.ecu.ui.MessageListener; import static com.romraider.util.HexUtil.asHex; import static com.romraider.util.ParamChecker.checkNotNull; @@ -47,7 +47,7 @@ public final class ResetManagerImpl implements ResetManager { try { Protocol protocol = getProtocol(settings.getLoggerProtocol()); ConnectionProperties connectionProperties = settings.getLoggerConnectionProperties(); - SerialConnectionManager connectionManager = new SerialConnectionManagerImpl(settings.getLoggerPort(), connectionProperties); + ConnectionManager connectionManager = new SerialConnectionManager(settings.getLoggerPort(), connectionProperties); try { messageListener.reportMessage("Sending ECU Reset..."); byte[] request = protocol.constructEcuResetRequest(); diff --git a/src/com/romraider/ramtune/test/command/executor/CommandExecutorImpl.java b/src/com/romraider/ramtune/test/command/executor/CommandExecutorImpl.java index d031bf26..ca4e0eee 100644 --- a/src/com/romraider/ramtune/test/command/executor/CommandExecutorImpl.java +++ b/src/com/romraider/ramtune/test/command/executor/CommandExecutorImpl.java @@ -21,9 +21,9 @@ package com.romraider.ramtune.test.command.executor; +import com.romraider.io.connection.ConnectionManager; import com.romraider.io.connection.ConnectionProperties; import com.romraider.io.serial.connection.SerialConnectionManager; -import com.romraider.io.serial.connection.SerialConnectionManagerImpl; import static com.romraider.util.ParamChecker.checkNotNull; import static com.romraider.util.ParamChecker.checkNotNullOrEmpty; @@ -39,7 +39,7 @@ public final class CommandExecutorImpl implements CommandExecutor { } public byte[] executeCommand(byte[] command) { - SerialConnectionManager connectionManager = new SerialConnectionManagerImpl(port, connectionProperties); + ConnectionManager connectionManager = new SerialConnectionManager(port, connectionProperties); try { int timeout = connectionProperties.getSendTimeout(); return connectionManager.send(command, timeout);