Skip to main content

rustflexstack/facilities/ca_basic_service/
cam_ldm_adaptation.rs

1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (C) 2024 Fundació Privada Internet i Innovació Digital a Catalunya (i2CAT)
3
4//! Local Dynamic Map (LDM) adaptation for the CA Basic Service.
5//!
6//! Mirrors `CABasicServiceLDM` in
7//! `flexstack/facilities/ca_basic_service/cam_ldm_adaptation.py`.
8//!
9//! Handles registration of the CAM data provider (ITS-AID 2) on IF.LDM.3
10//! and ingestion of transmitted and received CAMs into the LDM store.
11
12use super::cam_coder::Cam;
13use crate::facilities::local_dynamic_map::{
14    ldm_constants::{now_its_ms, ITS_AID_CAM},
15    ldm_storage::ItsDataObject,
16    ldm_types::{AddDataProviderReq, AddDataProviderResult, RegisterDataProviderReq},
17    LdmHandle,
18};
19
20/// Default time validity for CAM records in the LDM (seconds).
21pub const DEFAULT_CAM_TIME_VALIDITY_S: u32 = 5;
22
23/// LDM adapter for the Cooperative Awareness Basic Service.
24///
25/// Simplifies registration and insertion of CAM data into the Local Dynamic Map.
26#[derive(Clone)]
27pub struct CABasicServiceLDM {
28    pub ldm: LdmHandle,
29    pub time_validity_s: u32,
30}
31
32/// Alias using standard Rust PascalCase convention.
33pub type CaBasicServiceLdm = CABasicServiceLDM;
34
35impl CABasicServiceLDM {
36    /// Create and register a new CA Basic Service LDM adapter.
37    ///
38    /// Automatically registers the CAM provider (`ITS_AID_CAM = 2`) with the LDM.
39    pub fn new(ldm: LdmHandle, time_validity_s: u32) -> Self {
40        ldm.if_ldm_3
41            .register_data_provider(RegisterDataProviderReq {
42                application_id: ITS_AID_CAM,
43            });
44
45        CABasicServiceLDM {
46            ldm,
47            time_validity_s,
48        }
49    }
50
51    /// Add a Cooperative Awareness Message to the LDM.
52    ///
53    /// Extracts the reference position (latitude, longitude, altitude) and
54    /// stores the typed CAM object.
55    ///
56    /// Returns the assigned `record_id` on success, or an error description.
57    pub fn add_provider_data_to_ldm(&self, cam: &Cam) -> Result<u64, String> {
58        let ref_pos = &cam.cam.cam_parameters.basic_container.reference_position;
59        let lat = ref_pos.latitude.0;
60        let lon = ref_pos.longitude.0;
61        let alt = ref_pos.altitude.altitude_value.0;
62
63        let req = AddDataProviderReq {
64            application_id: ITS_AID_CAM,
65            timestamp_its: now_its_ms(),
66            lat_etsi: lat,
67            lon_etsi: lon,
68            altitude_cm: alt / 10, // AltitudeValue is 0.01 m -> cm
69            time_validity_s: self.time_validity_s,
70            data_object: ItsDataObject::Cam(Box::new(cam.clone())),
71        };
72
73        let resp = self.ldm.if_ldm_3.add_provider_data(req);
74        match resp.result {
75            AddDataProviderResult::Succeed => resp
76                .record_id
77                .ok_or_else(|| "LDM returned Succeed without a record_id".to_string()),
78            AddDataProviderResult::Failed => {
79                Err("LDM add_provider_data returned Failed".to_string())
80            }
81        }
82    }
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88    use crate::facilities::ca_basic_service::cam_coder::generate_white_cam_static;
89    use crate::facilities::local_dynamic_map::LdmFacility;
90
91    #[test]
92    fn test_init_registers_data_provider() {
93        let ldm = LdmFacility::new(415_520_000, 21_340_000, 5_000.0);
94        let adapter = CABasicServiceLDM::new(ldm, 5);
95        assert_eq!(adapter.time_validity_s, 5);
96    }
97
98    #[test]
99    fn test_add_provider_data_to_ldm_success() {
100        let ldm = LdmFacility::new(415_520_000, 21_340_000, 50_000.0);
101        let adapter = CABasicServiceLDM::new(ldm, 5);
102        let cam = generate_white_cam_static();
103
104        let record_id = adapter
105            .add_provider_data_to_ldm(&cam)
106            .expect("should add CAM to LDM");
107        assert!(record_id > 0);
108    }
109}