only:refactoring around SocketCAN
This commit is contained in:
parent
b7e8bd3a25
commit
97c1f0c917
|
@ -0,0 +1,63 @@
|
|||
package com.rusefi.io.can;
|
||||
|
||||
import com.devexperts.logging.Logging;
|
||||
import com.rusefi.uds.CanConnector;
|
||||
import org.jetbrains.annotations.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 com.devexperts.logging.Logging.getLogging;
|
||||
import static tel.schich.javacan.CanFrame.FD_NO_FLAGS;
|
||||
import static tel.schich.javacan.CanSocketOptions.RECV_OWN_MSGS;
|
||||
|
||||
public class SocketCANHelper {
|
||||
private static Logging log = getLogging(SocketCANIoStream.class);
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
||||
public static CanConnector.CanPacket read(RawCanChannel socket) throws IOException {
|
||||
CanFrame rx = socket.read();
|
||||
if (log.debugEnabled())
|
||||
log.debug("GOT " + String.format("%X", rx));
|
||||
byte[] raw = new byte[rx.getDataLength()];
|
||||
rx.getData(raw, 0, raw.length);
|
||||
return new CanConnector.CanPacket() {
|
||||
@Override
|
||||
public int id() {
|
||||
return rx.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] payload() {
|
||||
return raw;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ import com.devexperts.logging.Logging;
|
|||
import com.opensr5.io.DataListener;
|
||||
import com.rusefi.binaryprotocol.IncomingDataBuffer;
|
||||
import com.rusefi.config.generated.Fields;
|
||||
import com.rusefi.uds.CanConnector;
|
||||
import com.rusefi.util.HexBinary;
|
||||
import com.rusefi.io.IoStream;
|
||||
import com.rusefi.io.can.isotp.IsoTpCanDecoder;
|
||||
|
@ -11,9 +12,6 @@ import com.rusefi.io.can.isotp.IsoTpConnector;
|
|||
import com.rusefi.io.serial.AbstractIoStream;
|
||||
import com.rusefi.io.tcp.BinaryProtocolServer;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import tel.schich.javacan.CanChannels;
|
||||
import tel.schich.javacan.CanFrame;
|
||||
import tel.schich.javacan.NetworkDevice;
|
||||
import tel.schich.javacan.RawCanChannel;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -22,8 +20,6 @@ import java.util.concurrent.Executors;
|
|||
|
||||
import static com.devexperts.logging.Logging.getLogging;
|
||||
import static com.rusefi.config.generated.Fields.CAN_ECU_SERIAL_TX_ID;
|
||||
import static tel.schich.javacan.CanFrame.FD_NO_FLAGS;
|
||||
import static tel.schich.javacan.CanSocketOptions.RECV_OWN_MSGS;
|
||||
|
||||
public class SocketCANIoStream extends AbstractIoStream {
|
||||
static Logging log = getLogging(SocketCANIoStream.class);
|
||||
|
@ -51,25 +47,11 @@ public class SocketCANIoStream extends AbstractIoStream {
|
|||
if (log.debugEnabled())
|
||||
log.debug("Sending " + HexBinary.printHexBinary(total));
|
||||
|
||||
CanFrame packet = CanFrame.create(Fields.CAN_ECU_SERIAL_RX_ID, FD_NO_FLAGS, total);
|
||||
try {
|
||||
socket.write(packet);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
SocketCANHelper.send(Fields.CAN_ECU_SERIAL_RX_ID, total, socket);
|
||||
}
|
||||
|
||||
public SocketCANIoStream() {
|
||||
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);
|
||||
}
|
||||
socket = SocketCANHelper.createSocket();
|
||||
// buffer could only be created once socket variable is not null due to callback
|
||||
dataBuffer = createDataBuffer();
|
||||
}
|
||||
|
@ -96,17 +78,13 @@ public class SocketCANIoStream extends AbstractIoStream {
|
|||
|
||||
private void readOnePacket(DataListener listener) {
|
||||
try {
|
||||
CanFrame rx = socket.read();
|
||||
CanConnector.CanPacket rx = SocketCANHelper.read(socket);
|
||||
if (rx.id() != CAN_ECU_SERIAL_TX_ID) {
|
||||
if (log.debugEnabled())
|
||||
log.debug("GOT " + String.format("%X", rx));
|
||||
if (rx.getId() != CAN_ECU_SERIAL_TX_ID) {
|
||||
if (log.debugEnabled())
|
||||
log.debug("Skipping non " + String.format("%X", CAN_ECU_SERIAL_TX_ID) + " packet: " + String.format("%X", rx.getId()));
|
||||
log.debug("Skipping non " + String.format("%X", CAN_ECU_SERIAL_TX_ID) + " packet: " + String.format("%X", rx.id()));
|
||||
return;
|
||||
}
|
||||
byte[] raw = new byte[rx.getDataLength()];
|
||||
rx.getData(raw, 0, raw.length);
|
||||
byte[] decode = canDecoder.decodePacket(raw);
|
||||
byte[] decode = canDecoder.decodePacket(rx.payload());
|
||||
listener.onDataArrived(decode);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
|
|
Loading…
Reference in New Issue