summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2018-04-08 16:22:30 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2018-04-08 16:22:30 +0100
commit7dcb6656251d4363d27d0ea3fb224a341a58ec3d (patch)
tree4717f8533116f45abf1a61ec33771498d6a36371
parente1d861381b3abce23798458122cf80315062c4da (diff)
downloadcanopied-7dcb6656251d4363d27d0ea3fb224a341a58ec3d.tar.bz2
Support definitions, try and send a packet every few seconds, not yet working
-rw-r--r--Cargo.toml7
-rw-r--r--definitions/raw.yaml59
-rw-r--r--src/definitions.rs71
-rw-r--r--src/isotp.rs2
-rw-r--r--src/main.rs65
5 files changed, 196 insertions, 8 deletions
diff --git a/Cargo.toml b/Cargo.toml
index cea6c29..a9fdb1f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,9 +4,12 @@ name = "canopied"
version = "0.1.0"
[dependencies]
env_logger = "0.5.6"
-futures = "0.1.20"
+futures = "0.1.21"
log = "0.4.1"
mio = "0.6.14"
+serde = "1.0.37"
+serde_derive = "1.0.37"
+serde_yaml = "0.7.3"
socketcan = "1.7.0"
time = "0.1.39"
-tokio = "0.1.4"
+tokio = "0.1.5"
diff --git a/definitions/raw.yaml b/definitions/raw.yaml
index a48878d..bc3ce9c 100644
--- a/definitions/raw.yaml
+++ b/definitions/raw.yaml
@@ -282,3 +282,62 @@
offset: 32
length: 1
kind: unsigned
+
+- name: ldc
+ description: Low Voltage DC-DC Converter
+ request:
+ sendto: 0x7c5
+ packet: [0x21, 0x01]
+ response:
+ length: 18
+ values:
+ - name: ldc_fixed
+ offset: 0
+ length: 6
+ check: [0x61, 0x01, 0x1F, 0x00, 0x00, 0x00]
+ - name: low_voltage
+ offset: 7
+ length: 1
+ kind: unsigned
+ - name: low_current
+ offset: 8
+ length: 1
+ kind: unsigned
+ - name: high_voltage
+ offset: 9
+ length: 1
+ kind: unsigned
+ - name: maybe_temperature
+ offset: 10
+ length: 1
+ kind: unsigned
+
+- name: obc
+ description: On-Board charger
+ request:
+ sendto: 0x794
+ packet: [0x21, 0x02]
+ response:
+ length: 23
+ values:
+ - name: obc_fixed
+ offset: 0
+ length: 6
+ check: [0x61, 0x02, 0xE8, 0x03, 0x1F, 0x00]
+ - name: ac_volts
+ offset: 8
+ length: 2
+ kind: unsigned
+ - name: dc_volts
+ offset: 14
+ length: 2
+ kind: unsigned
+ - name: ac_current
+ offset: 16
+ length: 2
+ kind: unsigned
+ - name: pilot_duty_cycle
+ offset: 19
+ length: 1
+ kind: unsigned
+
diff --git a/src/definitions.rs b/src/definitions.rs
new file mode 100644
index 0000000..6059c93
--- /dev/null
+++ b/src/definitions.rs
@@ -0,0 +1,71 @@
+use serde_yaml;
+use serde_derive;
+
+#[derive(Debug, Deserialize)]
+pub struct RawECURequest {
+ sendto: u16,
+ recvfrom: Option<u16>,
+ flowctrl: Option<u16>,
+ packet: Vec<u8>,
+}
+
+impl RawECURequest {
+ pub fn sendto(&self) -> u16 {
+ self.sendto
+ }
+
+ pub fn recvfrom(&self) -> u16 {
+ self.recvfrom.unwrap_or(self.sendto + 8)
+ }
+
+ pub fn flowctrl(&self) -> u16 {
+ self.flowctrl.unwrap_or(self.sendto)
+ }
+
+ pub fn packet(&self) -> &[u8] {
+ &self.packet
+ }
+}
+
+#[derive(Debug, Deserialize)]
+pub struct RawECUResponseEntry {
+ name: String,
+ offset: u16,
+ length: u16,
+ check: Option<Vec<u8>>,
+ kind: Option<String>,
+ base: Option<u8>,
+ count: Option<u8>,
+}
+
+#[derive(Debug, Deserialize)]
+pub struct RawECUResponse {
+ length: u16,
+ values: Vec<RawECUResponseEntry>,
+}
+
+#[derive(Debug, Deserialize)]
+pub struct RawECUEntry {
+ name: String,
+ description: String,
+ request: RawECURequest,
+ response: RawECUResponse,
+}
+
+impl RawECUEntry {
+ pub fn get() -> Vec<RawECUEntry> {
+ serde_yaml::from_slice(include_bytes!("../definitions/raw.yaml")).unwrap()
+ }
+
+ pub fn name(&self) -> &str {
+ &self.name
+ }
+
+ pub fn description(&self) -> &str {
+ &self.description
+ }
+
+ pub fn request(&self) -> &RawECURequest {
+ &self.request
+ }
+}
diff --git a/src/isotp.rs b/src/isotp.rs
index d5dc3d5..91394bc 100644
--- a/src/isotp.rs
+++ b/src/isotp.rs
@@ -155,7 +155,7 @@ impl ISOTP {
tokio::spawn(
receiver
.map(|f| {
- trace!("Forwarding {:X}", f);
+ trace!("Forwarding to CANStream {:X}", f);
f
})
.forward(sink.sink_map_err(|_| ()))
diff --git a/src/main.rs b/src/main.rs
index dcc3597..a0a4b7c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -6,20 +6,75 @@ extern crate tokio;
extern crate log;
extern crate env_logger;
+extern crate serde;
+#[macro_use]
+extern crate serde_derive;
+extern crate serde_yaml;
+
mod canstream;
mod isotp;
+mod definitions;
use tokio::prelude::*;
+use std::env::args;
+use futures::sync::mpsc;
+use tokio::timer::Delay;
+use std::time::{Duration, Instant};
fn main() {
env_logger::init();
- let world = futures::future::lazy(|| {
- let (sink, stream) = isotp::ISOTP::new("slcan0").split();
+ let raw_defs = definitions::RawECUEntry::get();
+
+ info!("Read {} raw ecu entries", raw_defs.len());
+
+ let world = futures::future::lazy(move || {
+ let args: Vec<String> = args().collect();
+ let iface = if args.len() > 1 {
+ args[1].clone()
+ } else {
+ "vcan0".into()
+ };
+ info!("Connecting Canopied to {}", iface);
+ let (sink, stream) = isotp::ISOTP::new(&iface).split();
+ let (sender, receiver) = mpsc::channel(100);
+
+ trace!("Spawning ISOTP forwarder");
+
+ tokio::spawn(
+ receiver
+ .map(|f| {
+ trace!("Forwarding into ISOTP {:?}", f);
+ f
+ })
+ .forward(sink.sink_map_err(|_| ()))
+ .map(|_| ()),
+ );
- tokio::spawn(futures::future::lazy(|| {
- sink.send((0x7E4, vec![0x21, 0x01])).map(|_| ())
- }));
+ let mut counter = 1;
+ for def in &raw_defs {
+ let req = def.request();
+ let dest = req.sendto();
+ let packet = req.packet().to_vec();
+ let mut targ = sender.clone();
+ let when = Instant::now() + Duration::from_millis(counter * 5000);
+ info!(
+ "Spawning read for {} ({}) at {:?}",
+ def.name(),
+ def.description(),
+ when
+ );
+ tokio::spawn(Delay::new(when).map_err(|_| ()).and_then(move |_| {
+ targ.send((dest, packet))
+ .wait()
+ .and_then(|mut s| {
+ s.close().unwrap();
+ Ok(())
+ })
+ .map_err(|_| ())
+ }));
+ counter += 1;
+ }
tokio::spawn(
stream