1 /*
2 * Copyright (c) 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 "device_networking_collect.h"
17
18 #include "sam_log.h"
19 #include "sa_profiles.h"
20 #include "system_ability_manager.h"
21
22 using namespace std;
23
24 using namespace OHOS::DistributedHardware;
25
26 namespace OHOS {
27 namespace {
28 const std::string PKG_NAME = "Samgr_Networking";
29 const std::string SA_TAG_DEVICE_ON_LINE = "deviceonline";
30 constexpr uint32_t INIT_EVENT = 10;
31 constexpr uint32_t DM_DIED_EVENT = 11;
32 constexpr uint64_t DELAY_TIME = 1000;
33 constexpr int32_t DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID = 4802;
34 }
DeviceNetworkingCollect(const sptr<IReport> & report)35 DeviceNetworkingCollect::DeviceNetworkingCollect(const sptr<IReport>& report)
36 : ICollectPlugin(report)
37 {
38 }
39
CleanFfrt()40 void DeviceNetworkingCollect::CleanFfrt()
41 {
42 if (workHandler_ != nullptr) {
43 workHandler_->CleanFfrt();
44 }
45 }
46
SetFfrt()47 void DeviceNetworkingCollect::SetFfrt()
48 {
49 if (workHandler_ != nullptr) {
50 workHandler_->SetFfrt();
51 }
52 }
53
OnStart()54 int32_t DeviceNetworkingCollect::OnStart()
55 {
56 HILOGI("DeviceNetworkingCollect OnStart called");
57 workHandler_ = std::make_shared<WorkHandler>(this);
58 initCallback_ = std::make_shared<DeviceInitCallBack>(workHandler_);
59 stateCallback_ = std::make_shared<DeviceStateCallback>(this);
60 workHandler_->SendEvent(INIT_EVENT);
61 return ERR_OK;
62 }
63
OnStop()64 int32_t DeviceNetworkingCollect::OnStop()
65 {
66 DeviceManager::GetInstance().UnRegisterDevStateCallback(PKG_NAME);
67 if (workHandler_ != nullptr) {
68 workHandler_ = nullptr;
69 }
70 initCallback_ = nullptr;
71 ClearDeviceOnlineSet();
72 stateCallback_ = nullptr;
73 return ERR_OK;
74 }
75
IsDmReady()76 bool DeviceNetworkingCollect::IsDmReady()
77 {
78 auto dmProxy = SystemAbilityManager::GetInstance()->CheckSystemAbility(
79 DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
80 if (dmProxy != nullptr) {
81 IPCObjectProxy* proxy = reinterpret_cast<IPCObjectProxy*>(dmProxy.GetRefPtr());
82 // make sure the proxy is not dead
83 if (proxy != nullptr && !proxy->IsObjectDead()) {
84 return true;
85 }
86 }
87 return false;
88 }
89
ReportMissedEvents()90 bool DeviceNetworkingCollect::ReportMissedEvents()
91 {
92 std::vector<DmDeviceInfo> devList;
93 int32_t ret = DeviceManager::GetInstance().GetTrustedDeviceList(PKG_NAME, "", devList);
94 if (ret != ERR_OK) {
95 HILOGE("DeviceNetworkingCollect GetTrustedDeviceList error");
96 return false;
97 }
98 bool isPreviousOnline = IsOnline();
99 if (isPreviousOnline) {
100 ClearDeviceOnlineSet();
101 if (devList.empty()) {
102 // send offline msg
103 OnDemandEvent event = { DEVICE_ONLINE, SA_TAG_DEVICE_ON_LINE, "off" };
104 ReportEvent(event);
105 } else {
106 // update the online set;
107 for (DmDeviceInfo& devInfo : devList) {
108 UpdateDeviceOnlineSet(devInfo.networkId);
109 }
110 }
111 } else {
112 // offline --> online
113 if (!devList.empty()) {
114 // update the online set;
115 for (DmDeviceInfo& devInfo : devList) {
116 UpdateDeviceOnlineSet(devInfo.networkId);
117 }
118 // send online msg
119 OnDemandEvent event = { DEVICE_ONLINE, SA_TAG_DEVICE_ON_LINE, "on" };
120 ReportEvent(event);
121 }
122 }
123 return true;
124 }
125
AddDeviceChangeListener()126 bool DeviceNetworkingCollect::AddDeviceChangeListener()
127 {
128 HILOGI("AddDeviceChangeListener called");
129 if (IsDmReady()) {
130 int32_t ret = DeviceManager::GetInstance().InitDeviceManager(PKG_NAME, initCallback_);
131 if (ret != ERR_OK) {
132 HILOGE("InitDeviceManager error");
133 return false;
134 }
135 if (!ReportMissedEvents()) {
136 HILOGE("ReportMissedEvents error");
137 return false;
138 }
139 ret = DeviceManager::GetInstance().RegisterDevStateCallback(PKG_NAME, "", stateCallback_);
140 if (ret != ERR_OK) {
141 DeviceManager::GetInstance().UnRegisterDevStateCallback(PKG_NAME);
142 HILOGE("RegisterDevStateCallback error");
143 return false;
144 }
145 HILOGI("AddDeviceChangeListener success");
146 return true;
147 }
148 return false;
149 }
150
UpdateDeviceOnlineSet(const std::string & deviceId)151 void DeviceNetworkingCollect::UpdateDeviceOnlineSet(const std::string& deviceId)
152 {
153 if (stateCallback_ != nullptr) {
154 stateCallback_->UpdateDeviceOnlineSet(deviceId);
155 }
156 }
157
ClearDeviceOnlineSet()158 void DeviceNetworkingCollect::ClearDeviceOnlineSet()
159 {
160 if (stateCallback_ != nullptr) {
161 stateCallback_->ClearDeviceOnlineSet();
162 }
163 }
164
CheckCondition(const OnDemandCondition & condition)165 bool DeviceNetworkingCollect::CheckCondition(const OnDemandCondition& condition)
166 {
167 bool isOnline = IsOnline();
168 if (condition.value == "on" && isOnline) {
169 return true;
170 }
171 if (condition.value == "off" && !isOnline) {
172 return true;
173 }
174 return false;
175 }
176
IsOnline()177 bool DeviceNetworkingCollect::IsOnline()
178 {
179 if (stateCallback_ != nullptr) {
180 return stateCallback_->IsOnline();
181 }
182 return false;
183 }
184
OnRemoteDied()185 void DeviceInitCallBack::OnRemoteDied()
186 {
187 HILOGI("DeviceNetworkingCollect DeviceInitCallBack OnRemoteDied");
188 if (handler_ != nullptr) {
189 handler_->SendEvent(DM_DIED_EVENT, DELAY_TIME);
190 }
191 }
192
OnDeviceOnline(const DmDeviceInfo & deviceInfo)193 void DeviceStateCallback::OnDeviceOnline(const DmDeviceInfo& deviceInfo)
194 {
195 HILOGI("DeviceNetworkingCollect OnDeviceOnline size %{public}zu", deviceOnlineSet_.size());
196 {
197 lock_guard<mutex> autoLock(deviceOnlineLock_);
198 deviceOnlineSet_.emplace(deviceInfo.networkId);
199 }
200
201 if (collect_ != nullptr) {
202 OnDemandEvent event = { DEVICE_ONLINE, SA_TAG_DEVICE_ON_LINE, "on" };
203 collect_->ReportEvent(event);
204 } else {
205 HILOGE("OnDeviceOnline collect_ isnull");
206 }
207 }
208
OnDeviceOffline(const DmDeviceInfo & deviceInfo)209 void DeviceStateCallback::OnDeviceOffline(const DmDeviceInfo& deviceInfo)
210 {
211 HILOGI("DeviceNetworkingCollect OnDeviceOffline size %{public}zu", deviceOnlineSet_.size());
212 bool isOffline = false;
213 {
214 lock_guard<mutex> autoLock(deviceOnlineLock_);
215 deviceOnlineSet_.erase(deviceInfo.networkId);
216 isOffline = deviceOnlineSet_.empty();
217 }
218 if (isOffline) {
219 OnDemandEvent event = { DEVICE_ONLINE, SA_TAG_DEVICE_ON_LINE, "off" };
220 if (collect_ != nullptr) {
221 collect_->ReportEvent(event);
222 } else {
223 HILOGE("OnDeviceOffline collect_ isnull");
224 }
225 } else {
226 HILOGI("OnDeviceOffline not report size %{public}zu", deviceOnlineSet_.size());
227 }
228 }
229
ClearDeviceOnlineSet()230 void DeviceStateCallback::ClearDeviceOnlineSet()
231 {
232 lock_guard<mutex> autoLock(deviceOnlineLock_);
233 deviceOnlineSet_.clear();
234 }
235
IsOnline()236 bool DeviceStateCallback::IsOnline()
237 {
238 lock_guard<mutex> autoLock(deviceOnlineLock_);
239 return !deviceOnlineSet_.empty();
240 }
241
UpdateDeviceOnlineSet(const std::string & deviceId)242 void DeviceStateCallback::UpdateDeviceOnlineSet(const std::string& deviceId)
243 {
244 lock_guard<mutex> autoLock(deviceOnlineLock_);
245 deviceOnlineSet_.emplace(deviceId);
246 }
247
OnDeviceChanged(const DmDeviceInfo & deviceInfo)248 void DeviceStateCallback::OnDeviceChanged(const DmDeviceInfo& deviceInfo)
249 {
250 HILOGD("DeviceNetworkingCollect OnDeviceChanged called");
251 }
252
OnDeviceReady(const DmDeviceInfo & deviceInfo)253 void DeviceStateCallback::OnDeviceReady(const DmDeviceInfo& deviceInfo)
254 {
255 HILOGI("DeviceNetworkingCollect DeviceStateCallback OnDeviceReady");
256 OnDemandEvent event = { DEVICE_ONLINE, SA_TAG_DEVICE_ON_LINE, "ready" };
257
258 if (collect_ != nullptr) {
259 collect_->ReportEvent(event);
260 } else {
261 HILOGE("OnDeviceOnline collect_ isnull");
262 }
263 }
264
CleanFfrt()265 void WorkHandler::CleanFfrt()
266 {
267 if (handler_ != nullptr) {
268 handler_->CleanFfrt();
269 }
270 }
271
SetFfrt()272 void WorkHandler::SetFfrt()
273 {
274 if (handler_ != nullptr) {
275 handler_->SetFfrt("WorkHandler");
276 }
277 }
278
ProcessEvent(uint32_t eventId)279 void WorkHandler::ProcessEvent(uint32_t eventId)
280 {
281 if (collect_ == nullptr) {
282 HILOGE("NetworkingCollect ProcessEvent collect or event is null!");
283 return;
284 }
285 if (eventId != INIT_EVENT && eventId != DM_DIED_EVENT) {
286 HILOGE("NetworkingCollect ProcessEvent error event code!");
287 return;
288 }
289 if (handler_ == nullptr) {
290 HILOGE("NetworkingCollect SendEvent handler is null!");
291 return;
292 }
293 if (!collect_->AddDeviceChangeListener()) {
294 HILOGW("AddDeviceChangeListener retry");
295 auto task = [this] {this->ProcessEvent(INIT_EVENT);};
296 if (handler_ == nullptr) {
297 HILOGE("NetworkingCollect ProcessEvent handler is null!");
298 return;
299 }
300 handler_->PostTask(task, DELAY_TIME);
301 }
302 }
303
SendEvent(uint32_t eventId)304 bool WorkHandler::SendEvent(uint32_t eventId)
305 {
306 if (handler_ == nullptr) {
307 HILOGE("NetworkingCollect SendEvent handler is null!");
308 return false;
309 }
310 auto task = [this, eventId] {this->ProcessEvent(eventId);};
311 return handler_->PostTask(task);
312 }
313
SendEvent(uint32_t eventId,uint64_t delayTime)314 bool WorkHandler::SendEvent(uint32_t eventId, uint64_t delayTime)
315 {
316 if (handler_ == nullptr) {
317 HILOGE("NetworkingCollect SendEvent handler is null!");
318 return false;
319 }
320 auto task = [this, eventId] {this->ProcessEvent(eventId);};
321 return handler_->PostTask(task, delayTime);
322 }
323 } // namespace OHOS
324