• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "online_task.h"
17 
18 #include "anonymous_string.h"
19 #include "capability_info_manager.h"
20 #include "dh_utils_tool.h"
21 #include "distributed_hardware_errno.h"
22 #include "distributed_hardware_log.h"
23 #include "dh_context.h"
24 #include "local_capability_info_manager.h"
25 #include "meta_info_manager.h"
26 #include "task_board.h"
27 #include "task_executor.h"
28 #include "task_factory.h"
29 #include "version_info_manager.h"
30 
31 namespace OHOS {
32 namespace DistributedHardware {
33 namespace {
34     constexpr uint16_t PHONE_TYPE = 14;
35 }
36 #undef DH_LOG_TAG
37 #define DH_LOG_TAG "OnLineTask"
38 
OnLineTask(const std::string & networkId,const std::string & uuid,const std::string & udid,const std::string & dhId,const DHType dhType)39 OnLineTask::OnLineTask(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::ON_LINE);
43     SetTaskSteps(std::vector<TaskStep> { TaskStep::SYNC_ONLINE_INFO, TaskStep::REGISTER_ONLINE_DISTRIBUTED_HARDWARE,
44         TaskStep::ENABLE_SINK, TaskStep::META_ENABLE_TASK});
45     DHLOGD("OnLineTask id: %{public}s, networkId: %{public}s, uuid: %{public}s, udid: %{public}s",
46         GetId().c_str(), GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str(),
47         GetAnonyString(udid).c_str());
48 }
49 
~OnLineTask()50 OnLineTask::~OnLineTask()
51 {
52     DHLOGD("id = %{public}s, uuid = %{public}s", GetId().c_str(), GetAnonyString(GetUUID()).c_str());
53 }
54 
DoTask()55 void OnLineTask::DoTask()
56 {
57     DHLOGD("start online task, id = %{public}s, networkId: %{public}s, uuid: %{public}s, udid: %{public}s",
58         GetId().c_str(), GetAnonyString(GetNetworkId()).c_str(), GetAnonyString(GetUUID()).c_str(),
59         GetAnonyString(GetUDID()).c_str());
60     this->SetTaskState(TaskState::RUNNING);
61     for (const auto& step : this->GetTaskSteps()) {
62         switch (step) {
63             case TaskStep::SYNC_ONLINE_INFO: {
64                 DoSyncInfo();
65                 break;
66             }
67             case TaskStep::REGISTER_ONLINE_DISTRIBUTED_HARDWARE: {
68                 CreateEnableTask();
69                 break;
70             }
71             case TaskStep::ENABLE_SINK: {
72                 CreateEnableSinkTask();
73                 break;
74             }
75             case TaskStep::META_ENABLE_TASK: {
76                 CreateMetaEnableTask();
77                 break;
78             }
79             default: {
80                 break;
81             }
82         }
83     }
84     SetTaskState(TaskState::SUCCESS);
85     DHLOGI("finish online task, remove it, id = %{public}s.", GetId().c_str());
86     TaskBoard::GetInstance().RemoveTask(this->GetId());
87 }
88 
DoSyncInfo()89 void OnLineTask::DoSyncInfo()
90 {
91     std::string deviceId = GetDeviceIdByUUID(GetUUID());
92     std::string udidHash = Sha256(GetUDID());
93     DHLOGI("DoSyncInfo, networkId: %{public}s, deviceId: %{public}s, uuid: %{public}s,"
94         "udid: %{public}s, udidHash: %{public}s", GetAnonyString(GetNetworkId()).c_str(),
95         GetAnonyString(deviceId).c_str(), GetAnonyString(GetUUID()).c_str(), GetAnonyString(GetUDID()).c_str(),
96         GetAnonyString(udidHash).c_str());
97 
98     auto ret = LocalCapabilityInfoManager::GetInstance()->SyncDeviceInfoFromDB(deviceId);
99     if (ret != DH_FWK_SUCCESS) {
100         DHLOGE("SyncLocalCapabilityInfoFromDB failed, deviceId = %{public}s, errCode = %{public}d",
101             GetAnonyString(deviceId).c_str(), ret);
102     }
103 
104     ret = MetaInfoManager::GetInstance()->SyncMetaInfoFromDB(udidHash);
105     if (ret != DH_FWK_SUCCESS) {
106         DHLOGE("SyncMetaInfoFromDB failed, udidHash = %{public}s, errCode = %{public}d",
107             GetAnonyString(udidHash).c_str(), ret);
108     }
109 }
110 
CreateEnableTask()111 void OnLineTask::CreateEnableTask()
112 {
113     DHLOGI("CreateEnableTask, networkId: %{public}s, uuid: %{public}s, udid: %{public}s",
114         GetAnonyString(GetNetworkId()).c_str(), GetAnonyString(GetUUID()).c_str(), GetAnonyString(GetUDID()).c_str());
115     std::string deviceId = GetDeviceIdByUUID(GetUUID());
116     std::vector<std::pair<std::string, DHType>> devDhInfos;
117     std::vector<std::shared_ptr<CapabilityInfo>> capabilityInfos;
118     CapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(deviceId, capabilityInfos);
119     std::for_each(capabilityInfos.begin(), capabilityInfos.end(), [&](std::shared_ptr<CapabilityInfo> cap) {
120         if (cap != nullptr) {
121             devDhInfos.push_back({cap->GetDHId(), cap->GetDHType()});
122         }
123     });
124 
125     if (devDhInfos.empty()) {
126         DHLOGW("Can not get cap info from CapabilityInfo, try use local Capability info");
127         LocalCapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(deviceId, capabilityInfos);
128         std::for_each(capabilityInfos.begin(), capabilityInfos.end(), [&](std::shared_ptr<CapabilityInfo> cap) {
129             if (cap != nullptr) {
130                 devDhInfos.push_back({cap->GetDHId(), cap->GetDHType()});
131             }
132         });
133     }
134 
135     if (devDhInfos.empty()) {
136         DHLOGW("Can not get cap info from local Capbility, try use meta info");
137         std::string udidHash = Sha256(GetUDID());
138         std::vector<std::shared_ptr<MetaCapabilityInfo>> metaCapInfos;
139         MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(udidHash, metaCapInfos);
140         std::for_each(metaCapInfos.begin(), metaCapInfos.end(), [&](std::shared_ptr<MetaCapabilityInfo> cap) {
141             if (cap != nullptr) {
142                 devDhInfos.push_back({cap->GetDHId(), cap->GetDHType()});
143             }
144         });
145     }
146 
147     if (devDhInfos.empty()) {
148         DHLOGE("Can not get cap info, uuid = %{public}s, deviceId = %{public}s", GetAnonyString(GetUUID()).c_str(),
149             GetAnonyString(deviceId).c_str());
150         return;
151     }
152 
153     for (const auto &info : devDhInfos) {
154         TaskParam taskParam = {
155             .networkId = GetNetworkId(),
156             .uuid = GetUUID(),
157             .udid = GetUDID(),
158             .dhId = info.first,
159             .dhType = info.second
160         };
161         auto task = TaskFactory::GetInstance().CreateTask(TaskType::ENABLE, taskParam, shared_from_this());
162         TaskExecutor::GetInstance().PushTask(task);
163     }
164 }
165 
CreateEnableSinkTask()166 void OnLineTask::CreateEnableSinkTask()
167 {
168     DeviceInfo localDeviceInfo = GetLocalDeviceInfo();
169     DHLOGI("CreateEnableSinkTask, uuid: %{public}s", GetAnonyString(localDeviceInfo.uuid).c_str());
170     std::vector<std::pair<std::string, DHType>> localMetaInfos;
171     std::vector<std::shared_ptr<MetaCapabilityInfo>> metaCapInfos;
172     MetaInfoManager::GetInstance()->GetMetaCapInfosByUdidHash(localDeviceInfo.udidHash, metaCapInfos);
173     std::for_each(metaCapInfos.begin(), metaCapInfos.end(), [&](std::shared_ptr<MetaCapabilityInfo> localMetaInfo) {
174         if (localMetaInfo != nullptr) {
175             localMetaInfos.push_back({localMetaInfo->GetDHId(), localMetaInfo->GetDHType()});
176         }
177     });
178     if (localMetaInfos.empty()) {
179         DHLOGE("Can not get localMetainfo.");
180         return;
181     }
182     for (const auto &localInfo : localMetaInfos) {
183         TaskParam taskParam = {
184             .networkId = localDeviceInfo.networkId,
185             .uuid = localDeviceInfo.uuid,
186             .udid = localDeviceInfo.udid,
187             .dhId = localInfo.first,
188             .dhType = localInfo.second
189         };
190         auto task = TaskFactory::GetInstance().CreateTask(TaskType::ENABLE, taskParam, shared_from_this());
191         TaskExecutor::GetInstance().PushTask(task);
192     }
193 }
194 
CreateMetaEnableTask()195 void OnLineTask::CreateMetaEnableTask()
196 {
197     DHLOGI("CreateMetaEnableTask, networkId: %{public}s, uuid: %{public}s, udid: %{public}s",
198         GetAnonyString(GetNetworkId()).c_str(), GetAnonyString(GetUUID()).c_str(), GetAnonyString(GetUDID()).c_str());
199     uint16_t deviceType = DHContext::GetInstance().GetDeviceTypeByNetworkId(GetNetworkId());
200     if (deviceType != PHONE_TYPE) {
201         DHLOGI("offline device not phone, deviceType = %{public}d", deviceType);
202         return;
203     }
204     TaskParam taskParam = {
205         .networkId = GetNetworkId(),
206         .uuid = GetUUID(),
207         .udid = GetUDID(),
208         .dhId = "Modem_1234",
209         .dhType = DHType::MODEM
210     };
211     auto task = TaskFactory::GetInstance().CreateTask(TaskType::META_ENABLE, taskParam, shared_from_this());
212     TaskExecutor::GetInstance().PushTask(task);
213 }
214 } // namespace DistributedHardware
215 } // namespace OHOS
216