@@ -57,7 +57,7 @@ public void send(byte[] request, byte[] response, PollingState pollState) {
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 void send(byte[] request, byte[] response, PollingState pollState) {
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.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 static byte[] filterRequestFromResponse(byte[] request, byte[] response,
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 byte[] constructReadAddressResponse(Collection<EcuQuery> queries, Polling
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 static byte[] filterRequestFromResponse(byte[] request, byte[] response,
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 void send(byte[] request, byte[] response, PollingState pollState) {
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 void send(byte[] request, byte[] response, PollingState pollState) {
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 @@ public void write(byte[] bytes) {
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 @@ public void read(byte[] bytes) {
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 void sendAddressReads(
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 @@


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 @@ private void runLogger(Module module) {
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 @@ private void runLogger(Module module) {
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 @@ private void runLogger(Module module) {
messageListener.reportError(e);
} finally {
txManager.stop();
pollState.setCurrentState(0);
pollState.setCurrentState(PollingState.State.STATE_0);
pollState.setNewQuery(true);
}
}
@@ -56,19 +56,16 @@ public RomTree(DefaultMutableTreeNode input) {

@Override
public void actionPerformed(ActionEvent e) {
try{
Object selectedRow = getLastSelectedPathComponent();
/* if nothing is selected */
if (selectedRow == null) {
return;
}

if(selectedRow instanceof TableTreeNode) {
showTable(selectedRow);
}
setLastSelectedRom(selectedRow);
}catch(NullPointerException ex) {
Object selectedRow = getLastSelectedPathComponent();
/* if nothing is selected */
if (selectedRow == null) {
return;
}

if (selectedRow instanceof TableTreeNode) {
showTable((TableTreeNode)selectedRow);
}
setLastSelectedRom(selectedRow);
}
};

@@ -87,32 +84,24 @@ public void mouseClicked(MouseEvent e) {
TreePath treePath = getPathForLocation(e.getX(), e.getY());
if (treePath == null)
return; // this happens if we click in the empty area
try {
Object selectedRow = treePath.getLastPathComponent();
/* if nothing is selected */
if (selectedRow == null) {
return;
}

if(e.getClickCount() >= SettingsManager.getSettings().getTableClickCount()
&& selectedRow instanceof TableTreeNode) {
showTable(selectedRow);
}
Object selectedRow = treePath.getLastPathComponent();
/* if nothing is selected */
if (selectedRow == null) {
return;
}

setLastSelectedRom(selectedRow);
}catch(NullPointerException ex) {
if (e.getClickCount() >= SettingsManager.getSettings().getTableClickCount()
&& selectedRow instanceof TableTreeNode) {
showTable((TableTreeNode)selectedRow);
}

setLastSelectedRom(selectedRow);
}

private void showTable(Object selectedRow) {
try{
if(selectedRow instanceof TableTreeNode) {
TableTreeNode node = (TableTreeNode) selectedRow;
if(null != node) {
getEditor().displayTable(node.getFrame());
}
}
} catch (NullPointerException ex) {
private void showTable(TableTreeNode selectedRow) {
TableTreeNode node = (TableTreeNode) selectedRow;
if (null != node) {
getEditor().displayTable(node.getFrame());
}
}

@@ -122,23 +111,23 @@ private void setLastSelectedRom(Object selectedNode) {
}

Rom romNode = getRomNode(selectedNode);
if(romNode == null) {
if (romNode == null) {
return;
}
getEditor().setLastSelectedRom(romNode);
getEditor().refreshUI();
}

private Rom getRomNode(Object currentNode){
if(currentNode == null) {
if (currentNode == null) {
return null;
} else if(currentNode instanceof Rom) {
return (Rom)currentNode;
} else if(currentNode instanceof TableTreeNode) {
return getRomNode(((TableTreeNode)currentNode).getParent());
} else if(currentNode instanceof CategoryTreeNode) {
return getRomNode(((CategoryTreeNode)currentNode).getParent());
}else {
} else {
return null;
}
}