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 "enable_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 "task_board.h"
29
30 namespace OHOS {
31 namespace DistributedHardware {
32 #undef DH_LOG_TAG
33 #define DH_LOG_TAG "EnableTask"
34
EnableTask(const std::string & networkId,const std::string & uuid,const std::string & dhId,const DHType dhType)35 EnableTask::EnableTask(const std::string &networkId, const std::string &uuid, const std::string &dhId,
36 const DHType dhType) : Task(networkId, uuid, dhId, dhType)
37 {
38 SetTaskType(TaskType::ENABLE);
39 SetTaskSteps(std::vector<TaskStep> { TaskStep::DO_ENABLE });
40 DHLOGD("id = %s, uuid = %s", GetId().c_str(), GetAnonyString(uuid).c_str());
41 }
42
~EnableTask()43 EnableTask::~EnableTask()
44 {
45 DHLOGD("id = %s, uuid = %s", GetId().c_str(), GetAnonyString(GetUUID()).c_str());
46 }
47
DoTask()48 void EnableTask::DoTask()
49 {
50 std::thread(&EnableTask::DoTaskInner, this).detach();
51 }
52
DoTaskInner()53 void EnableTask::DoTaskInner()
54 {
55 int32_t ret = pthread_setname_np(pthread_self(), ENABLE_TASK_INNER);
56 if (ret != DH_FWK_SUCCESS) {
57 DHLOGE("DoTaskInner setname failed.");
58 }
59 DHLOGD("id = %s, uuid = %s, dhId = %s", GetId().c_str(), GetAnonyString(GetUUID()).c_str(),
60 GetAnonyString(GetDhId()).c_str());
61 SetTaskState(TaskState::RUNNING);
62 auto result = RegisterHardware();
63 auto state = (result == DH_FWK_SUCCESS) ? TaskState::SUCCESS : TaskState::FAIL;
64 SetTaskState(state);
65 DHLOGD("finish enable task, remove it, id = %s", GetId().c_str());
66 if (result == DH_FWK_SUCCESS) {
67 TaskParam taskParam = {
68 .networkId = GetNetworkId(),
69 .uuid = GetUUID(),
70 .dhId = GetDhId(),
71 .dhType = GetDhType()
72 };
73 std::string enabledDeviceKey = CapabilityUtils::GetCapabilityKey(GetDeviceIdByUUID(GetUUID()), GetDhId());
74 TaskBoard::GetInstance().SaveEnabledDevice(enabledDeviceKey, taskParam);
75 }
76 TaskBoard::GetInstance().RemoveTask(GetId());
77 }
78
RegisterHardware()79 int32_t EnableTask::RegisterHardware()
80 {
81 DHCompMgrTraceStart(GetAnonyString(GetNetworkId()), GetAnonyString(GetDhId()), DH_ENABLE_START);
82 auto result = ComponentManager::GetInstance().Enable(GetNetworkId(), GetUUID(), GetDhId(), GetDhType());
83 DHLOGI("enable task %s, id = %s, uuid = %s, dhId = %s", (result == DH_FWK_SUCCESS) ? "success" : "failed",
84 GetId().c_str(), GetAnonyString(GetUUID()).c_str(), GetAnonyString(GetDhId()).c_str());
85 DHTraceEnd();
86 return result;
87 }
88 } // namespace DistributedHardware
89 } // namespace OHOS
90