1 /*
2 * Copyright (c) 2024-2025 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 "component_loader.h"
25 #include "dh_context.h"
26 #include "dh_modem_context_ext.h"
27 #include "distributed_hardware_errno.h"
28 #include "distributed_hardware_log.h"
29 #include "offline_task.h"
30 #include "task_board.h"
31
32 namespace OHOS {
33 namespace DistributedHardware {
34 namespace {
35 constexpr const char *META_DISABLE_TASK_INNER = "MetaDisableTask";
36 }
37
38 #undef DH_LOG_TAG
39 #define DH_LOG_TAG "MetaDisableTask"
40
MetaDisableTask(const std::string & networkId,const std::string & uuid,const std::string & udid,const std::string & dhId,const DHType dhType)41 MetaDisableTask::MetaDisableTask(const std::string &networkId, const std::string &uuid, const std::string &udid,
42 const std::string &dhId, const DHType dhType) : Task(networkId, uuid, udid, dhId, dhType)
43 {
44 SetTaskType(TaskType::META_DISABLE);
45 SetTaskSteps(std::vector<TaskStep> { TaskStep::DO_MODEM_META_DISABLE });
46 DHLOGD("DisableTask id: %{public}s, networkId: %{public}s, dhId: %{public}s",
47 GetId().c_str(), GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str());
48 }
49
~MetaDisableTask()50 MetaDisableTask::~MetaDisableTask()
51 {
52 DHLOGD("id = %{public}s, uuid = %{public}s", GetId().c_str(), GetAnonyString(GetUUID()).c_str());
53 }
54
DoTask()55 void MetaDisableTask::DoTask()
56 {
57 ffrt::submit([this]() { this->DoTaskInner(); });
58 }
59
DoTaskInner()60 void MetaDisableTask::DoTaskInner()
61 {
62 int32_t ret = pthread_setname_np(pthread_self(), META_DISABLE_TASK_INNER);
63 if (ret != DH_FWK_SUCCESS) {
64 DHLOGE("DoTaskInner setname failed.");
65 }
66 DHLOGD("id = %{public}s, uuid = %{public}s, dhId = %{public}s", GetId().c_str(), GetAnonyString(GetUUID()).c_str(),
67 GetAnonyString(GetDhId()).c_str());
68 SetTaskState(TaskState::RUNNING);
69
70 auto result = Disable();
71 auto state = (result == DH_FWK_SUCCESS) ? TaskState::SUCCESS : TaskState::FAIL;
72 SetTaskState(state);
73
74 DHLOGD("finish meta disable task, remove it, id = %{public}s", GetId().c_str());
75 DHContext::GetInstance().DeleteOnlineDeviceType(GetNetworkId());
76 std::string taskId = GetId();
77 std::shared_ptr<Task> father = GetFatherTask().lock();
78 TaskBoard::GetInstance().RemoveTask(taskId);
79 /* if finish task, notify father finish */
80 if (father != nullptr) {
81 auto offLineTask = std::static_pointer_cast<OffLineTask>(father);
82 offLineTask->NotifyFatherFinish(taskId);
83 }
84 }
85
Disable()86 int32_t MetaDisableTask::Disable()
87 {
88 DHLOGI("MetaDisableTask enter.");
89 IDistributedHardwareSource *sourcePtr = nullptr;
90 auto ret = ComponentLoader::GetInstance().GetSource(DHType::MODEM, sourcePtr);
91 if (ret != DH_FWK_SUCCESS) {
92 DHLOGE("Get Modem Source failed.");
93 return ret;
94 }
95 if (sourcePtr == nullptr) {
96 DHLOGW("GetDHSourceInstance is nullptr.");
97 return ERR_DH_FWK_POINTER_IS_NULL;
98 }
99 std::shared_ptr<IDistributedModemExt> dhModemExt = DHModemContextExt::GetInstance().GetModemExtInstance();
100 if (dhModemExt == nullptr) {
101 DHLOGE("GetModemExtInstance is nullptr.");
102 return ERR_DH_FWK_POINTER_IS_NULL;
103 }
104
105 DHDescriptor dhDescriptor {
106 .id = GetDhId(),
107 .dhType = GetDhType()
108 };
109 ret = ComponentManager::GetInstance().DisableMetaSource(GetNetworkId(), dhDescriptor, dhModemExt, sourcePtr);
110 if (ret != DH_FWK_SUCCESS) {
111 DHLOGE("DisableMetaSource DhType = %{public}#X, failed!", GetDhType());
112 }
113 return ret;
114 }
115
116 } // namespace DistributedHardware
117 } // namespace OHOS
118