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
178
179
180
181
182
183
184
pub struct Name {
name: u64,
}
impl Name {
pub fn new(
arbitrary_address_capable: bool,
industry_group: u8,
vehicle_system_instance: u8,
vehicle_system: u8,
function: u8,
function_instance: u8,
ecu_instance: u8,
manufacturer_code: u16,
identity_number: u32,
) -> Self {
let mut name: u64 = 0;
if arbitrary_address_capable {
name |= 0x8000000000000000;
}
name |= (industry_group as u64) << 60;
name |= (vehicle_system_instance as u64) << 56;
name |= (vehicle_system as u64) << 49;
name |= (function as u64) << 40;
name |= (function_instance as u64) << 35;
name |= (ecu_instance as u64) << 32;
name |= (manufacturer_code as u64) << 21;
name |= identity_number as u64;
Name { name }
}
pub fn arbitrary_address_capable(&self) -> bool {
self.name & 0x8000000000000000 > 0
}
pub fn industry_group(&self) -> u8 {
((self.name >> 60) & 0x07) as u8
}
pub fn vehicle_system_instance(&self) -> u8 {
((self.name >> 56) & 0x0f) as u8
}
pub fn vehicle_system(&self) -> u8 {
((self.name >> 49) & 0x7f) as u8
}
pub fn function(&self) -> u8 {
((self.name >> 40) & 0xff) as u8
}
pub fn function_instance(&self) -> u8 {
((self.name >> 35) & 0x1f) as u8
}
pub fn ecu_instance(&self) -> u8 {
((self.name >> 32) & 0x07) as u8
}
pub fn manufacturer_code(&self) -> u16 {
((self.name >> 21) & 0x07ff) as u16
}
pub fn identity_number(&self) -> u32 {
(self.name & 0x1fffff) as u32
}
pub fn value(&self) -> u64 {
self.name
}
}
#[cfg(test)]
mod tests {
use crate::Name;
#[test]
fn name_new() {
struct TestCase {
arbitrary_address_capable: bool,
industry_group: u8,
vehicle_system_instance: u8,
vehicle_system: u8,
function: u8,
function_instance: u8,
ecu_instance: u8,
manufacturer_code: u16,
identity_number: u32,
}
let test_cases = [
TestCase {
arbitrary_address_capable: true,
industry_group: 0x02,
vehicle_system_instance: 0x05,
vehicle_system: 0x55,
function: 0x55,
function_instance: 0x15,
ecu_instance: 0x05,
manufacturer_code: 0x0555,
identity_number: 0x00155555,
},
TestCase {
arbitrary_address_capable: true,
industry_group: 0x02,
vehicle_system_instance: 0x0a,
vehicle_system: 0x55,
function: 0xaa,
function_instance: 0x15,
ecu_instance: 0x02,
manufacturer_code: 0x0555,
identity_number: 0x000aaaaa,
},
TestCase {
arbitrary_address_capable: false,
industry_group: 0x05,
vehicle_system_instance: 0x05,
vehicle_system: 0x2a,
function: 0x55,
function_instance: 0x0a,
ecu_instance: 0x05,
manufacturer_code: 0x02aa,
identity_number: 0x00155555,
},
];
for i in &test_cases {
let name = Name::new(
i.arbitrary_address_capable,
i.industry_group,
i.vehicle_system_instance,
i.vehicle_system,
i.function,
i.function_instance,
i.ecu_instance,
i.manufacturer_code,
i.identity_number,
);
assert_eq!(
i.arbitrary_address_capable,
name.arbitrary_address_capable()
);
assert_eq!(i.industry_group, name.industry_group());
assert_eq!(i.vehicle_system_instance, name.vehicle_system_instance());
assert_eq!(i.vehicle_system, name.vehicle_system());
assert_eq!(i.function, name.function());
assert_eq!(i.function_instance, name.function_instance());
assert_eq!(i.ecu_instance, name.ecu_instance());
assert_eq!(i.manufacturer_code, name.manufacturer_code());
assert_eq!(i.identity_number, name.identity_number());
}
}
}