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_SINK_HANDLER_H
17#define DISTRIBUTED_INPUT_SINK_HANDLER_H
18
19#include <condition_variable>
20#include <mutex>
21#include <string>
22
23#include "idistributed_hardware_sink.h"
24#include "iservice_registry.h"
25#include "single_instance.h"
26#include "system_ability_definition.h"
27#include "system_ability_load_callback_stub.h"
28
29#include "distributed_input_client.h"
30#include "i_distributed_sink_input.h"
31
32namespace OHOS {
33namespace DistributedHardware {
34namespace DistributedInput {
35class DistributedInputSinkHandler : public IDistributedHardwareSink {
36DECLARE_SINGLE_INSTANCE_BASE(DistributedInputSinkHandler);
37public:
38    int32_t InitSink(const std::string &params) override;
39    int32_t ReleaseSink() override;
40    int32_t SubscribeLocalHardware(const std::string &dhId, const std::string &params) override;
41    int32_t UnsubscribeLocalHardware(const std::string &dhId) override;
42    void OnRemoteSinkSvrDied(const wptr<IRemoteObject> &remote);
43    void FinishStartSA(const std::string &params, const sptr<IRemoteObject> &remoteObject);
44    int32_t RegisterPrivacyResources(std::shared_ptr<PrivacyResourcesListener> listener) override;
45    int32_t PauseDistributedHardware(const std::string &networkId) override;
46    int32_t ResumeDistributedHardware(const std::string &networkId) override;
47    int32_t StopDistributedHardware(const std::string &networkId) override;
48
49public:
50    class SALoadSinkCb : public OHOS::SystemAbilityLoadCallbackStub {
51    public:
52        void OnLoadSystemAbilitySuccess(int32_t systemAbilityId,
53            const OHOS::sptr<IRemoteObject> &remoteObject) override;
54
55        void OnLoadSystemAbilityFail(int32_t systemAbilityId) override;
56
57        int32_t GetSystemAbilityId() const
58        {
59            return currSystemAbilityId;
60        }
61
62        OHOS::sptr<IRemoteObject> GetRemoteObject() const
63        {
64            return currRemoteObject;
65        }
66    private:
67        int32_t currSystemAbilityId = 0;
68        OHOS::sptr<OHOS::IRemoteObject> currRemoteObject;
69    };
70
71private:
72    class DInputSinkSvrRecipient : public IRemoteObject::DeathRecipient {
73    public:
74        void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
75    };
76    DistributedInputSinkHandler();
77    ~DistributedInputSinkHandler();
78    OHOS::sptr<OHOS::ISystemAbilityLoadCallback> sysSinkCallback = nullptr;
79
80    sptr<IDistributedSinkInput> dInputSinkProxy_ = nullptr;
81    sptr<DInputSinkSvrRecipient> sinkSvrRecipient_ = nullptr;
82    std::mutex proxyMutex_;
83    std::condition_variable proxyConVar_;
84};
85
86#ifdef __cplusplus
87extern "C" {
88#endif
89__attribute__((visibility("default"))) IDistributedHardwareSink *GetSinkHardwareHandler();
90#ifdef __cplusplus
91}
92#endif
93} // namespace DistributedInput
94} // namespace DistributedHardware
95} // namespace OHOS
96
97#endif // DISTRIBUTED_INPUT_SINK_HANDLER_H
98