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 "nfa_api.h"
22 #include "SyncEvent.h"
23 
24 
25 /*****************************************************************************
26 **
27 **  Name:           PowerSwitch
28 **
29 **  Description:    Adjust the controller's power states.
30 **
31 *****************************************************************************/
32 class PowerSwitch
33 {
34 public:
35 
36 
37     /*******************************************************************************
38     **
39     ** Description:     UNKNOWN_LEVEL: power level is unknown because the stack is off.
40     **                  FULL_POWER: controller is in full-power state.
41     **                  LOW_POWER: controller is in lower-power state.
42     **
43     *******************************************************************************/
44     enum PowerLevel {UNKNOWN_LEVEL, FULL_POWER, LOW_POWER, POWER_OFF};
45 
46     /*******************************************************************************
47     **
48     ** Description:     POWER_STATE_OFF: power level is OFF when screen is off.
49     **                  POWER_STATE_FULL: controller is in full-power state when screen is off.
50     **                                    after a period of inactivity, controller goes into snooze
51     **                                    mode.
52     **                  POWER_STATE_CARD_EMULATION: screen-off card-emulation
53     **                                              (CE4/CE3/CE1 modes are used),
54     **
55     *******************************************************************************/
56     enum ScreenOffPowerState {
57         POWER_STATE_OFF = 0,
58         POWER_STATE_FULL = 1,
59         POWER_STATE_CARD_EMULATION = 2
60     };
61 
62     /*******************************************************************************
63     **
64     ** Description:     DISCOVERY: Discovery is enabled
65     **                  SE_ROUTING: Routing to SE is enabled.
66     **                  SE_CONNECTED: SE is connected.
67     **
68     *******************************************************************************/
69     typedef int PowerActivity;
70     static const PowerActivity DISCOVERY;
71     static const PowerActivity SE_ROUTING;
72     static const PowerActivity SE_CONNECTED;
73     static const PowerActivity HOST_ROUTING;
74 
75     /*******************************************************************************
76     **
77     ** Description:     Platform Power Level, copied from 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 user locked
82     **                  SCREEN_ON_UNLOCKED: The phone is turned ON, screen is ON, and user unlocked
83     **
84     *******************************************************************************/
85     static const int PLATFORM_UNKNOWN_LEVEL = 0;
86     static const int PLATFORM_POWER_OFF = 1;
87     static const int PLATFORM_SCREEN_OFF = 2;
88     static const int PLATFORM_SCREEN_ON_LOCKED = 3;
89     static const int PLATFORM_SCREEN_ON_UNLOCKED = 4;
90 
91     static const int VBAT_MONITOR_ENABLED = 1;
92     static const int VBAT_MONITOR_PRIMARY_THRESHOLD = 5;
93     static const int VBAT_MONITOR_SECONDARY_THRESHOLD = 8;
94     /*******************************************************************************
95     **
96     ** Function:        PowerSwitch
97     **
98     ** Description:     Initialize member variables.
99     **
100     ** Returns:         None
101     **
102     *******************************************************************************/
103     PowerSwitch ();
104 
105 
106     /*******************************************************************************
107     **
108     ** Function:        ~PowerSwitch
109     **
110     ** Description:     Release all resources.
111     **
112     ** Returns:         None
113     **
114     *******************************************************************************/
115     ~PowerSwitch ();
116 
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     **
143     ** Function:        getLevel
144     **
145     ** Description:     Get the current power level of the controller.
146     **
147     ** Returns:         Power level.
148     **
149     *******************************************************************************/
150     PowerLevel getLevel ();
151 
152 
153     /*******************************************************************************
154     **
155     ** Function:        setLevel
156     **
157     ** Description:     Set the controller's power level.
158     **                  level: power level.
159     **
160     ** Returns:         True if ok.
161     **
162     *******************************************************************************/
163     bool setLevel (PowerLevel level);
164 
165     /*******************************************************************************
166     **
167     ** Function:        setScreenOffPowerState
168     **
169     ** Description:     Set the controller's screen off power state.
170     **                  state: the desired screen off power state.
171     **
172     ** Returns:         True if ok.
173     **
174     *******************************************************************************/
175     bool setScreenOffPowerState (ScreenOffPowerState state);
176 
177     /*******************************************************************************
178     **
179     ** Function:        setModeOff
180     **
181     ** Description:     Set a mode to be deactive.
182     **
183     ** Returns:         True if any mode is still active.
184     **
185     *******************************************************************************/
186     bool setModeOff (PowerActivity deactivated);
187 
188 
189     /*******************************************************************************
190     **
191     ** Function:        setModeOn
192     **
193     ** Description:     Set a mode to be active.
194     **
195     ** Returns:         True if any mode is active.
196     **
197     *******************************************************************************/
198     bool setModeOn (PowerActivity activated);
199 
200 
201     /*******************************************************************************
202     **
203     ** Function:        abort
204     **
205     ** Description:     Abort and unblock currrent operation.
206     **
207     ** Returns:         None
208     **
209     *******************************************************************************/
210     void abort ();
211 
212 
213     /*******************************************************************************
214     **
215     ** Function:        deviceManagementCallback
216     **
217     ** Description:     Callback function for the stack.
218     **                  event: event ID.
219     **                  eventData: event's data.
220     **
221     ** Returns:         None
222     **
223     *******************************************************************************/
224     static void deviceManagementCallback (UINT8 event, tNFA_DM_CBACK_DATA* eventData);
225 
226 
227     /*******************************************************************************
228     **
229     ** Function:        isPowerOffSleepFeatureEnabled
230     **
231     ** Description:     Whether power-off-sleep feature is enabled in .conf file.
232     **
233     ** Returns:         True if feature is enabled.
234     **
235     *******************************************************************************/
236     bool isPowerOffSleepFeatureEnabled ();
237 
238 private:
239     PowerLevel mCurrLevel;
240     UINT8 mCurrDeviceMgtPowerState; //device management power state; such as NFA_BRCM_PWR_MODE_???
241     UINT8 mExpectedDeviceMgtPowerState; //device management power state; such as NFA_BRCM_PWR_MODE_???
242     int mDesiredScreenOffPowerState; //read from .conf file; 0=power-off-sleep; 1=full power; 2=CE4 power
243     static PowerSwitch sPowerSwitch; //singleton object
244     static const UINT8 NFA_DM_PWR_STATE_UNKNOWN = -1; //device management power state power state is unknown
245     SyncEvent mPowerStateEvent;
246     PowerActivity mCurrActivity;
247     Mutex mMutex;
248 
249 
250     /*******************************************************************************
251     **
252     ** Function:        setPowerOffSleepState
253     **
254     ** Description:     Adjust controller's power-off-sleep state.
255     **                  sleep: whether to enter sleep state.
256     **
257     ** Returns:         True if ok.
258     **
259     *******************************************************************************/
260     bool setPowerOffSleepState (bool sleep);
261 
262 
263     /*******************************************************************************
264     **
265     ** Function:        deviceMgtPowerStateToString
266     **
267     ** Description:     Decode power level to a string.
268     **                  deviceMgtPowerState: power level.
269     **
270     ** Returns:         Text representation of power level.
271     **
272     *******************************************************************************/
273     const char* deviceMgtPowerStateToString (UINT8 deviceMgtPowerState);
274 
275 
276     /*******************************************************************************
277     **
278     ** Function:        powerLevelToString
279     **
280     ** Description:     Decode power level to a string.
281     **                  level: power level.
282     **
283     ** Returns:         Text representation of power level.
284     **
285     *******************************************************************************/
286     const char* powerLevelToString (PowerLevel level);
287 
288 
289     /*******************************************************************************
290     **
291     ** Function:        screenOffPowerStateToString
292     **
293     ** Description:     Decode screen-off power state to a string.
294     **                  state: power state
295     **
296     ** Returns:         Text representation of screen-off power state.
297     **
298     *******************************************************************************/
299     const char* screenOffPowerStateToString (ScreenOffPowerState state);
300 };
301