Skip to content

Commit

Permalink
add helpers for J1939 CAN ID parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
pschichtel committed Feb 27, 2024
1 parent aba0fc8 commit c8372bd
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* The MIT License
* Copyright © 2018 Phillip Schichtel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package tel.schich.javacan.test;

import org.junit.jupiter.api.Test;
import tel.schich.javacan.J1939Utils;

import static java.lang.Integer.toHexString;
import static org.junit.jupiter.api.Assertions.*;

class J1939UtilsTest {

@Test
void parameterGroupNumberFromRawAddress() {
assertEquals(toHexString(0xEFA1), toHexString(J1939Utils.parameterGroupNumberFromRawAddress(0x18EFA1EB)));
}

@Test
void sourceAddressFromRawAddress() {
assertEquals(toHexString((byte)0xEB), toHexString(J1939Utils.sourceAddressFromRawAddress(0x18EFA1EB)));
}

@Test
void priorityFromRawAddress() {
assertEquals(6, J1939Utils.priorityFromRawAddress(0x18EFA1EB));
}
}
4 changes: 4 additions & 0 deletions core/src/main/java/tel/schich/javacan/J1939Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ public interface J1939Address {
byte getAddress();

ImmutableJ1939Address copy();

static ImmutableJ1939Address of(NetworkDevice device, long name, int parameterGroupNumber, byte address) {
return new ImmutableJ1939Address(device, name, parameterGroupNumber, address);
}
}
40 changes: 40 additions & 0 deletions core/src/main/java/tel/schich/javacan/J1939Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* The MIT License
* Copyright © 2018 Phillip Schichtel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package tel.schich.javacan;

public class J1939Utils {
private J1939Utils() {
}

public static int parameterGroupNumberFromRawAddress(int canId) {
return (canId >>> 8) & 0b0011_1111_1111_1111_1111;
}

public static byte sourceAddressFromRawAddress(int canId) {
return (byte)(canId & 0b1111_1111);
}

public static byte priorityFromRawAddress(int canId) {
return (byte)((canId >>> 26) & 0b0111);
}
}

0 comments on commit c8372bd

Please sign in to comment.