Skip to content

Commit bfcefb3

Browse files
committed
Revise the command callback trampoline to be able to report e.g. the PD`s input/output states
1 parent 613a37c commit bfcefb3

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

libosdp/src/pd.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,23 @@ unsafe extern "C" fn log_handler(
5050
}
5151
}
5252

53-
extern "C" fn trampoline<F>(data: *mut c_void, cmd: *mut libosdp_sys::osdp_cmd) -> i32
53+
extern "C" fn trampoline<F>(data_ptr: *mut c_void, cmd_ptr: *mut libosdp_sys::osdp_cmd) -> i32
5454
where
55-
F: FnMut(OsdpCommand) -> i32,
55+
F: FnMut(&mut OsdpCommand) -> i32,
5656
{
57-
let cmd: OsdpCommand = unsafe { (*cmd).into() };
58-
let callback: &mut F = unsafe { &mut *(data as *mut F) };
59-
callback(cmd)
57+
let mut cmd: OsdpCommand = unsafe { cmd_ptr.read().into() };
58+
let callback: &mut F = unsafe { &mut *(data_ptr as *mut F) };
59+
let res = callback(&mut cmd);
60+
// TODO: Free data_ptr's box?
61+
unsafe {
62+
cmd_ptr.write(cmd.into());
63+
}
64+
res
6065
}
6166

6267
fn get_trampoline<F>(_closure: &F) -> CommandCallback
6368
where
64-
F: FnMut(OsdpCommand) -> i32,
69+
F: FnMut(&mut OsdpCommand) -> i32,
6570
{
6671
trampoline::<F>
6772
}
@@ -131,7 +136,7 @@ impl PeripheralDevice {
131136
/// CP.
132137
pub fn set_command_callback<F>(&mut self, closure: F)
133138
where
134-
F: FnMut(OsdpCommand) -> i32,
139+
F: FnMut(&mut OsdpCommand) -> i32,
135140
{
136141
unsafe {
137142
let callback = get_trampoline(&closure);

libosdp/tests/common/device.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl PdDevice {
9494
let mut pd = PeripheralDevice::new(pd_info, bus)?;
9595
let (cmd_tx, cmd_rx) = std::sync::mpsc::channel::<OsdpCommand>();
9696
pd.set_command_callback(|command| {
97-
cmd_tx.send(command).unwrap();
97+
cmd_tx.send(command.clone()).unwrap();
9898
0
9999
});
100100

@@ -106,7 +106,7 @@ impl PdDevice {
106106
let dev = dev_clone;
107107
let sender = cmd_tx;
108108
dev.lock().unwrap().set_command_callback(|command| {
109-
sender.send(command).expect("PD command send");
109+
sender.send(command.clone()).expect("PD command send");
110110
0
111111
});
112112
loop {

0 commit comments

Comments
 (0)