• 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 #include "distributed_hardware_manager.h"
17 
18 #include "anonymous_string.h"
19 #include "capability_info_manager.h"
20 #include "component_loader.h"
21 #include "component_manager.h"
22 #include "dh_context.h"
23 #include "dh_utils_hisysevent.h"
24 #include "dh_utils_tool.h"
25 #include "distributed_hardware_errno.h"
26 #include "distributed_hardware_log.h"
27 #include "hidump_helper.h"
28 #include "local_hardware_manager.h"
29 #include "publisher.h"
30 #include "task_board.h"
31 #include "task_executor.h"
32 #include "task_factory.h"
33 #include "version_info_manager.h"
34 #include "version_manager.h"
35 
36 namespace OHOS {
37 namespace DistributedHardware {
38 #undef DH_LOG_TAG
39 #define DH_LOG_TAG "DistributedHardwareManager"
40 
41 
42 IMPLEMENT_SINGLE_INSTANCE(DistributedHardwareManager);
43 
Initialize()44 int32_t DistributedHardwareManager::Initialize()
45 {
46     DHLOGI("start");
47 
48     VersionInfoManager::GetInstance()->Init();
49 
50     ComponentLoader::GetInstance().Init();
51 
52     VersionManager::GetInstance().Init();
53 
54     ComponentManager::GetInstance().Init();
55 
56     CapabilityInfoManager::GetInstance()->Init();
57 
58     LocalHardwareManager::GetInstance().Init();
59 
60     return DH_FWK_SUCCESS;
61 }
62 
Release()63 int32_t DistributedHardwareManager::Release()
64 {
65     DHLOGI("start");
66     TaskBoard::GetInstance().WaitForALLTaskFinish();
67 
68     LocalHardwareManager::GetInstance().UnInit();
69 
70     CapabilityInfoManager::GetInstance()->UnInit();
71 
72     ComponentManager::GetInstance().UnInit();
73 
74     VersionManager::GetInstance().UnInit();
75 
76     ComponentLoader::GetInstance().UnInit();
77 
78     VersionInfoManager::GetInstance()->UnInit();
79 
80     return DH_FWK_SUCCESS;
81 }
82 
SendOnLineEvent(const std::string & networkId,const std::string & uuid,uint16_t deviceType)83 int32_t DistributedHardwareManager::SendOnLineEvent(const std::string &networkId, const std::string &uuid,
84     uint16_t deviceType)
85 {
86     (void)deviceType;
87 
88     if (networkId.size() == 0 || networkId.size() > MAX_ID_LEN || uuid.size() == 0 || uuid.size() > MAX_ID_LEN) {
89         DHLOGE("NetworkId or uuid is invalid");
90         return ERR_DH_FWK_PARA_INVALID;
91     }
92 
93     DHLOGI("networkId = %s, uuid = %s", GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str());
94 
95     TaskParam taskParam = {
96         .networkId = networkId,
97         .uuid = uuid,
98         .dhId = "",
99         .dhType = DHType::UNKNOWN
100     };
101     auto task = TaskFactory::GetInstance().CreateTask(TaskType::ON_LINE, taskParam, nullptr);
102     TaskExecutor::GetInstance().PushTask(task);
103     return DH_FWK_SUCCESS;
104 }
105 
SendOffLineEvent(const std::string & networkId,const std::string & uuid,uint16_t deviceType)106 int32_t DistributedHardwareManager::SendOffLineEvent(const std::string &networkId, const std::string &uuid,
107     uint16_t deviceType)
108 {
109     (void)deviceType;
110 
111     if (networkId.empty() || networkId.size() > MAX_ID_LEN || uuid.empty() || uuid.size() > MAX_ID_LEN) {
112         DHLOGE("NetworkId or uuid is invalid");
113         return ERR_DH_FWK_PARA_INVALID;
114     }
115 
116     DHLOGI("networkId = %s, uuid = %s", GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str());
117 
118     TaskParam taskParam = {
119         .networkId = networkId,
120         .uuid = uuid,
121         .dhId = "",
122         .dhType = DHType::UNKNOWN
123     };
124     auto task = TaskFactory::GetInstance().CreateTask(TaskType::OFF_LINE, taskParam, nullptr);
125     TaskExecutor::GetInstance().PushTask(task);
126     Publisher::GetInstance().PublishMessage(DHTopic::TOPIC_DEV_OFFLINE, networkId);
127 
128     HiSysEventWriteCompOfflineMsg(DHFWK_DEV_OFFLINE, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
129         GetAnonyString(networkId), "dhfwk device offline event.");
130 
131     return DH_FWK_SUCCESS;
132 }
133 
GetOnLineCount()134 size_t DistributedHardwareManager::GetOnLineCount()
135 {
136     return DHContext::GetInstance().GetOnlineCount();
137 }
138 
GetComponentVersion(std::unordered_map<DHType,std::string> & versionMap)139 int32_t DistributedHardwareManager::GetComponentVersion(std::unordered_map<DHType, std::string> &versionMap)
140 {
141     DHLOGI("start");
142     DHVersion dhVersion;
143     int32_t ret = ComponentLoader::GetInstance().GetLocalDHVersion(dhVersion);
144     if (ret != DH_FWK_SUCCESS) {
145         DHLOGE("GetLocalDHVersion fail, errCode = %d", ret);
146         return ret;
147     }
148 
149     for (auto iter = dhVersion.compVersions.cbegin(); iter != dhVersion.compVersions.cend(); ++iter) {
150         versionMap.emplace(iter->first, iter->second.sinkVersion);
151     }
152     return DH_FWK_SUCCESS;
153 }
154 
Dump(const std::vector<std::string> & argsStr,std::string & result)155 int32_t DistributedHardwareManager::Dump(const std::vector<std::string> &argsStr, std::string &result)
156 {
157     return HidumpHelper::GetInstance().Dump(argsStr, result);
158 }
159 } // namespace DistributedHardware
160 } // namespace OHOS
161