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 "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, bool isActive = false); 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 DumpLoadedCompsource(std::set<DHType> &compSourceType); 63 void DumpLoadedCompsink(std::set<DHType> &compSinkType); 64 void Recover(DHType dhType); 65 std::map<DHType, IDistributedHardwareSink*> GetDHSinkInstance(); 66 void TriggerFullCapsSync(const std::string &networkId); 67 void SaveNeedRefreshTask(const TaskParam &taskParam); 68 IDistributedHardwareSource* GetDHSourceInstance(DHType dhType); 69 /** 70 * @brief find the task param and return it. 71 * If the task param exist, get and remove from the cached task params, 72 * save it at the second task param, then return true. 73 * If the task param not exist, return false. 74 * 75 * @param taskKey the task param uuid and dhid pair. 76 * @param taskParam if the task param exist, save it. 77 * @return true if the task param exist, return true. 78 * @return false if the task param not exist, return false. 79 */ 80 bool FetchNeedRefreshTask(const std::pair<std::string, std::string> &taskKey, TaskParam &taskParam); 81 82 int32_t CheckDemandStart(const std::string &uuid, const DHType dhType, bool &enableSink, bool &enableSource); 83 int32_t RegisterDHStatusListener(sptr<IHDSinkStatusListener> listener, int32_t callingUid, int32_t callingPid); 84 int32_t UnregisterDHStatusListener(sptr<IHDSinkStatusListener> listener, int32_t callingUid, int32_t callingPid); 85 int32_t RegisterDHStatusListener(const std::string &networkId, 86 sptr<IHDSourceStatusListener> listener, int32_t callingUid, int32_t callingPid); 87 int32_t UnregisterDHStatusListener(const std::string &networkId, 88 sptr<IHDSourceStatusListener> listener, int32_t callingUid, int32_t callingPid); 89 int32_t EnableSink(const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid); 90 int32_t DisableSink(const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid); 91 int32_t EnableSource(const std::string &networkId, 92 const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid); 93 int32_t DisableSource(const std::string &networkId, 94 const DHDescriptor &dhDescriptor, int32_t callingUid, int32_t callingPid); 95 int32_t ForceDisableSink(const DHDescriptor &dhDescriptor); 96 int32_t ForceDisableSource(const std::string &networkId, const DHDescriptor &dhDescriptor); 97 int32_t CheckIdenticalAccount(const std::string &networkId, 98 const std::string &uuid, const DHDescriptor &dhDescriptor); 99 100 class ComponentManagerEventHandler : public AppExecFwk::EventHandler { 101 public: 102 ComponentManagerEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> runner); 103 ~ComponentManagerEventHandler() override = default; 104 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; 105 }; 106 std::shared_ptr<ComponentManager::ComponentManagerEventHandler> GetEventHandler(); 107 108 private: 109 enum class Action : int32_t { 110 START_SOURCE, 111 START_SINK, 112 STOP_SOURCE, 113 STOP_SINK 114 }; 115 116 enum class EnableState : int32_t { 117 DISABLED, 118 ENABLED 119 }; 120 121 struct DHStatusCtrlKey { 122 int32_t uid; 123 int32_t pid; 124 125 bool operator == (const DHStatusCtrlKey &other) const 126 { 127 return (uid == other.uid) && (pid == other.pid); 128 } 129 130 bool operator < (const DHStatusCtrlKey &other) const 131 { 132 if (uid < other.uid) { 133 return true; 134 } else if (uid > other.uid) { 135 return false; 136 } 137 return pid < other.pid; 138 } 139 }; 140 141 struct DHStatusCtrl { 142 EnableState enableState; DHStatusCtrlDHStatusCtrl143 DHStatusCtrl() 144 { 145 enableState = EnableState::DISABLED; 146 } 147 }; 148 149 struct DHStatusEnableInfo { 150 int32_t refEnable; 151 std::map<DHStatusCtrlKey, DHStatusCtrl> dhStatusCtrl; DHStatusEnableInfoDHStatusEnableInfo152 DHStatusEnableInfo() 153 { 154 refEnable = 0; 155 } 156 }; 157 158 struct DHStatusSourceEnableInfoKey { 159 std::string networkId; 160 std::string dhId; 161 bool operator == (const DHStatusSourceEnableInfoKey &other) const 162 { 163 return (networkId == other.networkId) && (dhId == other.dhId); 164 } 165 bool operator < (const DHStatusSourceEnableInfoKey &other) const 166 { 167 if (networkId < other.networkId) { 168 return true; 169 } else if (networkId > other.networkId) { 170 return false; 171 } 172 return dhId < other.dhId; 173 } 174 }; 175 176 struct DHSinkStatus { 177 int32_t refLoad; 178 std::map<std::string, DHStatusEnableInfo> enableInfos; // key is dhid 179 std::map<DHStatusCtrlKey, sptr<IHDSinkStatusListener>> listeners; DHSinkStatusDHSinkStatus180 DHSinkStatus() 181 { 182 refLoad = 0; 183 } 184 }; 185 186 struct DHSourceStatus { 187 int32_t refLoad; 188 std::map<DHStatusSourceEnableInfoKey, DHStatusEnableInfo> enableInfos; 189 std::map<DHStatusCtrlKey, sptr<IHDSourceStatusListener>> listeners; DHSourceStatusDHSourceStatus190 DHSourceStatus() 191 { 192 refLoad = 0; 193 } 194 }; 195 196 DHType GetDHType(const std::string &uuid, const std::string &dhId) const; 197 int32_t InitCompSource(DHType dhType); 198 int32_t UninitCompSource(DHType dhType); 199 int32_t InitCompSink(DHType dhType); 200 int32_t UninitCompSink(DHType dhType); 201 ActionResult StartSource(DHType dhType); 202 ActionResult StopSource(DHType dhType); 203 ActionResult StartSink(DHType dhType); 204 ActionResult StopSink(DHType dhType); 205 bool WaitForResult(const Action &action, ActionResult result); 206 int32_t GetEnableParam(const std::string &networkId, const std::string &uuid, const std::string &dhId, 207 DHType dhType, EnableParam ¶m); 208 int32_t GetVersionFromVerMgr(const std::string &uuid, const DHType dhType, std::string &version, bool isSink); 209 int32_t GetVersionFromVerInfoMgr(const std::string &uuid, const DHType dhType, std::string &version, bool isSink); 210 int32_t GetVersion(const std::string &uuid, DHType dhType, std::string &version, bool isSink); 211 void UpdateVersionCache(const std::string &uuid, const VersionInfo &versionInfo); 212 213 void DoRecover(DHType dhType); 214 void ReStartSA(DHType dhType); 215 void RecoverDistributedHardware(DHType dhType); 216 bool IsIdenticalAccount(const std::string &networkId); 217 int32_t RetryGetEnableParam(const std::string &networkId, const std::string &uuid, 218 const std::string &dhId, const DHType dhType, EnableParam ¶m); 219 void StopPrivacy(); 220 int32_t GetEnableCapParam(const std::string &networkId, const std::string &uuid, DHType dhType, EnableParam ¶m, 221 std::shared_ptr<CapabilityInfo> capability); 222 int32_t GetEnableMetaParam(const std::string &networkId, const std::string &uuid, DHType dhType, EnableParam ¶m, 223 std::shared_ptr<MetaCapabilityInfo> metaCapPtr); 224 int32_t GetCapParam(const std::string &uuid, const std::string &dhId, std::shared_ptr<CapabilityInfo> &capability); 225 int32_t GetMetaParam(const std::string &uuid, const std::string &dhId, 226 std::shared_ptr<MetaCapabilityInfo> &metaCapPtr); 227 int32_t CheckSubtypeResource(const std::string &subtype, const std::string &networkId); 228 229 int32_t GetRemoteVerInfo(CompVersion &compVersion, const std::string &uuid, DHType dhType); 230 bool IsFeatureMatched(const std::vector<std::string> &sourceFeatureFilters, 231 const std::vector<std::string> &sinkSupportedFeatures); 232 int32_t EnableSinkInternal(const DHDescriptor &dhDescriptor, 233 int32_t callingUid, int32_t callingPid, sptr<IHDSinkStatusListener> &listener); 234 int32_t DisableSinkInternal(const DHDescriptor &dhDescriptor, 235 int32_t callingUid, int32_t callingPid, sptr<IHDSinkStatusListener> &listener); 236 int32_t EnableSourceInternal(const std::string &networkId, const DHDescriptor &dhDescriptor, 237 int32_t callingUid, int32_t callingPid, sptr<IHDSourceStatusListener> &listener); 238 int32_t DisableSourceInternal(const std::string &networkId, const DHDescriptor &dhDescriptor, 239 int32_t callingUid, int32_t callingPid, sptr<IHDSourceStatusListener> &listener); 240 int32_t ForceDisableSinkInternal( 241 const DHDescriptor &dhDescriptor, std::vector<sptr<IHDSinkStatusListener>> &listeners); 242 int32_t ForceDisableSourceInternal(const std::string &networkId, 243 const DHDescriptor &dhDescriptor, std::vector<sptr<IHDSourceStatusListener>> &listeners); 244 int32_t RealEnableSource(const std::string &networkId, const std::string &uuid, const DHDescriptor &dhDescriptor, 245 DHStatusCtrl &statusCtrl, DHStatusEnableInfo &enableInfo, DHSourceStatus &status, bool isActive); 246 int32_t RealDisableSource(const std::string &networkId, const std::string &uuid, const DHDescriptor &dhDescriptor, 247 DHStatusCtrl &statusCtrl, DHStatusEnableInfo &enableInfo, DHSourceStatus &status); 248 249 private: 250 std::map<DHType, IDistributedHardwareSource*> compSource_; 251 std::shared_mutex compSourceMutex_; 252 std::map<DHType, IDistributedHardwareSink*> compSink_; 253 std::shared_mutex compSinkMutex_; 254 std::map<DHType, int32_t> compSrcSaId_; 255 std::shared_ptr<ComponentPrivacy> audioCompPrivacy_ = nullptr; 256 std::shared_ptr<ComponentPrivacy> cameraCompPrivacy_ = nullptr; 257 std::shared_ptr<ComponentMonitor> compMonitorPtr_ = nullptr; 258 sptr<LowLatencyListener> lowLatencyListener_ = nullptr; 259 260 std::atomic<bool> isUnInitTimeOut_; 261 // record the remote device business state, {{deviceUUID, dhId}, BusinessState}. 262 std::map<std::pair<std::string, std::string>, BusinessState> dhBizStates_; 263 std::mutex bizStateMtx_; 264 std::shared_ptr<DistributedHardwareStateListener> dhStateListener_; 265 std::shared_ptr<DataSyncTriggerListener> dataSyncTriggerListener_; 266 267 std::shared_ptr<ComponentManager::ComponentManagerEventHandler> eventHandler_; 268 std::shared_ptr<DHCommTool> dhCommToolPtr_; 269 270 // save those remote dh that need refresh by full capability, {{device networkId, dhId}, TaskParam}. 271 std::map<std::pair<std::string, std::string>, TaskParam> needRefreshTaskParams_; 272 std::mutex needRefreshTaskParamsMtx_; 273 274 // distributed hardware enable status maintenance. 275 std::map<DHType, DHSinkStatus> dhSinkStatus_; 276 std::mutex dhSinkStatusMtx_; 277 std::map<DHType, DHSourceStatus> dhSourceStatus_; 278 std::mutex dhSourceStatusMtx_; 279 }; 280 } // namespace DistributedHardware 281 } // namespace OHOS 282 #endif 283