use lock_order::Unlocked;
use net_types::ip::Ip;
use netstack3_base::{
ContextPair as _, ContextProvider, CtxPair, TimerBindingsTypes, TimerHandler as _,
};
use netstack3_device::queue::{ReceiveQueueApi, TransmitQueueApi};
use netstack3_device::socket::DeviceSocketApi;
use netstack3_device::{DeviceAnyApi, DeviceApi};
use netstack3_filter::FilterApi;
use netstack3_icmp_echo::IcmpEchoSocketApi;
use netstack3_ip::device::{DeviceIpAnyApi, DeviceIpApi};
use netstack3_ip::multicast_forwarding::MulticastForwardingApi;
use netstack3_ip::nud::NeighborApi;
use netstack3_ip::raw::RawIpSocketApi;
use netstack3_ip::{RoutesAnyApi, RoutesApi};
use netstack3_tcp::TcpApi;
use netstack3_udp::UdpApi;
use crate::context::CoreCtx;
use crate::counters::CountersApi;
use crate::time::TimerId;
use crate::BindingsTypes;
type CoreApiCtxPair<'a, BP> = CtxPair<CoreCtx<'a, <BP as ContextProvider>::Context, Unlocked>, BP>;
pub struct CoreApi<'a, BP>(CoreApiCtxPair<'a, BP>)
where
BP: ContextProvider,
BP::Context: BindingsTypes;
impl<'a, BP> CoreApi<'a, BP>
where
BP: ContextProvider,
BP::Context: BindingsTypes,
{
pub(crate) fn new(ctx_pair: CoreApiCtxPair<'a, BP>) -> Self {
Self(ctx_pair)
}
pub fn udp<I: Ip>(self) -> UdpApi<I, CoreApiCtxPair<'a, BP>> {
let Self(ctx) = self;
UdpApi::new(ctx)
}
pub fn icmp_echo<I: Ip>(self) -> IcmpEchoSocketApi<I, CoreApiCtxPair<'a, BP>> {
let Self(ctx) = self;
IcmpEchoSocketApi::new(ctx)
}
pub fn tcp<I: Ip>(self) -> TcpApi<I, CoreApiCtxPair<'a, BP>> {
let Self(ctx) = self;
TcpApi::new(ctx)
}
pub fn raw_ip_socket<I: Ip>(self) -> RawIpSocketApi<I, CoreApiCtxPair<'a, BP>> {
let Self(ctx) = self;
RawIpSocketApi::new(ctx)
}
pub fn device_socket(self) -> DeviceSocketApi<CoreApiCtxPair<'a, BP>> {
let Self(ctx) = self;
DeviceSocketApi::new(ctx)
}
pub fn filter(self) -> FilterApi<CoreApiCtxPair<'a, BP>> {
let Self(ctx) = self;
FilterApi::new(ctx)
}
pub fn routes<I: Ip>(self) -> RoutesApi<I, CoreApiCtxPair<'a, BP>> {
let Self(ctx) = self;
RoutesApi::new(ctx)
}
pub fn routes_any(self) -> RoutesAnyApi<CoreApiCtxPair<'a, BP>> {
let Self(ctx) = self;
RoutesAnyApi::new(ctx)
}
pub fn multicast_forwarding<I: Ip>(self) -> MulticastForwardingApi<I, CoreApiCtxPair<'a, BP>> {
let Self(ctx) = self;
MulticastForwardingApi::new(ctx)
}
pub fn neighbor<I: Ip, D>(self) -> NeighborApi<I, D, CoreApiCtxPair<'a, BP>> {
let Self(ctx) = self;
NeighborApi::new(ctx)
}
pub fn device<D>(self) -> DeviceApi<D, CoreApiCtxPair<'a, BP>> {
let Self(ctx) = self;
DeviceApi::new(ctx)
}
pub fn device_any(self) -> DeviceAnyApi<CoreApiCtxPair<'a, BP>> {
let Self(ctx) = self;
DeviceAnyApi::new(ctx)
}
pub fn device_ip<I: Ip>(self) -> DeviceIpApi<I, CoreApiCtxPair<'a, BP>> {
let Self(ctx) = self;
DeviceIpApi::new(ctx)
}
pub fn device_ip_any(self) -> DeviceIpAnyApi<CoreApiCtxPair<'a, BP>> {
let Self(ctx) = self;
DeviceIpAnyApi::new(ctx)
}
pub fn transmit_queue<D>(self) -> TransmitQueueApi<D, CoreApiCtxPair<'a, BP>> {
let Self(ctx) = self;
TransmitQueueApi::new(ctx)
}
pub fn receive_queue<D>(self) -> ReceiveQueueApi<D, CoreApiCtxPair<'a, BP>> {
let Self(ctx) = self;
ReceiveQueueApi::new(ctx)
}
pub fn counters(self) -> CountersApi<CoreApiCtxPair<'a, BP>> {
let Self(ctx) = self;
CountersApi::new(ctx)
}
pub fn handle_timer(
&mut self,
dispatch: TimerId<BP::Context>,
timer: <BP::Context as TimerBindingsTypes>::UniqueTimerId,
) where
BP::Context: crate::BindingsContext,
{
let Self(ctx) = self;
let (core_ctx, bindings_ctx) = ctx.contexts();
core_ctx.handle_timer(bindings_ctx, dispatch, timer)
}
}