1 /* 2 * Copyright (C) 2021 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 #ifndef OHOS_DBINDER_TEST_SERVICE_SKELETON_H 17 #define OHOS_DBINDER_TEST_SERVICE_SKELETON_H 18 19 #include "ipc_types.h" 20 #include "iremote_broker.h" 21 #include "iremote_object.h" 22 #include "iremote_stub.h" 23 #include "iremote_proxy.h" 24 #include "hilog/log.h" 25 #include "log_tags.h" 26 #include "hitrace/trace.h" 27 28 namespace OHOS { 29 class IDBinderTestService : public IRemoteBroker { 30 public: 31 enum { 32 REVERSEINT = 0, 33 REVERSEINTDELAY = 1, 34 PING_SERVICE = 2, 35 GET_FOO_SERVICE = 3, 36 ONLY_DELAY = 4, 37 TRANS_PROXY_OBJECT = 5, 38 TRANS_OVERSIZED_PKT = 6, 39 TRANS_RAW_DATA = 7, 40 RECEIVE_RAW_DATA = 8, 41 TRANS_TRACE_ID = 9, 42 TRANS_STUB_OBJECT = 10, 43 GET_REMOTE_STUB_OBJECT = 11, 44 GET_REMOTE_DES_TIMES = 12, 45 CLEAR_REMOTE_DES_TIMES = 13, 46 TRANS_OBJECT_OVER_DEVICE_OVER_PROCESS = 14, 47 TRANS_RPC_OBJECT_TO_LOCAL = 15, 48 TRANS_STUB_OBJECT_REFCOUNT = 16, 49 TRANS_PROXY_OBJECT_REFCOUNT = 17, 50 }; 51 52 enum { 53 NOT_SAVE = 0, 54 SAVE = 1, 55 WITHDRAW = 2, 56 }; 57 58 enum { 59 FIRST_OBJECT = 0, // Acquired once will be released automatically 60 SECOND_OBJECT = 1, // Acquired twice will be released automatically 61 }; 62 63 DECLARE_INTERFACE_DESCRIPTOR(u"ohos.rpc.IDBinderTestService"); 64 virtual int ReverseInt(int data, int &rep) = 0; 65 virtual int ReverseIntDelay(int data, int &rep) = 0; 66 virtual int Delay(int data, int &rep) = 0; 67 virtual int PingService(std::u16string &serviceName) = 0; 68 virtual int ReverseIntDelayAsync(int data, int &rep) = 0; 69 virtual int TransProxyObject(int data, sptr<IRemoteObject> &transObject, int operation, int &rep, 70 int &withdrawRes) = 0; 71 virtual int TransProxyObjectRefCount(sptr<IRemoteObject> &transObject, int operation) = 0; 72 virtual int TransProxyObjectAgain(int data, sptr<IRemoteObject> &transObject, int operation, int &rep, 73 int &withdrawRes) = 0; 74 virtual int TransStubObject(int data, sptr<IRemoteObject> &transObject, int &rep, int &stubRep) = 0; 75 virtual int TransStubObjectRefCount(sptr<IRemoteObject> &transObject, int operation) = 0; 76 virtual int TransOversizedPkt(const std::string &dataStr, std::string &repStr) = 0; 77 virtual int ProxyTransRawData(int lengths) = 0; 78 virtual int StubTransRawData(int length) = 0; 79 virtual int GetChildId(uint64_t &rep) = 0; 80 virtual int FlushAsyncCommands(int count, int length) = 0; 81 virtual sptr<IRemoteObject> GetRemoteObject(int type) = 0; 82 virtual int GetRemoteDecTimes() = 0; 83 virtual void ClearRemoteDecTimes() = 0; 84 85 private: 86 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_RPC, "IDBinderTestService" }; 87 }; 88 89 class DBinderTestServiceStub : public IRemoteStub<IDBinderTestService> { 90 public: 91 int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; 92 int ReverseIntDelayAsync(int data, int &rep) override; 93 static pid_t GetLastCallingPid(); 94 static uid_t GetLastCallingUid(); 95 96 private: 97 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_RPC, "DBinderTestStub" }; 98 static pid_t g_lastCallingPid; 99 static pid_t g_lastCallinguid; 100 sptr<IRemoteObject> recvProxy_; 101 102 int OnReverseInt(MessageParcel &data, MessageParcel &reply); 103 int OnReverseIntDelay(MessageParcel &data, MessageParcel &reply); 104 int OnPingService(MessageParcel &data, MessageParcel &reply); 105 int OnDelay(MessageParcel &data, MessageParcel &reply); 106 int OnReceivedObject(MessageParcel &data, MessageParcel &reply); 107 int OnReceivedProxyObjectRefCount(MessageParcel &data, MessageParcel &reply); 108 int OnReceivedObjectTransAgain(MessageParcel &data, MessageParcel &reply); 109 int OnReceivedStubObject(MessageParcel &data, MessageParcel &reply); 110 int OnReceivedStubObjectRefCount(MessageParcel &data, MessageParcel &reply); 111 int OnReceivedOversizedPkt(MessageParcel &data, MessageParcel &reply); 112 int OnReceivedRawData(MessageParcel &data, MessageParcel &reply); 113 int OnGetChildId(MessageParcel &data, MessageParcel &reply); 114 int OnSentRawData(MessageParcel &data, MessageParcel &reply); 115 int OnReceivedGetStubObject(MessageParcel &data, MessageParcel &reply); 116 int OnReceivedGetDecTimes(MessageParcel &data, MessageParcel &reply); 117 int OnReceivedClearDecTimes(MessageParcel &data, MessageParcel &reply); 118 }; 119 120 121 class DBinderTestServiceProxy : public IRemoteProxy<IDBinderTestService> { 122 public: 123 explicit DBinderTestServiceProxy(const sptr<IRemoteObject> &impl); 124 ~DBinderTestServiceProxy() = default; 125 int ReverseInt(int data, int &rep) override; 126 int ReverseIntNullReply(int data, int &rep); 127 int ReverseIntVoidData(int data, int &rep); 128 int ReverseIntDelay(int data, int &rep) override; 129 int Delay(int data, int &rep) override; 130 int ReverseIntDelayAsync(int data, int &rep) override; 131 int PingService(std::u16string &serviceName) override; 132 int TransProxyObject(int data, sptr<IRemoteObject> &transObject, int operation, int &rep, 133 int &withdrawRes) override; 134 int TransProxyObjectAgain(int data, sptr<IRemoteObject> &transObject, int operation, int &rep, 135 int &withdrawRes) override; 136 int TransProxyObjectRefCount(sptr<IRemoteObject> &transObject, int operation) override; 137 int TransStubObject(int data, sptr<IRemoteObject> &transObject, int &rep, int &stubRep) override; 138 int TransOversizedPkt(const std::string &dataStr, std::string &repStr) override; 139 int TransStubObjectRefCount(sptr<IRemoteObject> &transObject, int operation) override; 140 int ProxyTransRawData(int length) override; 141 int StubTransRawData(int length) override; 142 int GetChildId(uint64_t &rep) override; 143 sptr<IRemoteObject> GetRemoteObject(int type) override; 144 int GetRemoteDecTimes() override; 145 void ClearRemoteDecTimes() override; 146 int FlushAsyncCommands(int count, int length) override; 147 148 private: 149 static inline BrokerDelegator<DBinderTestServiceProxy> delegator_; 150 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_RPC, "DBinderTestProxy" }; 151 }; 152 153 154 class DBinderTestDeathRecipient : public IRemoteObject::DeathRecipient { 155 public: 156 virtual void OnRemoteDied(const wptr<IRemoteObject> &remote); 157 DBinderTestDeathRecipient(); 158 virtual ~DBinderTestDeathRecipient(); 159 static bool GotDeathRecipient(); 160 static void ClearDeathRecipient(); 161 static bool g_gotDeathRecipient; 162 163 private: 164 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_RPC, "DBinderTestDeathRecipient" }; 165 }; 166 } // namespace OHOS 167 #endif // OHOS_DBINDER_TEST_SERVICE_SKELETON_H 168