1// Copyright (C) 2024 Huawei Device Co., Ltd.
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6//     http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14
15use ffi::*;
16
17use crate::remote::RemoteObj;
18
19#[cxx::bridge(namespace = "OHOS::IpcRust")]
20pub mod ffi {
21
22    unsafe extern "C++" {
23        include!("skeleton_wrapper.h");
24        type IRemoteObjectWrapper = crate::remote::wrapper::IRemoteObjectWrapper;
25
26        fn SetMaxWorkThreadNum(maxThreadNum: i32) -> bool;
27
28        fn JoinWorkThread();
29
30        fn StopWorkThread();
31
32        fn GetCallingPid() -> u64;
33
34        fn GetCallingRealPid() -> u64;
35
36        fn GetCallingUid() -> u64;
37
38        fn GetCallingTokenID() -> u32;
39
40        fn GetCallingFullTokenID() -> u64;
41
42        fn GetFirstTokenID() -> u32;
43
44        fn GetFirstFullTokenID() -> u64;
45
46        fn GetSelfTokenID() -> u64;
47
48        fn GetLocalDeviceID() -> String;
49
50        fn GetCallingDeviceID() -> String;
51
52        fn IsLocalCalling() -> bool;
53
54        fn GetContextObject() -> UniquePtr<IRemoteObjectWrapper>;
55
56        fn FlushCommands(object: Pin<&mut IRemoteObjectWrapper>) -> i32;
57
58        fn ResetCallingIdentity() -> String;
59
60        fn SetCallingIdentity(identity: &str) -> bool;
61
62        fn IsHandlingTransaction() -> bool;
63    }
64}
65
66/// Ipc Skeleton
67pub struct Skeleton;
68
69impl Skeleton {
70    /// Sets the maximum number of threads.
71    pub fn set_max_work_thread_num(max_thread_num: i32) -> bool {
72        SetMaxWorkThreadNum(max_thread_num)
73    }
74
75    /// Makes current thread join to the IPC/RPC work thread pool.
76    pub fn join_work_thread() {
77        JoinWorkThread();
78    }
79
80    /// Exits current thread from IPC/RPC work thread pool.
81    pub fn stop_work_thread() {
82        StopWorkThread();
83    }
84
85    /// Returns the calling process id of caller.
86    pub fn calling_pid() -> u64 {
87        GetCallingPid()
88    }
89
90    /// Returns the calling process id of caller.
91    pub fn calling_real_pid() -> u64 {
92        GetCallingRealPid()
93    }
94
95    /// Returns the calling user id of caller.
96    pub fn calling_uid() -> u64 {
97        GetCallingUid()
98    }
99
100    /// Returns the calling token ID of caller.
101    pub fn calling_token_id() -> u32 {
102        GetCallingTokenID()
103    }
104
105    /// Returns the calling token ID of caller.
106    pub fn calling_full_token_id() -> u64 {
107        GetCallingFullTokenID()
108    }
109
110    /// Returns the the first token ID.
111    pub fn first_token_id() -> u32 {
112        GetFirstTokenID()
113    }
114
115    /// Returns the the first full token ID.
116    pub fn first_full_token_id() -> u64 {
117        GetFirstFullTokenID()
118    }
119
120    /// Returns the the token ID of the self.
121    pub fn self_token_id() -> u64 {
122        GetSelfTokenID()
123    }
124
125    /// Returns the local device ID.
126    pub fn local_device_id() -> String {
127        GetLocalDeviceID()
128    }
129
130    /// Returns the calling device id.
131    pub fn calling_device_id() -> String {
132        GetCallingDeviceID()
133    }
134
135    /// Returns true if it is a local call.
136    pub fn is_local_calling() -> bool {
137        IsLocalCalling()
138    }
139
140    /// Returns the context object.
141    pub fn get_context_object() -> Option<RemoteObj> {
142        RemoteObj::try_new(GetContextObject())
143    }
144
145    /// Flushes all pending commands.
146    pub fn flush_commands(remote: &mut RemoteObj) -> i32 {
147        FlushCommands(remote.inner.pin_mut())
148    }
149
150    /// Resets calling identity.
151    pub fn reset_calling_identity() -> String {
152        ResetCallingIdentity()
153    }
154
155    /// Sets calling identity.
156    pub fn set_calling_identity(identity: &str) -> bool {
157        SetCallingIdentity(identity)
158    }
159}
160