1 /*
2 * Copyright (c) 2021-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 "adapter/dnetwork_adapter.h"
17
18 #include <chrono>
19 #include <functional>
20 #include <thread>
21 #include <unistd.h>
22
23 #include "errors.h"
24 #include "event_runner.h"
25
26 #include "distributed_sched_utils.h"
27 #include "dtbschedmgr_device_info_storage.h"
28 #include "dtbschedmgr_log.h"
29 #include "mission/dsched_sync_e2e.h"
30
31 namespace OHOS {
32 namespace DistributedSchedule {
33 using namespace std::chrono_literals;
34 using namespace DistributedHardware;
35
36 namespace {
37 constexpr int32_t RETRY_REGISTER_CALLBACK_TIMES = 5;
38 constexpr int32_t RETRY_REGISTER_CALLBACK_DELAY_TIME = 1000; // 1s
39 const std::string PKG_NAME = "DBinderBus_Dms_" + std::to_string(getprocpid());
40
41 const std::string EMPTY_DEVICE_ID = "";
42 const std::string TAG = "DnetworkAdapter";
43 }
44
45 std::shared_ptr<AppExecFwk::EventHandler> DnetworkAdapter::dnetworkHandler_;
46 std::mutex DnetworkAdapter::listenerSetMutex_;
47 std::set<std::shared_ptr<DeviceListener>> DnetworkAdapter::listenerSet_;
48
GetInstance()49 std::shared_ptr<DnetworkAdapter> DnetworkAdapter::GetInstance()
50 {
51 static auto instance = std::make_shared<DnetworkAdapter>();
52 return instance;
53 }
54
Init()55 void DnetworkAdapter::Init()
56 {
57 initCallback_ = std::make_shared<DeviceInitCallBack>();
58 stateCallback_ = std::make_shared<DmsDeviceStateCallback>();
59 auto runner = AppExecFwk::EventRunner::Create("dmsDnetwork");
60 dnetworkHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
61 }
62
OnRemoteDied()63 void DnetworkAdapter::DeviceInitCallBack::OnRemoteDied()
64 {
65 HILOGI("DeviceInitCallBack OnRemoteDied");
66 DtbschedmgrDeviceInfoStorage::GetInstance().Init();
67 }
68
OnDeviceOnline(const DmDeviceInfo & deviceInfo)69 void DnetworkAdapter::DmsDeviceStateCallback::OnDeviceOnline(const DmDeviceInfo& deviceInfo)
70 {
71 HILOGI("OnNodeOnline netwokId: %{public}s.", GetAnonymStr(deviceInfo.networkId).c_str());
72 if (DmsKvSyncE2E::GetInstance()->CheckDeviceCfg()) {
73 DmsKvSyncE2E::GetInstance()->PushAndPullData(deviceInfo.networkId);
74 }
75
76 auto onlineNotifyTask = [deviceInfo]() {
77 std::lock_guard<std::mutex> autoLock(listenerSetMutex_);
78 for (auto& listener : listenerSet_) {
79 if (listener != nullptr) {
80 listener->OnDeviceOnline(deviceInfo);
81 }
82 }
83 };
84 if (dnetworkHandler_ == nullptr || !dnetworkHandler_->PostTask(onlineNotifyTask)) {
85 HILOGE("OnNodeOnline post task failed");
86 return;
87 }
88 }
89
OnDeviceOffline(const DmDeviceInfo & deviceInfo)90 void DnetworkAdapter::DmsDeviceStateCallback::OnDeviceOffline(const DmDeviceInfo& deviceInfo)
91 {
92 HILOGI("OnNodeOffline networkId: %{public}s.", GetAnonymStr(deviceInfo.networkId).c_str());
93 auto offlineNotifyTask = [deviceInfo]() {
94 std::lock_guard<std::mutex> autoLock(listenerSetMutex_);
95 for (auto& listener : listenerSet_) {
96 if (listener != nullptr) {
97 listener->OnDeviceOffline(deviceInfo);
98 }
99 }
100 };
101 if (dnetworkHandler_ == nullptr || !dnetworkHandler_->PostTask(offlineNotifyTask)) {
102 HILOGE("OnNodeOffline post task failed");
103 return;
104 }
105 }
106
OnDeviceChanged(const DmDeviceInfo & deviceInfo)107 void DnetworkAdapter::DmsDeviceStateCallback::OnDeviceChanged(const DmDeviceInfo& deviceInfo)
108 {
109 std::string networkId = deviceInfo.networkId;
110 HILOGI("OnDeviceChanged networkId: %{public}s.", GetAnonymStr(networkId).c_str());
111 }
112
OnDeviceReady(const DmDeviceInfo & deviceInfo)113 void DnetworkAdapter::DmsDeviceStateCallback::OnDeviceReady(const DmDeviceInfo& deviceInfo)
114 {
115 HILOGI("called");
116 }
117
AddDeviceChangeListener(const std::shared_ptr<DeviceListener> & listener)118 bool DnetworkAdapter::AddDeviceChangeListener(const std::shared_ptr<DeviceListener>& listener)
119 {
120 HILOGD("AddDeviceChangeListener called");
121 if (dnetworkHandler_ == nullptr) {
122 HILOGE("handler is null");
123 return false;
124 }
125 {
126 std::lock_guard<std::mutex> autoLock(listenerSetMutex_);
127 if (listenerSet_.find(listener) == listenerSet_.end()) {
128 listenerSet_.insert(listener);
129 }
130 if (listenerSet_.size() > 1) {
131 return true;
132 }
133 }
134 auto registerTask = [this]() {
135 HILOGD("AddDeviceChangeListener register mission...");
136 int32_t retryTimes = 0;
137 int32_t errCode = ERR_OK;
138 while (retryTimes++ < RETRY_REGISTER_CALLBACK_TIMES) {
139 int32_t ret = DeviceManager::GetInstance().InitDeviceManager(PKG_NAME, initCallback_);
140 if (ret != ERR_OK) {
141 HILOGE("init device manager failed, ret:%{public}d", ret);
142 std::this_thread::sleep_for(std::chrono::milliseconds(RETRY_REGISTER_CALLBACK_DELAY_TIME));
143 continue;
144 }
145 errCode = DeviceManager::GetInstance().RegisterDevStateCallback(PKG_NAME, "", stateCallback_);
146 if (errCode != ERR_OK) {
147 HILOGD("AddDeviceChangeListener Reg errCode = %{public}d, retrying...", errCode);
148 errCode = DeviceManager::GetInstance().UnRegisterDevStateCallback(PKG_NAME);
149 HILOGD("AddDeviceChangeListener Unreg errCode = %{public}d", errCode);
150 std::this_thread::sleep_for(std::chrono::milliseconds(RETRY_REGISTER_CALLBACK_DELAY_TIME));
151 continue;
152 }
153 if (UpdateDeviceInfoStorage()) {
154 break;
155 }
156 }
157 HILOGI("AddDeviceChangeListener %{public}s", (errCode == ERR_OK) ? "success" : "timeout");
158 };
159 if (!dnetworkHandler_->PostTask(registerTask)) {
160 HILOGE("AddDeviceChangeListener post task failed");
161 return false;
162 }
163 return true;
164 }
165
UpdateDeviceInfoStorage()166 bool DnetworkAdapter::UpdateDeviceInfoStorage()
167 {
168 HILOGI("UpdateDeviceInfoStorage start.");
169 return DtbschedmgrDeviceInfoStorage::GetInstance().UpdateDeviceInfoStorage();
170 }
171
RemoveDeviceChangeListener(const std::shared_ptr<DeviceListener> & listener)172 void DnetworkAdapter::RemoveDeviceChangeListener(const std::shared_ptr<DeviceListener>& listener)
173 {
174 HILOGD("RemoveDeviceChangeListener called");
175 {
176 std::lock_guard<std::mutex> autoLock(listenerSetMutex_);
177 listenerSet_.erase(listener);
178 if (listenerSet_.size() > 0) {
179 return;
180 }
181 }
182
183 int32_t errCode = DeviceManager::GetInstance().UnRegisterDevStateCallback(PKG_NAME);
184 if (errCode != ERR_OK) {
185 HILOGE("RemoveDeviceChangeListener remove failed, errCode = %{public}d", errCode);
186 }
187 HILOGD("RemoveDeviceChangeListener remove ok");
188 }
189
GetLocalBasicInfo(DmDeviceInfo & dmDeviceInfo)190 bool DnetworkAdapter::GetLocalBasicInfo(DmDeviceInfo& dmDeviceInfo)
191 {
192 int32_t errCode = DeviceManager::GetInstance().GetLocalDeviceInfo(PKG_NAME, dmDeviceInfo);
193 if (errCode != ERR_OK) {
194 HILOGE("GetLocalBasicInfo errCode = %{public}d", errCode);
195 return false;
196 }
197 return true;
198 }
199
GetUdidByNetworkId(const std::string & networkId)200 std::string DnetworkAdapter::GetUdidByNetworkId(const std::string& networkId)
201 {
202 if (networkId.empty()) {
203 HILOGE("networkId is empty");
204 return "";
205 }
206 std::string udid = "";
207 int32_t errCode = DeviceManager::GetInstance().GetUdidByNetworkId(PKG_NAME, networkId, udid);
208 if (errCode != ERR_OK) {
209 HILOGE("GetUdidByNetworkId errCode = %{public}d", errCode);
210 return "";
211 }
212 return udid;
213 }
214
GetUuidByNetworkId(const std::string & networkId)215 std::string DnetworkAdapter::GetUuidByNetworkId(const std::string& networkId)
216 {
217 if (networkId.empty()) {
218 HILOGE("networkId is empty");
219 return "";
220 }
221 std::string uuid = "";
222 int32_t errCode = DeviceManager::GetInstance().GetUuidByNetworkId(PKG_NAME, networkId, uuid);
223 if (errCode != ERR_OK) {
224 HILOGE("GetUuidByNetworkId errCode = %{public}d", errCode);
225 return "";
226 }
227 return uuid;
228 }
229 } // namespace DistributedSchedule
230 } // namespace OHOS
231