summaryrefslogtreecommitdiff
path: root/src/definitions.rs
blob: aa7880962c38a4f2d8f7ae608030efe33f26df8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use serde_yaml;

use types::{NamedValue, Value};

#[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: usize,
    length: usize,
    check: Option<Vec<u8>>,
    kind: Option<String>,
    base: Option<u8>,
    count: Option<u8>,
}

#[derive(Debug, Deserialize)]
pub struct RawECUResponse {
    length: usize,
    values: Vec<RawECUResponseEntry>,
}

#[derive(Debug, Deserialize)]
pub struct RawECUEntry {
    name: String,
    description: String,
    request: RawECURequest,
    response: RawECUResponse,
}

impl RawECUEntry {
    pub fn get_all() -> &'static [RawECUEntry] {
        lazy_static! {
            static ref ECUS: Vec<RawECUEntry> =
                serde_yaml::from_slice(include_bytes!("../definitions/raw.yaml")).unwrap();
        }
        &ECUS
    }

    pub fn ecu_for(id: u16, payload: &[u8]) -> Option<&'static RawECUEntry> {
        'ecu: for ecu in RawECUEntry::get_all() {
            if (ecu.request().sendto() == id || ecu.request().recvfrom() == id
                || ecu.request().flowctrl() == id)
                && ecu.response.length == payload.len()
            {
                for entry in &ecu.response.values {
                    if let Some(ref vals) = entry.check {
                        let has = &payload[entry.offset..(entry.offset + vals.len())];
                        trace!("Check: Does {:?} == {:?}", vals, has);
                        for i in 0..vals.len() {
                            if has[i] != vals[i] {
                                trace!("Oops, not the same");
                                continue 'ecu;
                            }
                        }
                    }
                }
                return Some(ecu);
            }
        }
        None
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn description(&self) -> &str {
        &self.description
    }

    pub fn request(&self) -> &RawECURequest {
        &self.request
    }

    pub fn decode(&self, payload: &[u8]) -> Option<Vec<NamedValue>> {
        let mut ret = Vec::new();

        if payload.len() != self.response.length {
            warn!(
                "Unable to decode, length incorrect. {} != {}",
                payload.len(),
                self.response.length
            );
            return None;
        }

        for entry in &self.response.values {
            if let Some(ref kind) = entry.kind {
                let value: Value = match kind.as_ref() {
                    "unsigned" => {
                        trace!("Unsigned");
                        match entry.length {
                            1 => payload[entry.offset].into(),
                            2 => (((payload[entry.offset] as u16) << 8)
                                + (payload[entry.offset + 1] as u16))
                                .into(),
                            3 => (((payload[entry.offset] as u32) << 16)
                                + ((payload[entry.offset + 1] as u32) << 8)
                                + (payload[entry.offset + 2] as u32))
                                .into(),
                            4 => (((payload[entry.offset] as u32) << 24)
                                + ((payload[entry.offset + 1] as u32) << 16)
                                + ((payload[entry.offset + 2] as u32) << 8)
                                + (payload[entry.offset + 3] as u32))
                                .into(),
                            _ => panic!("Unhandled length: {}", entry.length),
                        }
                    }
                    "signed" => {
                        trace!("Signed");
                        match entry.length {
                            1 => (payload[entry.offset] as i8).into(),
                            2 => ((((payload[entry.offset] as u16) << 8)
                                + (payload[entry.offset + 1] as u16))
                                as i16)
                                .into(),
                            3 => ((((payload[entry.offset] as u32) << 16)
                                + ((payload[entry.offset + 1] as u32) << 8)
                                + (payload[entry.offset + 2] as u32))
                                as i32)
                                .into(),
                            4 => ((((payload[entry.offset] as u32) << 24)
                                + ((payload[entry.offset + 1] as u32) << 16)
                                + ((payload[entry.offset + 2] as u32) << 8)
                                + (payload[entry.offset + 3] as u32))
                                as i32)
                                .into(),
                            _ => panic!("Unhandled length: {}", entry.length),
                        }
                    }
                    "bit" => {
                        trace!("Bit");
                        ((payload[entry.offset] & (1 << entry.base.unwrap())) != 0).into()
                    }
                    _ => {
                        panic!("Unhandled");
                    }
                };
                let name = {
                    let mut n = self.name.clone();
                    n.push('.');
                    n.push_str(&entry.name);
                    n
                };
                ret.push(NamedValue::new(&name, Some(value)));
            }
        }

        Some(ret)
    }
}