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: DISCOVERY: Discovery is enabled 49 ** SE_ROUTING: Routing to SE is enabled. 50 ** SE_CONNECTED: SE is connected. 51 ** 52 *******************************************************************************/ 53 typedef int PowerActivity; 54 static const PowerActivity DISCOVERY; 55 static const PowerActivity SE_ROUTING; 56 static const PowerActivity SE_CONNECTED; 57 58 /******************************************************************************* 59 ** 60 ** Description: Platform Power Level, copied from NativeNfcBrcmPowerMode.java. 61 ** UNKNOWN_LEVEL: power level is unknown. 62 ** POWER_OFF: The phone is turned OFF 63 ** SCREEN_OFF: The phone is turned ON but screen is OFF 64 ** SCREEN_ON_LOCKED: The phone is turned ON, screen is ON but user locked 65 ** SCREEN_ON_UNLOCKED: The phone is turned ON, screen is ON, and user unlocked 66 ** 67 *******************************************************************************/ 68 static const int PLATFORM_UNKNOWN_LEVEL = 0; 69 static const int PLATFORM_POWER_OFF = 1; 70 static const int PLATFORM_SCREEN_OFF = 2; 71 static const int PLATFORM_SCREEN_ON_LOCKED = 3; 72 static const int PLATFORM_SCREEN_ON_UNLOCKED = 4; 73 74 static const int VBAT_MONITOR_ENABLED = 1; 75 static const int VBAT_MONITOR_PRIMARY_THRESHOLD = 5; 76 static const int VBAT_MONITOR_SECONDARY_THRESHOLD = 8; 77 /******************************************************************************* 78 ** 79 ** Function: PowerSwitch 80 ** 81 ** Description: Initialize member variables. 82 ** 83 ** Returns: None 84 ** 85 *******************************************************************************/ 86 PowerSwitch (); 87 88 89 /******************************************************************************* 90 ** 91 ** Function: ~PowerSwitch 92 ** 93 ** Description: Release all resources. 94 ** 95 ** Returns: None 96 ** 97 *******************************************************************************/ 98 ~PowerSwitch (); 99 100 101 /******************************************************************************* 102 ** 103 ** Function: getInstance 104 ** 105 ** Description: Get the singleton of this object. 106 ** 107 ** Returns: Reference to this object. 108 ** 109 *******************************************************************************/ 110 static PowerSwitch& getInstance (); 111 112 /******************************************************************************* 113 ** 114 ** Function: initialize 115 ** 116 ** Description: Initialize member variables. 117 ** 118 ** Returns: None 119 ** 120 *******************************************************************************/ 121 void initialize (PowerLevel level); 122 123 124 /******************************************************************************* 125 ** 126 ** Function: getLevel 127 ** 128 ** Description: Get the current power level of the controller. 129 ** 130 ** Returns: Power level. 131 ** 132 *******************************************************************************/ 133 PowerLevel getLevel (); 134 135 136 /******************************************************************************* 137 ** 138 ** Function: setLevel 139 ** 140 ** Description: Set the controller's power level. 141 ** level: power level. 142 ** 143 ** Returns: True if ok. 144 ** 145 *******************************************************************************/ 146 bool setLevel (PowerLevel level); 147 148 149 /******************************************************************************* 150 ** 151 ** Function: setModeOff 152 ** 153 ** Description: Set a mode to be deactive. 154 ** 155 ** Returns: True if any mode is still active. 156 ** 157 *******************************************************************************/ 158 bool setModeOff (PowerActivity deactivated); 159 160 161 /******************************************************************************* 162 ** 163 ** Function: setModeOn 164 ** 165 ** Description: Set a mode to be active. 166 ** 167 ** Returns: True if any mode is active. 168 ** 169 *******************************************************************************/ 170 bool setModeOn (PowerActivity activated); 171 172 173 /******************************************************************************* 174 ** 175 ** Function: abort 176 ** 177 ** Description: Abort and unblock currrent operation. 178 ** 179 ** Returns: None 180 ** 181 *******************************************************************************/ 182 void abort (); 183 184 185 /******************************************************************************* 186 ** 187 ** Function: deviceManagementCallback 188 ** 189 ** Description: Callback function for the stack. 190 ** event: event ID. 191 ** eventData: event's data. 192 ** 193 ** Returns: None 194 ** 195 *******************************************************************************/ 196 static void deviceManagementCallback (UINT8 event, tNFA_DM_CBACK_DATA* eventData); 197 198 199 /******************************************************************************* 200 ** 201 ** Function: isPowerOffSleepFeatureEnabled 202 ** 203 ** Description: Whether power-off-sleep feature is enabled in .conf file. 204 ** 205 ** Returns: True if feature is enabled. 206 ** 207 *******************************************************************************/ 208 bool isPowerOffSleepFeatureEnabled (); 209 210 private: 211 PowerLevel mCurrLevel; 212 UINT8 mCurrDeviceMgtPowerState; //device management power state; such as NFA_DM_PWR_STATE_??? 213 int mDesiredScreenOffPowerState; //read from .conf file; 0=power-off-sleep; 1=full power; 2=CE4 power 214 static PowerSwitch sPowerSwitch; //singleton object 215 static const UINT8 NFA_DM_PWR_STATE_UNKNOWN = -1; //device management power state power state is unknown 216 SyncEvent mPowerStateEvent; 217 PowerActivity mCurrActivity; 218 Mutex mMutex; 219 220 221 /******************************************************************************* 222 ** 223 ** Function: setPowerOffSleepState 224 ** 225 ** Description: Adjust controller's power-off-sleep state. 226 ** sleep: whether to enter sleep state. 227 ** 228 ** Returns: True if ok. 229 ** 230 *******************************************************************************/ 231 bool setPowerOffSleepState (bool sleep); 232 233 234 /******************************************************************************* 235 ** 236 ** Function: deviceMgtPowerStateToString 237 ** 238 ** Description: Decode power level to a string. 239 ** deviceMgtPowerState: power level. 240 ** 241 ** Returns: Text representation of power level. 242 ** 243 *******************************************************************************/ 244 const char* deviceMgtPowerStateToString (UINT8 deviceMgtPowerState); 245 246 247 /******************************************************************************* 248 ** 249 ** Function: powerLevelToString 250 ** 251 ** Description: Decode power level to a string. 252 ** level: power level. 253 ** 254 ** Returns: Text representation of power level. 255 ** 256 *******************************************************************************/ 257 const char* powerLevelToString (PowerLevel level); 258 }; 259