1 /*
2  * Copyright (c) 2023 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_WIFI_EVENT_SUBSCRIBER_MANAGER_H
17 #define OHOS_WIFI_EVENT_SUBSCRIBER_MANAGER_H
18 
19 #ifndef OHOS_ARCH_LITE
20 #include <mutex>
21 #include <functional>
22 #include "wifi_errcode.h"
23 #include "wifi_internal_msg.h"
24 #include "wifi_system_ability_listerner.h"
25 #include "common_event_manager.h"
26 #include "wifi_event_handler.h"
27 
28 namespace OHOS {
29 namespace Wifi {
30 #ifdef HAS_POWERMGR_PART
31 const std::string COMMON_EVENT_POWER_MANAGER_STATE_CHANGED = "usual.event.POWER_MANAGER_STATE_CHANGED";
32 #endif
33 const int CAST_ENGINE_SA_ID = 65546;
34 const int SHARE_SERVICE_ID = 2902;
35 const int MOUSE_CROSS_SERVICE_ID = 65569;
36 #ifdef SUPPORT_ClOUD_WIFI_ASSET
37 inline const std::string COMMON_EVENT_ASSETCLOUD_MANAGER_STATE_CHANGED = "usual.event.ASSET_SYNC_DATA_CHANGED_SA";
38 const int ASSETID = 6226;
39 #endif
40 class CesEventSubscriber : public OHOS::EventFwk::CommonEventSubscriber {
41 public:
42     explicit CesEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo);
43     virtual ~CesEventSubscriber();
44     void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &eventData) override;
45     void OnReceiveStandbyEvent(const OHOS::EventFwk::CommonEventData &eventData);
46     void OnReceiveScreenEvent(const OHOS::EventFwk::CommonEventData &eventData);
47     void OnReceiveAirplaneEvent(const OHOS::EventFwk::CommonEventData &eventData);
48     void OnReceiveBatteryEvent(const OHOS::EventFwk::CommonEventData &eventData);
49     void OnReceiveAppEvent(const OHOS::EventFwk::CommonEventData &eventData);
50     void OnReceiveThermalEvent(const OHOS::EventFwk::CommonEventData &eventData);
51     void OnReceiveNotificationEvent(const OHOS::EventFwk::CommonEventData &eventData);
52     void OnReceiveUserUnlockedEvent(const OHOS::EventFwk::CommonEventData &eventData);
53     void OnReceiveDataShareReadyEvent(const OHOS::EventFwk::CommonEventData &eventData);
54 private:
55     bool lastSleepState = false;
56 };
57 
58 class NotificationEventSubscriber : public OHOS::EventFwk::CommonEventSubscriber {
59 public:
60     explicit NotificationEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo);
61     virtual ~NotificationEventSubscriber();
62     void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &eventData) override;
63     void OnReceiveWlanKeepConnected(const OHOS::EventFwk::CommonEventData &eventData);
64 };
65 
66 #ifdef HAS_POWERMGR_PART
67 class PowermgrEventSubscriber : public OHOS::EventFwk::CommonEventSubscriber {
68 public:
69     explicit PowermgrEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo);
70     virtual ~PowermgrEventSubscriber();
71     void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &eventData) override;
72 };
73 #endif
74 #ifdef SUPPORT_ClOUD_WIFI_ASSET
75 class AssetEventSubscriber : public OHOS::EventFwk::CommonEventSubscriber {
76 public:
77     explicit AssetEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo);
78     virtual ~AssetEventSubscriber();
79     void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &eventData) override;
80 };
81 #endif
82 
83 class NetworkStateChangeSubscriber : public OHOS::EventFwk::CommonEventSubscriber {
84 public:
85     explicit NetworkStateChangeSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo);
86     ~NetworkStateChangeSubscriber() = default;
87     void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &eventData) override;
88 };
89 
90 class WifiScanEventChangeSubscriber : public OHOS::EventFwk::CommonEventSubscriber {
91 public:
92     explicit WifiScanEventChangeSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo);
93     ~WifiScanEventChangeSubscriber() = default;
94     void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &eventData) override;
95 };
96 
97 class SettingsEnterSubscriber : public OHOS::EventFwk::CommonEventSubscriber {
98 public:
99     explicit SettingsEnterSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo);
100     ~SettingsEnterSubscriber() = default;
101     void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &eventData) override;
102 };
103 
104 class WifiEventSubscriberManager : public WifiSystemAbilityListener {
105 public:
106     WifiEventSubscriberManager();
107     virtual ~WifiEventSubscriberManager();
108 
109     void OnSystemAbilityChanged(int systemAbilityId, bool add) override;
110     void GetAirplaneModeByDatashare();
111     void GetWifiAllowSemiActiveByDatashare();
112     bool GetLocationModeByDatashare();
113     void DealLocationModeChangeEvent();
114     void CheckAndStartStaByDatashare();
115     bool IsMdmForbidden(void);
116     void AccessDataShare();
117     void RegisterLocationEvent();
118 
119 private:
120     void InitSubscribeListener();
121     void HandleAppMgrServiceChange(bool add);
122     void HandleCommNetConnManagerSysChange(int systemAbilityId, bool add);
123     void HandleCommonEventServiceChange(int systemAbilityId, bool add);
124 #ifdef HAS_MOVEMENT_PART
125     void HandleHasMovementPartChange(int systemAbilityId, bool add);
126 #endif
127     void HandleDistributedKvDataServiceChange(bool add);
128     void HandleCastServiceChange(bool add);
129     void HandleShareServiceChange(bool add);
130     void HandleMouseCrossServiceChange(bool add);
131     int GetLastStaStateByDatashare();
132     void RegisterCesEvent();
133 #ifdef HAS_POWERMGR_PART
134     void RegisterPowermgrEvent();
135     void UnRegisterPowermgrEvent();
136     std::shared_ptr<PowermgrEventSubscriber> wifiPowermgrEventSubsciber_ = nullptr;
137     std::mutex powermgrEventMutex;
138 #endif
139     void UnRegisterCesEvent();
140     void UnRegisterLocationEvent();
141     void RegisterNotificationEvent();
142     void UnRegisterNotificationEvent();
143     void GetMdmProp();
144     void RegisterMdmPropListener();
145     static void MdmPropChangeEvt(const char *key, const char *value, void *context);
146 #ifdef HAS_MOVEMENT_PART
147     void RegisterMovementCallBack();
148     void UnRegisterMovementCallBack();
149 #endif
150 #ifdef FEATURE_P2P_SUPPORT
151     void HandleP2pBusinessChange(int systemAbilityId, bool add);
152 #endif
153 #ifdef SUPPORT_ClOUD_WIFI_ASSET
154     void RegisterAssetEvent();
155     void UnRegisterAssetEvent();
156 #endif
157     void RegisterNetworkStateChangeEvent();
158     void UnRegisterNetworkStateChangeEvent();
159     void RegisterWifiScanChangeEvent();
160     void UnRegisterWifiScanChangeEvent();
161     void RegisterSettingsEnterEvent();
162     void UnRegisterSettingsEnterEvent();
163 
164 private:
165     uint32_t cesTimerId{0};
166     uint32_t notificationTimerId{0};
167     uint32_t networkStateChangeTimerId{0};
168     uint32_t wifiScanChangeTimerId{0};
169     uint32_t settingsTimerId{0};
170     std::mutex cesEventMutex;
171     std::mutex notificationEventMutex;
172     std::mutex networkStateChangeEventMutex;
173     std::mutex wifiScanChangeEventMutex;
174     std::mutex settingsEnterEventMutex;
175     bool isCesEventSubscribered = false;
176     std::shared_ptr<CesEventSubscriber> cesEventSubscriber_ = nullptr;
177     std::shared_ptr<NotificationEventSubscriber> wifiNotificationSubsciber_ = nullptr;
178     std::shared_ptr<NetworkStateChangeSubscriber> networkStateChangeSubsciber_ = nullptr;
179     std::shared_ptr<WifiScanEventChangeSubscriber> wifiScanEventChangeSubscriber_ = nullptr;
180     std::shared_ptr<SettingsEnterSubscriber> settingsEnterSubscriber_ = nullptr;
181 #ifdef HAS_MOVEMENT_PART
182     std::mutex deviceMovementEventMutex;
183 #endif
184     static bool mIsMdmForbidden;
185     bool islocationModeObservered = false;
186     std::mutex locationEventMutex;
187     std::unique_ptr<WifiEventHandler> mWifiEventSubsThread = nullptr;
188 #ifdef SUPPORT_ClOUD_WIFI_ASSET
189     std::shared_ptr<AssetEventSubscriber> wifiAssetrEventSubsciber_ = nullptr;
190     std::mutex AssetEventMutex;
191     uint32_t assetMgrId{0};
192 #endif
193 
194     bool accessDataShare_ = false;
195     std::mutex accessDataShareMutex_;
196 };
197 
198 }  // namespace Wifi
199 }  // namespace OHOS
200 #endif
201 #endif // OHOS_WIFI_EVENT_SUBSCRIBER_MANAGER_H
202