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