kube32-sdk 0.1.0

no_std Rust bindings for the kube32 WebAssembly pod ABI
Documentation
//! Raw host imports, one per declaration in `include/runtime/k32_app_abi.h`.
//!
//! Signatures mirror the `m3_LinkRawFunction` calls in
//! `src/runtime/app_pod.cpp` (`k32_*`), `src/runtime/pod_linalg.cpp`
//! (`k32_matvec_*`, `k32_matmul_*`, `k32_vecmat_f32_i8`, `k32_softmax_f32`, the element-wise
//! `k32_rmsnorm_f32`, `k32_quantize_i8`, `k32_rope_f32`, `k32_swiglu_f32`, `k32_add_f32`,
//! `k32_argmax_f32`, `k32_attention_f32_i8` and `k32_attention_full_f32_i8`, the convolution `k32_conv3x3_i8`,
//! `k32_add_f32_i8` and `k32_quantize_i8_static`, raw here only) and `src/runtime/host_functions.cpp`
//! (`esp32_delay_ms`, `esp32_rmt_*`, `kube32_channel_*`): pointers and
//! lengths are `i32`/`u32` in wasm, `k32_time_ms` returns `i64`. The GPIO,
//! ADC, I2C and UART imports of host_functions.cpp are not wrapped by this
//! crate.
//! Everything here is `unsafe`; the safe wrappers live in the other modules.
#![allow(clippy::missing_safety_doc)]

