1#![allow(non_camel_case_types)]
11
12use std::ffi::{c_char, c_void};
13
14#[rustfmt::skip]
15mod generated;
16pub use generated::*;
17
18pub const OBAPI_OK: i32 = 0;
19pub const OBAPI_ERR_EXCEPTION: i32 = -1;
20pub const OBAPI_ERR_UNKNOWN: i32 = -2;
21pub const OBAPI_ERR_WIRE: i32 = -3;
22pub const OBAPI_ERR_INVALID: i32 = -4;
23
24pub type obapi_dispatch_fn =
26 unsafe extern "C" fn(ctx: *mut c_void, op: u32, data: *const u8, len: usize);
27
28#[repr(C)]
30#[derive(Debug)]
31pub struct obapi_vtable {
32 pub module: *const c_char,
33 pub role: *const c_char,
34 pub create: unsafe extern "C" fn(
35 domain: i32,
36 name: *const c_char,
37 name_len: usize,
38 ctx: *mut c_void,
39 dispatch: Option<obapi_dispatch_fn>,
40 out: *mut *mut c_void,
41 ) -> i32,
42 pub send:
43 unsafe extern "C" fn(handle: *mut c_void, op: u32, data: *const u8, len: usize) -> i32,
44 pub destroy: unsafe extern "C" fn(handle: *mut c_void),
45 pub is_valid: unsafe extern "C" fn(handle: *mut c_void) -> bool,
46 pub wait_until_connected: Option<unsafe extern "C" fn(handle: *mut c_void) -> i32>,
47 pub set_name: Option<
48 unsafe extern "C" fn(handle: *mut c_void, name: *const c_char, name_len: usize) -> i32,
49 >,
50}
51
52unsafe impl Sync for obapi_vtable {}
54
55unsafe extern "C" {
56 pub fn obapi_last_error(buf: *mut c_char, cap: usize) -> usize;
58}
59
60pub fn last_error() -> String {
62 let mut buf = vec![0u8; 512];
63 let n = unsafe { obapi_last_error(buf.as_mut_ptr().cast(), buf.len()) };
65 if n > buf.len() {
66 buf.resize(n, 0);
67 unsafe { obapi_last_error(buf.as_mut_ptr().cast(), buf.len()) };
69 }
70 buf.truncate(n);
71 String::from_utf8_lossy(&buf).into_owned()
72}