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
use std::boxed::Box;
use std::convert::TryInto;
use std::ptr::null;
use libc::{c_char, c_int, c_uchar, size_t};
use crate::error_codes::*;
use crate::{
copy_string, PluginAPI, PluginData, PluginError, Val, ATTRIBUTE_PRE_INIT_FALSE,
ATTRIBUTE_PRE_INIT_TRUE, ERRORS,
};
pub type Phase = c_int;
pub extern "C" fn plugin_free(plugin_data: *mut PluginData) {
if plugin_data.is_null() {
return;
}
let plugin_data = plugin_data as *mut Box<PluginData>;
unsafe {
Box::from_raw(plugin_data);
}
}
pub unsafe extern "C" fn plugin_init<T: PluginAPI<E>, E: PluginError + 'static>(
plugin_data: *mut PluginData,
) -> c_int {
if plugin_data.is_null() {
log::error!("plugin_data pointer is null");
return NULL_PTR_ERR;
};
let plugin_data = plugin_data as *mut T;
match (*plugin_data).init() {
Ok(_) => {
log::debug!("Successfully initialized plugin");
PLUGIN_OK
}
Err(e) => {
log::error!("Plugin failed to initialize: {}", e);
e.error_code()
}
}
}
pub extern "C" fn error_message_ns(error_code: c_int) -> *const c_uchar {
let error_code: size_t = match error_code.try_into() {
Ok(error_code) => error_code,
Err(_) => {
log::error!("Unrecognized error code provided");
return null();
}
};
ERRORS.get(error_code).map_or(null(), |e| e.as_ptr())
}
pub unsafe extern "C" fn attribute_count<T: PluginAPI<E>, E: PluginError + 'static>(
plugin_data: *const PluginData,
count: *mut size_t,
) -> c_int {
if plugin_data.is_null() {
log::error!("plugin_data pointer is null");
return NULL_PTR_ERR;
};
let plugin_data = plugin_data as *const T;
*count = (*plugin_data).attribute_count();
PLUGIN_OK
}
pub unsafe extern "C" fn attribute_ids<T: PluginAPI<E>, E: PluginError + 'static>(
plugin_data: *const PluginData,
buffer: *mut size_t,
length: size_t,
) -> c_int {
if plugin_data.is_null() {
log::error!("plugin_data pointer is null");
return NULL_PTR_ERR;
}
let plugin_data = plugin_data as *const T;
let ids = (*plugin_data).attribute_ids();
match copy_string(&ids, buffer, length) {
Ok(_) => PLUGIN_OK,
Err(_) => UNDEFINED_ERR,
}
}
pub unsafe extern "C" fn attribute_name<T: PluginAPI<E>, E: PluginError + 'static>(
plugin_data: *const PluginData,
id: size_t,
buffer: *mut c_uchar,
length: size_t,
) -> c_int {
if plugin_data.is_null() {
log::error!("plugin_data pointer is null");
return NULL_PTR_ERR;
}
let plugin_data = plugin_data as *const T;
match (*plugin_data).attribute_name(id) {
Ok(name) => copy_string(name.to_bytes_with_nul(), buffer, length)
.map(|_| PLUGIN_OK)
.unwrap_or_else(|_| UNDEFINED_ERR),
Err(e) => e.error_code(),
}
}
pub unsafe extern "C" fn attribute_pre_init<T: PluginAPI<E>, E: PluginError + 'static>(
plugin_data: *const PluginData,
id: size_t,
pre_init: *mut c_char,
) -> c_int {
if plugin_data.is_null() {
log::error!("plugin_data pointer is null");
return NULL_PTR_ERR;
}
if pre_init.is_null() {
log::error!("pre_init pointer is null");
return NULL_PTR_ERR;
}
let plugin_data = plugin_data as *const T;
match (*plugin_data).attribute_pre_init(id) {
Ok(pre_init_resp) => {
log::debug!(
"Response for pre-init status of attribute {}: {}",
id,
pre_init_resp
);
if pre_init_resp {
*pre_init = ATTRIBUTE_PRE_INIT_TRUE;
} else {
*pre_init = ATTRIBUTE_PRE_INIT_FALSE;
};
PLUGIN_OK
}
Err(e) => e.error_code(),
}
}
pub unsafe extern "C" fn attribute_value<T: PluginAPI<E>, E: PluginError + 'static>(
plugin_data: *const PluginData,
id: size_t,
value: *mut Val,
phase: Phase,
) -> c_int {
if plugin_data.is_null() {
log::error!("plugin_data pointer is null");
return NULL_PTR_ERR;
}
let plugin_data = plugin_data as *const T;
match (*plugin_data).attribute_value(id, phase) {
Ok(new_value) => {
log::debug!(
"Response for the value of attribute {}: {:?}",
id,
new_value
);
*value = new_value
}
Err(e) => return e.error_code(),
};
PLUGIN_OK
}
pub unsafe extern "C" fn set_attribute_value<T: PluginAPI<E>, E: PluginError + 'static>(
plugin_data: *mut PluginData,
id: size_t,
value: *const Val,
phase: Phase,
) -> c_int {
if plugin_data.is_null() {
log::error!("plugin_data pointer is null");
return NULL_PTR_ERR;
}
if value.is_null() {
log::error!("value pointer is null");
return NULL_PTR_ERR;
}
let plugin_data = plugin_data as *mut T;
match (*plugin_data).attribute_set_value(id, &*value, phase) {
Ok(_) => {
log::debug!("Set attribute {} to {:?}", id, *value);
PLUGIN_OK
}
Err(e) => e.error_code(),
}
}