1 /*
2 * Copyright (c) 2021-2025 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 "task_board.h"
17
18 #include "anonymous_string.h"
19 #include "dh_context.h"
20 #include "dh_utils_tool.h"
21 #include "distributed_hardware_errno.h"
22 #include "distributed_hardware_log.h"
23
24 namespace OHOS {
25 namespace DistributedHardware {
26 #undef DH_LOG_TAG
27 #define DH_LOG_TAG "TaskBoard"
28
29 constexpr int32_t TASK_TIMEOUT_MS = 5000;
30
31 IMPLEMENT_SINGLE_INSTANCE(TaskBoard);
32
WaitForALLTaskFinish()33 int32_t TaskBoard::WaitForALLTaskFinish()
34 {
35 // wait for all task finish until timeout
36 std::unique_lock<std::mutex> lock(tasksMtx_);
37 auto status = conVar_.wait_for(lock, std::chrono::milliseconds(TASK_TIMEOUT_MS),
38 [this]() { return tasks_.empty(); });
39 if (!status) {
40 DHLOGE("wait for all task finish timeout");
41 return ERR_DH_FWK_TASK_TIMEOUT;
42 }
43 DHLOGI("all task finished");
44
45 return DH_FWK_SUCCESS;
46 }
47
IsAllTaskFinish()48 bool TaskBoard::IsAllTaskFinish()
49 {
50 std::lock_guard<std::mutex> lock(tasksMtx_);
51 return this->tasks_.empty();
52 }
53
AddTask(std::shared_ptr<Task> task)54 void TaskBoard::AddTask(std::shared_ptr<Task> task)
55 {
56 if (task == nullptr) {
57 DHLOGE("task is null, error");
58 return;
59 }
60
61 std::lock_guard<std::mutex> lock(tasksMtx_);
62 DHLOGI("Add task, id: %{public}s", task->GetId().c_str());
63 if (this->tasks_.find(task->GetId()) != this->tasks_.end()) {
64 DHLOGE("Task id duplicate, id: %{public}s", task->GetId().c_str());
65 return;
66 }
67 this->tasks_.emplace(task->GetId(), task);
68 }
69
IsAllDisableTaskFinish()70 bool TaskBoard::IsAllDisableTaskFinish()
71 {
72 std::lock_guard<std::mutex> lock(tasksMtx_);
73 int32_t disableCount = 0;
74 for (auto iter = tasks_.begin(); iter != tasks_.end(); iter++) {
75 if (iter->second->GetTaskType() == TaskType::DISABLE || iter->second->GetTaskType() == TaskType::META_DISABLE) {
76 disableCount++;
77 }
78 }
79 DHLOGI("DisableTask count: %{public}d", disableCount);
80 if (disableCount == 0) {
81 return true;
82 }
83 return false;
84 }
85
RemoveTask(std::string taskId)86 void TaskBoard::RemoveTask(std::string taskId)
87 {
88 std::lock_guard<std::mutex> lock(tasksMtx_);
89 DHLOGI("Remove task, id: %{public}s", taskId.c_str());
90 RemoveTaskInner(taskId);
91 if (tasks_.empty()) {
92 conVar_.notify_one();
93 }
94 }
95
RemoveTaskInner(std::string taskId)96 void TaskBoard::RemoveTaskInner(std::string taskId)
97 {
98 if (tasks_.find(taskId) == tasks_.end()) {
99 DHLOGE("Can not find removed task");
100 return;
101 }
102
103 tasks_.erase(taskId);
104 }
105
DumpAllTasks(std::vector<TaskDump> & taskInfos)106 void TaskBoard::DumpAllTasks(std::vector<TaskDump> &taskInfos)
107 {
108 std::lock_guard<std::mutex> lock(tasksMtx_);
109 for (auto t : tasks_) {
110 TaskDump taskInfo = {
111 .id = t.second->GetId(),
112 .taskType = t.second->GetTaskType(),
113 .taskParm = {
114 .networkId = t.second->GetNetworkId(),
115 .uuid = t.second->GetUUID(),
116 .dhId = t.second->GetDhId(),
117 .dhType = t.second->GetDhType(),
118 },
119 .taskSteps = t.second->GetTaskSteps()
120 };
121 taskInfos.emplace_back(taskInfo);
122 }
123 }
124
SaveEnabledDevice(const std::string & enabledDeviceKey,const TaskParam & taskParam)125 void TaskBoard::SaveEnabledDevice(const std::string &enabledDeviceKey, const TaskParam &taskParam)
126 {
127 std::lock_guard<std::mutex> lock(enabledDevicesMutex_);
128 DHLOGI("SaveEnabledDevice key is %{public}s", GetAnonyString(enabledDeviceKey).c_str());
129 enabledDevices_[enabledDeviceKey] = taskParam;
130 }
131
RemoveEnabledDevice(const std::string & enabledDeviceKey)132 void TaskBoard::RemoveEnabledDevice(const std::string &enabledDeviceKey)
133 {
134 std::lock_guard<std::mutex> lock(enabledDevicesMutex_);
135 DHLOGI("RemoveEnabledDevice key is %{public}s", GetAnonyString(enabledDeviceKey).c_str());
136 enabledDevices_.erase(enabledDeviceKey);
137 }
138
GetEnabledDevice()139 const std::unordered_map<std::string, TaskParam> TaskBoard::GetEnabledDevice()
140 {
141 std::lock_guard<std::mutex> lock(enabledDevicesMutex_);
142 if (enabledDevices_.empty()) {
143 DHLOGI("enabledDevices is empty!");
144 }
145 return enabledDevices_;
146 }
147
IsEnabledDevice(const std::string & enabledDeviceKey)148 bool TaskBoard::IsEnabledDevice(const std::string &enabledDeviceKey)
149 {
150 std::lock_guard<std::mutex> lock(enabledDevicesMutex_);
151 bool flag = false;
152 for (auto iter = enabledDevices_.begin(); iter != enabledDevices_.end(); iter++) {
153 if (iter->first == enabledDeviceKey) {
154 flag = true;
155 break;
156 }
157 }
158 return flag;
159 }
160 } // namespace DistributedHardware
161 } // namespace OHOS
162