Skip to content

Commit

Permalink
Merge pull request #20 from fpagliughi/netlink-updates
Browse files Browse the repository at this point in the history
Initial modernization (WIP)
  • Loading branch information
fpagliughi committed Nov 23, 2022
2 parents 8e4067b + 48ca2b2 commit d8e0fb1
Show file tree
Hide file tree
Showing 9 changed files with 375 additions and 370 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
/target
Cargo.lock

32 changes: 24 additions & 8 deletions Cargo.toml
Expand Up @@ -3,18 +3,34 @@ authors = ["Marc Brinkmann <git@marcbrinkmann.de>"]
description = "Linux SocketCAN library. Send and receive CAN frames via CAN bus on Linux."
documentation = "https://docs.rs/socketcan"
license = "MIT"
edition = "2018"
name = "socketcan"
repository = "https://github.com/mbr/socketcan-rs"
version = "2.0.0"

# Features:
#
# "netlink" (default) - Whether to include CAN interface configuration
# capabilities based on netlink kernel communications
# "utils" - Build command-line utilities
#

[features]
default = ["netlink"]
netlink = ["neli"]
vcan_tests = ["netlink"]
utils = ["clap", "anyhow"]

[dependencies]
byte_conv = "0.1.1"
hex = "^0.2"
itertools = "^0.4"
libc = "^0.2"
netlink-rs = { git = "https://github.com/mbr/netlink-rs", rev = "01cba6fcc7b11917890bc3d2b4635009fde8082c" }
nix = "^0.5"
try_from = "0.2.0"
hex = "0.4"
itertools = "0.10"
libc = "0.2"
nix = "0.23"
clap = { version = "2.33", optional = true }
anyhow = { version = "1.0", optional = true }
neli = { version = "0.6", optional = true }

[features]
vcan_tests = []
[[bin]]
name = "can"
required-features = ["utils"]
96 changes: 96 additions & 0 deletions src/bin/can.rs
@@ -0,0 +1,96 @@
// socketcan-rs/src/bin/can.rs

//! Simple CLI tool to run basic CAN bus functionality from the Linux
//! command line, similar to 'can-utils'.

use anyhow::{anyhow, Result};
use clap::{
App,
Arg,
ArgMatches,
SubCommand,
};
use socketcan::CanInterface;
use std::process;

// Make the app version the same as the package.
const VERSION: &str = env!("CARGO_PKG_VERSION");

// --------------------------------------------------------------------------

/// Process the 'iface' subcommand.
///
/// Set parameters on the interface, or bring it up or down.
#[cfg(feature = "netlink")]
fn iface_cmd(iface_name: &str, opts: &ArgMatches) -> Result<()> {
let iface = CanInterface::open(iface_name)?;

match opts.subcommand_name() {
Some("up") => {
iface.bring_up()
},
Some("down") => {
iface.bring_down()
},
Some("bitrate") => {
return Err(anyhow!("Unimplemented"))
},
_ => return Err(anyhow!("Unknown 'iface' subcommand"))
}?;
Ok(())
}

#[cfg(not(feature = "netlink"))]
fn iface_cmd(_iface_name: &str, _opts: &ArgMatches) -> Result<()> {
Err(anyhow!(
"The 'netlink' feature is required to configure an inteface."
))
}

// --------------------------------------------------------------------------

fn main() {
let opts = App::new("can")
.author("Frank Pagliughi")
.version(VERSION)
.about("Command line tool to interact with the CAN bus on Linux")
.help_short("?")
.arg(Arg::with_name("iface")
.help("The CAN interface to use, like 'can0', 'vcan0', etc")
.required(true)
.index(1))
.subcommand(
SubCommand::with_name("iface")
.help_short("?")
.about("Get/set parameters on the CAN interface")
.subcommand(
SubCommand::with_name("up")
.about("Bring the interface up")
)
.subcommand(
SubCommand::with_name("down")
.about("Bring the interface down")
)
.subcommand(
SubCommand::with_name("bitrate")
.about("Set the bit rate on the interface.")
)

)
.get_matches();

let iface_name = opts.value_of("iface").unwrap();

let res = if let Some(sub_opts) = opts.subcommand_matches("iface") {
iface_cmd(&iface_name, &sub_opts)
}
else {
Err(anyhow!("Need to specify a subcommand (-? for help)."))
};

if let Err(err) = res {
eprintln!("{}", err);
process::exit(1);
}
}

6 changes: 3 additions & 3 deletions src/dump.rs
Expand Up @@ -159,9 +159,9 @@ impl<R: io::BufRead> Reader<R> {
false)?;

Ok(Some(CanDumpRecord {
t_us: t_us,
device: device,
frame: frame,
t_us,
device,
frame,
}))
}
}
Expand Down

0 comments on commit d8e0fb1

Please sign in to comment.