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