• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 "dh_modem_context_ext.h"
32 #include "event_handler.h"
33 #include "idistributed_hardware.h"
34 #include "idistributed_hardware_sink.h"
35 #include "idistributed_hardware_source.h"
36 #include "impl_utils.h"
37 #include "low_latency_listener.h"
38 #include "meta_capability_info.h"
39 #include "task_board.h"
40 #include "task_factory.h"
41 #include "version_info.h"
42 #include "component_privacy.h"
43 
44 namespace OHOS {
45 namespace DistributedHardware {
46 using ActionResult = std::unordered_map<DHType, std::shared_future<int32_t>>;
47 class ComponentManager {
48     DECLARE_SINGLE_INSTANCE_BASE(ComponentManager);
49 
50 public:
51     ComponentManager();
52     ~ComponentManager();
53 
54 public:
55     int32_t Init();
56     int32_t UnInit();
57     int32_t Enable(const std::string &networkId, const std::string &uuid, const std::string &dhId,
58         const DHType dhType, bool isActive = false);
59     int32_t Disable(const std::string &networkId, const std::string &uuid, const std::string &dhId,
60         const DHType dhType);
61     void UpdateBusinessState(const std::string &uuid, const std::string &dhId, BusinessState state);
62     BusinessState QueryBusinessState(const std::string &uuid, const std::string &dhId);
63     void DumpLoadedCompsource(std::set<DHType> &compSourceType);
64     void DumpLoadedCompsink(std::set<DHType> &compSinkType);
65     void Recover(DHType dhType);
66     std::map<DHType, IDistributedHardwareSink*> GetDHSinkInstance();
67     void TriggerFullCapsSync(const std::string &networkId);
68     void SaveNeedRefreshTask(const TaskParam &taskParam);
69     IDistributedHardwareSource* GetDHSourceInstance(DHType dhType);
70     /**
71      * @brief find the task param and return it.
72      *        If the task param exist, get and remove from the cached task params,
73      *        save it at the second task param, then return true.
74      *        If the task param not exist, return false.
75      *
76      * @param taskKey the task param uuid and dhid pair.
77      * @param taskParam if the task param exist, save it.
78      * @return true if the task param exist, return true.
79      * @return false if the task param not exist, return false.
80      */
81     bool FetchNeedRefreshTask(const std::pair<std::string, std::string> &taskKey, TaskParam &taskParam);
82 
83     int32_t CheckSinkConfigStart(const DHType dhType, bool &enableSink);
84     int32_t CheckDemandStart(const std::string &uuid, const DHType dhType, bool &enableSource);
85     int32_t RegisterDHStatusListener(sptr<IHDSinkStatusListener> listener, int32_t callingUid, int32_t callingPid);
86     int32_t UnregisterDHStatusListener(sptr<IHDSinkStatusListener> listener, int32_t callingUid, int32_t callingPid);
87     int32_t RegisterDHStatusListener(const std::string &networkId,
88         sptr<IHDSourceStatusListener> listener, int32_t callingUid, int32_t callingPid);
89     int32_t UnregisterDHStatusListener(const std::string &networkId,
90         sptr<IHDSourceStatusListener> listener, int32_t callingUid, int32_t callingPid);
91     int32_t EnableSink(const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid);
92     int32_t DisableSink(const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid);
93     int32_t EnableSource(const std::string &networkId,
94         const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid);
95     int32_t DisableSource(const std::string &networkId,
96         const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid);
97     int32_t ForceDisableSink(const DHDescriptor &dhDescriptor);
98     int32_t ForceDisableSource(const std::string &networkId, const DHDescriptor &dhDescriptor);
99     int32_t CheckIdenticalAccount(const std::string &networkId,
100         const std::string &uuid, const DHDescriptor &dhDescriptor);
101     int32_t EnableMetaSource(const std::string &networkId, const DHDescriptor &dhDescriptor,
102         std::shared_ptr<IDistributedModemExt> dhModemExt, IDistributedHardwareSource *&sourcePtr);
103     int32_t DisableMetaSource(const std::string &networkId, const DHDescriptor &dhDescriptor,
104         std::shared_ptr<IDistributedModemExt> dhModemExt, IDistributedHardwareSource *&sourcePtr);
105     class ComponentManagerEventHandler : public AppExecFwk::EventHandler {
106     public:
107         ComponentManagerEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> runner);
108         ~ComponentManagerEventHandler() override = default;
109         void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
110     };
111     std::shared_ptr<ComponentManager::ComponentManagerEventHandler> GetEventHandler();
112 
113 private:
114     enum class Action : int32_t {
115         START_SOURCE,
116         START_SINK,
117         STOP_SOURCE,
118         STOP_SINK
119     };
120 
121     enum class EnableState : int32_t {
122         DISABLED,
123         ENABLED
124     };
125 
126     struct DHStatusCtrlKey {
127         int32_t uid;
128         int32_t pid;
129 
130         bool operator == (const DHStatusCtrlKey &other) const
131         {
132             return (uid == other.uid) && (pid == other.pid);
133         }
134 
135         bool operator < (const DHStatusCtrlKey &other) const
136         {
137             if (uid < other.uid) {
138                 return true;
139             } else if (uid > other.uid) {
140                 return false;
141             }
142             return pid < other.pid;
143         }
144     };
145 
146     struct DHStatusCtrl {
147         EnableState enableState;
DHStatusCtrlDHStatusCtrl148         DHStatusCtrl()
149         {
150             enableState = EnableState::DISABLED;
151         }
152     };
153 
154     struct DHStatusEnableInfo {
155         int32_t refEnable;
156         std::map<DHStatusCtrlKey, DHStatusCtrl> dhStatusCtrl;
DHStatusEnableInfoDHStatusEnableInfo157         DHStatusEnableInfo()
158         {
159             refEnable = 0;
160         }
161     };
162 
163     struct DHStatusSourceEnableInfoKey {
164         std::string networkId;
165         std::string dhId;
166         bool operator == (const DHStatusSourceEnableInfoKey &other) const
167         {
168             return (networkId == other.networkId) && (dhId == other.dhId);
169         }
170         bool operator < (const DHStatusSourceEnableInfoKey &other) const
171         {
172             if (networkId < other.networkId) {
173                 return true;
174             } else if (networkId > other.networkId) {
175                 return false;
176             }
177             return dhId < other.dhId;
178         }
179     };
180 
181     struct DHSinkStatus {
182         int32_t refLoad;
183         std::map<std::string, DHStatusEnableInfo> enableInfos;  // key is dhid
184         std::map<DHStatusCtrlKey, sptr<IHDSinkStatusListener>> listeners;
DHSinkStatusDHSinkStatus185         DHSinkStatus()
186         {
187             refLoad = 0;
188         }
189     };
190 
191     struct DHSourceStatus {
192         int32_t refLoad;
193         std::map<DHStatusSourceEnableInfoKey, DHStatusEnableInfo> enableInfos;
194         std::map<DHStatusCtrlKey, sptr<IHDSourceStatusListener>> listeners;
DHSourceStatusDHSourceStatus195         DHSourceStatus()
196         {
197             refLoad = 0;
198         }
199     };
200 
201     DHType GetDHType(const std::string &uuid, const std::string &dhId) const;
202     int32_t InitCompSource(DHType dhType);
203     int32_t UninitCompSource(DHType dhType);
204     int32_t InitCompSink(DHType dhType);
205     int32_t UninitCompSink(DHType dhType);
206     void InitDHCommTool();
207     void UnInitDHCommTool();
208     int32_t StartSource(DHType dhType, ActionResult &sourceResult);
209     int32_t StopSource(DHType dhType, ActionResult &sourceResult);
210     int32_t StartSink(DHType dhType, ActionResult &sinkResult);
211     int32_t StopSink(DHType dhType, ActionResult &sinkResult);
212     bool WaitForResult(const Action &action, ActionResult result);
213     int32_t GetEnableParam(const std::string &networkId, const std::string &uuid, const std::string &dhId,
214         DHType dhType, EnableParam &param);
215     int32_t GetVersionFromVerMgr(const std::string &uuid, const DHType dhType, std::string &version, bool isSink);
216     int32_t GetVersionFromVerInfoMgr(const std::string &uuid, const DHType dhType, std::string &version, bool isSink);
217     int32_t GetVersion(const std::string &uuid, DHType dhType, std::string &version, bool isSink);
218     void UpdateVersionCache(const std::string &uuid, const VersionInfo &versionInfo);
219 
220     void DoRecover(DHType dhType);
221     bool IsIdenticalAccount(const std::string &networkId);
222     int32_t RetryGetEnableParam(const std::string &networkId, const std::string &uuid,
223         const std::string &dhId, const DHType dhType, EnableParam &param);
224     void StopPrivacy();
225     int32_t GetEnableCapParam(const std::string &networkId, const std::string &uuid, DHType dhType, EnableParam &param,
226         std::shared_ptr<CapabilityInfo> capability);
227     int32_t GetEnableMetaParam(const std::string &networkId, const std::string &uuid, DHType dhType, EnableParam &param,
228         std::shared_ptr<MetaCapabilityInfo> metaCapPtr);
229     int32_t GetCapParam(const std::string &uuid, const std::string &dhId, std::shared_ptr<CapabilityInfo> &capability);
230     int32_t GetMetaParam(const std::string &uuid, const std::string &dhId,
231         std::shared_ptr<MetaCapabilityInfo> &metaCapPtr);
232     int32_t CheckSubtypeResource(const std::string &subtype, const std::string &networkId);
233 
234     int32_t GetRemoteVerInfo(CompVersion &compVersion, const std::string &uuid, DHType dhType);
235     bool IsFeatureMatched(const std::vector<std::string> &sourceFeatureFilters,
236         const std::vector<std::string> &sinkSupportedFeatures);
237     int32_t EnableSinkInternal(const DHDescriptor &dhDescriptor,
238         int32_t callingUid, int32_t callingPid, sptr<IHDSinkStatusListener> &listener);
239     int32_t DisableSinkInternal(const DHDescriptor &dhDescriptor,
240         int32_t callingUid, int32_t callingPid, sptr<IHDSinkStatusListener> &listener);
241     int32_t EnableSourceInternal(const std::string &networkId, const DHDescriptor &dhDescriptor,
242         int32_t callingUid, int32_t callingPid, sptr<IHDSourceStatusListener> &listener);
243     int32_t DisableSourceInternal(const std::string &networkId, const DHDescriptor &dhDescriptor,
244         int32_t callingUid, int32_t callingPid, sptr<IHDSourceStatusListener> &listener);
245     int32_t ForceDisableSinkInternal(
246         const DHDescriptor &dhDescriptor, std::vector<sptr<IHDSinkStatusListener>> &listeners);
247     int32_t ForceDisableSourceInternal(const std::string &networkId,
248         const DHDescriptor &dhDescriptor, std::vector<sptr<IHDSourceStatusListener>> &listeners);
249     int32_t RealEnableSource(const std::string &networkId, const std::string &uuid, const DHDescriptor &dhDescriptor,
250         DHStatusCtrl &statusCtrl, DHStatusEnableInfo &enableInfo, DHSourceStatus &status, bool isActive);
251     int32_t RealDisableSource(const std::string &networkId, const std::string &uuid, const DHDescriptor &dhDescriptor,
252         DHStatusCtrl &statusCtrl, DHStatusEnableInfo &enableInfo, DHSourceStatus &status);
253     int32_t EnableMetaSourceInternal(const std::string &networkId, const DHDescriptor &dhDescriptor,
254         DHStatusCtrl &statusCtrl, DHStatusEnableInfo &enableInfo, DHSourceStatus &status,
255         std::shared_ptr<IDistributedModemExt> dhModemExt, IDistributedHardwareSource *&sourcePtr);
256     int32_t DisableMetaSourceInternal(const std::string &networkId, const DHDescriptor &dhDescriptor,
257         DHStatusCtrl &statusCtrl, DHStatusEnableInfo &enableInfo, DHSourceStatus &status,
258         std::shared_ptr<IDistributedModemExt> dhModemExt, IDistributedHardwareSource *&sourcePtr);
259     void ResetSinkEnableStatus(DHType dhType);
260     void ResetSourceEnableStatus(DHType dhType);
261     void RecoverAutoEnableSink(DHType dhType);
262     void RecoverAutoEnableSource(DHType dhType);
263     void RecoverActiveEnableSink(DHType dhType);
264     void RecoverActiveEnableSource(DHType dhType);
265 private:
266     std::map<DHType, IDistributedHardwareSource*> compSource_;
267     std::shared_mutex compSourceMutex_;
268     std::map<DHType, IDistributedHardwareSink*> compSink_;
269     std::shared_mutex compSinkMutex_;
270     std::map<DHType, int32_t> compSrcSaId_;
271     std::shared_ptr<ComponentPrivacy> audioCompPrivacy_ = nullptr;
272     std::shared_ptr<ComponentPrivacy> cameraCompPrivacy_ = nullptr;
273     std::shared_ptr<ComponentMonitor> compMonitorPtr_ = nullptr;
274     sptr<LowLatencyListener> lowLatencyListener_ = nullptr;
275 
276     std::atomic<bool> isUnInitTimeOut_;
277     // record the remote device business state, {{deviceUUID, dhId}, BusinessState}.
278     std::map<std::pair<std::string, std::string>, BusinessState> dhBizStates_;
279     std::mutex bizStateMtx_;
280     std::shared_ptr<DistributedHardwareStateListener> dhStateListener_;
281     std::shared_ptr<DataSyncTriggerListener> dataSyncTriggerListener_;
282 
283     std::shared_ptr<ComponentManager::ComponentManagerEventHandler> eventHandler_;
284     std::shared_ptr<DHCommTool> dhCommToolPtr_;
285 
286     // save those remote dh that need refresh by full capability, {{device networkId, dhId}, TaskParam}.
287     std::map<std::pair<std::string, std::string>, TaskParam> needRefreshTaskParams_;
288     std::mutex needRefreshTaskParamsMtx_;
289 
290     // distributed hardware enable status maintenance.
291     std::map<DHType, DHSinkStatus> dhSinkStatus_;
292     std::mutex dhSinkStatusMtx_;
293     std::map<DHType, DHSourceStatus> dhSourceStatus_;
294     std::mutex dhSourceStatusMtx_;
295 };
296 } // namespace DistributedHardware
297 } // namespace OHOS
298 #endif
299