-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Use hex string for Can Header representation
- Loading branch information
1 parent
0cdde06
commit ba959c0
Showing
6 changed files
with
189 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 49 additions & 12 deletions
61
src/test/java/org/obd/metrics/connection/cannelloni/CannelloniConnectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |