Skip to content

Commit

Permalink
feat: Use hex string for Can Header representation
Browse files Browse the repository at this point in the history
  • Loading branch information
tzebrowski committed Jan 26, 2024
1 parent 0cdde06 commit ba959c0
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 33 deletions.
21 changes: 10 additions & 11 deletions src/main/java/org/obd/metrics/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
**/
package org.obd.metrics.command;

import java.nio.ByteBuffer;
import java.util.UUID;

import lombok.EqualsAndHashCode;
Expand Down Expand Up @@ -49,8 +48,16 @@ protected Command(final String query, final String mode, final String label) {
this(query, mode, label, "");
}

protected Command(String canId, final String data) {
this.data = MergeUtils.merge(canId, data);
this.query = new String(this.data);
this.label = null;
this.mode = null;
this.canMode = null;
}

protected Command(final byte[] canId, final byte[] data) {
this.data = merge(canId, data).array();
this.data = MergeUtils.merge(canId, data);
this.query = new String(this.data);
this.label = null;
this.mode = null;
Expand All @@ -69,13 +76,5 @@ protected Command(final String query, final String mode, final String label, fin
public String toString() {
return "[query=" + query + "]";
}

private ByteBuffer merge(final byte[] canId, final byte[] data) {
final byte[] allByteArray = new byte[canId.length + 1 + data.length];
final ByteBuffer buff = ByteBuffer.wrap(allByteArray);
buff.put(canId);
buff.put((byte) data.length);
buff.put(data);
return buff;
}

}
60 changes: 60 additions & 0 deletions src/main/java/org/obd/metrics/command/MergeUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright 2019-2024, Tomasz Żebrowski
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.obd.metrics.command;

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

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
final class MergeUtils {

static byte[] merge(final String canIdHex, final String dataHex) {
final byte[] data = hexStringToByteArray(dataHex);
final byte[] canId = hexStringToByteArray(canIdHex);
final byte[] header = new byte[] { 0x0, 0x0, 0x0, 0x0 };

for (int i = 0; i < canId.length; i++) {
final byte b = canId[i];
header[i + header.length - canId.length] = b;
}

final ByteBuffer buff = ByteBuffer.wrap(new byte[header.length + 1 + data.length]);
buff.put(header);
buff.put((byte) data.length);
buff.put(data);
return buff.array();
}

static byte[] merge(final byte[] canId, final byte[] data) {
final byte[] allByteArray = new byte[canId.length + 1 + data.length];
final ByteBuffer buff = ByteBuffer.wrap(allByteArray);
buff.put(canId);
buff.put((byte) data.length);
buff.put(data);
return buff.array();
}

private static byte[] hexStringToByteArray(String hex) {
return new BigInteger(hex, 16).toByteArray();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@

import org.obd.metrics.command.Command;

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

public CannelloniCommand() {
super(INIT_MESSAGE, null, null);
public CannelloniMessage() {
super(PEER_HELLO_MESSAGE, null, null);
}

public CannelloniCommand(final byte[] canId, final byte[] data) {
public CannelloniMessage(final String canId, final String data) {
super(canId, data);
}

public CannelloniMessage(final byte[] canId, final byte[] data) {
super(canId, data);
}
}
45 changes: 45 additions & 0 deletions src/main/java/org/obd/metrics/transport/CanUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright 2019-2024, Tomasz Żebrowski
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.obd.metrics.transport;

import java.nio.ByteBuffer;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class CanUtils {

private static final int CAN_SFF_MASK = 0x000007FF;
private static final int CAN_EFF_MASK = 0x1FFFFFFF;

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

final ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
buffer.put(canIdArray);
buffer.rewind();
final int canId = buffer.getInt();

if ((canId & CAN_EFF_MASK) == 1) {
return Integer.toHexString(canId & CAN_EFF_MASK);
} else {
return Integer.toHexString(canId & CAN_SFF_MASK);
}
}
}
21 changes: 16 additions & 5 deletions src/main/java/org/obd/metrics/transport/CannelloniConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import org.obd.metrics.api.model.Adjustments;
import org.obd.metrics.command.Command;
import org.obd.metrics.command.obd.CannelloniCommand;
import org.obd.metrics.command.obd.CannelloniMessage;
import org.obd.metrics.transport.message.ConnectorResponse;
import org.obd.metrics.transport.message.ConnectorResponseFactory;

Expand Down Expand Up @@ -99,8 +99,8 @@ public synchronized void transmit(@NonNull final Command command) {
} else {
try {
if (adjustments != null && adjustments.isDebugEnabled()) {
if ( command instanceof CannelloniCommand) {
log.info("TX: {}", printMessage((CannelloniCommand) command));
if ( command instanceof CannelloniMessage) {
log.info("TX: {}", printMessage((CannelloniMessage) command));
} else {
log.info("TX: {}", command.getQuery());
}
Expand Down Expand Up @@ -184,9 +184,20 @@ private void reset() {
Arrays.fill(buffer, 0, buffer.length, (byte) 0);
}

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

buffer.append(" ");

for (int i = 5; i< message.getData().length; i++) {
final byte b = message.getData()[i];
buffer.append(String.format("%02X ", b));
}
return buffer.toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,82 @@
/**
* Copyright 2019-2024, Tomasz Żebrowski
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.obd.metrics.connection.cannelloni;

import java.io.IOException;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.obd.metrics.api.model.Adjustments;
import org.obd.metrics.command.obd.CannelloniCommand;
import org.obd.metrics.command.obd.CannelloniMessage;
import org.obd.metrics.transport.Connector;
import org.obd.metrics.transport.TcpAdapterConnection;

import lombok.extern.slf4j.Slf4j;

///usr/local/bin/cannelloni -I vcan0 -C s -d c -p
// sudo systemctl status cannelloni-vcan0.service
@Disabled

//@Disabled
@Slf4j
public class CannelloniConnectorTest {


public static void main(String[] args) {
final String abc = "ABCDE";
System.out.println(abc.getBytes().length);

}

@Disabled
@Test
public void transmitTest() throws IOException {
public void transitBenchmarkTest() throws IOException {

final TcpAdapterConnection tcpAdapterConnection = TcpAdapterConnection.of("127.0.0.1", 20000);

final Connector connector = Connector
.builder()
.connection(tcpAdapterConnection)
final Connector connector = Connector.builder().connection(tcpAdapterConnection)
.adjustments(Adjustments.builder().debugEnabled(true).build()).type(Connector.Type.CANNELLONI).build();

connector.transmit(new CannelloniCommand());
connector.transmit(new CannelloniMessage());
connector.receive().getMessage();

long tt = System.currentTimeMillis();
for (int i = 0; i < 100; i++) {
final byte[] canId = new byte[] { 0x0, 0x0, (byte) i, 0x3};
final byte[] canId = new byte[] { 0x0, 0x0, (byte) i, 0x3 };
final byte[] data = new byte[] { 0x2, (byte) i, 0x2 };
final CannelloniCommand message = new CannelloniCommand(canId, data);
final CannelloniMessage message = new CannelloniMessage(canId, data);
connector.transmit(message);
}
tt = System.currentTimeMillis() - tt;
log.info("transit time: {}", tt);
}

@Disabled
@Test
public void transitTest() throws IOException {

final TcpAdapterConnection tcpAdapterConnection = TcpAdapterConnection.of("127.0.0.1", 20000);

final Connector connector = Connector.builder().connection(tcpAdapterConnection)
.adjustments(Adjustments.builder().debugEnabled(true).build()).type(Connector.Type.CANNELLONI).build();

connector.transmit(new CannelloniMessage());
connector.receive().getMessage();
final CannelloniMessage message = new CannelloniMessage("7AB", "ABCDEF");
connector.transmit(message);
}
}

0 comments on commit ba959c0

Please sign in to comment.