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 "disable_task.h"
17
18 #include "anonymous_string.h"
19 #include "capability_utils.h"
20 #include "component_manager.h"
21 #include "dh_utils_hitrace.h"
22 #include "dh_utils_tool.h"
23 #include "distributed_hardware_errno.h"
24 #include "distributed_hardware_log.h"
25 #include "offline_task.h"
26 #include "task_board.h"
27
28 namespace OHOS {
29 namespace DistributedHardware {
30 #undef DH_LOG_TAG
31 #define DH_LOG_TAG "DisableTask"
32
DisableTask(const std::string & networkId,const std::string & uuid,const std::string & dhId,const DHType dhType)33 DisableTask::DisableTask(const std::string &networkId, const std::string &uuid, const std::string &dhId,
34 const DHType dhType) : Task(networkId, uuid, dhId, dhType)
35 {
36 SetTaskType(TaskType::DISABLE);
37 SetTaskSteps(std::vector<TaskStep> { TaskStep::DO_DISABLE });
38 DHLOGD("id = %s, uuid = %s", GetId().c_str(), GetAnonyString(uuid).c_str());
39 }
40
~DisableTask()41 DisableTask::~DisableTask()
42 {
43 DHLOGD("id = %s, uuid = %s", GetId().c_str(), GetAnonyString(GetUUID()).c_str());
44 }
45
DoTask()46 void DisableTask::DoTask()
47 {
48 std::thread(&DisableTask::DoTaskInner, this).detach();
49 }
50
DoTaskInner()51 void DisableTask::DoTaskInner()
52 {
53 DHLOGD("id = %s, uuid = %s, dhId = %s", GetId().c_str(), GetAnonyString(GetUUID()).c_str(),
54 GetAnonyString(GetDhId()).c_str());
55 SetTaskState(TaskState::RUNNING);
56
57 /* trigger Unregister Distributed Hardware Task, sync function */
58 auto result = UnRegisterHardware();
59 auto state = (result == DH_FWK_SUCCESS) ? TaskState::SUCCESS : TaskState::FAIL;
60 SetTaskState(state);
61
62 /* if finish task, notify father finish */
63 std::shared_ptr<Task> father = GetFatherTask().lock();
64 if (father != nullptr) {
65 auto offLineTask = std::static_pointer_cast<OffLineTask>(father);
66 offLineTask->NotifyFatherFinish(GetId());
67 }
68 DHLOGD("finish disable task, remove it, id = %s", GetId().c_str());
69 TaskBoard::GetInstance().RemoveTask(GetId());
70 if (result == DH_FWK_SUCCESS) {
71 std::string enabledDeviceKey = CapabilityUtils::GetCapabilityKey(GetDeviceIdByUUID(GetUUID()), GetDhId());
72 TaskBoard::GetInstance().RemoveEnabledDevice(enabledDeviceKey);
73 }
74 }
75
UnRegisterHardware()76 int32_t DisableTask::UnRegisterHardware()
77 {
78 DHCompMgrTraceStart(GetAnonyString(GetNetworkId()), GetAnonyString(GetDhId()), DH_DISABLE_START);
79 auto result = ComponentManager::GetInstance().Disable(GetNetworkId(), GetUUID(), GetDhId(), GetDhType());
80 DHLOGI("disable task %s, id = %s, uuid = %s, dhId = %s", (result == DH_FWK_SUCCESS) ? "success" : "failed",
81 GetId().c_str(), GetAnonyString(GetUUID()).c_str(), GetAnonyString(GetDhId()).c_str());
82 DHTraceEnd();
83 return result;
84 }
85 } // namespace DistributedHardware
86 } // namespace OHOS
87