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