1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 //! This module implements the stub of the Asset service.
17
18 use ipc_rust::{BorrowedMsgParcel, FileDesc, IRemoteStub, IpcResult, IpcStatusCode, RemoteStub, String16};
19
20 use asset_definition::{AssetError, Result};
21 use asset_ipc::{deserialize_map, serialize_maps, IAsset, IpcCode, IPC_SUCCESS, SA_NAME};
22 use asset_log::loge;
23
24 /// IPC stub type.
25 pub struct AssetStub(Box<dyn IAsset + Sync + Send>);
26
27 impl AssetStub {
28 /// Create a new remote stub service.
new_remote_stub<T: IAsset + Send + Sync + 'static>(obj: T) -> Option<RemoteStub<Self>>29 pub fn new_remote_stub<T: IAsset + Send + Sync + 'static>(obj: T) -> Option<RemoteStub<Self>> {
30 RemoteStub::new(AssetStub(Box::new(obj)))
31 }
32 }
33
34 impl IRemoteStub for AssetStub {
35 /// Get stub object descriptor.
get_descriptor() -> &'static str36 fn get_descriptor() -> &'static str {
37 SA_NAME
38 }
39
40 /// Callback to deal IPC request for this stub.
on_remote_request(&self, code: u32, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> i3241 fn on_remote_request(&self, code: u32, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> i32 {
42 match on_remote_request(&*self.0, code, data, reply) {
43 Ok(_) => IPC_SUCCESS as i32,
44 Err(e) => e as i32,
45 }
46 }
47
48 /// Callback to dump.
on_dump(&self, file: &FileDesc, args: &mut Vec<String16>) -> i3249 fn on_dump(&self, file: &FileDesc, args: &mut Vec<String16>) -> i32 {
50 self.0.dump(file, args)
51 }
52 }
53
asset_err_handle(e: AssetError) -> IpcStatusCode54 fn asset_err_handle(e: AssetError) -> IpcStatusCode {
55 loge!("[IPC]Asset error code = {}, msg is {}", e.code, e.msg);
56 IpcStatusCode::InvalidValue
57 }
58
reply_handle(ret: Result<()>, reply: &mut BorrowedMsgParcel) -> IpcResult<()>59 fn reply_handle(ret: Result<()>, reply: &mut BorrowedMsgParcel) -> IpcResult<()> {
60 match ret {
61 Ok(_) => reply.write::<u32>(&IPC_SUCCESS),
62 Err(e) => {
63 reply.write::<u32>(&(e.code as u32))?;
64 reply.write::<String>(&e.msg)
65 },
66 }
67 }
68
on_remote_request( stub: &dyn IAsset, code: u32, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel, ) -> IpcResult<()>69 fn on_remote_request(
70 stub: &dyn IAsset,
71 code: u32,
72 data: &BorrowedMsgParcel,
73 reply: &mut BorrowedMsgParcel,
74 ) -> IpcResult<()> {
75 let ipc_code = IpcCode::try_from(code).map_err(asset_err_handle)?;
76 let map = deserialize_map(data).map_err(asset_err_handle)?;
77 match ipc_code {
78 IpcCode::Add => reply_handle(stub.add(&map), reply),
79 IpcCode::Remove => reply_handle(stub.remove(&map), reply),
80 IpcCode::Update => {
81 let update_map = deserialize_map(data).map_err(asset_err_handle)?;
82 reply_handle(stub.update(&map, &update_map), reply)
83 },
84 IpcCode::PreQuery => match stub.pre_query(&map) {
85 Ok(res) => {
86 reply_handle(Ok(()), reply)?;
87 reply.write::<Vec<u8>>(&res)
88 },
89 Err(e) => reply_handle(Err(e), reply),
90 },
91 IpcCode::Query => match stub.query(&map) {
92 Ok(res) => {
93 reply_handle(Ok(()), reply)?;
94 serialize_maps(&res, reply).map_err(asset_err_handle)
95 },
96 Err(e) => reply_handle(Err(e), reply),
97 },
98 IpcCode::PostQuery => reply_handle(stub.post_query(&map), reply),
99 }
100 }
101