• 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 #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 <set>
21 
22 #include "device_manager.h"
23 #include "ffrt_handler.h"
24 
25 namespace OHOS {
26 class DeviceStateCallback : public DistributedHardware::DeviceStateCallback {
27     public:
DeviceStateCallback(const sptr<ICollectPlugin> & collect)28         explicit DeviceStateCallback(const sptr<ICollectPlugin>& collect) : collect_(collect) {}
29         ~DeviceStateCallback() = default;
30         void OnDeviceOnline(const DistributedHardware::DmDeviceInfo& deviceInfo) override;
31         void OnDeviceOffline(const DistributedHardware::DmDeviceInfo& deviceInfo) override;
32         void OnDeviceChanged(const DistributedHardware::DmDeviceInfo& deviceInfo) override;
33         void OnDeviceReady(const DistributedHardware::DmDeviceInfo& deviceInfo) override;
34         void ClearDeviceOnlineSet();
35         bool IsOnline();
36         void UpdateDeviceOnlineSet(const std::string& deviceId);
37     private:
38        sptr<ICollectPlugin> collect_;
39        std::set<std::string> deviceOnlineSet_;
40        samgr::mutex deviceOnlineLock_;
41 };
42 
43 class WorkHandler;
44 class DeviceInitCallBack : public DistributedHardware::DmInitCallback {
45     public:
DeviceInitCallBack(const std::shared_ptr<WorkHandler> & handler)46         explicit DeviceInitCallBack(const std::shared_ptr<WorkHandler>& handler) : handler_(handler) {}
47         ~DeviceInitCallBack() = default;
48         void OnRemoteDied() override;
49     private:
50         std::shared_ptr<WorkHandler> handler_;
51 };
52 
53 class DeviceNetworkingCollect : public ICollectPlugin {
54 public:
55     explicit DeviceNetworkingCollect(const sptr<IReport>& report);
56     ~DeviceNetworkingCollect() = default;
57 
58     void CleanFfrt() override;
59     void SetFfrt() override;
60     int32_t OnStart() override;
61     int32_t OnStop() override;
62     bool CheckCondition(const OnDemandCondition& condition) override;
63     bool AddDeviceChangeListener();
64 private:
65     std::shared_ptr<DeviceStateCallback> stateCallback_;
66     std::shared_ptr<DistributedHardware::DmInitCallback> initCallback_;
67     std::shared_ptr<WorkHandler> workHandler_;
68 
69     bool IsDmReady();
70     void ClearDeviceOnlineSet();
71     bool IsOnline();
72     void UpdateDeviceOnlineSet(const std::string& deviceId);
73     bool ReportMissedEvents();
74 };
75 
76 class WorkHandler : public std::enable_shared_from_this<WorkHandler> {
77     public:
WorkHandler(const sptr<DeviceNetworkingCollect> & collect)78         WorkHandler(const sptr<DeviceNetworkingCollect>& collect) : collect_(collect)
79         {
80             handler_ = std::make_shared<FFRTHandler>("WorkHandler");
81         }
82         ~WorkHandler() = default;
83         void ProcessEvent(uint32_t eventId);
84         bool SendEvent(uint32_t eventId);
85         bool SendEvent(uint32_t eventId, uint64_t delayTime);
86         void CleanFfrt();
87         void SetFfrt();
88 
89     private:
90         sptr<DeviceNetworkingCollect> collect_;
91         std::shared_ptr<FFRTHandler> handler_;
92 };
93 } // namespace OHOS
94 #endif // OHOS_SYSTEM_ABILITY_MANAGER_DEVICE_NETWORKING_COLLECT_H