rustflexstack/facilities/local_dynamic_map/mod.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) facility.
5//!
6//! Implements the ETSI TS 103 301 Local Dynamic Map in pure Rust, replacing
7//! the Python reference's TinyDB JSON-on-disk backend with a lock-guarded
8//! in-memory `HashMap`.
9//!
10//! # Architecture
11//! ```text
12//! ┌──────────────────────────────────────────────────────────┐
13//! │ LdmFacility │
14//! │ │
15//! │ IfLdm3 ──────────────────────────────────┐ │
16//! │ (register/add/update/delete provider data) │ │
17//! │ ▼ │
18//! │ LdmService │
19//! │ │ │
20//! │ IfLdm4 ──────────────────────────────►─┤ │
21//! │ (register/query/subscribe consumer) │ │
22//! │ ▼ │
23//! │ LdmStore │
24//! │ (HashMap<u64, StoredRecord>) │
25//! │ ▲ │
26//! │ LdmMaintenance (1 Hz thread) │
27//! └──────────────────────────────────────────────────────────┘
28//! ```
29//!
30//! # Quick start
31//! ```no_run
32//! use rustflexstack::facilities::local_dynamic_map::{LdmFacility, LdmHandle};
33//! use rustflexstack::facilities::local_dynamic_map::ldm_types::{
34//! RegisterDataConsumerReq, RequestDataObjectsReq,
35//! };
36//! use rustflexstack::facilities::local_dynamic_map::ldm_constants::ITS_AID_CAM;
37//!
38//! // Parc Tecnològic del Vallès, 5 km radius
39//! let ldm: LdmHandle = LdmFacility::new(415_520_000, 21_340_000, 5_000.0);
40//!
41//! ldm.if_ldm_4.register_data_consumer(RegisterDataConsumerReq { application_id: ITS_AID_CAM });
42//!
43//! let resp = ldm.if_ldm_4.request_data_objects(RequestDataObjectsReq {
44//! application_id: ITS_AID_CAM,
45//! data_object_types: vec![ITS_AID_CAM],
46//! filter: None,
47//! order: None,
48//! max_results: None,
49//! });
50//! println!("LDM contains {} CAM record(s)", resp.data_objects.len());
51//! ```
52
53pub mod if_ldm_3;
54pub mod if_ldm_4;
55pub mod ldm_constants;
56pub mod ldm_maintenance;
57pub mod ldm_service;
58pub mod ldm_storage;
59pub mod ldm_types;
60
61pub use if_ldm_3::IfLdm3;
62pub use if_ldm_4::IfLdm4;
63pub use ldm_maintenance::AreaCenter;
64pub use ldm_storage::ItsDataObject;
65
66use std::sync::Arc;
67
68use ldm_maintenance::LdmMaintenance;
69use ldm_service::LdmService;
70use ldm_storage::LdmStore;
71
72/// A cheaply-cloneable handle to a running LDM facility.
73///
74/// All clones share the same underlying service and store.
75pub type LdmHandle = Arc<LdmFacility>;
76
77/// Top-level LDM facility.
78///
79/// Holds both ETSI interfaces and exposes them as public fields so callers
80/// can invoke provider and consumer operations directly:
81///
82/// ```ignore
83/// # use rustflexstack::facilities::local_dynamic_map::{LdmFacility, LdmHandle};
84/// # let ldm: LdmHandle = LdmFacility::new(0, 0, 0.0);
85/// ldm.if_ldm_3.add_provider_data(/* AddDataProviderReq */);
86/// ldm.if_ldm_4.request_data_objects(/* RequestDataObjectsReq */);
87/// ```
88pub struct LdmFacility {
89 /// ETSI IF.LDM.3 — Data Provider interface.
90 pub if_ldm_3: IfLdm3,
91 /// ETSI IF.LDM.4 — Data Consumer interface.
92 pub if_ldm_4: IfLdm4,
93 /// Dynamically updatable maintenance area centre.
94 pub area_center: AreaCenter,
95}
96
97impl LdmFacility {
98 /// Create a new LDM facility and spawn the maintenance thread.
99 ///
100 /// # Arguments
101 /// * `area_lat_etsi` — latitude of the local station (ETSI × 1e7).
102 /// * `area_lon_etsi` — longitude of the local station (ETSI × 1e7).
103 /// * `area_radius_m` — maintenance area radius in metres.
104 /// Pass `0.0` to disable spatial GC.
105 ///
106 /// # Returns
107 /// An `LdmHandle` (`Arc<LdmFacility>`) that can be shared freely across
108 /// threads.
109 pub fn new(area_lat_etsi: i32, area_lon_etsi: i32, area_radius_m: f64) -> LdmHandle {
110 let store = Arc::new(std::sync::RwLock::new(LdmStore::new()));
111 let service = LdmService::new(store);
112
113 let if_ldm_3 = IfLdm3::new(service.clone());
114 let if_ldm_4 = IfLdm4::new(service.clone());
115
116 let area_center =
117 LdmMaintenance::spawn(service, area_lat_etsi, area_lon_etsi, area_radius_m);
118
119 Arc::new(LdmFacility {
120 if_ldm_3,
121 if_ldm_4,
122 area_center,
123 })
124 }
125}