use capsules_core::virtualizers::virtual_flash::FlashUser;
use capsules_core::virtualizers::virtual_flash::MuxFlash;
use capsules_extra::tickv::TicKVSystem;
use core::mem::MaybeUninit;
use kernel::capabilities;
use kernel::component::Component;
use kernel::create_capability;
use kernel::hil;
use kernel::hil::flash::HasClient;
use kernel::hil::hasher::Hasher;
#[macro_export]
macro_rules! tickv_component_static {
($F:ty, $H:ty, $PAGE_SIZE:expr $(,)?) => {{
let flash =
kernel::static_buf!(capsules_core::virtualizers::virtual_flash::FlashUser<'static, $F>);
let tickv = kernel::static_buf!(
capsules_extra::tickv::TicKVSystem<
'static,
capsules_core::virtualizers::virtual_flash::FlashUser<'static, $F>,
$H,
$PAGE_SIZE,
>
);
(flash, tickv)
};};
}
#[macro_export]
macro_rules! tickv_dedicated_flash_component_static {
($F:ty, $H:ty, $PAGE_SIZE:expr $(,)?) => {{
let tickfs_read_buffer = kernel::static_buf!([u8; $PAGE_SIZE]);
let tickv =
kernel::static_buf!(capsules_extra::tickv::TicKVSystem<'static, $F, $H, $PAGE_SIZE>);
(tickv, tickfs_read_buffer)
};};
}
pub struct TicKVComponent<
F: 'static + hil::flash::Flash + hil::flash::HasClient<'static, MuxFlash<'static, F>>,
H: 'static + Hasher<'static, 8>,
const PAGE_SIZE: usize,
> {
mux_flash: &'static MuxFlash<'static, F>,
hasher: &'static H,
region_offset: usize,
flash_size: usize,
tickfs_read_buf: &'static mut [u8; PAGE_SIZE],
flash_read_buffer: &'static mut F::Page,
}
impl<
F: 'static + hil::flash::Flash + hil::flash::HasClient<'static, MuxFlash<'static, F>>,
H: Hasher<'static, 8>,
const PAGE_SIZE: usize,
> TicKVComponent<F, H, PAGE_SIZE>
{
pub fn new(
hasher: &'static H,
mux_flash: &'static MuxFlash<'static, F>,
region_offset: usize,
flash_size: usize,
tickfs_read_buf: &'static mut [u8; PAGE_SIZE],
flash_read_buffer: &'static mut F::Page,
) -> Self {
Self {
mux_flash,
hasher,
region_offset,
flash_size,
tickfs_read_buf,
flash_read_buffer,
}
}
}
impl<
F: 'static + hil::flash::Flash + hil::flash::HasClient<'static, MuxFlash<'static, F>>,
H: 'static + Hasher<'static, 8>,
const PAGE_SIZE: usize,
> Component for TicKVComponent<F, H, PAGE_SIZE>
{
type StaticInput = (
&'static mut MaybeUninit<FlashUser<'static, F>>,
&'static mut MaybeUninit<TicKVSystem<'static, FlashUser<'static, F>, H, PAGE_SIZE>>,
);
type Output = &'static TicKVSystem<'static, FlashUser<'static, F>, H, PAGE_SIZE>;
fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
let _grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
let virtual_flash = static_buffer.0.write(FlashUser::new(self.mux_flash));
let driver = static_buffer.1.write(TicKVSystem::new(
virtual_flash,
self.hasher,
self.tickfs_read_buf,
self.flash_read_buffer,
self.region_offset,
self.flash_size,
));
virtual_flash.set_client(driver);
driver.initialise();
driver
}
}
pub type TicKVDedicatedFlashComponentType<F, H, const PAGE: usize> =
capsules_extra::tickv::TicKVSystem<'static, F, H, PAGE>;
pub struct TicKVDedicatedFlashComponent<
F: 'static
+ hil::flash::Flash
+ hil::flash::HasClient<'static, TicKVSystem<'static, F, H, PAGE_SIZE>>,
H: 'static + Hasher<'static, 8>,
const PAGE_SIZE: usize,
> {
flash: &'static F,
hasher: &'static H,
region_offset: usize,
flash_size: usize,
flash_read_buffer: &'static mut F::Page,
}
impl<
F: 'static
+ hil::flash::Flash
+ hil::flash::HasClient<'static, TicKVSystem<'static, F, H, PAGE_SIZE>>,
H: Hasher<'static, 8>,
const PAGE_SIZE: usize,
> TicKVDedicatedFlashComponent<F, H, PAGE_SIZE>
{
pub fn new(
hasher: &'static H,
flash: &'static F,
region_offset: usize,
flash_size: usize,
flash_read_buffer: &'static mut F::Page,
) -> Self {
Self {
flash,
hasher,
region_offset,
flash_size,
flash_read_buffer,
}
}
}
impl<
F: 'static
+ hil::flash::Flash
+ hil::flash::HasClient<'static, TicKVSystem<'static, F, H, PAGE_SIZE>>,
H: 'static + Hasher<'static, 8>,
const PAGE_SIZE: usize,
> Component for TicKVDedicatedFlashComponent<F, H, PAGE_SIZE>
{
type StaticInput = (
&'static mut MaybeUninit<TicKVSystem<'static, F, H, PAGE_SIZE>>,
&'static mut MaybeUninit<[u8; PAGE_SIZE]>,
);
type Output = &'static TicKVSystem<'static, F, H, PAGE_SIZE>;
fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
let _grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
let tickfs_read_buf = static_buffer.1.write([0; PAGE_SIZE]);
let tickv = static_buffer.0.write(TicKVSystem::new(
self.flash,
self.hasher,
tickfs_read_buf,
self.flash_read_buffer,
self.region_offset,
self.flash_size,
));
self.flash.set_client(tickv);
self.hasher.set_client(tickv);
tickv.initialise();
tickv
}
}