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 "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 "dh_context.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 namespace {
36 constexpr const char *DISABLE_TASK_INNER = "DisableTask";
37 }
38
39 #undef DH_LOG_TAG
40 #define DH_LOG_TAG "DisableTask"
41
DisableTask(const std::string & networkId,const std::string & uuid,const std::string & udid,const std::string & dhId,const DHType dhType)42 DisableTask::DisableTask(const std::string &networkId, const std::string &uuid, const std::string &udid,
43 const std::string &dhId, const DHType dhType) : Task(networkId, uuid, udid, dhId, dhType)
44 {
45 SetTaskType(TaskType::DISABLE);
46 SetTaskSteps(std::vector<TaskStep> { TaskStep::DO_DISABLE });
47 DHLOGD("DisableTask id: %{public}s, networkId: %{public}s, dhId: %{public}s",
48 GetId().c_str(), GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str());
49 }
50
~DisableTask()51 DisableTask::~DisableTask()
52 {
53 DHLOGD("id = %{public}s, uuid = %{public}s", GetId().c_str(), GetAnonyString(GetUUID()).c_str());
54 }
55
DoTask()56 void DisableTask::DoTask()
57 {
58 ffrt::submit([this]() { this->DoTaskInner(); });
59 }
60
DoTaskInner()61 void DisableTask::DoTaskInner()
62 {
63 int32_t ret = pthread_setname_np(pthread_self(), DISABLE_TASK_INNER);
64 if (ret != DH_FWK_SUCCESS) {
65 DHLOGE("DoTaskInner setname failed.");
66 }
67 DHLOGD("id = %{public}s, uuid = %{public}s, dhId = %{public}s", GetId().c_str(), GetAnonyString(GetUUID()).c_str(),
68 GetAnonyString(GetDhId()).c_str());
69 SetTaskState(TaskState::RUNNING);
70
71 /* trigger Unregister Distributed Hardware Task, sync function */
72 auto result = UnRegisterHardware();
73 if (result == DH_FWK_SUCCESS) {
74 std::string enabledDeviceKey = GetCapabilityKey(GetDeviceIdByUUID(GetUUID()), GetDhId());
75 TaskBoard::GetInstance().RemoveEnabledDevice(enabledDeviceKey);
76 }
77 auto state = (result == DH_FWK_SUCCESS) ? TaskState::SUCCESS : TaskState::FAIL;
78 SetTaskState(state);
79
80 DHLOGD("finish disable task, remove it, id = %{public}s", GetId().c_str());
81 std::string taskId = GetId();
82 std::shared_ptr<Task> father = GetFatherTask().lock();
83 TaskBoard::GetInstance().RemoveTask(taskId);
84 /* if finish task, notify father finish */
85 if (father != nullptr) {
86 auto offLineTask = std::static_pointer_cast<OffLineTask>(father);
87 offLineTask->NotifyFatherFinish(taskId);
88 }
89 }
90
UnRegisterHardware()91 int32_t DisableTask::UnRegisterHardware()
92 {
93 DHCompMgrTraceStart(GetAnonyString(GetNetworkId()), GetAnonyString(GetDhId()), DH_DISABLE_START);
94
95 int32_t ret = DH_FWK_SUCCESS;
96
97 // Determine whether it is an active disable
98 if (GetCallingUid() || GetCallingPid()) {
99 // It is an active disable
100 ret = DoActiveDisable();
101 } else {
102 // It is an auto disable
103 ret = DoAutoDisable();
104 }
105 DHLOGI("disable task %{public}s, id = %{public}s, uuid = %{public}s, dhId = %{public}s.",
106 (ret == DH_FWK_SUCCESS) ? "success" : "failed", GetId().c_str(), GetAnonyString(GetUUID()).c_str(),
107 GetAnonyString(GetDhId()).c_str());
108 DHTraceEnd();
109 return ret;
110 }
111
SetEffectSink(bool isEffect)112 void DisableTask::SetEffectSink(bool isEffect)
113 {
114 effectSink_ = isEffect;
115 }
116
GetEffectSink()117 bool DisableTask::GetEffectSink()
118 {
119 return effectSink_;
120 }
121
SetEffectSource(bool isEffect)122 void DisableTask::SetEffectSource(bool isEffect)
123 {
124 effectSource_ = isEffect;
125 }
126
GetEffectSource()127 bool DisableTask::GetEffectSource()
128 {
129 return effectSource_;
130 }
131
SetCallingUid(int32_t callingUid)132 void DisableTask::SetCallingUid(int32_t callingUid)
133 {
134 callingUid_ = callingUid;
135 }
136
GetCallingUid()137 int32_t DisableTask::GetCallingUid()
138 {
139 return callingUid_;
140 }
141
SetCallingPid(int32_t callingPid)142 void DisableTask::SetCallingPid(int32_t callingPid)
143 {
144 callingPid_ = callingPid;
145 }
146
GetCallingPid()147 int32_t DisableTask::GetCallingPid()
148 {
149 return callingPid_;
150 }
151
DoAutoDisable()152 int32_t DisableTask::DoAutoDisable()
153 {
154 std::string localUdid = GetLocalUdid();
155 if (localUdid == GetUDID()) {
156 auto ret = DH_FWK_SUCCESS;
157 if (DHContext::GetInstance().GetRealTimeOnlineDeviceCount() == 0 &&
158 DHContext::GetInstance().GetIsomerismConnectCount() == 0) {
159 DHDescriptor dhDescriptor {
160 .id = GetDhId(),
161 .dhType = GetDhType()
162 };
163 DHLOGI("DisableSinkTask DhType = %{public}#X, id= %{public}s", GetDhType(),
164 GetAnonyString(GetDhId()).c_str());
165 ret = ComponentManager::GetInstance().ForceDisableSink(dhDescriptor);
166 if (ret != DH_FWK_SUCCESS) {
167 DHLOGE("DisableTask DhType = %{public}#X, failed!", GetDhType());
168 }
169 }
170 return ret;
171 }
172
173 DHDescriptor dhDescriptor {
174 .id = GetDhId(),
175 .dhType = GetDhType()
176 };
177 DHLOGI("DisableSourceTask DhType = %{public}#X, id= %{public}s", GetDhType(), GetAnonyString(GetDhId()).c_str());
178 auto ret = ComponentManager::GetInstance().ForceDisableSource(GetNetworkId(), dhDescriptor);
179 if (ret != DH_FWK_SUCCESS) {
180 DHLOGE("DisableSource failed!");
181 }
182 return ret;
183 }
184
DoActiveDisable()185 int32_t DisableTask::DoActiveDisable()
186 {
187 int32_t ret = DH_FWK_SUCCESS;
188 DHDescriptor dhDescriptor {
189 .id = GetDhId(),
190 .dhType = GetDhType()
191 };
192 if (GetEffectSink()) {
193 ret = ComponentManager::GetInstance().DisableSink(dhDescriptor, GetCallingUid(), GetCallingPid());
194 if (ret != DH_FWK_SUCCESS) {
195 DHLOGE("DisableSink failed!");
196 }
197 }
198 if (GetEffectSource()) {
199 ret = ComponentManager::GetInstance().DisableSource(
200 GetNetworkId(), dhDescriptor, GetCallingUid(), GetCallingPid());
201 if (ret != DH_FWK_SUCCESS) {
202 DHLOGE("DisableSource failed!");
203 }
204 }
205 return ret;
206 }
207 } // namespace DistributedHardware
208 } // namespace OHOS
209