• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-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_WIFI_PROTECT_MANAGER_H
17 #define OHOS_WIFI_PROTECT_MANAGER_H
18 
19 #include <vector>
20 #include "wifi_protect.h"
21 #ifndef OHOS_ARCH_LITE
22 #include "appmgr/application_state_observer_stub.h"
23 #include "appmgr/app_state_data.h"
24 #include "ipc_skeleton.h"
25 #include "appmgr/app_mgr_interface.h"
26 #include "wifi_event_handler.h"
27 #endif
28 
29 namespace OHOS {
30 namespace Wifi {
31 #ifndef OHOS_ARCH_LITE
32 class AppStateObserver;
33 #endif
34 class WifiProtectManager {
35 public:
36     ~WifiProtectManager();
37     static WifiProtectManager &GetInstance();
38 
39     /**
40      * @Description Validate that the protect mode is valid
41      *
42      * @param protectMode - The protect mode to verify
43      * @return true - valid
44      * @return false - invalid
45      */
46     static bool IsValidProtectMode(const WifiProtectMode &protectMode);
47 
48     /**
49      * @Description Get the nearly protect type currently held by the WifiProtectManager
50      *
51      * @return WifiProtectMode - currently held protect
52      */
53     WifiProtectMode GetNearlyProtectMode();
54 
55     /**
56      * @Description Create a Wifi Protect.
57      *
58      * @param protectMode - representation of the Wifi Protect type
59      * @param protectName - represent the protect name
60      * @return true - create the protect success
61      * @return false - create protect failed
62      */
63     bool InitWifiProtect(const WifiProtectType &protectType, const std::string &protectName);
64 
65     /**
66      * @Description Allowing a calling app to acquire a Wifi Protect in the supplied mode
67      *
68      * @param protectMode - representation of the Wifi Protect type
69      * @param name - represent the protect
70      * @return true - acquired the protect success
71      * @return false - acquired protect failed
72      */
73     bool GetWifiProtect(const WifiProtectMode &protectMode, const std::string name);
74 
75     /**
76      * @Description Applications to release a WiFi Wake protect
77      *
78      * @param name - represent the protect
79      * @return true - put protect success
80      * @return false - put failed, the caller did not hold this protect
81      */
82     bool PutWifiProtect(const std::string &name);
83 
84     /**
85      * @Description Applications whether or not hold WiFi protect
86      *
87      * @param name - represent the protect
88      * @return true - app has held wifi protect
89      * @return false - app has not held wifi protect
90      */
91     bool IsHeldWifiProtect(const std::string &name);
92     /**
93      * @Description Set hi-perf mode protect state
94      *
95      * @param isEnabled - True to force hi-perf mode, false to leave it up to acquired wifiProtects
96      * @return true - success
97      * @return false - failed
98      */
99     bool ChangeToPerfMode(bool isEnabled);
100 
101     /**
102      * @Description Handler for screen state (on/off) changes
103      *
104      * @param screenOn - screen on/off state
105      */
106     void HandleScreenStateChanged(bool screenOn);
107 
108     /**
109      * @Description Handler for Wifi Client mode state changes
110      *
111      * @param isConnected - wifi client connect state
112      */
113     void UpdateWifiClientConnected(bool isConnected);
114 
115     /**
116      * @Description set low latency mode
117      *
118      * @param enabled - true: enable low latency, false: disable low latency
119      * @return bool - operation result
120      */
121     bool SetLowLatencyMode(bool enabled);
122     bool ChangeWifiPowerMode();
123 #ifndef OHOS_ARCH_LITE
124     void RegisterAppStateObserver();
125     void OnAppDied(const std::string bundlename);
126     void OnAppForegroudChanged(const std::string &bundleName, int state);
127 #endif
128 private:
129     WifiProtectManager();
130     bool AddProtect(const WifiProtectMode &protectMode, const std::string &name);
131     bool ReleaseProtect(const std::string &name);
132     std::shared_ptr<WifiProtect> RemoveProtect(const std::string &name);
133 #ifndef OHOS_ARCH_LITE
134     int GetFgLowlatyProtectCount();
135 #endif
136 private:
137     std::vector<std::shared_ptr<WifiProtect>> mWifiProtects;
138     WifiProtectMode mCurrentOpMode {WifiProtectMode::WIFI_PROTECT_NO_HELD};
139     int mFullHighPerfProtectsAcquired {0};
140     int mFullHighPerfProtectsReleased {0};
141     int mFullLowLatencyProtectsAcquired {0};
142     int mFullLowLatencyProtectsReleased {0};
143     /* Not used: long mCurrentSessionStartTimeMs; */
144     bool mWifiConnected {false};
145     bool mScreenOn {false};
146     bool mForceHiPerfMode {false};
147     bool mForceLowLatencyMode {false};
148     std::mutex mMutex;
149 #ifndef OHOS_ARCH_LITE
150     sptr<AppExecFwk::IAppMgr> mAppObject {nullptr};
151 #endif
152 };
153 } // namespace Wifi
154 } // namespace OHOS
155 
156 #endif
157