sim-lib-auto-core 0.1.0

Automotive domain citizens and capability manifests for SIM.
Documentation
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! Automotive citizens used by core manifests and transport descriptors.

use sim_citizen_derive::Citizen;
use sim_kernel::CapabilityName;

use crate::{
    AUTO_CONTROL_EXEC, AUTO_DIAGNOSTICS_READ, AUTO_MANIFEST_READ, AUTO_ORDER, AUTO_SERVICE_WRITE,
    AUTO_TELEMETRY_READ, AUTO_TRANSPORT_CONNECT,
};

mod fields;

/// A modeled vehicle identity safe for committed fixtures and manifests.
#[derive(Clone, Debug, PartialEq, Eq, Citizen)]
#[citizen(symbol = "auto/VehicleId", version = 0)]
pub struct VehicleId {
    /// Namespace that owns the modeled key, such as a shop or fixture set.
    pub namespace: String,
    /// Synthetic key for the vehicle inside the namespace.
    pub key: String,
}

impl Default for VehicleId {
    fn default() -> Self {
        vehicle_id_example()
    }
}

impl VehicleId {
    /// Builds a modeled vehicle identity.
    pub fn new(namespace: impl Into<String>, key: impl Into<String>) -> Self {
        Self {
            namespace: namespace.into(),
            key: key.into(),
        }
    }
}

/// A decoded diagnostic trouble code with a modeled description.
#[derive(Clone, Debug, PartialEq, Eq, Citizen)]
#[citizen(symbol = "auto/Dtc", version = 0)]
pub struct Dtc {
    /// Diagnostic family or subsystem.
    pub system: String,
    /// Diagnostic code text.
    pub code: String,
    /// Human-facing description for the modeled code.
    pub description: String,
    /// Standardized diagnostic status bits supplied by the transport.
    #[citizen(with = "fields::dtc_status_field")]
    pub status: DtcStatus,
}

impl Default for Dtc {
    fn default() -> Self {
        dtc_example()
    }
}

impl Dtc {
    /// Builds a diagnostic trouble code descriptor.
    pub fn new(
        system: impl Into<String>,
        code: impl Into<String>,
        description: impl Into<String>,
    ) -> Self {
        Self::with_status(system, code, description, DtcStatus::default())
    }

    /// Builds a diagnostic trouble code descriptor with explicit status bits.
    pub fn with_status(
        system: impl Into<String>,
        code: impl Into<String>,
        description: impl Into<String>,
        status: DtcStatus,
    ) -> Self {
        Self {
            system: system.into(),
            code: code.into(),
            description: description.into(),
            status,
        }
    }
}

/// Standard UDS diagnostic status bits for a trouble code.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Citizen)]
#[citizen(symbol = "auto/DtcStatus", version = 0)]
pub struct DtcStatus {
    /// The DTC test is failed now.
    pub test_failed: bool,
    /// The DTC test failed during the current operation cycle.
    pub test_failed_this_operation_cycle: bool,
    /// The DTC is pending confirmation.
    pub pending: bool,
    /// The DTC is confirmed.
    pub confirmed: bool,
    /// The DTC has not completed since the last clear operation.
    pub test_not_completed_since_clear: bool,
    /// The DTC failed at least once since the last clear operation.
    pub test_failed_since_clear: bool,
    /// The DTC test has not completed in the current operation cycle.
    pub test_not_completed_this_operation_cycle: bool,
    /// The warning indicator is requested.
    pub warning_indicator: bool,
}

impl DtcStatus {
    /// Decodes a UDS DTC status byte.
    pub fn from_byte(byte: u8) -> Self {
        Self {
            test_failed: byte & 0x01 != 0,
            test_failed_this_operation_cycle: byte & 0x02 != 0,
            pending: byte & 0x04 != 0,
            confirmed: byte & 0x08 != 0,
            test_not_completed_since_clear: byte & 0x10 != 0,
            test_failed_since_clear: byte & 0x20 != 0,
            test_not_completed_this_operation_cycle: byte & 0x40 != 0,
            warning_indicator: byte & 0x80 != 0,
        }
    }

    /// Encodes the status bits back to a UDS status byte.
    pub fn to_byte(self) -> u8 {
        u8::from(self.test_failed)
            | (u8::from(self.test_failed_this_operation_cycle) << 1)
            | (u8::from(self.pending) << 2)
            | (u8::from(self.confirmed) << 3)
            | (u8::from(self.test_not_completed_since_clear) << 4)
            | (u8::from(self.test_failed_since_clear) << 5)
            | (u8::from(self.test_not_completed_this_operation_cycle) << 6)
            | (u8::from(self.warning_indicator) << 7)
    }
}