#[cfg(target_arch = "wasm32")]
#[link(wasm_import_module = "env")]
extern "C" {
    // Response streaming
    pub fn k32_resp_begin(status: i32, content_type: *const u8, content_type_len: u32);
    pub fn k32_resp_header(name: *const u8, name_len: u32, value: *const u8, value_len: u32);
    pub fn k32_resp_write(data: *const u8, len: u32) -> i32;
    pub fn k32_resp_end();

    // Cluster events (needs the CHANNEL_IPC capability)
    pub fn k32_event_emit(topic: *const u8, topic_len: u32, data: *const u8, len: u32) -> i32;

    // Node services
    pub fn k32_time_ms() -> i64;
    pub fn k32_unix_time_ms() -> i64;
    pub fn k32_log(msg: *const u8, len: u32);
    pub fn k32_random() -> u32;
    pub fn k32_env_get(key: *const u8, key_len: u32, out: *mut u8, out_cap: u32) -> i32;
    pub fn k32_mem_stats(out_used: *mut u32, out_limit: *mut u32) -> i32;

    // Hardware, gated by the capability firewall
    pub fn k32_display_text(x: i32, y: i32, text: *const u8, len: u32, fg565: u32, bg565: u32) -> i32;
    pub fn k32_display_rect(x: i32, y: i32, w: i32, h: i32, color565: u32) -> i32;
    pub fn k32_display_bar(x: i32, y: i32, w: i32, h: i32, pct: u32, fg565: u32, bg565: u32) -> i32;
    pub fn k32_key_read() -> i32;
    pub fn k32_tone(freq_hz: u32, duration_ms: u32, volume_pct: u32) -> i32;

    // Pod storage on the microSD card, gated by hardware.kube32.io/sd
    pub fn k32_fs_open(path: *const u8, path_len: u32, flags: u32) -> i32;
    pub fn k32_fs_read(fd: i32, buf: *mut u8, len: u32) -> i32;
    pub fn k32_fs_write(fd: i32, buf: *const u8, len: u32) -> i32;
    pub fn k32_fs_close(fd: i32) -> i32;
    pub fn k32_fs_size(fd: i32) -> i32;
    pub fn k32_fs_list(dir: *const u8, dir_len: u32, out: *mut u8, out_cap: u32) -> i32;
    pub fn k32_fs_remove(path: *const u8, path_len: u32) -> i32;

    // Infrared (RMT pulse trains), gated by esp32.io/rmt-tx and esp32.io/rmt-rx
    pub fn esp32_rmt_send(durations_us: *const u16, count: u32) -> i32;
    pub fn esp32_rmt_receive(out_us: *mut u16, cap: u32) -> i32;

    // The chip's temperature sensor, gated by hardware.kube32.io/chip-temp
    pub fn esp32_chip_temp_read(out_milli_c: *mut i32) -> i32;

    // Linear algebra for quantized neural networks, computed natively
    pub fn k32_matvec_i8(out: *mut f32, w: *const i8, w_scale: *const f32, x: *const i8, x_scale: f32, rows: u32, cols: u32, row_stride: u32) -> i32;
    pub fn k32_matvec_i4(out: *mut f32, w: *const u8, w_scale: *const f32, x: *const i8, x_scale: f32, rows: u32, cols: u32, row_stride: u32) -> i32;
    pub fn k32_matmul_i8(
        out: *mut f32,
        w: *const i8,
        w_scale: *const f32,
        x: *const i8,
        x_scale: *const f32,
        rows: u32,
        cols: u32,
        row_stride: u32,
        n: u32,
    ) -> i32;
    pub fn k32_matmul_i4(
        out: *mut f32,
        w: *const u8,
        w_scale: *const f32,
        x: *const i8,
        x_scale: *const f32,
        rows: u32,
        cols: u32,
        row_stride: u32,
        n: u32,
    ) -> i32;
    pub fn k32_matvec_f32_i8(
        out: *mut f32,
        w: *const i8,
        w_scale: *const f32,
        x: *const f32,
        alpha: f32,
        rows: u32,
        cols: u32,
        row_stride: u32,
        scale_stride: u32,
    ) -> i32;
    pub fn k32_vecmat_f32_i8(out: *mut f32, w: *const i8, w_scale: *const f32, x: *const f32, rows: u32, cols: u32, row_stride: u32, scale_stride: u32) -> i32;
    pub fn k32_softmax_f32(x: *mut f32, n: u32) -> i32;
    pub fn k32_rmsnorm_f32(out: *mut f32, x: *const f32, w: *const f32, n: u32, count: u32, eps: f32) -> i32;
    pub fn k32_quantize_i8(q: *mut i8, scale: *mut f32, x: *const f32, n: u32, count: u32) -> i32;
    pub fn k32_rope_f32(x: *mut f32, cs: *const f32, n: u32, head_size: u32, count: u32) -> i32;
    pub fn k32_swiglu_f32(x: *mut f32, g: *const f32, n: u32) -> i32;
    pub fn k32_add_f32(x: *mut f32, y: *const f32, n: u32) -> i32;
    pub fn k32_argmax_f32(x: *const f32, n: u32) -> i32;
    pub fn k32_add_f32_i8(x: *mut f32, q: *const i8, scale: f32, n: u32) -> i32;
    pub fn k32_attention_f32_i8(out: *mut f32, kq: *const i8, ks: *const f32, vq: *const i8, vs: *const f32, q: *const f32, scale: f32, heads: u32, kvh: u32, hs: u32, kv_stride: u32, pos0: u32, n: u32, att: *mut f32) -> i32;
    pub fn k32_attention_full_f32_i8(out: *mut f32, kq: *const i8, ks: *const f32, vq: *const i8, vs: *const f32, q: *const f32, scale: f32, heads: u32, kvh: u32, hs: u32, kv_stride: u32, rows: u32, n: u32, att: *mut f32) -> i32;
    pub fn k32_quantize_i8_static(q: *mut i8, x: *const f32, inv: f32, n: u32) -> i32;
    pub fn k32_conv3x3_i8(out: *mut f32, w: *const i8, w_scale: *const f32, bias: *const f32, input: *const i8, in_scale: f32, cin: u32, cout: u32, h: u32, w_: u32, stride: u32, up: u32, n: u32, y0: u32, y1: u32) -> i32;

    // Low-level imports from host_functions.cpp that app pods use
    pub fn esp32_delay_ms(ms: u32);
    /// `name` must be NUL-terminated: the host reads it with strnlen.
    pub fn kube32_channel_open(name: *const u8, flags: u32) -> i32;
    pub fn kube32_channel_send(handle: i32, data: *const u8, len: u32) -> i32;
    pub fn kube32_channel_recv(handle: i32, buf: *mut u8, max_len: u32, timeout_ms: u32) -> i32;
    pub fn kube32_channel_close(handle: i32) -> i32;
}

