Skip to main content

rustflexstack/facilities/vru_awareness_service/
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//! VRU Awareness Service — VRU Awareness Basic Service (ETSI TS 103 300-3 V2.3.1).
5//!
6//! Implements VAM generation and reception on top of the BTP and
7//! GeoNetworking layers already present in this crate.
8//!
9//! # Architecture
10//! ```text
11//! LocationService ──GpsFix──► VAMTransmissionManagement ──BTPDataRequest──► BTP
12//!                                                                            │
13//!                              VAMReceptionManagement  ◄──BTPDataIndication──┘
14//!                                      │
15//!                               Sender<Vam>  ──►  application (Receiver<Vam>)
16//! ```
17//!
18//! # Quick start
19//! ```no_run
20//! use rustflexstack::btp::router::Router as BTPRouter;
21//! use rustflexstack::geonet::mib::Mib;
22//! use rustflexstack::facilities::vru_awareness_service::{
23//!     VruAwarenessService, DeviceData,
24//! };
25//! use rustflexstack::facilities::location_service::LocationService;
26//!
27//! let mib = Mib::new();
28//! let (btp_handle, _) = BTPRouter::spawn(mib);
29//! let mut loc_svc = LocationService::new();
30//!
31//! let (vru_svc, vam_rx) =
32//!     VruAwarenessService::new(btp_handle, DeviceData::default());
33//! vru_svc.start(loc_svc.subscribe());
34//!
35//! while let Ok(vam) = vam_rx.recv() {
36//!     println!("VAM from station {}", vam.header.0.station_id.0);
37//! }
38//! ```
39
40pub mod vam_bindings;
41pub mod vam_coder;
42pub mod vam_reception;
43pub mod vam_transmission;
44
45pub use vam_coder::{Vam, VamCoder};
46pub use vam_reception::VAMReceptionManagement;
47pub use vam_transmission::{DeviceData, VAMTransmissionManagement};
48
49use crate::btp::router::BTPRouterHandle;
50use crate::facilities::location_service::GpsFix;
51use std::sync::mpsc::{self, Receiver, Sender};
52
53/// Top-level VRU Awareness Service.
54///
55/// Mirrors `VRUAwarenessService` in
56/// `flexstack/facilities/vru_awareness_service/vru_awareness_service.py`.
57///
58/// Create with [`new`](Self::new), then call [`start`](Self::start) once the
59/// GPS channel wiring is ready.
60pub struct VruAwarenessService {
61    btp_handle: BTPRouterHandle,
62    device_data: DeviceData,
63    /// Reception management writes decoded VAMs into this sender.
64    vam_tx: Sender<Vam>,
65}
66
67impl VruAwarenessService {
68    /// Create a new VRU Awareness Service.
69    ///
70    /// Returns `(service, vam_receiver)`.  Hold `vam_receiver` to consume
71    /// decoded incoming VAMs.  Call [`start`](Self::start) with a GPS fix
72    /// receiver to begin transmitting and receiving.
73    pub fn new(btp_handle: BTPRouterHandle, device_data: DeviceData) -> (Self, Receiver<Vam>) {
74        let (vam_tx, vam_rx) = mpsc::channel::<Vam>();
75        let svc = VruAwarenessService {
76            btp_handle,
77            device_data,
78            vam_tx,
79        };
80        (svc, vam_rx)
81    }
82
83    /// Spawn the transmission and reception management threads.
84    ///
85    /// * `gps_rx` — a `Receiver<GpsFix>` from [`LocationService::subscribe`].
86    ///
87    /// The reception thread registers BTP port 2018 immediately.
88    /// The transmission thread starts producing VAMs as soon as fixes arrive.
89    pub fn start(self, gps_rx: Receiver<GpsFix>) {
90        let coder = VamCoder::new();
91        VAMReceptionManagement::spawn(self.btp_handle.clone(), coder.clone(), self.vam_tx);
92        VAMTransmissionManagement::spawn(self.btp_handle, coder, self.device_data, gps_rx);
93    }
94}