Skip to content

Commit

Permalink
SocketCAN sender
Browse files Browse the repository at this point in the history
  • Loading branch information
rusefillc committed Jan 6, 2024
1 parent 1e9efdf commit e05d024
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ apply from: 'dependencies.gradle'
group 'org.rusefi'
version '0.11-SNAPSHOT'

repositories {
mavenCentral()
allprojects {
repositories {
mavenCentral()
}
}

dependencies {
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
javaCanVersion=3.2.4
2 changes: 2 additions & 0 deletions playback/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ plugins {
dependencies {
api rootProject
api project(':peak-can-basic')
api group: 'tel.schich', name: 'javacan-core', version: "$javaCanVersion"
api group: 'tel.schich', name: 'javacan-core', version: "$javaCanVersion", classifier: 'x86_64'
}
39 changes: 39 additions & 0 deletions playback/src/main/java/com/rusefi/io/can/SocketCANHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.rusefi.io.can;

import com.sun.istack.internal.NotNull;
import tel.schich.javacan.CanChannels;
import tel.schich.javacan.CanFrame;
import tel.schich.javacan.NetworkDevice;
import tel.schich.javacan.RawCanChannel;

import java.io.IOException;

import static tel.schich.javacan.CanFrame.FD_NO_FLAGS;
import static tel.schich.javacan.CanSocketOptions.RECV_OWN_MSGS;

public class SocketCANHelper {
@NotNull
public static RawCanChannel createSocket() {
final RawCanChannel socket;
try {
NetworkDevice canInterface = NetworkDevice.lookup(System.getProperty("CAN_DEVICE_NAME", "can0"));
socket = CanChannels.newRawChannel();
socket.bind(canInterface);

socket.configureBlocking(true); // we want reader thread to wait for messages
socket.setOption(RECV_OWN_MSGS, false);
} catch (IOException e) {
throw new IllegalStateException("Error looking up", e);
}
return socket;
}

public static void send(int id, byte[] payload, RawCanChannel channel) {
CanFrame packet = CanFrame.create(id, FD_NO_FLAGS, payload);
try {
channel.write(packet);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}

0 comments on commit e05d024

Please sign in to comment.