icsneoc2 0.1002002.0-rc.4

High-level Rust interface for Intrepid Control Systems vehicle network adapters
Documentation
//! CoreMini script management and status.
//!
//! Intrepid devices can run on-board "CoreMini" scripts that filter,
//! process, or generate network traffic autonomously. This module
//! provides [`ScriptStatus`], an owned handle to the current script
//! state returned by [`Device::script_status`](crate::Device::script_status).
//!
//! Scripts are started, stopped, uploaded, and cleared via the
//! corresponding methods on [`Device`](crate::Device).

use std::ptr::NonNull;

use crate::error::Result;
use crate::functions::check;
use crate::sys;

/// Owned handle to the CoreMini script status of a device.
///
/// Obtained via [`Device::script_status`](crate::Device::script_status).
/// Freed automatically on drop via `icsneoc2_script_status_free`.
pub struct ScriptStatus {
    ptr: NonNull<sys::icsneoc2_script_status_t>,
}

unsafe impl Send for ScriptStatus {}

impl Drop for ScriptStatus {
    fn drop(&mut self) {
        unsafe { sys::icsneoc2_script_status_free(self.ptr.as_ptr()) };
    }
}

impl ScriptStatus {
    pub(crate) fn new(ptr: NonNull<sys::icsneoc2_script_status_t>) -> Self {
        Self { ptr }
    }

    /// Returns `true` if a CoreMini script is currently running on the device.
    pub fn is_running(&self) -> Result<bool> {
        let mut value = false;
        check(unsafe {
            sys::icsneoc2_script_status_is_coremini_running(self.ptr.as_ptr(), &raw mut value)
        })?;
        Ok(value)
    }

    /// Returns `true` if the loaded CoreMini script is encrypted.
    pub fn is_encrypted(&self) -> Result<bool> {
        let mut value = false;
        check(unsafe {
            sys::icsneoc2_script_status_is_encrypted(self.ptr.as_ptr(), &raw mut value)
        })?;
        Ok(value)
    }

    /// Number of sector overflows during script execution.
    pub fn sector_overflows(&self) -> Result<u32> {
        let mut value: u32 = 0;
        check(unsafe {
            sys::icsneoc2_script_status_sector_overflows_get(self.ptr.as_ptr(), &raw mut value)
        })?;
        Ok(value)
    }

    /// Number of remaining sector buffers available.
    pub fn remaining_sector_buffers(&self) -> Result<u32> {
        let mut value: u32 = 0;
        check(unsafe {
            sys::icsneoc2_script_status_remaining_sector_buffers_get(
                self.ptr.as_ptr(),
                &raw mut value,
            )
        })?;
        Ok(value)
    }

    /// Last sector index written by the script.
    pub fn last_sector(&self) -> Result<u32> {
        let mut value: u32 = 0;
        check(unsafe {
            sys::icsneoc2_script_status_last_sector_get(self.ptr.as_ptr(), &raw mut value)
        })?;
        Ok(value)
    }

    /// Size of the script binary read from the device (in bytes).
    pub fn read_bin_size(&self) -> Result<u32> {
        let mut value: u32 = 0;
        check(unsafe {
            sys::icsneoc2_script_status_read_bin_size_get(self.ptr.as_ptr(), &raw mut value)
        })?;
        Ok(value)
    }

    /// Minimum sector index used by the script.
    pub fn min_sector(&self) -> Result<u32> {
        let mut value: u32 = 0;
        check(unsafe {
            sys::icsneoc2_script_status_min_sector_get(self.ptr.as_ptr(), &raw mut value)
        })?;
        Ok(value)
    }

    /// Maximum sector index used by the script.
    pub fn max_sector(&self) -> Result<u32> {
        let mut value: u32 = 0;
        check(unsafe {
            sys::icsneoc2_script_status_max_sector_get(self.ptr.as_ptr(), &raw mut value)
        })?;
        Ok(value)
    }

    /// Current sector index being processed.
    pub fn current_sector(&self) -> Result<u32> {
        let mut value: u32 = 0;
        check(unsafe {
            sys::icsneoc2_script_status_current_sector_get(self.ptr.as_ptr(), &raw mut value)
        })?;
        Ok(value)
    }

    /// Timestamp when the CoreMini binary was created (Unix epoch seconds).
    pub fn create_time(&self) -> Result<u64> {
        let mut value: u64 = 0;
        check(unsafe {
            sys::icsneoc2_script_status_coremini_create_time_get(self.ptr.as_ptr(), &raw mut value)
        })?;
        Ok(value)
    }

    /// CRC-16 checksum of the script binary.
    pub fn file_checksum(&self) -> Result<u16> {
        let mut value: u16 = 0;
        check(unsafe {
            sys::icsneoc2_script_status_file_checksum_get(self.ptr.as_ptr(), &raw mut value)
        })?;
        Ok(value)
    }

    /// Version of the CoreMini engine on the device.
    pub fn coremini_version(&self) -> Result<u16> {
        let mut value: u16 = 0;
        check(unsafe {
            sys::icsneoc2_script_status_coremini_version_get(self.ptr.as_ptr(), &raw mut value)
        })?;
        Ok(value)
    }

    /// Size of the CoreMini binary header (in bytes).
    pub fn header_size(&self) -> Result<u16> {
        let mut value: u16 = 0;
        check(unsafe {
            sys::icsneoc2_script_status_coremini_header_size_get(self.ptr.as_ptr(), &raw mut value)
        })?;
        Ok(value)
    }

    /// Current diagnostic error code (DTC) reported by the script.
    pub fn diagnostic_error_code(&self) -> Result<u8> {
        let mut value: u8 = 0;
        check(unsafe {
            sys::icsneoc2_script_status_diagnostic_error_code_get(self.ptr.as_ptr(), &raw mut value)
        })?;
        Ok(value)
    }

    /// Total number of diagnostic error codes reported.
    pub fn diagnostic_error_code_count(&self) -> Result<u8> {
        let mut value: u8 = 0;
        check(unsafe {
            sys::icsneoc2_script_status_diagnostic_error_code_count_get(
                self.ptr.as_ptr(),
                &raw mut value,
            )
        })?;
        Ok(value)
    }

    /// Maximum CoreMini script size the device supports (in KB).
    pub fn max_coremini_size_kb(&self) -> Result<u16> {
        let mut value: u16 = 0;
        check(unsafe {
            sys::icsneoc2_script_status_max_coremini_size_kb_get(self.ptr.as_ptr(), &raw mut value)
        })?;
        Ok(value)
    }
}