/// Brand or workshop capability set.
#[derive(Clone, Debug, PartialEq, Eq, Citizen)]
#[citizen(symbol = "auto/BrandCaps", version = 0)]
pub struct BrandCaps {
    /// Brand, workshop, or fleet label.
    pub brand: String,
    /// Capabilities granted by this brand profile.
    pub capabilities: Vec<CapabilityName>,
}

impl Default for BrandCaps {
    fn default() -> Self {
        brand_caps_example()
    }
}

impl BrandCaps {
    /// Builds a brand capability set.
    pub fn new(brand: impl Into<String>, capabilities: Vec<CapabilityName>) -> Self {
        Self {
            brand: brand.into(),
            capabilities,
        }
    }
}

/// An open automotive lane name, such as diagnostics or telemetry.
#[derive(Clone, Debug, PartialEq, Eq, Citizen)]
#[citizen(symbol = "auto/AutoLane", version = 0)]
pub struct AutoLane {
    /// Lane name.
    pub name: String,
}

impl Default for AutoLane {
    fn default() -> Self {
        auto_lane_example()
    }
}

impl AutoLane {
    /// Builds an automotive lane descriptor.
    pub fn new(name: impl Into<String>) -> Self {
        Self { name: name.into() }
    }
}

/// An open effect classification used by operation capabilities.
#[derive(Clone, Debug, PartialEq, Eq, Citizen)]
#[citizen(symbol = "auto/EffectClass", version = 0)]
pub struct EffectClass {
    /// Effect class name.
    pub name: String,
}

impl Default for EffectClass {
    fn default() -> Self {
        effect_class_example()
    }
}

impl EffectClass {
    /// Builds an automotive effect class.
    pub fn new(name: impl Into<String>) -> Self {
        Self { name: name.into() }
    }
}

/// Capability required to run one automotive operation.
#[derive(Clone, Debug, PartialEq, Eq, Citizen)]
#[citizen(symbol = "auto/OpCap", version = 0)]
pub struct OpCap {
    /// Operation symbol text.
    pub operation: String,
    /// Capability required by the operation.
    pub capability: CapabilityName,
    /// Effect class applied by the operation.
    pub effect_class: String,
}

impl Default for OpCap {
    fn default() -> Self {
        op_cap_example()
    }
}

impl OpCap {
    /// Builds an operation capability descriptor.
    pub fn new(
        operation: impl Into<String>,
        capability: CapabilityName,
        effect_class: impl Into<String>,
    ) -> Self {
        Self {
            operation: operation.into(),
            capability,
            effect_class: effect_class.into(),
        }
    }
}

/// Transport endpoint descriptor for an automotive site.
#[derive(Clone, Debug, PartialEq, Eq, Citizen)]
#[citizen(symbol = "auto/TransportSpec", version = 0)]
pub struct TransportSpec {
    /// Transport name.
    pub name: String,
    /// Protocol or codec family.
    pub protocol: String,
    /// Lane this transport serves.
    pub lane: String,
    /// Capability required to read through the transport.
    pub read_capability: CapabilityName,
    /// Capability required to write through the transport.
    pub write_capability: CapabilityName,
}

impl Default for TransportSpec {
    fn default() -> Self {
        transport_spec_example()
    }
}

impl TransportSpec {
    /// Builds an automotive transport descriptor.
    pub fn new(
        name: impl Into<String>,
        protocol: impl Into<String>,
        lane: impl Into<String>,
        read_capability: CapabilityName,
        write_capability: CapabilityName,
    ) -> Self {
        Self {
            name: name.into(),
            protocol: protocol.into(),
            lane: lane.into(),
            read_capability,
            write_capability,
        }
    }
}

/// Site-level automotive manifest.
#[derive(Clone, Debug, PartialEq, Eq, Citizen)]
#[citizen(symbol = "auto/SiteManifest", version = 0)]
pub struct SiteManifest {
    /// Site label.
    pub site: String,
    /// Modeled vehicle key the site describes.
    pub vehicle: String,
    /// Brand or workshop label.
    pub brand: String,
    /// Vehicle makes this site covers, or `*` for a multi-brand fallback.
    pub makes: Vec<String>,
    /// Lane names exposed by the site.
    pub lanes: Vec<String>,
    /// Transport names exposed by the site.
    pub transports: Vec<String>,
    /// Operation names exposed by the site.
    pub operations: Vec<String>,
    /// Explicit per-operation capability and effect policy.
    #[citizen(with = "fields::op_caps_field")]
    pub op_caps: Vec<OpCap>,
    /// Capability ceiling this site may ever hold.
    pub ceiling: Vec<CapabilityName>,
}

