From c4114ab7435765a1ecc9112cb00f169d48bb73c1 Mon Sep 17 00:00:00 2001 From: kascade Date: Tue, 23 Sep 2008 12:28:57 +0000 Subject: [PATCH] add j2534 support git-svn-id: https://svn2.assembla.com/svn/romraider/trunk@156 38686702-15cf-42e4-a595-3071df8bf5ea --- .../io/connection/ConnectionManager.java | 1 - .../io/j2534/api/J2534ConnectionManager.java | 36 +++++++++++----- .../io/j2534/api/J2534Exception.java | 4 ++ .../comms/io/connection/LoggerConnection.java | 5 ++- .../connection/LoggerConnectionFactory.java | 27 +++++++++--- .../io/connection/SSMLoggerConnection.java | 42 ++++++++++++------- .../ecu/comms/io/protocol/LoggerProtocol.java | 9 ++++ .../comms/io/protocol/SSMLoggerProtocol.java | 24 +++++++++++ .../ecu/comms/manager/QueryManagerImpl.java | 24 +++-------- .../manager/TransmissionManagerImpl.java | 25 ++++------- .../ecu/comms/reset/ResetManagerImpl.java | 23 +++------- 11 files changed, 135 insertions(+), 85 deletions(-) diff --git a/src/com/romraider/io/connection/ConnectionManager.java b/src/com/romraider/io/connection/ConnectionManager.java index 46403e9b..e0ebf938 100644 --- a/src/com/romraider/io/connection/ConnectionManager.java +++ b/src/com/romraider/io/connection/ConnectionManager.java @@ -22,7 +22,6 @@ package com.romraider.io.connection; public interface ConnectionManager { - void send(byte[] request, byte[] response, long sendTimeout); byte[] send(byte[] bytes, long maxWait); diff --git a/src/com/romraider/io/j2534/api/J2534ConnectionManager.java b/src/com/romraider/io/j2534/api/J2534ConnectionManager.java index 4c3d2aa2..e07407c4 100644 --- a/src/com/romraider/io/j2534/api/J2534ConnectionManager.java +++ b/src/com/romraider/io/j2534/api/J2534ConnectionManager.java @@ -43,6 +43,7 @@ public final class J2534ConnectionManager implements ConnectionManager { public J2534ConnectionManager(ConnectionProperties connectionProperties) { checkNotNull(connectionProperties, "connectionProperties"); initJ2534(connectionProperties.getBaudRate()); + LOGGER.info("J2534 connection initialised"); } // Send request and wait for response with known length @@ -66,14 +67,21 @@ public final class J2534ConnectionManager implements ConnectionManager { stopMsgFilter(); disconnectChannel(); closeDevice(); + resetHandles(); + LOGGER.info("J2534 connection closed"); } private void initJ2534(int baudRate) { - this.deviceId = api.open(); - version(deviceId); - this.channelId = api.connect(deviceId, FLAG_ISO9141_NO_CHECKSUM, baudRate); - setConfig(channelId); - this.msgId = api.startPassMsgFilter(channelId, (byte) 0x00, (byte) 0x00); + deviceId = api.open(); + try { + version(deviceId); + channelId = api.connect(deviceId, FLAG_ISO9141_NO_CHECKSUM, baudRate); + setConfig(channelId); + msgId = api.startPassMsgFilter(channelId, (byte) 0x00, (byte) 0x00); + } catch (Exception e) { + close(); + throw new J2534Exception("Error during J2534 init: " + e.getMessage(), e); + } } private void version(int deviceId) { @@ -91,25 +99,31 @@ public final class J2534ConnectionManager implements ConnectionManager { private void stopMsgFilter() { try { - api.stopMsgFilter(channelId, msgId); + if (channelId > 0 && msgId > 0) api.stopMsgFilter(channelId, msgId); } catch (Exception e) { - LOGGER.warn("Error stopping msg filter"); + LOGGER.warn("Error stopping msg filter: " + e.getMessage()); } } private void disconnectChannel() { try { - api.disconnect(channelId); + if (channelId > 0) api.disconnect(channelId); } catch (Exception e) { - LOGGER.warn("Error disconnecting channel"); + LOGGER.warn("Error disconnecting channel: " + e.getMessage()); } } private void closeDevice() { try { - api.close(deviceId); + if (deviceId > 0) api.close(deviceId); } catch (Exception e) { - LOGGER.warn("Error closing device"); + LOGGER.warn("Error closing device: " + e.getMessage()); } } + + private void resetHandles() { + channelId = 0; + deviceId = 0; + msgId = 0; + } } \ No newline at end of file diff --git a/src/com/romraider/io/j2534/api/J2534Exception.java b/src/com/romraider/io/j2534/api/J2534Exception.java index 01904092..0ff3399c 100644 --- a/src/com/romraider/io/j2534/api/J2534Exception.java +++ b/src/com/romraider/io/j2534/api/J2534Exception.java @@ -4,4 +4,8 @@ public final class J2534Exception extends RuntimeException { public J2534Exception(String msg) { super(msg); } + + public J2534Exception(String msg, Throwable t) { + super(msg, t); + } } diff --git a/src/com/romraider/logger/ecu/comms/io/connection/LoggerConnection.java b/src/com/romraider/logger/ecu/comms/io/connection/LoggerConnection.java index 8503807d..3d5412a3 100644 --- a/src/com/romraider/logger/ecu/comms/io/connection/LoggerConnection.java +++ b/src/com/romraider/logger/ecu/comms/io/connection/LoggerConnection.java @@ -21,13 +21,16 @@ package com.romraider.logger.ecu.comms.io.connection; +import com.romraider.logger.ecu.comms.query.EcuInitCallback; import com.romraider.logger.ecu.comms.query.EcuQuery; import java.util.Collection; public interface LoggerConnection { + void ecuReset(); + + void ecuInit(EcuInitCallback callback); void sendAddressReads(Collection queries); void close(); - } diff --git a/src/com/romraider/logger/ecu/comms/io/connection/LoggerConnectionFactory.java b/src/com/romraider/logger/ecu/comms/io/connection/LoggerConnectionFactory.java index cca22f93..d805fbd3 100644 --- a/src/com/romraider/logger/ecu/comms/io/connection/LoggerConnectionFactory.java +++ b/src/com/romraider/logger/ecu/comms/io/connection/LoggerConnectionFactory.java @@ -21,23 +21,38 @@ package com.romraider.logger.ecu.comms.io.connection; +import com.romraider.io.connection.ConnectionManager; import com.romraider.io.connection.ConnectionProperties; +import com.romraider.io.j2534.api.J2534ConnectionManager; +import com.romraider.io.serial.connection.SerialConnectionManager; import com.romraider.logger.ecu.exception.UnsupportedProtocolException; +import org.apache.log4j.Logger; +import static org.apache.log4j.Logger.getLogger; public final class LoggerConnectionFactory { - private static final LoggerConnectionFactory INSTANCE = new LoggerConnectionFactory(); + private static final Logger LOGGER = getLogger(LoggerConnectionFactory.class); private LoggerConnectionFactory() { } - public static LoggerConnectionFactory getInstance() { - return INSTANCE; + public static LoggerConnection getConnection(String protocolName, String portName, ConnectionProperties connectionProperties) { + ConnectionManager manager = getManager(portName, connectionProperties); + return instantiateConnection(protocolName, manager); } - public LoggerConnection getLoggerConnection(String protocolName, String portName, ConnectionProperties connectionProperties) { + private static ConnectionManager getManager(String portName, ConnectionProperties connectionProperties) { try { - Class cls = Class.forName(this.getClass().getPackage().getName() + "." + protocolName + "LoggerConnection"); - return (LoggerConnection) cls.getConstructor(String.class, ConnectionProperties.class).newInstance(portName, connectionProperties); + return new J2534ConnectionManager(connectionProperties); + } catch (Exception e) { + LOGGER.info("J2534 connection not available [" + e.getMessage() + "], trying serial connection..."); + return new SerialConnectionManager(portName, connectionProperties); + } + } + + private static LoggerConnection instantiateConnection(String protocolName, ConnectionManager manager) { + try { + Class cls = Class.forName(LoggerConnectionFactory.class.getPackage().getName() + "." + protocolName + "LoggerConnection"); + return (LoggerConnection) cls.getConstructor(ConnectionManager.class).newInstance(manager); } catch (Exception e) { throw new UnsupportedProtocolException("'" + protocolName + "' is not a supported protocol", e); } 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 10b00406..4f41c6de 100644 --- a/src/com/romraider/logger/ecu/comms/io/connection/SSMLoggerConnection.java +++ b/src/com/romraider/logger/ecu/comms/io/connection/SSMLoggerConnection.java @@ -22,38 +22,52 @@ package com.romraider.logger.ecu.comms.io.connection; import com.romraider.io.connection.ConnectionManager; -import com.romraider.io.connection.ConnectionProperties; -import com.romraider.io.j2534.api.J2534ConnectionManager; 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.EcuInitCallback; import com.romraider.logger.ecu.comms.query.EcuQuery; +import static com.romraider.util.HexUtil.asHex; import static com.romraider.util.ParamChecker.checkNotNull; -import static com.romraider.util.ParamChecker.checkNotNullOrEmpty; +import org.apache.log4j.Logger; +import static org.apache.log4j.Logger.getLogger; import java.util.Collection; public final class SSMLoggerConnection implements LoggerConnection { + private static final Logger LOGGER = getLogger(SSMLoggerConnection.class); private static final long SEND_TIMEOUT = 2000L; private final LoggerProtocol protocol = new SSMLoggerProtocol(); private final ConnectionManager manager; - public SSMLoggerConnection(String portName, ConnectionProperties connectionProperties) { - checkNotNullOrEmpty(portName, "portName"); - checkNotNull(connectionProperties); -// this.manager = new SerialConnectionManager(portName, connectionProperties); - this.manager = new J2534ConnectionManager(connectionProperties); + public SSMLoggerConnection(ConnectionManager manager) { + checkNotNull(manager, "manager"); + this.manager = manager; + } + + public void ecuReset() { + byte[] request = protocol.constructEcuResetRequest(); + LOGGER.debug("Ecu Reset Request ---> " + asHex(request)); + byte[] response = manager.send(request, SEND_TIMEOUT); + byte[] processedResponse = protocol.preprocessResponse(request, response); + LOGGER.debug("Ecu Reset Response <--- " + asHex(processedResponse)); + protocol.processEcuResetResponse(processedResponse); + } + + public void ecuInit(EcuInitCallback callback) { + byte[] request = protocol.constructEcuInitRequest(); + LOGGER.debug("Ecu Init Request ---> " + asHex(request)); + byte[] response = manager.send(request, SEND_TIMEOUT); + byte[] processedResponse = protocol.preprocessResponse(request, response); + LOGGER.debug("Ecu Init Response <--- " + asHex(processedResponse)); + protocol.processEcuInitResponse(callback, processedResponse); } public void sendAddressReads(Collection queries) { byte[] request = protocol.constructReadAddressRequest(queries); byte[] response = protocol.constructReadAddressResponse(queries); - + LOGGER.trace("ECU Request ---> " + asHex(request)); manager.send(request, response, SEND_TIMEOUT); - byte[] processedResponse = protocol.preprocessResponse(request, response); - -// LOGGER.trace("ECU Request ---> " + asHex(request)); -// LOGGER.trace("ECU Response <--- " + asHex(processedResponse)); - + LOGGER.trace("ECU Response <--- " + asHex(processedResponse)); protocol.processReadAddressResponses(queries, processedResponse); } diff --git a/src/com/romraider/logger/ecu/comms/io/protocol/LoggerProtocol.java b/src/com/romraider/logger/ecu/comms/io/protocol/LoggerProtocol.java index bd4942af..2bc4dc7c 100644 --- a/src/com/romraider/logger/ecu/comms/io/protocol/LoggerProtocol.java +++ b/src/com/romraider/logger/ecu/comms/io/protocol/LoggerProtocol.java @@ -21,16 +21,25 @@ package com.romraider.logger.ecu.comms.io.protocol; +import com.romraider.logger.ecu.comms.query.EcuInitCallback; import com.romraider.logger.ecu.comms.query.EcuQuery; import java.util.Collection; public interface LoggerProtocol { + byte[] constructEcuInitRequest(); + + byte[] constructEcuResetRequest(); + byte[] constructReadAddressRequest(Collection queries); byte[] constructReadAddressResponse(Collection queries); byte[] preprocessResponse(byte[] request, byte[] response); + void processEcuInitResponse(EcuInitCallback callback, byte[] response); + + void processEcuResetResponse(byte[] response); + void processReadAddressResponses(Collection queries, byte[] response); } diff --git a/src/com/romraider/logger/ecu/comms/io/protocol/SSMLoggerProtocol.java b/src/com/romraider/logger/ecu/comms/io/protocol/SSMLoggerProtocol.java index 96b0971d..c82243f2 100644 --- a/src/com/romraider/logger/ecu/comms/io/protocol/SSMLoggerProtocol.java +++ b/src/com/romraider/logger/ecu/comms/io/protocol/SSMLoggerProtocol.java @@ -29,7 +29,10 @@ import static com.romraider.io.protocol.ssm.SSMProtocol.REQUEST_NON_DATA_BYTES; import static com.romraider.io.protocol.ssm.SSMProtocol.RESPONSE_NON_DATA_BYTES; import static com.romraider.io.protocol.ssm.SSMResponseProcessor.extractResponseData; import static com.romraider.io.protocol.ssm.SSMResponseProcessor.filterRequestFromResponse; +import com.romraider.logger.ecu.comms.query.EcuInit; +import com.romraider.logger.ecu.comms.query.EcuInitCallback; import com.romraider.logger.ecu.comms.query.EcuQuery; +import static com.romraider.util.ParamChecker.checkNotNull; import static com.romraider.util.ParamChecker.checkNotNullOrEmpty; import java.util.ArrayList; import java.util.Collection; @@ -39,6 +42,14 @@ import java.util.Map; public final class SSMLoggerProtocol implements LoggerProtocol { private final Protocol protocol = new SSMProtocol(); + public byte[] constructEcuInitRequest() { + return protocol.constructEcuInitRequest(); + } + + public byte[] constructEcuResetRequest() { + return protocol.constructEcuResetRequest(); + } + public byte[] constructReadAddressRequest(Collection queries) { Collection filteredQueries = filterDuplicates(queries); return protocol.constructReadAddressRequest(convertToByteAddresses(filteredQueries)); @@ -60,6 +71,19 @@ public final class SSMLoggerProtocol implements LoggerProtocol { return filterRequestFromResponse(request, response); } + public void processEcuInitResponse(EcuInitCallback callback, byte[] response) { + checkNotNull(callback, "callback"); + checkNotNullOrEmpty(response, "response"); + protocol.checkValidEcuInitResponse(response); + EcuInit ecuInit = protocol.parseEcuInitResponse(response); + callback.callback(ecuInit); + } + + public void processEcuResetResponse(byte[] response) { + checkNotNullOrEmpty(response, "response"); + protocol.checkValidEcuResetResponse(response); + } + // processes the response bytes and sets individual responses on corresponding query objects @SuppressWarnings({"PointlessArithmeticExpression"}) public void processReadAddressResponses(Collection queries, byte[] response) { diff --git a/src/com/romraider/logger/ecu/comms/manager/QueryManagerImpl.java b/src/com/romraider/logger/ecu/comms/manager/QueryManagerImpl.java index d3f93a06..4248266c 100644 --- a/src/com/romraider/logger/ecu/comms/manager/QueryManagerImpl.java +++ b/src/com/romraider/logger/ecu/comms/manager/QueryManagerImpl.java @@ -22,11 +22,8 @@ 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.logger.ecu.comms.io.connection.LoggerConnection; +import static com.romraider.logger.ecu.comms.io.connection.LoggerConnectionFactory.getConnection; import com.romraider.logger.ecu.comms.query.EcuInitCallback; import com.romraider.logger.ecu.comms.query.EcuQuery; import com.romraider.logger.ecu.comms.query.EcuQueryImpl; @@ -43,7 +40,6 @@ import com.romraider.logger.ecu.ui.MessageListener; import com.romraider.logger.ecu.ui.StatusChangeListener; import com.romraider.logger.ecu.ui.handler.DataUpdateHandler; import com.romraider.logger.ecu.ui.handler.file.FileLoggerControllerSwitchMonitor; -import static com.romraider.util.HexUtil.asHex; import static com.romraider.util.ParamChecker.checkNotNull; import static com.romraider.util.ThreadUtil.runAsDaemon; import static com.romraider.util.ThreadUtil.sleep; @@ -141,23 +137,15 @@ public final class QueryManagerImpl implements QueryManager { private boolean doEcuInit() { try { - Protocol protocol = getProtocol(settings.getLoggerProtocol()); - ConnectionProperties connectionProperties = settings.getLoggerConnectionProperties(); -// ConnectionManager connectionManager = new SerialConnectionManager(settings.getLoggerPort(), connectionProperties); - ConnectionManager connectionManager = new J2534ConnectionManager(connectionProperties); + LoggerConnection connection = getConnection(settings.getLoggerProtocol(), settings.getLoggerPort(), + settings.getLoggerConnectionProperties()); try { messageListener.reportMessage("Sending ECU Init..."); - byte[] request = protocol.constructEcuInitRequest(); - LOGGER.debug("Ecu Init Request ---> " + asHex(request)); - byte[] response = connectionManager.send(request, 500L); - byte[] processedResponse = protocol.preprocessResponse(request, response); - protocol.checkValidEcuInitResponse(processedResponse); - LOGGER.debug("Ecu Init Response <--- " + asHex(processedResponse)); - ecuInitCallback.callback(protocol.parseEcuInitResponse(processedResponse)); + connection.ecuInit(ecuInitCallback); messageListener.reportMessage("Sending ECU Init...done."); return true; } finally { - connectionManager.close(); + connection.close(); } } catch (Exception e) { messageListener.reportMessage("Unable to send ECU init - check correct serial port has been selected, cable is connected and ignition is on."); diff --git a/src/com/romraider/logger/ecu/comms/manager/TransmissionManagerImpl.java b/src/com/romraider/logger/ecu/comms/manager/TransmissionManagerImpl.java index 08f859f4..c29bbc6c 100644 --- a/src/com/romraider/logger/ecu/comms/manager/TransmissionManagerImpl.java +++ b/src/com/romraider/logger/ecu/comms/manager/TransmissionManagerImpl.java @@ -23,16 +23,16 @@ package com.romraider.logger.ecu.comms.manager; import com.romraider.Settings; import com.romraider.logger.ecu.comms.io.connection.LoggerConnection; -import com.romraider.logger.ecu.comms.io.connection.LoggerConnectionFactory; +import static com.romraider.logger.ecu.comms.io.connection.LoggerConnectionFactory.getConnection; import com.romraider.logger.ecu.comms.query.EcuQuery; import com.romraider.logger.ecu.exception.NotConnectedException; -import com.romraider.logger.ecu.exception.SerialCommunicationException; import static com.romraider.util.ParamChecker.checkNotNull; import org.apache.log4j.Logger; +import static org.apache.log4j.Logger.getLogger; import java.util.Collection; public final class TransmissionManagerImpl implements TransmissionManager { - private static final Logger LOGGER = Logger.getLogger(TransmissionManagerImpl.class); + private static final Logger LOGGER = getLogger(TransmissionManagerImpl.class); private final Settings settings; private LoggerConnection connection; @@ -43,30 +43,21 @@ public final class TransmissionManagerImpl implements TransmissionManager { public void start() { try { - connection = LoggerConnectionFactory.getInstance().getLoggerConnection(settings.getLoggerProtocol(), settings.getLoggerPort(), - settings.getLoggerConnectionProperties()); - LOGGER.info("Connected to: " + settings.getLoggerPort() + "; using protocol: " + settings.getLoggerProtocol() + "; conn props: " - + settings.getLoggerConnectionProperties()); + connection = getConnection(settings.getLoggerProtocol(), settings.getLoggerPort(), settings.getLoggerConnectionProperties()); + LOGGER.info("Connected."); } catch (Throwable e) { stop(); - throw new SerialCommunicationException("Unable to connect to port: " + settings.getLoggerPort() + ", with protocol: " - + settings.getLoggerProtocol(), e); } } public void sendQueries(Collection queries) { checkNotNull(queries, "queries"); - if (connection != null) { - connection.sendAddressReads(queries); - } else { - throw new NotConnectedException("TransmissionManager must be started before queries can be sent!"); - } + if (connection == null) throw new NotConnectedException("TransmissionManager must be started before queries can be sent!"); + connection.sendAddressReads(queries); } public void stop() { - if (connection != null) { - connection.close(); - } + if (connection != null) connection.close(); LOGGER.info("Disconnected."); } diff --git a/src/com/romraider/logger/ecu/comms/reset/ResetManagerImpl.java b/src/com/romraider/logger/ecu/comms/reset/ResetManagerImpl.java index f0ba3429..6f3e71b8 100644 --- a/src/com/romraider/logger/ecu/comms/reset/ResetManagerImpl.java +++ b/src/com/romraider/logger/ecu/comms/reset/ResetManagerImpl.java @@ -22,13 +22,9 @@ 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.j2534.api.J2534ConnectionManager; -import com.romraider.io.protocol.Protocol; -import static com.romraider.io.protocol.ProtocolFactory.getProtocol; +import com.romraider.logger.ecu.comms.io.connection.LoggerConnection; +import static com.romraider.logger.ecu.comms.io.connection.LoggerConnectionFactory.getConnection; import com.romraider.logger.ecu.ui.MessageListener; -import static com.romraider.util.HexUtil.asHex; import static com.romraider.util.ParamChecker.checkNotNull; import org.apache.log4j.Logger; @@ -45,22 +41,15 @@ public final class ResetManagerImpl implements ResetManager { public boolean resetEcu() { try { - Protocol protocol = getProtocol(settings.getLoggerProtocol()); - ConnectionProperties connectionProperties = settings.getLoggerConnectionProperties(); -// ConnectionManager connectionManager = new SerialConnectionManager(settings.getLoggerPort(), connectionProperties); - ConnectionManager connectionManager = new J2534ConnectionManager(connectionProperties); + LoggerConnection connection = getConnection(settings.getLoggerProtocol(), settings.getLoggerPort(), + settings.getLoggerConnectionProperties()); try { messageListener.reportMessage("Sending ECU Reset..."); - byte[] request = protocol.constructEcuResetRequest(); - LOGGER.debug("Ecu Reset Request ---> " + asHex(request)); - byte[] response = connectionManager.send(request, 500L); - byte[] processedResponse = protocol.preprocessResponse(request, response); - protocol.checkValidEcuResetResponse(processedResponse); - LOGGER.debug("Ecu Reset Response <--- " + asHex(processedResponse)); + connection.ecuReset(); messageListener.reportMessage("Sending ECU Reset...done."); return true; } finally { - connectionManager.close(); + connection.close(); } } catch (Exception e) { messageListener.reportMessage("Unable to reset ecu - check correct serial port has been selected, cable is connected and ignition is on.");