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 //! demo rust SA
17 extern crate ipc_rust;
18 extern crate listen_rust_ipc;
19 extern crate audio_rust_ipc;
20 extern crate system_ability_fwk_rust;
21 extern crate rust_samgr;
22 use std::ffi::{c_char, CString};
23 use hilog_rust::{error, info, hilog, HiLogLabel, LogType};
24 const LOG_LABEL: HiLogLabel = HiLogLabel {
25 log_type: LogType::LogCore,
26 domain: 0xd001800,
27 tag: "rustSA"
28 };
29
30 use ipc_rust::{
31 IRemoteBroker, IpcResult, RemoteObj
32 };
33 use system_ability_fwk_rust::{
34 define_system_ability, RSystemAbility, ISystemAbility, IMethod
35 };
36
37 use rust_samgr::{
38 get_service_proxy
39 };
40
41 use listen_rust_ipc::{
42 IListen, TestStub
43 };
44
45 use audio_rust_ipc::{
46 ITest
47 };
48
49 /// TEST_LISTEN_ID SAID
50 pub const TEST_LISTEN_ID: i32 = 1494;
51 /// TEST_AUDIO_ID SAID
52 pub const TEST_AUDIO_ID: i32 = 1499;
53
54 /// TestService type
55 pub struct TestService;
56
57 impl IListen for TestService {
echo_str(&self, value: &str) -> IpcResult<String>58 fn echo_str(&self, value: &str) -> IpcResult<String> {
59 info!(LOG_LABEL,"TestService echo_str: {}", value);
60 Ok(String::from(value))
61 }
62
request_concurent(&self, is_async: bool) -> IpcResult<bool>63 fn request_concurent(&self, is_async: bool) -> IpcResult<bool> {
64 info!(LOG_LABEL,"TestService request_concurent: {}", is_async);
65 Ok(true)
66 }
67 }
68
69 impl IRemoteBroker for TestService {}
70
on_start<T: ISystemAbility + IMethod>(ability: &T)71 fn on_start<T: ISystemAbility + IMethod>(ability: &T) {
72 info!(LOG_LABEL,"1494on_start");
73 let object = get_service_proxy::<dyn ITest>(TEST_AUDIO_ID);
74 let object = match object {
75 Ok(object) => object,
76 Err(error) => {
77 error!(LOG_LABEL,"convert RemoteObj to TestProxy failed: {}", error);
78 panic!();
79 }
80 };
81 let _value = object.unload().expect("should return string");
82 let service = TestStub::new_remote_stub(TestService).expect("create TestService failed");
83 ability.publish(&service.as_object().expect("get IListen service failed"), -1);
84 }
85
on_stop<T: ISystemAbility + IMethod>(ability: &T)86 fn on_stop<T: ISystemAbility + IMethod>(ability: &T) {
87 info!(LOG_LABEL,"on_stop");
88 let service = TestStub::new_remote_stub(TestService).expect("create TestService failed");
89 ability.publish(&service.as_object().expect("get IListen service failed"), -1);
90 }
91
92 define_system_ability!(
93 sa: SystemAbility(on_start, on_stop),
94 );
95
96 #[used]
97 #[link_section = ".init_array"]
98 static A:extern fn() = {
init()99 extern fn init() {
100 let r_sa = SystemAbility::new_system_ability(TEST_LISTEN_ID, true).expect("create TestService failed");
101 r_sa.register();
102 }
103 init
104 };