• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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: %s", task->GetId().c_str());
63     if (this->tasks_.find(task->GetId()) != this->tasks_.end()) {
64         DHLOGE("Task id duplicate, id: %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: %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.networkId = t.second->GetNetworkId(),
98             .taskParm.uuid = t.second->GetUUID(),
99             .taskParm.dhId = t.second->GetDhId(),
100             .taskParm.dhType = t.second->GetDhType(),
101             .taskSteps = t.second->GetTaskSteps()
102         };
103         taskInfos.emplace_back(taskInfo);
104     }
105 }
106 
SaveEnabledDevice(const std::string & enabledDeviceKey,const TaskParam & taskParam)107 void TaskBoard::SaveEnabledDevice(const std::string &enabledDeviceKey, const TaskParam &taskParam)
108 {
109     std::lock_guard<std::mutex> lock(enabledDevicesMutex_);
110     DHLOGI("SaveEnabledDevice key is %s", GetAnonyString(enabledDeviceKey).c_str());
111     enabledDevices_[enabledDeviceKey] = taskParam;
112 }
113 
RemoveEnabledDevice(const std::string & enabledDeviceKey)114 void TaskBoard::RemoveEnabledDevice(const std::string &enabledDeviceKey)
115 {
116     std::lock_guard<std::mutex> lock(enabledDevicesMutex_);
117     DHLOGI("RemoveEnabledDevice key is %s", GetAnonyString(enabledDeviceKey).c_str());
118     enabledDevices_.erase(enabledDeviceKey);
119 }
120 
GetEnabledDevice()121 const std::unordered_map<std::string, TaskParam> TaskBoard::GetEnabledDevice()
122 {
123     std::lock_guard<std::mutex> lock(enabledDevicesMutex_);
124     if (enabledDevices_.empty()) {
125         DHLOGI("enabledDevices is empty!");
126     }
127     return enabledDevices_;
128 }
129 } // namespace DistributedHardware
130 } // namespace OHOS
131