• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 <set>
21 #include <unordered_map>
22 #include <mutex>
23 #include <future>
24 
25 #include "single_instance.h"
26 #include "component_monitor.h"
27 #include "device_type.h"
28 #include "idistributed_hardware.h"
29 #include "idistributed_hardware_sink.h"
30 #include "idistributed_hardware_source.h"
31 #include "version_info.h"
32 
33 namespace OHOS {
34 namespace DistributedHardware {
35 using ActionResult = std::unordered_map<DHType, std::shared_future<int32_t>>;
36 class ComponentManager {
37     DECLARE_SINGLE_INSTANCE_BASE(ComponentManager);
38 
39 public:
40     ComponentManager();
41     ~ComponentManager();
42 
43 public:
44     int32_t Init();
45     int32_t UnInit();
46     int32_t Enable(const std::string &networkId, const std::string &uuid, const std::string &dhId,
47         const DHType dhType);
48     int32_t Disable(const std::string &networkId, const std::string &uuid, const std::string &dhId,
49         const DHType dhType);
50 
51     void DumpLoadedComps(std::set<DHType> &compSourceType, std::set<DHType> &compSinkType);
52     void Recover(DHType dhType);
53 
54 private:
55     enum class Action : int32_t {
56         START_SOURCE,
57         START_SINK,
58         STOP_SOURCE,
59         STOP_SINK
60     };
61 
62     DHType GetDHType(const std::string &uuid, const std::string &dhId) const;
63     bool InitCompSource();
64     bool InitCompSink();
65     ActionResult StartSource();
66     ActionResult StartSource(DHType dhType);
67     ActionResult StopSource();
68     ActionResult StartSink();
69     ActionResult StartSink(DHType dhType);
70     ActionResult StopSink();
71     bool WaitForResult(const Action &action, ActionResult result);
72     int32_t GetEnableParam(const std::string &networkId, const std::string &uuid, const std::string &dhId,
73         DHType dhType, EnableParam &param);
74     int32_t GetSinkVersionFromVerMgr(const std::string &uuid, const DHType dhType,
75         std::string &sinkVersion);
76     int32_t GetSinkVersionFromVerInfoMgr(const std::string &uuid, const DHType dhType,
77         std::string &sinkVersion);
78     int32_t GetSinkVersion(const std::string &networkId, const std::string &uuid,
79         DHType dhType, std::string &sinkVersion);
80     void UpdateVersionCache(const std::string &uuid, const VersionInfo &versionInfo);
81 
82     void DoRecover(DHType dhType);
83     void ReStartSA(DHType dhType);
84     void RecoverDistributedHardware(DHType dhType);
85 
86 private:
87     std::map<DHType, IDistributedHardwareSource*> compSource_;
88     std::map<DHType, IDistributedHardwareSink*> compSink_;
89     std::map<DHType, int32_t> compSrcSaId_;
90     std::shared_ptr<ComponentMonitor> compMonitorPtr_;
91 };
92 } // namespace DistributedHardware
93 } // namespace OHOS
94 #endif
95