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