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 #ifndef OHOS_SYSTEM_ABILITY_MANAGER_DEVICE_NETWORKING_COLLECT_H 17 #define OHOS_SYSTEM_ABILITY_MANAGER_DEVICE_NETWORKING_COLLECT_H 18 #include "icollect_plugin.h" 19 20 #include <mutex> 21 #include <set> 22 23 #include "device_manager.h" 24 #include "event_handler.h" 25 26 namespace OHOS { 27 class DeviceStateCallback : public DistributedHardware::DeviceStateCallback { 28 public: DeviceStateCallback(const sptr<ICollectPlugin> & collect)29 explicit DeviceStateCallback(const sptr<ICollectPlugin>& collect) : collect_(collect) {} 30 ~DeviceStateCallback() = default; 31 void OnDeviceOnline(const DistributedHardware::DmDeviceInfo& deviceInfo) override; 32 void OnDeviceOffline(const DistributedHardware::DmDeviceInfo& deviceInfo) override; 33 void OnDeviceChanged(const DistributedHardware::DmDeviceInfo& deviceInfo) override; 34 void OnDeviceReady(const DistributedHardware::DmDeviceInfo& deviceInfo) override; 35 void ClearDeviceOnlineSet(); 36 bool IsOnline(); 37 void UpdateDeviceOnlineSet(const std::string& deviceId); 38 private: 39 sptr<ICollectPlugin> collect_; 40 std::set<std::string> deviceOnlineSet_; 41 bool isExistDeviceReady_ = false; 42 std::mutex deviceOnlineLock_; 43 }; 44 45 class DeviceInitCallBack : public DistributedHardware::DmInitCallback { 46 public: DeviceInitCallBack(const std::shared_ptr<AppExecFwk::EventHandler> & handler)47 explicit DeviceInitCallBack(const std::shared_ptr<AppExecFwk::EventHandler>& handler) : handler_(handler) {} 48 ~DeviceInitCallBack() = default; 49 void OnRemoteDied() override; 50 private: 51 std::shared_ptr<AppExecFwk::EventHandler> handler_; 52 }; 53 54 class DeviceNetworkingCollect : public ICollectPlugin { 55 public: 56 explicit DeviceNetworkingCollect(const sptr<IReport>& report); 57 ~DeviceNetworkingCollect() = default; 58 59 int32_t OnStart() override; 60 int32_t OnStop() override; 61 bool CheckCondition(const OnDemandCondition& condition) override; 62 bool AddDeviceChangeListener(); 63 private: 64 std::shared_ptr<DeviceStateCallback> stateCallback_; 65 std::shared_ptr<DistributedHardware::DmInitCallback> initCallback_; 66 std::shared_ptr<AppExecFwk::EventHandler> workHandler_; 67 68 bool IsDmReady(); 69 void ClearDeviceOnlineSet(); 70 bool IsOnline(); 71 void UpdateDeviceOnlineSet(const std::string& deviceId); 72 bool ReportMissedEvents(); 73 }; 74 75 class WorkHandler : public AppExecFwk::EventHandler { 76 public: WorkHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner,const sptr<DeviceNetworkingCollect> & collect)77 WorkHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner, 78 const sptr<DeviceNetworkingCollect>& collect) : AppExecFwk::EventHandler(runner), collect_(collect) {} 79 ~WorkHandler() = default; 80 void ProcessEvent(const OHOS::AppExecFwk::InnerEvent::Pointer& event) override; 81 82 private: 83 sptr<DeviceNetworkingCollect> collect_; 84 }; 85 } // namespace OHOS 86 #endif // OHOS_SYSTEM_ABILITY_MANAGER_DEVICE_NETWORKING_COLLECT_H