• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "anonymous_string.h"
19 #include "capability_utils.h"
20 #include "component_manager.h"
21 #include "dh_utils_hitrace.h"
22 #include "dh_utils_tool.h"
23 #include "distributed_hardware_errno.h"
24 #include "distributed_hardware_log.h"
25 #include "task_board.h"
26 
27 namespace OHOS {
28 namespace DistributedHardware {
29 #undef DH_LOG_TAG
30 #define DH_LOG_TAG "EnableTask"
31 
EnableTask(const std::string & networkId,const std::string & uuid,const std::string & dhId,const DHType dhType)32 EnableTask::EnableTask(const std::string &networkId, const std::string &uuid, const std::string &dhId,
33     const DHType dhType) : Task(networkId, uuid, dhId, dhType)
34 {
35     SetTaskType(TaskType::ENABLE);
36     SetTaskSteps(std::vector<TaskStep> { TaskStep::DO_ENABLE });
37     DHLOGD("id = %s, uuid = %s", GetId().c_str(), GetAnonyString(uuid).c_str());
38 }
39 
~EnableTask()40 EnableTask::~EnableTask()
41 {
42     DHLOGD("id = %s, uuid = %s", GetId().c_str(), GetAnonyString(GetUUID()).c_str());
43 }
44 
DoTask()45 void EnableTask::DoTask()
46 {
47     std::thread(&EnableTask::DoTaskInner, this).detach();
48 }
49 
DoTaskInner()50 void EnableTask::DoTaskInner()
51 {
52     DHLOGD("id = %s, uuid = %s, dhId = %s", GetId().c_str(), GetAnonyString(GetUUID()).c_str(),
53         GetAnonyString(GetDhId()).c_str());
54     SetTaskState(TaskState::RUNNING);
55     auto result = RegisterHardware();
56     auto state = (result == DH_FWK_SUCCESS) ? TaskState::SUCCESS : TaskState::FAIL;
57     SetTaskState(state);
58     DHLOGD("finish enable task, remove it, id = %s", GetId().c_str());
59     if (result == DH_FWK_SUCCESS) {
60         TaskParam taskParam = {
61             .networkId = GetNetworkId(),
62             .uuid = GetUUID(),
63             .dhId = GetDhId(),
64             .dhType = GetDhType()
65         };
66         std::string enabledDeviceKey = CapabilityUtils::GetCapabilityKey(GetDeviceIdByUUID(GetUUID()), GetDhId());
67         TaskBoard::GetInstance().SaveEnabledDevice(enabledDeviceKey, taskParam);
68     }
69     TaskBoard::GetInstance().RemoveTask(GetId());
70 }
71 
RegisterHardware()72 int32_t EnableTask::RegisterHardware()
73 {
74     DHCompMgrTraceStart(GetAnonyString(GetNetworkId()), GetAnonyString(GetDhId()), DH_ENABLE_START);
75     auto result = ComponentManager::GetInstance().Enable(GetNetworkId(), GetUUID(), GetDhId(), GetDhType());
76     DHLOGI("enable task %s, id = %s, uuid = %s, dhId = %s", (result == DH_FWK_SUCCESS) ? "success" : "failed",
77         GetId().c_str(), GetAnonyString(GetUUID()).c_str(), GetAnonyString(GetDhId()).c_str());
78     DHTraceEnd();
79     return result;
80 }
81 } // namespace DistributedHardware
82 } // namespace OHOS
83