1 /*
2  * Copyright (c) 2021-2023 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 DISTRIBUTED_INPUT_SOURCE_HANDLER_H
17 #define DISTRIBUTED_INPUT_SOURCE_HANDLER_H
18 
19 #include <condition_variable>
20 #include <mutex>
21 #include <string>
22 
23 #include "idistributed_hardware_source.h"
24 #include "iservice_registry.h"
25 #include "single_instance.h"
26 #include "system_ability_load_callback_stub.h"
27 #include "system_ability_definition.h"
28 
29 #include "distributed_input_client.h"
30 #include "distributed_input_source_manager.h"
31 #include "i_distributed_source_input.h"
32 #include "load_d_input_source_callback.h"
33 
34 namespace OHOS {
35 namespace DistributedHardware {
36 namespace DistributedInput {
37 class DistributedInputSourceHandler : public IDistributedHardwareSource {
38 DECLARE_SINGLE_INSTANCE_BASE(DistributedInputSourceHandler);
39 public:
40     int32_t InitSource(const std::string &params) override;
41     int32_t ReleaseSource() override;
42     int32_t RegisterDistributedHardware(const std::string &devId, const std::string &dhId,
43         const EnableParam &param, std::shared_ptr<RegisterCallback> callback) override;
44     int32_t UnregisterDistributedHardware(const std::string &devId, const std::string &dhId,
45         std::shared_ptr<UnregisterCallback> callback) override;
46     int32_t ConfigDistributedHardware(const std::string &devId, const std::string &dhId, const std::string &key,
47         const std::string &value) override;
48     void RegisterDistributedHardwareStateListener(std::shared_ptr<DistributedHardwareStateListener> listener) override;
49     void UnregisterDistributedHardwareStateListener() override;
50     void RegisterDataSyncTriggerListener(std::shared_ptr<DataSyncTriggerListener> listener) override;
51     void UnregisterDataSyncTriggerListener() override;
52     void FinishStartSA(const std::string &params, const sptr<IRemoteObject> &remoteObject);
53     void OnRemoteSourceSvrDied(const wptr<IRemoteObject> &remote);
54 
55 public:
56 
57     class SALoadSourceCb : public OHOS::SystemAbilityLoadCallbackStub {
58     public:
59         void OnLoadSystemAbilitySuccess(int32_t systemAbilityId,
60                     const OHOS::sptr<IRemoteObject> &remoteObject) override;
61 
62         void OnLoadSystemAbilityFail(int32_t systemAbilityId) override;
63 
GetSystemAbilityId()64         int32_t GetSystemAbilityId() const
65         {
66             return currSystemAbilityId;
67         }
68 
GetRemoteObject()69         OHOS::sptr<IRemoteObject> GetRemoteObject() const
70         {
71             return currRemoteObject;
72         }
73     private:
74         int32_t currSystemAbilityId = 0;
75         OHOS::sptr<OHOS::IRemoteObject> currRemoteObject;
76     };
77 private:
78     DistributedInputSourceHandler();
79     ~DistributedInputSourceHandler() = default;
80     class DInputSourceSvrRecipient : public IRemoteObject::DeathRecipient {
81     public:
82         void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
83     };
84     OHOS::sptr<OHOS::ISystemAbilityLoadCallback> sysSourceCallback = nullptr;
85 
86     std::mutex proxyMutex_;
87     std::condition_variable proxyConVar_;
88     sptr<IDistributedSourceInput> dInputSourceProxy_ = nullptr;
89     sptr<LoadDInputSourceCallback> dInputSourceCallback_ = nullptr;
90     sptr<DInputSourceSvrRecipient> sourceSvrRecipient_ = nullptr;
91 };
92 
93 #ifdef __cplusplus
94 extern "C" {
95 #endif // __cplusplus
96 __attribute__((visibility("default"))) IDistributedHardwareSource *GetSourceHardwareHandler();
97 #ifdef __cplusplus
98 }
99 #endif // __cplusplus
100 } // namespace DistributedInput
101 } // namespace DistributedHardware
102 } // namespace OHOS
103 
104 #endif // DISTRIBUTED_INPUT_SOURCE_HANDLER_H
105