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("AddDMListener 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("AddDMListener 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     SystemAbilityManager::GetInstance()->InitDbinderService();
197     {
198         lock_guard<mutex> autoLock(deviceOnlineLock_);
199         deviceOnlineSet_.emplace(deviceInfo.networkId);
200     }
201 
202     if (collect_ != nullptr) {
203         OnDemandEvent event = { DEVICE_ONLINE, SA_TAG_DEVICE_ON_LINE, "on" };
204         collect_->ReportEvent(event);
205     } else {
206         HILOGE("OnDeviceOnline collect_ isnull");
207     }
208 }
209 
OnDeviceOffline(const DmDeviceInfo & deviceInfo)210 void DeviceStateCallback::OnDeviceOffline(const DmDeviceInfo& deviceInfo)
211 {
212     HILOGI("DeviceNetworkingCollect OnDeviceOffline size %{public}zu", deviceOnlineSet_.size());
213     bool isOffline = false;
214     {
215         lock_guard<mutex> autoLock(deviceOnlineLock_);
216         deviceOnlineSet_.erase(deviceInfo.networkId);
217         isOffline = deviceOnlineSet_.empty();
218     }
219     if (isOffline) {
220         OnDemandEvent event = { DEVICE_ONLINE, SA_TAG_DEVICE_ON_LINE, "off" };
221         if (collect_ != nullptr) {
222             collect_->ReportEvent(event);
223         } else {
224             HILOGE("OnDeviceOffline collect_ isnull");
225         }
226     } else {
227         HILOGI("OnDeviceOffline not report size %{public}zu", deviceOnlineSet_.size());
228     }
229 }
230 
ClearDeviceOnlineSet()231 void DeviceStateCallback::ClearDeviceOnlineSet()
232 {
233     lock_guard<mutex> autoLock(deviceOnlineLock_);
234     deviceOnlineSet_.clear();
235 }
236 
IsOnline()237 bool DeviceStateCallback::IsOnline()
238 {
239     lock_guard<mutex> autoLock(deviceOnlineLock_);
240     return !deviceOnlineSet_.empty();
241 }
242 
UpdateDeviceOnlineSet(const std::string & deviceId)243 void DeviceStateCallback::UpdateDeviceOnlineSet(const std::string& deviceId)
244 {
245     lock_guard<mutex> autoLock(deviceOnlineLock_);
246     deviceOnlineSet_.emplace(deviceId);
247 }
248 
OnDeviceChanged(const DmDeviceInfo & deviceInfo)249 void DeviceStateCallback::OnDeviceChanged(const DmDeviceInfo& deviceInfo)
250 {
251     HILOGD("DeviceNetworkingCollect OnDeviceChanged called");
252 }
253 
OnDeviceReady(const DmDeviceInfo & deviceInfo)254 void DeviceStateCallback::OnDeviceReady(const DmDeviceInfo& deviceInfo)
255 {
256     HILOGI("DeviceNetworkingCollect DeviceStateCallback OnDeviceReady");
257     OnDemandEvent event = { DEVICE_ONLINE, SA_TAG_DEVICE_ON_LINE, "ready" };
258 
259     if (collect_ != nullptr) {
260         collect_->ReportEvent(event);
261     } else {
262         HILOGE("OnDeviceOnline collect_ isnull");
263     }
264 }
265 
CleanFfrt()266 void WorkHandler::CleanFfrt()
267 {
268     if (handler_ != nullptr) {
269         handler_->CleanFfrt();
270     }
271 }
272 
SetFfrt()273 void WorkHandler::SetFfrt()
274 {
275     if (handler_ != nullptr) {
276         handler_->SetFfrt("WorkHandler");
277     }
278 }
279 
ProcessEvent(uint32_t eventId)280 void WorkHandler::ProcessEvent(uint32_t eventId)
281 {
282     if (collect_ == nullptr) {
283         HILOGE("NetworkingCollect ProcessEvent collect or event is null!");
284         return;
285     }
286     if (eventId != INIT_EVENT && eventId != DM_DIED_EVENT) {
287         HILOGE("NetworkingCollect ProcessEvent error event code!");
288         return;
289     }
290     if (handler_ == nullptr) {
291         HILOGE("NetworkingCollect SendEvent handler is null!");
292         return;
293     }
294     if (!collect_->AddDeviceChangeListener()) {
295         HILOGW("AddDMListener retry");
296         auto task = [this] {this->ProcessEvent(INIT_EVENT);};
297         if (handler_ == nullptr) {
298             HILOGE("NetworkingCollect ProcessEvent handler is null!");
299             return;
300         }
301         handler_->PostTask(task, DELAY_TIME);
302     }
303 }
304 
SendEvent(uint32_t eventId)305 bool WorkHandler::SendEvent(uint32_t eventId)
306 {
307     if (handler_ == nullptr) {
308         HILOGE("NetworkingCollect SendEvent handler is null!");
309         return false;
310     }
311     auto task = [this, eventId] {this->ProcessEvent(eventId);};
312     return handler_->PostTask(task);
313 }
314 
SendEvent(uint32_t eventId,uint64_t delayTime)315 bool WorkHandler::SendEvent(uint32_t eventId, uint64_t delayTime)
316 {
317     if (handler_ == nullptr) {
318         HILOGE("NetworkingCollect SendEvent handler is null!");
319         return false;
320     }
321     auto task = [this, eventId] {this->ProcessEvent(eventId);};
322     return handler_->PostTask(task, delayTime);
323 }
324 }  // namespace OHOS
325