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