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 HDF_REMOTE_ADPATER_H
17 #define HDF_REMOTE_ADPATER_H
18 
19 #include <ipc_object_stub.h>
20 #include <parcel.h>
21 #include <string>
22 
23 #include "hdf_remote_adapter_if.h"
24 #include "hdf_remote_service.h"
25 #include <shared_mutex>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif /* __cplusplus */
30 
31 class HdfRemoteServiceStub : public OHOS::IPCObjectStub {
32 public:
33     explicit HdfRemoteServiceStub(struct HdfRemoteService *service);
34     int OnRemoteRequest(uint32_t code,
35         OHOS::MessageParcel &data, OHOS::MessageParcel &reply, OHOS::MessageOption &option) override;
36     ~HdfRemoteServiceStub();
37     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
38     void HdfRemoteStubClearHolder();
39 private:
40     struct HdfRemoteService *service_;
41     std::shared_mutex mutex_;
42 };
43 
44 class HdfDeathNotifier : public OHOS::IRemoteObject::DeathRecipient {
45 public:
46     HdfDeathNotifier(struct HdfRemoteService *service, struct HdfDeathRecipient *recipient);
47     virtual ~HdfDeathNotifier();
48     void OnRemoteDied(const OHOS::wptr<OHOS::IRemoteObject> &object)  override;
49 
50 private:
51     struct HdfDeathRecipient *recipient_;
52     struct HdfRemoteService *service_;
53 };
54 
55 struct HdfRemoteServiceHolder {
56     HdfRemoteServiceHolder();
57     ~HdfRemoteServiceHolder();
58 
59     bool SetInterfaceDescriptor(const char *desc);
60     struct HdfRemoteService service_;
61     OHOS::sptr<OHOS::IRemoteObject> remote_;
62     OHOS::sptr<OHOS::IRemoteObject::DeathRecipient> deathRecipient_;
63     std::u16string descriptor_;
64 };
65 
66 struct HdfRemoteService *HdfRemoteAdapterBind(OHOS::sptr<OHOS::IRemoteObject> binder);
67 
68 #ifdef __cplusplus
69 }
70 #endif /* __cplusplus */
71 #endif /* HDF_REMOTE_ADPATER_H */
72