1 /* 2 * Copyright (c) 2021 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 <map> 20 #include <unordered_map> 21 #include <mutex> 22 #include <future> 23 24 #include "single_instance.h" 25 #include "device_type.h" 26 #include "idistributed_hardware.h" 27 #include "idistributed_hardware_sink.h" 28 #include "idistributed_hardware_source.h" 29 30 namespace OHOS { 31 namespace DistributedHardware { 32 class ComponentManager { 33 DECLARE_SINGLE_INSTANCE_BASE(ComponentManager); 34 35 public: ComponentManager()36 ComponentManager() {} 37 ~ComponentManager(); 38 39 public: 40 int32_t Init(); 41 int32_t UnInit(); 42 int32_t Enable(const std::string &networkId, const std::string &uuid, const std::string &dhId); 43 int32_t Disable(const std::string &networkId, const std::string &uuid, const std::string &dhId); 44 45 private: 46 enum class Action : int32_t { 47 START_SOURCE, 48 START_SINK, 49 STOP_SOURCE, 50 STOP_SINK 51 }; 52 53 using ActionResult = std::unordered_map<DHType, std::shared_future<int32_t>>; 54 55 DHType GetDHType(const std::string &uuid, const std::string &dhId) const; 56 bool InitCompSource(); 57 bool InitCompSink(); 58 ActionResult StartSource(); 59 ActionResult StopSource(); 60 ActionResult StartSink(); 61 ActionResult StopSink(); 62 bool WaitForResult(const Action &action, ActionResult result); 63 int32_t GetEnableParam(const std::string &networkId, const std::string &uuid, const std::string &dhId, 64 DHType dhType, EnableParam ¶m); 65 std::string GetSinkVersion(const std::string &networkId, const std::string &uuid, DHType dhType); 66 std::string GetVersionFromCache(const std::string &uuid, DHType dhType); 67 int32_t UpdateVersionCache(const std::string &networkId, const std::string &uuid); 68 sptr<IDistributedHardware> GetRemoteDHMS(const std::string &networkId) const; 69 70 private: 71 std::map<DHType, IDistributedHardwareSource*> compSource_; 72 std::map<DHType, IDistributedHardwareSink*> compSink_; 73 std::unordered_map<std::string, std::unordered_map<DHType, std::string>> sinkVersions_; 74 std::mutex sinkVersionMutex_; 75 }; 76 } 77 } 78 #endif 79