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