diff --git a/README.md b/README.md index c90f33a..e157051 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,9 @@ git clone ssh://git@szv-y.codehub.huawei.com:2222/y00522150/asset.git 在vendor/huawei/build/component_config/system/generic_generic_arm_64only/hisi_higeneric/newphone_standard/part_config.json添加 "security:asset":{}, +在vendor/huawei/build/component_config/system/generic_generic_arm_64only/hisi_newbaltimore/pc_standard/part_config.json添加 +"security:asset":{}, + ```bash # 首次编译命令:(修改BUILD.gn时执行) ./build_system.sh --abi-type generic_generic_arm_64only --device-type hisi_higeneric_newphone_standard --ccache --build-variant root --build-target out/generic_generic_arm_64only/hisi_higeneric_newphone_standard/build_configs/security/asset:asset --build-target out/generic_generic_arm_64only/hisi_higeneric_newphone_standard/build_configs/security/asset:asset_test @@ -27,6 +30,9 @@ git clone ssh://git@szv-y.codehub.huawei.com:2222/y00522150/asset.git # 非首次编译命令:(未修改BUILD.gn时执行) ./build_system.sh --abi-type generic_generic_arm_64only --device-type hisi_higeneric_newphone_standard --ccache --build-variant root --build-target out/generic_generic_arm_64only/hisi_higeneric_newphone_standard/build_configs/security/asset:asset --build-target out/generic_generic_arm_64only/hisi_higeneric_newphone_standard/build_configs/security/asset:asset_test --fast-rebuild +# PC编译命令: +./build_system.sh --abi-type generic_generic_arm_64only --device-type hisi_newbaltimore_pc_standard --ccache --build-variant root --build-target out/generic_generic_arm_64only/hisi_newbaltimore_pc_standard/build_configs/security/asset:asset --build-target out/generic_generic_arm_64only/hisi_newbaltimore_pc_standard/build_configs/security/asset:asset_test + # 支持SA自启:(仅在调试设备上执行一次) ./scripts/push_asset_cfg.bat diff --git a/frameworks/common/src/lib.rs b/frameworks/common/src/lib.rs index 133351f..14e7295 100755 --- a/frameworks/common/src/lib.rs +++ b/frameworks/common/src/lib.rs @@ -13,7 +13,7 @@ * limitations under the License. */ -//! This create implement the asset +//! Module shared by the SDK and Service #[macro_use] pub mod asset_hilog; diff --git a/frameworks/ipc_define/BUILD.gn b/frameworks/ipc_define/BUILD.gn index 0f9141b..3d871e5 100755 --- a/frameworks/ipc_define/BUILD.gn +++ b/frameworks/ipc_define/BUILD.gn @@ -15,7 +15,7 @@ import("//build/ohos.gni") COMPONENT_DIR = "//base/security/asset" -ohos_rust_static_library("asset_ipc_define_lib") { +ohos_rust_static_library("asset_ipc") { sources = [ "src/lib.rs" ] deps = [ "$COMPONENT_DIR/frameworks/common:asset_common" ] external_deps = [ @@ -23,7 +23,7 @@ ohos_rust_static_library("asset_ipc_define_lib") { "ipc:ipc_rust", ] - crate_name = "asset_ipc_define_lib" + crate_name = "asset_ipc" crate_type = "rlib" subsystem_name = "security" diff --git a/frameworks/ipc_define/Cargo.toml b/frameworks/ipc_define/Cargo.toml index 8e1f88d..7740a33 100755 --- a/frameworks/ipc_define/Cargo.toml +++ b/frameworks/ipc_define/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "asset_ipc_define_lib" +name = "asset_ipc" version = "0.1.0" edition = "2021" diff --git a/frameworks/ipc_define/src/asset_service.rs b/frameworks/ipc_define/src/asset_service.rs index 2cfa10a..41382ad 100644 --- a/frameworks/ipc_define/src/asset_service.rs +++ b/frameworks/ipc_define/src/asset_service.rs @@ -33,51 +33,49 @@ use asset_common::{ impl_try_from!{ /// Asset ipc code #[derive(Clone, Copy)] - pub enum AssetIpcCode { + pub enum IpcCode { /// insert data Insert = FIRST_CALL_TRANSACTION, - /// add an asset + /// IPC code for AddAsset Add, } } -impl fmt::Display for AssetIpcCode { +impl fmt::Display for IpcCode { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { - AssetIpcCode::Insert => write!(f, "insert"), - AssetIpcCode::Add => write!(f, "add"), + IpcCode::Insert => write!(f, "insert"), + IpcCode::Add => write!(f, "add"), } } } -/// SA ID for "example_asset_ipc_service" +/// SA ID for "security_asset_service" pub const ASSET_SERVICE_ID: i32 = 3511; /// Function between proxy and stub of AssetService -pub trait AssetBroker: IRemoteBroker { +pub trait IAsset: IRemoteBroker { /// xxx fn insert(&self, input: &AssetMap) -> Result; // fn transform(&self, code: u32, input: &AssetMap) -> Result; /// add an assert - fn add(&self, input: &AssetMap) -> Result; + fn add(&self, input: &AssetMap) -> Result<()>; } -fn on_asset_remote_request( - stub: &dyn AssetBroker, - code: u32, - data: &BorrowedMsgParcel, - reply: &mut BorrowedMsgParcel, -) -> IpcResult<()> { +/// IPC entry of the Asset service +fn on_remote_request(stub: &dyn IAsset, code: u32, data: &BorrowedMsgParcel, + reply: &mut BorrowedMsgParcel) -> IpcResult<()> { + logi!("on_remote_request, calling function: {}", code); let input_map = AssetMap::deserialize(data); if input_map.is_err() { - loge!("deserialize in on_asset_remote_request failed!"); + loge!("deserialize in on_remote_request failed!"); return Err(IpcStatusCode::InvalidValue); } - if let Ok(ipc_code) = AssetIpcCode::try_from(code) { + if let Ok(ipc_code) = IpcCode::try_from(code) { match ipc_code { - AssetIpcCode::Insert => { - logi!("on_asset_remote_request Insert"); + IpcCode::Insert => { + logi!("on_remote_request Insert"); match stub.insert(input_map.as_ref().unwrap()) { Ok(res) => { reply.write::(&(ErrCode::Success as i32))?; @@ -88,13 +86,12 @@ fn on_asset_remote_request( } } }, - AssetIpcCode::Add => { - logi!("on_asset_remote_request add"); + IpcCode::Add => { + logi!("on_remote_request add"); match stub.add(input_map.as_ref().unwrap()) { - Ok(res) => { + Ok(_) => { reply.write::(&(ErrCode::Success as i32))?; - res.serialize(reply)?; }, Err(e) => { reply.write::(&(e as i32))?; @@ -109,52 +106,47 @@ fn on_asset_remote_request( } define_remote_object!( - AssetBroker["security_asset_service"] { - stub: AssetStub(on_asset_remote_request), + IAsset["security_asset_service"] { + stub: AssetStub(on_remote_request), proxy: AssetProxy, } ); -// Make RemoteStub object can call AssetBroker function directly. -impl AssetBroker for RemoteStub { +// Make RemoteStub object can call IAsset function directly. +impl IAsset for RemoteStub { fn insert(&self, input: &AssetMap) -> Result { self.0.insert(input) } - fn add(&self, input: &AssetMap) -> Result { + fn add(&self, input: &AssetMap) -> Result<()> { self.0.add(input) } } -fn transform(proxy: &AssetProxy, code: AssetIpcCode, input: &AssetMap) -> Result { - let parce_new = MsgParcel::new(); - match parce_new { - Some(mut send_parcel) => { - input.serialize(&mut send_parcel.borrowed())?; +impl IAsset for AssetProxy { + fn insert(&self, _input: &AssetMap) -> Result { + Ok(AssetMap::new()) + } - let reply_parcel = - proxy.remote.send_request(code as u32, &send_parcel, false); - if let Ok(reply) = reply_parcel { - let res_code = ErrCode::try_from(reply.read::()?)?; - if res_code != ErrCode::Success { - return Err(res_code); + fn add(&self, input: &AssetMap) -> Result<()> { + let parce_new = MsgParcel::new(); + match parce_new { + Some(mut send_parcel) => { + input.serialize(&mut send_parcel.borrowed())?; + let reply_parcel = + self.remote.send_request(IpcCode::Add as u32, &send_parcel, false); + if let Ok(reply) = reply_parcel { + let res_code = ErrCode::try_from(reply.read::()?)?; + if res_code != ErrCode::Success { + return Err(res_code); + } + Ok(()) + } else { + loge!("AssetProxy transform {} failed!", IpcCode::Add); + Err(ErrCode::Failed) } - Ok(AssetMap::deserialize(reply.borrowed_ref())?) - } else { - loge!("AssetProxy transform {} failed!", code); - Err(ErrCode::Failed) - } - }, - None => Err(ErrCode::Failed) - } -} - -impl AssetBroker for AssetProxy { - fn insert(&self, input: &AssetMap) -> Result { - transform(self, AssetIpcCode::Insert, input) - } - - fn add(&self, input: &AssetMap) -> Result { - transform(self, AssetIpcCode::Add, input) + }, + None => Err(ErrCode::Failed) + } } } diff --git a/interfaces/inner_api/rs/BUILD.gn b/interfaces/inner_api/rs/BUILD.gn index 4468c1d..8fb588e 100755 --- a/interfaces/inner_api/rs/BUILD.gn +++ b/interfaces/inner_api/rs/BUILD.gn @@ -20,7 +20,7 @@ ohos_rust_shared_library("asset_rust_sdk") { deps = [ "$COMPONENT_DIR/frameworks/common:asset_common", - "$COMPONENT_DIR/frameworks/ipc_define:asset_ipc_define_lib", + "$COMPONENT_DIR/frameworks/ipc_define:asset_ipc", ] external_deps = [ diff --git a/interfaces/inner_api/rs/Cargo.toml b/interfaces/inner_api/rs/Cargo.toml index 092cbff..fed8afa 100755 --- a/interfaces/inner_api/rs/Cargo.toml +++ b/interfaces/inner_api/rs/Cargo.toml @@ -7,4 +7,4 @@ edition = "2021" [dependencies] asset_common = { path = "../../../frameworks/common" } -asset_ipc_define_lib = { path = "../../../frameworks/ipc_define" } \ No newline at end of file +asset_ipc = { path = "../../../frameworks/ipc_define" } \ No newline at end of file diff --git a/interfaces/inner_api/rs/src/asset_request.rs b/interfaces/inner_api/rs/src/asset_request.rs index 550c36e..f6dcc7b 100755 --- a/interfaces/inner_api/rs/src/asset_request.rs +++ b/interfaces/inner_api/rs/src/asset_request.rs @@ -15,41 +15,39 @@ //! This create implement the send request -use asset_common::{ - logi, - definition::{AssetMap, Result, ErrCode}, loge, -}; - -use asset_ipc_define_lib::asset_service::{AssetBroker, ASSET_SERVICE_ID}; - -use ipc_rust::RemoteObjRef; - -use rust_samgr::get_service_proxy; - -use hilog_rust::hilog; use std::ffi::{c_char, CString}; -fn get_asset_service() -> Result> { - let object = get_service_proxy::(ASSET_SERVICE_ID); +use hilog_rust::hilog; +use ipc_rust::RemoteObjRef; +use rust_samgr::get_service_proxy; + +use asset_common::{ + logi, loge, + definition::{AssetMap, Result, ErrCode}, +}; +use asset_ipc::asset_service::{IAsset, ASSET_SERVICE_ID}; + +fn get_asset_service() -> Result> { + let object = get_service_proxy::(ASSET_SERVICE_ID); match object { Ok(remote) => Ok(remote), Err(e) => { - loge!("get_asset_service failed {}!", @public(e)); + loge!("[FATAL]get_asset_service failed {}!", @public(e)); Err(ErrCode::ServiceUnvailable) } } } /// sender -pub(crate) struct AssetIpcProxy { - proxy: RemoteObjRef, +pub(crate) struct AssetProxy { + proxy: RemoteObjRef, } /// 2222 -impl AssetIpcProxy { +impl AssetProxy { /// xxx - pub(crate) fn new() -> Result { - Ok(AssetIpcProxy { proxy: get_asset_service()? }) + pub(crate) fn build() -> Result { + Ok(AssetProxy { proxy: get_asset_service()? }) } /// xxx @@ -59,7 +57,7 @@ impl AssetIpcProxy { } /// add - pub(crate) fn add(&self, input: &AssetMap) -> Result { + pub(crate) fn add(&self, input: &AssetMap) -> Result<()> { logi!("AssetIpcSender add"); self.proxy.add(input) } diff --git a/interfaces/inner_api/rs/src/binding.rs b/interfaces/inner_api/rs/src/binding.rs index 5f84aed..60b3e2f 100755 --- a/interfaces/inner_api/rs/src/binding.rs +++ b/interfaces/inner_api/rs/src/binding.rs @@ -32,7 +32,7 @@ use asset_common::{ Value, asset_type_transform::GetType }}; -use asset_rust_sdk::{asset_insert, add_asset}; +use asset_rust_sdk::{asset_insert, add}; // asset_rust_sdk的crate名字叫asset_sdk或asset, libasset @@ -58,7 +58,7 @@ pub extern "C" fn AssetInsert(code: i32) -> i32 #[no_mangle] pub unsafe extern "C" fn AddAssetC2Rust(attributes: *const AssetParam, attr_cnt: u32) -> i32 { loge!("[YZT] enter AddAssetC2Rust!"); - if attributes.is_null() || attr_cnt == 0 { // todo: 待确认是否需要校验 + if attributes.is_null() || attr_cnt == 0 { return ErrCode::InvalidArgument as i32; } @@ -87,7 +87,11 @@ pub unsafe extern "C" fn AddAssetC2Rust(attributes: *const AssetParam, attr_cnt: } } loge!("[YZT] end AddAssetC2Rust!"); - add_asset(map) as i32 + if let Err(e) = add(map) { + e as i32 + } else { + 0 + } } /// asset param from c diff --git a/interfaces/inner_api/rs/src/lib.rs b/interfaces/inner_api/rs/src/lib.rs index 016cae3..48357e1 100755 --- a/interfaces/inner_api/rs/src/lib.rs +++ b/interfaces/inner_api/rs/src/lib.rs @@ -22,14 +22,14 @@ use asset_common::{ loge, logi, definition::{AssetMap, Result, ErrCode, Tag, Value}, }; -use crate::asset_request::AssetIpcProxy; +use crate::asset_request::AssetProxy; use hilog_rust::hilog; use std::ffi::{c_char, CString}; /// insert data into asset pub fn asset_insert(_code: i32) -> Result { logi!("enter asser insert"); - if let Ok(sender) = AssetIpcProxy::new() { + if let Ok(sender) = AssetProxy::build() { let mut map = AssetMap::new(); map.insert(Tag::AuthType, Value::NUMBER(5)); sender.insert(&map)?; // ingore reply @@ -49,14 +49,14 @@ pub fn asset_insert(_code: i32) -> Result { } } -/// add an asset -pub fn add(input: AssetMap) -> Result { - logi!("enter assert add"); - AssetIpcProxy::new()?.add(&input) -} +// /// add an asset +// pub fn add(input: AssetMap) -> Result { +// logi!("enter assert add"); +// AssetProxy::new()?.add(&input) +// } -/// the mock function -pub fn add_asset(_input: AssetMap) -> ErrCode { - logi!("enter assert add"); - ErrCode::Success +/// add an asset +pub fn add(input: AssetMap) -> Result<()> { + logi!("[YZT][RUST SDK]enter asset add"); + AssetProxy::build()?.add(&input) } \ No newline at end of file diff --git a/services/core_service/BUILD.gn b/services/core_service/BUILD.gn index 588e8c9..ad00eda 100755 --- a/services/core_service/BUILD.gn +++ b/services/core_service/BUILD.gn @@ -20,7 +20,7 @@ ohos_rust_shared_library("asset_service") { deps = [ "$COMPONENT_DIR/frameworks/common:asset_common", - "$COMPONENT_DIR/frameworks/ipc_define:asset_ipc_define_lib", + "$COMPONENT_DIR/frameworks/ipc_define:asset_ipc", "$COMPONENT_DIR/services/db_operator:db_operator", "$COMPONENT_DIR/services/crypto_manager:crypto_manager", "$COMPONENT_DIR/services/wrapper/os_account_wrapper:libasset_os_account_wrapper", diff --git a/services/core_service/Cargo.toml b/services/core_service/Cargo.toml index 95c87b7..f0663c0 100755 --- a/services/core_service/Cargo.toml +++ b/services/core_service/Cargo.toml @@ -7,6 +7,6 @@ edition = "2021" [dependencies] asset_common = { path = "../../frameworks/common" } -asset_ipc_define_lib = { path = "../../frameworks/ipc_define" } +asset_ipc = { path = "../../frameworks/ipc_define" } db_operator = { path = "../../services/db_operator" } crypto_manager = { path = "../crypto_manager" } diff --git a/services/core_service/src/calling_process_info.rs b/services/core_service/src/calling_process_info.rs index c787333..79d07f7 100644 --- a/services/core_service/src/calling_process_info.rs +++ b/services/core_service/src/calling_process_info.rs @@ -19,7 +19,6 @@ mod calling_owner_type; mod calling_owner_user_id; -use asset_common::definition::Result; use calling_owner_type::{OwnerType, get_calling_owner_type}; use calling_owner_user_id::get_calling_user_id; @@ -33,14 +32,12 @@ pub(crate) struct CallingInfo { impl CallingInfo { /// x - pub(crate) fn new() -> Result { + pub(crate) fn new() -> Self { let uid = get_calling_uid(); - Ok( - CallingInfo { - owner_type: get_calling_owner_type(uid)?, - user_id: get_calling_user_id(uid) - } - ) + CallingInfo { + owner_type: get_calling_owner_type(uid), + user_id: get_calling_user_id(uid) + } } /// x diff --git a/services/core_service/src/calling_process_info/calling_owner_type.rs b/services/core_service/src/calling_process_info/calling_owner_type.rs index 60b1bb7..c6a77d4 100644 --- a/services/core_service/src/calling_process_info/calling_owner_type.rs +++ b/services/core_service/src/calling_process_info/calling_owner_type.rs @@ -16,12 +16,9 @@ //! This create implement the asset #![allow(dead_code)] -use asset_common::definition::Result; - /// OwnerType pub(crate) enum OwnerType { Hap(Vec), - Sa(Vec), Native(Vec) } @@ -32,11 +29,8 @@ impl OwnerType { Self::Hap(_) => { 1 }, - Self::Sa(_) => { - 2 - }, Self::Native(_) => { - 3 + 2 } } } @@ -47,9 +41,6 @@ impl OwnerType { Self::Hap(owner_text) => { owner_text }, - Self::Sa(owner_text) => { - owner_text - }, Self::Native(owner_text) => { owner_text } @@ -57,12 +48,12 @@ impl OwnerType { } } -fn get_native_owner_info(uid: u64) -> Result{ - Ok(OwnerType::Native(Vec::from(format!("{}", uid).as_bytes()))) +fn get_native_owner_info(uid: u64) -> OwnerType { + OwnerType::Native(Vec::from(format!("{}", uid).as_bytes())) } /// xxx -pub(crate) fn get_calling_owner_type(uid: u64) -> Result { +pub(crate) fn get_calling_owner_type(uid: u64) -> OwnerType { // Ok(OwnerType::Native(Vec::from("123"))) // to do get_native_owner_info(uid) } \ No newline at end of file diff --git a/services/core_service/src/lib.rs b/services/core_service/src/lib.rs index 85bac4a..0c77973 100644 --- a/services/core_service/src/lib.rs +++ b/services/core_service/src/lib.rs @@ -19,7 +19,7 @@ use asset_common::{ logi, definition::{AssetMap, Result, Tag, Value}, }; -use asset_ipc_define_lib::asset_service::{AssetBroker, AssetStub, ASSET_SERVICE_ID}; +use asset_ipc::asset_service::{IAsset, AssetStub, ASSET_SERVICE_ID}; use ipc_rust::{IRemoteBroker, RemoteObj}; @@ -38,17 +38,16 @@ pub struct AssetService; impl IRemoteBroker for AssetService {} -impl AssetBroker for AssetService { +impl IAsset for AssetService { fn insert(&self, _input: &AssetMap) -> Result { let mut map = AssetMap::new(); map.insert(Tag::AuthType, Value::NUMBER(2)); // to do Ok(map) } - fn add(&self, input: &AssetMap) -> Result { + fn add(&self, input: &AssetMap) -> Result<()> { // get calling uid userid appid etc - let calling_info = CallingInfo::new()?; - + let calling_info = CallingInfo::new(); operations::add(input, &calling_info) } } diff --git a/services/core_service/src/operations/operation_add.rs b/services/core_service/src/operations/operation_add.rs index 997d141..b36f6c2 100644 --- a/services/core_service/src/operations/operation_add.rs +++ b/services/core_service/src/operations/operation_add.rs @@ -51,7 +51,7 @@ fn construct_data<'a>(input: &'a AssetMap, calling_info: &'a CallingInfo) -> Res Ok(data_vec) } -pub(crate) fn add(input: &AssetMap, calling_info: &CallingInfo) -> Result { +pub(crate) fn add(input: &AssetMap, calling_info: &CallingInfo) -> Result<()> { // arrange the table value let mut db_data = construct_data(input, calling_info)?; @@ -90,5 +90,5 @@ pub(crate) fn add(input: &AssetMap, calling_info: &CallingInfo) -> Result(input: &'a AssetMap, column_name: &'a str, tag: T logi!("get {} {} successfully", @public(column_name), @public(tag as u32)); return Ok(()); } - loge!("{} missed", @public(tag as u32)); + loge!("{:x} missed", @public(tag as u32)); Err(ErrCode::InvalidArgument) } diff --git a/test/unittest/inner_api_rust/src/main.rs b/test/unittest/inner_api_rust/src/main.rs index 931d1e3..ce6e6b2 100644 --- a/test/unittest/inner_api_rust/src/main.rs +++ b/test/unittest/inner_api_rust/src/main.rs @@ -15,7 +15,7 @@ use core::panic; -use asset_rust_sdk::definition::{AssetMap, Accessibility, Tag, InsertAttribute, AuthType, SyncType, Value}; +use asset_rust_sdk::definition::{AssetMap, Accessibility, Tag, InsertAttribute, AuthType, SyncType}; #[test] fn test_for_add() { @@ -27,8 +27,6 @@ fn test_for_add() { input.insert_attr(Tag::Accessibility, Accessibility::DeviceSecure).unwrap(); input.insert_attr(Tag::Alias, Vec::from("alias".as_bytes())).unwrap(); - input.insert(Tag::Accessibility, Value::NUMBER(Accessibility::DeviceSecure as u32)); - match asset_rust_sdk::add(input) { Ok(_) => (), Err(err) => {