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 //! Implementation of SA of device status service.
17 
18 #![allow(unused_variables)]
19 
20 use std::ffi::{ c_char, CString };
21 use fusion_ipc_service_rust::{ FusionIpcStub, MSDP_DEVICESTATUS_SERVICE_ID };
22 use fusion_services_rust::FusionService;
23 use hilog_rust::{ info, error, hilog, HiLogLabel, LogType };
24 use ipc_rust::{ IRemoteBroker, RemoteObj };
25 use system_ability_fwk_rust::{ define_system_ability, RSystemAbility, ISystemAbility, IMethod };
26 use crate::fusion_ipc_delegator::FusionIpcDelegator;
27 
28 const LOG_LABEL: HiLogLabel = HiLogLabel {
29     log_type: LogType::LogCore,
30     domain: 0xD002220,
31     tag: "DeviceStatusSA",
32 };
33 
on_start<T: ISystemAbility + IMethod>(ability: &T)34 fn on_start<T: ISystemAbility + IMethod>(ability: &T) {
35     info!(LOG_LABEL, "Create remote stub");
36     if let Some(service) = FusionIpcStub::new_remote_stub(FusionIpcDelegator::new()) {
37         if let Some(obj) = service.as_object() {
38             info!(LOG_LABEL, "Publishing service");
39             ability.publish(&obj, MSDP_DEVICESTATUS_SERVICE_ID);
40 
41             if let Some(proxy) = FusionService::get_instance() {
42                 proxy.on_start();
43             } else {
44                 error!(LOG_LABEL, "No proxy");
45             }
46         } else {
47             error!(LOG_LABEL, "Remote object is none");
48         }
49     } else {
50         error!(LOG_LABEL, "Can not create remote stub");
51     }
52 }
53 
on_stop<T: ISystemAbility + IMethod>(ability: &T)54 fn on_stop<T: ISystemAbility + IMethod>(ability: &T) {
55     info!(LOG_LABEL, "In on_stop(): enter");
56     if let Some(proxy) = FusionService::get_instance() {
57         proxy.on_stop();
58     } else {
59         error!(LOG_LABEL, "No proxy");
60     }
61 }
62 
63 define_system_ability!(
64     sa: SystemAbility(on_start, on_stop),
65 );
66 
67 #[used]
68 #[link_section = ".init_array"]
69 static A: extern fn() = {
init()70     extern fn init() {
71         let sa = SystemAbility::new_system_ability(MSDP_DEVICESTATUS_SERVICE_ID, true).expect(
72             "Failed to create sa instance"
73         );
74         sa.register();
75     }
76 
77     init
78 };
79