Modify the status code returned by the function

Signed-off-by: chenchong_666 <chenchong57@huawei.com>
This commit is contained in:
chenchong_666 2023-03-14 16:16:54 +08:00
parent b802ad9351
commit 1e721bf4f4
28 changed files with 353 additions and 285 deletions

View File

@ -17,12 +17,12 @@ extern crate ipc_rust;
extern crate example_calc_ipc_service;
use ipc_rust::{FromRemoteObj, RemoteObjRef, get_service,};
use example_calc_ipc_service::{ICalc, CalcProxy, EXAMPLE_IPC_CALC_SERVICE_ID, init_access_token};
use example_calc_ipc_service::{ICalc, EXAMPLE_IPC_CALC_SERVICE_ID};
fn get_calc_service() -> RemoteObjRef<dyn ICalc>
{
let object = get_service(EXAMPLE_IPC_CALC_SERVICE_ID).expect("get icalc service failed");
let remote = <dyn ICalc as FromRemoteObj>::from(object);
let remote = <dyn ICalc as FromRemoteObj>::try_from(object);
let remote = match remote {
Ok(x) => x,
Err(error) => {
@ -34,7 +34,7 @@ fn get_calc_service() -> RemoteObjRef<dyn ICalc>
}
#[test]
fn Calculator_Ability() {
fn calculator_ability() {
let remote = get_calc_service();
// add
let ret = remote.add(5, 5).expect("add failed");

View File

@ -19,22 +19,22 @@ extern crate example_calc_ipc_service;
extern crate ipc_rust;
use example_calc_ipc_service::{ICalc, CalcStub, EXAMPLE_IPC_CALC_SERVICE_ID, init_access_token, add, sub, mul, div};
use ipc_rust::{IRemoteBroker, join_work_thread, Result, add_service,};
use ipc_rust::{IRemoteBroker, join_work_thread, IpcResult, add_service,};
/// example.calc.ipc.ICalcService type
pub struct CalcService;
impl ICalc for CalcService {
fn add(&self, num1: i32, num2: i32) -> Result<i32> {
fn add(&self, num1: i32, num2: i32) -> IpcResult<i32> {
Ok(add(&num1, &num2))
}
fn sub(&self, num1: i32, num2: i32) -> Result<i32> {
fn sub(&self, num1: i32, num2: i32) -> IpcResult<i32> {
Ok(sub(&num1, &num2))
}
fn mul(&self, num1: i32, num2: i32) -> Result<i32> {
fn mul(&self, num1: i32, num2: i32) -> IpcResult<i32> {
Ok(mul(&num1, &num2))
}
fn div(&self, num1: i32, num2: i32) -> Result<i32> {
fn div(&self, num1: i32, num2: i32) -> IpcResult<i32> {
Ok(div(&num1, &num2))
}
}

View File

@ -20,7 +20,7 @@ extern crate ipc_rust;
mod access_token;
use ipc_rust::{
IRemoteBroker, IRemoteObj, RemoteStub, Result,
IRemoteBroker, IRemoteObj, RemoteStub, IpcResult, IpcStatusCode,
RemoteObj, define_remote_object, FIRST_CALL_TRANSACTION,
};
use ipc_rust::{
@ -71,14 +71,14 @@ pub enum ICalcCode {
}
impl TryFrom<u32> for ICalcCode {
type Error = i32;
fn try_from(code: u32) -> Result<Self> {
type Error = IpcStatusCode;
fn try_from(code: u32) -> IpcResult<Self> {
match code {
_ if code == ICalcCode::CodeAdd as u32 => Ok(ICalcCode::CodeAdd),
_ if code == ICalcCode::CodeSub as u32 => Ok(ICalcCode::CodeSub),
_ if code == ICalcCode::CodeMul as u32 => Ok(ICalcCode::CodeMul),
_ if code == ICalcCode::CodeDiv as u32 => Ok(ICalcCode::CodeDiv),
_ => Err(-1),
_ => Err(IpcStatusCode::Failed),
}
}
}
@ -86,17 +86,17 @@ impl TryFrom<u32> for ICalcCode {
/// Function between proxy and stub of ICalcService
pub trait ICalc: IRemoteBroker {
/// Calc add num1 + num2
fn add(&self, num1: i32, num2: i32) -> Result<i32>;
fn add(&self, num1: i32, num2: i32) -> IpcResult<i32>;
/// Calc sub num1 + num2
fn sub(&self, num1: i32, num2: i32) -> Result<i32>;
fn sub(&self, num1: i32, num2: i32) -> IpcResult<i32>;
/// Calc mul num1 + num2
fn mul(&self, num1: i32, num2: i32) -> Result<i32>;
fn mul(&self, num1: i32, num2: i32) -> IpcResult<i32>;
/// Calc div num1 + num2
fn div(&self, num1: i32, num2: i32) -> Result<i32>;
fn div(&self, num1: i32, num2: i32) -> IpcResult<i32>;
}
fn on_icalc_remote_request(stub: &dyn ICalc, code: u32, data: &BorrowedMsgParcel,
reply: &mut BorrowedMsgParcel) -> Result<()> {
reply: &mut BorrowedMsgParcel) -> IpcResult<()> {
match code.try_into()? {
ICalcCode::CodeAdd => {
let num1: i32 = data.read().expect("Failed to read num1 in addition operation");
@ -138,22 +138,22 @@ define_remote_object!(
// Make RemoteStub<CalcStub> object can call ICalc function directly.
impl ICalc for RemoteStub<CalcStub> {
fn add (&self, num1: i32, num2: i32) -> Result<i32> {
fn add (&self, num1: i32, num2: i32) -> IpcResult<i32> {
self.0.add(num1, num2)
}
fn sub (&self, num1: i32, num2: i32) -> Result<i32> {
fn sub (&self, num1: i32, num2: i32) -> IpcResult<i32> {
self.0.sub(num1, num2)
}
fn mul (&self, num1: i32, num2: i32) -> Result<i32> {
fn mul (&self, num1: i32, num2: i32) -> IpcResult<i32> {
self.0.mul(num1, num2)
}
fn div (&self, num1: i32, num2: i32) -> Result<i32> {
fn div (&self, num1: i32, num2: i32) -> IpcResult<i32> {
self.0.div(num1, num2)
}
}
impl ICalc for CalcProxy {
fn add(&self, num1: i32, num2: i32) -> Result<i32> {
fn add(&self, num1: i32, num2: i32) -> IpcResult<i32> {
let mut data = MsgParcel::new().expect("MsgParcel should success");
data.write(&num1)?;
data.write(&num2)?;
@ -162,7 +162,7 @@ impl ICalc for CalcProxy {
let ret: i32 = reply.read().expect("need reply i32");
Ok(ret)
}
fn sub(&self, num1: i32, num2: i32) -> Result<i32> {
fn sub(&self, num1: i32, num2: i32) -> IpcResult<i32> {
let mut data = MsgParcel::new().expect("MsgParcel should success");
data.write(&num1)?;
data.write(&num2)?;
@ -171,7 +171,7 @@ impl ICalc for CalcProxy {
let ret: i32 = reply.read().expect("need reply i32");
Ok(ret)
}
fn mul(&self, num1: i32, num2: i32) -> Result<i32> {
fn mul(&self, num1: i32, num2: i32) -> IpcResult<i32> {
let mut data = MsgParcel::new().expect("MsgParcel should success");
data.write(&num1)?;
data.write(&num2)?;
@ -180,7 +180,7 @@ impl ICalc for CalcProxy {
let ret: i32 = reply.read().expect("need reply i32");
Ok(ret)
}
fn div(&self, num1: i32, num2: i32) -> Result<i32> {
fn div(&self, num1: i32, num2: i32) -> IpcResult<i32> {
let mut data = MsgParcel::new().expect("MsgParcel should success");
data.write(&num1)?;
data.write(&num2)?;

View File

@ -14,7 +14,7 @@
*/
use crate::{
ipc_binding, RawData, Result, BorrowedMsgParcel, result_status,
ipc_binding, RawData, IpcResult, IpcStatusCode, BorrowedMsgParcel, status_result,
AsRawPtr
};
use crate::ipc_binding::CAshmem;
@ -156,13 +156,13 @@ impl Ashmem {
}
/// Read ashmem
pub fn read(&self, size: i32, offset: i32) -> Result<RawData> {
pub fn read(&self, size: i32, offset: i32) -> IpcResult<RawData> {
let raw_ptr = unsafe {
// SAFETY: Rust Ashmem always hold a valid native CAshmem.
ipc_binding::ReadFromCAshmem(self.as_inner(), size, offset)
};
if raw_ptr.is_null() {
Err(-1)
Err(IpcStatusCode::Failed)
} else {
Ok(RawData::new(raw_ptr, size as u32))
}
@ -199,23 +199,23 @@ impl Drop for Ashmem {
/// Write a ashmem
impl Serialize for Ashmem {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
// SAFETY:
let ret = unsafe {
ipc_binding::CParcelWriteAshmem(parcel.as_mut_raw(), self.as_inner())
};
result_status::<()>(ret, ())
status_result::<()>(ret as i32, ())
}
}
/// read a ashmem
impl Deserialize for Ashmem {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> Result<Self> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self> {
let ptr = unsafe {
ipc_binding::CParcelReadAshmem(parcel.as_raw())
};
if ptr.is_null() {
Err(-1)
Err(IpcStatusCode::Failed)
} else {
// SAFETY:
unsafe {

View File

@ -13,14 +13,77 @@
* limitations under the License.
*/
/// IPC specific Result, error is i32 type
pub type Result<T> = std::result::Result<T, i32>;
use std::error::Error;
use std::fmt;
use std::ffi::{CString, c_char};
use hilog_rust::{debug, hilog, HiLogLabel, LogType};
/// Generate a rust ipc Result by ret and val arguments.
pub fn result_status<T>(ret: bool, val: T) -> Result<T> {
if ret {
Ok(val)
} else {
Err(-1)
const LOG_LABEL: HiLogLabel = HiLogLabel {
log_type: LogType::LogCore,
domain: 0xd001510,
tag: "RustStatus"
};
/// IPC specific Result, error is i32 type
pub type IpcResult<T> = std::result::Result<T, IpcStatusCode>;
/// usage:
/// status_result::<()>(result, ())
/// or
/// status_result::<MsgParcel>(result, reply)
pub fn status_result<T>(code: i32, val: T) -> IpcResult<T> {
debug!(LOG_LABEL, "rust status code: {}", code);
match parse_status_code(code) {
IpcStatusCode::Ok => Ok(val),
e => Err(e),
}
}
/// Parse status code
pub fn parse_status_code(code: i32) -> IpcStatusCode {
match code {
e if e == IpcStatusCode::Ok as i32 => IpcStatusCode::Ok,
e if e == IpcStatusCode::Failed as i32 => IpcStatusCode::Failed,
e if e == IpcStatusCode::Einval as i32 => IpcStatusCode::Einval,
e if e == IpcStatusCode::ErrNullObject as i32 => IpcStatusCode::ErrNullObject,
e if e == IpcStatusCode::ErrDeadObject as i32 => IpcStatusCode::ErrDeadObject,
e if e == IpcStatusCode::InvalidValue as i32 => IpcStatusCode::InvalidValue,
_ => IpcStatusCode::Unknow,
}
}
/// IPC unified status code
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum IpcStatusCode {
/// success
Ok = 1,
/// failed
Failed = -1,
/// RemoteObj Err Code
/// Invalide Params
Einval = 22,
/// Object is null
ErrNullObject = 7,
/// The object has died
ErrDeadObject = -32,
/// invail value
InvalidValue = 0,
/// unknow value
Unknow = 99999,
}
impl Error for IpcStatusCode {}
impl fmt::Display for IpcStatusCode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
IpcStatusCode::Ok => write!(f, "Call Ok"),
IpcStatusCode::Failed => write!(f, "Call Failed"),
IpcStatusCode::Einval => write!(f, "Invalid Params"),
IpcStatusCode::ErrNullObject => write!(f, "Null Obj"),
IpcStatusCode::ErrDeadObject => write!(f, "Dead Obj"),
IpcStatusCode::InvalidValue => write!(f, "Invalid Value"),
_ => write!(f, "Unknow Error"),
}
}
}

View File

@ -46,7 +46,7 @@ macro_rules! define_remote_object {
impl $proxy {
/// Create proxy object by RemoteObj
fn from_remote_object(remote: &RemoteObj) -> $crate::Result<Self> {
fn from_remote_object(remote: &RemoteObj) -> $crate::IpcResult<Self> {
Ok(Self {
remote: remote.clone(),
$($item_name: $item_init),*
@ -89,10 +89,11 @@ macro_rules! define_remote_object {
reply: &mut $crate::BorrowedMsgParcel) -> i32 {
// For example, "self.0" is "Box<dyn ITest>", "*self.0" is "dyn ITest"
let result = $on_remote_request(&*self.0, code, data, reply);
match result {
Ok(_) => 0,
Err(error) => {
error
error as i32
}
}
}
@ -101,7 +102,7 @@ macro_rules! define_remote_object {
impl $crate::FromRemoteObj for dyn $remote_broker {
/// For example, convert RemoteObj to RemoteObjRef<dyn ITest>
fn try_from(object: $crate::RemoteObj)
-> $crate::Result<$crate::RemoteObjRef<dyn $remote_broker>> {
-> $crate::IpcResult<$crate::RemoteObjRef<dyn $remote_broker>> {
Ok($crate::RemoteObjRef::new(Box::new($proxy::from_remote_object(&object)?)))
}
}

View File

@ -17,7 +17,7 @@ pub mod remote_obj;
pub mod remote_stub;
pub mod macros;
use crate::{BorrowedMsgParcel, MsgParcel, Result, DeathRecipient,};
use crate::{BorrowedMsgParcel, MsgParcel, IpcResult, DeathRecipient,};
use std::ops::{Deref};
use std::cmp::Ordering;
use crate::String16;
@ -28,7 +28,7 @@ pub use crate::RemoteObj;
/// Like C++ IRemoteObject class, define function for both proxy and stub object
pub trait IRemoteObj {
/// Send a IPC request to remote service
fn send_request(&self, code: u32, data: &MsgParcel, is_async: bool) -> Result<MsgParcel>;
fn send_request(&self, code: u32, data: &MsgParcel, is_async: bool) -> IpcResult<MsgParcel>;
/// Add a death recipient
fn add_death_recipient(&self, recipient: &mut DeathRecipient) -> bool;
@ -46,7 +46,7 @@ pub trait IRemoteObj {
fn is_dead(&self) -> bool;
/// get interface descriptor
fn interface_descriptor(&self) -> Result<String>;
fn interface_descriptor(&self) -> IpcResult<String>;
}
/// Like C++ IPCObjectStub class, define function for stub object only, like on_remote_request().
@ -70,7 +70,7 @@ pub trait IRemoteBroker: Send + Sync {
/// dynamic trait object: IRemoteObject. For example, "dyn ITest" should implements this trait
pub trait FromRemoteObj: IRemoteBroker {
/// Convert a RemoteObj to RemoteObjeRef
fn try_from(object: RemoteObj) -> Result<RemoteObjRef<Self>>;
fn try_from(object: RemoteObj) -> IpcResult<RemoteObjRef<Self>>;
}
/// Strong reference for "dyn IRemoteBroker" object, for example T is "dyn ITest"
@ -93,7 +93,9 @@ impl<T: FromRemoteObj + ?Sized> Deref for RemoteObjRef<T> {
impl<I: FromRemoteObj + ?Sized> Clone for RemoteObjRef<I> {
fn clone(&self) -> Self {
// non None
// Clone is a method in the RemoteObjRef structure.
// T in RemoteObjRef<T>implements the trait FromRemoteObj,
// so self.0.as_ Object(). unwrap() must be a RemoteObj object that exists
FromRemoteObj::try_from(self.0.as_object().unwrap()).unwrap()
}
}

View File

@ -17,9 +17,9 @@
use std::ptr;
use crate::{
ipc_binding, IRemoteObj, DeathRecipient, Result,
ipc_binding, IRemoteObj, DeathRecipient, IpcResult, IpcStatusCode,
MsgParcel, BorrowedMsgParcel, AsRawPtr, parcel::on_string16_writer,
parcel::vec_u16_to_string,
parcel::vec_u16_to_string, parse_status_code,
};
use crate::ipc_binding::{CRemoteObject, CDeathRecipient};
use crate::parcel::parcelable::{Serialize, Deserialize, allocate_vec_with_buffer};
@ -55,7 +55,7 @@ impl RemoteObj {
}
impl IRemoteObj for RemoteObj {
fn send_request(&self, code: u32, data: &MsgParcel, is_async: bool) -> Result<MsgParcel> {
fn send_request(&self, code: u32, data: &MsgParcel, is_async: bool) -> IpcResult<MsgParcel> {
// SAFETY:
unsafe {
let mut reply = MsgParcel::new().expect("create reply MsgParcel not success");
@ -64,7 +64,7 @@ impl IRemoteObj for RemoteObj {
if result == 0 {
Ok(reply)
} else {
Err(result)
Err(parse_status_code(result))
}
}
}
@ -108,7 +108,7 @@ impl IRemoteObj for RemoteObj {
}
}
fn interface_descriptor(&self) -> Result<String> {
fn interface_descriptor(&self) -> IpcResult<String> {
let mut vec: Option<Vec<u16>> = None;
let ok_status = unsafe {
// SAFETY:
@ -121,26 +121,26 @@ impl IRemoteObj for RemoteObj {
if ok_status {
vec_u16_to_string(vec)
} else {
Err(-1)
Err(IpcStatusCode::Failed)
}
}
}
impl Serialize for RemoteObj {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
let ret = unsafe {
ipc_binding::CParcelWriteRemoteObject(parcel.as_mut_raw(), self.as_inner())
};
if ret {
Ok(())
} else {
Err(-1)
Err(IpcStatusCode::Failed)
}
}
}
impl Deserialize for RemoteObj {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> Result<Self> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self> {
// Safety: `Parcel` always contains a valid pointer to an
// `AParcel`. We pass a valid, mutable pointer to `val`, a
// literal of type `$ty`, and `$read_fn` will write the
@ -151,7 +151,7 @@ impl Deserialize for RemoteObj {
if let Some(x) = object {
Ok(x)
} else {
Err(-1)
Err(IpcStatusCode::Failed)
}
}
}

View File

@ -23,7 +23,7 @@ mod process;
mod ashmem;
// Export types of this crate
pub use crate::errors::{Result, result_status};
pub use crate::errors::{IpcResult, status_result, IpcStatusCode, parse_status_code};
pub use crate::ipc::{
IRemoteBroker, IRemoteObj, IRemoteStub, FromRemoteObj, RemoteObjRef,
remote_obj::RemoteObj, remote_obj::death_recipient::DeathRecipient,

View File

@ -21,7 +21,7 @@ pub use types::vec_u16_to_string;
pub use types::vec_to_string;
pub use parcelable::allocate_vec_with_buffer;
use crate::{ipc_binding, Result};
use crate::{ipc_binding, IpcResult, IpcStatusCode};
use crate::ipc_binding::{CParcel};
use std::marker::PhantomData;
use std::mem::{ManuallyDrop,MaybeUninit};
@ -128,7 +128,7 @@ pub trait IMsgParcel: AsRawPtr<CParcel> {
}
/// Read a sized bytes stream from parcel
fn read_buffer(&self, len: u32) -> Result<Vec<u8>> {
fn read_buffer(&self, len: u32) -> IpcResult<Vec<u8>> {
let mut buffer: Vec<MaybeUninit<u8>> = Vec::with_capacity(len as usize);
// SAFETY: this is safe because the vector contains MaybeUninit elements which can be uninitialized
unsafe{
@ -148,7 +148,7 @@ pub trait IMsgParcel: AsRawPtr<CParcel> {
std::mem::transmute(v)
}
let buffer = unsafe { transmute_vec(buffer) };
if ok_status { Ok(buffer) } else { Err(-1) }
if ok_status { Ok(buffer) } else { Err(IpcStatusCode::Failed) }
}
/// Write a large bytes stream into parcel
@ -161,12 +161,12 @@ pub trait IMsgParcel: AsRawPtr<CParcel> {
}
/// Read a big bytes stream from parcel
fn read_raw_data(&self, len: u32) -> Result<RawData> {
fn read_raw_data(&self, len: u32) -> IpcResult<RawData> {
let raw_data_ptr = unsafe {
ipc_binding::CParcelReadRawData(self.as_raw(), len)
};
if raw_data_ptr.is_null() {
Err(-1)
Err(IpcStatusCode::Failed)
} else {
Ok(RawData::new(raw_data_ptr, len))
}
@ -234,9 +234,9 @@ impl RawData{
/// The caller should ensure that the u8 slice can be
/// correctly converted to other rust types
pub fn read(&self, start: u32, len: u32) -> Result<&[u8]> {
pub fn read(&self, start: u32, len: u32) -> IpcResult<&[u8]> {
if len == 0 || len > self.len || start >= self.len || (start + len) > self.len {
return Err(-1);
return Err(IpcStatusCode::Failed);
}
let data_ptr = unsafe {
@ -253,7 +253,7 @@ impl RawData{
Ok(slice::from_raw_parts::<u8>(data_ptr, len as usize))
}
} else {
Err(-1)
Err(IpcStatusCode::Failed)
}
}
}
@ -385,24 +385,24 @@ unsafe impl<'a> AsRawPtr<CParcel> for BorrowedMsgParcel<'a> {
impl MsgParcel {
/// Read a data object which implements the Deserialize trait from MsgParcel
pub fn read<D: Deserialize>(&self) -> Result<D> {
pub fn read<D: Deserialize>(&self) -> IpcResult<D> {
self.borrowed_ref().read()
}
/// Write a data object which implements the Serialize trait to MsgParcel
pub fn write<S: Serialize + ?Sized>(&mut self, parcelable: &S) -> Result<()> {
pub fn write<S: Serialize + ?Sized>(&mut self, parcelable: &S) -> IpcResult<()> {
self.borrowed().write(parcelable)
}
}
impl<'a> BorrowedMsgParcel<'a> {
/// Read a data object which implements the Deserialize trait from BorrowedMsgParcel
pub fn read<D: Deserialize>(&self) -> Result<D> {
pub fn read<D: Deserialize>(&self) -> IpcResult<D> {
D::deserialize(self)
}
/// Write a data object which implements the Serialize trait to BorrowedMsgParcel
pub fn write<S: Serialize + ?Sized>(&mut self, parcelable: &S) -> Result<()> {
pub fn write<S: Serialize + ?Sized>(&mut self, parcelable: &S) -> IpcResult<()> {
parcelable.serialize(self)
}
}

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
use crate::{Result, result_status, BorrowedMsgParcel, ipc_binding, AsRawPtr};
use crate::{IpcResult, IpcStatusCode, status_result, BorrowedMsgParcel, ipc_binding, AsRawPtr};
use std::mem::MaybeUninit;
use std::ffi::{c_void, c_ulong};
use std::ptr;
@ -26,14 +26,14 @@ use std::ptr;
/// struct Year(i64);
///
/// impl Serialize for Year {
/// fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
/// fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
/// parcel::write(self.0);
/// }
/// }
/// ```
pub trait Serialize {
/// Serialize Self to BorrowedMsgParcel
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()>;
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()>;
}
/// Implement `Deserialize` trait to deserialize a custom MsgParcel.
@ -44,7 +44,7 @@ pub trait Serialize {
/// struct Year(i64);
///
/// impl Deserialize for Year {
/// fn deserialize(parcel: &mut BorrowedMsgParcel<'_>) -> Result<Self> {
/// fn deserialize(parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<Self> {
/// let i = parcel::read::<i64>(parcel);
/// Ok(Year(i))
/// }
@ -52,7 +52,7 @@ pub trait Serialize {
/// ```
pub trait Deserialize: Sized {
/// Deserialize an instance from the given [`Parcel`].
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> Result<Self>;
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self>;
}
pub const NULL_FLAG : i32 = 0;
@ -61,7 +61,7 @@ pub const NON_NULL_FLAG : i32 = 1;
/// Define trait function for Option<T> which T must implements the trait Serialize.
pub trait SerOption: Serialize {
/// Serialize the Option<T>
fn ser_option(this: Option<&Self>, parcel: &mut BorrowedMsgParcel<'_>) -> Result<(), > {
fn ser_option(this: Option<&Self>, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<(), > {
if let Some(inner) = this {
parcel.write(&NON_NULL_FLAG)?;
parcel.write(inner)
@ -74,7 +74,7 @@ pub trait SerOption: Serialize {
/// Define trait function for Option<T> which T must implements the trait Deserialize.
pub trait DeOption: Deserialize {
/// Deserialize the Option<T>
fn de_option(parcel: &BorrowedMsgParcel<'_>) -> Result<Option<Self>> {
fn de_option(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Option<Self>> {
let null: i32 = parcel.read()?;
if null == NULL_FLAG {
Ok(None)
@ -131,7 +131,7 @@ unsafe extern "C" fn allocate_vec<T>(
// a few special ones like `readByteArray` for `u8`.
pub trait SerArray: Serialize + Sized {
/// Default array serialize implement.
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
let ret = unsafe {
// SAFETY: Safe FFI, slice will always be a safe pointer to pass.
ipc_binding::CParcelWriteParcelableArray(
@ -141,7 +141,7 @@ pub trait SerArray: Serialize + Sized {
ser_element::<Self>,
)
};
result_status::<()>(ret, ())
status_result::<()>(ret as i32, ())
}
}
@ -175,7 +175,7 @@ pub(crate) unsafe extern "C" fn ser_element<T: Serialize>(
/// but can be overridden for custom implementations like `readByteArray`.
pub trait DeArray: Deserialize {
/// Deserialize an array of type from the given parcel.
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> Result<Option<Vec<Self>>> {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Option<Vec<Self>>> {
let mut vec: Option<Vec<MaybeUninit<Self>>> = None;
let ok_status = unsafe {
// SAFETY: Safe FFI, vec is the correct opaque type expected by
@ -200,7 +200,7 @@ pub trait DeArray: Deserialize {
};
Ok(vec)
} else {
Err(-1)
Err(IpcStatusCode::Failed)
}
}
}

View File

@ -47,4 +47,7 @@ pub use self::strings::vec_to_string;
use crate::parcel::parcelable::*;
use std::ffi::{c_char, c_void};
use crate::{ipc_binding, BorrowedMsgParcel, AsRawPtr, result_status, Result, SerOption, DeOption};
use crate::{
ipc_binding, BorrowedMsgParcel, AsRawPtr, status_result,
IpcResult, IpcStatusCode, SerOption, DeOption
};

View File

@ -19,29 +19,28 @@ use std::mem::MaybeUninit;
impl_serde_option_for_parcelable!(bool);
impl Serialize for bool {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
unsafe {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
let ret = ipc_binding::CParcelWriteBool(parcel.as_mut_raw(), *self);
result_status::<()>(ret, ())
status_result::<()>(ret as i32, ())
}
}
}
impl Deserialize for bool {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> Result<Self> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self> {
let mut val = Self::default();
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
let ret = unsafe {
ipc_binding::CParcelReadBool(parcel.as_raw(), &mut val)
};
result_status::<bool>(ret, val)
status_result::<bool>(ret as i32, val)
}
}
impl SerArray for bool {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
let ret = unsafe {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
// If the slice is > 0 length, `slice.as_ptr()` will be a
@ -55,12 +54,12 @@ impl SerArray for bool {
slice.len().try_into().unwrap(),
)
};
result_status::<()>(ret, ())
status_result::<()>(ret as i32, ())
}
}
impl DeArray for bool {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> Result<Option<Vec<Self>>> {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Option<Vec<Self>>> {
let mut vec: Option<Vec<MaybeUninit<Self>>> = None;
let ok_status = unsafe {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
@ -83,7 +82,7 @@ impl DeArray for bool {
};
Ok(vec)
} else {
Err(-1)
Err(IpcStatusCode::Failed)
}
}
}

View File

@ -16,25 +16,25 @@
use super::*;
impl<T: Serialize> Serialize for Box<T> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
Serialize::serialize(&**self, parcel)
}
}
impl<T: Deserialize> Deserialize for Box<T> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> Result<Self> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self> {
Deserialize::deserialize(parcel).map(Box::new)
}
}
impl<T: SerOption> SerOption for Box<T> {
fn ser_option(this: Option<&Self>, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn ser_option(this: Option<&Self>, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
SerOption::ser_option(this.map(|inner| &**inner), parcel)
}
}
impl<T: DeOption> DeOption for Box<T> {
fn de_option(parcel: &BorrowedMsgParcel<'_>) -> Result<Option<Self>> {
fn de_option(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Option<Self>> {
DeOption::de_option(parcel).map(|t| t.map(Box::new))
}
}

View File

@ -16,14 +16,14 @@
use super::*;
impl<T: SerArray, const N: usize> Serialize for [T; N] {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
// forwards to T::serialize_array.
SerArray::ser_array(self, parcel)
}
}
impl<T: SerArray, const N: usize> SerOption for [T; N] {
fn ser_option(this: Option<&Self>, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn ser_option(this: Option<&Self>, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
SerOption::ser_option(this.map(|arr| &arr[..]), parcel)
}
}
@ -31,18 +31,18 @@ impl<T: SerArray, const N: usize> SerOption for [T; N] {
impl<T: SerArray, const N: usize> SerArray for [T; N] {}
impl<T: DeArray, const N: usize> Deserialize for [T; N] {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> Result<Self> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self> {
let vec = DeArray::de_array(parcel)
.transpose()
.unwrap_or(Err(-1))?;
vec.try_into().or(Err(-1))
.unwrap_or(Err(IpcStatusCode::Failed))?;
vec.try_into().or(Err(IpcStatusCode::Failed))
}
}
impl<T: DeArray, const N: usize> DeOption for [T; N] {
fn de_option(parcel: &BorrowedMsgParcel<'_>) -> Result<Option<Self>> {
fn de_option(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Option<Self>> {
let vec = DeArray::de_array(parcel)?;
vec.map(|v| v.try_into().or(Err(-1))).transpose()
vec.map(|v| v.try_into().or(Err(IpcStatusCode::Failed))).transpose()
}
}

View File

@ -14,7 +14,7 @@
*/
use super::*;
use crate::{ipc_binding, BorrowedMsgParcel, AsRawPtr, result_status, Result};
use crate::{ipc_binding, BorrowedMsgParcel, AsRawPtr, status_result, IpcResult, IpcStatusCode};
use std::fs::File;
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
@ -73,18 +73,18 @@ impl PartialEq for FileDesc {
impl Eq for FileDesc {}
impl Serialize for FileDesc {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
let fd = self.0.as_raw_fd();
let ret = unsafe {
// SAFETY: `parcel` always contains a valid pointer to an `CParcel`.
ipc_binding::CParcelWriteFileDescriptor(parcel.as_mut_raw(), fd)
};
result_status::<()>(ret, ())
status_result::<()>(ret as i32, ())
}
}
impl SerOption for FileDesc {
fn ser_option(this: Option<&Self>, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn ser_option(this: Option<&Self>, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
if let Some(f) = this {
f.serialize(parcel)
} else {
@ -94,13 +94,13 @@ impl SerOption for FileDesc {
// descriptor to signify serializing a null file descriptor.
ipc_binding::CParcelWriteFileDescriptor(parcel.as_mut_raw(), -1i32)
};
result_status::<()>(ret, ())
status_result::<()>(ret as i32, ())
}
}
}
impl DeOption for FileDesc {
fn de_option(parcel: &BorrowedMsgParcel<'_>) -> Result<Option<Self>> {
fn de_option(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Option<Self>> {
let mut fd = -1i32;
let ok_status = unsafe {
// SAFETY: `parcel` always contains a valid pointer to an `CParcel`.
@ -117,7 +117,7 @@ impl DeOption for FileDesc {
if ok_status{
if fd < 0 {
error!(LOG_LABEL, "file descriptor is invalid from native");
Err(-1)
Err(IpcStatusCode::Failed)
} else {
let file = unsafe {
// SAFETY: At this point, we know that the file descriptor was
@ -129,15 +129,15 @@ impl DeOption for FileDesc {
}
} else {
error!(LOG_LABEL, "read file descriptor failed from native");
Err(-1)
Err(IpcStatusCode::Failed)
}
}
}
impl Deserialize for FileDesc {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> Result<Self> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self> {
Deserialize::deserialize(parcel)
.transpose()
.unwrap_or(Err(-1))
.unwrap_or(Err(IpcStatusCode::Failed))
}
}

View File

@ -14,7 +14,7 @@
*/
use super::*;
use crate::{ipc_binding, BorrowedMsgParcel, Result, result_status, AsRawPtr};
use crate::{ipc_binding, BorrowedMsgParcel, IpcResult, IpcStatusCode, status_result, AsRawPtr};
use std::mem::MaybeUninit;
impl_serde_option_for_parcelable!(i8, u8, i16, u16, i32, u32, i64, u64, f32,f64);
@ -23,63 +23,63 @@ impl_serde_option_for_parcelable!(i8, u8, i16, u16, i32, u32, i64, u64, f32,f64)
/// i8 && u8
impl Serialize for i8 {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
let ret = unsafe {
ipc_binding::CParcelWriteInt8(parcel.as_mut_raw(), *self)
};
result_status::<()>(ret, ())
status_result::<()>(ret as i32, ())
}
}
impl Deserialize for i8 {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> Result<Self> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self> {
let mut val = Self::default();
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
let ret = unsafe {
ipc_binding::CParcelReadInt8(parcel.as_raw(), &mut val)
};
result_status::<i8>(ret, val)
status_result::<i8>(ret as i32, val)
}
}
// u8 -> i8
impl Serialize for u8 {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
(*self as i8).serialize(parcel)
}
}
// i8 -> u8
impl Deserialize for u8 {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> Result<Self> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self> {
i8::deserialize(parcel).map(|v| v as u8)
}
}
/// i16 && u16
impl Serialize for i16 {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
let ret = unsafe {
ipc_binding::CParcelWriteInt16(parcel.as_mut_raw(), *self)
};
result_status::<()>(ret, ())
status_result::<()>(ret as i32, ())
}
}
impl Deserialize for i16 {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> Result<Self> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self> {
let mut val = Self::default();
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
let ret = unsafe {
ipc_binding::CParcelReadInt16(parcel.as_raw(), &mut val)
};
result_status::<i16>(ret, val)
status_result::<i16>(ret as i32, val)
}
}
impl SerArray for i8 {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
let ret = unsafe {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
// If the slice is > 0 length, `slice.as_ptr()` will be a
@ -93,12 +93,12 @@ impl SerArray for i8 {
slice.len().try_into().unwrap(),
)
};
result_status::<()>(ret, ())
status_result::<()>(ret as i32, ())
}
}
impl DeArray for i8 {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> Result<Option<Vec<Self>>> {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Option<Vec<Self>>> {
let mut vec: Option<Vec<MaybeUninit<Self>>> = None;
let ok_status = unsafe {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
@ -121,13 +121,13 @@ impl DeArray for i8 {
};
Ok(vec)
} else {
Err(-1)
Err(IpcStatusCode::Failed)
}
}
}
impl SerArray for u8 {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
// SAFETY:
let slice = unsafe {std::slice::from_raw_parts(slice.as_ptr() as *const i8, slice.len()) };
i8::ser_array(slice, parcel)
@ -135,7 +135,7 @@ impl SerArray for u8 {
}
impl DeArray for u8 {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> Result<Option<Vec<Self>>> {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Option<Vec<Self>>> {
i8::de_array(parcel).map(|v|
v.map(|mut v| v.iter_mut().map(|i| *i as u8).collect())
)
@ -143,7 +143,7 @@ impl DeArray for u8 {
}
impl SerArray for i16 {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
let ret = unsafe {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
// If the slice is > 0 length, `slice.as_ptr()` will be a
@ -157,12 +157,12 @@ impl SerArray for i16 {
slice.len().try_into().unwrap()
)
};
result_status::<()>(ret, ())
status_result::<()>(ret as i32, ())
}
}
impl DeArray for i16 {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> Result<Option<Vec<Self>>> {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Option<Vec<Self>>> {
let mut vec: Option<Vec<MaybeUninit<Self>>> = None;
let ok_status = unsafe {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
@ -185,13 +185,13 @@ impl DeArray for i16 {
};
Ok(vec)
} else {
Err(-1)
Err(IpcStatusCode::Failed)
}
}
}
impl SerArray for u16 {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
// SAFETY:
let slice = unsafe {std::slice::from_raw_parts(slice.as_ptr() as *const i16, slice.len()) };
i16::ser_array(slice, parcel)
@ -199,7 +199,7 @@ impl SerArray for u16 {
}
impl DeArray for u16 {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> Result<Option<Vec<Self>>> {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Option<Vec<Self>>> {
i16::de_array(parcel).map(|v|
v.map(|mut v| v.iter_mut().map(|i| *i as u16).collect())
)
@ -207,41 +207,41 @@ impl DeArray for u16 {
}
impl Serialize for u16 {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
(*self as i16).serialize(parcel)
}
}
impl Deserialize for u16 {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> Result<Self> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self> {
i16::deserialize(parcel).map(|v| v as u16)
}
}
/// i32 && u32
impl Serialize for i32 {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
let ret = unsafe {
ipc_binding::CParcelWriteInt32(parcel.as_mut_raw(), *self)
};
result_status::<()>(ret, ())
status_result::<()>(ret as i32, ())
}
}
impl Deserialize for i32 {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> Result<Self> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self> {
let mut val = Self::default();
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
let ret = unsafe {
ipc_binding::CParcelReadInt32(parcel.as_raw(), &mut val)
};
result_status::<i32>(ret, val)
status_result::<i32>(ret as i32, val)
}
}
impl SerArray for i32 {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
let ret = unsafe {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
// If the slice is > 0 length, `slice.as_ptr()` will be a
@ -255,12 +255,12 @@ impl SerArray for i32 {
slice.len().try_into().unwrap(),
)
};
result_status::<()>(ret, ())
status_result::<()>(ret as i32, ())
}
}
impl DeArray for i32 {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> Result<Option<Vec<Self>>> {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Option<Vec<Self>>> {
let mut vec: Option<Vec<MaybeUninit<Self>>> = None;
let ok_status = unsafe {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
@ -283,13 +283,13 @@ impl DeArray for i32 {
};
Ok(vec)
} else {
Err(-1)
Err(IpcStatusCode::Failed)
}
}
}
impl SerArray for u32 {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
// SAFETY:
let slice = unsafe {std::slice::from_raw_parts(slice.as_ptr() as *const i32, slice.len()) };
i32::ser_array(slice, parcel)
@ -297,7 +297,7 @@ impl SerArray for u32 {
}
impl DeArray for u32 {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> Result<Option<Vec<Self>>> {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Option<Vec<Self>>> {
i32::de_array(parcel).map(|v|
v.map(|mut v| v.iter_mut().map(|i| *i as u32).collect())
)
@ -305,41 +305,41 @@ impl DeArray for u32 {
}
impl Serialize for u32 {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
(*self as i32).serialize(parcel)
}
}
impl Deserialize for u32 {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> Result<Self> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self> {
i32::deserialize(parcel).map(|v| v as u32)
}
}
/// i64 && u64
impl Serialize for i64 {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
let ret = unsafe {
ipc_binding::CParcelWriteInt64(parcel.as_mut_raw(), *self)
};
result_status::<()>(ret, ())
status_result::<()>(ret as i32, ())
}
}
impl Deserialize for i64 {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> Result<Self> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self> {
let mut val = Self::default();
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
let ret = unsafe {
ipc_binding::CParcelReadInt64(parcel.as_raw(), &mut val)
};
result_status::<i64>(ret, val)
status_result::<i64>(ret as i32, val)
}
}
impl SerArray for i64 {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
let ret = unsafe {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
// If the slice is > 0 length, `slice.as_ptr()` will be a
@ -353,12 +353,12 @@ impl SerArray for i64 {
slice.len().try_into().unwrap()
)
};
result_status::<()>(ret, ())
status_result::<()>(ret as i32, ())
}
}
impl DeArray for i64 {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> Result<Option<Vec<Self>>> {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Option<Vec<Self>>> {
let mut vec: Option<Vec<MaybeUninit<Self>>> = None;
let ok_status = unsafe {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
@ -381,13 +381,13 @@ impl DeArray for i64 {
};
Ok(vec)
} else {
Err(-1)
Err(IpcStatusCode::Failed)
}
}
}
impl SerArray for u64 {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
// SAFETY:
let slice = unsafe {std::slice::from_raw_parts(slice.as_ptr() as *const i64, slice.len()) };
i64::ser_array(slice, parcel)
@ -395,7 +395,7 @@ impl SerArray for u64 {
}
impl DeArray for u64 {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> Result<Option<Vec<Self>>> {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Option<Vec<Self>>> {
i64::de_array(parcel).map(|v|
v.map(|mut v| v.iter_mut().map(|i| *i as u64).collect())
)
@ -403,41 +403,41 @@ impl DeArray for u64 {
}
impl Serialize for u64 {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
(*self as i64).serialize(parcel)
}
}
impl Deserialize for u64 {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> Result<Self> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self> {
i64::deserialize(parcel).map(|v| v as u64)
}
}
/// f32
impl Serialize for f32 {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
let ret = unsafe {
ipc_binding::CParcelWriteFloat(parcel.as_mut_raw(), *self)
};
result_status::<()>(ret, ())
status_result::<()>(ret as i32, ())
}
}
impl Deserialize for f32 {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> Result<Self> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self> {
let mut val = Self::default();
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
let ret = unsafe {
ipc_binding::CParcelReadFloat(parcel.as_raw(), &mut val)
};
result_status::<f32>(ret, val)
status_result::<f32>(ret as i32, val)
}
}
impl SerArray for f32 {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
let ret = unsafe {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
// If the slice is > 0 length, `slice.as_ptr()` will be a
@ -448,15 +448,15 @@ impl SerArray for f32 {
ipc_binding::CParcelWriteFloatArray(
parcel.as_mut_raw(),
slice.as_ptr(),
slice.len().try_into().or(Err(-1))?,
slice.len().try_into().or(Err(IpcStatusCode::Failed))?,
)
};
result_status::<()>(ret, ())
status_result::<()>(ret as i32, ())
}
}
impl DeArray for f32 {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> Result<Option<Vec<Self>>> {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Option<Vec<Self>>> {
let mut vec: Option<Vec<MaybeUninit<Self>>> = None;
let ok_status = unsafe {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
@ -479,35 +479,35 @@ impl DeArray for f32 {
};
Ok(vec)
} else {
Err(-1)
Err(IpcStatusCode::Failed)
}
}
}
/// f64
impl Serialize for f64 {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
let ret = unsafe {
ipc_binding::CParcelWriteDouble(parcel.as_mut_raw(), *self)
};
result_status::<()>(ret, ())
status_result::<()>(ret as i32, ())
}
}
impl Deserialize for f64 {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> Result<Self> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self> {
let mut val = Self::default();
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
let ret = unsafe {
ipc_binding::CParcelReadDouble(parcel.as_raw(), &mut val)
};
result_status::<f64>(ret, val)
status_result::<f64>(ret as i32, val)
}
}
impl SerArray for f64 {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
let ret = unsafe {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
// If the slice is > 0 length, `slice.as_ptr()` will be a
@ -521,12 +521,12 @@ impl SerArray for f64 {
slice.len().try_into().unwrap()
)
};
result_status::<()>(ret, ())
status_result::<()>(ret as i32, ())
}
}
impl DeArray for f64 {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> Result<Option<Vec<Self>>> {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Option<Vec<Self>>> {
let mut vec: Option<Vec<MaybeUninit<Self>>> = None;
let ok_status = unsafe {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
@ -549,7 +549,7 @@ impl DeArray for f64 {
};
Ok(vec)
} else {
Err(-1)
Err(IpcStatusCode::Failed)
}
}
}

View File

@ -14,7 +14,7 @@
*/
use super::*;
use crate::{ipc_binding, BorrowedMsgParcel, Result, AsRawPtr, result_status};
use crate::{ipc_binding, BorrowedMsgParcel, IpcResult, IpcStatusCode, AsRawPtr, status_result};
use std::convert::TryInto;
use std::ffi::{CString, c_char};
use hilog_rust::{error, hilog, HiLogLabel, LogType};
@ -41,7 +41,7 @@ impl InterfaceToken {
}
impl Serialize for InterfaceToken {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
let token = &self.0;
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
let ret = unsafe {
@ -50,12 +50,12 @@ impl Serialize for InterfaceToken {
token.as_ptr() as *const c_char,
token.as_bytes().len().try_into().unwrap()
)};
result_status::<()>(ret, ())
status_result::<()>(ret as i32, ())
}
}
impl Deserialize for InterfaceToken {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> Result<Self> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self> {
let mut vec: Option<Vec<u8>> = None;
let ok_status = unsafe {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
@ -77,11 +77,11 @@ impl Deserialize for InterfaceToken {
Ok(Self(val))
} else {
error!(LOG_LABEL, "convert interface token to String fail");
Err(-1)
Err(IpcStatusCode::Failed)
}
}else{
error!(LOG_LABEL, "read interface token from native fail");
Err(-1)
Err(IpcStatusCode::Failed)
}
}
}

View File

@ -16,13 +16,13 @@
use super::*;
impl<T: SerOption> Serialize for Option<T> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
SerOption::ser_option(self.as_ref(), parcel)
}
}
impl<T: DeOption> Deserialize for Option<T> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> Result<Self> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self> {
DeOption::de_option(parcel)
}
}

View File

@ -14,17 +14,17 @@
*/
use super::*;
use crate::{BorrowedMsgParcel, Result};
use crate::{BorrowedMsgParcel, IpcResult};
// We need these to support Option<&T> for all T
impl<T: Serialize + ?Sized> Serialize for &T {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
Serialize::serialize(*self, parcel)
}
}
impl<T: SerOption + ?Sized> SerOption for &T {
fn ser_option(this: Option<&Self>, parcel: &mut BorrowedMsgParcel<'_>) -> Result<(), > {
fn ser_option(this: Option<&Self>, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<(), > {
SerOption::ser_option(this.copied(), parcel)
}
}

View File

@ -14,16 +14,16 @@
*/
use super::*;
use crate::{BorrowedMsgParcel, Result};
use crate::{BorrowedMsgParcel, IpcResult};
impl<T: SerArray> Serialize for [T] {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
SerArray::ser_array(self, parcel)
}
}
impl<T: SerArray> SerOption for [T] {
fn ser_option(this: Option<&Self>, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn ser_option(this: Option<&Self>, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
if let Some(v) = this {
SerArray::ser_array(v, parcel)
} else {

View File

@ -14,7 +14,7 @@
*/
use super::*;
use crate::{ipc_binding, BorrowedMsgParcel, Result, result_status, AsRawPtr};
use crate::{ipc_binding, BorrowedMsgParcel, IpcResult, IpcStatusCode, status_result, AsRawPtr};
use std::convert::TryInto;
use std::ffi::{CString};
use hilog_rust::{error, hilog, HiLogLabel, LogType};
@ -41,7 +41,7 @@ impl String16 {
}
impl Serialize for String16 {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
let string = &self.0;
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
let ret = unsafe {
@ -50,12 +50,12 @@ impl Serialize for String16 {
string.as_ptr() as *const c_char,
string.as_bytes().len().try_into().unwrap()
)};
result_status::<()>(ret, ())
status_result::<()>(ret as i32, ())
}
}
impl Deserialize for String16 {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> Result<Self> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self> {
let mut vec: Option<Vec<u8>> = None;
let ok_status = unsafe {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
@ -77,11 +77,11 @@ impl Deserialize for String16 {
Ok(Self(val))
} else {
error!(LOG_LABEL, "convert native string16 to String fail");
Err(-1)
Err(IpcStatusCode::Failed)
}
} else {
error!(LOG_LABEL, "read string16 from native fail");
Err(-1)
Err(IpcStatusCode::Failed)
}
}
}

View File

@ -15,7 +15,7 @@
use super::*;
use crate::{
ipc_binding, BorrowedMsgParcel, Result, result_status,
ipc_binding, BorrowedMsgParcel, IpcResult, IpcStatusCode, status_result,
AsRawPtr
};
use std::convert::TryInto;
@ -34,25 +34,25 @@ impl SerOption for String {}
impl DeOption for String {}
impl Serialize for str {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
let ret = unsafe {
ipc_binding::CParcelWriteString(
parcel.as_mut_raw(),
self.as_ptr() as *const c_char,
self.as_bytes().len().try_into().unwrap()
)};
result_status::<()>(ret, ())
status_result::<()>(ret as i32, ())
}
}
impl Serialize for String {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
self.as_str().serialize(parcel)
}
}
impl Deserialize for String {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> Result<Self> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self> {
let mut vec: Option<Vec<u8>> = None;
let ok_status = unsafe {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
@ -66,13 +66,13 @@ impl Deserialize for String {
if ok_status {
vec_to_string(vec)
} else {
Err(-1)
Err(IpcStatusCode::Failed)
}
}
}
impl SerArray for &str {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
let ret = unsafe {
// SAFETY:
ipc_binding::CParcelWriteStringArray(
@ -82,12 +82,12 @@ impl SerArray for &str {
on_str_writer,
)
};
result_status::<()>(ret, ())
status_result::<()>(ret as i32, ())
}
}
impl SerArray for String {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn ser_array(slice: &[Self], parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
let ret = unsafe {
// SAFETY:
ipc_binding::CParcelWriteStringArray(
@ -97,12 +97,12 @@ impl SerArray for String {
on_string_writer,
)
};
result_status::<()>(ret, ())
status_result::<()>(ret as i32, ())
}
}
impl DeArray for String {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> Result<Option<Vec<Self>>> {
fn de_array(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Option<Vec<Self>>> {
let mut vec: Option<Vec<MaybeUninit<Self>>> = None;
let ok_status = unsafe {
// SAFETY: `parcel` always contains a valid pointer to a `CParcel`
@ -124,7 +124,7 @@ impl DeArray for String {
Ok(vec)
} else {
error!(LOG_LABEL, "read string from native fail");
Err(-1)
Err(IpcStatusCode::Failed)
}
}
}
@ -236,31 +236,31 @@ unsafe extern "C" fn on_string_reader(
true
}
pub fn vec_to_string(vec: Option<Vec<u8>>) -> Result<String> {
pub fn vec_to_string(vec: Option<Vec<u8>>) -> IpcResult<String> {
let value = vec.map(|s| {
// The vector includes a null-terminator and
// we don't want the string to be null-terminated for Rust.
String::from_utf8(s).or(Err(-1))
String::from_utf8(s).or(Err(IpcStatusCode::Failed))
});
if let Some(ret) = value {
ret
} else {
error!(LOG_LABEL, "convert vector u8 to String fail");
Err(-1)
Err(IpcStatusCode::Failed)
}
}
pub fn vec_u16_to_string(vec: Option<Vec<u16>>) -> Result<String> {
pub fn vec_u16_to_string(vec: Option<Vec<u16>>) -> IpcResult<String> {
let value = vec.map(|s| {
// The vector includes a null-terminator and
// we don't want the string to be null-terminated for Rust.
let slice = &s[..];
String::from_utf16(slice).or(Err(-1))
String::from_utf16(slice).or(Err(IpcStatusCode::Failed))
});
if let Some(ret) = value {
ret
} else {
error!(LOG_LABEL, "convert vector u16 to String fail");
Err(-1)
Err(IpcStatusCode::Failed)
}
}

View File

@ -16,27 +16,27 @@
use super::*;
impl<T: SerArray> Serialize for Vec<T> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
SerArray::ser_array(&self[..], parcel)
}
}
impl<T: SerArray> SerOption for Vec<T> {
fn ser_option(this: Option<&Self>, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn ser_option(this: Option<&Self>, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
SerOption::ser_option(this.map(Vec::as_slice), parcel)
}
}
impl<T: DeArray> Deserialize for Vec<T> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> Result<Self> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self> {
DeArray::de_array(parcel)
.transpose()
.unwrap_or(Err(-1))
.unwrap_or(Err(IpcStatusCode::Failed))
}
}
impl<T: DeArray> DeOption for Vec<T> {
fn de_option(parcel: &BorrowedMsgParcel<'_>) -> Result<Option<Self>> {
fn de_option(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Option<Self>> {
DeArray::de_array(parcel)
}
}

View File

@ -15,7 +15,7 @@
use crate::{
ipc_binding, MsgParcel, RemoteObj, IRemoteObj, InterfaceToken, String16,
Result,
IpcResult, IpcStatusCode, parse_status_code,
};
use crate::parcel::{vec_to_string, allocate_vec_with_buffer,};
use std::ffi::{CString, c_char, c_void};
@ -37,7 +37,7 @@ pub fn get_context_object() -> Option<RemoteObj>
}
/// Add a service to samgr
pub fn add_service(service: &RemoteObj, said: i32) -> Result<()>
pub fn add_service(service: &RemoteObj, said: i32) -> IpcResult<()>
{
let samgr = get_context_object().expect("samgr is not null");
let mut data = MsgParcel::new().expect("MsgParcel is not null");
@ -51,11 +51,11 @@ pub fn add_service(service: &RemoteObj, said: i32) -> Result<()>
let reply = samgr.send_request(3, &data, false)?;
let reply_value: i32 = reply.read()?;
info!(LOG_LABEL, "register service result: {}", reply_value);
if reply_value == 0 { Ok(())} else { Err(reply_value) }
if reply_value == 0 { Ok(())} else { Err(parse_status_code(reply_value)) }
}
/// Get a service proxy from samgr
pub fn get_service(said: i32) -> Result<RemoteObj>
pub fn get_service(said: i32) -> IpcResult<RemoteObj>
{
let samgr = get_context_object().expect("samgr is not null");
let mut data = MsgParcel::new().expect("MsgParcel is not null");
@ -164,7 +164,7 @@ pub fn set_calling_identity(identity: String) -> bool
/// get local device id
#[inline]
pub fn get_local_device_id() -> Result<String>
pub fn get_local_device_id() -> IpcResult<String>
{
let mut vec: Option<Vec<u8>> = None;
let ok_status = unsafe {
@ -178,13 +178,13 @@ pub fn get_local_device_id() -> Result<String>
if ok_status {
vec_to_string(vec)
} else {
Err(-1)
Err(IpcStatusCode::Failed)
}
}
/// get calling device id
#[inline]
pub fn get_calling_device_id() -> Result<String>
pub fn get_calling_device_id() -> IpcResult<String>
{
let mut vec: Option<Vec<u8>> = None;
let ok_status = unsafe {
@ -198,13 +198,13 @@ pub fn get_calling_device_id() -> Result<String>
if ok_status {
vec_to_string(vec)
} else {
Err(-1)
Err(IpcStatusCode::Failed)
}
}
/// reset calling identity
#[inline]
pub fn reset_calling_identity() -> Result<String>
pub fn reset_calling_identity() -> IpcResult<String>
{
let mut vec: Option<Vec<u8>> = None;
let ok_status = unsafe {
@ -218,6 +218,6 @@ pub fn reset_calling_identity() -> Result<String>
if ok_status {
vec_to_string(vec)
} else {
Err(-1)
Err(IpcStatusCode::Failed)
}
}

View File

@ -25,9 +25,9 @@ use std::io::{Read, SeekFrom, Seek};
use ipc_rust::{
FromRemoteObj, DeathRecipient, IRemoteObj, FileDesc, RemoteObjRef,
MsgParcel, String16, InterfaceToken, get_service, get_first_token_id,
get_self_token_id, get_calling_pid, get_calling_uid, IMsgParcel, Result,
get_self_token_id, get_calling_pid, get_calling_uid, IMsgParcel, IpcResult,
RawData, set_max_work_thread, reset_calling_identity, set_calling_identity,
is_local_calling, get_local_device_id, get_calling_device_id,
is_local_calling, get_local_device_id, get_calling_device_id, IpcStatusCode,
};
use ipc_rust::{Serialize, Deserialize, BorrowedMsgParcel, Ashmem};
@ -651,17 +651,17 @@ mod parcel_type_test {
struct Year(i64);
impl Serialize for Year {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> Result<()> {
fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()> {
parcel.write(&self.0)
}
}
impl Deserialize for Year {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> Result<Self> {
fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self> {
let ret = parcel.read::<i64>();
match ret {
Ok(year) => Ok(Year(year)),
Err(_) => Err(-1),
Err(_) => Err(IpcStatusCode::Failed),
}
}
}
@ -722,7 +722,7 @@ mod parcel_type_test {
let ashmem2: Ashmem = parcel.read().expect("read MsgParcel failed");
assert_eq!(ashmem2.map_readonly(), true);
let res: Result<RawData> = ashmem2.read(ashmemString.len() as i32, 0);
let res: IpcResult<RawData> = ashmem2.read(ashmemString.len() as i32, 0);
let ptr = res.unwrap();
let read_string = ptr.read(0, ashmemString.len() as u32);
let res = std::str::from_utf8(read_string.unwrap()).unwrap();

View File

@ -19,10 +19,10 @@ extern crate ipc_rust;
extern crate test_ipc_service;
use ipc_rust::{
IRemoteBroker, join_work_thread, FileDesc, InterfaceToken, Result,
IRemoteBroker, join_work_thread, FileDesc, InterfaceToken, IpcResult,
add_service, get_calling_token_id, get_first_token_id, get_calling_pid,
get_calling_uid, String16, RemoteObj, IRemoteStub, get_local_device_id,
get_calling_device_id,
get_calling_device_id, IpcStatusCode,
};
use test_ipc_service::{ITest, TestStub, IPC_TEST_SERVICE_ID, reverse, IFoo, FooStub, init_access_token};
use std::io::Write;
@ -42,31 +42,31 @@ impl IRemoteBroker for FooService {
pub struct TestService;
impl ITest for TestService {
fn test_sync_transaction(&self, value: i32, delay_time: i32) -> Result<i32> {
fn test_sync_transaction(&self, value: i32, delay_time: i32) -> IpcResult<i32> {
if delay_time > 0 {
thread::sleep(time::Duration::from_millis(delay_time as u64));
}
Ok(reverse(value))
}
fn test_async_transaction(&self, _value: i32, delay_time: i32) -> Result<()> {
fn test_async_transaction(&self, _value: i32, delay_time: i32) -> IpcResult<()> {
if delay_time > 0 {
thread::sleep(time::Duration::from_millis(delay_time as u64));
}
Ok(())
}
fn test_ping_service(&self, service_name: &String16) -> Result<()> {
fn test_ping_service(&self, service_name: &String16) -> IpcResult<()> {
let name = service_name.get_string();
println!("test_ping_service recv service name: {}", name);
if name == TestStub::get_descriptor() {
Ok(())
} else {
Err(-1)
Err(IpcStatusCode::Failed)
}
}
fn test_transact_fd(&self) -> Result<FileDesc> {
fn test_transact_fd(&self) -> IpcResult<FileDesc> {
let path = "/data/test.txt";
let mut value = OpenOptions::new().read(true)
.write(true)
@ -77,20 +77,20 @@ impl ITest for TestService {
Ok(FileDesc::new(value))
}
fn test_transact_string(&self, value: &str) -> Result<i32> {
fn test_transact_string(&self, value: &str) -> IpcResult<i32> {
Ok(value.len() as i32)
}
fn test_get_foo_service(&self) -> Result<RemoteObj> {
fn test_get_foo_service(&self) -> IpcResult<RemoteObj> {
let service = FooStub::new_remote_stub(FooService).expect("create FooService success");
Ok(service.as_object().expect("get a RemoteObj success"))
}
fn echo_interface_token(&self, token: &InterfaceToken) -> Result<InterfaceToken> {
fn echo_interface_token(&self, token: &InterfaceToken) -> IpcResult<InterfaceToken> {
Ok(InterfaceToken::new(&token.get_token()))
}
fn echo_calling_info(&self) -> Result<(u64, u64, u64, u64)> {
fn echo_calling_info(&self) -> IpcResult<(u64, u64, u64, u64)> {
let token_id = get_calling_token_id();
let first_token_id = get_first_token_id();
let pid = get_calling_pid();
@ -98,14 +98,14 @@ impl ITest for TestService {
Ok((token_id, first_token_id, pid, uid))
}
fn test_get_device_id(&self) -> Result<(String, String)> {
fn test_get_device_id(&self) -> IpcResult<(String, String)> {
let local_device_id = get_local_device_id();
let calling_device_id = get_calling_device_id();
if let (Ok(local_id), Ok(calling_id)) = (local_device_id, calling_device_id) {
Ok((local_id, calling_id))
} else {
Err(-1)
Err(IpcStatusCode::Failed)
}
}
}

View File

@ -20,8 +20,8 @@ extern crate ipc_rust;
mod access_token;
use ipc_rust::{
IRemoteBroker, IRemoteObj, RemoteStub, Result,
RemoteObj, define_remote_object, FIRST_CALL_TRANSACTION
IRemoteBroker, IRemoteObj, RemoteStub, IpcResult, IpcStatusCode,
RemoteObj, define_remote_object, FIRST_CALL_TRANSACTION,
};
use ipc_rust::{
MsgParcel, BorrowedMsgParcel, FileDesc, InterfaceToken, String16,
@ -67,8 +67,8 @@ pub enum ITestCode {
}
impl TryFrom<u32> for ITestCode {
type Error = i32;
fn try_from(code: u32) -> Result<Self> {
type Error = IpcStatusCode;
fn try_from(code: u32) -> IpcResult<Self> {
match code {
_ if code == ITestCode::CodeSyncTransaction as u32 => Ok(ITestCode::CodeSyncTransaction),
_ if code == ITestCode::CodeAsyncTransaction as u32 => Ok(ITestCode::CodeAsyncTransaction),
@ -79,7 +79,7 @@ impl TryFrom<u32> for ITestCode {
_ if code == ITestCode::CodeInterfaceToekn as u32 => Ok(ITestCode::CodeInterfaceToekn),
_ if code == ITestCode::CodeCallingInfo as u32 => Ok(ITestCode::CodeCallingInfo),
_ if code == ITestCode::CodeGetDeviceId as u32 => Ok(ITestCode::CodeGetDeviceId),
_ => Err(-1),
_ => Err(IpcStatusCode::Failed),
}
}
}
@ -87,27 +87,27 @@ impl TryFrom<u32> for ITestCode {
/// Function between proxy and stub of ITestService
pub trait ITest: IRemoteBroker {
/// Test sync transaction
fn test_sync_transaction(&self, value: i32, delay_time: i32) -> Result<i32>;
fn test_sync_transaction(&self, value: i32, delay_time: i32) -> IpcResult<i32>;
/// Test async transaction
fn test_async_transaction(&self, value: i32, delay_time: i32) -> Result<()>;
fn test_async_transaction(&self, value: i32, delay_time: i32) -> IpcResult<()>;
/// Test ping service transaction
fn test_ping_service(&self, service_name: &String16) -> Result<()>;
fn test_ping_service(&self, service_name: &String16) -> IpcResult<()>;
/// Test file descriptor transaction
fn test_transact_fd(&self) -> Result<FileDesc>;
fn test_transact_fd(&self) -> IpcResult<FileDesc>;
/// Test string transaction
fn test_transact_string(&self, value: &str) -> Result<i32>;
fn test_transact_string(&self, value: &str) -> IpcResult<i32>;
/// Test get foo service IPC object transaction
fn test_get_foo_service(&self) -> Result<RemoteObj>;
fn test_get_foo_service(&self) -> IpcResult<RemoteObj>;
/// Test interface token transaction
fn echo_interface_token(&self, token: &InterfaceToken) -> Result<InterfaceToken>;
fn echo_interface_token(&self, token: &InterfaceToken) -> IpcResult<InterfaceToken>;
/// Test calling infomation transaction
fn echo_calling_info(&self) -> Result<(u64, u64, u64, u64)>;
fn echo_calling_info(&self) -> IpcResult<(u64, u64, u64, u64)>;
/// Test get device id
fn test_get_device_id(&self) -> Result<(String, String)>;
fn test_get_device_id(&self) -> IpcResult<(String, String)>;
}
fn on_itest_remote_request(stub: &dyn ITest, code: u32, data: &BorrowedMsgParcel,
reply: &mut BorrowedMsgParcel) -> Result<()> {
reply: &mut BorrowedMsgParcel) -> IpcResult<()> {
match code.try_into()? {
ITestCode::CodeSyncTransaction => {
let value: i32 = data.read().expect("should a value");
@ -175,45 +175,45 @@ define_remote_object!(
// Make RemoteStub<TestStub> object can call ITest function directly.
impl ITest for RemoteStub<TestStub> {
fn test_sync_transaction(&self, value: i32, delay_time: i32) -> Result<i32> {
fn test_sync_transaction(&self, value: i32, delay_time: i32) -> IpcResult<i32> {
self.0.test_sync_transaction(value, delay_time)
}
fn test_async_transaction(&self, value: i32, delay_time: i32) -> Result<()> {
fn test_async_transaction(&self, value: i32, delay_time: i32) -> IpcResult<()> {
self.0.test_async_transaction(value, delay_time)
}
fn test_ping_service(&self, service_name: &String16) -> Result<()> {
fn test_ping_service(&self, service_name: &String16) -> IpcResult<()> {
self.0.test_ping_service(service_name)
}
fn test_transact_fd(&self) -> Result<FileDesc> {
fn test_transact_fd(&self) -> IpcResult<FileDesc> {
self.0.test_transact_fd()
}
fn test_transact_string(&self, value: &str) -> Result<i32> {
fn test_transact_string(&self, value: &str) -> IpcResult<i32> {
self.0.test_transact_string(value)
}
fn test_get_foo_service(&self) -> Result<RemoteObj> {
fn test_get_foo_service(&self) -> IpcResult<RemoteObj> {
self.0.test_get_foo_service()
}
fn echo_interface_token(&self, token: &InterfaceToken) -> Result<InterfaceToken> {
fn echo_interface_token(&self, token: &InterfaceToken) -> IpcResult<InterfaceToken> {
self.0.echo_interface_token(token)
}
fn echo_calling_info(&self) -> Result<(u64, u64, u64, u64)> {
fn echo_calling_info(&self) -> IpcResult<(u64, u64, u64, u64)> {
self.0.echo_calling_info()
}
fn test_get_device_id(&self) -> Result<(String, String)> {
fn test_get_device_id(&self) -> IpcResult<(String, String)> {
self.0.test_get_device_id()
}
}
impl ITest for TestProxy {
fn test_sync_transaction(&self, value: i32, delay_time: i32) -> Result<i32> {
fn test_sync_transaction(&self, value: i32, delay_time: i32) -> IpcResult<i32> {
let mut data = MsgParcel::new().expect("MsgParcel should success");
data.write(&value)?;
data.write(&delay_time)?;
@ -223,7 +223,7 @@ impl ITest for TestProxy {
Ok(ret)
}
fn test_async_transaction(&self, value: i32, delay_time: i32) -> Result<()> {
fn test_async_transaction(&self, value: i32, delay_time: i32) -> IpcResult<()> {
let mut data = MsgParcel::new().expect("MsgParcel should success");
data.write(&value)?;
data.write(&delay_time)?;
@ -232,7 +232,7 @@ impl ITest for TestProxy {
Ok(())
}
fn test_ping_service(&self, service_name: &String16) -> Result<()> {
fn test_ping_service(&self, service_name: &String16) -> IpcResult<()> {
let mut data = MsgParcel::new().expect("MsgParcel should success");
data.write(service_name)?;
let _reply = self.remote.send_request(ITestCode::CodePingService as u32,
@ -240,7 +240,7 @@ impl ITest for TestProxy {
Ok(())
}
fn test_transact_fd(&self) -> Result<FileDesc> {
fn test_transact_fd(&self) -> IpcResult<FileDesc> {
let data = MsgParcel::new().expect("MsgParcel should success");
let reply = self.remote.send_request(ITestCode::CodeTransactFd as u32,
&data, false)?;
@ -248,7 +248,7 @@ impl ITest for TestProxy {
Ok(fd)
}
fn test_transact_string(&self, value: &str) -> Result<i32> {
fn test_transact_string(&self, value: &str) -> IpcResult<i32> {
let mut data = MsgParcel::new().expect("MsgParcel should success");
data.write(value).expect("should write string success");
let reply = self.remote.send_request(ITestCode::CodeTransactString as u32,
@ -257,7 +257,7 @@ impl ITest for TestProxy {
Ok(len)
}
fn test_get_foo_service(&self) -> Result<RemoteObj> {
fn test_get_foo_service(&self) -> IpcResult<RemoteObj> {
let data = MsgParcel::new().expect("MsgParcel should success");
let reply = self.remote.send_request(ITestCode::CodeGetFooService as u32,
&data, false)?;
@ -265,7 +265,7 @@ impl ITest for TestProxy {
Ok(service)
}
fn echo_interface_token(&self, token: &InterfaceToken) -> Result<InterfaceToken> {
fn echo_interface_token(&self, token: &InterfaceToken) -> IpcResult<InterfaceToken> {
let mut data = MsgParcel::new().expect("MsgParcel should success");
data.write(token).expect("write token should success");
let reply = self.remote.send_request(ITestCode::CodeInterfaceToekn as u32,
@ -274,7 +274,7 @@ impl ITest for TestProxy {
Ok(echo_value)
}
fn echo_calling_info(&self) -> Result<(u64, u64, u64, u64)> {
fn echo_calling_info(&self) -> IpcResult<(u64, u64, u64, u64)> {
let data = MsgParcel::new().expect("MsgParcel should success");
let reply = self.remote.send_request(ITestCode::CodeCallingInfo as u32,
&data, false)?;
@ -285,7 +285,7 @@ impl ITest for TestProxy {
Ok((token_id, first_token_id, pid, uid))
}
fn test_get_device_id(&self) -> Result<(String, String)> {
fn test_get_device_id(&self) -> IpcResult<(String, String)> {
let data = MsgParcel::new().expect("MsgParcel should success");
let reply = self.remote.send_request(ITestCode::CodeGetDeviceId as u32,
&data, false)?;
@ -300,7 +300,7 @@ pub trait IFoo: IRemoteBroker {
}
fn on_foo_remote_request(_stub: &dyn IFoo, _code: u32, _data: &BorrowedMsgParcel,
_reply: &mut BorrowedMsgParcel) -> Result<()> {
_reply: &mut BorrowedMsgParcel) -> IpcResult<()> {
Ok(())
}