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 "enable_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 "task_board.h"
31
32 namespace OHOS {
33 namespace DistributedHardware {
34 #undef DH_LOG_TAG
35 #define DH_LOG_TAG "EnableTask"
36
EnableTask(const std::string & networkId,const std::string & uuid,const std::string & udid,const std::string & dhId,const DHType dhType)37 EnableTask::EnableTask(const std::string &networkId, const std::string &uuid, const std::string &udid,
38 const std::string &dhId, const DHType dhType) : Task(networkId, uuid, udid, dhId, dhType)
39 {
40 SetTaskType(TaskType::ENABLE);
41 SetTaskSteps(std::vector<TaskStep> { TaskStep::DO_ENABLE });
42 DHLOGD("EnableTask id: %{public}s, networkId: %{public}s, dhId: %{public}s",
43 GetId().c_str(), GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str());
44 }
45
~EnableTask()46 EnableTask::~EnableTask()
47 {
48 DHLOGD("id = %{public}s, uuid = %{public}s", GetId().c_str(), GetAnonyString(GetUUID()).c_str());
49 }
50
DoTask()51 void EnableTask::DoTask()
52 {
53 ffrt::submit([this]() { this->DoTaskInner(); });
54 }
55
DoTaskInner()56 void EnableTask::DoTaskInner()
57 {
58 int32_t ret = pthread_setname_np(pthread_self(), ENABLE_TASK_INNER);
59 if (ret != DH_FWK_SUCCESS) {
60 DHLOGE("DoTaskInner setname failed.");
61 }
62 DHLOGD("DoTaskInner id = %{public}s, uuid = %{public}s, dhId = %{public}s", GetId().c_str(),
63 GetAnonyString(GetUUID()).c_str(), GetAnonyString(GetDhId()).c_str());
64 SetTaskState(TaskState::RUNNING);
65 auto result = RegisterHardware();
66 auto state = (result == DH_FWK_SUCCESS) ? TaskState::SUCCESS : TaskState::FAIL;
67 SetTaskState(state);
68 DHLOGD("finish enable task, remove it, id = %{public}s", GetId().c_str());
69 if (result == DH_FWK_SUCCESS) {
70 TaskParam taskParam = {
71 .networkId = GetNetworkId(),
72 .uuid = GetUUID(),
73 .udid = GetUDID(),
74 .dhId = GetDhId(),
75 .dhType = GetDhType()
76 };
77 std::string enabledDeviceKey = GetCapabilityKey(GetDeviceIdByUUID(GetUUID()), GetDhId());
78 TaskBoard::GetInstance().SaveEnabledDevice(enabledDeviceKey, taskParam);
79 }
80 TaskBoard::GetInstance().RemoveTask(GetId());
81 }
82
RegisterHardware()83 int32_t EnableTask::RegisterHardware()
84 {
85 DHCompMgrTraceStart(GetAnonyString(GetNetworkId()), GetAnonyString(GetDhId()), DH_ENABLE_START);
86
87 int32_t ret = DH_FWK_SUCCESS;
88
89 // Determine whether it is an active enable
90 if (GetCallingUid() || GetCallingPid()) {
91 // It is an active enable
92 ret = DoActiveEnable();
93 } else {
94 // It is an auto enable
95 ret = DoAutoEnable();
96 }
97 DHLOGI("enable task %{public}s, id = %{public}s, uuid = %{public}s, dhId = %{public}s.",
98 (ret == DH_FWK_SUCCESS) ? "success" : "failed", GetId().c_str(), GetAnonyString(GetUUID()).c_str(),
99 GetAnonyString(GetDhId()).c_str());
100 DHTraceEnd();
101 return ret;
102 }
103
SetEffectSink(bool isEffect)104 void EnableTask::SetEffectSink(bool isEffect)
105 {
106 effectSink_ = isEffect;
107 }
108
GetEffectSink()109 bool EnableTask::GetEffectSink()
110 {
111 return effectSink_;
112 }
113
SetEffectSource(bool isEffect)114 void EnableTask::SetEffectSource(bool isEffect)
115 {
116 effectSource_ = isEffect;
117 }
118
GetEffectSource()119 bool EnableTask::GetEffectSource()
120 {
121 return effectSource_;
122 }
123
SetCallingUid(int32_t callingUid)124 void EnableTask::SetCallingUid(int32_t callingUid)
125 {
126 callingUid_ = callingUid;
127 }
128
GetCallingUid()129 int32_t EnableTask::GetCallingUid()
130 {
131 return callingUid_;
132 }
133
SetCallingPid(int32_t callingPid)134 void EnableTask::SetCallingPid(int32_t callingPid)
135 {
136 callingPid_ = callingPid;
137 }
138
GetCallingPid()139 int32_t EnableTask::GetCallingPid()
140 {
141 return callingPid_;
142 }
143
DoAutoEnable()144 int32_t EnableTask::DoAutoEnable()
145 {
146 bool enableSink = false;
147 bool enableSource = false;
148 int32_t ret = ComponentManager::GetInstance().CheckDemandStart(GetUUID(), GetDhType(), enableSink, enableSource);
149 if (ret != DH_FWK_SUCCESS) {
150 DHLOGE("CheckDemandStart failed!");
151 return ret;
152 }
153 if (!enableSink && !enableSource) {
154 return ERR_DH_FWK_COMPONENT_LIMIT_DEMAND_START;
155 }
156 DHDescriptor dhDescriptor {
157 .dhType = GetDhType(),
158 .id = GetDhId()
159 };
160 if (enableSink) {
161 ret = ComponentManager::GetInstance().EnableSink(dhDescriptor, GetCallingUid(), GetCallingPid());
162 if (ret != DH_FWK_SUCCESS) {
163 DHLOGE("EnableSink failed!");
164 }
165 }
166 if (enableSource) {
167 ret = ComponentManager::GetInstance().EnableSource(
168 GetNetworkId(), dhDescriptor, GetCallingUid(), GetCallingPid());
169 if (ret != DH_FWK_SUCCESS) {
170 DHLOGE("EnableSource failed!");
171 }
172 }
173 return ret;
174 }
175
DoActiveEnable()176 int32_t EnableTask::DoActiveEnable()
177 {
178 int32_t ret = DH_FWK_SUCCESS;
179 DHDescriptor dhDescriptor {
180 .dhType = GetDhType(),
181 .id = GetDhId()
182 };
183 if (GetEffectSink()) {
184 ret = ComponentManager::GetInstance().EnableSink(dhDescriptor, GetCallingUid(), GetCallingPid());
185 if (ret != DH_FWK_SUCCESS) {
186 DHLOGE("EnableSink failed!");
187 }
188 }
189 if (GetEffectSource()) {
190 ret = ComponentManager::GetInstance().EnableSource(
191 GetNetworkId(), dhDescriptor, GetCallingUid(), GetCallingPid());
192 if (ret != DH_FWK_SUCCESS) {
193 DHLOGE("EnableSource failed!");
194 }
195 }
196 return ret;
197 }
198 } // namespace DistributedHardware
199 } // namespace OHOS
200