• 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 "disable_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 "offline_task.h"
23 #include "task_board.h"
24 
25 namespace OHOS {
26 namespace DistributedHardware {
27 #undef DH_LOG_TAG
28 #define DH_LOG_TAG "DisableTask"
29 
DisableTask(const std::string & networkId,const std::string & uuid,const std::string & dhId)30 DisableTask::DisableTask(const std::string &networkId, const std::string &uuid, const std::string &dhId)
31     : Task(networkId, uuid, dhId)
32 {
33     SetTaskType(TaskType::DISABLE);
34     SetTaskSteps(std::vector<TaskStep> { TaskStep::DO_DISABLE });
35     DHLOGD("id = %s, uuid = %s", GetId().c_str(), GetAnonyString(uuid).c_str());
36 }
37 
~DisableTask()38 DisableTask::~DisableTask()
39 {
40     DHLOGD("id = %s, uuid = %s", GetId().c_str(), GetAnonyString(GetUUID()).c_str());
41 }
42 
DoTask()43 void DisableTask::DoTask()
44 {
45     std::thread(&DisableTask::DoTaskInner, this).detach();
46 }
47 
DoTaskInner()48 void DisableTask::DoTaskInner()
49 {
50     DHLOGD("id = %s, uuid = %s, dhId = %s", GetId().c_str(), GetAnonyString(GetUUID()).c_str(), GetDhId().c_str());
51     SetTaskState(TaskState::RUNNING);
52 
53     /* trigger Unregister Distributed Hardware Task, sync function */
54     auto result = UnRegisterHardware();
55     auto state = (result == DH_FWK_SUCCESS) ? TaskState::SUCCESS : TaskState::FAIL;
56     SetTaskState(state);
57 
58     /* if finish task, notify father finish */
59     std::shared_ptr<Task> father = GetFatherTask().lock();
60     if (father != nullptr) {
61         auto offLineTask = std::static_pointer_cast<OffLineTask>(father);
62         offLineTask->NotifyFatherFinish(GetId());
63     }
64 
65     TaskBoard::GetInstance().RemoveTask(GetId());
66     DHLOGD("finish disable task, remove it, id = %s", GetId().c_str());
67 }
68 
UnRegisterHardware()69 int32_t DisableTask::UnRegisterHardware()
70 {
71     auto result = ComponentManager::GetInstance().Disable(GetNetworkId(), GetUUID(), GetDhId());
72     DHLOGI("disable task %s, id = %s, uuid = %s, dhId = %s", (result == DH_FWK_SUCCESS) ? "success" : "failed",
73         GetId().c_str(), GetAnonyString(GetUUID()).c_str(), GetDhId().c_str());
74     return result;
75 }
76 }
77 }