1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  *  Adjust the controller's power states.
19  */
20 #pragma once
21 #include "SyncEvent.h"
22 #include "nfa_api.h"
23 
24 /*****************************************************************************
25 **
26 **  Name:           PowerSwitch
27 **
28 **  Description:    Adjust the controller's power states.
29 **
30 *****************************************************************************/
31 class PowerSwitch {
32  public:
33   /*******************************************************************************
34   **
35   ** Description:     UNKNOWN_LEVEL: power level is unknown because the stack is
36   *off.
37   **                  FULL_POWER: controller is in full-power state.
38   **                  LOW_POWER: controller is in lower-power state.
39   **
40   *******************************************************************************/
41   enum PowerLevel { UNKNOWN_LEVEL, FULL_POWER, LOW_POWER, POWER_OFF };
42 
43   /*******************************************************************************
44   **
45   ** Description:     POWER_STATE_OFF: power level is OFF when screen is off.
46   **                  POWER_STATE_FULL: controller is in full-power state when
47   *screen is off.
48   **                                    after a period of inactivity, controller
49   *goes into snooze
50   **                                    mode.
51   **                  POWER_STATE_CARD_EMULATION: screen-off card-emulation
52   **                                              (CE4/CE3/CE1 modes are used),
53   **
54   *******************************************************************************/
55   enum ScreenOffPowerState {
56     POWER_STATE_OFF = 0,
57     POWER_STATE_FULL = 1,
58     POWER_STATE_CARD_EMULATION = 2
59   };
60 
61   /*******************************************************************************
62   **
63   ** Description:     DISCOVERY: Discovery is enabled
64   **                  SE_ROUTING: Routing to SE is enabled.
65   **                  SE_CONNECTED: SE is connected.
66   **
67   *******************************************************************************/
68   typedef int PowerActivity;
69   static const PowerActivity DISCOVERY;
70   static const PowerActivity SE_ROUTING;
71   static const PowerActivity SE_CONNECTED;
72   static const PowerActivity HOST_ROUTING;
73 
74   /*******************************************************************************
75   **
76   ** Description:     Platform Power Level, copied from
77   *NativeNfcBrcmPowerMode.java.
78   **                  UNKNOWN_LEVEL: power level is unknown.
79   **                  POWER_OFF: The phone is turned OFF
80   **                  SCREEN_OFF: The phone is turned ON but screen is OFF
81   **                  SCREEN_ON_LOCKED: The phone is turned ON, screen is ON but
82   *user locked
83   **                  SCREEN_ON_UNLOCKED: The phone is turned ON, screen is ON,
84   *and user unlocked
85   **
86   *******************************************************************************/
87   static const int PLATFORM_UNKNOWN_LEVEL = 0;
88   static const int PLATFORM_POWER_OFF = 1;
89   static const int PLATFORM_SCREEN_OFF = 2;
90   static const int PLATFORM_SCREEN_ON_LOCKED = 3;
91   static const int PLATFORM_SCREEN_ON_UNLOCKED = 4;
92 
93   static const int VBAT_MONITOR_ENABLED = 1;
94   static const int VBAT_MONITOR_PRIMARY_THRESHOLD = 5;
95   static const int VBAT_MONITOR_SECONDARY_THRESHOLD = 8;
96   /*******************************************************************************
97   **
98   ** Function:        PowerSwitch
99   **
100   ** Description:     Initialize member variables.
101   **
102   ** Returns:         None
103   **
104   *******************************************************************************/
105   PowerSwitch();
106 
107   /*******************************************************************************
108   **
109   ** Function:        ~PowerSwitch
110   **
111   ** Description:     Release all resources.
112   **
113   ** Returns:         None
114   **
115   *******************************************************************************/
116   ~PowerSwitch();
117 
118   /*******************************************************************************
119   **
120   ** Function:        getInstance
121   **
122   ** Description:     Get the singleton of this object.
123   **
124   ** Returns:         Reference to this object.
125   **
126   *******************************************************************************/
127   static PowerSwitch& getInstance();
128 
129   /*******************************************************************************
130   **
131   ** Function:        initialize
132   **
133   ** Description:     Initialize member variables.
134   **
135   ** Returns:         None
136   **
137   *******************************************************************************/
138   void initialize(PowerLevel level);
139 
140   /*******************************************************************************
141   **
142   ** Function:        getLevel
143   **
144   ** Description:     Get the current power level of the controller.
145   **
146   ** Returns:         Power level.
147   **
148   *******************************************************************************/
149   PowerLevel getLevel();
150 
151   /*******************************************************************************
152   **
153   ** Function:        setLevel
154   **
155   ** Description:     Set the controller's power level.
156   **                  level: power level.
157   **
158   ** Returns:         True if ok.
159   **
160   *******************************************************************************/
161   bool setLevel(PowerLevel level);
162 
163   /*******************************************************************************
164   **
165   ** Function:        setScreenOffPowerState
166   **
167   ** Description:     Set the controller's screen off power state.
168   **                  state: the desired screen off power state.
169   **
170   ** Returns:         True if ok.
171   **
172   *******************************************************************************/
173   bool setScreenOffPowerState(ScreenOffPowerState state);
174 
175   /*******************************************************************************
176   **
177   ** Function:        setModeOff
178   **
179   ** Description:     Set a mode to be deactive.
180   **
181   ** Returns:         True if any mode is still active.
182   **
183   *******************************************************************************/
184   bool setModeOff(PowerActivity deactivated);
185 
186   /*******************************************************************************
187   **
188   ** Function:        setModeOn
189   **
190   ** Description:     Set a mode to be active.
191   **
192   ** Returns:         True if any mode is active.
193   **
194   *******************************************************************************/
195   bool setModeOn(PowerActivity activated);
196 
197   /*******************************************************************************
198   **
199   ** Function:        abort
200   **
201   ** Description:     Abort and unblock currrent operation.
202   **
203   ** Returns:         None
204   **
205   *******************************************************************************/
206   void abort();
207 
208   /*******************************************************************************
209   **
210   ** Function:        deviceManagementCallback
211   **
212   ** Description:     Callback function for the stack.
213   **                  event: event ID.
214   **                  eventData: event's data.
215   **
216   ** Returns:         None
217   **
218   *******************************************************************************/
219   static void deviceManagementCallback(uint8_t event,
220                                        tNFA_DM_CBACK_DATA* eventData);
221 
222   /*******************************************************************************
223   **
224   ** Function:        isPowerOffSleepFeatureEnabled
225   **
226   ** Description:     Whether power-off-sleep feature is enabled in .conf file.
227   **
228   ** Returns:         True if feature is enabled.
229   **
230   *******************************************************************************/
231   bool isPowerOffSleepFeatureEnabled();
232 
233  private:
234   PowerLevel mCurrLevel;
235   uint8_t mCurrDeviceMgtPowerState;  // device management power state; such as
236                                      // NFA_BRCM_PWR_MODE_???
237   uint8_t mExpectedDeviceMgtPowerState;  // device management power state; such
238                                          // as NFA_BRCM_PWR_MODE_???
239   int mDesiredScreenOffPowerState;  // read from .conf file; 0=power-off-sleep;
240                                     // 1=full power; 2=CE4 power
241   static PowerSwitch sPowerSwitch;  // singleton object
242   static const uint8_t NFA_DM_PWR_STATE_UNKNOWN =
243       -1;  // device management power state power state is unknown
244   SyncEvent mPowerStateEvent;
245   PowerActivity mCurrActivity;
246   Mutex mMutex;
247 
248   /*******************************************************************************
249   **
250   ** Function:        setPowerOffSleepState
251   **
252   ** Description:     Adjust controller's power-off-sleep state.
253   **                  sleep: whether to enter sleep state.
254   **
255   ** Returns:         True if ok.
256   **
257   *******************************************************************************/
258   bool setPowerOffSleepState(bool sleep);
259 
260   /*******************************************************************************
261   **
262   ** Function:        deviceMgtPowerStateToString
263   **
264   ** Description:     Decode power level to a string.
265   **                  deviceMgtPowerState: power level.
266   **
267   ** Returns:         Text representation of power level.
268   **
269   *******************************************************************************/
270   const char* deviceMgtPowerStateToString(uint8_t deviceMgtPowerState);
271 
272   /*******************************************************************************
273   **
274   ** Function:        powerLevelToString
275   **
276   ** Description:     Decode power level to a string.
277   **                  level: power level.
278   **
279   ** Returns:         Text representation of power level.
280   **
281   *******************************************************************************/
282   const char* powerLevelToString(PowerLevel level);
283 
284   /*******************************************************************************
285   **
286   ** Function:        screenOffPowerStateToString
287   **
288   ** Description:     Decode screen-off power state to a string.
289   **                  state: power state
290   **
291   ** Returns:         Text representation of screen-off power state.
292   **
293   *******************************************************************************/
294   const char* screenOffPowerStateToString(ScreenOffPowerState state);
295 };
296