1 /*
2  * Copyright (c) 2021-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 OHOS_DISTRIBUTED_HARDWARE_COMPONENT_MANAGER_H
17 #define OHOS_DISTRIBUTED_HARDWARE_COMPONENT_MANAGER_H
18 
19 #include <atomic>
20 #include <map>
21 #include <set>
22 #include <unordered_map>
23 #include <mutex>
24 #include <future>
25 
26 #include "single_instance.h"
27 #include "component_monitor.h"
28 #include "capability_info.h"
29 #include "device_type.h"
30 #include "dh_comm_tool.h"
31 #include "event_handler.h"
32 #include "idistributed_hardware.h"
33 #include "idistributed_hardware_sink.h"
34 #include "idistributed_hardware_source.h"
35 #include "impl_utils.h"
36 #include "low_latency_listener.h"
37 #include "meta_capability_info.h"
38 #include "task_board.h"
39 #include "task_factory.h"
40 #include "version_info.h"
41 #include "component_privacy.h"
42 
43 namespace OHOS {
44 namespace DistributedHardware {
45 using ActionResult = std::unordered_map<DHType, std::shared_future<int32_t>>;
46 class ComponentManager {
47     DECLARE_SINGLE_INSTANCE_BASE(ComponentManager);
48 
49 public:
50     ComponentManager();
51     ~ComponentManager();
52 
53 public:
54     int32_t Init();
55     int32_t UnInit();
56     int32_t Enable(const std::string &networkId, const std::string &uuid, const std::string &dhId,
57         const DHType dhType);
58     int32_t Disable(const std::string &networkId, const std::string &uuid, const std::string &dhId,
59         const DHType dhType);
60     void UpdateBusinessState(const std::string &uuid, const std::string &dhId, BusinessState state);
61     BusinessState QueryBusinessState(const std::string &uuid, const std::string &dhId);
62     void DumpLoadedComps(std::set<DHType> &compSourceType, std::set<DHType> &compSinkType);
63     void Recover(DHType dhType);
64     std::map<DHType, IDistributedHardwareSink*> GetDHSinkInstance();
65     void TriggerFullCapsSync(const std::string &networkId);
66     void SaveNeedRefreshTask(const TaskParam &taskParam);
67     IDistributedHardwareSource* GetDHSourceInstance(DHType dhType);
68     /**
69      * @brief find the task param and return it.
70      *        If the task param exist, get and remove from the cached task params,
71      *        save it at the second task param, then return true.
72      *        If the task param not exist, return false.
73      *
74      * @param taskKey the task param uuid and dhid pair.
75      * @param taskParam if the task param exist, save it.
76      * @return true if the task param exist, return true.
77      * @return false if the task param not exist, return false.
78      */
79     bool FetchNeedRefreshTask(const std::pair<std::string, std::string> &taskKey, TaskParam &taskParam);
80 
81     class ComponentManagerEventHandler : public AppExecFwk::EventHandler {
82     public:
83         ComponentManagerEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> runner);
84         ~ComponentManagerEventHandler() override = default;
85         void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
86     };
87     std::shared_ptr<ComponentManager::ComponentManagerEventHandler> GetEventHandler();
88 
89 private:
90     enum class Action : int32_t {
91         START_SOURCE,
92         START_SINK,
93         STOP_SOURCE,
94         STOP_SINK
95     };
96 
97     DHType GetDHType(const std::string &uuid, const std::string &dhId) const;
98     bool InitCompSource();
99     bool InitCompSink();
100     ActionResult StartSource();
101     ActionResult StartSource(DHType dhType);
102     ActionResult StopSource();
103     ActionResult StartSink();
104     ActionResult StartSink(DHType dhType);
105     ActionResult StopSink();
106     bool WaitForResult(const Action &action, ActionResult result);
107     int32_t GetEnableParam(const std::string &networkId, const std::string &uuid, const std::string &dhId,
108         DHType dhType, EnableParam &param);
109     int32_t GetVersionFromVerMgr(const std::string &uuid, const DHType dhType, std::string &version, bool isSink);
110     int32_t GetVersionFromVerInfoMgr(const std::string &uuid, const DHType dhType, std::string &version, bool isSink);
111     int32_t GetVersion(const std::string &uuid, DHType dhType, std::string &version, bool isSink);
112     void UpdateVersionCache(const std::string &uuid, const VersionInfo &versionInfo);
113 
114     void DoRecover(DHType dhType);
115     void ReStartSA(DHType dhType);
116     void RecoverDistributedHardware(DHType dhType);
117     bool IsIdenticalAccount(const std::string &networkId);
118     int32_t RetryGetEnableParam(const std::string &networkId, const std::string &uuid,
119         const std::string &dhId, const DHType dhType, EnableParam &param);
120     void InitComponentHandler();
121     int32_t InitSAMonitor();
122     void StartComponent();
123     void RegisterDHStateListener();
124     void RegisterDataSyncTriggerListener();
125     void InitDHCommTool();
126 
127     void UnInitSAMonitor();
128     void UnregisterDHStateListener();
129     void UnregisterDataSyncTriggerListener();
130     void UnInitDHCommTool();
131     void StopComponent();
132     void StopPrivacy();
133     int32_t GetEnableCapParam(const std::string &networkId, const std::string &uuid, DHType dhType, EnableParam &param,
134         std::shared_ptr<CapabilityInfo> capability);
135     int32_t GetEnableMetaParam(const std::string &networkId, const std::string &uuid, DHType dhType, EnableParam &param,
136         std::shared_ptr<MetaCapabilityInfo> metaCapPtr);
137     int32_t GetCapParam(const std::string &uuid, const std::string &dhId, std::shared_ptr<CapabilityInfo> &capability);
138     int32_t GetMetaParam(const std::string &uuid, const std::string &dhId,
139         std::shared_ptr<MetaCapabilityInfo> &metaCapPtr);
140 
141 private:
142     std::map<DHType, IDistributedHardwareSource*> compSource_;
143     std::map<DHType, IDistributedHardwareSink*> compSink_;
144     std::map<DHType, int32_t> compSrcSaId_;
145     std::shared_ptr<ComponentPrivacy> audioCompPrivacy_ = nullptr;
146     std::shared_ptr<ComponentPrivacy> cameraCompPrivacy_ = nullptr;
147     std::shared_ptr<ComponentMonitor> compMonitorPtr_ = nullptr;
148     sptr<LowLatencyListener> lowLatencyListener_ = nullptr;
149 
150     std::atomic<bool> isUnInitTimeOut_;
151     // record the remote device business state, {{deviceUUID, dhId}, BusinessState}.
152     std::map<std::pair<std::string, std::string>, BusinessState> dhBizStates_;
153     std::mutex bizStateMtx_;
154     std::shared_ptr<DistributedHardwareStateListener> dhStateListener_;
155     std::shared_ptr<DataSyncTriggerListener> dataSyncTriggerListener_;
156 
157     std::shared_ptr<ComponentManager::ComponentManagerEventHandler> eventHandler_;
158     std::shared_ptr<DHCommTool> dhCommToolPtr_;
159 
160     // save those remote dh that need refresh by full capability, {{device networkId, dhId}, TaskParam}.
161     std::map<std::pair<std::string, std::string>, TaskParam> needRefreshTaskParams_;
162     std::mutex needRefreshTaskParamsMtx_;
163 };
164 } // namespace DistributedHardware
165 } // namespace OHOS
166 #endif
167