1 /*
2 * Copyright (c) 2021 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 "offline_task.h"
17
18 #include <thread>
19
20 #include "anonymous_string.h"
21 #include "capability_info_manager.h"
22 #include "dh_utils_tool.h"
23 #include "distributed_hardware_errno.h"
24 #include "distributed_hardware_log.h"
25 #include "task_board.h"
26 #include "task_executor.h"
27 #include "task_factory.h"
28
29 namespace OHOS {
30 namespace DistributedHardware {
31 #undef DH_LOG_TAG
32 #define DH_LOG_TAG "OffLineTask"
33
OffLineTask(const std::string & networkId,const std::string & uuid,const std::string & dhId,const DHType dhType)34 OffLineTask::OffLineTask(const std::string &networkId, const std::string &uuid, const std::string &dhId,
35 const DHType dhType) : Task(networkId, uuid, dhId, dhType)
36 {
37 this->SetTaskType(TaskType::OFF_LINE);
38 this->SetTaskSteps({TaskStep::UNREGISTER_OFFLINE_DISTRIBUTED_HARDWARE, TaskStep::WAIT_UNREGISTGER_COMPLETE,
39 TaskStep::CLEAR_OFFLINE_INFO});
40 DHLOGD("id = %s, uuid = %s", GetId().c_str(), GetAnonyString(uuid).c_str());
41 }
42
~OffLineTask()43 OffLineTask::~OffLineTask()
44 {
45 DHLOGD("id = %s, uuid = %s", GetId().c_str(), GetAnonyString(GetUUID()).c_str());
46 }
47
DoTask()48 void OffLineTask::DoTask()
49 {
50 std::thread(&OffLineTask::DoTaskInner, this).detach();
51 }
52
DoTaskInner()53 void OffLineTask::DoTaskInner()
54 {
55 DHLOGD("start offline task, id = %s, uuid = %s", GetId().c_str(), GetAnonyString(GetUUID()).c_str());
56 this->SetTaskState(TaskState::RUNNING);
57 for (const auto& step : this->GetTaskSteps()) {
58 switch (step) {
59 case TaskStep::UNREGISTER_OFFLINE_DISTRIBUTED_HARDWARE: {
60 CreateDisableTask();
61 break;
62 }
63 case TaskStep::WAIT_UNREGISTGER_COMPLETE: {
64 WaitDisableTaskFinish();
65 break;
66 }
67 case TaskStep::CLEAR_OFFLINE_INFO: {
68 ClearOffLineInfo();
69 break;
70 }
71 default: {
72 break;
73 }
74 }
75 }
76
77 this->SetTaskState(TaskState::SUCCESS);
78 DHLOGD("Finish OffLine task, remove it, id: %s", GetId().c_str());
79 TaskBoard::GetInstance().RemoveTask(this->GetId());
80 }
81
CreateDisableTask()82 void OffLineTask::CreateDisableTask()
83 {
84 DHLOGI("networkId = %s, uuid = %s", GetAnonyString(GetNetworkId()).c_str(), GetAnonyString(GetUUID()).c_str());
85 std::vector<std::shared_ptr<CapabilityInfo>> capabilityInfos;
86 CapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(GetDeviceIdByUUID(GetUUID()), capabilityInfos);
87 if (capabilityInfos.empty()) {
88 DHLOGE("capabilityInfos is empty, can not create disableTask, uuid = %s", GetAnonyString(GetUUID()).c_str());
89 return;
90 }
91 for (const auto &iter : capabilityInfos) {
92 if (iter == nullptr) {
93 DHLOGE("capabilityInfo is null");
94 continue;
95 }
96 TaskParam taskParam = {
97 .networkId = GetNetworkId(),
98 .uuid = GetUUID(),
99 .dhId = iter->GetDHId(),
100 .dhType = iter->GetDHType()
101 };
102 auto task = TaskFactory::GetInstance().CreateTask(TaskType::DISABLE, taskParam, shared_from_this());
103 TaskExecutor::GetInstance().PushTask(task);
104 }
105 }
106
WaitDisableTaskFinish()107 void OffLineTask::WaitDisableTaskFinish()
108 {
109 DHLOGI("start wait disable task finish");
110 std::unique_lock<std::mutex> waitLock(unFinishTaskMtx_);
111 finishCondVar_.wait(waitLock, [&] { return this->unFinishChildrenTasks_.empty(); });
112 DHLOGI("all disable task finish");
113 }
114
ClearOffLineInfo()115 void OffLineTask::ClearOffLineInfo()
116 {
117 DHLOGI("start clear resource when device offline, uuid = %s", GetAnonyString(GetUUID()).c_str());
118 auto ret = CapabilityInfoManager::GetInstance()->RemoveCapabilityInfoInMem(GetDeviceIdByUUID(GetUUID()));
119 if (ret != DH_FWK_SUCCESS) {
120 DHLOGE("RemoveCapabilityInfoInMem failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret);
121 }
122 }
123
NotifyFatherFinish(std::string taskId)124 void OffLineTask::NotifyFatherFinish(std::string taskId)
125 {
126 {
127 std::lock_guard<std::mutex> lock(unFinishTaskMtx_);
128 this->unFinishChildrenTasks_.erase(taskId);
129 }
130 finishCondVar_.notify_all();
131 }
132
AddChildrenTask(std::shared_ptr<Task> childrenTask)133 void OffLineTask::AddChildrenTask(std::shared_ptr<Task> childrenTask)
134 {
135 std::lock_guard<std::mutex> lock(unFinishTaskMtx_);
136 this->unFinishChildrenTasks_.insert(childrenTask->GetId());
137 }
138 } // namespace DistributedHardware
139 } // namespace OHOS
140