1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
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;

/// Returns battery state of charge as a percentage value from `0.0` to `100.0`.
///
/// # Panics
///
/// This function will panic if passed pointer is `NULL`
#[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>()
}

/// Returns battery energy (in `joule`).
///
/// # Panics
///
/// This function will panic if passed pointer is `NULL`
#[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>()
}

/// Returns battery energy (in `joule`) when it is considered full.
///
/// # Panics
///
/// This function will panic if passed pointer is `NULL`
#[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>()
}

/// Returns battery energy (in `joule`) designed to hold when it is considered full.
///
/// # Panics
///
/// This function will panic if passed pointer is `NULL`
#[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>()
}

/// Returns battery energy rate (in `W`).
///
/// # Panics
///
/// This function will panic if passed pointer is `NULL`
#[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>()
}

/// Returns battery voltage (in `V`)
///
/// # Panics
///
/// This function will panic if passed pointer is `NULL`
#[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>()
}

/// Returns battery state of health as a percentage value from `0.0` to `100.0`.
///
/// # Panics
///
/// This function will panic if passed pointer is `NULL`
#[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>()
}

/// Returns battery state.
///
/// # Panics
///
/// This function will panic if passed pointer is `NULL`
#[no_mangle]
pub unsafe extern "C" fn battery_get_state(ptr: *const Battery) -> State {
    assert!(!ptr.is_null());
    let battery = &*ptr;

    battery.state().into()
}

/// Returns battery technology.
///
/// # Panics
///
/// This function will panic if passed pointer is `NULL`
#[no_mangle]
pub unsafe extern "C" fn battery_get_technology(ptr: *const Battery) -> Technology {
    assert!(!ptr.is_null());
    let battery = &*ptr;

    battery.technology().into()
}

/// Returns battery temperature in Kelvin.
///
/// # Returns
///
/// If value is not available, function returns `NaN`.
///
/// # Panics
///
/// This function will panic if passed pointer is `NULL`
#[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>(),
    }
}

/// Returns battery cycles count.
///
/// # Returns
///
/// If value is not available, function returns max possible value for `uint32` type (`4294967295`).
///
/// # Panics
///
/// This function will panic if passed pointer is `NULL`
#[no_mangle]
pub unsafe extern "C" fn battery_get_cycle_count(ptr: *const Battery) -> libc::uint32_t {
    assert!(!ptr.is_null());
    let battery = &*ptr;

    match battery.cycle_count() {
        None => u32::MAX,
        Some(value) => value,
    }
}

/// Returns battery vendor.
///
/// Caller is required to free returned value with [battery_str_free](fn.battery_str_free.html)
/// function after using it.
///
/// # Returns
///
/// This function might return `NULL` if vendor data is not available.
/// Calling [battery_str_free](fn.battery_str_free.html) is not required in that case,
/// yet it will not lead to any error.
///
/// # Panics
///
/// This function will panic if passed pointer is `NULL`
#[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(),
    }
}

/// Returns battery model.
///
/// Caller is required to free returned value with [battery_str_free](fn.battery_str_free.html)
/// function after using it.
///
/// # Returns
///
/// This function might return `NULL` if model data is not available.
/// Calling [battery_str_free](fn.battery_str_free.html) is not required in that case,
/// yet it will not lead to any error.
///
/// # Panics
///
/// This function will panic if passed pointer is `NULL`
#[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(),
    }
}

/// Returns battery serial number.
///
/// Caller is required to free returned value with [battery_str_free](fn.battery_str_free.html)
/// function after using it.
///
/// # Returns
///
/// This function might return `NULL` if serial number data is not available.
/// Calling [battery_str_free](fn.battery_str_free.html) is not required in that case,
/// yet it will not lead to any error.
///
/// # Panics
///
/// This function will panic if passed pointer is `NULL`
#[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(),
    }
}

/// Returns battery time to full.
///
/// # Returns
///
/// If battery is not charging at the moment, this function will return `NaN`,
/// otherwise it will return seconds amount.
///
/// # Panics
///
/// This function will panic if passed pointer is `NULL`
#[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>(),
    }
}

/// Returns battery time to empty.
///
/// # Returns
///
/// If battery is not discharging at the moment, this function will return `NaN`,
/// otherwise it will return seconds amount.
///
/// # Panics
///
/// This function will panic if passed pointer is `NULL`
#[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>(),
    }
}

/// Frees battery instance.
///
/// Caller is required to call this function when battery pointer is not needed anymore
/// in order to properly free memory.
#[no_mangle]
pub unsafe extern "C" fn battery_free(ptr: *mut Battery) {
    if ptr.is_null() {
        return;
    }

    Box::from_raw(ptr);
}

/// Frees battery information string value.
///
/// Caller is required to call this function for return values for the following functions:
///  * [battery_vendor](fn.battery_vendor.html)
///  * [battery_model](fn.battery_model.html)
///  * [battery_serial_number](fn.battery_serial_number.html)
#[no_mangle]
pub unsafe extern "C" fn battery_str_free(ptr: *mut libc::c_char) {
    if ptr.is_null() {
        return;
    }

    CString::from_raw(ptr);
}