• 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 "component_manager.h"
20 #include "distributed_hardware_errno.h"
21 #include "distributed_hardware_log.h"
22 #include "task_board.h"
23 
24 namespace OHOS {
25 namespace DistributedHardware {
26 #undef DH_LOG_TAG
27 #define DH_LOG_TAG "EnableTask"
28 
EnableTask(const std::string & networkId,const std::string & uuid,const std::string & dhId)29 EnableTask::EnableTask(const std::string &networkId, const std::string &uuid, const std::string &dhId)
30     : Task(networkId, uuid, dhId)
31 {
32     SetTaskType(TaskType::ENABLE);
33     SetTaskSteps(std::vector<TaskStep> { TaskStep::DO_ENABLE });
34     DHLOGD("id = %s, uuid = %s", GetId().c_str(), GetAnonyString(uuid).c_str());
35 }
36 
~EnableTask()37 EnableTask::~EnableTask()
38 {
39     DHLOGD("id = %s, uuid = %s", GetId().c_str(), GetAnonyString(GetUUID()).c_str());
40 }
41 
DoTask()42 void EnableTask::DoTask()
43 {
44     std::thread(&EnableTask::DoTaskInner, this).detach();
45 }
46 
DoTaskInner()47 void EnableTask::DoTaskInner()
48 {
49     DHLOGD("id = %s, uuid = %s, dhId = %s", GetId().c_str(), GetAnonyString(GetUUID()).c_str(), GetDhId().c_str());
50     SetTaskState(TaskState::RUNNING);
51     auto result = RegisterHardware();
52     auto state = (result == DH_FWK_SUCCESS) ? TaskState::SUCCESS : TaskState::FAIL;
53     SetTaskState(state);
54     TaskBoard::GetInstance().RemoveTask(GetId());
55     DHLOGD("finish enable task, remove it, id = %s", GetId().c_str());
56 }
57 
RegisterHardware()58 int32_t EnableTask::RegisterHardware()
59 {
60     auto result = ComponentManager::GetInstance().Enable(GetNetworkId(), GetUUID(), GetDhId());
61     DHLOGI("enable task %s, id = %s, uuid = %s, dhId = %s", (result == DH_FWK_SUCCESS) ? "success" : "failed",
62         GetId().c_str(), GetAnonyString(GetUUID()).c_str(), GetDhId().c_str());
63     return result;
64 }
65 }
66 }