use core::fmt::Write;
use serde::Serialize;
#[cfg(feature = "alloc")]
pub mod ble;
#[cfg(all(feature = "alloc", feature = "db"))]
pub mod db;
mod errors;
#[cfg(feature = "cells")]
mod cell;
#[cfg(feature = "cells")]
mod in_memory;
#[cfg(all(feature = "cells", feature = "db"))]
pub mod gateway;
mod arguments;
pub mod gpio;
mod logging;
pub mod outlet;
#[cfg(feature = "alloc")]
mod runtime;
pub mod tap;
mod time;
pub use arguments::get_arguments;
#[cfg(feature = "cells")]
pub use cell::{
ClassHandle, ClassRef, CommandError, SpawnBuilder, SpawnError, SpawnRequest, TerminateError,
TimerHandle, delay, interval, interval_at, spawn_cell, stop_self, terminate_cell,
};
#[cfg(feature = "cells")]
pub use cell::publish_event;
#[cfg(feature = "cells")]
pub(crate) use cell::send_command;
#[cfg(feature = "cells")]
pub use in_memory::InMemory;
pub use errors::report_error;
pub use logging::{LogLevel, debug_str, error_str, info_str, log, log_buffer, trace_str, warn_str};
pub use outlet::Outlet;
#[cfg(feature = "alloc")]
pub use runtime::{runtime_id, runtime_tags};
pub use tap::{Tap, TapKind, list_entry, list_len};
pub use time::{now, uptime, wait};
pub struct RawBuf {
ptr: *mut u8,
cap: usize,
len: usize,
}
impl RawBuf {
pub fn new(ptr: *mut u8, cap: usize) -> Self {
Self { ptr, cap, len: 0 }
}
}
#[allow(clippy::cast_sign_loss)]
impl Write for RawBuf {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
let bytes = s.as_bytes();
let remaining_cap = self.cap - self.len;
let copy = bytes.len().min(remaining_cap);
unsafe {
core::ptr::copy_nonoverlapping(bytes.as_ptr(), self.ptr.add(self.len), copy);
}
self.len += copy;
Ok(())
}
}
#[cfg(feature = "alloc")]
#[allow(dead_code)]
pub(crate) fn call<T>(
req: &T,
func: unsafe extern "C" fn(buffer: *const u8, length: core::ffi::c_int) -> core::ffi::c_int,
) -> Result<(), core::ffi::c_int>
where
T: Serialize + ?Sized,
{
let bytes = postcard::to_allocvec(&req).map_err(|_err| -120 as core::ffi::c_int)?;
let errno = unsafe { func(bytes.as_ptr(), bytes.len() as i32) };
match errno {
0 => Ok(()),
n => Err(n),
}
}