rustflexstack/facilities/decentralized_environmental_notification_service/
denm_reception.rs1use super::den_protocol::{DenmState, ReceptionOutcome};
17use super::denm_coder::{Denm, DenmCoder};
18use super::denm_ssp::ssp_consistent;
19use super::denm_transmission::{new_protocol, DenShared, VehicleData, DENM_BTP_PORT};
20use crate::btp::router::BTPRouterHandle;
21use crate::btp::service_access_point::BTPDataIndication;
22use crate::geonet::service_access_point::{HeaderSubType, HeaderType};
23use std::sync::mpsc::{self, Sender};
24use std::sync::Arc;
25use std::thread;
26
27#[derive(Debug, Clone)]
29pub struct ReceivedDenm {
30 pub denm: Denm,
31 pub state: DenmState,
33}
34
35pub struct DENMReceptionManagement;
37
38impl DENMReceptionManagement {
39 pub fn spawn(btp_handle: BTPRouterHandle, coder: DenmCoder, denm_tx: Sender<Denm>) {
42 let shared = DenShared::new(new_protocol(&VehicleData::default(), false, &coder));
43 Self::spawn_with(btp_handle, coder, shared, Some(denm_tx), None);
44 }
45
46 pub fn spawn_with(
50 btp_handle: BTPRouterHandle,
51 coder: DenmCoder,
52 shared: Arc<DenShared>,
53 denm_tx: Option<Sender<Denm>>,
54 events_tx: Option<Sender<ReceivedDenm>>,
55 ) {
56 let (ind_tx, ind_rx) = mpsc::channel::<BTPDataIndication>();
57 btp_handle.register_port(DENM_BTP_PORT, ind_tx);
58 thread::spawn(move || {
59 while let Ok(indication) = ind_rx.recv() {
60 let Some(received) = process_indication(&coder, &shared, &indication) else {
61 continue;
62 };
63 let mut delivered = false;
64 if let Some(tx) = &denm_tx {
65 delivered |= tx.send(received.denm.clone()).is_ok();
66 }
67 if let Some(tx) = &events_tx {
68 delivered |= tx.send(received).is_ok();
69 }
70 if !delivered && (denm_tx.is_some() || events_tx.is_some()) {
71 break; }
73 }
74 });
75 }
76}
77
78pub fn process_indication(
81 coder: &DenmCoder,
82 shared: &DenShared,
83 indication: &BTPDataIndication,
84) -> Option<ReceivedDenm> {
85 if indication
88 .gn_its_aid
89 .is_some_and(|aid| aid != super::denm_transmission::DENM_ITS_AID)
90 {
91 return None;
92 }
93 let denm = coder.decode(&indication.data).ok()?;
95 let area = match (
96 indication.gn_destination_area,
97 &indication.gn_packet_transport_type.header_type,
98 &indication.gn_packet_transport_type.header_sub_type,
99 ) {
100 (Some(area), HeaderType::GeoBroadcast, HeaderSubType::GeoBroadcast(kind)) => {
101 Some((area, *kind))
102 }
103 _ => None,
104 };
105 let now = shared.now();
106 let ssp_ok = ssp_consistent(indication.gn_permissions.as_deref(), &denm);
107 let outcome: ReceptionOutcome = shared
108 .protocol
109 .lock()
110 .unwrap()
111 .receive(&denm, now, area, ssp_ok);
112 shared.wakeup.notify_all();
113 if !outcome.accepted {
114 return None;
115 }
116 Some(ReceivedDenm {
117 denm,
118 state: outcome.state.unwrap_or(DenmState::Active),
119 })
120}