适配Ipc,samgr,safwk接口修改

Signed-off-by: fqwert <yanglv2@huawei.com>
Change-Id: Ic2cdb11eb3f77eecacb929ceb5639d91a6c66d1b
This commit is contained in:
fqwert 2024-04-02 11:23:32 +08:00
parent 12a36827e7
commit 83a160ba85
12 changed files with 119 additions and 254 deletions

View File

@ -21,3 +21,4 @@ edition = "2021"
[dependencies]
asset_definition = { path = "../definition" }
asset_log = { path = "../os_dependency/log" }
ipc = { git = "https://gitee.com/openharmony/communication_ipc.git" }

View File

@ -15,7 +15,7 @@
//! This module defines IPC interfaces and constants.
use ipc_rust::{BorrowedMsgParcel, IpcStatusCode};
use ipc::{parcel::MsgParcel, IpcStatusCode};
use asset_definition::{
impl_enum_trait, log_throw_error, AssetError, AssetMap, Conversion, DataType, ErrCode, Result, Tag, Value,
@ -36,7 +36,7 @@ impl_enum_trait! {
#[derive(Clone, Copy)]
pub enum IpcCode {
/// Code for AddAsset.
Add = ipc_rust::FIRST_CALL_TRANSACTION,
Add = ipc::FIRST_CALL_TRANSACTION,
/// Code for RemoveAsset.
Remove,
/// Code for UpdateAsset.
@ -50,29 +50,8 @@ impl_enum_trait! {
}
}
/// Function between proxy and stub of Asset service.
pub trait IAsset: ipc_rust::IRemoteBroker {
/// Add an Asset.
fn add(&self, attributes: &AssetMap) -> Result<()>;
/// Remove one or more Assets that match a search query.
fn remove(&self, query: &AssetMap) -> Result<()>;
/// Update an Asset that matches a search query.
fn update(&self, query: &AssetMap, attributes_to_update: &AssetMap) -> Result<()>;
/// Preprocessing for querying one or more Assets that require user authentication.
fn pre_query(&self, query: &AssetMap) -> Result<Vec<u8>>;
/// Query one or more Assets that match a search query.
fn query(&self, query: &AssetMap) -> Result<Vec<AssetMap>>;
/// Post-processing for querying multiple Assets that require user authentication.
fn post_query(&self, query: &AssetMap) -> Result<()>;
}
/// serialize the map to parcel
pub fn serialize_map(map: &AssetMap, parcel: &mut BorrowedMsgParcel) -> Result<()> {
pub fn serialize_map(map: &AssetMap, parcel: &mut MsgParcel) -> Result<()> {
if map.len() as u32 > MAX_MAP_CAPACITY {
return log_throw_error!(ErrCode::InvalidArgument, "[FALTAL][IPC]The map size exceeds the limit.");
}
@ -97,7 +76,7 @@ pub fn serialize_map(map: &AssetMap, parcel: &mut BorrowedMsgParcel) -> Result<(
}
/// deserialize the map from parcel
pub fn deserialize_map(parcel: &BorrowedMsgParcel) -> Result<AssetMap> {
pub fn deserialize_map(parcel: &mut MsgParcel) -> Result<AssetMap> {
let len = parcel.read::<u32>().map_err(ipc_err_handle)?;
if len > MAX_MAP_CAPACITY {
return log_throw_error!(ErrCode::InvalidArgument, "[FATAL][IPC]The map size exceeds the limit.");
@ -125,7 +104,7 @@ pub fn deserialize_map(parcel: &BorrowedMsgParcel) -> Result<AssetMap> {
}
/// Serialize the collection of map to parcel.
pub fn serialize_maps(vec: &Vec<AssetMap>, parcel: &mut BorrowedMsgParcel) -> Result<()> {
pub fn serialize_maps(vec: &Vec<AssetMap>, parcel: &mut MsgParcel) -> Result<()> {
if vec.len() as u32 > MAX_VEC_CAPACITY {
return log_throw_error!(ErrCode::InvalidArgument, "[FATAL][IPC]The vector size exceeds the limit.");
}
@ -137,7 +116,7 @@ pub fn serialize_maps(vec: &Vec<AssetMap>, parcel: &mut BorrowedMsgParcel) -> Re
}
/// Deserialize the collection of map from parcel.
pub fn deserialize_maps(parcel: &BorrowedMsgParcel) -> Result<Vec<AssetMap>> {
pub fn deserialize_maps(parcel: &mut MsgParcel) -> Result<Vec<AssetMap>> {
let len = parcel.read::<u32>().map_err(ipc_err_handle)?;
if len > MAX_VEC_CAPACITY {
return log_throw_error!(ErrCode::InvalidArgument, "[FATAL][IPC]The vector size exceeds the limit.");

View File

@ -19,3 +19,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
hilog_rust = { git = "https://gitee.com/openharmony/hiviewdfx_hilog.git" }

View File

@ -22,10 +22,8 @@ ohos_rust_shared_library("asset_sdk") {
]
external_deps = [
"c_utils:utils",
"hilog:libhilog",
"ipc:ipc_rust",
"samgr:rust_samgr",
"samgr:samgr_rust",
]
crate_name = "asset_sdk"

View File

@ -19,6 +19,9 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
samgr = { git = "https://gitee.com/openharmony/systemabilitymgr_samgr.git" }
ipc = { git = "https://gitee.com/openharmony/communication_ipc.git" }
asset_definition = { path = "../../../frameworks/definition" }
asset_log = { path = "../../../frameworks/os_dependency/log" }
asset_ipc = { path = "../../../frameworks/ipc" }
asset_ipc = { path = "../../../frameworks/ipc" }

View File

@ -17,19 +17,16 @@
pub use asset_definition::*;
use ipc_rust::RemoteObjRef;
use ipc::{parcel::MsgParcel, remote::RemoteObj};
use samgr::manage::SystemAbilityManager;
use asset_ipc::{IAsset, SA_ID};
use asset_ipc::{deserialize_maps, ipc_err_handle, serialize_map, IpcCode, IPC_SUCCESS, SA_ID};
mod proxy;
use proxy::AssetProxy;
fn get_remote() -> Result<RemoteObjRef<AssetProxy>> {
let object = rust_samgr::get_service_proxy::<AssetProxy>(SA_ID);
match object {
Ok(remote) => Ok(remote),
Err(e) => {
log_throw_error!(ErrCode::ServiceUnavailable, "[FATAL][RUST SDK]get remote service failed. Error = {}", e)
fn get_remote() -> Result<RemoteObj> {
match SystemAbilityManager::get_system_ability(SA_ID) {
Some(remote) => Ok(remote),
None => {
log_throw_error!(ErrCode::ServiceUnavailable, "[FATAL][RUST SDK]get remote service failed")
},
}
}
@ -37,7 +34,7 @@ fn get_remote() -> Result<RemoteObjRef<AssetProxy>> {
/// This manager provides the capabilities for life cycle management of sensitive user data (Asset) such as passwords
/// and tokens, including adding, removing, updating, and querying.
pub struct Manager {
remote: RemoteObjRef<AssetProxy>,
remote: RemoteObj,
}
impl Manager {
@ -49,31 +46,63 @@ impl Manager {
/// Add an Asset.
pub fn add(&self, attributes: &AssetMap) -> Result<()> {
self.remote.add(attributes)
let mut parcel = MsgParcel::new();
serialize_map(attributes, &mut parcel)?;
self.send_request(parcel, IpcCode::Add)?;
Ok(())
}
/// Remove one or more Assets that match a search query.
pub fn remove(&self, query: &AssetMap) -> Result<()> {
self.remote.remove(query)
let mut parcel = MsgParcel::new();
serialize_map(query, &mut parcel)?;
self.send_request(parcel, IpcCode::Remove)?;
Ok(())
}
/// Update an Asset that matches a search query.
pub fn update(&self, query: &AssetMap, attributes_to_update: &AssetMap) -> Result<()> {
self.remote.update(query, attributes_to_update)
let mut parcel = MsgParcel::new();
serialize_map(query, &mut parcel)?;
serialize_map(attributes_to_update, &mut parcel)?;
self.send_request(parcel, IpcCode::Update)?;
Ok(())
}
/// Preprocessing for querying one or more Assets that require user authentication.
pub fn pre_query(&self, query: &AssetMap) -> Result<Vec<u8>> {
self.remote.pre_query(query)
let mut parcel = MsgParcel::new();
serialize_map(query, &mut parcel)?;
let mut reply = self.send_request(parcel, IpcCode::PreQuery)?;
let res = reply.read::<Vec<u8>>().map_err(ipc_err_handle)?;
Ok(res)
}
/// Query one or more Assets that match a search query.
pub fn query(&self, query: &AssetMap) -> Result<Vec<AssetMap>> {
self.remote.query(query)
let mut parcel = MsgParcel::new();
serialize_map(query, &mut parcel)?;
let mut reply = self.send_request(parcel, IpcCode::Query)?;
let res = deserialize_maps(&mut reply)?;
Ok(res)
}
/// Post-processing for querying multiple Assets that require user authentication.
pub fn post_query(&self, query: &AssetMap) -> Result<()> {
self.remote.post_query(query)
let mut parcel = MsgParcel::new();
serialize_map(query, &mut parcel)?;
self.send_request(parcel, IpcCode::PostQuery)?;
Ok(())
}
fn send_request(&self, mut parcel: MsgParcel, ipc_code: IpcCode) -> Result<MsgParcel> {
let mut reply = self.remote.send_request(ipc_code as u32, &mut parcel).map_err(ipc_err_handle)?;
match reply.read::<u32>().map_err(ipc_err_handle)? {
IPC_SUCCESS => Ok(reply),
e => {
let msg = reply.read::<String>().map_err(ipc_err_handle)?;
log_throw_error!(ErrCode::try_from(e)?, "{}", msg)
}
}
}
}

View File

@ -1,121 +0,0 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//! This module implements the proxy of the Asset service.
#![allow(dead_code)]
use ipc_rust::{FromRemoteObj, IRemoteBroker, IRemoteObj, IpcResult, MsgParcel, RemoteObj, RemoteObjRef};
use asset_definition::{log_throw_error, AssetMap, ErrCode, Result};
use asset_ipc::{deserialize_maps, ipc_err_handle, serialize_map, IAsset, IpcCode, IPC_SUCCESS, SA_NAME};
/// Proxy of Asset Service.
pub(crate) struct AssetProxy {
remote: RemoteObj,
}
impl AssetProxy {
/// Create proxy object by RemoteObj.
fn from_remote_object(remote: &RemoteObj) -> IpcResult<Self> {
Ok(Self { remote: remote.clone() })
}
/// Get proxy object descriptor.
pub fn get_descriptor() -> &'static str {
SA_NAME
}
}
impl IRemoteBroker for AssetProxy {
/// Get RemoteObject object from proxy.
fn as_object(&self) -> Option<RemoteObj> {
Some(self.remote.clone())
}
}
impl FromRemoteObj for AssetProxy {
/// Convert RemoteObj to RemoteObjRef<dyn IAsset>.
fn try_from(object: RemoteObj) -> IpcResult<RemoteObjRef<AssetProxy>> {
Ok(RemoteObjRef::new(Box::new(AssetProxy::from_remote_object(&object)?)))
}
}
impl AssetProxy {
fn send_request(&self, parcel: MsgParcel, ipc_code: IpcCode) -> Result<MsgParcel> {
let reply = self.remote.send_request(ipc_code as u32, &parcel, false).map_err(ipc_err_handle)?;
match reply.read::<u32>().map_err(ipc_err_handle)? {
IPC_SUCCESS => Ok(reply),
e => {
let msg = reply.read::<String>().map_err(ipc_err_handle)?;
log_throw_error!(ErrCode::try_from(e)?, "{}", msg)
},
}
}
}
fn new_parcel() -> Result<MsgParcel> {
match MsgParcel::new() {
Some(p) => Ok(p),
None => log_throw_error!(ErrCode::IpcError, "[FATAL]Get MsgParcel failed."),
}
}
impl IAsset for AssetProxy {
fn add(&self, attributes: &AssetMap) -> Result<()> {
let mut parcel = new_parcel()?;
serialize_map(attributes, &mut parcel.borrowed())?;
self.send_request(parcel, IpcCode::Add)?;
Ok(())
}
fn remove(&self, query: &AssetMap) -> Result<()> {
let mut parcel = new_parcel()?;
serialize_map(query, &mut parcel.borrowed())?;
self.send_request(parcel, IpcCode::Remove)?;
Ok(())
}
fn update(&self, query: &AssetMap, attributes_to_update: &AssetMap) -> Result<()> {
let mut parcel = new_parcel()?;
serialize_map(query, &mut parcel.borrowed())?;
serialize_map(attributes_to_update, &mut parcel.borrowed())?;
self.send_request(parcel, IpcCode::Update)?;
Ok(())
}
fn pre_query(&self, query: &AssetMap) -> Result<Vec<u8>> {
let mut parcel = new_parcel()?;
serialize_map(query, &mut parcel.borrowed())?;
let reply = self.send_request(parcel, IpcCode::PreQuery)?;
let res = reply.read::<Vec<u8>>().map_err(ipc_err_handle)?;
Ok(res)
}
fn query(&self, query: &AssetMap) -> Result<Vec<AssetMap>> {
let mut parcel = new_parcel()?;
serialize_map(query, &mut parcel.borrowed())?;
let mut reply = self.send_request(parcel, IpcCode::Query)?;
let res = deserialize_maps(&reply.borrowed())?;
Ok(res)
}
fn post_query(&self, query: &AssetMap) -> Result<()> {
let mut parcel = new_parcel()?;
serialize_map(query, &mut parcel.borrowed())?;
self.send_request(parcel, IpcCode::PostQuery)?;
Ok(())
}
}

View File

@ -19,5 +19,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
ipc = { git = "https://gitee.com/openharmony/communication_ipc" }
asset_definition = { path = "../../frameworks/definition" }
asset_log = { path = "../../frameworks/os_dependency/log" }

View File

@ -15,7 +15,7 @@
//! This module implements the capability of processing the identity information of the Asset caller.
use ipc_rust::get_calling_uid;
use ipc::Skeleton;
use asset_definition::{log_throw_error, ErrCode, Result};
@ -70,7 +70,7 @@ impl CallingInfo {
/// Build a instance of CallingInfo.
pub fn build() -> Result<Self> {
let uid = get_calling_uid();
let uid = Skeleton::calling_uid();
let user_id: i32 = get_user_id(uid)?;
let mut owner_info = vec![0u8; 256];
let mut len = 256u32;

View File

@ -19,6 +19,12 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
hilog_rust = { git = "https://gitee.com/openharmony/hiviewdfx_hilog.git" }
system_ability_fwk = { git = "https://gitee.com/openharmony/systemabilitymgr_safwk" }
hitrace_meter_rust = { git = "https://gitee.com/openharmony/hiviewdfx_hitrace.git" }
hisysevent = { git = "https://gitee.com/openharmony/hiviewdfx_hisysevent.git" }
ipc = { git = "https://gitee.com/openharmony/communication_ipc" }
asset_constants = { path = "../constants" }
asset_crypto_manager = { path = "../crypto_manager" }
asset_db_operator = { path = "../db_operator" }

View File

@ -15,19 +15,13 @@
//! This module implements the Asset service.
use std::{
ffi::{c_char, CString},
thread,
time::Instant,
};
use std::{thread, time::Instant};
use hilog_rust::{error, hilog, HiLogLabel, LogType};
use ipc_rust::{IRemoteBroker, RemoteObj};
use system_ability_fwk_rust::{define_system_ability, IMethod, ISystemAbility, RSystemAbility};
use system_ability_fwk::ability::{Ability, PublishHandler};
use asset_constants::CallingInfo;
use asset_definition::{log_throw_error, AssetMap, ErrCode, Result};
use asset_ipc::{IAsset, SA_ID};
use asset_ipc::SA_ID;
use asset_log::{loge, logi};
mod listener;
@ -36,52 +30,46 @@ mod stub;
mod sys_event;
mod trace_scope;
use stub::AssetStub;
use sys_event::upload_system_event;
use trace_scope::TraceScope;
const LOG_LABEL: HiLogLabel = HiLogLabel { log_type: LogType::LogCore, domain: 0xD002F08, tag: "Asset" };
struct AssetAbility;
define_system_ability!(
sa: SystemAbility(on_start, on_stop),
);
impl Ability for AssetAbility {
fn on_start(&self, handler: PublishHandler) {
let func_name = hisysevent::function!();
let start = Instant::now();
let _trace = TraceScope::trace(func_name);
let calling_info = CallingInfo::new_self();
fn start_service<T: ISystemAbility + IMethod>(ability: &T) -> Result<()> {
let Some(service) = AssetStub::new_remote_stub(AssetService) else {
return log_throw_error!(ErrCode::IpcError, "Create AssetService failed!");
let _ = upload_system_event(start_service(handler), &calling_info, start, func_name);
}
fn on_stop(&self) {
logi!("[INFO]Asset service on_stop");
listener::unsubscribe();
}
}
fn start_service(handler: PublishHandler) -> Result<()> {
if handler.publish(AssetService) {
return log_throw_error!(ErrCode::IpcError, "Asset publish stub object failed");
};
let Some(obj) = service.as_object() else {
return log_throw_error!(ErrCode::IpcError, "Asset service as_object failed!");
};
ability.publish(&obj, SA_ID);
logi!("[INFO]Asset service on_start");
thread::spawn(listener::subscribe);
Ok(())
}
fn on_start<T: ISystemAbility + IMethod>(ability: &T) {
let func_name = hisysevent::function!();
let start = Instant::now();
let _trace = TraceScope::trace(func_name);
let calling_info = CallingInfo::new_self();
let _ = upload_system_event(start_service(ability), &calling_info, start, func_name);
}
fn on_stop<T: ISystemAbility + IMethod>(_ability: &T) {
logi!("[INFO]Asset service on_stop");
listener::unsubscribe();
}
#[used]
#[link_section = ".init_array"]
static A: extern "C" fn() = {
extern "C" fn init() {
let Some(sa) = SystemAbility::new_system_ability(SA_ID, true) else {
let Some(sa) = AssetAbility.build_system_ability(SA_ID, true) else {
loge!("Create Asset service failed.");
return;
};
sa.register();
}
init
@ -99,8 +87,7 @@ macro_rules! execute {
}};
}
impl IRemoteBroker for AssetService {}
impl IAsset for AssetService {
impl AssetService {
fn add(&self, attributes: &AssetMap) -> Result<()> {
execute!(operations::add, attributes)
}

View File

@ -15,63 +15,28 @@
//! This module implements the stub of the Asset service.
use ipc_rust::{BorrowedMsgParcel, FileDesc, IRemoteStub, IpcResult, IpcStatusCode, RemoteStub, String16};
use ipc::{parcel::MsgParcel, remote::RemoteStub, IpcResult, IpcStatusCode};
use asset_definition::{AssetError, Result};
use asset_ipc::{deserialize_map, serialize_maps, IAsset, IpcCode, IPC_SUCCESS, SA_NAME};
use asset_ipc::{deserialize_map, serialize_maps, IpcCode, IPC_SUCCESS, SA_NAME};
use asset_log::loge;
/// IPC stub type.
pub struct AssetStub(Box<dyn IAsset + Sync + Send>);
use crate::AssetService;
impl AssetStub {
/// Create a new remote stub service.
pub fn new_remote_stub<T: IAsset + Send + Sync + 'static>(obj: T) -> Option<RemoteStub<Self>> {
RemoteStub::new(AssetStub(Box::new(obj)))
}
}
impl IRemoteStub for AssetStub {
/// Get stub object descriptor.
fn get_descriptor() -> &'static str {
SA_NAME
}
/// Callback to deal IPC request for this stub.
fn on_remote_request(&self, code: u32, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> i32 {
match on_remote_request(&*self.0, code, data, reply) {
impl RemoteStub for AssetService {
fn on_remote_request(&self, code: u32, data: &mut ipc::parcel::MsgParcel, reply: &mut ipc::parcel::MsgParcel) -> i32 {
match on_remote_request(self, code, data, reply) {
Ok(_) => IPC_SUCCESS as i32,
Err(e) => e as i32,
}
}
/// Callback to dump.
fn on_dump(&self, file: &FileDesc, args: &mut Vec<String16>) -> i32 {
self.0.dump(file, args)
fn descriptor(&self) -> &'static str {
SA_NAME
}
}
fn asset_err_handle(e: AssetError) -> IpcStatusCode {
loge!("[IPC]Asset error code = {}, msg is {}", e.code, e.msg);
IpcStatusCode::InvalidValue
}
fn reply_handle(ret: Result<()>, reply: &mut BorrowedMsgParcel) -> IpcResult<()> {
match ret {
Ok(_) => reply.write::<u32>(&IPC_SUCCESS),
Err(e) => {
reply.write::<u32>(&(e.code as u32))?;
reply.write::<String>(&e.msg)
},
}
}
fn on_remote_request(
stub: &dyn IAsset,
code: u32,
data: &BorrowedMsgParcel,
reply: &mut BorrowedMsgParcel,
) -> IpcResult<()> {
fn on_remote_request(stub: &AssetService, code: u32, data: &mut MsgParcel, reply: &mut MsgParcel) -> IpcResult<()> {
let ipc_code = IpcCode::try_from(code).map_err(asset_err_handle)?;
let map = deserialize_map(data).map_err(asset_err_handle)?;
match ipc_code {
@ -98,3 +63,18 @@ fn on_remote_request(
IpcCode::PostQuery => reply_handle(stub.post_query(&map), reply),
}
}
fn asset_err_handle(e: AssetError) -> IpcStatusCode {
loge!("[IPC]Asset error code = {}, msg is {}", e.code, e.msg);
IpcStatusCode::InvalidValue
}
fn reply_handle(ret: Result<()>, reply: &mut MsgParcel) -> IpcResult<()> {
match ret {
Ok(_) => reply.write::<u32>(&IPC_SUCCESS),
Err(e) => {
reply.write::<u32>(&(e.code as u32))?;
reply.write::<String>(&e.msg)
}
}
}