mirror of https://github.com/rusefi/RomRaider.git
refactoring: better type safety - explicit enum type instead of magic integers for state
This commit is contained in:
parent
8a1e84413d
commit
c1a20c3aac
|
@ -57,7 +57,7 @@ public final class J2534ConnectionISO15765 implements ConnectionManager {
|
|||
checkNotNull(response, "response");
|
||||
checkNotNull(pollState, "pollState");
|
||||
pollState.setFastPoll(false);
|
||||
pollState.setCurrentState(0);
|
||||
pollState.setCurrentState(PollingState.State.STATE_0);
|
||||
api.writeMsg(channelId, request, timeout, TxFlags.ISO15765_FRAME_PAD);
|
||||
final byte[] readMsg = api.readMsg(channelId, 1, timeout);
|
||||
System.arraycopy(readMsg, 0, response, 0, readMsg.length) ;
|
||||
|
|
|
@ -57,16 +57,17 @@ public final class J2534ConnectionISO9141 implements ConnectionManager {
|
|||
checkNotNull(response, "response");
|
||||
checkNotNull(pollState, "pollState");
|
||||
|
||||
if (pollState.getCurrentState() == 0 && pollState.getLastState() == 1) {
|
||||
if (pollState.getCurrentState() == PollingState.State.STATE_0 &&
|
||||
pollState.getLastState() == PollingState.State.STATE_1) {
|
||||
clearLine();
|
||||
}
|
||||
|
||||
if (pollState.getCurrentState() == 0) {
|
||||
if (pollState.getCurrentState() == PollingState.State.STATE_0) {
|
||||
api.writeMsg(channelId, request, timeout, TxFlags.NO_FLAGS);
|
||||
}
|
||||
api.readMsg(channelId, response, timeout);
|
||||
|
||||
if (pollState.getCurrentState() == 1){
|
||||
if (pollState.getCurrentState() == PollingState.State.STATE_1){
|
||||
if ( response[0] == (byte) 0x80
|
||||
&& response[1] == (byte) 0xF0
|
||||
&& (response[2] == (byte) 0x10 || response[2] == (byte) 0x18)
|
||||
|
|
|
@ -39,13 +39,13 @@ import com.romraider.logger.ecu.definition.Module;
|
|||
import com.romraider.logger.ecu.exception.UnsupportedProtocolException;
|
||||
|
||||
public final class DS2Protocol implements ProtocolDS2 {
|
||||
public static final byte[] READ_MEMORY_COMMAND = new byte[]{0x06, 0x00};
|
||||
public static final byte[] READ_ADDRESS_COMMAND = new byte[]{0x0B, 0x02, 0x0e};
|
||||
public static final byte[] ECU_INIT_COMMAND = new byte[]{0x00};
|
||||
public static final byte[] ECU_RESET_COMMAND = new byte[]{0x43};
|
||||
private static final byte[] READ_MEMORY_COMMAND = new byte[]{0x06, 0x00};
|
||||
private static final byte[] READ_ADDRESS_COMMAND = new byte[]{0x0B, 0x02, 0x0e};
|
||||
private static final byte[] ECU_INIT_COMMAND = new byte[]{0x00};
|
||||
private static final byte[] ECU_RESET_COMMAND = new byte[]{0x43};
|
||||
public static final byte VALID_RESPONSE = (byte) 0xA0;
|
||||
public static final int ADDRESS_SIZE = 3;
|
||||
public static final int DATA_SIZE = 1;
|
||||
private static final int ADDRESS_SIZE = 3;
|
||||
private static final int DATA_SIZE = 1;
|
||||
public static final int RESPONSE_NON_DATA_BYTES = 4;
|
||||
public static Module module;
|
||||
private final ByteArrayOutputStream bb = new ByteArrayOutputStream(255);
|
||||
|
|
|
@ -42,11 +42,11 @@ public final class DS2ResponseProcessor {
|
|||
checkNotNullOrEmpty(response, "response");
|
||||
checkNotNull(pollState, "pollState");
|
||||
byte[] filteredResponse = new byte[0];
|
||||
if (pollState.getCurrentState() == 0) {
|
||||
if (pollState.getCurrentState() == PollingState.State.STATE_0) {
|
||||
filteredResponse = new byte[response.length - request.length];
|
||||
System.arraycopy(response, request.length, filteredResponse, 0, filteredResponse.length);
|
||||
}
|
||||
if (pollState.getCurrentState() == 1) {
|
||||
if (pollState.getCurrentState() == PollingState.State.STATE_1) {
|
||||
filteredResponse = new byte[response.length];
|
||||
System.arraycopy(response, 0, filteredResponse, 0, filteredResponse.length);
|
||||
}
|
||||
|
|
|
@ -68,9 +68,9 @@ public final class SSMLoggerProtocol implements LoggerProtocol {
|
|||
numAddresses += (ecuQuery.getBytes().length / ADDRESS_SIZE);
|
||||
}
|
||||
switch (pollState.getCurrentState()) {
|
||||
case 0:
|
||||
case STATE_0:
|
||||
return new byte[(numAddresses * DATA_SIZE + RESPONSE_NON_DATA_BYTES) + (numAddresses * ADDRESS_SIZE + REQUEST_NON_DATA_BYTES)];
|
||||
case 1:
|
||||
case STATE_1:
|
||||
return new byte[(numAddresses * DATA_SIZE + RESPONSE_NON_DATA_BYTES)];
|
||||
default:
|
||||
throw new UnsupportedOperationException("Poll mode not supported:" + pollState.getCurrentState());
|
||||
|
|
|
@ -48,11 +48,11 @@ public final class SSMResponseProcessor {
|
|||
checkNotNullOrEmpty(response, "response");
|
||||
checkNotNull(pollState, "pollState");
|
||||
byte[] filteredResponse = new byte[0];
|
||||
if (request[4] != READ_ADDRESS_COMMAND || pollState.getCurrentState() == 0){
|
||||
if (request[4] != READ_ADDRESS_COMMAND || pollState.getCurrentState() == PollingState.State.STATE_0) {
|
||||
filteredResponse = new byte[response.length - request.length];
|
||||
System.arraycopy(response, request.length, filteredResponse, 0, filteredResponse.length);
|
||||
}
|
||||
if (request[4] == READ_ADDRESS_COMMAND && pollState.getCurrentState() == 1){
|
||||
if (request[4] == READ_ADDRESS_COMMAND && pollState.getCurrentState() == PollingState.State.STATE_1) {
|
||||
filteredResponse = new byte[response.length];
|
||||
System.arraycopy(response, 0, filteredResponse, 0, filteredResponse.length);
|
||||
}
|
||||
|
|
|
@ -60,11 +60,12 @@ public final class SerialConnectionManager implements ConnectionManager {
|
|||
checkNotNull(response, "response");
|
||||
checkNotNull(pollState, "pollState");
|
||||
|
||||
if (pollState.getCurrentState() == 0 && pollState.getLastState() == 1) {
|
||||
if (pollState.getCurrentState() == PollingState.State.STATE_0 &&
|
||||
pollState.getLastState() == PollingState.State.STATE_1) {
|
||||
clearLine();
|
||||
}
|
||||
|
||||
if (pollState.getCurrentState() == 0) {
|
||||
if (pollState.getCurrentState() == PollingState.State.STATE_0) {
|
||||
connection.readStaleData();
|
||||
connection.write(request);
|
||||
}
|
||||
|
@ -80,7 +81,7 @@ public final class SerialConnectionManager implements ConnectionManager {
|
|||
readTimeout = timeout;
|
||||
connection.read(response);
|
||||
|
||||
if (pollState.getCurrentState() == 1){
|
||||
if (pollState.getCurrentState() == PollingState.State.STATE_1){
|
||||
if ( response[0] == (byte) 0x80
|
||||
&& response[1] == (byte) 0xF0
|
||||
&& (response[2] == (byte) 0x10 || response[2] == (byte) 0x18)
|
||||
|
|
|
@ -92,7 +92,10 @@ final class TestSerialConnection2 implements SerialConnection {
|
|||
public int available() {
|
||||
if (close) return 0;
|
||||
if (pollState.isLastQuery() && !pollState.isNewQuery() &&
|
||||
pollState.getCurrentState() == 0 && pollState.getLastState() == 1) return 0;
|
||||
pollState.getCurrentState() == PollingState.State.STATE_0 &&
|
||||
pollState.getLastState() == PollingState.State.STATE_1) {
|
||||
return 0;
|
||||
}
|
||||
if (isEcuInitRequest()) {
|
||||
String init = "";
|
||||
if (module.getName().equalsIgnoreCase("ECU")){
|
||||
|
@ -157,12 +160,12 @@ final class TestSerialConnection2 implements SerialConnection {
|
|||
response[i++] = READ_ADDRESS_RESPONSE;
|
||||
System.arraycopy(responseData, 0, response, i, responseData.length);
|
||||
response[i += responseData.length] = calculateChecksum(response);
|
||||
if (pollState.getCurrentState() == 0) {
|
||||
if (pollState.getCurrentState() == PollingState.State.STATE_0) {
|
||||
readResponse = new byte[request.length + response.length];
|
||||
System.arraycopy(request, 0, readResponse, 0, request.length);
|
||||
System.arraycopy(response, 0, readResponse, request.length, response.length);
|
||||
}
|
||||
if (pollState.getCurrentState() == 1) {
|
||||
if (pollState.getCurrentState() == PollingState.State.STATE_1) {
|
||||
readResponse = new byte[response.length];
|
||||
System.arraycopy(response, 0, readResponse, 0, response.length);
|
||||
sleepTime = 20L;
|
||||
|
|
|
@ -118,7 +118,7 @@ public final class SSMLoggerConnection implements LoggerConnection {
|
|||
else {
|
||||
final byte[] request = protocol.constructReadAddressRequest(
|
||||
module, queries);
|
||||
if (pollState.getCurrentState() == 0) {
|
||||
if (pollState.getCurrentState() == PollingState.State.STATE_0) {
|
||||
LOGGER.debug("Mode:" + pollState.getCurrentState() + " " +
|
||||
module + " Request ---> " + asHex(request));
|
||||
}
|
||||
|
|
|
@ -21,14 +21,18 @@ package com.romraider.logger.ecu.comms.manager;
|
|||
|
||||
|
||||
public interface PollingState {
|
||||
enum State {
|
||||
STATE_0,
|
||||
STATE_1
|
||||
}
|
||||
|
||||
int getCurrentState();
|
||||
State getCurrentState();
|
||||
|
||||
void setCurrentState(int i);
|
||||
void setCurrentState(State state);
|
||||
|
||||
int getLastState();
|
||||
State getLastState();
|
||||
|
||||
void setLastState(int i);
|
||||
void setLastState(State state);
|
||||
|
||||
boolean isNewQuery();
|
||||
|
||||
|
|
|
@ -20,34 +20,36 @@
|
|||
package com.romraider.logger.ecu.comms.manager;
|
||||
|
||||
public final class PollingStateImpl implements PollingState {
|
||||
private static int currentState;
|
||||
private static int lastpollState;
|
||||
// todo: something is not right here! why are these fields all 'static'? looks like should be instance variables
|
||||
// todo: not class variables?
|
||||
private static State currentState;
|
||||
private static State lastpollState;
|
||||
private static boolean newQuery;
|
||||
private static boolean lastQuery;
|
||||
private static boolean fastPoll;
|
||||
|
||||
public PollingStateImpl() {
|
||||
setCurrentState(0);
|
||||
setLastState(0);
|
||||
setCurrentState(State.STATE_0);
|
||||
setLastState(State.STATE_0);
|
||||
setNewQuery(true);
|
||||
setLastQuery(false);
|
||||
setFastPoll(false);
|
||||
}
|
||||
|
||||
public int getCurrentState() {
|
||||
public State getCurrentState() {
|
||||
return currentState;
|
||||
}
|
||||
|
||||
public void setCurrentState(int i) {
|
||||
currentState = i;
|
||||
public void setCurrentState(State state) {
|
||||
currentState = state;
|
||||
}
|
||||
|
||||
public int getLastState() {
|
||||
public State getLastState() {
|
||||
return lastpollState;
|
||||
}
|
||||
|
||||
public void setLastState(int i) {
|
||||
lastpollState = i;
|
||||
public void setLastState(State state) {
|
||||
lastpollState = state;
|
||||
}
|
||||
|
||||
public boolean isNewQuery() {
|
||||
|
|
|
@ -217,9 +217,9 @@ public final class QueryManagerImpl implements QueryManager {
|
|||
updateQueryList();
|
||||
if (queryMap.isEmpty()) {
|
||||
if (pollState.isLastQuery() &&
|
||||
pollState.getCurrentState() == 0) {
|
||||
pollState.getCurrentState() == PollingState.State.STATE_0) {
|
||||
endEcuQueries(txManager);
|
||||
pollState.setLastState(0);
|
||||
pollState.setLastState(PollingState.State.STATE_0);
|
||||
}
|
||||
start = System.currentTimeMillis();
|
||||
count = 0;
|
||||
|
@ -237,40 +237,40 @@ public final class QueryManagerImpl implements QueryManager {
|
|||
endEcuQueries(txManager);
|
||||
}
|
||||
if (pollState.isFastPoll()) {
|
||||
if (pollState.getCurrentState() == 0 &&
|
||||
if (pollState.getCurrentState() == PollingState.State.STATE_0 &&
|
||||
pollState.isNewQuery()) {
|
||||
pollState.setCurrentState(1);
|
||||
pollState.setCurrentState(PollingState.State.STATE_1);
|
||||
pollState.setNewQuery(false);
|
||||
}
|
||||
if (pollState.getCurrentState() == 0 &&
|
||||
if (pollState.getCurrentState() == PollingState.State.STATE_0 &&
|
||||
!pollState.isNewQuery()) {
|
||||
pollState.setCurrentState(1);
|
||||
pollState.setCurrentState(PollingState.State.STATE_1);
|
||||
}
|
||||
if (pollState.getCurrentState() == 1 &&
|
||||
if (pollState.getCurrentState() == PollingState.State.STATE_1 &&
|
||||
pollState.isNewQuery()) {
|
||||
pollState.setCurrentState(0);
|
||||
pollState.setLastState(1);
|
||||
pollState.setCurrentState(PollingState.State.STATE_0);
|
||||
pollState.setLastState(PollingState.State.STATE_1);
|
||||
pollState.setNewQuery(false);
|
||||
}
|
||||
if (pollState.getCurrentState() == 1 &&
|
||||
if (pollState.getCurrentState() == PollingState.State.STATE_1 &&
|
||||
!pollState.isNewQuery()) {
|
||||
pollState.setLastState(1);
|
||||
pollState.setLastState(PollingState.State.STATE_1);
|
||||
}
|
||||
pollState.setLastQuery(true);
|
||||
}
|
||||
else {
|
||||
pollState.setCurrentState(0);
|
||||
pollState.setLastState(0);
|
||||
pollState.setCurrentState(PollingState.State.STATE_0);
|
||||
pollState.setLastState(PollingState.State.STATE_0);
|
||||
pollState.setNewQuery(false);
|
||||
}
|
||||
lastPollState = pollState.isFastPoll();
|
||||
}
|
||||
else {
|
||||
if (pollState.isLastQuery() &&
|
||||
pollState.getLastState() == 1) {
|
||||
pollState.getLastState() == PollingState.State.STATE_1) {
|
||||
endEcuQueries(txManager);
|
||||
pollState.setLastState(0);
|
||||
pollState.setCurrentState(0);
|
||||
pollState.setLastState(PollingState.State.STATE_0);
|
||||
pollState.setCurrentState(PollingState.State.STATE_0);
|
||||
pollState.setNewQuery(true);
|
||||
}
|
||||
}
|
||||
|
@ -290,7 +290,7 @@ public final class QueryManagerImpl implements QueryManager {
|
|||
messageListener.reportError(e);
|
||||
} finally {
|
||||
txManager.stop();
|
||||
pollState.setCurrentState(0);
|
||||
pollState.setCurrentState(PollingState.State.STATE_0);
|
||||
pollState.setNewQuery(true);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue