1 /*
2  * Copyright (c) 2022 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_DHFWK_SA_MANAGER_H
17 #define OHOS_DHFWK_SA_MANAGER_H
18 
19 #include <atomic>
20 #include <functional>
21 #include <mutex>
22 #include <set>
23 
24 #include "refbase.h"
25 #include "system_ability_status_change_stub.h"
26 
27 #include "single_instance.h"
28 #include "idistributed_hardware.h"
29 
30 namespace OHOS {
31 namespace DistributedHardware {
32 // DHFWK sa state callback, param true for started, false for stopped.
33 using DHFWKSAStateCb = std::function<void(bool)>;
34 class DHFWKSAManager {
35 DECLARE_SINGLE_INSTANCE_BASE(DHFWKSAManager);
36 public:
37     DHFWKSAManager();
38     virtual ~DHFWKSAManager();
39     void RegisterAbilityListener();
40     sptr<IDistributedHardware> GetDHFWKProxy();
41     void RegisterSAStateCallback(DHFWKSAStateCb callback);
42 
43     int32_t AddPublisherListenerToCache(const DHTopic topic, sptr<IPublisherListener> listener);
44     void RemovePublisherListenerFromCache(const DHTopic topic, sptr<IPublisherListener> listener);
45     void AddAVTransControlCenterCbToCache(int32_t engineId, const sptr<IAVTransControlCenterCallback> callback);
46     void RemoveAVTransControlCenterCbFromCache(int32_t engineId);
47 
48 public:
49 class SystemAbilityListener : public SystemAbilityStatusChangeStub {
50 public:
51     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
52     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
53 };
54 
55 private:
56     int32_t RestoreListener();
57 
58 private:
59     std::atomic<bool> dhfwkOnLine_;
60     std::atomic<bool> isSubscribeDHFWKSAChangeListener_;
61     std::mutex proxyMutex_;
62     sptr<IDistributedHardware> dhfwkProxy_;
63     sptr<SystemAbilityListener> saListener_;
64     std::mutex saStatCbMutex_;
65     DHFWKSAStateCb saStateCallback_;
66     std::mutex publisherListenersMutex_;
67     std::unordered_map<DHTopic, std::set<sptr<IPublisherListener>>> publisherListenersCache_;
68     std::mutex avTransControlCenterCbMutex_;
69     std::unordered_map<int32_t, sptr<IAVTransControlCenterCallback>> avTransControlCenterCbCache_;
70 };
71 } // DistributedHardware
72 } // OHOS
73 #endif // OHOS_DHFWK_SA_MANAGER_H