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