diff --git a/src/enginuity/io/protocol/Protocol.java b/src/enginuity/io/protocol/Protocol.java index 457f375d..e4d49bf4 100644 --- a/src/enginuity/io/protocol/Protocol.java +++ b/src/enginuity/io/protocol/Protocol.java @@ -23,7 +23,6 @@ package enginuity.io.protocol; import enginuity.io.connection.ConnectionProperties; import enginuity.logger.ecu.comms.query.EcuInit; -import enginuity.logger.ecu.exception.InvalidResponseException; public interface Protocol { @@ -41,9 +40,13 @@ public interface Protocol { byte[] parseResponseData(byte[] processedResponse); - void checkValidEcuInitResponse(byte[] processedResponse) throws InvalidResponseException; + void checkValidEcuInitResponse(byte[] processedResponse); EcuInit parseEcuInitResponse(byte[] processedResponse); + byte[] constructEcuResetRequest(); + + void checkValidEcuResetResponse(byte[] processedResponse); + ConnectionProperties getDefaultConnectionProperties(); } diff --git a/src/enginuity/io/protocol/SSMProtocol.java b/src/enginuity/io/protocol/SSMProtocol.java index 467d9deb..e6575884 100644 --- a/src/enginuity/io/protocol/SSMProtocol.java +++ b/src/enginuity/io/protocol/SSMProtocol.java @@ -96,7 +96,7 @@ public final class SSMProtocol implements Protocol { return extractResponseData(processedResponse); } - public void checkValidEcuInitResponse(byte[] processedResponse) throws InvalidResponseException { + public void checkValidEcuInitResponse(byte[] processedResponse) { // response_header 3_unknown_bytes 5_ecu_id_bytes readable_params_switches... checksum // 80F01039FF A21011315258400673FACB842B83FEA800000060CED4FDB060000F200000000000DC0000551E30C0F222000040FB00E10000000000000000 59 checkNotNullOrEmpty(processedResponse, "processedResponse"); @@ -111,6 +111,23 @@ public final class SSMProtocol implements Protocol { return new SSMEcuInit(parseResponseData(processedResponse)); } + public byte[] constructEcuResetRequest() { + // 80 10 F0 05 B8 00 00 60 40 DD + //FIXME: Create a buildWriteAddressRequest() method + byte[] resetDataBytes = new byte[]{(byte) 0x00, (byte) 0x00, (byte) 0x60, (byte) 0x40}; + return buildRequest(WRITE_ADDRESS_COMMAND, false, resetDataBytes); + } + + public void checkValidEcuResetResponse(byte[] processedResponse) { + // 80 F0 10 02 F8 40 BA + checkNotNullOrEmpty(processedResponse, "processedResponse"); + validateResponse(processedResponse); + byte responseType = processedResponse[4]; + if (responseType != WRITE_ADDRESS_RESPONSE || processedResponse[5] != (byte) 0x40) { + throw new InvalidResponseException("Unexpected ECU Reset response: " + asHex(processedResponse)); + } + } + public ConnectionProperties getDefaultConnectionProperties() { return new ConnectionProperties() { diff --git a/src/enginuity/logger/ecu/EcuLogger.java b/src/enginuity/logger/ecu/EcuLogger.java index 197855c2..21db31c8 100644 --- a/src/enginuity/logger/ecu/EcuLogger.java +++ b/src/enginuity/logger/ecu/EcuLogger.java @@ -27,6 +27,8 @@ import enginuity.logger.ecu.comms.controller.LoggerController; import enginuity.logger.ecu.comms.controller.LoggerControllerImpl; import enginuity.logger.ecu.comms.query.EcuInit; import enginuity.logger.ecu.comms.query.EcuInitCallback; +import enginuity.logger.ecu.comms.reset.ResetManager; +import enginuity.logger.ecu.comms.reset.ResetManagerImpl; import enginuity.logger.ecu.definition.EcuDataLoader; import enginuity.logger.ecu.definition.EcuDataLoaderImpl; import enginuity.logger.ecu.definition.EcuDefinition; @@ -147,6 +149,7 @@ public final class EcuLogger extends JFrame implements WindowListener, PropertyC private static final String ECU_ID_LABEL = "ECU ID"; private Settings settings; private LoggerController controller; + private ResetManager resetManager; private JLabel messageLabel; private JLabel calIdLabel; private JLabel ecuIdLabel; @@ -228,6 +231,7 @@ public final class EcuLogger extends JFrame implements WindowListener, PropertyC dashboardUpdateHandler = new DashboardUpdateHandler(dashboardPanel); controller = new LoggerControllerImpl(settings, ecuInitCallback, this, liveDataUpdateHandler, graphUpdateHandler, dashboardUpdateHandler, fileUpdateHandler, TableUpdateHandler.getInstance()); + resetManager = new ResetManagerImpl(settings, this); messageLabel = new JLabel(ENGINUITY_ECU_LOGGER_TITLE); calIdLabel = new JLabel(buildEcuInfoLabelText(CAL_ID_LABEL, null)); ecuIdLabel = new JLabel(buildEcuInfoLabelText(ECU_ID_LABEL, null)); @@ -815,6 +819,10 @@ public final class EcuLogger extends JFrame implements WindowListener, PropertyC sleep(1000L); } + public boolean resetEcu() { + return resetManager.resetEcu(); + } + public void handleExit() { try { try { diff --git a/src/enginuity/logger/ecu/comms/reset/ResetManager.java b/src/enginuity/logger/ecu/comms/reset/ResetManager.java new file mode 100644 index 00000000..ad34d053 --- /dev/null +++ b/src/enginuity/logger/ecu/comms/reset/ResetManager.java @@ -0,0 +1,5 @@ +package enginuity.logger.ecu.comms.reset; + +public interface ResetManager { + boolean resetEcu(); +} diff --git a/src/enginuity/logger/ecu/comms/reset/ResetManagerImpl.java b/src/enginuity/logger/ecu/comms/reset/ResetManagerImpl.java new file mode 100644 index 00000000..aed47059 --- /dev/null +++ b/src/enginuity/logger/ecu/comms/reset/ResetManagerImpl.java @@ -0,0 +1,55 @@ +package enginuity.logger.ecu.comms.reset; + +import enginuity.Settings; +import enginuity.io.connection.EcuConnection; +import enginuity.io.connection.EcuConnectionImpl; +import enginuity.io.protocol.Protocol; +import enginuity.io.protocol.ProtocolFactory; +import enginuity.logger.ecu.ui.MessageListener; +import static enginuity.util.HexUtil.asHex; +import static enginuity.util.ParamChecker.checkNotNull; +import org.apache.log4j.Logger; + +public final class ResetManagerImpl implements ResetManager { + private static final Logger LOGGER = Logger.getLogger(ResetManagerImpl.class); + private final Settings settings; + private final MessageListener messageListener; + + public ResetManagerImpl(Settings settings, MessageListener messageListener) { + checkNotNull(settings, messageListener); + this.settings = settings; + this.messageListener = messageListener; + } + + public boolean resetEcu() { + try { + Protocol protocol = ProtocolFactory.getInstance().getProtocol(settings.getLoggerProtocol()); + EcuConnection ecuConnection = new EcuConnectionImpl(settings.getLoggerConnectionProperties(), settings.getLoggerPort()); + try { + messageListener.reportMessage("Sending ECU Reset..."); + byte[] request = protocol.constructEcuResetRequest(); + LOGGER.debug("Ecu Reset Request ---> " + asHex(request)); + byte[] response = ecuConnection.send(request); + byte[] processedResponse = protocol.preprocessResponse(request, response); + protocol.checkValidEcuResetResponse(processedResponse); + LOGGER.debug("Ecu Reset Response <--- " + asHex(processedResponse)); + messageListener.reportMessage("Sending ECU Reset...done."); + return true; + } finally { + ecuConnection.close(); + } + } catch (Exception e) { + messageListener.reportMessage("Unable to reset ecu - check correct serial port has been selected, cable is connected and ignition is on."); + logError(e); + return false; + } + } + + private void logError(Exception e) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Error sending ecu reset", e); + } else { + LOGGER.info("Error sending ecu reset: " + e.getMessage()); + } + } +} diff --git a/src/enginuity/logger/ecu/ui/swing/menubar/action/ResetEcuAction.java b/src/enginuity/logger/ecu/ui/swing/menubar/action/ResetEcuAction.java index 351bf956..d57898d9 100644 --- a/src/enginuity/logger/ecu/ui/swing/menubar/action/ResetEcuAction.java +++ b/src/enginuity/logger/ecu/ui/swing/menubar/action/ResetEcuAction.java @@ -2,6 +2,8 @@ package enginuity.logger.ecu.ui.swing.menubar.action; import enginuity.logger.ecu.EcuLogger; +import static javax.swing.JOptionPane.ERROR_MESSAGE; +import static javax.swing.JOptionPane.INFORMATION_MESSAGE; import static javax.swing.JOptionPane.OK_OPTION; import static javax.swing.JOptionPane.WARNING_MESSAGE; import static javax.swing.JOptionPane.YES_NO_OPTION; @@ -16,12 +18,14 @@ public final class ResetEcuAction extends AbstractAction { public void actionPerformed(ActionEvent actionEvent) { if (showConfirmation() == OK_OPTION) { - //TODO: Finish reset!! - /* - --> 80 10 F0 05 B8 00 00 60 40 DD - <-- 80 10 F0 05 B8 00 00 60 40 DD 80 F0 10 02 F8 40 BA - */ - showMessageDialog(logger, "Not yet implemented!"); +// logger.stopLogging(); + if (logger.resetEcu()) { + showMessageDialog(logger, "Reset Successful!\nTurn your ignition OFF and then\nback ON to complete the process.", + "Reset ECU", INFORMATION_MESSAGE); + } else { + showMessageDialog(logger, "Error performing reset.\nCheck the following:\n* Correct COM port selected\n" + + "* Cable is connected properly\n* Ignition is ON\n* Logger is stopped", "Reset ECU", ERROR_MESSAGE); + } } }