Skip to content

Commit

Permalink
add more system properties
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesMc86 committed Dec 4, 2024
1 parent d4337e9 commit 9ce1a73
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 12 deletions.
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# will have compiled files and executables
/target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

.idea
.vscode

116 changes: 116 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ni-syscfg/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use thiserror::Error;

#[derive(Error, Debug)]
pub enum NiSystemConfigurationError {
#[error("API Error")]
#[error("API Error: {0:?}")]
ApiError(NiSysCfgApiStatus),
#[error("String Conversion Error From The API")]
#[error("String Conversion Error From The API: {0}")]
IntoStringError(#[from] std::ffi::IntoStringError),
#[error("Null in String from API")]
NulStringError(#[from] std::ffi::NulError),
Expand Down
2 changes: 1 addition & 1 deletion ni-syscfg/src/hardware_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl HardwareFilter {
pub fn new(session: &Session) -> Result<Self> {
let mut handle = null_mut();
unsafe {
api_status(NISysCfgCreateFilter(*session.handle(), &mut handle))?;
api_status(NISysCfgCreateFilter(session.handle(), &mut handle))?;
}
Ok(Self {
handle,
Expand Down
2 changes: 2 additions & 0 deletions ni-syscfg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ mod resources;
mod session;
pub mod software;
pub(crate) mod types;
mod system;

pub use experts::ExpertType;
pub use hardware_filter::{FilterMode, HardwareFilter};
pub use session::*;
pub use system::RealTimeSession;
81 changes: 81 additions & 0 deletions ni-syscfg/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ pub trait ReadableParameter: Sized {
id: NISysCfgIndexedProperty,
index: u32,
) -> Result<Self>;
fn read_system_parameter(
handle: NISysCfgSessionHandle,
id: NISysCfgSystemProperty
) -> Result<Self>;
}

impl ReadableParameter for i32 {
Expand Down Expand Up @@ -64,6 +68,17 @@ impl ReadableParameter for i32 {
};
Ok(value)
}

fn read_system_parameter(
handle: NISysCfgSessionHandle,
id: NISysCfgSystemProperty
) -> Result<i32> {
let mut value = 0i32;
let status = unsafe {
api_status(NISysCfgGetSystemProperty(handle, id, &mut value as *mut _ as *mut c_void))?
};
Ok(value)
}
}

impl ReadableParameter for String {
Expand Down Expand Up @@ -102,6 +117,16 @@ impl ReadableParameter for String {
};
Ok(value.into_string()?)
}

fn read_system_parameter(handle: NISysCfgSessionHandle, id: NISysCfgSystemProperty) -> Result<Self> {
let mut value = new_simple_string();
let value_ptr = value.into_raw();
let status = unsafe {
api_status(NISysCfgGetSystemProperty(handle, id, value_ptr as *mut c_void))?;
value = CString::from_raw(value_ptr);
};
Ok(value.into_string()?)
}
}

/// Marker trait for enums to be used as property values.
Expand Down Expand Up @@ -150,6 +175,22 @@ where
Err(NiSystemConfigurationError::UnexpectedEnumValue(value))
}
}
fn read_system_parameter(handle: NISysCfgSessionHandle, id: NISysCfgSystemProperty) -> Result<Self> {
let mut value = 0i32;
let status = unsafe {
api_status(NISysCfgGetSystemProperty(
handle,
id,
&mut value as *mut _ as *mut c_void,
))
};
if let Some(inner) = T::from_i32(value) {
Ok(inner)
} else {
Err(NiSystemConfigurationError::UnexpectedEnumValue(value))
}

}
}

#[repr(i32)]
Expand Down Expand Up @@ -183,6 +224,37 @@ pub enum ApiBool {
True = NISysCfgBool_NISysCfgBoolTrue,
}

impl FromPrimitive for ApiBool {
fn from_i32(n: i32) -> Option<Self> {
if n == 0 {
Some(Self::False)
}
else {
Some(Self::True)
}
}

fn from_i64(n: i64) -> Option<Self> {
if n == 0 {
Some(Self::False)
}
else {
Some(Self::True)
}
}

fn from_u64(n: u64) -> Option<Self> {
if n == 0 {
Some(Self::False)
}
else {
Some(Self::True)
}
}
}

impl ValueEnum for ApiBool {}

impl From<bool> for ApiBool {
fn from(input: bool) -> Self {
if input {
Expand All @@ -192,3 +264,12 @@ impl From<bool> for ApiBool {
}
}
}

impl From<ApiBool> for bool {
fn from(input: ApiBool) -> Self {
match input {
ApiBool::False => false,
ApiBool::True => true,
}
}
}
2 changes: 1 addition & 1 deletion ni-syscfg/src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl<'a> Iterator for HardwareResourceList<'a> {
unsafe {
let mut resource_handle = std::ptr::null_mut();
let result = api_status(NISysCfgNextResource(
*self.session.handle(),
self.session.handle(),
self.handle,
&mut resource_handle,
));
Expand Down
4 changes: 2 additions & 2 deletions ni-syscfg/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ impl Session {
Self { handle }
}

pub(crate) fn handle(&self) -> &NISysCfgSessionHandle {
&self.handle
pub(crate) fn handle(&self) -> NISysCfgSessionHandle {
self.handle
}

/// Create a new filter for the session to use as part of [find_hardware]
Expand Down
4 changes: 2 additions & 2 deletions ni-syscfg/src/software.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl Session {

unsafe {
api_status(NISysCfgCreateSystemImageAsFolder(
*handle,
handle,
title.as_ptr(),
id.as_ptr(),
version.as_ptr(),
Expand Down Expand Up @@ -145,7 +145,7 @@ impl Session {

unsafe {
api_status(NISysCfgSetSystemImageFromFolder2(
*handle,
handle,
FfiBoolean::from(auto_restart) as i32,
path.as_ptr(),
password_ptr,
Expand Down
34 changes: 34 additions & 0 deletions ni-syscfg/src/system/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//! Implements system parameters on the session.
mod real_time;

use ni_syscfg_sys::{NISysCfgSystemProperty, NISysCfgSystemProperty_NISysCfgSystemPropertyHostname, NISysCfgSystemProperty_NISysCfgSystemPropertyIsLocked, NISysCfgSystemProperty_NISysCfgSystemPropertyIsLockingSupported, NISysCfgSystemProperty_NISysCfgSystemPropertyProductName, NISysCfgSystemProperty_NISysCfgSystemPropertySerialNumber};
use crate::Session;
use crate::parameters::{ApiBool, ReadableParameter};
use crate::error::Result;
pub use real_time::RealTimeSession;

impl Session {

pub fn locked(&self) -> Result<bool> {
ApiBool::read_system_parameter(self.handle(), NISysCfgSystemProperty_NISysCfgSystemPropertyIsLocked).map(|a| a.into())
}

pub fn locking_supported(&self) -> Result<bool> {
ApiBool::read_system_parameter(self.handle(), NISysCfgSystemProperty_NISysCfgSystemPropertyIsLockingSupported).map(|a| a.into())
}

pub fn hostname(&self) -> Result<String> {
String::read_system_parameter(self.handle(), NISysCfgSystemProperty_NISysCfgSystemPropertyHostname)
}

pub fn serial_number(&self) -> Result<String> {
String::read_system_parameter(self.handle(), NISysCfgSystemProperty_NISysCfgSystemPropertySerialNumber)
}

pub fn product_name(&self) -> Result<String> {
String::read_system_parameter(self.handle(), NISysCfgSystemProperty_NISysCfgSystemPropertyProductName)
}


}
25 changes: 25 additions & 0 deletions ni-syscfg/src/system/real_time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//! System interface specific to real time systems.
use ni_syscfg_sys::{NISysCfgSessionHandle, NISysCfgSystemProperty_NISysCfgSystemPropertySystemState};
use crate::Session;
use crate::error::Result;
use crate::parameters::ReadableParameter;

pub struct RealTimeSession {
handle: NISysCfgSessionHandle
}


impl RealTimeSession {
pub fn from_session(session: &Session) -> RealTimeSession {
Self {
handle: session.handle()
}
}

pub fn status(&self) -> Result<String> {
String::read_system_parameter(self.handle, NISysCfgSystemProperty_NISysCfgSystemPropertySystemState)
}


}

0 comments on commit 9ce1a73

Please sign in to comment.