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
mod errors;
mod ffi;
mod strings;
use std::cmp::{Eq, PartialEq};
use std::error::Error;
use std::ffi::{CStr, CString};
use libc::{c_double, c_int, c_long, c_uchar, size_t};
pub use errors::constants;
pub use errors::ERRORS;
pub use ffi::*;
pub use strings::copy_string;
pub trait PluginAPI<E: Error + PluginError> {
type Plugin;
fn new() -> Result<Self::Plugin, E>;
fn attribute_name(&self, id: usize) -> Result<&CStr, E>;
fn attribute_value(&self, id: usize) -> Result<Value, E>;
fn attribute_set_value(&mut self, id: usize, value: &Value) -> Result<(), E>;
}
pub trait PluginError: std::error::Error {
fn error_code(&self) -> c_int;
}
#[derive(Clone, Debug)]
#[repr(C)]
pub struct Plugin {
pub peripheral: *mut Peripheral,
pub vtable: VTable,
}
impl Drop for Plugin {
fn drop(&mut self) {
(self.vtable.peripheral_free)(self.peripheral);
}
}
unsafe impl Send for Plugin {}
#[derive(Debug)]
#[repr(C)]
pub struct Peripheral {
_private: [u8; 0],
}
#[derive(Clone, Debug)]
#[repr(C)]
pub struct VTable {
pub peripheral_free: extern "C" fn(*mut Peripheral),
pub error_message: extern "C" fn(c_int) -> *const c_uchar,
pub attribute_name: extern "C" fn(
peripheral: *const Peripheral,
id: size_t,
buffer: *mut c_uchar,
length: size_t,
) -> c_int,
pub attribute_value:
extern "C" fn(peripheral: *const Peripheral, id: size_t, value: *mut Value) -> c_int,
pub set_attribute_value:
extern "C" fn(peripheral: *mut Peripheral, id: size_t, value: *const Value) -> c_int,
}
pub type KpalPluginInit = extern "C" fn(*mut Plugin) -> c_int;
#[derive(Debug)]
#[repr(C)]
pub struct Attribute {
pub name: CString,
pub value: Value,
}
impl Eq for Attribute {}
impl PartialEq for Attribute {
fn eq(&self, other: &Attribute) -> bool {
self.name == other.name
}
}
#[derive(Clone, Debug, PartialEq)]
#[repr(C)]
pub enum Value {
Int(c_long),
Float(c_double),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn properties_with_the_same_name_and_same_values_are_equal() {
let prop1_left = Attribute {
name: CString::new("prop1").unwrap(),
value: Value::Int(0),
};
let prop1_right = Attribute {
name: CString::new("prop1").unwrap(),
value: Value::Int(0),
};
assert_eq!(prop1_left, prop1_right);
}
#[test]
fn properties_with_the_same_name_and_different_values_are_equal() {
let prop1_left = Attribute {
name: CString::new("prop1").unwrap(),
value: Value::Int(0),
};
let prop1_right = Attribute {
name: CString::new("prop1").unwrap(),
value: Value::Int(1),
};
assert_eq!(prop1_left, prop1_right);
}
#[test]
fn properties_with_different_names_are_not_equal() {
let prop1 = Attribute {
name: CString::new("prop1").unwrap(),
value: Value::Int(0),
};
let prop2 = Attribute {
name: CString::new("prop2").unwrap(),
value: Value::Int(0),
};
assert_ne!(prop1, prop2);
}
}