Init
This commit is contained in:
commit
29a5c87299
4 changed files with 468 additions and 0 deletions
208
src/lib.rs
Normal file
208
src/lib.rs
Normal file
|
@ -0,0 +1,208 @@
|
|||
use anyhow::{bail, Result};
|
||||
use dlopen::wrapper::{Container, WrapperApi};
|
||||
use dlopen_derive::WrapperApi;
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::os::raw::{c_char, c_int, c_uint, c_void};
|
||||
|
||||
type LipcType = c_void;
|
||||
|
||||
type LipcCode = c_uint;
|
||||
|
||||
const LIPC_CODE_LIPC_OK: LipcCode = 0;
|
||||
// const LIPC_CODE_LIPC_ERROR_UNKNOWN: LIPCcode = 1;
|
||||
// const LIPC_CODE_LIPC_ERROR_INTERNAL: LIPCcode = 2;
|
||||
// const LIPC_CODE_LIPC_ERROR_NO_SUCH_SOURCE: LIPCcode = 3;
|
||||
// const LIPC_CODE_LIPC_ERROR_OPERATION_NOT_SUPPORTED: LIPCcode = 4;
|
||||
// const LIPC_CODE_LIPC_ERROR_OUT_OF_MEMORY: LIPCcode = 5;
|
||||
// const LIPC_CODE_LIPC_ERROR_SUBSCRIPTION_FAILED: LIPCcode = 6;
|
||||
// const LIPC_CODE_LIPC_ERROR_NO_SUCH_PARAM: LIPCcode = 7;
|
||||
// const LIPC_CODE_LIPC_ERROR_NO_SUCH_PROPERTY: LIPCcode = 8;
|
||||
// const LIPC_CODE_LIPC_ERROR_ACCESS_NOT_ALLOWED: LIPCcode = 9;
|
||||
// const LIPC_CODE_LIPC_ERROR_BUFFER_TOO_SMALL: LIPCcode = 10;
|
||||
// const LIPC_CODE_LIPC_ERROR_INVALID_HANDLE: LIPCcode = 11;
|
||||
// const LIPC_CODE_LIPC_ERROR_INVALID_ARG: LIPCcode = 12;
|
||||
// const LIPC_CODE_LIPC_ERROR_OPERATION_NOT_ALLOWED: LIPCcode = 13;
|
||||
// const LIPC_CODE_LIPC_ERROR_PARAMS_SIZE_EXCEEDED: LIPCcode = 14;
|
||||
// const LIPC_CODE_LIPC_ERROR_TIMED_OUT: LIPCcode = 15;
|
||||
// const LIPC_CODE_LIPC_ERROR_SERVICE_NAME_TOO_LONG: LIPCcode = 16;
|
||||
// const LIPC_CODE_LIPC_ERROR_DUPLICATE_SERVICE_NAME: LIPCcode = 17;
|
||||
// const LIPC_CODE_LIPC_ERROR_INIT_DBUS: LIPCcode = 18;
|
||||
// const LIPC_CODE_LIPC_PROP_ERROR_INVALID_STATE: LIPCcode = 256;
|
||||
// const LIPC_CODE_LIPC_PROP_ERROR_NOT_INITIALIZED: LIPCcode = 257;
|
||||
// const LIPC_CODE_LIPC_PROP_ERROR_INTERNAL: LIPCcode = 258;
|
||||
|
||||
#[derive(WrapperApi)]
|
||||
pub struct LipcApi {
|
||||
#[dlopen_name = "LipcOpenNoName"]
|
||||
lipc_open_no_name: unsafe extern "C" fn() -> *mut LipcType,
|
||||
|
||||
#[dlopen_name = "LipcClose"]
|
||||
lipc_close: unsafe extern "C" fn(lipc: *mut LipcType),
|
||||
|
||||
#[dlopen_name = "LipcGetErrorString"]
|
||||
lipc_get_error_string: unsafe extern "C" fn(code: LipcCode) -> *const c_char,
|
||||
|
||||
#[dlopen_name = "LipcFreeString"]
|
||||
lipc_free_string: unsafe extern "C" fn(string: *mut c_char),
|
||||
|
||||
#[dlopen_name = "LipcGetStringProperty"]
|
||||
lipc_get_string_property: unsafe extern "C" fn(
|
||||
lipc: *mut LipcType,
|
||||
service: *const c_char,
|
||||
property: *const c_char,
|
||||
value: *mut *mut c_char,
|
||||
) -> LipcCode,
|
||||
|
||||
#[dlopen_name = "LipcSetStringProperty"]
|
||||
lipc_set_string_property: unsafe extern "C" fn(
|
||||
lipc: *mut LipcType,
|
||||
service: *const c_char,
|
||||
property: *const c_char,
|
||||
value: *const c_char,
|
||||
) -> LipcCode,
|
||||
|
||||
#[dlopen_name = "LipcGetIntProperty"]
|
||||
lipc_get_int_property: unsafe extern "C" fn(
|
||||
lipc: *mut LipcType,
|
||||
service: *const c_char,
|
||||
property: *const c_char,
|
||||
value: *mut c_int,
|
||||
) -> LipcCode,
|
||||
|
||||
#[dlopen_name = "LipcSetIntProperty"]
|
||||
lipc_set_int_property: unsafe extern "C" fn(
|
||||
lipc: *mut LipcType,
|
||||
service: *const c_char,
|
||||
property: *const c_char,
|
||||
value: c_int,
|
||||
) -> LipcCode,
|
||||
}
|
||||
|
||||
impl LipcApi {
|
||||
const DEFAULT_NAME: &str = "liblipc.so";
|
||||
|
||||
pub fn load(path: Option<&str>) -> Result<Container<Self>> {
|
||||
let name = match path {
|
||||
Some(x) => x,
|
||||
None => Self::DEFAULT_NAME,
|
||||
};
|
||||
let cont: Container<Self> = unsafe { Container::load(name) }?;
|
||||
|
||||
Ok(cont)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Lipc {
|
||||
api: Container<LipcApi>,
|
||||
conn: *mut LipcType,
|
||||
}
|
||||
|
||||
impl Lipc {
|
||||
pub fn new(api: Container<LipcApi>) -> Result<Self> {
|
||||
let lipc = unsafe { (api.lipc_open_no_name)() };
|
||||
if lipc == (std::ptr::null_mut() as *mut c_void) {
|
||||
bail!("Failed to create connection!");
|
||||
}
|
||||
|
||||
Ok(Self { api, conn: lipc })
|
||||
}
|
||||
|
||||
pub fn load(name: Option<&str>) -> Result<Self> {
|
||||
let api = LipcApi::load(name)?;
|
||||
Self::new(api)
|
||||
}
|
||||
|
||||
fn code_to_result(&self, code: LipcCode) -> Result<()> {
|
||||
if code == LIPC_CODE_LIPC_OK {
|
||||
Ok(())
|
||||
} else {
|
||||
bail!("LIPC error code: {} ({})", self.code_to_string(code)?, code);
|
||||
}
|
||||
}
|
||||
|
||||
fn code_to_string(&self, code: LipcCode) -> Result<String> {
|
||||
let cstr = unsafe {
|
||||
let str_ptr = (self.api.lipc_get_error_string)(code);
|
||||
CStr::from_ptr(str_ptr)
|
||||
};
|
||||
|
||||
Ok(String::from(cstr.to_str()?))
|
||||
}
|
||||
|
||||
pub fn get_int_prop(&self, service: &str, prop: &str) -> Result<c_int> {
|
||||
let mut val: c_int = 0;
|
||||
let service = CString::new(service)?;
|
||||
let prop = CString::new(prop)?;
|
||||
|
||||
let code = unsafe {
|
||||
(self.api.lipc_get_int_property)(self.conn, service.as_ptr(), prop.as_ptr(), &mut val)
|
||||
};
|
||||
self.code_to_result(code)?;
|
||||
|
||||
Ok(val)
|
||||
}
|
||||
|
||||
pub fn set_int_prop(&self, service: &str, prop: &str, value: c_int) -> Result<()> {
|
||||
let service = CString::new(service)?;
|
||||
let prop = CString::new(prop)?;
|
||||
|
||||
let code = unsafe {
|
||||
(self.api.lipc_set_int_property)(self.conn, service.as_ptr(), prop.as_ptr(), value)
|
||||
};
|
||||
self.code_to_result(code)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_str_prop(&self, service: &str, prop: &str) -> Result<String> {
|
||||
let mut handle: *mut c_char = std::ptr::null_mut();
|
||||
let handle_ptr: *mut *mut c_char = &mut handle;
|
||||
|
||||
let service = CString::new(service)?;
|
||||
let prop = CString::new(prop)?;
|
||||
|
||||
let code = unsafe {
|
||||
(self.api.lipc_get_string_property)(
|
||||
self.conn,
|
||||
service.as_ptr(),
|
||||
prop.as_ptr(),
|
||||
handle_ptr,
|
||||
)
|
||||
};
|
||||
self.code_to_result(code)?;
|
||||
|
||||
let val: String;
|
||||
unsafe {
|
||||
val = CStr::from_ptr(handle).to_str()?.to_string();
|
||||
(self.api.lipc_free_string)(handle);
|
||||
}
|
||||
|
||||
Ok(val)
|
||||
}
|
||||
|
||||
pub fn set_str_prop(&self, service: &str, prop: &str, value: &str) -> Result<()> {
|
||||
let service = CString::new(service)?;
|
||||
let prop = CString::new(prop)?;
|
||||
let value = CString::new(value)?;
|
||||
|
||||
let code = unsafe {
|
||||
(self.api.lipc_set_string_property)(
|
||||
self.conn,
|
||||
service.as_ptr(),
|
||||
prop.as_ptr(),
|
||||
value.as_ptr(),
|
||||
)
|
||||
};
|
||||
self.code_to_result(code)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Lipc {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
(self.api.lipc_close)(self.conn);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue