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 DmsKvSyncE2E::GetInstance()->ClearSyncRecord(deviceInfo.networkId);
76
77 auto onlineNotifyTask = [deviceInfo]() {
78 std::lock_guard<std::mutex> autoLock(listenerSetMutex_);
79 for (auto& listener : listenerSet_) {
80 if (listener != nullptr) {
81 listener->OnDeviceOnline(deviceInfo);
82 }
83 }
84 };
85 if (dnetworkHandler_ == nullptr || !dnetworkHandler_->PostTask(onlineNotifyTask)) {
86 HILOGE("OnNodeOnline post task failed");
87 return;
88 }
89 }
90
OnDeviceOffline(const DmDeviceInfo & deviceInfo)91 void DnetworkAdapter::DmsDeviceStateCallback::OnDeviceOffline(const DmDeviceInfo& deviceInfo)
92 {
93 HILOGI("OnNodeOffline networkId: %{public}s.", GetAnonymStr(deviceInfo.networkId).c_str());
94 DmsKvSyncE2E::GetInstance()->ClearSyncRecord(deviceInfo.networkId);
95 auto offlineNotifyTask = [deviceInfo]() {
96 std::lock_guard<std::mutex> autoLock(listenerSetMutex_);
97 for (auto& listener : listenerSet_) {
98 if (listener != nullptr) {
99 listener->OnDeviceOffline(deviceInfo);
100 }
101 }
102 };
103 if (dnetworkHandler_ == nullptr || !dnetworkHandler_->PostTask(offlineNotifyTask)) {
104 HILOGE("OnNodeOffline post task failed");
105 return;
106 }
107 }
108
OnDeviceChanged(const DmDeviceInfo & deviceInfo)109 void DnetworkAdapter::DmsDeviceStateCallback::OnDeviceChanged(const DmDeviceInfo& deviceInfo)
110 {
111 std::string networkId = deviceInfo.networkId;
112 HILOGI("OnDeviceChanged networkId: %{public}s.", GetAnonymStr(networkId).c_str());
113 }
114
OnDeviceReady(const DmDeviceInfo & deviceInfo)115 void DnetworkAdapter::DmsDeviceStateCallback::OnDeviceReady(const DmDeviceInfo& deviceInfo)
116 {
117 HILOGI("called");
118 }
119
AddDeviceChangeListener(const std::shared_ptr<DeviceListener> & listener)120 bool DnetworkAdapter::AddDeviceChangeListener(const std::shared_ptr<DeviceListener>& listener)
121 {
122 HILOGD("AddDeviceChangeListener called");
123 if (dnetworkHandler_ == nullptr) {
124 HILOGE("handler is null");
125 return false;
126 }
127 {
128 std::lock_guard<std::mutex> autoLock(listenerSetMutex_);
129 if (listenerSet_.find(listener) == listenerSet_.end()) {
130 listenerSet_.insert(listener);
131 }
132 if (listenerSet_.size() > 1) {
133 return true;
134 }
135 }
136 auto registerTask = [this]() {
137 HILOGD("AddDeviceChangeListener register mission...");
138 int32_t retryTimes = 0;
139 int32_t errCode = ERR_OK;
140 while (retryTimes++ < RETRY_REGISTER_CALLBACK_TIMES) {
141 int32_t ret = DeviceManager::GetInstance().InitDeviceManager(PKG_NAME, initCallback_);
142 if (ret != ERR_OK) {
143 HILOGE("init device manager failed, ret:%{public}d", ret);
144 std::this_thread::sleep_for(std::chrono::milliseconds(RETRY_REGISTER_CALLBACK_DELAY_TIME));
145 continue;
146 }
147 errCode = DeviceManager::GetInstance().RegisterDevStateCallback(PKG_NAME, "", stateCallback_);
148 if (errCode != ERR_OK) {
149 HILOGD("AddDeviceChangeListener Reg errCode = %{public}d, retrying...", errCode);
150 errCode = DeviceManager::GetInstance().UnRegisterDevStateCallback(PKG_NAME);
151 HILOGD("AddDeviceChangeListener Unreg errCode = %{public}d", errCode);
152 std::this_thread::sleep_for(std::chrono::milliseconds(RETRY_REGISTER_CALLBACK_DELAY_TIME));
153 continue;
154 }
155 if (UpdateDeviceInfoStorage()) {
156 break;
157 }
158 }
159 HILOGI("AddDeviceChangeListener %{public}s", (errCode == ERR_OK) ? "success" : "timeout");
160 };
161 if (!dnetworkHandler_->PostTask(registerTask)) {
162 HILOGE("AddDeviceChangeListener post task failed");
163 return false;
164 }
165 return true;
166 }
167
UpdateDeviceInfoStorage()168 bool DnetworkAdapter::UpdateDeviceInfoStorage()
169 {
170 HILOGI("UpdateDeviceInfoStorage start.");
171 return DtbschedmgrDeviceInfoStorage::GetInstance().UpdateDeviceInfoStorage();
172 }
173
RemoveDeviceChangeListener(const std::shared_ptr<DeviceListener> & listener)174 void DnetworkAdapter::RemoveDeviceChangeListener(const std::shared_ptr<DeviceListener>& listener)
175 {
176 HILOGD("RemoveDeviceChangeListener called");
177 {
178 std::lock_guard<std::mutex> autoLock(listenerSetMutex_);
179 listenerSet_.erase(listener);
180 if (listenerSet_.size() > 0) {
181 return;
182 }
183 }
184
185 int32_t errCode = DeviceManager::GetInstance().UnRegisterDevStateCallback(PKG_NAME);
186 if (errCode != ERR_OK) {
187 HILOGE("RemoveDeviceChangeListener remove failed, errCode = %{public}d", errCode);
188 }
189 HILOGD("RemoveDeviceChangeListener remove ok");
190 }
191
GetLocalBasicInfo(DmDeviceInfo & dmDeviceInfo)192 bool DnetworkAdapter::GetLocalBasicInfo(DmDeviceInfo& dmDeviceInfo)
193 {
194 int32_t errCode = DeviceManager::GetInstance().GetLocalDeviceInfo(PKG_NAME, dmDeviceInfo);
195 if (errCode != ERR_OK) {
196 HILOGE("GetLocalBasicInfo errCode = %{public}d", errCode);
197 return false;
198 }
199 return true;
200 }
201
GetUdidByNetworkId(const std::string & networkId)202 std::string DnetworkAdapter::GetUdidByNetworkId(const std::string& networkId)
203 {
204 if (networkId.empty()) {
205 HILOGE("networkId is empty");
206 return "";
207 }
208 std::string udid = "";
209 int32_t errCode = DeviceManager::GetInstance().GetUdidByNetworkId(PKG_NAME, networkId, udid);
210 if (errCode != ERR_OK) {
211 HILOGE("GetUdidByNetworkId errCode = %{public}d", errCode);
212 return "";
213 }
214 return udid;
215 }
216
GetUuidByNetworkId(const std::string & networkId)217 std::string DnetworkAdapter::GetUuidByNetworkId(const std::string& networkId)
218 {
219 if (networkId.empty()) {
220 HILOGE("networkId is empty");
221 return "";
222 }
223 std::string uuid = "";
224 int32_t errCode = DeviceManager::GetInstance().GetUuidByNetworkId(PKG_NAME, networkId, uuid);
225 if (errCode != ERR_OK) {
226 HILOGE("GetUuidByNetworkId errCode = %{public}d", errCode);
227 return "";
228 }
229 return uuid;
230 }
231 } // namespace DistributedSchedule
232 } // namespace OHOS
233