1 /*
2 * Copyright (c) 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 "meta_disable_task.h"
17
18 #include <pthread.h>
19
20 #include "ffrt.h"
21
22 #include "anonymous_string.h"
23 #include "component_manager.h"
24 #include "dh_modem_context_ext.h"
25 #include "distributed_hardware_errno.h"
26 #include "distributed_hardware_log.h"
27 #include "offline_task.h"
28 #include "task_board.h"
29
30 namespace OHOS {
31 namespace DistributedHardware {
32 #undef DH_LOG_TAG
33 #define DH_LOG_TAG "MetaDisableTask"
34
MetaDisableTask(const std::string &networkId, const std::string &uuid, const std::string &udid, const std::string &dhId, const DHType dhType)35 MetaDisableTask::MetaDisableTask(const std::string &networkId, const std::string &uuid, const std::string &udid,
36 const std::string &dhId, const DHType dhType) : Task(networkId, uuid, udid, dhId, dhType)
37 {
38 SetTaskType(TaskType::META_DISABLE);
39 SetTaskSteps(std::vector<TaskStep> { TaskStep::DO_MODEM_META_DISABLE });
40 DHLOGD("DisableTask id: %{public}s, networkId: %{public}s, dhId: %{public}s",
41 GetId().c_str(), GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str());
42 }
43
~MetaDisableTask()44 MetaDisableTask::~MetaDisableTask()
45 {
46 DHLOGD("id = %{public}s, uuid = %{public}s", GetId().c_str(), GetAnonyString(GetUUID()).c_str());
47 }
48
DoTask()49 void MetaDisableTask::DoTask()
50 {
51 ffrt::submit([this]() { this->DoTaskInner(); });
52 }
53
DoTaskInner()54 void MetaDisableTask::DoTaskInner()
55 {
56 int32_t ret = pthread_setname_np(pthread_self(), META_DISABLE_TASK_INNER);
57 if (ret != DH_FWK_SUCCESS) {
58 DHLOGE("DoTaskInner setname failed.");
59 }
60 DHLOGD("id = %{public}s, uuid = %{public}s, dhId = %{public}s", GetId().c_str(), GetAnonyString(GetUUID()).c_str(),
61 GetAnonyString(GetDhId()).c_str());
62 SetTaskState(TaskState::RUNNING);
63
64 auto result = Disable();
65 auto state = (result == DH_FWK_SUCCESS) ? TaskState::SUCCESS : TaskState::FAIL;
66 SetTaskState(state);
67 /* if finish task, notify father finish */
68 std::shared_ptr<Task> father = GetFatherTask().lock();
69 if (father != nullptr) {
70 auto offLineTask = std::static_pointer_cast<OffLineTask>(father);
71 offLineTask->NotifyFatherFinish(GetId());
72 }
73 DHLOGD("finish meta disable task, remove it, id = %{public}s", GetId().c_str());
74 TaskBoard::GetInstance().RemoveTask(GetId());
75 }
76
Disable()77 int32_t MetaDisableTask::Disable()
78 {
79 IDistributedHardwareSource *sourcePtr = ComponentManager::GetInstance().GetDHSourceInstance(DHType::MODEM);
80 if (sourcePtr == nullptr) {
81 DHLOGW("GetDHSourceInstance is nullptr.");
82 return ERR_DH_FWK_POINTER_IS_NULL;
83 }
84 std::shared_ptr<IDistributedModemExt> distributedModemExt_ =
85 DHModemContextExt::GetInstance().GetModemExtInstance();
86 if (distributedModemExt_ == nullptr) {
87 DHLOGE("GetModemExtInstance is nullptr.");
88 return ERR_DH_FWK_POINTER_IS_NULL;
89 }
90 DHLOGI("Meta disable, networkId = %{public}s", GetAnonyString(GetNetworkId()).c_str());
91 if (distributedModemExt_->Disable(GetNetworkId(), sourcePtr) != DH_FWK_SUCCESS) {
92 DHLOGW("Meta disable failed, dhId = %{public}s.", GetAnonyString(GetDhId()).c_str());
93 return ERR_DH_FWK_PARA_INVALID;
94 }
95 return DH_FWK_SUCCESS;
96 }
97
98 } // namespace DistributedHardware
99 } // namespace OHOS
100