15ba71b47Sopenharmony_ci// Copyright (C) 2024 Huawei Device Co., Ltd.
25ba71b47Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
35ba71b47Sopenharmony_ci// you may not use this file except in compliance with the License.
45ba71b47Sopenharmony_ci// You may obtain a copy of the License at
55ba71b47Sopenharmony_ci//
65ba71b47Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
75ba71b47Sopenharmony_ci//
85ba71b47Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
95ba71b47Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
105ba71b47Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
115ba71b47Sopenharmony_ci// See the License for the specific language governing permissions and
125ba71b47Sopenharmony_ci// limitations under the License.
135ba71b47Sopenharmony_ci
145ba71b47Sopenharmony_ciuse std::pin::Pin;
155ba71b47Sopenharmony_ci
165ba71b47Sopenharmony_cipub(crate) use ffi::*;
175ba71b47Sopenharmony_ciuse ipc::cxx_share::RemoteStubWrapper;
185ba71b47Sopenharmony_ciuse ipc::remote::RemoteStub;
195ba71b47Sopenharmony_ci
205ba71b47Sopenharmony_ci#[cxx::bridge(namespace = "OHOS::SamgrRust")]
215ba71b47Sopenharmony_cimod ffi {
225ba71b47Sopenharmony_ci
235ba71b47Sopenharmony_ci    #[derive(Debug)]
245ba71b47Sopenharmony_ci    struct SystemProcessInfo {
255ba71b47Sopenharmony_ci        processName: String,
265ba71b47Sopenharmony_ci        pid: i32,
275ba71b47Sopenharmony_ci        uid: i32,
285ba71b47Sopenharmony_ci    }
295ba71b47Sopenharmony_ci
305ba71b47Sopenharmony_ci    struct AddSystemAbilityConfig {
315ba71b47Sopenharmony_ci        is_distributed: bool,
325ba71b47Sopenharmony_ci        dump_flags: u32,
335ba71b47Sopenharmony_ci        capability: String,
345ba71b47Sopenharmony_ci        permission: String,
355ba71b47Sopenharmony_ci    }
365ba71b47Sopenharmony_ci
375ba71b47Sopenharmony_ci    extern "Rust" {
385ba71b47Sopenharmony_ci        type AbilityStub;
395ba71b47Sopenharmony_ci
405ba71b47Sopenharmony_ci        fn on_remote_request(
415ba71b47Sopenharmony_ci            self: &mut AbilityStub,
425ba71b47Sopenharmony_ci            code: u32,
435ba71b47Sopenharmony_ci            data: Pin<&mut MessageParcel>,
445ba71b47Sopenharmony_ci            reply: Pin<&mut MessageParcel>,
455ba71b47Sopenharmony_ci        ) -> i32;
465ba71b47Sopenharmony_ci
475ba71b47Sopenharmony_ci        fn dump(self: &mut AbilityStub, fd: i32, args: Vec<String>) -> i32;
485ba71b47Sopenharmony_ci
495ba71b47Sopenharmony_ci    }
505ba71b47Sopenharmony_ci
515ba71b47Sopenharmony_ci    unsafe extern "C++" {
525ba71b47Sopenharmony_ci        include!("system_ability_manager_wrapper.h");
535ba71b47Sopenharmony_ci        include!("status_change_wrapper.h");
545ba71b47Sopenharmony_ci
555ba71b47Sopenharmony_ci        #[namespace = OHOS]
565ba71b47Sopenharmony_ci        type SptrIRemoteObject = ipc::cxx_share::SptrIRemoteObject;
575ba71b47Sopenharmony_ci
585ba71b47Sopenharmony_ci        #[namespace = "OHOS"]
595ba71b47Sopenharmony_ci        type MessageParcel = ipc::cxx_share::MessageParcel;
605ba71b47Sopenharmony_ci
615ba71b47Sopenharmony_ci        pub type UnSubscribeSystemAbilityHandler;
625ba71b47Sopenharmony_ci        pub type UnSubscribeSystemProcessHandler;
635ba71b47Sopenharmony_ci        fn LoadSystemAbility(said: i32, timeout: i32) -> UniquePtr<SptrIRemoteObject>;
645ba71b47Sopenharmony_ci
655ba71b47Sopenharmony_ci        fn LoadSystemAbilityWithCallback(said: i32, on_success: fn(), on_fail: fn()) -> i32;
665ba71b47Sopenharmony_ci
675ba71b47Sopenharmony_ci        fn GetSystemAbility(said: i32) -> UniquePtr<SptrIRemoteObject>;
685ba71b47Sopenharmony_ci
695ba71b47Sopenharmony_ci        fn GetContextManager() -> UniquePtr<SptrIRemoteObject>;
705ba71b47Sopenharmony_ci
715ba71b47Sopenharmony_ci        fn CheckSystemAbility(said: i32) -> UniquePtr<SptrIRemoteObject>;
725ba71b47Sopenharmony_ci
735ba71b47Sopenharmony_ci        fn GetSystemAbilityWithDeviceId(
745ba71b47Sopenharmony_ci            said: i32,
755ba71b47Sopenharmony_ci            deviceId: &CxxString,
765ba71b47Sopenharmony_ci        ) -> UniquePtr<SptrIRemoteObject>;
775ba71b47Sopenharmony_ci
785ba71b47Sopenharmony_ci        fn RemoveSystemAbility(said: i32) -> i32;
795ba71b47Sopenharmony_ci
805ba71b47Sopenharmony_ci        fn CheckSystemAbilityWithDeviceId(
815ba71b47Sopenharmony_ci            said: i32,
825ba71b47Sopenharmony_ci            deviceId: &CxxString,
835ba71b47Sopenharmony_ci        ) -> UniquePtr<SptrIRemoteObject>;
845ba71b47Sopenharmony_ci
855ba71b47Sopenharmony_ci        fn ListSystemAbilities() -> Vec<String>;
865ba71b47Sopenharmony_ci        fn ListSystemAbilitiesWithDumpFlag(dump_flag: u32) -> Vec<String>;
875ba71b47Sopenharmony_ci
885ba71b47Sopenharmony_ci        fn SubscribeSystemAbility(
895ba71b47Sopenharmony_ci            said: i32,
905ba71b47Sopenharmony_ci            on_add: fn(i32, &str),
915ba71b47Sopenharmony_ci            on_remove: fn(i32, &str),
925ba71b47Sopenharmony_ci        ) -> UniquePtr<UnSubscribeSystemAbilityHandler>;
935ba71b47Sopenharmony_ci
945ba71b47Sopenharmony_ci        fn UnSubscribe(self: Pin<&mut UnSubscribeSystemAbilityHandler>);
955ba71b47Sopenharmony_ci        fn AddOnDemandSystemAbilityInfo(said: i32, localAbilityManagerName: &str) -> i32;
965ba71b47Sopenharmony_ci
975ba71b47Sopenharmony_ci        fn UnloadSystemAbility(said: i32) -> i32;
985ba71b47Sopenharmony_ci        fn CancelUnloadSystemAbility(said: i32) -> i32;
995ba71b47Sopenharmony_ci        fn UnloadAllIdleSystemAbility() -> i32;
1005ba71b47Sopenharmony_ci        fn GetSystemProcessInfo(said: i32) -> SystemProcessInfo;
1015ba71b47Sopenharmony_ci
1025ba71b47Sopenharmony_ci        fn GetRunningSystemProcess() -> Vec<SystemProcessInfo>;
1035ba71b47Sopenharmony_ci        fn SendStrategy(
1045ba71b47Sopenharmony_ci            s_type: i32,
1055ba71b47Sopenharmony_ci            saids: Vec<i32>,
1065ba71b47Sopenharmony_ci            level: i32,
1075ba71b47Sopenharmony_ci            action: Pin<&mut CxxString>,
1085ba71b47Sopenharmony_ci        ) -> i32;
1095ba71b47Sopenharmony_ci
1105ba71b47Sopenharmony_ci        fn GetCommonEventExtraDataIdlist(
1115ba71b47Sopenharmony_ci            said: i32,
1125ba71b47Sopenharmony_ci            extraids: &mut Vec<i64>,
1135ba71b47Sopenharmony_ci            event_name: &CxxString,
1145ba71b47Sopenharmony_ci        ) -> i32;
1155ba71b47Sopenharmony_ci
1165ba71b47Sopenharmony_ci        fn AddSystemAbility(
1175ba71b47Sopenharmony_ci            said: i32,
1185ba71b47Sopenharmony_ci            ability: Box<AbilityStub>,
1195ba71b47Sopenharmony_ci            config: AddSystemAbilityConfig,
1205ba71b47Sopenharmony_ci        ) -> i32;
1215ba71b47Sopenharmony_ci
1225ba71b47Sopenharmony_ci        fn SubscribeSystemProcess(
1235ba71b47Sopenharmony_ci            on_start: fn(&SystemProcessInfo),
1245ba71b47Sopenharmony_ci            on_stop: fn(&SystemProcessInfo),
1255ba71b47Sopenharmony_ci        ) -> UniquePtr<UnSubscribeSystemProcessHandler>;
1265ba71b47Sopenharmony_ci
1275ba71b47Sopenharmony_ci        fn UnSubscribe(self: Pin<&mut UnSubscribeSystemProcessHandler>);
1285ba71b47Sopenharmony_ci
1295ba71b47Sopenharmony_ci        fn GetOnDemandReasonExtraData(extra_data_id: i64, parcel: Pin<&mut MessageParcel>) -> i32;
1305ba71b47Sopenharmony_ci    }
1315ba71b47Sopenharmony_ci}
1325ba71b47Sopenharmony_ci
1335ba71b47Sopenharmony_cipub struct AbilityStub {
1345ba71b47Sopenharmony_ci    remote: RemoteStubWrapper,
1355ba71b47Sopenharmony_ci}
1365ba71b47Sopenharmony_ci
1375ba71b47Sopenharmony_ciimpl AbilityStub {
1385ba71b47Sopenharmony_ci    pub fn new<A: RemoteStub + 'static>(remote: A) -> Self {
1395ba71b47Sopenharmony_ci        Self {
1405ba71b47Sopenharmony_ci            remote: RemoteStubWrapper::new(remote),
1415ba71b47Sopenharmony_ci        }
1425ba71b47Sopenharmony_ci    }
1435ba71b47Sopenharmony_ci
1445ba71b47Sopenharmony_ci    fn on_remote_request(
1455ba71b47Sopenharmony_ci        &mut self,
1465ba71b47Sopenharmony_ci        code: u32,
1475ba71b47Sopenharmony_ci        data: Pin<&mut MessageParcel>,
1485ba71b47Sopenharmony_ci        reply: Pin<&mut MessageParcel>,
1495ba71b47Sopenharmony_ci    ) -> i32 {
1505ba71b47Sopenharmony_ci        self.remote.on_remote_request(code, data, reply)
1515ba71b47Sopenharmony_ci    }
1525ba71b47Sopenharmony_ci
1535ba71b47Sopenharmony_ci    fn dump(&mut self, fd: i32, args: Vec<String>) -> i32 {
1545ba71b47Sopenharmony_ci        self.remote.dump(fd, args)
1555ba71b47Sopenharmony_ci    }
1565ba71b47Sopenharmony_ci}
157