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 "online_task.h"
17
18 #include "anonymous_string.h"
19 #include "capability_info_manager.h"
20 #include "dh_utils_tool.h"
21 #include "distributed_hardware_errno.h"
22 #include "distributed_hardware_log.h"
23 #include "task_board.h"
24 #include "task_executor.h"
25 #include "task_factory.h"
26 #include "version_info_manager.h"
27
28 namespace OHOS {
29 namespace DistributedHardware {
30 #undef DH_LOG_TAG
31 #define DH_LOG_TAG "OnLineTask"
32
OnLineTask(const std::string & networkId,const std::string & uuid,const std::string & dhId,const DHType dhType)33 OnLineTask::OnLineTask(const std::string &networkId, const std::string &uuid, const std::string &dhId,
34 const DHType dhType) : Task(networkId, uuid, dhId, dhType)
35 {
36 SetTaskType(TaskType::ON_LINE);
37 SetTaskSteps(std::vector<TaskStep> { TaskStep::SYNC_ONLINE_INFO, TaskStep::REGISTER_ONLINE_DISTRIBUTED_HARDWARE });
38 DHLOGD("id = %s, uuid = %s", GetId().c_str(), GetAnonyString(uuid).c_str());
39 }
40
~OnLineTask()41 OnLineTask::~OnLineTask()
42 {
43 DHLOGD("id = %s, uuid = %s", GetId().c_str(), GetAnonyString(GetUUID()).c_str());
44 }
45
DoTask()46 void OnLineTask::DoTask()
47 {
48 DHLOGD("start online task, id = %s, uuid = %s", GetId().c_str(), GetAnonyString(GetUUID()).c_str());
49 this->SetTaskState(TaskState::RUNNING);
50 for (const auto& step : this->GetTaskSteps()) {
51 switch (step) {
52 case TaskStep::SYNC_ONLINE_INFO: {
53 DoSyncInfo();
54 break;
55 }
56 case TaskStep::REGISTER_ONLINE_DISTRIBUTED_HARDWARE: {
57 CreateEnableTask();
58 break;
59 }
60 default: {
61 break;
62 }
63 }
64 }
65 SetTaskState(TaskState::SUCCESS);
66 DHLOGD("finish online task, remove it, id = %s.", GetId().c_str());
67 TaskBoard::GetInstance().RemoveTask(this->GetId());
68 }
69
DoSyncInfo()70 void OnLineTask::DoSyncInfo()
71 {
72 auto ret = CapabilityInfoManager::GetInstance()->SyncDeviceInfoFromDB(GetDeviceIdByUUID(GetUUID()));
73 if (ret != DH_FWK_SUCCESS) {
74 DHLOGE("SyncDeviceInfoFromDB failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret);
75 }
76
77 ret = VersionInfoManager::GetInstance()->SyncVersionInfoFromDB(GetDeviceIdByUUID(GetUUID()));
78 if (ret != DH_FWK_SUCCESS) {
79 DHLOGE("SyncVersionInfoFromDB failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret);
80 }
81 }
82
CreateEnableTask()83 void OnLineTask::CreateEnableTask()
84 {
85 DHLOGI("networkId = %s, uuid = %s", GetAnonyString(GetNetworkId()).c_str(), GetAnonyString(GetUUID()).c_str());
86 std::vector<std::shared_ptr<CapabilityInfo>> capabilityInfos;
87 CapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(GetDeviceIdByUUID(GetUUID()), capabilityInfos);
88 if (capabilityInfos.empty()) {
89 DHLOGE("capabilityInfos is empty, can not create enableTask, uuid = %s", GetAnonyString(GetUUID()).c_str());
90 return;
91 }
92 for (const auto &iter : capabilityInfos) {
93 if (iter == nullptr) {
94 DHLOGE("capabilityInfo is null");
95 continue;
96 }
97 TaskParam taskParam = {
98 .networkId = GetNetworkId(),
99 .uuid = GetUUID(),
100 .dhId = iter->GetDHId(),
101 .dhType = iter->GetDHType()
102 };
103 auto task = TaskFactory::GetInstance().CreateTask(TaskType::ENABLE, taskParam, shared_from_this());
104 TaskExecutor::GetInstance().PushTask(task);
105 }
106 }
107 } // namespace DistributedHardware
108 } // namespace OHOS
109