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 
68     DHLOGD("finish meta disable task, remove it, id = %{public}s", GetId().c_str());
69     std::string taskId = GetId();
70     std::shared_ptr<Task> father = GetFatherTask().lock();
71     TaskBoard::GetInstance().RemoveTask(taskId);
72     /* if finish task, notify father finish */
73     if (father != nullptr) {
74         auto offLineTask = std::static_pointer_cast<OffLineTask>(father);
75         offLineTask->NotifyFatherFinish(taskId);
76     }
77 }
78 
Disable()79 int32_t MetaDisableTask::Disable()
80 {
81     IDistributedHardwareSource *sourcePtr = ComponentManager::GetInstance().GetDHSourceInstance(DHType::MODEM);
82     if (sourcePtr == nullptr) {
83         DHLOGW("GetDHSourceInstance is nullptr.");
84         return ERR_DH_FWK_POINTER_IS_NULL;
85     }
86     std::shared_ptr<IDistributedModemExt> distributedModemExt_ =
87             DHModemContextExt::GetInstance().GetModemExtInstance();
88     if (distributedModemExt_ == nullptr) {
89         DHLOGE("GetModemExtInstance is nullptr.");
90         return ERR_DH_FWK_POINTER_IS_NULL;
91     }
92     DHLOGI("Meta disable, networkId = %{public}s", GetAnonyString(GetNetworkId()).c_str());
93     if (distributedModemExt_->Disable(GetNetworkId(), sourcePtr) != DH_FWK_SUCCESS) {
94         DHLOGW("Meta disable failed, dhId = %{public}s.", GetAnonyString(GetDhId()).c_str());
95         return ERR_DH_FWK_PARA_INVALID;
96     }
97     return DH_FWK_SUCCESS;
98 }
99 
100 } // namespace DistributedHardware
101 } // namespace OHOS
102