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 "ffrt_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 std::mutex deviceOnlineLock_; 42 }; 43 44 class WorkHandler; 45 class DeviceInitCallBack : public DistributedHardware::DmInitCallback { 46 public: DeviceInitCallBack(const std::shared_ptr<WorkHandler> & handler)47 explicit DeviceInitCallBack(const std::shared_ptr<WorkHandler>& handler) : handler_(handler) {} 48 ~DeviceInitCallBack() = default; 49 void OnRemoteDied() override; 50 private: 51 std::shared_ptr<WorkHandler> handler_; 52 }; 53 54 class DeviceNetworkingCollect : public ICollectPlugin { 55 public: 56 explicit DeviceNetworkingCollect(const sptr<IReport>& report); 57 ~DeviceNetworkingCollect() = default; 58 59 void CleanFfrt() override; 60 void SetFfrt() override; 61 int32_t OnStart() override; 62 int32_t OnStop() override; 63 bool CheckCondition(const OnDemandCondition& condition) override; 64 bool AddDeviceChangeListener(); 65 private: 66 std::shared_ptr<DeviceStateCallback> stateCallback_; 67 std::shared_ptr<DistributedHardware::DmInitCallback> initCallback_; 68 std::shared_ptr<WorkHandler> workHandler_; 69 70 bool IsDmReady(); 71 void ClearDeviceOnlineSet(); 72 bool IsOnline(); 73 void UpdateDeviceOnlineSet(const std::string& deviceId); 74 bool ReportMissedEvents(); 75 }; 76 77 class WorkHandler { 78 public: WorkHandler(const sptr<DeviceNetworkingCollect> & collect)79 WorkHandler(const sptr<DeviceNetworkingCollect>& collect) : collect_(collect) 80 { 81 handler_ = std::make_shared<FFRTHandler>("WorkHandler"); 82 } 83 ~WorkHandler() = default; 84 void ProcessEvent(uint32_t eventId); 85 bool SendEvent(uint32_t eventId); 86 bool SendEvent(uint32_t eventId, uint64_t delayTime); 87 void CleanFfrt(); 88 void SetFfrt(); 89 90 private: 91 sptr<DeviceNetworkingCollect> collect_; 92 std::shared_ptr<FFRTHandler> handler_; 93 }; 94 } // namespace OHOS 95 #endif // OHOS_SYSTEM_ABILITY_MANAGER_DEVICE_NETWORKING_COLLECT_H