1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
45use futures::prelude::*;
6use openthread::prelude::*;
7use spinel_pack::prelude::*;
89use 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;
1617#[allow(unused_imports)]
18use log::{debug, error, info, trace, warn};
1920mod alarm;
21mod infra_if;
22mod nat64;
23mod radio;
24mod reset;
25mod resolver;
26mod trel;
27mod udp;
2829pub(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::*;
3637pub(super) struct PlatformBacking {
38pub(super) ot_to_rcp_sender: RefCell<mpsc::Sender<Vec<u8>>>,
39pub(super) rcp_to_ot_receiver: RefCell<mpsc::Receiver<Vec<u8>>>,
40pub(super) alarm: AlarmInstance,
41pub(super) netif_index_thread: Option<ot::NetifIndex>,
42pub(super) netif_index_backbone: Option<ot::NetifIndex>,
43pub(super) trel: RefCell<Option<trel::TrelInstance>>,
44pub(super) infra_if: Option<InfraIfInstance>,
45pub(super) is_platform_reset_requested: AtomicBool,
46pub(super) nat64: Nat64Instance,
47pub(super) resolver: Resolver,
48}
4950impl PlatformBacking {
51// SAFETY: Unsafe because the type system cannot enforce thread safety on globals.
52 // Caller should ensure that no other calls in this section are being
53 // simultaneously made on other threads.
54unsafe fn glob() -> &'static mut Option<PlatformBacking> {
55static mut SINGLETON_BACKING: Option<PlatformBacking> = None;
56// TODO(b/319328255) -- Fix usage so lint no longer applies
57#[allow(static_mut_refs)]
58&mut SINGLETON_BACKING
59 }
6061// SAFETY: Unsafe because the type system cannot enforce thread safety on globals.
62 // Caller should ensure that no other calls in this section are being
63 // simultaneously made on other threads.
64pub(super) unsafe fn as_ref() -> &'static PlatformBacking {
65Self::glob().as_ref().expect("Platform is uninitialized")
66 }
6768// SAFETY: Unsafe because the type system cannot enforce thread safety on globals.
69 // Caller should ensure that no other calls in this section are being
70 // simultaneously made on other threads.
71pub(super) unsafe fn set_singleton(backing: PlatformBacking) {
72assert!(Self::glob().replace(backing).is_none(), "Tried to make two Platform instances");
73 }
7475// SAFETY: Must only be called from Drop.
76pub(super) unsafe fn drop_singleton() {
77// SAFETY: When we are dropped, we can safely assume no other simultaneous calls are
78 // being made on other threads.
79assert!(Self::glob().take().is_some(), "Tried to drop singleton that was never allocated");
80 }
81}
8283impl PlatformBacking {
84fn lookup_netif_index(&self, id: ot::NetifIdentifier) -> Option<ot::NetifIndex> {
85match id {
86 NetifIdentifier::Backbone => self.netif_index_backbone,
87 NetifIdentifier::Thread => self.netif_index_thread,
88 NetifIdentifier::Unspecified => Some(ot::NETIF_INDEX_UNSPECIFIED),
89 }
90 }
91}