Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init j1939 datalink #44

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
38309bf
init j1939 datalink
JannesBrands Jan 27, 2024
bf52d30
remove socketcan feature and move socketcan to dev dependency
Jan 27, 2024
4b15403
extend address helper functions
Jan 27, 2024
aa657d7
add custom error for priority and helper functions
Jan 27, 2024
bfb4a82
socketcan only on unix platforms
Jan 27, 2024
d611862
replace old id code and remove tests to add them later again
Jan 27, 2024
bb2428a
fix mod.rs
Jan 27, 2024
b4cdd4c
compiling but tests are missing
Feb 7, 2024
0299a21
fix clippy
Feb 7, 2024
448abf5
clippy allow enum variant names
Feb 7, 2024
a31ff25
simple example without library usage yet
JannesBrands Feb 11, 2024
2133ea4
fmt
Feb 11, 2024
e9e8d1e
add standard id support & testing of pgn
Feb 15, 2024
ce79721
adds a ton of testing, documentation & fixes
JannesBrands Feb 21, 2024
5b87c07
fix unused import
JannesBrands Feb 21, 2024
6a98902
adding tests to standard id
JannesBrands Feb 21, 2024
5a7da26
adding doc
JannesBrands Feb 21, 2024
073356f
add test for Default
JannesBrands Feb 21, 2024
0c8d683
Adds tests to ext ID & fixes
JannesBrands Feb 21, 2024
d5914dd
add test
JannesBrands Feb 21, 2024
5010f2b
to be finished
JannesBrands Feb 21, 2024
25e97f3
add important addresses & addr ranges, documentation
JannesBrands Feb 22, 2024
ff93894
forward example only on linux (for this moment)
JannesBrands Feb 22, 2024
a8dc97c
..
JannesBrands Feb 22, 2024
97d6ca0
fix
JannesBrands Feb 22, 2024
d541b5e
remove wrong import
JannesBrands Feb 22, 2024
4b32adf
..
JannesBrands Feb 22, 2024
6a51750
remove confusing clippy hint
JannesBrands Feb 22, 2024
24f567a
borrow
JannesBrands Feb 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ keywords = ["agriculture", "can", "canbus", "isobus", "j1939", "agritech", "smar
[dependencies]
bitvec = "1.0.1"
rand = "0.8.5"
socketcan = { version = "2.0.0", optional = true }
strum_macros = "0.25.2"
embedded-can = "0.4.1"

[features]
default = []
socketcan = ["dep:socketcan"]

[dev-dependencies]
clap = { version = "4.3.19", features = ["derive"] }
Expand All @@ -23,6 +22,8 @@ ctrlc = "3.4.0"
tracing = "0.1.37"
tracing-subscriber = "0.3.17"

[target.'cfg(unix)'.dev-dependencies]
socketcan = { version = "3.3.0" }

[[example]]
name = "forward"
required-features = ["socketcan"]
68 changes: 32 additions & 36 deletions examples/forward.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright 2023 Raven Industries inc.

use std::sync::mpsc::channel;

use ag_iso_stack::driver::{Driver, DriverReadError, Frame, SocketcanDriver};
use clap::Parser;

#[cfg(target_os = "linux")]
use socketcan::{BlockingCan, CanSocket, Socket};

/// Forward CAN traffic from one interface to another
#[derive(Debug, Parser)]
#[clap(name = "forward", verbatim_doc_comment)]
Expand All @@ -16,22 +16,25 @@ struct Options {
/// The interface to read traffic from
///
/// Can be either a string interface name, or an integer interface index
#[clap(short, long, default_value_t = String::from("can0"))]
#[clap(short, long, default_value_t = String::from("vcan0"))]
pub input_interface: String,

/// The interface to write traffic to
///
/// Can be either a string interface name, or an integer interface index
#[clap(short, long, default_value_t = String::from("can1"))]
#[clap(short, long, default_value_t = String::from("vcan1"))]
pub output_interface: String,
}

fn create_driver(iface: &str) -> impl Driver {
if let Ok(index) = iface.parse::<u32>() {
SocketcanDriver::new_by_index(index)
} else {
SocketcanDriver::new_by_name(iface)
}
#[cfg(target_os = "linux")]
fn open_can_interface(input_name: &str, output_name: &str) -> (CanSocket, CanSocket) {
let mut input =
CanSocket::open(input_name).expect("The given input interface cannot be opened!");

let mut output =
CanSocket::open(output_name).expect("The given output interface cannot be opened!");

(input, output)
}

fn main() {
Expand All @@ -50,33 +53,26 @@ fn main() {
opts.output_interface
);

let mut input = create_driver(&opts.input_interface);
let mut output = create_driver(&opts.output_interface);

input.open().unwrap();
output.open().unwrap();

let (tx, rx) = channel();
ctrlc::set_handler(move || tx.send(true).unwrap()).unwrap();

loop {
if rx.try_recv().is_ok() {
break;
}

let mut frame = Frame::default();
#[cfg(target_os = "linux")]
|opts: Options| {
let (input, output) = open_can_interface(&opts.input_interface, &opts.output_interface);
input
.set_nonblocking(true)
.expect("Could not set input bus to non-blocking!");
output
.set_nonblocking(true)
.expect("Could not set output bus to non-blocking!");

match input.read_nonblocking(&mut frame) {
Ok(_) => {
tracing::info!("Read frame: {frame:?}");
tracing::info!("Attempting to write frame");
match output.write_nonblocking(&frame) {
Ok(_) => tracing::info!("Wrote frame: {frame:?}"),
Err(e) => tracing::info!("Failed to write frame: {e:?}"),
loop {
match input.borrow().receive() {
Ok(frame) => {
output
.borrow()
.transmit(&frame)
.expect("Could not forward received message!");
}
Err(_err) => continue,
}
Err(DriverReadError::NoFrameReady) => {}
Err(e) => tracing::error!("Failed to read frame: {e:?}"),
}
}
};
}
16 changes: 0 additions & 16 deletions src/driver/address.rs

This file was deleted.

Loading
Loading