impl Default for SiteManifest {
    fn default() -> Self {
        site_manifest_example()
    }
}

impl SiteManifest {
    /// Builds an automotive site manifest.
    pub fn new(
        site: impl Into<String>,
        vehicle: impl Into<String>,
        brand: impl Into<String>,
        lanes: Vec<String>,
        transports: Vec<String>,
        operations: Vec<String>,
    ) -> Self {
        let brand = brand.into();
        Self {
            site: site.into(),
            vehicle: vehicle.into(),
            makes: vec![brand.clone()],
            brand,
            lanes,
            transports,
            operations,
            op_caps: Vec::new(),
            ceiling: Vec::new(),
        }
    }

    /// Replaces the make coverage set.
    pub fn with_makes(mut self, makes: Vec<String>) -> Self {
        self.makes = makes;
        self
    }

    /// Replaces the explicit operation capability policy.
    pub fn with_op_caps(mut self, op_caps: Vec<OpCap>) -> Self {
        self.op_caps = op_caps;
        self
    }

    /// Replaces the site capability ceiling.
    pub fn with_ceiling(mut self, ceiling: Vec<CapabilityName>) -> Self {
        self.ceiling = ceiling;
        self
    }
}

/// Standard diagnostics lane.
pub fn diagnostic_lane() -> AutoLane {
    auto_lane("diagnostics")
}

/// Standard telemetry lane.
pub fn telemetry_lane() -> AutoLane {
    auto_lane("telemetry")
}

/// Standard manifest lane.
pub fn manifest_lane() -> AutoLane {
    auto_lane("manifest")
}

/// Builds an open automotive lane.
pub fn auto_lane(name: impl Into<String>) -> AutoLane {
    AutoLane::new(name)
}

/// Standard diagnostic effect class.
pub fn diagnostic_effect() -> EffectClass {
    EffectClass::new("diagnostic-read")
}

/// Standard control effect class.
pub fn control_effect() -> EffectClass {
    EffectClass::new("control-write")
}

fn vehicle_id_example() -> VehicleId {
    VehicleId::new("fixture", "vehicle-alpha")
}

fn dtc_example() -> Dtc {
    Dtc::with_status(
        "body",
        "B0000",
        "modeled diagnostic",
        DtcStatus::from_byte(0x08),
    )
}

fn brand_caps_example() -> BrandCaps {
    BrandCaps::new(
        "fixture-brand",
        vec![
            CapabilityName::new(AUTO_DIAGNOSTICS_READ),
            CapabilityName::new(AUTO_TELEMETRY_READ),
        ],
    )
}

fn auto_lane_example() -> AutoLane {
    diagnostic_lane()
}

fn effect_class_example() -> EffectClass {
    diagnostic_effect()
}

fn op_cap_example() -> OpCap {
    OpCap::new(
        "diagnostics/read-dtc",
        CapabilityName::new(AUTO_DIAGNOSTICS_READ),
        "diagnostic-read",
    )
}

fn transport_spec_example() -> TransportSpec {
    TransportSpec::new(
        "fixture-transport",
        "modeled-bus",
        "diagnostics",
        CapabilityName::new(AUTO_TRANSPORT_CONNECT),
        CapabilityName::new(AUTO_SERVICE_WRITE),
    )
}

fn site_manifest_example() -> SiteManifest {
    SiteManifest::new(
        "fixture-site",
        "vehicle-alpha",
        "fixture-brand",
        vec!["diagnostics".to_owned(), "telemetry".to_owned()],
        vec!["fixture-transport".to_owned()],
        vec!["diagnostics/read-dtc".to_owned()],
    )
}

#[allow(dead_code)]
fn capability_examples() -> [CapabilityName; 5] {
    [
        CapabilityName::new(AUTO_CONTROL_EXEC),
        CapabilityName::new(AUTO_MANIFEST_READ),
        CapabilityName::new(AUTO_SERVICE_WRITE),
        CapabilityName::new(AUTO_ORDER),
        CapabilityName::new(AUTO_TELEMETRY_READ),
    ]
}