1 /*
2  * Copyright (c) 2022-2024 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 DINPUT_SA_MANAGER_H
17 #define DINPUT_SA_MANAGER_H
18 
19 #include <atomic>
20 #include <mutex>
21 #include <set>
22 
23 #include "i_distributed_source_input.h"
24 #include "i_distributed_sink_input.h"
25 #include "single_instance.h"
26 #include "system_ability_status_change_stub.h"
27 
28 #include "idistributed_hardware_source.h"
29 #include "idistributed_hardware_sink.h"
30 #include "event_handler.h"
31 
32 namespace OHOS {
33 namespace DistributedHardware {
34 namespace DistributedInput {
35 const uint32_t DINPUT_CLIENT_CHECK_SOURCE_CALLBACK_REGISTER_MSG = 1;
36 const uint32_t DINPUT_CLIENT_CHECK_SINK_CALLBACK_REGISTER_MSG = 2;
37 const uint32_t DINPUT_CLIENT_CLEAR_SOURCE_CALLBACK_REGISTER_MSG = 3;
38 const uint32_t DINPUT_CLIENT_CLEAR_SINK_CALLBACK_REGISTER_MSG = 4;
39 class DInputSAManager {
40 DECLARE_SINGLE_INSTANCE_BASE(DInputSAManager);
41 public:
42     void Init();
43     bool GetDInputSourceProxy();
44     bool HasDInputSourceProxy();
45     bool SetDInputSourceProxy(const sptr<IRemoteObject> &remoteObject);
46     bool GetDInputSinkProxy();
47     bool HasDInputSinkProxy();
48     bool SetDInputSinkProxy(const sptr<IRemoteObject> &remoteObject);
49     void RegisterEventHandler(std::shared_ptr<AppExecFwk::EventHandler> handler);
50     int32_t RestoreRegisterListenerAndCallback();
51     void AddSimEventListenerToCache(sptr<ISimulationEventListener> listener);
52     void RemoveSimEventListenerFromCache(sptr<ISimulationEventListener> listener);
53     void AddSessionStateCbToCache(sptr<ISessionStateCallback> callback);
54     void RemoveSessionStateCbFromCache();
55 
56 public:
57     class SystemAbilityListener : public SystemAbilityStatusChangeStub {
58     public:
59         void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
60         void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
61     };
62 
63 private:
64     std::mutex simEventListenerCacheMtx_;
65     std::set<sptr<ISimulationEventListener>> simEventListenerCache_;
66     std::mutex sessionStateCbCacheMtx_;
67     std::set<sptr<ISessionStateCallback>> sessionStateCbCache_;
68 
69 private:
70     DInputSAManager() = default;
71     ~DInputSAManager() = default;
72 
73 public:
74     std::atomic<bool> dInputSourceSAOnline_ = false;
75     std::atomic<bool> dInputSinkSAOnline_ = false;
76     std::atomic<bool> isSubscribeSrcSAChangeListener_ = false;
77     std::atomic<bool> isSubscribeSinkSAChangeListener_ = false;
78     std::mutex sinkMutex_;
79     std::mutex sourceMutex_;
80     std::mutex handlerMutex_;
81 
82     sptr<IDistributedSourceInput> dInputSourceProxy_ = nullptr;
83     sptr<IDistributedSinkInput> dInputSinkProxy_ = nullptr;
84     sptr<SystemAbilityListener> saListenerCallback = nullptr;
85 
86     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_;
87 };
88 } // namespace DistributedInput
89 } // namespace DistributedHardware
90 } // namespace OHOS
91 
92 #endif // DINPUT_SA_MANAGER_H