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
use std::ptr;
use crate::{Batteries, Battery, Manager};
#[no_mangle]
pub extern "C" fn battery_manager_new() -> *mut Manager {
match Manager::new() {
Ok(manager) => Box::into_raw(Box::new(manager)),
Err(e) => {
crate::errors::set_last_error(e);
ptr::null_mut()
}
}
}
#[no_mangle]
pub unsafe extern "C" fn battery_manager_iter(ptr: *mut Manager) -> *mut Batteries {
assert!(!ptr.is_null());
let manager = &*ptr;
match manager.batteries() {
Ok(iterator) => Box::into_raw(Box::new(iterator)),
Err(e) => {
crate::errors::set_last_error(e);
ptr::null_mut()
}
}
}
pub unsafe extern "C" fn battery_manager_refresh(manager_ptr: *mut Manager, battery_ptr: *mut Battery) -> libc::c_int {
assert!(!manager_ptr.is_null());
let manager = &mut *manager_ptr;
assert!(!battery_ptr.is_null());
let mut battery = &mut *battery_ptr;
match manager.refresh(&mut battery) {
Ok(_) => 0,
Err(e) => {
crate::errors::set_last_error(e);
-1
}
}
}
#[no_mangle]
pub unsafe extern "C" fn battery_manager_free(ptr: *mut Manager) {
if ptr.is_null() {
return;
}
Box::from_raw(ptr);
}