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 "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     auto result = ComponentManager::GetInstance().Enable(GetNetworkId(), GetUUID(), GetDhId(), GetDhType());
87     DHLOGI("enable task %{public}s, id = %{public}s, uuid = %{public}s, dhId = %{public}s",
88         (result == DH_FWK_SUCCESS) ? "success" : "failed", GetId().c_str(), GetAnonyString(GetUUID()).c_str(),
89         GetAnonyString(GetDhId()).c_str());
90     DHTraceEnd();
91     return result;
92 }
93 } // namespace DistributedHardware
94 } // namespace OHOS
95