提交 cbaada97 编写于 作者: C chenchong_666

Add rust interface

Signed-off-by: Nchenchong_666 <chenchong57@huawei.com>
上级 4614331e
......@@ -35,7 +35,7 @@ public:
return true;
};
bool IsObjectDead() const;
bool IsObjectDead() const override;
int32_t GetObjectRefCount() override;
......@@ -72,7 +72,7 @@ public:
std::string GetGrantedSessionName();
int GetProto() const;
void WaitForInit();
std::u16string GetInterfaceDescriptor();
std::u16string GetInterfaceDescriptor() override;
private:
void MarkObjectDied();
......
......@@ -59,6 +59,10 @@ public:
virtual bool IsProxyObject() const;
virtual bool IsObjectDead() const;
virtual std::u16string GetInterfaceDescriptor();
virtual bool CheckObjectLegality() const;
virtual bool AddDeathRecipient(const sptr<DeathRecipient> &recipient) = 0;
......
......@@ -61,6 +61,14 @@
"virtual thunk to OHOS::IPCFileDescriptor::Marshalling(OHOS::Parcel&)";
"virtual thunk to OHOS::IPCFileDescriptor::~ipcFileDescriptor()";
"virtual thunk to OHOS::IRemoteObject::Marshalling(OHOS::Parcel&) const";
"virtual thunk to OHOS::IRemoteObject::IsObjectDead() const";
"OHOS::IRemoteObject::IsObjectDead() const";
"OHOS::IPCObjectProxy::IsObjectDead()";
"OHOS::IPCObjectStub::IsObjectDead()";
"virtual thunk to OHOS::IRemoteObject::GetInterfaceDescriptor()";
"OHOS::IRemoteObject::GetInterfaceDescriptor()";
"OHOS::IPCObjectProxy::GetInterfaceDescriptor()";
"OHOS::IPCObjectStub::GetInterfaceDescriptor()";
};
local:
*;
......
......@@ -61,6 +61,9 @@
"OHOS::MessageOption::MessageOption(int, int)";
"OHOS::MessageOption::GetFlags() const";
"OHOS::MessageOption::SetFlags(int)";
"OHOS::MessageParcel::GetRawDataSize() const";
"OHOS::MessageParcel::GetRawDataCapacity() const";
"OHOS::MessageParcel::ClearFileDescriptor()";
"OHOS::IPCSkeleton::GetContextObject()";
"OHOS::IPCSkeleton::GetInstance()";
"OHOS::IPCSkeleton::SetMaxWorkThreadNum(int)";
......@@ -74,6 +77,9 @@
"OHOS::IPCSkeleton::StopWorkThread()";
"OHOS::IPCSkeleton::GetFirstFullTokenID()";
"OHOS::IPCSkeleton::ResetCallingIdentity()";
"OHOS::IPCSkeleton::IsLocalCalling()";
"OHOS::IPCSkeleton::GetLocalDeviceID()";
"OHOS::IPCSkeleton::GetCallingDeviceID()";
"OHOS::PeerHolder::PeerHolder(OHOS::sptr<OHOS::IRemoteObject> const&)";
"OHOS::PeerHolder::Remote()";
"OHOS::IRemoteObject::CheckObjectLegality() const";
......@@ -84,6 +90,14 @@
"OHOS::IRemoteObject::IsProxyObject() const";
"virtual thunk to OHOS::IRemoteObject::Marshalling(OHOS::Parcel&) const";
"OHOS::IPC_SINGLE::IPCThreadSkeleton::JoinWorkThread(int)";
"virtual thunk to OHOS::IRemoteObject::IsObjectDead() const";
"OHOS::IRemoteObject::IsObjectDead() const";
"OHOS::IPCObjectProxy::IsObjectDead()";
"OHOS::IPCObjectStub::IsObjectDead()";
"virtual thunk to OHOS::IRemoteObject::GetInterfaceDescriptor()";
"OHOS::IRemoteObject::GetInterfaceDescriptor()";
"OHOS::IPCObjectProxy::GetInterfaceDescriptor()";
"OHOS::IPCObjectStub::GetInterfaceDescriptor()";
};
local:
*;
......
......@@ -61,7 +61,7 @@ macro_rules! define_remote_object {
}
impl $crate::IRemoteBroker for $proxy {
/// Get RemoteObje object from proxy
/// Get RemoteObject object from proxy
fn as_object(&self) -> Option<$crate::RemoteObj> {
Some(self.remote.clone())
}
......
......@@ -17,9 +17,10 @@ pub mod remote_obj;
pub mod remote_stub;
pub mod macros;
use crate::{BorrowedMsgParcel, MsgParcel, Result, DeathRecipient};
use crate::{BorrowedMsgParcel, MsgParcel, Result, DeathRecipient,};
use std::ops::{Deref};
use std::cmp::Ordering;
use crate::String16;
// Export types of this module
pub use crate::RemoteObj;
......@@ -34,6 +35,18 @@ pub trait IRemoteObj {
/// Remove a death recipient
fn remove_death_recipient(&self, recipient: &mut DeathRecipient) -> bool;
/// Determine whether it is a proxy object
fn is_proxy(&self) -> bool;
/// Dump a service through a string
fn dump(&self, fd: i32, args: &mut Vec<String16>) -> i32;
/// Judge whether the object is dead
fn is_dead(&self) -> bool;
/// get interface descriptor
fn interface_descriptor(&self) -> Result<String>;
}
/// Like C++ IPCObjectStub class, define function for stub object only, like on_remote_request().
......@@ -85,7 +98,7 @@ impl<I: FromRemoteObj + ?Sized> Clone for RemoteObjRef<I> {
}
}
impl<I: FromRemoteObj + ?Sized> Ord for RemoteObjRef<I> {
impl<I: FromRemoteObj + ?Sized> Ord for RemoteObjRef<I> {
fn cmp(&self, other: &Self) -> Ordering {
self.0.as_object().cmp(&other.0.as_object())
}
......
......@@ -18,11 +18,13 @@
use std::ptr;
use crate::{
ipc_binding, IRemoteObj, DeathRecipient, Result,
MsgParcel, BorrowedMsgParcel, AsRawPtr
MsgParcel, BorrowedMsgParcel, AsRawPtr, parcel::on_string16_writer,
parcel::vec_u16_to_string,
};
use crate::ipc_binding::{CRemoteObject, CDeathRecipient};
use crate::parcel::parcelable::{Serialize, Deserialize};
use crate::parcel::parcelable::{Serialize, Deserialize, allocate_vec_with_buffer};
use std::ffi::{c_void};
use crate::String16;
pub mod death_recipient;
pub mod cmp;
......@@ -69,6 +71,7 @@ impl IRemoteObj for RemoteObj {
// Add death Recipient
fn add_death_recipient(&self, recipient: &mut DeathRecipient) -> bool {
// SAFETY:
unsafe {
ipc_binding::AddDeathRecipient(self.as_inner(), recipient.as_mut_raw())
}
......@@ -76,10 +79,51 @@ impl IRemoteObj for RemoteObj {
// remove death Recipients
fn remove_death_recipient(&self, recipient: &mut DeathRecipient) -> bool {
// SAFETY:
unsafe {
ipc_binding::RemoveDeathRecipient(self.as_inner(), recipient.as_mut_raw())
}
}
fn is_proxy(&self) -> bool {
// SAFETY:
unsafe {
ipc_binding::IsProxyObject(self.as_inner())
}
}
fn dump(&self, fd: i32, args: &mut Vec<String16>) -> i32 {
let slice = &args[..];
// SAFETY:
unsafe {
ipc_binding::Dump(self.as_inner(), fd, slice.as_ptr() as *const c_void,
slice.len().try_into().unwrap(), on_string16_writer)
}
}
fn is_dead(&self) -> bool {
// SAFETY:
unsafe {
ipc_binding::IsObjectDead(self.as_inner())
}
}
fn interface_descriptor(&self) -> Result<String> {
let mut vec: Option<Vec<u16>> = None;
let ok_status = unsafe {
// SAFETY:
ipc_binding::GetInterfaceDescriptor(
self.as_inner(),
&mut vec as *mut _ as *mut c_void,
allocate_vec_with_buffer::<u16>
)
};
if ok_status {
vec_u16_to_string(vec)
} else {
Err(-1)
}
}
}
impl Serialize for RemoteObj {
......
......@@ -129,6 +129,14 @@ extern "C" {
recipient: *mut CDeathRecipient) -> bool;
pub fn RemoveDeathRecipient(object: *mut CRemoteObject,
recipient: *mut CDeathRecipient) -> bool;
pub fn IsProxyObject(object: *mut CRemoteObject) -> bool;
pub fn Dump(object: *mut CRemoteObject, fd: i32, value: *const c_void, len: i32,
writer: OnStringArrayWrite) -> i32;
pub fn IsObjectDead(object: *mut CRemoteObject) -> bool;
pub fn GetInterfaceDescriptor(object: *mut CRemoteObject,
value: *mut c_void, allocator: OnCParcelBytesAllocator::<u16>) -> bool;
}
// C interface for Parcel
......@@ -194,6 +202,8 @@ extern "C" {
writer: OnStringArrayWrite) -> bool;
pub fn CParcelWriteStringElement(data: *const c_void, value: *const c_char,
len: i32) -> bool;
pub fn CParcelWritU16stringElement(data: *const c_void, value: *const c_char,
len: i32) -> bool;
pub fn CParcelReadStringArray(parcel: *const CParcel, value: *mut c_void,
reader: OnStringArrayRead) -> bool;
pub fn CParcelReadStringElement(index: u32, data: *const c_void, value: *mut c_void,
......@@ -215,9 +225,15 @@ extern "C" {
pub fn CParcelGetWritePosition(parcel: *const CParcel) -> u32;
pub fn CParcelRewindRead(parcel: *mut CParcel, new_pos: u32) -> bool;
pub fn CParcelRewindWrite(parcel: *mut CParcel, new_pos: u32) -> bool;
pub fn CParcelWriteAshmem(parcel: *mut CParcel, ashmem: *mut CAshmem) -> bool;
pub fn CParcelReadAshmem(parcel: *const CParcel) -> *mut CAshmem;
pub fn CParcelContainFileDescriptors(parcel: *const CParcel) -> bool;
pub fn CParcelGetRawDataSize(parcel: *const CParcel) -> usize;
pub fn CParcelGetRawDataCapacity(parcel: *const CParcel) -> usize;
pub fn CParcelClearFileDescriptor(parcel: *mut CParcel);
pub fn CParcelSetClearFdFlag(parcel: *mut CParcel);
pub fn CParcelAppend(parcel: *mut CParcel, data: *mut CParcel) -> bool;
}
// C interface for Ashmem
......@@ -250,4 +266,11 @@ extern "C" {
pub fn GetSelfToekenId() -> u64;
pub fn GetCallingPid() -> u64;
pub fn GetCallingUid() -> u64;
pub fn SetMaxWorkThreadNum(maxThreadNum: i32) -> bool;
pub fn IsLocalCalling() -> bool;
pub fn SetCallingIdentity(identity: *const c_char) -> bool;
pub fn GetLocalDeviceID(value: *mut c_void, allocator: OnCParcelBytesAllocator::<u8>) -> bool;
pub fn GetCallingDeviceID(value: *mut c_void, allocator: OnCParcelBytesAllocator::<u8>) -> bool;
pub fn ResetCallingIdentity(value: *mut c_void, allocator: OnCParcelBytesAllocator::<u8>) -> bool;
}
\ No newline at end of file
......@@ -44,7 +44,8 @@ pub use crate::ashmem::{
pub use crate::process::{
get_context_object, add_service, get_service, join_work_thread, stop_work_thread,
get_calling_uid, get_calling_token_id, get_first_token_id, get_self_token_id,
get_calling_pid,
get_calling_pid, set_max_work_thread, is_local_calling, set_calling_identity,
get_local_device_id, get_calling_device_id, reset_calling_identity,
};
/// First request code available for user IPC request(inclusive)
......
......@@ -16,6 +16,11 @@
pub mod parcelable;
pub mod types;
pub use types::on_string16_writer;
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::{CParcel};
use std::marker::PhantomData;
......@@ -166,6 +171,49 @@ pub trait IMsgParcel: AsRawPtr<CParcel> {
Ok(RawData::new(raw_data_ptr, len))
}
}
/// contain file descriptors
fn has_fd(&self) -> bool {
unsafe {
ipc_binding::CParcelContainFileDescriptors(self.as_raw())
}
}
/// clear file descriptor
fn clear_fd(&mut self) {
unsafe {
ipc_binding::CParcelClearFileDescriptor(self.as_mut_raw());
}
}
/// get raw data size
fn get_raw_data_size(&self) -> usize {
unsafe {
ipc_binding::CParcelGetRawDataSize(self.as_raw())
}
}
/// get raw data capacity
fn get_raw_data_capacity(&self) -> usize {
unsafe {
ipc_binding::CParcelGetRawDataCapacity(self.as_raw())
}
}
/// set clear fd flag
fn set_clear_fd_flag(&mut self) {
unsafe {
ipc_binding::CParcelSetClearFdFlag(self.as_mut_raw());
}
}
/// append a MsgParcel
fn append(&mut self, data: &mut MsgParcel) -> bool {
let data_parcel = data.as_mut_raw();
unsafe {
ipc_binding::CParcelAppend(self.as_mut_raw(), data_parcel)
}
}
}
/// Rust RawData type which just for fetch data from C++ MssageParcel::ReadRawData()
......
......@@ -41,6 +41,10 @@ pub mod const_array;
pub mod slices;
pub mod vector;
pub use self::string16::on_string16_writer;
pub use self::strings::vec_u16_to_string;
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};
......@@ -85,3 +85,36 @@ impl Deserialize for String16 {
}
}
}
/// Callback to serialize a String16 array to c++ std::vector<std::u16string>.
///
/// Safety: We are relying on c interface to not overrun our slice. As long
/// as it doesn't provide an index larger than the length of the original
/// slice in ser_array, this operation is safe. The index provided
/// is zero-based.
#[allow(dead_code)]
pub unsafe extern "C" fn on_string16_writer(
array: *const c_void, // C++ vector pointer
value: *mut c_void, // Rust slice pointer
len: u32,
) -> bool {
if len == 0 {
return false;
}
let len = len as usize;
let slice: &[String16] = std::slice::from_raw_parts(value.cast(), len);
for item in slice.iter().take(len) {
// SAFETY:
let ret = unsafe {
ipc_binding::CParcelWritU16stringElement(
array,
item.0.as_ptr() as *const c_char,
item.0.as_bytes().len().try_into().unwrap())
};
if !ret {
return false;
}
}
true
}
......@@ -149,7 +149,8 @@ unsafe extern "C" fn on_str_writer(
for item in slice.iter().take(len) {
let ret = unsafe {
ipc_binding::CParcelWriteStringElement(array,
ipc_binding::CParcelWriteStringElement(
array,
item.as_ptr() as *const c_char,
item.as_bytes().len().try_into().unwrap())
};
......@@ -180,7 +181,8 @@ unsafe extern "C" fn on_string_writer(
for item in slice.iter().take(len) {
let ret = unsafe {
ipc_binding::CParcelWriteStringElement(array,
ipc_binding::CParcelWriteStringElement(
array,
item.as_ptr() as *const c_char,
item.as_bytes().len().try_into().unwrap())
};
......@@ -234,7 +236,7 @@ unsafe extern "C" fn on_string_reader(
true
}
fn vec_to_string(vec: Option<Vec<u8>>) -> Result<String> {
pub fn vec_to_string(vec: Option<Vec<u8>>) -> Result<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.
......@@ -247,3 +249,18 @@ fn vec_to_string(vec: Option<Vec<u8>>) -> Result<String> {
Err(-1)
}
}
pub fn vec_u16_to_string(vec: Option<Vec<u16>>) -> Result<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))
});
if let Some(ret) = value {
ret
} else {
error!(LOG_LABEL, "convert vector u16 to String fail");
Err(-1)
}
}
......@@ -17,7 +17,8 @@ use crate::{
ipc_binding, MsgParcel, RemoteObj, IRemoteObj, InterfaceToken, String16,
Result,
};
use std::ffi::{CString, c_char};
use crate::parcel::{vec_to_string, allocate_vec_with_buffer,};
use std::ffi::{CString, c_char, c_void};
use hilog_rust::{info, hilog, HiLogLabel, LogType};
const LOG_LABEL: HiLogLabel = HiLogLabel {
......@@ -127,4 +128,96 @@ pub fn get_calling_uid() -> u64
unsafe {
ipc_binding::GetCallingUid()
}
}
/// Set the maximum number of threads
#[inline]
pub fn set_max_work_thread(max_thread_num: i32) -> bool
{
unsafe {
ipc_binding::SetMaxWorkThreadNum(max_thread_num)
}
}
/// Determine whether it is a local call
#[inline]
pub fn is_local_calling() -> bool
{
unsafe {
ipc_binding::IsLocalCalling()
}
}
/// Set calling identity
#[inline]
pub fn set_calling_identity(identity: String) -> bool
{
match CString::new(identity.as_str()) {
Ok(name) => {
unsafe {
ipc_binding::SetCallingIdentity(name.as_ptr())
}
},
Err(_) => false,
}
}
/// get local device id
#[inline]
pub fn get_local_device_id() -> Result<String>
{
let mut vec: Option<Vec<u8>> = None;
let ok_status = unsafe {
// SAFETY:
ipc_binding::GetLocalDeviceID(
&mut vec as *mut _ as *mut c_void,
allocate_vec_with_buffer::<u8>
)
};
if ok_status {
vec_to_string(vec)
} else {
Err(-1)
}
}
/// get calling device id
#[inline]
pub fn get_calling_device_id() -> Result<String>
{
let mut vec: Option<Vec<u8>> = None;
let ok_status = unsafe {
// SAFETY:
ipc_binding::GetCallingDeviceID(
&mut vec as *mut _ as *mut c_void,
allocate_vec_with_buffer::<u8>
)
};
if ok_status {
vec_to_string(vec)
} else {
Err(-1)
}
}
/// reset calling identity
#[inline]
pub fn reset_calling_identity() -> Result<String>
{
let mut vec: Option<Vec<u8>> = None;
let ok_status = unsafe {
// SAFETY:
ipc_binding::ResetCallingIdentity(
&mut vec as *mut _ as *mut c_void,
allocate_vec_with_buffer::<u8>
)
};
if ok_status {
vec_to_string(vec)
} else {
Err(-1)
}
}
\ No newline at end of file
......@@ -18,6 +18,8 @@
#include "c_ashmem.h"
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
......@@ -95,6 +97,8 @@ bool CParcelReadDoubleArray(const CParcel *parcel, void *value, OnCParcelDoubleA
bool CParcelWriteStringArray(CParcel *parcel, const void *value,
int32_t len, OnStringArrayWrite writer);
bool CParcelWriteStringElement(void *data, const char *value, int32_t len);
bool CParcelWritU16stringElement(void *data, const char16_t *value, int32_t len);
bool CParcelReadStringArray(const CParcel *parcel, void *value, OnStringArrayRead reader);
bool CParcelReadStringElement(uint32_t index, const void *data, void *value,
OnCParcelBytesAllocator allocator);
......@@ -116,10 +120,16 @@ uint32_t CParcelGetReadPosition(const CParcel *parcel);
uint32_t CParcelGetWritePosition(const CParcel *parcel);
bool CParcelRewindRead(CParcel *parcel, uint32_t new_pos);
bool CParcelRewindWrite(CParcel *parcel, uint32_t new_pos);
bool CParcelWriteAshmem(CParcel *parcel, CAshmem *ashmem);
CAshmem *CParcelReadAshmem(const CParcel *parcel);
bool CParcelContainFileDescriptors(const CParcel *parcel);
size_t CParcelGetRawDataSize(const CParcel *parcel);
size_t CParcelGetRawDataCapacity(const CParcel *parcel);
void CParcelClearFileDescriptor(CParcel *parcel);
void CParcelSetClearFdFlag(CParcel *parcel);
bool CParcelAppend(CParcel *parcel, CParcel *data);
#ifdef __cplusplus
}
#endif
......
......@@ -16,6 +16,8 @@
#ifndef IPC_C_PROCESS_H
#define IPC_C_PROCESS_H
#include "c_parcel.h"
#ifdef __cplusplus
extern "C" {
#endif
......@@ -33,6 +35,13 @@ uint64_t GetSelfToekenId(void);
uint64_t GetCallingPid(void);
uint64_t GetCallingUid(void);
bool IsLocalCalling(void);
bool SetMaxWorkThreadNum(int maxThreadNum);
bool SetCallingIdentity(const char *identity);
bool GetLocalDeviceID(void *value, OnCParcelBytesAllocator allocator);
bool GetCallingDeviceID(void *value, OnCParcelBytesAllocator allocator);
bool ResetCallingIdentity(void *value, OnCParcelBytesAllocator allocator);
#ifdef __cplusplus
}
#endif
......
......@@ -38,6 +38,8 @@ typedef void (*OnRemoteObjectDestroyCb)(const void *userData);
typedef void (*OnDeathRecipientCb)(const void *userData);
typedef void (*OnDeathRecipientDestroyCb)(const void *userData);
typedef bool (*On16BytesAllocator)(void *stringData, uint16_t **buffer, int32_t len);
CRemoteObject *CreateRemoteStub(const char *desc, OnRemoteRequestCb callback,
OnRemoteObjectDestroyCb destroy, const void *userData);
......@@ -56,6 +58,12 @@ void DeathRecipientDecStrongRef(CDeathRecipient *recipient);
bool AddDeathRecipient(CRemoteObject *object, CDeathRecipient *recipient);
bool RemoveDeathRecipient(CRemoteObject *object, CDeathRecipient *recipient);
bool IsProxyObject(CRemoteObject *object);
int Dump(CRemoteObject *object, int fd, const void *value,
int32_t len, OnStringArrayWrite writer);
bool IsObjectDead(CRemoteObject *object);
bool GetInterfaceDescriptor(CRemoteObject *object, void *value, On16BytesAllocator allocator);
#ifdef __cplusplus
}
#endif
......
......@@ -633,6 +633,21 @@ bool CParcelWriteStringElement(void *data, const char *value, int32_t len)
return true;
}
bool CParcelWritU16stringElement(void *data, const char16_t *value, int32_t len)
{
std::vector<std::u16string> *u16stringVector = reinterpret_cast<std::vector<std::u16string> *>(data);
if (u16stringVector == nullptr) {
ZLOGE(LOG_LABEL, "%{public}s: stringVector is null\n", __func__);
return false;
}
if (len < 0) {
ZLOGE(LOG_LABEL, "%{public}s: string len is invalid: %d\n", __func__, len);
return false;
}
u16stringVector->push_back(std::u16string(value, len));
return true;
}
bool CParcelReadStringArray(const CParcel *parcel, void *value, OnStringArrayRead reader)
{
if (!IsValidParcel(parcel, __func__) || reader == nullptr) {
......@@ -848,4 +863,52 @@ CAshmem *CParcelReadAshmem(const CParcel *parcel)
}
ashmem->IncStrongRef(nullptr);
return cashmem;
}
bool CParcelContainFileDescriptors(const CParcel *parcel)
{
if (!IsValidParcel(parcel, __func__)) {
return false;
}
return parcel->parcel_->ContainFileDescriptors();
}
size_t CParcelGetRawDataSize(const CParcel *parcel)
{
if (!IsValidParcel(parcel, __func__)) {
return -1;
}
return parcel->parcel_->GetRawDataSize();
}
size_t CParcelGetRawDataCapacity(const CParcel *parcel)
{
if (!IsValidParcel(parcel, __func__)) {
return -1;
}
return parcel->parcel_->GetRawDataCapacity();
}
void CParcelClearFileDescriptor(CParcel *parcel)
{
if (!IsValidParcel(parcel, __func__)) {
return;
}
parcel->parcel_->ClearFileDescriptor();
}
void CParcelSetClearFdFlag(CParcel *parcel)
{
if (!IsValidParcel(parcel, __func__)) {
return;
}
parcel->parcel_->SetClearFdFlag();
}
bool CParcelAppend(CParcel *parcel, CParcel *data)
{
if (!IsValidParcel(parcel, __func__)) {
return false;
}
return parcel->parcel_->Append(*(data->parcel_));
}
\ No newline at end of file
......@@ -15,6 +15,7 @@
#include "c_process.h"
#include <securec.h>
#include "c_remote_object_internal.h"
#include "log_tags.h"
#include "ipc_debug.h"
......@@ -72,4 +73,70 @@ uint64_t GetCallingPid(void)
uint64_t GetCallingUid(void)
{
return static_cast<uint64_t>(IPCSkeleton::GetCallingUid());
}
bool SetMaxWorkThreadNum(int maxThreadNum)
{
return IPCSkeleton::SetMaxWorkThreadNum(maxThreadNum);
}
bool IsLocalCalling(void)
{
return IPCSkeleton::IsLocalCalling();
}
bool GetLocalDeviceID(void *value, OnCParcelBytesAllocator allocator)
{
std::string str(IPCSkeleton::GetLocalDeviceID());
char *buffer = nullptr;
bool isSuccess = allocator(value, &buffer, str.length());
if (!isSuccess) {
ZLOGE(LOG_LABEL, "%{public}s: allocate string buffer is null\n", __func__);
return false;
}
if (str.length() > 0 && memcpy_s(buffer, str.length(), str.data(), str.length()) != EOK) {
ZLOGE(LOG_LABEL, "%{public}s: memcpy string failed\n", __func__);
return false;
}
return true;
}
bool GetCallingDeviceID(void *value, OnCParcelBytesAllocator allocator)
{
std::string str(IPCSkeleton::GetCallingDeviceID());
char *buffer = nullptr;
bool isSuccess = allocator(value, &buffer, str.length());
if (!isSuccess) {
ZLOGE(LOG_LABEL, "%{public}s: allocate string buffer is null\n", __func__);
return false;
}
if (str.length() > 0 && memcpy_s(buffer, str.length(), str.data(), str.length()) != EOK) {
ZLOGE(LOG_LABEL, "%{public}s: memcpy string failed\n", __func__);
return false;
}
return true;
}
bool SetCallingIdentity(const char *identity)
{
if (identity == nullptr) {
return false;
}
std::string str = identity;
return IPCSkeleton::SetCallingIdentity(str);
}
bool ResetCallingIdentity(void *value, OnCParcelBytesAllocator allocator)
{
std::string str(IPCSkeleton::ResetCallingIdentity());
char *buffer = nullptr;
bool isSuccess = allocator(value, &buffer, str.length());
if (!isSuccess) {
ZLOGE(LOG_LABEL, "%{public}s: allocate string buffer is null\n", __func__);
return false;
}
if (str.length() > 0 && memcpy_s(buffer, str.length(), str.data(), str.length()) != EOK) {
ZLOGE(LOG_LABEL, "%{public}s: memcpy string failed\n", __func__);
return false;
}
return true;
}
\ No newline at end of file
......@@ -15,6 +15,7 @@
#include "c_remote_object.h"
#include <securec.h>
#include <string_ex.h>
#include "c_parcel_internal.h"
#include "c_remote_object_internal.h"
......@@ -211,3 +212,65 @@ bool RemoveDeathRecipient(CRemoteObject *object, CDeathRecipient *recipient)
sptr<IRemoteObject::DeathRecipient> callback(recipient);
return object->remote_->RemoveDeathRecipient(callback);
}
bool IsProxyObject(CRemoteObject *object)
{
if (!IsValidRemoteObject(object, __func__)) {
ZLOGE(LOG_LABEL, "%{public}s: recipient is null\n", __func__);
return false;
}
return object->remote_->IsProxyObject();
}
int Dump(CRemoteObject *object, int fd, const void *value, int32_t len, OnStringArrayWrite writer)
{
if (!IsValidRemoteObject(object, __func__) || writer == nullptr) {
ZLOGE(LOG_LABEL, "%{public}s: recipient is null\n", __func__);
return -1;
}
std::vector<std::u16string> stringVector;
if (len > 0 && !writer(reinterpret_cast<void *>(&stringVector), value, static_cast<uint32_t>(len))) {
ZLOGE(LOG_LABEL, "%{public}s: write string array to vector failed\n", __func__);
return -1;
}
return object->remote_->Dump(fd, stringVector);
}
bool IsObjectDead(CRemoteObject *object)
{
if (!IsValidRemoteObject(object, __func__)) {
ZLOGE(LOG_LABEL, "%{public}s: recipient is null\n", __func__);
return false;
}
if (!IsProxyObject(object)) {
return false;
}
return object->remote_->IsObjectDead();
}
bool GetInterfaceDescriptor(CRemoteObject *object, void *value, On16BytesAllocator allocator)
{
if (!IsValidRemoteObject(object, __func__)) {
ZLOGE(LOG_LABEL, "%{public}s: recipient is null\n", __func__);
return false;
}
if (!IsProxyObject(object)) {
return false;
}
std::u16string str(object->remote_->GetInterfaceDescriptor());
uint16_t *buffer = nullptr;
bool isSuccess = allocator(value, &buffer, str.length());
if (!isSuccess) {
ZLOGE(LOG_LABEL, "%{public}s: allocate string buffer is null\n", __func__);
return false;
}
int32_t size = sizeof(char16_t) * str.length();
if (str.length() > 0 && memcpy_s(buffer, size, str.data(), size) != EOK) {
ZLOGE(LOG_LABEL, "%{public}s: memcpy string failed\n", __func__);
return false;
}
return true;
}
\ No newline at end of file
......@@ -80,4 +80,15 @@ IRemoteObject::IRemoteObject(std::u16string descriptor) : descriptor_(descriptor
{
asRemote_ = true;
}
bool IRemoteObject::IsObjectDead() const
{
return false;
}
std::u16string IRemoteObject::GetInterfaceDescriptor()
{
return descriptor_;
}
} // namespace OHOS
......@@ -26,7 +26,8 @@ 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,
RawData,
RawData, set_max_work_thread, reset_calling_identity, set_calling_identity,
is_local_calling, get_local_device_id, get_calling_device_id,
};
use ipc_rust::{Serialize, Deserialize, BorrowedMsgParcel, Ashmem};
......@@ -142,6 +143,21 @@ fn test_calling_info() {
assert_eq!(uid, get_calling_uid());
}
#[test]
fn test_get_device_id() {
assert_eq!(true, set_max_work_thread(3));
let identity_str: String = reset_calling_identity().expect("Failed to reset calling identity");
assert_eq!(true, set_calling_identity(identity_str));
assert_eq!(true, is_local_calling());
let remote = get_test_service();
let (local_device_id, calling_device_id) =
remote.test_get_device_id().expect("test_get_device_id is failed");
assert_eq!(local_device_id, get_local_device_id().expect("Failed toget local device id"));
assert_eq!(calling_device_id, get_calling_device_id().expect("Failed to get calling device id"));
}
#[test]
fn test_sync_request() {
let remote = get_test_service();
......@@ -718,4 +734,74 @@ mod parcel_type_test {
ashmem2.close();
}
}
#[test]
fn test_clear_file_descriptor(){
let mut parcel = MsgParcel::new().expect("create MsgParcel failed");
parcel.clear_fd();
assert_eq!(parcel.get_raw_data_size(), 0);
}
#[test]
fn test_contain_file_descriptors(){
let parcel = MsgParcel::new().expect("create MsgParcel failed");
parcel.has_fd();
assert_eq!(parcel.get_raw_data_size(), 0);
}
#[test]
fn test_get_raw_data_size(){
let parcel = MsgParcel::new().expect("create MsgParcel failed");
assert_eq!(parcel.get_raw_data_size(), 0);
}
#[test]
fn test_get_raw_data_capacity(){
let parcel = MsgParcel::new().expect("create MsgParcel failed");
let size = parcel.get_raw_data_capacity();
assert_eq!(size, 128 * 1024 * 1024);
}
#[test]
fn test_set_clear_fd_flag(){
let mut parcel = MsgParcel::new().expect("create MsgParcel failed");
parcel.set_clear_fd_flag();
}
#[test]
fn test_append(){
let mut parcel_num1 = MsgParcel::new().expect("create MsgParcel failed");
let mut parcel_num2 = MsgParcel::new().expect("create MsgParcel failed");
assert_eq!(parcel_num2.append(&mut parcel_num1), true);
}
}
#[test]
fn test_is_proxy_object() {
let object = get_service(IPC_TEST_SERVICE_ID).expect("get itest service failed");
assert_eq!(object.is_proxy(), true);
}
#[test]
fn test_dump() {
let object = get_service(IPC_TEST_SERVICE_ID).expect("get itest service failed");
let args: &mut Vec<String16> = &mut Vec::new();
args.push(String16::new("test.ipc.ITestService"));
let res = object.dump(0, args);
assert_eq!(0, res);
}
#[test]
fn test_is_object_dead() {
let remote = get_service(IPC_TEST_SERVICE_ID).expect("get itest service failed");
let ret = remote.is_dead();
assert_eq!(ret, false);
}
#[test]
fn test_get_interface_descriptor() {
let remote = get_service(IPC_TEST_SERVICE_ID).expect("get itest service failed");
let descriptor = String16::new(TestProxy::get_descriptor());
let ret = remote.interface_descriptor().expect("get interface descriptor failed");
assert_eq!(descriptor.get_string(), ret);
}
\ No newline at end of file
......@@ -21,7 +21,8 @@ extern crate test_ipc_service;
use ipc_rust::{
IRemoteBroker, join_work_thread, FileDesc, InterfaceToken, Result,
add_service, get_calling_token_id, get_first_token_id, get_calling_pid,
get_calling_uid, String16, RemoteObj, IRemoteStub,
get_calling_uid, String16, RemoteObj, IRemoteStub, get_local_device_id,
get_calling_device_id,
};
use test_ipc_service::{ITest, TestStub, IPC_TEST_SERVICE_ID, reverse, IFoo, FooStub, init_access_token};
use std::io::Write;
......@@ -96,6 +97,17 @@ impl ITest for TestService {
let uid = get_calling_uid();
Ok((token_id, first_token_id, pid, uid))
}
fn test_get_device_id(&self) -> Result<(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)
}
}
}
impl IRemoteBroker for TestService {}
......
......@@ -62,6 +62,8 @@ pub enum ITestCode {
CodeInterfaceToekn,
/// Transaction calling infomation code
CodeCallingInfo,
/// Transaction device id code
CodeGetDeviceId,
}
impl TryFrom<u32> for ITestCode {
......@@ -76,6 +78,7 @@ impl TryFrom<u32> for ITestCode {
_ if code == ITestCode::CodeTransactString as u32 => Ok(ITestCode::CodeTransactString),
_ 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),
}
}
......@@ -99,6 +102,8 @@ pub trait ITest: IRemoteBroker {
fn echo_interface_token(&self, token: &InterfaceToken) -> Result<InterfaceToken>;
/// Test calling infomation transaction
fn echo_calling_info(&self) -> Result<(u64, u64, u64, u64)>;
/// Test get device id
fn test_get_device_id(&self) -> Result<(String, String)>;
}
fn on_itest_remote_request(stub: &dyn ITest, code: u32, data: &BorrowedMsgParcel,
......@@ -152,6 +157,12 @@ fn on_itest_remote_request(stub: &dyn ITest, code: u32, data: &BorrowedMsgParcel
reply.write(&uid).expect("write uid failed");
Ok(())
}
ITestCode::CodeGetDeviceId => {
let (local_device_id, calling_device_id) = stub.test_get_device_id()?;
reply.write(&local_device_id).expect("write local device id failed");
reply.write(&calling_device_id).expect("write calling device id failed");
Ok(())
}
}
}
......@@ -195,6 +206,10 @@ impl ITest for RemoteStub<TestStub> {
fn echo_calling_info(&self) -> Result<(u64, u64, u64, u64)> {
self.0.echo_calling_info()
}
fn test_get_device_id(&self) -> Result<(String, String)> {
self.0.test_get_device_id()
}
}
impl ITest for TestProxy {
......@@ -269,6 +284,15 @@ impl ITest for TestProxy {
let uid: u64 = reply.read().expect("need reply calling uid");
Ok((token_id, first_token_id, pid, uid))
}
fn test_get_device_id(&self) -> Result<(String, String)> {
let data = MsgParcel::new().expect("MsgParcel should success");
let reply = self.remote.send_request(ITestCode::CodeGetDeviceId as u32,
&data, false)?;
let local_device_id: String = reply.read().expect("need reply calling local device id");
let calling_device_id: String = reply.read().expect("need reply first calling device id");
Ok((local_device_id, calling_device_id))
}
}
/// Interface trait for FooService
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册