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