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 "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
RemoveTask(std::string taskId)70 void TaskBoard::RemoveTask(std::string taskId)
71 {
72 std::lock_guard<std::mutex> lock(tasksMtx_);
73 DHLOGI("Remove task, id: %{public}s", taskId.c_str());
74 RemoveTaskInner(taskId);
75 if (tasks_.empty()) {
76 conVar_.notify_one();
77 }
78 }
79
RemoveTaskInner(std::string taskId)80 void TaskBoard::RemoveTaskInner(std::string taskId)
81 {
82 if (tasks_.find(taskId) == tasks_.end()) {
83 DHLOGE("Can not find removed task");
84 return;
85 }
86
87 tasks_.erase(taskId);
88 }
89
DumpAllTasks(std::vector<TaskDump> & taskInfos)90 void TaskBoard::DumpAllTasks(std::vector<TaskDump> &taskInfos)
91 {
92 std::lock_guard<std::mutex> lock(tasksMtx_);
93 for (auto t : tasks_) {
94 TaskDump taskInfo = {
95 .id = t.second->GetId(),
96 .taskType = t.second->GetTaskType(),
97 .taskParm = {
98 .networkId = t.second->GetNetworkId(),
99 .uuid = t.second->GetUUID(),
100 .dhId = t.second->GetDhId(),
101 .dhType = t.second->GetDhType(),
102 },
103 .taskSteps = t.second->GetTaskSteps()
104 };
105 taskInfos.emplace_back(taskInfo);
106 }
107 }
108
SaveEnabledDevice(const std::string & enabledDeviceKey,const TaskParam & taskParam)109 void TaskBoard::SaveEnabledDevice(const std::string &enabledDeviceKey, const TaskParam &taskParam)
110 {
111 std::lock_guard<std::mutex> lock(enabledDevicesMutex_);
112 DHLOGI("SaveEnabledDevice key is %{public}s", GetAnonyString(enabledDeviceKey).c_str());
113 enabledDevices_[enabledDeviceKey] = taskParam;
114 }
115
RemoveEnabledDevice(const std::string & enabledDeviceKey)116 void TaskBoard::RemoveEnabledDevice(const std::string &enabledDeviceKey)
117 {
118 std::lock_guard<std::mutex> lock(enabledDevicesMutex_);
119 DHLOGI("RemoveEnabledDevice key is %{public}s", GetAnonyString(enabledDeviceKey).c_str());
120 enabledDevices_.erase(enabledDeviceKey);
121 }
122
GetEnabledDevice()123 const std::unordered_map<std::string, TaskParam> TaskBoard::GetEnabledDevice()
124 {
125 std::lock_guard<std::mutex> lock(enabledDevicesMutex_);
126 if (enabledDevices_.empty()) {
127 DHLOGI("enabledDevices is empty!");
128 }
129 return enabledDevices_;
130 }
131
IsEnabledDevice(const std::string & enabledDeviceKey)132 bool TaskBoard::IsEnabledDevice(const std::string &enabledDeviceKey)
133 {
134 std::lock_guard<std::mutex> lock(enabledDevicesMutex_);
135 bool flag = false;
136 for (auto iter = enabledDevices_.begin(); iter != enabledDevices_.end(); iter++) {
137 if (iter->first == enabledDeviceKey) {
138 flag = true;
139 break;
140 }
141 }
142 return flag;
143 }
144 } // namespace DistributedHardware
145 } // namespace OHOS
146