// On any other target (host unit tests of the parsers) the imports do not
// exist. These stubs keep the crate compiling; calling one is a bug.
#[cfg(not(target_arch = "wasm32"))]
mod stubs {
    #![allow(unused_variables)]
    fn no_host() -> ! {
        panic!("kube32-sdk: host import called outside the Kube32 runtime")
    }
    pub unsafe fn k32_resp_begin(status: i32, content_type: *const u8, content_type_len: u32) { no_host() }
    pub unsafe fn k32_resp_header(name: *const u8, name_len: u32, value: *const u8, value_len: u32) { no_host() }
    pub unsafe fn k32_resp_write(data: *const u8, len: u32) -> i32 { no_host() }
    pub unsafe fn k32_resp_end() { no_host() }
    pub unsafe fn k32_event_emit(topic: *const u8, topic_len: u32, data: *const u8, len: u32) -> i32 { no_host() }
    pub unsafe fn k32_time_ms() -> i64 { no_host() }
    pub unsafe fn k32_unix_time_ms() -> i64 { no_host() }
    pub unsafe fn k32_log(msg: *const u8, len: u32) { no_host() }
    pub unsafe fn k32_random() -> u32 { no_host() }
    pub unsafe fn k32_env_get(key: *const u8, key_len: u32, out: *mut u8, out_cap: u32) -> i32 { no_host() }
    pub unsafe fn k32_mem_stats(out_used: *mut u32, out_limit: *mut u32) -> i32 { no_host() }
    pub unsafe fn k32_display_text(x: i32, y: i32, text: *const u8, len: u32, fg565: u32, bg565: u32) -> i32 { no_host() }
    pub unsafe fn k32_display_rect(x: i32, y: i32, w: i32, h: i32, color565: u32) -> i32 { no_host() }
    pub unsafe fn k32_display_bar(x: i32, y: i32, w: i32, h: i32, pct: u32, fg565: u32, bg565: u32) -> i32 { no_host() }
    pub unsafe fn k32_key_read() -> i32 { no_host() }
    pub unsafe fn k32_tone(freq_hz: u32, duration_ms: u32, volume_pct: u32) -> i32 { no_host() }
    pub unsafe fn k32_fs_open(path: *const u8, path_len: u32, flags: u32) -> i32 { no_host() }
    pub unsafe fn k32_fs_read(fd: i32, buf: *mut u8, len: u32) -> i32 { no_host() }
    pub unsafe fn k32_fs_write(fd: i32, buf: *const u8, len: u32) -> i32 { no_host() }
    pub unsafe fn k32_fs_close(fd: i32) -> i32 { no_host() }
    pub unsafe fn k32_fs_size(fd: i32) -> i32 { no_host() }
    pub unsafe fn k32_fs_list(dir: *const u8, dir_len: u32, out: *mut u8, out_cap: u32) -> i32 { no_host() }
    pub unsafe fn k32_fs_remove(path: *const u8, path_len: u32) -> i32 { no_host() }
    pub unsafe fn esp32_rmt_send(durations_us: *const u16, count: u32) -> i32 { no_host() }
    pub unsafe fn esp32_rmt_receive(out_us: *mut u16, cap: u32) -> i32 { no_host() }
    pub unsafe fn esp32_chip_temp_read(out_milli_c: *mut i32) -> i32 { no_host() }
    pub unsafe fn k32_matvec_i8(out: *mut f32, w: *const i8, w_scale: *const f32, x: *const i8, x_scale: f32, rows: u32, cols: u32, row_stride: u32) -> i32 { no_host() }
    pub unsafe fn k32_matvec_i4(out: *mut f32, w: *const u8, w_scale: *const f32, x: *const i8, x_scale: f32, rows: u32, cols: u32, row_stride: u32) -> i32 { no_host() }
    #[allow(clippy::too_many_arguments)]
    pub unsafe fn k32_matmul_i8(out: *mut f32, w: *const i8, w_scale: *const f32, x: *const i8, x_scale: *const f32, rows: u32, cols: u32, row_stride: u32, n: u32) -> i32 { no_host() }
    #[allow(clippy::too_many_arguments)]
    pub unsafe fn k32_matmul_i4(out: *mut f32, w: *const u8, w_scale: *const f32, x: *const i8, x_scale: *const f32, rows: u32, cols: u32, row_stride: u32, n: u32) -> i32 { no_host() }
    #[allow(clippy::too_many_arguments)]
    pub unsafe fn k32_matvec_f32_i8(out: *mut f32, w: *const i8, w_scale: *const f32, x: *const f32, alpha: f32, rows: u32, cols: u32, row_stride: u32, scale_stride: u32) -> i32 { no_host() }
    pub unsafe fn k32_vecmat_f32_i8(out: *mut f32, w: *const i8, w_scale: *const f32, x: *const f32, rows: u32, cols: u32, row_stride: u32, scale_stride: u32) -> i32 { no_host() }
    pub unsafe fn k32_softmax_f32(x: *mut f32, n: u32) -> i32 { no_host() }
    pub unsafe fn k32_rmsnorm_f32(out: *mut f32, x: *const f32, w: *const f32, n: u32, count: u32, eps: f32) -> i32 { no_host() }
    pub unsafe fn k32_quantize_i8(q: *mut i8, scale: *mut f32, x: *const f32, n: u32, count: u32) -> i32 { no_host() }
    pub unsafe fn k32_rope_f32(x: *mut f32, cs: *const f32, n: u32, head_size: u32, count: u32) -> i32 { no_host() }
    pub unsafe fn k32_swiglu_f32(x: *mut f32, g: *const f32, n: u32) -> i32 { no_host() }
    pub unsafe fn k32_add_f32(x: *mut f32, y: *const f32, n: u32) -> i32 { no_host() }
    pub unsafe fn k32_argmax_f32(x: *const f32, n: u32) -> i32 { no_host() }
    pub unsafe fn k32_add_f32_i8(x: *mut f32, q: *const i8, scale: f32, n: u32) -> i32 { no_host() }
    pub unsafe fn k32_attention_f32_i8(out: *mut f32, kq: *const i8, ks: *const f32, vq: *const i8, vs: *const f32, q: *const f32, scale: f32, heads: u32, kvh: u32, hs: u32, kv_stride: u32, pos0: u32, n: u32, att: *mut f32) -> i32 { no_host() }
    pub unsafe fn k32_attention_full_f32_i8(out: *mut f32, kq: *const i8, ks: *const f32, vq: *const i8, vs: *const f32, q: *const f32, scale: f32, heads: u32, kvh: u32, hs: u32, kv_stride: u32, rows: u32, n: u32, att: *mut f32) -> i32 { no_host() }
    pub unsafe fn k32_quantize_i8_static(q: *mut i8, x: *const f32, inv: f32, n: u32) -> i32 { no_host() }
    pub unsafe fn k32_conv3x3_i8(out: *mut f32, w: *const i8, w_scale: *const f32, bias: *const f32, input: *const i8, in_scale: f32, cin: u32, cout: u32, h: u32, w_: u32, stride: u32, up: u32, n: u32, y0: u32, y1: u32) -> i32 { no_host() }
    pub unsafe fn esp32_delay_ms(ms: u32) { no_host() }
    pub unsafe fn kube32_channel_open(name: *const u8, flags: u32) -> i32 { no_host() }
    pub unsafe fn kube32_channel_send(handle: i32, data: *const u8, len: u32) -> i32 { no_host() }
    pub unsafe fn kube32_channel_recv(handle: i32, buf: *mut u8, max_len: u32, timeout_ms: u32) -> i32 { no_host() }
    pub unsafe fn kube32_channel_close(handle: i32) -> i32 { no_host() }
}
#[cfg(not(target_arch = "wasm32"))]
pub use stubs::*;