Skip to content

Commit

Permalink
feat: Enhance message readings
Browse files Browse the repository at this point in the history
  • Loading branch information
tzebrowski committed Jan 28, 2024
1 parent ba959c0 commit 207d214
Show file tree
Hide file tree
Showing 5 changed files with 3,193 additions and 54 deletions.
11 changes: 8 additions & 3 deletions src/main/java/org/obd/metrics/command/obd/CannelloniMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import org.obd.metrics.command.Command;

public final class CannelloniMessage extends Command {
private final static String PEER_HELLO_MESSAGE = "CANNELLONIv1";
public final static String PEER_HELLO_MESSAGE = "CANNELLONIv1";

public CannelloniMessage() {
super(PEER_HELLO_MESSAGE, null, null);
public static CannelloniMessage hello() {
return new CannelloniMessage(PEER_HELLO_MESSAGE);
}

public CannelloniMessage(final String canId, final String data) {
Expand All @@ -34,4 +34,9 @@ public CannelloniMessage(final String canId, final String data) {
public CannelloniMessage(final byte[] canId, final byte[] data) {
super(canId, data);
}


private CannelloniMessage(String msg) {
super(msg, null, null);
}
}
13 changes: 13 additions & 0 deletions src/main/java/org/obd/metrics/transport/CanUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
**/
package org.obd.metrics.transport;

import java.math.BigInteger;
import java.nio.ByteBuffer;

import lombok.AccessLevel;
Expand All @@ -29,6 +30,18 @@ public final class CanUtils {
private static final int CAN_SFF_MASK = 0x000007FF;
private static final int CAN_EFF_MASK = 0x1FFFFFFF;

public static String intToHex(int value) {
return Integer.toHexString(value).toUpperCase();
}

public static int hexToInt(String hex) {
final byte[] bytes = new BigInteger(hex, 16).toByteArray();
final ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
buffer.put(bytes);
buffer.rewind();
return buffer.getInt();
}

public static String canIdToHex(final byte[] canIdArray) {

final ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
Expand Down
83 changes: 45 additions & 38 deletions src/main/java/org/obd/metrics/transport/CannelloniConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
@Slf4j
final class CannelloniConnector implements Connector {

private static final char NEXT_MESSAGE_SIGNAL = '\n';
private static final int CAN_ID_LENGTH = 4;
private static final ConnectorResponse EMPTY_MESSAGE = ConnectorResponseFactory.wrap(new byte[] {}, 0, 0);

private transient boolean hello = false;

@Getter
private boolean faulty;

Expand All @@ -52,8 +54,7 @@ final class CannelloniConnector implements Connector {
private final AdapterConnection connection;
private final Adjustments adjustments;

private final byte[] buffer = new byte[BUFFER_SIZE];
private long tts = 0;
private final byte[] buffer = new byte[96];
private boolean closed = false;

CannelloniConnector(final AdapterConnection connection, final Adjustments adjustments) throws IOException {
Expand Down Expand Up @@ -93,14 +94,13 @@ public void close() {

@Override
public synchronized void transmit(@NonNull final Command command) {
tts = System.currentTimeMillis();
if (isFaulty()) {
log.warn("Previous IO failed. Cannot perform another IO operation");
} else {
try {
if (adjustments != null && adjustments.isDebugEnabled()) {
if ( command instanceof CannelloniMessage) {
log.info("TX: {}", printMessage((CannelloniMessage) command));
if (command instanceof CannelloniMessage) {
log.info("TX: {}", printMessage(command.getData(), command.getData().length));
} else {
log.info("TX: {}", command.getQuery());
}
Expand All @@ -120,39 +120,48 @@ public synchronized ConnectorResponse receive() {
} else {
try {
if (in != null) {

short cnt = 0;
int nextByte;
char characterRead;

while ((nextByte = in.read()) > -1 && (characterRead = (char) nextByte) != NEXT_MESSAGE_SIGNAL
&& cnt != buffer.length) {
if (Characters.isCharacterAllowed(characterRead)) {
buffer[cnt++] = (byte) Character.toUpperCase(characterRead);
}
// CANNELLONIv1
if (buffer[0] == 'C' && buffer[1] == 'A' && buffer[2] == 'N' && buffer[3] == 'N'
&& buffer[4] == 'E' && buffer[5] == 'L' && buffer[6] == 'L' && buffer[7] == 'O'
&& buffer[8] == 'N' && buffer[9] == 'I' && buffer[10] == 'V' && buffer[11] == '1') {
break;
int canIdCnt = 0;
int dataLength = -1;
int dataLengthCnt = 0;

while ((nextByte = in.read()) > -1 && cnt != buffer.length) {
buffer[cnt++] = (byte) Character.toUpperCase(nextByte);
if (hello) {
if (canIdCnt == CAN_ID_LENGTH) {
if (dataLength < 0) {
dataLength = nextByte;
} else {
dataLengthCnt++;
}

} else {
canIdCnt++;
}
if (dataLength == dataLengthCnt) {
break;
}

} else {
if (buffer[0] == 'C' && buffer[1] == 'A' && buffer[2] == 'N' && buffer[3] == 'N'
&& buffer[4] == 'E' && buffer[5] == 'L' && buffer[6] == 'L' && buffer[7] == 'O'
&& buffer[8] == 'N' && buffer[9] == 'I' && buffer[10] == 'V' && buffer[11] == '1') {
hello = true;
break;
}
}
}

short start = 0;
if ((char) buffer[0] == 'S' && (char) buffer[1] == 'E' && (char) buffer[2] == 'A'
&& (char) buffer[3] == 'R') {
// SEARCHING...
start = 12;
cnt = (short) (cnt - start);
}

final ConnectorResponse response = ConnectorResponseFactory.wrap(buffer, start, start + cnt);

reset();

tts = System.currentTimeMillis() - tts;
if (adjustments != null && adjustments.isDebugEnabled()) {
log.info("RX: {}, processing time: {}ms", response.getMessage(), tts);
log.info("RX: {}", printMessage(buffer, cnt));
}
reset();

return response;
}
Expand All @@ -164,6 +173,7 @@ public synchronized ConnectorResponse receive() {
return EMPTY_MESSAGE;
}


void reconnect() {
if (closed) {
log.error("Connection is closed. Do not try to reconnect.");
Expand All @@ -184,20 +194,17 @@ private void reset() {
Arrays.fill(buffer, 0, buffer.length, (byte) 0);
}

private String printMessage(CannelloniMessage message) {
private String printMessage(byte []message, int length) {
final StringBuilder buffer = new StringBuilder();
buffer.append("[");
buffer.append(CanUtils.canIdToHex(new byte[] {
message.getData()[0],
message.getData()[1],
message.getData()[2],
message.getData()[3]}));
buffer.append(CanUtils.canIdToHex(
new byte[] { message[0], message[1], message[2], message[3] }));
buffer.append("]");

buffer.append(" ");
for (int i = 5; i< message.getData().length; i++) {
final byte b = message.getData()[i];

for (int i = CAN_ID_LENGTH + 1; i < length; i++) {
final byte b = message[i];
buffer.append(String.format("%02X ", b));
}
return buffer.toString();
Expand Down
Loading

0 comments on commit 207d214

Please sign in to comment.