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_enable_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 "device_type.h"
25 #include "dh_modem_context_ext.h"
26 #include "distributed_hardware_errno.h"
27 #include "distributed_hardware_log.h"
28 #include "task_board.h"
29 
30 namespace OHOS {
31 namespace DistributedHardware {
32 #undef DH_LOG_TAG
33 #define DH_LOG_TAG "MetaEnableTask"
34 
MetaEnableTask(const std::string & networkId,const std::string & uuid,const std::string & udid,const std::string & dhId,const DHType dhType)35 MetaEnableTask::MetaEnableTask(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_ENABLE);
39     SetTaskSteps(std::vector<TaskStep> { TaskStep::DO_MODEM_META_ENABLE });
40     DHLOGD("EnableTask id: %{public}s, networkId: %{public}s, dhId: %{public}s",
41         GetId().c_str(), GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str());
42 }
43 
~MetaEnableTask()44 MetaEnableTask::~MetaEnableTask()
45 {
46     DHLOGD("id = %{public}s, uuid = %{public}s", GetId().c_str(), GetAnonyString(GetUUID()).c_str());
47 }
48 
DoTask()49 void MetaEnableTask::DoTask()
50 {
51     ffrt::submit([this]() { this->DoTaskInner(); });
52 }
53 
DoTaskInner()54 void MetaEnableTask::DoTaskInner()
55 {
56     int32_t ret = pthread_setname_np(pthread_self(), META_ENABLE_TASK_INNER);
57     if (ret != DH_FWK_SUCCESS) {
58         DHLOGE("DoTaskInner setname failed.");
59     }
60     DHLOGD("DoTaskInner id = %{public}s, uuid = %{public}s, dhId = %{public}s", GetId().c_str(),
61         GetAnonyString(GetUUID()).c_str(), GetAnonyString(GetDhId()).c_str());
62     SetTaskState(TaskState::RUNNING);
63 
64     int32_t result = Enable();
65     auto state = (result == DH_FWK_SUCCESS) ? TaskState::SUCCESS : TaskState::FAIL;
66     SetTaskState(state);
67     DHLOGD("finish meta enable task, remove it, id = %{public}s", GetId().c_str());
68     TaskBoard::GetInstance().RemoveTask(GetId());
69 }
70 
Enable()71 int32_t MetaEnableTask::Enable()
72 {
73     if (DHModemContextExt::GetInstance().GetHandler() != DH_FWK_SUCCESS) {
74         DHLOGE("load distributed modem_ext so failed");
75     }
76     IDistributedHardwareSource *sourcePtr = ComponentManager::GetInstance().GetDHSourceInstance(DHType::MODEM);
77     if (sourcePtr == nullptr) {
78         DHLOGW("GetDHSourceInstance is nullptr.");
79         return ERR_DH_FWK_POINTER_IS_NULL;
80     }
81     std::shared_ptr<IDistributedModemExt> distributedModemExt_ =
82             DHModemContextExt::GetInstance().GetModemExtInstance();
83     if (distributedModemExt_ == nullptr) {
84         DHLOGE("GetModemExtInstance is nullptr.");
85         return ERR_DH_FWK_POINTER_IS_NULL;
86     }
87     DHLOGI("Meta enable, networkId = %{public}s", GetAnonyString(GetNetworkId()).c_str());
88     if (distributedModemExt_->Enable(GetNetworkId(), sourcePtr) != DH_FWK_SUCCESS) {
89         DHLOGW("Meta enable failed, dhId = %{public}s udid = %{public}s.",
90             GetAnonyString(GetDhId()).c_str(), GetAnonyString(GetUDID()).c_str());
91         return ERR_DH_FWK_PARA_INVALID;
92     }
93     return DH_FWK_SUCCESS;
94 }
95 } // namespace DistributedHardware
96 } // namespace OHOS
97