1 /*
2 * Copyright (c) 2021-2024 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 "component_manager.h"
17
18 #include <cinttypes>
19 #include <future>
20 #include <pthread.h>
21 #include <string>
22 #include <thread>
23
24 #include "ffrt.h"
25 #include "ipc_object_stub.h"
26 #include "iservice_registry.h"
27 #include "system_ability_definition.h"
28
29 #include "anonymous_string.h"
30 #include "capability_info_manager.h"
31 #include "component_disable.h"
32 #include "component_enable.h"
33 #include "component_loader.h"
34 #include "constants.h"
35 #include "device_manager.h"
36 #include "dh_context.h"
37 #include "dh_data_sync_trigger_listener.h"
38 #include "dh_state_listener.h"
39 #include "dh_utils_hitrace.h"
40 #include "dh_utils_hisysevent.h"
41 #include "dh_utils_tool.h"
42 #include "distributed_hardware_errno.h"
43 #include "distributed_hardware_log.h"
44 #include "enabled_comps_dump.h"
45 #include "local_capability_info_manager.h"
46 #include "low_latency.h"
47 #include "meta_info_manager.h"
48 #include "publisher.h"
49 #include "task_executor.h"
50 #include "task_factory.h"
51 #include "version_info_manager.h"
52 #include "version_manager.h"
53
54 namespace OHOS {
55 namespace DistributedHardware {
56 #undef DH_LOG_TAG
57 #define DH_LOG_TAG "ComponentManager"
58
59 IMPLEMENT_SINGLE_INSTANCE(ComponentManager);
60
61 namespace {
62 constexpr int32_t ENABLE_RETRY_MAX_TIMES = 3;
63 constexpr int32_t DISABLE_RETRY_MAX_TIMES = 3;
64 constexpr int32_t ENABLE_PARAM_RETRY_TIME = 500 * 1000;
65 constexpr int32_t INVALID_SA_ID = -1;
66 constexpr int32_t UNINIT_COMPONENT_TIMEOUT_SECONDS = 2;
67 const std::string MONITOR_TASK_TIMER_ID = "monitor_task_timer_id";
68 }
69
ComponentManager()70 ComponentManager::ComponentManager() : compSource_({}), compSink_({}), compSrcSaId_({}),
71 compMonitorPtr_(std::make_shared<ComponentMonitor>()),
72 lowLatencyListener_(sptr<LowLatencyListener>(new(std::nothrow) LowLatencyListener())),
73 isUnInitTimeOut_(false), dhBizStates_({}), dhStateListener_(std::make_shared<DHStateListener>()),
74 dataSyncTriggerListener_(std::make_shared<DHDataSyncTriggerListener>()),
75 dhCommToolPtr_(std::make_shared<DHCommTool>()), needRefreshTaskParams_({})
76 {
77 DHLOGI("Ctor ComponentManager");
78 }
79
~ComponentManager()80 ComponentManager::~ComponentManager()
81 {
82 DHLOGD("Dtor ComponentManager");
83 compMonitorPtr_.reset();
84 compMonitorPtr_ = nullptr;
85 lowLatencyListener_ = nullptr;
86 }
87
Init()88 int32_t ComponentManager::Init()
89 {
90 DHLOGI("start.");
91 DHTraceStart(COMPONENT_INIT_START);
92 InitComponentHandler();
93
94 int32_t ret = InitSAMonitor();
95 if (ret != DH_FWK_SUCCESS) {
96 DHLOGE("Init SA monitor failed, ret: %{public}d", ret);
97 return ret;
98 }
99
100 StartComponent();
101 RegisterDHStateListener();
102 RegisterDataSyncTriggerListener();
103 InitDHCommTool();
104 #ifdef DHARDWARE_LOW_LATENCY
105 Publisher::GetInstance().RegisterListener(DHTopic::TOPIC_LOW_LATENCY, lowLatencyListener_);
106 #endif
107 DHLOGI("Init component success");
108 DHTraceEnd();
109 return DH_FWK_SUCCESS;
110 }
111
InitComponentHandler()112 void ComponentManager::InitComponentHandler()
113 {
114 DHLOGI("start.");
115 if (!InitCompSource()) {
116 DHLOGE("InitCompSource failed.");
117 DHTraceEnd();
118 }
119 if (!InitCompSink()) {
120 DHLOGE("InitCompSink failed.");
121 DHTraceEnd();
122 }
123 }
124
InitSAMonitor()125 int32_t ComponentManager::InitSAMonitor()
126 {
127 if (compMonitorPtr_ == nullptr) {
128 DHLOGE("compMonitorPtr_ is null.");
129 return ERR_DH_FWK_COMPONENT_MONITOR_NULL;
130 }
131 for (const auto &comp : compSource_) {
132 if (compSrcSaId_.find(comp.first) == compSrcSaId_.end()) {
133 continue;
134 }
135 compMonitorPtr_->AddSAMonitor(compSrcSaId_.at(comp.first));
136 }
137 return DH_FWK_SUCCESS;
138 }
139
StartComponent()140 void ComponentManager::StartComponent()
141 {
142 auto sourceResult = StartSource();
143 auto sinkResult = StartSink();
144
145 if (!WaitForResult(Action::START_SOURCE, sourceResult)) {
146 DHLOGE("StartSource failed, some virtual components maybe cannot work, but want to continue");
147 HiSysEventWriteMsg(DHFWK_INIT_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
148 "dhfwk start source failed.");
149 }
150 if (!WaitForResult(Action::START_SINK, sinkResult)) {
151 DHLOGE("StartSink failed, some virtual components maybe cannot work, but want to continue");
152 HiSysEventWriteMsg(DHFWK_INIT_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
153 "dhfwk start sink failed.");
154 }
155 }
156
RegisterDHStateListener()157 void ComponentManager::RegisterDHStateListener()
158 {
159 for (const auto &item : compSource_) {
160 DHLOGI("Register DH State listener, dhType: %{public}" PRIu32, (uint32_t)item.first);
161 if (item.second == nullptr) {
162 DHLOGE("comp source ptr is null");
163 continue;
164 }
165 item.second->RegisterDistributedHardwareStateListener(dhStateListener_);
166 }
167 }
168
RegisterDataSyncTriggerListener()169 void ComponentManager::RegisterDataSyncTriggerListener()
170 {
171 std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create(true);
172 eventHandler_ = std::make_shared<ComponentManager::ComponentManagerEventHandler>(runner);
173
174 for (const auto &item : compSource_) {
175 DHLOGI("Register Data Sync Trigger listener, dhType: %{public}" PRIu32, (uint32_t)item.first);
176 if (item.second == nullptr) {
177 DHLOGE("comp source ptr is null");
178 continue;
179 }
180 item.second->RegisterDataSyncTriggerListener(dataSyncTriggerListener_);
181 }
182 }
183
InitDHCommTool()184 void ComponentManager::InitDHCommTool()
185 {
186 if (dhCommToolPtr_ == nullptr) {
187 DHLOGE("DH communication tool ptr is null");
188 return;
189 }
190 DHLOGI("Init DH communication tool");
191 dhCommToolPtr_->Init();
192 }
193
UnInit()194 int32_t ComponentManager::UnInit()
195 {
196 DHLOGI("start.");
197 UnregisterDHStateListener();
198 UnregisterDataSyncTriggerListener();
199 UnInitDHCommTool();
200 StopPrivacy();
201 UnInitSAMonitor();
202 StopComponent();
203
204 #ifdef DHARDWARE_LOW_LATENCY
205 Publisher::GetInstance().UnregisterListener(DHTopic::TOPIC_LOW_LATENCY, lowLatencyListener_);
206 LowLatency::GetInstance().CloseLowLatency();
207 #endif
208 DHLOGI("Release component success");
209
210 if (isUnInitTimeOut_.load()) {
211 DHLOGE("Some component stop timeout, FORCE exit!");
212 _Exit(0);
213 }
214
215 return DH_FWK_SUCCESS;
216 }
217
UnInitSAMonitor()218 void ComponentManager::UnInitSAMonitor()
219 {
220 // clear SA monitor
221 if (compMonitorPtr_ == nullptr) {
222 DHLOGE("compMonitorPtr_ is null.");
223 return;
224 }
225 for (const auto &comp : compSource_) {
226 if (compSrcSaId_.find(comp.first) == compSrcSaId_.end()) {
227 continue;
228 }
229 compMonitorPtr_->RemoveSAMonitor(compSrcSaId_.at(comp.first));
230 }
231 }
232
UnregisterDHStateListener()233 void ComponentManager::UnregisterDHStateListener()
234 {
235 for (const auto &item : compSource_) {
236 DHLOGI("Unregister DH State listener, dhType: %{public}" PRIu32, (uint32_t)item.first);
237 if (item.second == nullptr) {
238 DHLOGE("comp source ptr is null");
239 continue;
240 }
241 item.second->UnregisterDistributedHardwareStateListener();
242 }
243 }
244
UnregisterDataSyncTriggerListener()245 void ComponentManager::UnregisterDataSyncTriggerListener()
246 {
247 for (const auto &item : compSource_) {
248 DHLOGI("Unregister Data Sync Trigger listener, dhType: %{public}" PRIu32, (uint32_t)item.first);
249 if (item.second == nullptr) {
250 DHLOGE("comp source ptr is null");
251 continue;
252 }
253 item.second->UnregisterDataSyncTriggerListener();
254 }
255 }
256
UnInitDHCommTool()257 void ComponentManager::UnInitDHCommTool()
258 {
259 if (dhCommToolPtr_ == nullptr) {
260 DHLOGE("DH communication tool ptr is null");
261 return;
262 }
263 DHLOGI("UnInit DH communication tool");
264 dhCommToolPtr_->UnInit();
265 }
266
StopComponent()267 void ComponentManager::StopComponent()
268 {
269 // stop source and sink sa
270 auto sourceResult = StopSource();
271 auto sinkResult = StopSink();
272
273 if (!WaitForResult(Action::STOP_SOURCE, sourceResult)) {
274 DHLOGE("StopSource failed, but want to continue");
275 }
276 if (!WaitForResult(Action::STOP_SINK, sinkResult)) {
277 DHLOGE("StopSource failed, but want to continue");
278 }
279
280 compSource_.clear();
281 compSink_.clear();
282 }
283
StopPrivacy()284 void ComponentManager::StopPrivacy()
285 {
286 // stop privacy
287 if (cameraCompPrivacy_ != nullptr && cameraCompPrivacy_->GetPageFlag()) {
288 cameraCompPrivacy_->StopPrivacePage("camera");
289 cameraCompPrivacy_->SetPageFlagFalse();
290 }
291
292 if (audioCompPrivacy_ != nullptr && audioCompPrivacy_->GetPageFlag()) {
293 audioCompPrivacy_->StopPrivacePage("mic");
294 audioCompPrivacy_->SetPageFlagFalse();
295 }
296 }
297
StartSource()298 ActionResult ComponentManager::StartSource()
299 {
300 DHLOGI("start.");
301 std::unordered_map<DHType, std::shared_future<int32_t>> futures;
302 std::string uuid = DHContext::GetInstance().GetDeviceInfo().uuid;
303 for (const auto &item : compSource_) {
304 if (item.second == nullptr) {
305 DHLOGE("comp source ptr is null");
306 continue;
307 }
308 CompVersion compversion;
309 VersionManager::GetInstance().GetCompVersion(uuid, item.first, compversion);
310 auto params = compversion.sourceVersion;
311 std::promise<int32_t> p;
312 std::future<int32_t> f = p.get_future();
313 std::thread([p = std::move(p), item, params] () mutable {
314 p.set_value(item.second->InitSource(params));
315 }).detach();
316 futures.emplace(item.first, f.share());
317 }
318 return futures;
319 }
320
StartSource(DHType dhType)321 ActionResult ComponentManager::StartSource(DHType dhType)
322 {
323 DHLOGI("Start Source, dhType: %{public}" PRIu32, (uint32_t)dhType);
324 std::unordered_map<DHType, std::shared_future<int32_t>> futures;
325 if (compSource_.find(dhType) == compSource_.end()) {
326 DHLOGE("Component for DHType: %{public}" PRIu32 " not init source handler", (uint32_t)dhType);
327 return futures;
328 }
329 if (compSource_[dhType] == nullptr) {
330 DHLOGE("comp source ptr is null");
331 return futures;
332 }
333 std::string uuid = DHContext::GetInstance().GetDeviceInfo().uuid;
334 CompVersion compVersion;
335 VersionManager::GetInstance().GetCompVersion(uuid, dhType, compVersion);
336 auto params = compVersion.sourceVersion;
337 std::promise<int32_t> p;
338 std::future<int32_t> f = p.get_future();
339 std::thread([p = std::move(p), this, dhType, params] () mutable {
340 p.set_value(compSource_[dhType]->InitSource(params));
341 }).detach();
342 futures.emplace(dhType, f.share());
343
344 return futures;
345 }
346
StartSink()347 ActionResult ComponentManager::StartSink()
348 {
349 DHLOGI("start.");
350 std::unordered_map<DHType, std::shared_future<int32_t>> futures;
351 std::string uuid = DHContext::GetInstance().GetDeviceInfo().uuid;
352 for (const auto &item : compSink_) {
353 if (item.second == nullptr) {
354 DHLOGE("comp sink ptr is null");
355 continue;
356 }
357 CompVersion compversion;
358 VersionManager::GetInstance().GetCompVersion(uuid, item.first, compversion);
359 auto params = compversion.sinkVersion;
360 std::promise<int32_t> p;
361 std::future<int32_t> f = p.get_future();
362 std::thread([p = std::move(p), item, params] () mutable {
363 p.set_value(item.second->InitSink(params));
364 }).detach();
365 futures.emplace(item.first, f.share());
366 if (cameraCompPrivacy_ == nullptr && item.first == DHType::CAMERA) {
367 cameraCompPrivacy_ = std::make_shared<ComponentPrivacy>();
368 item.second->RegisterPrivacyResources(cameraCompPrivacy_);
369 }
370 if (audioCompPrivacy_ == nullptr && item.first == DHType::AUDIO) {
371 audioCompPrivacy_ = std::make_shared<ComponentPrivacy>();
372 item.second->RegisterPrivacyResources(audioCompPrivacy_);
373 }
374 }
375 return futures;
376 }
377
StartSink(DHType dhType)378 ActionResult ComponentManager::StartSink(DHType dhType)
379 {
380 DHLOGI("Start Sink, dhType: %{public}" PRIu32, (uint32_t)dhType);
381 std::unordered_map<DHType, std::shared_future<int32_t>> futures;
382 if (compSink_.find(dhType) == compSink_.end()) {
383 DHLOGE("Component for DHType: %{public}" PRIu32 " not init sink handler", (uint32_t)dhType);
384 return futures;
385 }
386 if (compSink_[dhType] == nullptr) {
387 DHLOGE("comp sink ptr is null");
388 return futures;
389 }
390 std::string uuid = DHContext::GetInstance().GetDeviceInfo().uuid;
391 CompVersion compVersion;
392 VersionManager::GetInstance().GetCompVersion(uuid, dhType, compVersion);
393 auto params = compVersion.sinkVersion;
394 std::promise<int32_t> p;
395 std::future<int32_t> f = p.get_future();
396 std::thread([p = std::move(p), this, dhType, params] () mutable {
397 p.set_value(compSink_[dhType]->InitSink(params));
398 }).detach();
399 futures.emplace(dhType, f.share());
400 if (cameraCompPrivacy_ == nullptr && dhType == DHType::CAMERA) {
401 cameraCompPrivacy_ = std::make_shared<ComponentPrivacy>();
402 compSink_[dhType]->RegisterPrivacyResources(cameraCompPrivacy_);
403 }
404 if (audioCompPrivacy_ == nullptr && dhType == DHType::AUDIO) {
405 audioCompPrivacy_ = std::make_shared<ComponentPrivacy>();
406 compSink_[dhType]->RegisterPrivacyResources(audioCompPrivacy_);
407 }
408
409 return futures;
410 }
411
StopSource()412 ActionResult ComponentManager::StopSource()
413 {
414 DHLOGI("start.");
415 std::unordered_map<DHType, std::shared_future<int32_t>> futures;
416 for (const auto &item : compSource_) {
417 if (item.second == nullptr) {
418 DHLOGE("comp source ptr is null");
419 continue;
420 }
421 std::promise<int32_t> p;
422 std::future<int32_t> f = p.get_future();
423 std::thread([p = std::move(p), item] () mutable {
424 p.set_value(item.second->ReleaseSource());
425 }).detach();
426 futures.emplace(item.first, f.share());
427 }
428 return futures;
429 }
430
StopSink()431 ActionResult ComponentManager::StopSink()
432 {
433 DHLOGI("start.");
434 std::unordered_map<DHType, std::shared_future<int32_t>> futures;
435 for (const auto &item : compSink_) {
436 if (item.second == nullptr) {
437 DHLOGE("comp sink ptr is null");
438 continue;
439 }
440 std::promise<int32_t> p;
441 std::future<int32_t> f = p.get_future();
442 std::thread([p = std::move(p), item] () mutable {
443 p.set_value(item.second->ReleaseSink());
444 IHardwareHandler *hardwareHandler = nullptr;
445 int32_t status = ComponentLoader::GetInstance().GetHardwareHandler(item.first, hardwareHandler);
446 if (status != DH_FWK_SUCCESS || hardwareHandler == nullptr) {
447 DHLOGE("GetHardwareHandler %{public}#X failed", item.first);
448 return status;
449 }
450 hardwareHandler->UnRegisterPluginListener();
451 return status;
452 }).detach();
453 futures.emplace(item.first, f.share());
454 }
455 return futures;
456 }
457
WaitForResult(const Action &action, ActionResult actionsResult)458 bool ComponentManager::WaitForResult(const Action &action, ActionResult actionsResult)
459 {
460 DHLOGD("start.");
461 auto ret = true;
462 for (auto &iter : actionsResult) {
463 std::future_status status = iter.second.wait_for(std::chrono::seconds(UNINIT_COMPONENT_TIMEOUT_SECONDS));
464 if (status == std::future_status::ready) {
465 auto result = iter.second.get();
466 DHLOGI("action = %{public}d, compType = %{public}#X, READY, ret = %{public}d.",
467 static_cast<int32_t>(action), iter.first, result);
468 if (result != DH_FWK_SUCCESS) {
469 ret = false;
470 DHLOGE("there is error, but want to continue.");
471 }
472 }
473
474 if (status == std::future_status::timeout) {
475 DHLOGI("action = %{public}d, compType = %{public}#X, TIMEOUT", static_cast<int32_t>(action), iter.first);
476 if (action == Action::STOP_SOURCE || action == Action::STOP_SINK) {
477 isUnInitTimeOut_ = true;
478 }
479 }
480
481 if (status == std::future_status::deferred) {
482 DHLOGI("action = %{public}d, compType = %{public}#X, DEFERRED", static_cast<int32_t>(action), iter.first);
483 }
484 }
485 DHLOGD("end.");
486 return ret;
487 }
488
InitCompSource()489 bool ComponentManager::InitCompSource()
490 {
491 auto compTypes = ComponentLoader::GetInstance().GetAllCompTypes();
492 for (const auto &type : compTypes) {
493 IDistributedHardwareSource *sourcePtr = nullptr;
494 auto ret = ComponentLoader::GetInstance().GetSource(type, sourcePtr);
495 if (ret != DH_FWK_SUCCESS) {
496 DHLOGW("GetSource failed, compType = %{public}#X, ret = %{public}d.", type, ret);
497 continue;
498 }
499 if (sourcePtr == nullptr) {
500 DHLOGW("sourcePtr is null, compType = %{public}#X.", type);
501 continue;
502 }
503 compSource_.insert(std::make_pair(type, sourcePtr));
504
505 int32_t saId = ComponentLoader::GetInstance().GetSourceSaId(type);
506 if (saId != INVALID_SA_ID) {
507 compSrcSaId_.insert(std::make_pair(type, saId));
508 }
509 }
510 return !compSource_.empty();
511 }
512
InitCompSink()513 bool ComponentManager::InitCompSink()
514 {
515 auto compTypes = ComponentLoader::GetInstance().GetAllCompTypes();
516 for (const auto &type : compTypes) {
517 IDistributedHardwareSink *sinkPtr = nullptr;
518 auto ret = ComponentLoader::GetInstance().GetSink(type, sinkPtr);
519 if (ret != DH_FWK_SUCCESS) {
520 DHLOGW("GetSink failed, compType = %{public}#X, ret = %{public}d.", type, ret);
521 continue;
522 }
523 if (sinkPtr == nullptr) {
524 DHLOGW("sinkPtr is null, compType = %{public}#X.", type);
525 continue;
526 }
527 compSink_.insert(std::make_pair(type, sinkPtr));
528 }
529 return !compSink_.empty();
530 }
531
Enable(const std::string &networkId, const std::string &uuid, const std::string &dhId, const DHType dhType)532 int32_t ComponentManager::Enable(const std::string &networkId, const std::string &uuid, const std::string &dhId,
533 const DHType dhType)
534 {
535 if (!IsIdLengthValid(networkId) || !IsIdLengthValid(uuid) || !IsIdLengthValid(dhId)) {
536 return ERR_DH_FWK_PARA_INVALID;
537 }
538 DHLOGI("start.");
539 if (compSource_.find(dhType) == compSource_.end()) {
540 DHLOGE("can not find handler for dhId = %{public}s.", GetAnonyString(dhId).c_str());
541 return ERR_DH_FWK_PARA_INVALID;
542 }
543 EnableParam param;
544 auto ret = GetEnableParam(networkId, uuid, dhId, dhType, param);
545 if (ret != DH_FWK_SUCCESS) {
546 DHLOGE("GetEnableParam failed, uuid = %{public}s, dhId = %{public}s, errCode = %{public}d",
547 GetAnonyString(uuid).c_str(), GetAnonyString(dhId).c_str(), ret);
548 if (RetryGetEnableParam(networkId, uuid, dhId, dhType, param) != DH_FWK_SUCCESS) {
549 return ret;
550 }
551 }
552 ret = CheckSubtypeResource(param.subtype, networkId);
553 if (ret != DH_FWK_SUCCESS) {
554 DHLOGE("CheckSubtypeResource failed, ret = %{public}d.", ret);
555 return ret;
556 }
557
558 auto compEnable = std::make_shared<ComponentEnable>();
559 auto result = compEnable->Enable(networkId, dhId, param, (compSource_.find(dhType))->second);
560 if (result != DH_FWK_SUCCESS) {
561 for (int32_t retryCount = 0; retryCount < ENABLE_RETRY_MAX_TIMES; retryCount++) {
562 if (!DHContext::GetInstance().IsDeviceOnline(uuid)) {
563 DHLOGE("device is already offline, no need try enable, uuid= %{public}s", GetAnonyString(uuid).c_str());
564 return result;
565 }
566 if (compEnable->Enable(networkId, dhId, param, (compSource_.find(dhType))->second) == DH_FWK_SUCCESS) {
567 DHLOGE("enable success, retryCount = %{public}d", retryCount);
568 EnabledCompsDump::GetInstance().DumpEnabledComp(networkId, dhType, dhId);
569 return DH_FWK_SUCCESS;
570 }
571 DHLOGE("enable failed, retryCount = %{public}d", retryCount);
572 }
573 return result;
574 }
575 DHLOGI("enable result is %{public}d, uuid = %{public}s, dhId = %{public}s", result, GetAnonyString(uuid).c_str(),
576 GetAnonyString(dhId).c_str());
577 EnabledCompsDump::GetInstance().DumpEnabledComp(networkId, dhType, dhId);
578
579 return result;
580 }
581
CheckSubtypeResource(const std::string &subtype, const std::string &networkId)582 int32_t ComponentManager::CheckSubtypeResource(const std::string &subtype, const std::string &networkId)
583 {
584 #ifdef DHARDWARE_CHECK_RESOURCE
585 std::map<std::string, bool> resourceDesc = ComponentLoader::GetInstance().GetCompResourceDesc();
586 if (resourceDesc.find(subtype) == resourceDesc.end()) {
587 DHLOGE("GetCompResourceDesc failed, subtype: %{public}s", subtype.c_str());
588 return ERR_DH_FWK_RESOURCE_KEY_IS_EMPTY;
589 }
590 if (resourceDesc[subtype] && !IsIdenticalAccount(networkId)) {
591 DHLOGE("Privacy resources must be logged in with the same account.");
592 return ERR_DH_FWK_COMPONENT_ENABLE_FAILED;
593 }
594 #endif
595 return DH_FWK_SUCCESS;
596 }
597
RetryGetEnableParam(const std::string &networkId, const std::string &uuid, const std::string &dhId, const DHType dhType, EnableParam ¶m)598 int32_t ComponentManager::RetryGetEnableParam(const std::string &networkId, const std::string &uuid,
599 const std::string &dhId, const DHType dhType, EnableParam ¶m)
600 {
601 if (!IsIdLengthValid(networkId) || !IsIdLengthValid(uuid) || !IsIdLengthValid(dhId)) {
602 return ERR_DH_FWK_PARA_INVALID;
603 }
604 for (int32_t retryCount = 0; retryCount < ENABLE_RETRY_MAX_TIMES; retryCount++) {
605 if (!DHContext::GetInstance().IsDeviceOnline(uuid)) {
606 DHLOGE("device is already offline, no need try GetEnableParam, uuid = %{public}s",
607 GetAnonyString(uuid).c_str());
608 return ERR_DH_FWK_COMPONENT_ENABLE_FAILED;
609 }
610 if (GetEnableParam(networkId, uuid, dhId, dhType, param) == DH_FWK_SUCCESS) {
611 DHLOGE("GetEnableParam success, retryCount = %{public}d", retryCount);
612 break;
613 }
614 DHLOGE("GetEnableParam failed, retryCount = %{public}d", retryCount);
615 usleep(ENABLE_PARAM_RETRY_TIME);
616 }
617 return DH_FWK_SUCCESS;
618 }
619
Disable(const std::string &networkId, const std::string &uuid, const std::string &dhId, const DHType dhType)620 int32_t ComponentManager::Disable(const std::string &networkId, const std::string &uuid, const std::string &dhId,
621 const DHType dhType)
622 {
623 if (!IsIdLengthValid(networkId) || !IsIdLengthValid(uuid) || !IsIdLengthValid(dhId)) {
624 return ERR_DH_FWK_PARA_INVALID;
625 }
626 auto find = compSource_.find(dhType);
627 if (find == compSource_.end()) {
628 DHLOGE("can not find handler for dhId = %{public}s.", GetAnonyString(dhId).c_str());
629 return ERR_DH_FWK_PARA_INVALID;
630 }
631
632 auto compDisable = std::make_shared<ComponentDisable>();
633 auto result = compDisable->Disable(networkId, dhId, find->second);
634 if (result != DH_FWK_SUCCESS) {
635 for (int32_t retryCount = 0; retryCount < DISABLE_RETRY_MAX_TIMES; retryCount++) {
636 if (DHContext::GetInstance().IsDeviceOnline(uuid)) {
637 DHLOGE("device is already online, no need try disable, uuid = %{public}s",
638 GetAnonyString(uuid).c_str());
639 return result;
640 }
641 if (compDisable->Disable(networkId, dhId, find->second) == DH_FWK_SUCCESS) {
642 DHLOGE("disable success, retryCount = %{public}d", retryCount);
643 EnabledCompsDump::GetInstance().DumpDisabledComp(networkId, dhType, dhId);
644 return DH_FWK_SUCCESS;
645 }
646 DHLOGE("disable failed, retryCount = %{public}d", retryCount);
647 }
648 return result;
649 }
650 DHLOGI("disable result is %{public}d, uuid = %{public}s, dhId = %{public}s", result, GetAnonyString(uuid).c_str(),
651 GetAnonyString(dhId).c_str());
652 EnabledCompsDump::GetInstance().DumpDisabledComp(networkId, dhType, dhId);
653
654 return result;
655 }
656
GetDHType(const std::string &uuid, const std::string &dhId) const657 DHType ComponentManager::GetDHType(const std::string &uuid, const std::string &dhId) const
658 {
659 std::shared_ptr<CapabilityInfo> capability = nullptr;
660 auto ret = CapabilityInfoManager::GetInstance()->GetCapability(GetDeviceIdByUUID(uuid), dhId, capability);
661 if ((ret == DH_FWK_SUCCESS) && (capability != nullptr)) {
662 return capability->GetDHType();
663 }
664 DHLOGE("get dhType failed, uuid = %{public}s, dhId = %{public}s", GetAnonyString(uuid).c_str(),
665 GetAnonyString(dhId).c_str());
666 return DHType::UNKNOWN;
667 }
668
GetEnableCapParam(const std::string &networkId, const std::string &uuid, DHType dhType, EnableParam ¶m, std::shared_ptr<CapabilityInfo> capability)669 int32_t ComponentManager::GetEnableCapParam(const std::string &networkId, const std::string &uuid,
670 DHType dhType, EnableParam ¶m, std::shared_ptr<CapabilityInfo> capability)
671 {
672 if (!IsIdLengthValid(networkId) || !IsIdLengthValid(uuid)) {
673 return ERR_DH_FWK_PARA_INVALID;
674 }
675 DeviceInfo sourceDeviceInfo = GetLocalDeviceInfo();
676 std::vector<std::shared_ptr<CapabilityInfo>> sourceCapInfos;
677 std::string sourceDHId;
678 CapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(sourceDeviceInfo.deviceId, sourceCapInfos);
679 for (const auto &capInfo : sourceCapInfos) {
680 if (dhType == capInfo->GetDHType()) {
681 param.sourceAttrs = capInfo->GetDHAttrs();
682 sourceDHId = capInfo->GetDHId();
683 }
684 }
685 std::string sourceVersion("");
686 auto ret = GetVersion(sourceDeviceInfo.uuid, dhType, sourceVersion, false);
687 if (ret != DH_FWK_SUCCESS) {
688 DHLOGE("Get source version failed.");
689 return ERR_DH_FWK_COMPONENT_GET_SINK_VERSION_FAILED;
690 }
691 param.sourceVersion = sourceVersion;
692
693 param.sinkAttrs = capability->GetDHAttrs();
694 std::string sinkVersion("");
695 ret = GetVersion(uuid, dhType, sinkVersion, true);
696 if (ret != DH_FWK_SUCCESS) {
697 DHLOGE("Get sink version failed.");
698 // If Version DB not sync, try get sink version from meta info
699 std::shared_ptr<MetaCapabilityInfo> metaCapPtr = nullptr;
700 ret = MetaInfoManager::GetInstance()->GetMetaCapInfo(DHContext::GetInstance().GetUdidHashIdByUUID(uuid),
701 capability->GetDHId(), metaCapPtr);
702 if ((ret == DH_FWK_SUCCESS) && (metaCapPtr != nullptr)) {
703 sinkVersion = metaCapPtr->GetSinkVersion();
704 } else {
705 return ERR_DH_FWK_COMPONENT_GET_SINK_VERSION_FAILED;
706 }
707 }
708 param.sinkVersion = sinkVersion;
709 param.subtype = capability->GetDHSubtype();
710 DHLOGI("GetEnableCapParam success. dhType = %{public}#X, sink uuid =%{public}s,"
711 "sinVersion = %{public}s, source uuid =%{public}s, source dhId = %{public}s, sourceVersion = %{public}s,"
712 "subtype = %{public}s", dhType, GetAnonyString(uuid).c_str(),
713 param.sinkVersion.c_str(), GetAnonyString(sourceDeviceInfo.uuid).c_str(), GetAnonyString(sourceDHId).c_str(),
714 param.sourceVersion.c_str(), param.subtype.c_str());
715 return DH_FWK_SUCCESS;
716 }
717
GetEnableMetaParam(const std::string &networkId, const std::string &uuid, DHType dhType, EnableParam ¶m, std::shared_ptr<MetaCapabilityInfo> metaCapPtr)718 int32_t ComponentManager::GetEnableMetaParam(const std::string &networkId, const std::string &uuid,
719 DHType dhType, EnableParam ¶m, std::shared_ptr<MetaCapabilityInfo> metaCapPtr)
720 {
721 if (!IsIdLengthValid(networkId) || !IsIdLengthValid(uuid)) {
722 return ERR_DH_FWK_PARA_INVALID;
723 }
724 DeviceInfo sourceDeviceInfo = GetLocalDeviceInfo();
725 std::vector<std::shared_ptr<MetaCapabilityInfo>> sourceMetaInfos;
726 std::string sourceDHId;
727 MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(sourceDeviceInfo.udidHash, sourceMetaInfos);
728 for (const auto &metaInfo : sourceMetaInfos) {
729 if (dhType == metaInfo->GetDHType()) {
730 param.sourceAttrs = metaInfo->GetDHAttrs();
731 sourceDHId = metaInfo->GetDHId();
732 }
733 }
734 std::string sourceVersion("");
735 auto ret = GetVersion(sourceDeviceInfo.uuid, dhType, sourceVersion, false);
736 if (ret != DH_FWK_SUCCESS) {
737 DHLOGE("Get source version failed.");
738 return ERR_DH_FWK_COMPONENT_GET_SINK_VERSION_FAILED;
739 }
740 param.sourceVersion = sourceVersion;
741
742 param.sinkAttrs = metaCapPtr->GetDHAttrs();
743 param.sinkVersion = metaCapPtr->GetSinkVersion();
744 param.subtype = metaCapPtr->GetDHSubtype();
745 DHLOGI("GetEnableCapParam success. dhType = %{public}#X, sink uuid =%{public}s,"
746 "sinVersion = %{public}s, source uuid =%{public}s, source dhId = %{public}s, sourceVersion = %{public}s,"
747 "subtype = %{public}s", dhType, GetAnonyString(uuid).c_str(),
748 param.sinkVersion.c_str(), GetAnonyString(sourceDeviceInfo.uuid).c_str(), GetAnonyString(sourceDHId).c_str(),
749 param.sourceVersion.c_str(), param.subtype.c_str());
750 return DH_FWK_SUCCESS;
751 }
752
GetCapParam(const std::string &uuid, const std::string &dhId, std::shared_ptr<CapabilityInfo> &capability)753 int32_t ComponentManager::GetCapParam(const std::string &uuid, const std::string &dhId,
754 std::shared_ptr<CapabilityInfo> &capability)
755 {
756 if (!IsIdLengthValid(uuid) || !IsIdLengthValid(dhId)) {
757 return ERR_DH_FWK_PARA_INVALID;
758 }
759 std::string deviceId = GetDeviceIdByUUID(uuid);
760 auto ret = CapabilityInfoManager::GetInstance()->GetCapability(deviceId, dhId, capability);
761 if ((ret == DH_FWK_SUCCESS) && (capability != nullptr)) {
762 DHLOGI("GetCapability success, deviceId: %{public}s, uuid: %{public}s, dhId: %{public}s, ret: %{public}d",
763 GetAnonyString(deviceId).c_str(), GetAnonyString(uuid).c_str(), GetAnonyString(dhId).c_str(), ret);
764 return ret;
765 }
766
767 ret = LocalCapabilityInfoManager::GetInstance()->GetCapability(deviceId, dhId, capability);
768 if ((ret == DH_FWK_SUCCESS) && (capability != nullptr)) {
769 DHLOGI("Local GetCaps success, deviceId: %{public}s, uuid: %{public}s, dhId: %{public}s, ret: %{public}d",
770 GetAnonyString(deviceId).c_str(), GetAnonyString(uuid).c_str(), GetAnonyString(dhId).c_str(), ret);
771 return ret;
772 }
773
774 return ret;
775 }
776
GetMetaParam(const std::string &uuid, const std::string &dhId, std::shared_ptr<MetaCapabilityInfo> &metaCapPtr)777 int32_t ComponentManager::GetMetaParam(const std::string &uuid, const std::string &dhId,
778 std::shared_ptr<MetaCapabilityInfo> &metaCapPtr)
779 {
780 if (!IsIdLengthValid(uuid) || !IsIdLengthValid(dhId)) {
781 return ERR_DH_FWK_PARA_INVALID;
782 }
783 auto ret = MetaInfoManager::GetInstance()->GetMetaCapInfo(DHContext::GetInstance().GetUdidHashIdByUUID(uuid),
784 dhId, metaCapPtr);
785 if ((ret == DH_FWK_SUCCESS) && (metaCapPtr != nullptr)) {
786 DHLOGI("GetCapability success, uuid =%{public}s, dhId = %{public}s, errCode = %{public}d",
787 GetAnonyString(uuid).c_str(), GetAnonyString(dhId).c_str(), ret);
788 return ret;
789 }
790 return ret;
791 }
792
GetEnableParam(const std::string &networkId, const std::string &uuid, const std::string &dhId, DHType dhType, EnableParam ¶m)793 int32_t ComponentManager::GetEnableParam(const std::string &networkId, const std::string &uuid,
794 const std::string &dhId, DHType dhType, EnableParam ¶m)
795 {
796 if (!IsIdLengthValid(networkId) || !IsIdLengthValid(uuid) || !IsIdLengthValid(dhId)) {
797 return ERR_DH_FWK_COMPONENT_GET_ENABLE_PARAM_FAILED;
798 }
799 DHLOGI("GetEnableParam start, networkId= %{public}s, uuid = %{public}s, dhId = %{public}s, dhType = %{public}#X,",
800 GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str(), GetAnonyString(dhId).c_str(), dhType);
801 std::shared_ptr<CapabilityInfo> capability = nullptr;
802 if (GetCapParam(uuid, dhId, capability) == DH_FWK_SUCCESS) {
803 auto ret = GetEnableCapParam(networkId, uuid, dhType, param, capability);
804 if (ret == DH_FWK_SUCCESS) {
805 return ret;
806 }
807 DHLOGE("GetEnableCapParam failed.");
808 }
809
810 std::shared_ptr<MetaCapabilityInfo> metaCapPtr = nullptr;
811 if (GetMetaParam(uuid, dhId, metaCapPtr) == DH_FWK_SUCCESS) {
812 auto ret = GetEnableMetaParam(networkId, uuid, dhType, param, metaCapPtr);
813 if (ret == DH_FWK_SUCCESS) {
814 return ret;
815 }
816 DHLOGE("GetEnableMetaParam failed.");
817 }
818 DHLOGE("GetEnableParam is failed.");
819 return ERR_DH_FWK_COMPONENT_GET_ENABLE_PARAM_FAILED;
820 }
821
GetVersionFromVerMgr(const std::string &uuid, const DHType dhType, std::string &version, bool isSink)822 int32_t ComponentManager::GetVersionFromVerMgr(const std::string &uuid, const DHType dhType,
823 std::string &version, bool isSink)
824 {
825 if (!IsIdLengthValid(uuid)) {
826 return ERR_DH_FWK_PARA_INVALID;
827 }
828 CompVersion compversion;
829 int32_t ret = VersionManager::GetInstance().GetCompVersion(uuid, dhType, compversion);
830 if (ret != DH_FWK_SUCCESS) {
831 DHLOGE("Get version Manager failed, uuid =%{public}s, dhType = %{public}#X, errCode = %{public}d",
832 GetAnonyString(uuid).c_str(), dhType, ret);
833 return ret;
834 }
835 DHLOGI("Get version mgr success, sinkVersion = %{public}s, sourceVersion = %{public}s,uuid = %{public}s, "
836 "dhType = %{public}#X", compversion.sinkVersion.c_str(), compversion.sourceVersion.c_str(),
837 GetAnonyString(uuid).c_str(), dhType);
838 version = isSink ? compversion.sinkVersion : compversion.sourceVersion;
839 return DH_FWK_SUCCESS;
840 }
841
GetVersionFromVerInfoMgr(const std::string &uuid, const DHType dhType, std::string &version, bool isSink)842 int32_t ComponentManager::GetVersionFromVerInfoMgr(const std::string &uuid, const DHType dhType,
843 std::string &version, bool isSink)
844 {
845 if (!IsIdLengthValid(uuid)) {
846 return ERR_DH_FWK_PARA_INVALID;
847 }
848 VersionInfo versionInfo;
849 int32_t ret = VersionInfoManager::GetInstance()->GetVersionInfoByDeviceId(GetDeviceIdByUUID(uuid), versionInfo);
850 if (ret != DH_FWK_SUCCESS) {
851 DHLOGE("Get Version info Manager failed, uuid =%{public}s, dhType = %{public}#X, errCode = %{public}d",
852 GetAnonyString(uuid).c_str(), dhType, ret);
853 return ret;
854 }
855 auto iter = versionInfo.compVersions.find(dhType);
856 if (iter == versionInfo.compVersions.end()) {
857 DHLOGE("can not find component version for dhType = %{public}d", dhType);
858 return ERR_DH_FWK_COMPONENT_DHTYPE_NOT_FOUND;
859 }
860 DHLOGI("Get version info mgr success, sinkVersion = %{public}s, sourceVersion = %{public}s, uuid = %{public}s, "
861 "dhType = %{public}#X", iter->second.sinkVersion.c_str(), iter->second.sourceVersion.c_str(),
862 GetAnonyString(uuid).c_str(), dhType);
863 UpdateVersionCache(uuid, versionInfo);
864 version = isSink ? iter->second.sinkVersion : iter->second.sourceVersion;
865 return DH_FWK_SUCCESS;
866 }
867
GetVersion(const std::string &uuid, DHType dhType, std::string &version, bool isSink)868 int32_t ComponentManager::GetVersion(const std::string &uuid, DHType dhType, std::string &version, bool isSink)
869 {
870 if (!IsIdLengthValid(uuid)) {
871 return ERR_DH_FWK_PARA_INVALID;
872 }
873 int32_t ret = GetVersionFromVerMgr(uuid, dhType, version, isSink);
874 if ((ret == DH_FWK_SUCCESS) && (!version.empty())) {
875 return DH_FWK_SUCCESS;
876 }
877
878 ret = GetVersionFromVerInfoMgr(uuid, dhType, version, isSink);
879 if ((ret == DH_FWK_SUCCESS) && (!version.empty())) {
880 return DH_FWK_SUCCESS;
881 }
882
883 return ret;
884 }
885
UpdateVersionCache(const std::string &uuid, const VersionInfo &versionInfo)886 void ComponentManager::UpdateVersionCache(const std::string &uuid, const VersionInfo &versionInfo)
887 {
888 if (!IsIdLengthValid(uuid)) {
889 return;
890 }
891 DHVersion dhVersion;
892 dhVersion.uuid = uuid;
893 dhVersion.dhVersion = versionInfo.dhVersion;
894 dhVersion.compVersions = versionInfo.compVersions;
895 VersionManager::GetInstance().AddDHVersion(uuid, dhVersion);
896 }
897
DumpLoadedComps(std::set<DHType> &compSourceType, std::set<DHType> &compSinkType)898 void ComponentManager::DumpLoadedComps(std::set<DHType> &compSourceType, std::set<DHType> &compSinkType)
899 {
900 for (auto compSource : compSource_) {
901 compSourceType.emplace(compSource.first);
902 }
903 for (auto compSink : compSink_) {
904 compSinkType.emplace(compSink.first);
905 }
906 }
907
Recover(DHType dhType)908 void ComponentManager::Recover(DHType dhType)
909 {
910 ffrt::submit([this, dhType]() { this->DoRecover(dhType); });
911 }
912
DoRecover(DHType dhType)913 void ComponentManager::DoRecover(DHType dhType)
914 {
915 int32_t ret = pthread_setname_np(pthread_self(), DO_RECOVER);
916 if (ret != DH_FWK_SUCCESS) {
917 DHLOGE("DoRecover setname failed.");
918 }
919 // step1: restart sa process
920 ReStartSA(dhType);
921 // step2: recover distributed hardware virtual driver
922 RecoverDistributedHardware(dhType);
923 }
924
ReStartSA(DHType dhType)925 void ComponentManager::ReStartSA(DHType dhType)
926 {
927 DHLOGI("Restart SA for DHType %{public}" PRIu32, (uint32_t)dhType);
928 auto sourceResult = StartSource(dhType);
929 auto sinkResult = StartSink(dhType);
930
931 if (!WaitForResult(Action::START_SOURCE, sourceResult)) {
932 DHLOGE("ReStartSource failed, DHType: %{public}" PRIu32, (uint32_t)dhType);
933 }
934
935 if (!WaitForResult(Action::START_SINK, sinkResult)) {
936 DHLOGE("ReStartSink failed, DHType: %{public}" PRIu32, (uint32_t)dhType);
937 }
938 DHLOGI("Finish Restart");
939 }
940
RecoverDistributedHardware(DHType dhType)941 void ComponentManager::RecoverDistributedHardware(DHType dhType)
942 {
943 MetaCapInfoMap metaInfoMap;
944 MetaInfoManager::GetInstance()->GetMetaDataByDHType(dhType, metaInfoMap);
945 for (const auto &metaInfo : metaInfoMap) {
946 std::string uuid = DHContext::GetInstance().GetUUIDByDeviceId(metaInfo.second->GetDeviceId());
947 if (uuid.empty()) {
948 DHLOGE("Can not find uuid by capability deviceId: %{public}s",
949 GetAnonyString(metaInfo.second->GetDeviceId()).c_str());
950 continue;
951 }
952
953 std::string networkId = DHContext::GetInstance().GetNetworkIdByUUID(uuid);
954 if (networkId.empty()) {
955 DHLOGI("Can not find network id by uuid: %{public}s", GetAnonyString(uuid).c_str());
956 continue;
957 }
958
959 TaskParam taskParam = {
960 .networkId = networkId,
961 .uuid = uuid,
962 .dhId = metaInfo.second->GetDHId(),
963 .dhType = metaInfo.second->GetDHType()
964 };
965 auto task = TaskFactory::GetInstance().CreateTask(TaskType::ENABLE, taskParam, nullptr);
966 TaskExecutor::GetInstance().PushTask(task);
967 }
968 }
969
GetDHSinkInstance()970 std::map<DHType, IDistributedHardwareSink*> ComponentManager::GetDHSinkInstance()
971 {
972 return compSink_;
973 }
974
IsIdenticalAccount(const std::string &networkId)975 bool ComponentManager::IsIdenticalAccount(const std::string &networkId)
976 {
977 if (!IsIdLengthValid(networkId)) {
978 return false;
979 }
980 DmAuthForm authForm = DmAuthForm::INVALID_TYPE;
981 std::vector<DmDeviceInfo> deviceList;
982 DeviceManager::GetInstance().GetTrustedDeviceList(DH_FWK_PKG_NAME, "", deviceList);
983 if (deviceList.size() == 0 || deviceList.size() > MAX_ONLINE_DEVICE_SIZE) {
984 DHLOGE("DeviceList size is invalid!");
985 return false;
986 }
987 for (const auto &deviceInfo : deviceList) {
988 if (std::string(deviceInfo.networkId) == networkId) {
989 authForm = deviceInfo.authForm;
990 break;
991 }
992 }
993 if (authForm == DmAuthForm::IDENTICAL_ACCOUNT) {
994 return true;
995 }
996 return false;
997 }
998
UpdateBusinessState(const std::string &networkId, const std::string &dhId, BusinessState state)999 void ComponentManager::UpdateBusinessState(const std::string &networkId, const std::string &dhId, BusinessState state)
1000 {
1001 if (!IsIdLengthValid(networkId) || !IsIdLengthValid(dhId)) {
1002 return;
1003 }
1004 DHLOGI("UpdateBusinessState, networkId: %{public}s, dhId: %{public}s, state: %{public}" PRIu32,
1005 GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str(), (uint32_t)state);
1006 {
1007 std::lock_guard<std::mutex> lock(bizStateMtx_);
1008 dhBizStates_[{networkId, dhId}] = state;
1009 }
1010
1011 if (state == BusinessState::IDLE) {
1012 TaskParam taskParam;
1013 if (!FetchNeedRefreshTask({networkId, dhId}, taskParam)) {
1014 return;
1015 }
1016 DHLOGI("The dh need refresh, networkId: %{public}s, dhId: %{public}s",
1017 GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str());
1018 auto task = TaskFactory::GetInstance().CreateTask(TaskType::ENABLE, taskParam, nullptr);
1019 TaskExecutor::GetInstance().PushTask(task);
1020 }
1021 }
1022
GetDHSourceInstance(DHType dhType)1023 IDistributedHardwareSource* ComponentManager::GetDHSourceInstance(DHType dhType)
1024 {
1025 if (compSource_.find(dhType) == compSource_.end()) {
1026 DHLOGE("can not find handler for dhType = %{public}d.", dhType);
1027 return nullptr;
1028 }
1029 return compSource_[dhType];
1030 }
1031
QueryBusinessState(const std::string &uuid, const std::string &dhId)1032 BusinessState ComponentManager::QueryBusinessState(const std::string &uuid, const std::string &dhId)
1033 {
1034 if (!IsIdLengthValid(uuid) || !IsIdLengthValid(dhId)) {
1035 return BusinessState::UNKNOWN;
1036 }
1037 std::lock_guard<std::mutex> lock(bizStateMtx_);
1038 std::pair<std::string, std::string> key = {uuid, dhId};
1039 if (dhBizStates_.find(key) == dhBizStates_.end()) {
1040 return BusinessState::UNKNOWN;
1041 }
1042
1043 return dhBizStates_.at(key);
1044 }
1045
TriggerFullCapsSync(const std::string &networkId)1046 void ComponentManager::TriggerFullCapsSync(const std::string &networkId)
1047 {
1048 if (!IsIdLengthValid(networkId)) {
1049 return;
1050 }
1051 if (dhCommToolPtr_ == nullptr) {
1052 DHLOGE("DH communication tool ptr is null");
1053 return;
1054 }
1055 dhCommToolPtr_->TriggerReqFullDHCaps(networkId);
1056 }
1057
SaveNeedRefreshTask(const TaskParam &taskParam)1058 void ComponentManager::SaveNeedRefreshTask(const TaskParam &taskParam)
1059 {
1060 std::lock_guard<std::mutex> lock(needRefreshTaskParamsMtx_);
1061 needRefreshTaskParams_[{taskParam.networkId, taskParam.dhId}] = taskParam;
1062 }
1063
FetchNeedRefreshTask(const std::pair<std::string, std::string> &taskKey, TaskParam &taskParam)1064 bool ComponentManager::FetchNeedRefreshTask(const std::pair<std::string, std::string> &taskKey, TaskParam &taskParam)
1065 {
1066 std::lock_guard<std::mutex> lock(needRefreshTaskParamsMtx_);
1067 if (needRefreshTaskParams_.find(taskKey) == needRefreshTaskParams_.end()) {
1068 return false;
1069 }
1070
1071 taskParam = needRefreshTaskParams_.at(taskKey);
1072 needRefreshTaskParams_.erase(taskKey);
1073 return true;
1074 }
1075
ComponentManagerEventHandler( const std::shared_ptr<AppExecFwk::EventRunner> runner)1076 ComponentManager::ComponentManagerEventHandler::ComponentManagerEventHandler(
1077 const std::shared_ptr<AppExecFwk::EventRunner> runner) : AppExecFwk::EventHandler(runner)
1078 {
1079 DHLOGI("Ctor ComponentManagerEventHandler");
1080 }
1081
ProcessEvent( const AppExecFwk::InnerEvent::Pointer &event)1082 void ComponentManager::ComponentManagerEventHandler::ProcessEvent(
1083 const AppExecFwk::InnerEvent::Pointer &event)
1084 {
1085 if (event == nullptr) {
1086 DHLOGE("event is nullptr");
1087 return;
1088 }
1089 uint32_t eventId = event->GetInnerEventId();
1090 switch (eventId) {
1091 case EVENT_DATA_SYNC_MANUAL: {
1092 // do muanul sync with remote
1093 auto sharedObjPtr = event->GetSharedObject<std::string>();
1094 if (sharedObjPtr == nullptr) {
1095 DHLOGE("The data sync param invalid!");
1096 break;
1097 }
1098 std::string networkId = *sharedObjPtr;
1099 DHLOGI("Try receive full capabiliy info from networkId: %{public}s", GetAnonyString(networkId).c_str());
1100 if (networkId.empty()) {
1101 DHLOGE("Can not get device uuid by networkId: %{public}s", GetAnonyString(networkId).c_str());
1102 break;
1103 }
1104 ComponentManager::GetInstance().TriggerFullCapsSync(networkId);
1105 break;
1106 }
1107 default:
1108 DHLOGE("event is undefined, id is %{public}d", eventId);
1109 break;
1110 }
1111 }
1112
GetEventHandler()1113 std::shared_ptr<ComponentManager::ComponentManagerEventHandler> ComponentManager::GetEventHandler()
1114 {
1115 return this->eventHandler_;
1116 }
1117
1118 } // namespace DistributedHardware
1119 } // namespace OHOS
1120