openthread_fuchsia/backing/
mod.rs1use futures::prelude::*;
6use openthread::prelude::*;
7use spinel_pack::prelude::*;
8
9use fuchsia_async as fasync;
10use futures::channel::mpsc as fmpsc;
11use lowpan_driver_common::spinel::*;
12use std::cell::{Cell, RefCell};
13use std::sync::atomic::AtomicBool;
14use std::sync::mpsc;
15use std::time::Duration;
16
17#[allow(unused_imports)]
18use log::{debug, error, info, trace, warn};
19
20mod alarm;
21mod infra_if;
22mod nat64;
23mod radio;
24mod reset;
25mod resolver;
26mod trel;
27mod udp;
28
29pub(crate) use alarm::*;
30pub(crate) use infra_if::InfraIfInstance;
31pub(crate) use nat64::{Nat64Instance, Nat64PlatformInstance};
32use openthread::ot::NetifIdentifier;
33pub(crate) use reset::PlatformResetRequested;
34pub(crate) use resolver::*;
35pub(crate) use udp::*;
36
37pub(super) struct PlatformBacking {
38 pub(super) ot_to_rcp_sender: RefCell<mpsc::Sender<Vec<u8>>>,
39 pub(super) rcp_to_ot_receiver: RefCell<mpsc::Receiver<Vec<u8>>>,
40 pub(super) alarm: AlarmInstance,
41 pub(super) netif_index_thread: Option<ot::NetifIndex>,
42 pub(super) netif_index_backbone: Option<ot::NetifIndex>,
43 pub(super) trel: RefCell<Option<trel::TrelInstance>>,
44 pub(super) infra_if: Option<InfraIfInstance>,
45 pub(super) is_platform_reset_requested: AtomicBool,
46 pub(super) nat64: Nat64Instance,
47 pub(super) resolver: Resolver,
48}
49
50impl PlatformBacking {
51 unsafe fn glob() -> &'static mut Option<PlatformBacking> {
55 static mut SINGLETON_BACKING: Option<PlatformBacking> = None;
56 #[allow(static_mut_refs)]
58 unsafe {
59 &mut SINGLETON_BACKING
60 }
61 }
62
63 pub(super) unsafe fn as_ref() -> &'static PlatformBacking {
67 unsafe { Self::glob() }.as_ref().expect("Platform is uninitialized")
68 }
69
70 pub(super) unsafe fn set_singleton(backing: PlatformBacking) {
74 assert!(
75 unsafe { Self::glob() }.replace(backing).is_none(),
76 "Tried to make two Platform instances"
77 );
78 }
79
80 pub(super) unsafe fn drop_singleton() {
82 assert!(
85 unsafe { Self::glob() }.take().is_some(),
86 "Tried to drop singleton that was never allocated"
87 );
88 }
89}
90
91impl PlatformBacking {
92 fn lookup_netif_index(&self, id: ot::NetifIdentifier) -> Option<ot::NetifIndex> {
93 match id {
94 NetifIdentifier::Backbone => self.netif_index_backbone,
95 NetifIdentifier::Thread => self.netif_index_thread,
96 NetifIdentifier::Unspecified => Some(ot::NETIF_INDEX_UNSPECIFIED),
97 }
98 }
99}