• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_IPC_FOO_SERVICE_H
17 #define OHOS_IPC_FOO_SERVICE_H
18 
19 #include <mutex>
20 #include <condition_variable>
21 #include "iremote_broker.h"
22 #include "iremote_proxy.h"
23 #include "iremote_stub.h"
24 #include "hilog/log.h"
25 #include "log_tags.h"
26 
27 namespace OHOS {
28 
29 class IFoo : public IRemoteBroker {
30 public:
31     enum FooInterFaceId {
32         GET_FOO_NAME = 0,
33         SEND_ASYNC_REPLY = 1,
34         SEND_WRONG_REPLY = 2,
35     };
36     virtual std::string GetFooName() = 0;
37     virtual void SendAsyncReply(int &reply) = 0;
38     virtual int TestNestingSend(int sendCode) = 0;
39 public:
40     DECLARE_INTERFACE_DESCRIPTOR(u"test.ipc.IFoo");
41 };
42 
43 class FooStub : public IRemoteStub<IFoo> {
44 public:
45     virtual ~FooStub();
46     int OnRemoteRequest(uint32_t code,
47         MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
48     std::string GetFooName() override;
49     void SendAsyncReply(int &reply) override;
50     int WaitForAsyncReply(int timeout);
51     static void CleanDecTimes();
52     static int GetDecTimes();
53     int TestNestingSend(int sendCode) override;
54 public:
55     static std::mutex decTimeMutex_;
56     static int decTimes_;
57 private:
58     int asyncReply_ = { 0 };
59     std::mutex mutex_;
60     std::condition_variable cv_;
61     static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_IPC, "FooStub" };
62 };
63 
64 class FooProxy : public IRemoteProxy<IFoo> {
65 public:
66     explicit FooProxy(const sptr<IRemoteObject> &impl);
67     ~FooProxy() = default;
68     std::string GetFooName() override;
69     void SendAsyncReply(int &reply) override;
70     int TestNestingSend(int sendCode) override;
71 private:
72     static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_IPC, "FooProxy" };
73     static inline BrokerDelegator<FooProxy> delegator_;
74 };
75 } // namespace OHOS
76 #endif // OHOS_IPC_FOO_SERVICE_H