use std::f32;
use std::ffi::CString;
use std::ptr;
use std::u32;
use crate::state::State;
use crate::technology::Technology;
use crate::Battery;
use battery::units::electric_potential::volt;
use battery::units::energy::joule;
use battery::units::power::watt;
use battery::units::ratio::percent;
use battery::units::thermodynamic_temperature::kelvin;
use battery::units::time::second;
#[no_mangle]
pub unsafe extern "C" fn battery_get_state_of_charge(ptr: *const Battery) -> libc::c_float {
assert!(!ptr.is_null());
let battery = &*ptr;
battery.state_of_charge().get::<percent>()
}
#[no_mangle]
pub unsafe extern "C" fn battery_get_energy(ptr: *const Battery) -> libc::c_float {
assert!(!ptr.is_null());
let battery = &*ptr;
battery.energy().get::<joule>()
}
#[no_mangle]
pub unsafe extern "C" fn battery_get_energy_full(ptr: *const Battery) -> libc::c_float {
assert!(!ptr.is_null());
let battery = &*ptr;
battery.energy_full().get::<joule>()
}
#[no_mangle]
pub unsafe extern "C" fn battery_get_energy_full_design(ptr: *const Battery) -> libc::c_float {
assert!(!ptr.is_null());
let battery = &*ptr;
battery.energy_full_design().get::<joule>()
}
#[no_mangle]
pub unsafe extern "C" fn battery_get_energy_rate(ptr: *const Battery) -> libc::c_float {
assert!(!ptr.is_null());
let battery = &*ptr;
battery.energy_rate().get::<watt>()
}
#[no_mangle]
pub unsafe extern "C" fn battery_get_voltage(ptr: *const Battery) -> libc::c_float {
assert!(!ptr.is_null());
let battery = &*ptr;
battery.voltage().get::<volt>()
}
#[no_mangle]
pub unsafe extern "C" fn battery_get_state_of_health(ptr: *const Battery) -> libc::c_float {
assert!(!ptr.is_null());
let battery = &*ptr;
battery.state_of_health().get::<percent>()
}
#[no_mangle]
pub unsafe extern "C" fn battery_get_state(ptr: *const Battery) -> State {
assert!(!ptr.is_null());
let battery = &*ptr;
battery.state().into()
}
#[no_mangle]
pub unsafe extern "C" fn battery_get_technology(ptr: *const Battery) -> Technology {
assert!(!ptr.is_null());
let battery = &*ptr;
battery.technology().into()
}
#[no_mangle]
pub unsafe extern "C" fn battery_get_temperature(ptr: *const Battery) -> libc::c_float {
assert!(!ptr.is_null());
let battery = &*ptr;
match battery.temperature() {
None => f32::NAN,
Some(temp) => temp.get::<kelvin>(),
}
}
#[no_mangle]
pub unsafe extern "C" fn battery_get_cycle_count(ptr: *const Battery) -> u32 {
assert!(!ptr.is_null());
let battery = &*ptr;
match battery.cycle_count() {
None => u32::MAX,
Some(value) => value,
}
}
#[no_mangle]
pub unsafe extern "C" fn battery_get_vendor(ptr: *const Battery) -> *mut libc::c_char {
assert!(!ptr.is_null());
let battery = &*ptr;
match battery.vendor() {
Some(vendor) => {
let c_str = CString::new(vendor).unwrap();
c_str.into_raw()
}
None => ptr::null_mut(),
}
}
#[no_mangle]
pub unsafe extern "C" fn battery_get_model(ptr: *const Battery) -> *mut libc::c_char {
assert!(!ptr.is_null());
let battery = &*ptr;
match battery.model() {
Some(model) => {
let c_str = CString::new(model).unwrap();
c_str.into_raw()
}
None => ptr::null_mut(),
}
}
#[no_mangle]
pub unsafe extern "C" fn battery_get_serial_number(ptr: *const Battery) -> *mut libc::c_char {
assert!(!ptr.is_null());
let battery = &*ptr;
match battery.serial_number() {
Some(sn) => {
let c_str = CString::new(sn).unwrap();
c_str.into_raw()
}
None => ptr::null_mut(),
}
}
#[no_mangle]
pub unsafe extern "C" fn battery_get_time_to_full(ptr: *const Battery) -> libc::c_float {
assert!(!ptr.is_null());
let battery = &*ptr;
match battery.time_to_full() {
None => f32::NAN,
Some(duration) => duration.get::<second>(),
}
}
#[no_mangle]
pub unsafe extern "C" fn battery_get_time_to_empty(ptr: *const Battery) -> libc::c_float {
assert!(!ptr.is_null());
let battery = &*ptr;
match battery.time_to_empty() {
None => f32::NAN,
Some(duration) => duration.get::<second>(),
}
}
#[no_mangle]
pub unsafe extern "C" fn battery_free(ptr: *mut Battery) {
if ptr.is_null() {
return;
}
Box::from_raw(ptr);
}
#[no_mangle]
pub unsafe extern "C" fn battery_str_free(ptr: *mut libc::c_char) {
if ptr.is_null() {
return;
}
CString::from_raw(ptr);
}