• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "task_board.h"
24 #include "task_executor.h"
25 #include "task_factory.h"
26 #include "version_info_manager.h"
27 
28 namespace OHOS {
29 namespace DistributedHardware {
30 #undef DH_LOG_TAG
31 #define DH_LOG_TAG "OnLineTask"
32 
OnLineTask(const std::string & networkId,const std::string & uuid,const std::string & dhId,const DHType dhType)33 OnLineTask::OnLineTask(const std::string &networkId, const std::string &uuid, const std::string &dhId,
34     const DHType dhType) : Task(networkId, uuid, dhId, dhType)
35 {
36     SetTaskType(TaskType::ON_LINE);
37     SetTaskSteps(std::vector<TaskStep> { TaskStep::SYNC_ONLINE_INFO, TaskStep::REGISTER_ONLINE_DISTRIBUTED_HARDWARE });
38     DHLOGD("id = %s, uuid = %s", GetId().c_str(), GetAnonyString(uuid).c_str());
39 }
40 
~OnLineTask()41 OnLineTask::~OnLineTask()
42 {
43     DHLOGD("id = %s, uuid = %s", GetId().c_str(), GetAnonyString(GetUUID()).c_str());
44 }
45 
DoTask()46 void OnLineTask::DoTask()
47 {
48     DHLOGD("start online task, id = %s, uuid = %s", GetId().c_str(), GetAnonyString(GetUUID()).c_str());
49     this->SetTaskState(TaskState::RUNNING);
50     for (const auto& step : this->GetTaskSteps()) {
51         switch (step) {
52             case TaskStep::SYNC_ONLINE_INFO: {
53                 DoSyncInfo();
54                 break;
55             }
56             case TaskStep::REGISTER_ONLINE_DISTRIBUTED_HARDWARE: {
57                 CreateEnableTask();
58                 break;
59             }
60             default: {
61                 break;
62             }
63         }
64     }
65     SetTaskState(TaskState::SUCCESS);
66     DHLOGD("finish online task, remove it, id = %s.", GetId().c_str());
67     TaskBoard::GetInstance().RemoveTask(this->GetId());
68 }
69 
DoSyncInfo()70 void OnLineTask::DoSyncInfo()
71 {
72     DHLOGI("start sync resource when device online, uuid = %s", GetAnonyString(GetUUID()).c_str());
73     auto ret = CapabilityInfoManager::GetInstance()->ManualSync(GetNetworkId());
74     if (ret != DH_FWK_SUCCESS) {
75         DHLOGW("ManualSync failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret);
76     }
77 
78     ret = VersionInfoManager::GetInstance()->ManualSync(GetNetworkId());
79     if (ret != DH_FWK_SUCCESS) {
80         DHLOGW("ManualSync version failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret);
81     }
82 
83     ret = CapabilityInfoManager::GetInstance()->SyncDeviceInfoFromDB(GetDeviceIdByUUID(GetUUID()));
84     if (ret != DH_FWK_SUCCESS) {
85         DHLOGE("SyncDeviceInfoFromDB failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret);
86     }
87 
88     ret = VersionInfoManager::GetInstance()->SyncVersionInfoFromDB(GetDeviceIdByUUID(GetUUID()));
89     if (ret != DH_FWK_SUCCESS) {
90         DHLOGE("SyncVersionInfoFromDB failed, uuid = %s, errCode = %d", GetAnonyString(GetUUID()).c_str(), ret);
91     }
92 }
93 
CreateEnableTask()94 void OnLineTask::CreateEnableTask()
95 {
96     DHLOGI("networkId = %s, uuid = %s", GetAnonyString(GetNetworkId()).c_str(), GetAnonyString(GetUUID()).c_str());
97     std::vector<std::shared_ptr<CapabilityInfo>> capabilityInfos;
98     CapabilityInfoManager::GetInstance()->GetCapabilitiesByDeviceId(GetDeviceIdByUUID(GetUUID()), capabilityInfos);
99     if (capabilityInfos.empty()) {
100         DHLOGE("capabilityInfos is empty, can not create enableTask, uuid = %s", GetAnonyString(GetUUID()).c_str());
101         return;
102     }
103     for (const auto &iter : capabilityInfos) {
104         if (iter == nullptr) {
105             DHLOGE("capabilityInfo is null");
106             continue;
107         }
108         TaskParam taskParam = {
109             .networkId = GetNetworkId(),
110             .uuid = GetUUID(),
111             .dhId = iter->GetDHId(),
112             .dhType = iter->GetDHType()
113         };
114         auto task = TaskFactory::GetInstance().CreateTask(TaskType::ENABLE, taskParam, shared_from_this());
115         TaskExecutor::GetInstance().PushTask(task);
116     }
117 }
118 } // namespace DistributedHardware
119 } // namespace OHOS
120