• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "device_type.h"
29 #include "idistributed_hardware.h"
30 #include "idistributed_hardware_sink.h"
31 #include "idistributed_hardware_source.h"
32 #include "low_latency_listener.h"
33 #include "monitor_task_timer.h"
34 #include "version_info.h"
35 #include "component_privacy.h"
36 
37 namespace OHOS {
38 namespace DistributedHardware {
39 using ActionResult = std::unordered_map<DHType, std::shared_future<int32_t>>;
40 class ComponentManager {
41     DECLARE_SINGLE_INSTANCE_BASE(ComponentManager);
42 
43 public:
44     ComponentManager();
45     ~ComponentManager();
46 
47 public:
48     int32_t Init();
49     int32_t UnInit();
50     int32_t Enable(const std::string &networkId, const std::string &uuid, const std::string &dhId,
51         const DHType dhType);
52     int32_t Disable(const std::string &networkId, const std::string &uuid, const std::string &dhId,
53         const DHType dhType);
54 
55     void DumpLoadedComps(std::set<DHType> &compSourceType, std::set<DHType> &compSinkType);
56     void Recover(DHType dhType);
57     std::map<DHType, IDistributedHardwareSink*> GetDHSinkInstance();
58 
59 private:
60     enum class Action : int32_t {
61         START_SOURCE,
62         START_SINK,
63         STOP_SOURCE,
64         STOP_SINK
65     };
66 
67     DHType GetDHType(const std::string &uuid, const std::string &dhId) const;
68     bool InitCompSource();
69     bool InitCompSink();
70     ActionResult StartSource();
71     ActionResult StartSource(DHType dhType);
72     ActionResult StopSource();
73     ActionResult StartSink();
74     ActionResult StartSink(DHType dhType);
75     ActionResult StopSink();
76     bool WaitForResult(const Action &action, ActionResult result);
77     int32_t GetEnableParam(const std::string &networkId, const std::string &uuid, const std::string &dhId,
78         DHType dhType, EnableParam &param);
79     int32_t GetVersionFromVerMgr(const std::string &uuid, const DHType dhType, std::string &version, bool isSink);
80     int32_t GetVersionFromVerInfoMgr(const std::string &uuid, const DHType dhType, std::string &version, bool isSink);
81     int32_t GetVersion(const std::string &networkId, const std::string &uuid,
82         DHType dhType, std::string &version, bool isSink);
83     void UpdateVersionCache(const std::string &uuid, const VersionInfo &versionInfo);
84 
85     void DoRecover(DHType dhType);
86     void ReStartSA(DHType dhType);
87     void RecoverDistributedHardware(DHType dhType);
88     bool IsIdenticalAccount(const std::string &networkId);
89     int32_t RetryGetEnableParam(const std::string &networkId, const std::string &uuid,
90         const std::string &dhId, const DHType dhType, EnableParam &param);
91 
92 private:
93     std::map<DHType, IDistributedHardwareSource*> compSource_;
94     std::map<DHType, IDistributedHardwareSink*> compSink_;
95     std::map<DHType, int32_t> compSrcSaId_;
96     std::shared_ptr<ComponentPrivacy> audioCompPrivacy_ = nullptr;
97     std::shared_ptr<ComponentPrivacy> cameraCompPrivacy_ = nullptr;
98     std::shared_ptr<ComponentMonitor> compMonitorPtr_ = nullptr;
99     sptr<LowLatencyListener> lowLatencyListener_ = nullptr;
100     std::shared_ptr<DHTimer> monitorTaskTimer_ = nullptr;
101 
102     std::atomic<bool> isUnInitTimeOut_;
103 };
104 } // namespace DistributedHardware
105 } // namespace OHOS
106 #endif
107