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