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 15 use ffi::*; 16 17 use crate::remote::RemoteObj; 18 19 #[cxx::bridge(namespace = "OHOS::IpcRust")] 20 pub mod ffi { 21 22 unsafe extern "C++" { 23 include!("skeleton_wrapper.h"); 24 type IRemoteObjectWrapper = crate::remote::wrapper::IRemoteObjectWrapper; 25 SetMaxWorkThreadNum(maxThreadNum: i32) -> bool26 fn SetMaxWorkThreadNum(maxThreadNum: i32) -> bool; 27 JoinWorkThread()28 fn JoinWorkThread(); 29 StopWorkThread()30 fn StopWorkThread(); 31 GetCallingPid() -> u6432 fn GetCallingPid() -> u64; 33 GetCallingRealPid() -> u6434 fn GetCallingRealPid() -> u64; 35 GetCallingUid() -> u6436 fn GetCallingUid() -> u64; 37 GetCallingTokenID() -> u3238 fn GetCallingTokenID() -> u32; 39 GetCallingFullTokenID() -> u6440 fn GetCallingFullTokenID() -> u64; 41 GetFirstTokenID() -> u3242 fn GetFirstTokenID() -> u32; 43 GetFirstFullTokenID() -> u6444 fn GetFirstFullTokenID() -> u64; 45 GetSelfTokenID() -> u6446 fn GetSelfTokenID() -> u64; 47 GetLocalDeviceID() -> String48 fn GetLocalDeviceID() -> String; 49 GetCallingDeviceID() -> String50 fn GetCallingDeviceID() -> String; 51 IsLocalCalling() -> bool52 fn IsLocalCalling() -> bool; 53 GetContextObject() -> UniquePtr<IRemoteObjectWrapper>54 fn GetContextObject() -> UniquePtr<IRemoteObjectWrapper>; 55 FlushCommands(object: Pin<&mut IRemoteObjectWrapper>) -> i3256 fn FlushCommands(object: Pin<&mut IRemoteObjectWrapper>) -> i32; 57 ResetCallingIdentity() -> String58 fn ResetCallingIdentity() -> String; 59 SetCallingIdentity(identity: &str) -> bool60 fn SetCallingIdentity(identity: &str) -> bool; 61 IsHandlingTransaction() -> bool62 fn IsHandlingTransaction() -> bool; 63 } 64 } 65 66 /// Ipc Skeleton 67 pub struct Skeleton; 68 69 impl Skeleton { 70 /// Sets the maximum number of threads. set_max_work_thread_num(max_thread_num: i32) -> bool71 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. join_work_thread()76 pub fn join_work_thread() { 77 JoinWorkThread(); 78 } 79 80 /// Exits current thread from IPC/RPC work thread pool. stop_work_thread()81 pub fn stop_work_thread() { 82 StopWorkThread(); 83 } 84 85 /// Returns the calling process id of caller. calling_pid() -> u6486 pub fn calling_pid() -> u64 { 87 GetCallingPid() 88 } 89 90 /// Returns the calling process id of caller. calling_real_pid() -> u6491 pub fn calling_real_pid() -> u64 { 92 GetCallingRealPid() 93 } 94 95 /// Returns the calling user id of caller. calling_uid() -> u6496 pub fn calling_uid() -> u64 { 97 GetCallingUid() 98 } 99 100 /// Returns the calling token ID of caller. calling_token_id() -> u32101 pub fn calling_token_id() -> u32 { 102 GetCallingTokenID() 103 } 104 105 /// Returns the calling token ID of caller. calling_full_token_id() -> u64106 pub fn calling_full_token_id() -> u64 { 107 GetCallingFullTokenID() 108 } 109 110 /// Returns the the first token ID. first_token_id() -> u32111 pub fn first_token_id() -> u32 { 112 GetFirstTokenID() 113 } 114 115 /// Returns the the first full token ID. first_full_token_id() -> u64116 pub fn first_full_token_id() -> u64 { 117 GetFirstFullTokenID() 118 } 119 120 /// Returns the the token ID of the self. self_token_id() -> u64121 pub fn self_token_id() -> u64 { 122 GetSelfTokenID() 123 } 124 125 /// Returns the local device ID. local_device_id() -> String126 pub fn local_device_id() -> String { 127 GetLocalDeviceID() 128 } 129 130 /// Returns the calling device id. calling_device_id() -> String131 pub fn calling_device_id() -> String { 132 GetCallingDeviceID() 133 } 134 135 /// Returns true if it is a local call. is_local_calling() -> bool136 pub fn is_local_calling() -> bool { 137 IsLocalCalling() 138 } 139 140 /// Returns the context object. get_context_object() -> Option<RemoteObj>141 pub fn get_context_object() -> Option<RemoteObj> { 142 RemoteObj::try_new(GetContextObject()) 143 } 144 145 /// Flushes all pending commands. flush_commands(remote: &mut RemoteObj) -> i32146 pub fn flush_commands(remote: &mut RemoteObj) -> i32 { 147 FlushCommands(remote.inner.pin_mut()) 148 } 149 150 /// Resets calling identity. reset_calling_identity() -> String151 pub fn reset_calling_identity() -> String { 152 ResetCallingIdentity() 153 } 154 155 /// Sets calling identity. set_calling_identity(identity: &str) -> bool156 pub fn set_calling_identity(identity: &str) -> bool { 157 SetCallingIdentity(identity) 158 } 159 } 160