Power management
Windows CE is a typical system that uses battery powered. This makes the correct operating system very critical, and the application does not need to pay attention to most of the time.
The power loss of the Windows CE device, but at some point you may pay attention to these losses.
When the user turns off a battery
Windows CE equipment, power system does not turn off the PC power supply, in fact, only the system is hang (translator Note: This is like some PocketPC to turn off the power in the position of the SIM card, unplug the SIM card really turns off the power. However, there is currently SMARTPHONE, because hardware devices, such as the CPU cannot enter low power consumption, so in order to save power, it is necessary to turn off the application processor and most of the devices, and then need to wake up, and then pass the timer or wireless module. Woke up. So the situation does not turn off the power is not absolute.) When the user opens the device power supply, the device does not restart as the PC, but is awakened, returns to the same state as the system hangs. This causes an application to operate as hanging before waking up. In fact, the application does not know that it is suspended unless it is clearly requested to notify it when the system hangs. From the point of view of the application, there are three ways to manage, query the power status, change the power status, and prevent the power state change.
Query power status
To query the current power status of the system, you must call
DWORD getSystemPowerStatusex2 (psystem_power_status_ex2 psystempowerStatusex2, DWORD DWLEN, BOOL fupdate);
The function has three parameters: a pointing
SYSTEM_POWER_STUS_EX2
The structure of the pointer, the length of the structure, and a boolean value indicates whether the operating system should query the battery driver to get the final information or directly return information in the battery cache. The system queries a battery condition approximately every 5 seconds, so if the third difference is false, the resulting data is not too old. Structure system_power_status_ex2 is defined as
Typedef struct _system_power_status_ex2 {
BYTE ACLINESTATUS;
BYTE BATTERYFLAG;
BYTE BATTERYLIFEPERCENT;
BYTE reserved1;
DWORD BATTERYLIFETIME;
DWORD BATTERYFULLLEFETIME;
Byte reserved2;
Byte backupbatteryflag;
Byte BackupbatteryLifePercent;
Byte reserved3;
DWORD backupbatterylifetime;
DWORD backupbatteryfull1;
Word batteryvoltage;
DWORD BATTERYCURRENT;
DWORD BATTERYAVERAGECURRENT;
DWORD BATTERYAVERAGEINTERVAL;
DWORD batterymaHourconsumed;
DWORD BATTERYTEMPERATURE;
DWORD backupbatteryvoltage;
BYTE BATTERYCHEMISTRY;
} System_power_status_ex2;
Before I described this huge structure, I must warn you that the data returned by this structure is the same as the battery driver. The same structure is transmitted to the battery drive to query its state.
Windows CE does not verify the data returned by the battery. The data returned by this function depends on the battery-driven, so different systems have different changes. For example, many systems do not report exact power levels when using AC power; other systems are the opposite. Application uses getSystemPowerStatusex2
Whether it is possible to prevent and detect whether the system can run an application.
The first area, AClineStatus, contains a flag indicating whether the system is connected to the AC power. If the value is ac_line_offline, the system does not use the AC power; ac_line_online, the system uses the AC power supply; ac_LINE_BACKUP_POWER and AC_LINE_UNKNOWN indicate the spare power and unknown power. The Batteryflag area provides a total identifier that represents the battery status of the current system, which can have the following values:
Battery_flag_high
The battery is filled or close.
Battery_flag_low
There is still a loss in the battery.
Battery_flag_critical
Battery power is in a critical state.
Battery_flag_charging
The battery is currently charging.
Battery_flag_no_battery
System-free battery
Battery_flag_unknown
Battery status unknown
BatteryLifePercent
The area contains an estimated percentage of battery power. The value may be one between 0 and 100, or by 255 to represent percentage unknown. The BatteryliFetime area indicates the number of seconds that can be maintained before the battery is exhausted. If this value cannot be estimated, the area fills in Battery_Life_Unknown. The BatteryFullLiFetime area contains the time that is fully charged. If this value cannot be estimated, fill in Battery_Life_Unknown. Note that these values may be difficult to measure in many systems. Most OEM vendors simply fill in Battery_life_unknown in each area.
The next fourth area (no reserved area) repeated the previous expression, but it is only the system backup battery. Because most of these values are difficult to measure, many systems simply return "Unknown" to these areas.
The remaining area describes the electricity state of the battery and spare battery, as many systems lack the ability to measure these values, which is also simple to be silently
"Unknown". The last area, BatteryChemistry, contains a logo, indicating the type of battery in the system. The currently defined value includes
·
Battery_Chemistry_alkaline
·
Battery_Chemistry_NICD
·
Battery_chemistry_nimh
·
Battery_Chemistry_lion
·
Battery_Chemistry_Lipoly
·
Battery_Chemistry_Unknown
Change the power status
The application can change the power status of the system through a range of ways. Be based on
In the newer system of the Windows CE.NET system, the preferred way is to use the power management program, and the subsequent chapters will be discussed. However, in any case, there is a large number of systems based on early Windows CE versions and Windows CE.NET does not include the power management program. For these systems, the following techniques are very convenient.
Turn off the power
Applications can call a less information
GwespowerOffsystem function hangs the system. This function can be used in most versions of Windows CE, but recently is only disclosed. In fact, most SDK does not include the prototype of this function, you may have to provide prototype. This function is defined as
Void gwespoweroffsystem (void);
Gwespoweroffsystem's use is very simple: Simple call, the system will hang.
If you want to avoid using a lot of data, you can close the system by simply simply simulating the user to press the Off button. You can easily allow your application to hang your system by using the keybd_event function, as follows:
KeyBD_Event (vk_off, 0, keyeventf_silent, 0);
KeyBD_Event (vk_off, 0, keyeventf_silent │ KeyEventf_Keyup, 0);
These two
KeyBD_EVENT calls the simulated press and release the power button, the virtual key value of the power button is
VK_off
. Perform the two lines of code will hang the system. Because the virtual key code is executed by the GWES performance, the two functions may have some states before the system hangs (the translator Note: the image related to the closed dialog on the screen, and the screen of the real press button) same). If your program can't stop working before the keybd_event function, add a SLEEP call to cause the application to pause some milliseconds to let GWES truely hang the system.
Close screen
If the system has a colored backlight display, the main power consumption is not
CPU is backlight. In some environments, an application needs to run but do not need to be displayed on the screen. An example is a music player application that does not pay attention to the screen when the user listens to music. In these cases, the ability to close the backlight will mean to increase the battery life.
Of course, when the user wants to see the screen, any simple user-friendly way to turn off the backlight application will reopen the screen. Similarly, remember that the typical idea of users is that the screen will be closed when the screen is black, so it is necessary to consider this. For example, a user may try to open the system power at the system, and do this, it is very unexpectedly found that the device power is turned off. Similarly, when the system is closed in this case, it also closes the touch screen. This means you can't tell the user to tap the screen to open. Instead, you need to use some other events, such as setting time, task completion, or user pressed a button. Finally, the way discussion here is based on most
The Windows CE 3.0 or updated version is relatively useful and is replaced by the power management program in Windows CE .NET 4.0. For newer systems, first look at whether the power management program is available, then control the screen. If it fails, the ExteScape method may be possible.
In Windows CE, the displayed control is through the ExteScape function. This is a back door that displays and printer drivers. Windows CE Display Drive Supports Many Device Side Codes (Escape Codes
) These are published in Platform Builder. For our purposes, only two escape code are used:
SetPowerManagement To set the displayed power status and queryescsupport to check if setPowerManagement is driven. The following example opens or off the system display by displaying the drive and supports a full escape code:
//
// Defines and structures taken from pwingdi.h in the Platform Builder
//
#define queryescsupport 8
#define setPowerManagement 6147
#define getPowerManagement 6148
Typedef enum _video_power_state {
VideoPoweron = 1,
VideoPowerStandby,
VideoPowersuspend,
VideoPoweroff
} Video_power_state, * pvideo_power_state;
TYPEDEF STRUCT _VIDEO_POWER_MANAGEMENT {
Ulong Length;
Ulong dpmsversion;
Ulong PowerState;
} Video_power_management, * pvideo_power_management;
/ / -------------------------------------------------------------------------------------------- ----------------------
// setvideopower - Turns on or off the display
//
INT setVideoPower (Bool fon) {
Video_Power_Management VPM;
Int rc, fqueryesc;
HDC HDC;
// Get the display DC.
HDC = GetDC (NULL);
// see if supported.
FQueryesc = setPowerManagement;
RC = ExteScape (HDC, Queryescsupport, Sizeof (FQueryesc),
(LPSTR) & FQuerysc, 0, 0);
IF (rc == 0) {
// no support, fail.
ReleaseDC (NULL, HDC);
Return -1;
}
// Fill in The Power Management Structure.
vpm.length = sizeof (vpm);
vpm.dpmsversion = 1;
IF (fon)
vpm.powerstate = videopower;
Else
VPM.Powerstate = VideoPowerOff;
// Tell the driver to turn on or off the display.
RC = EXTESCAPE (HDC, SETPOWERMANAGEMENT, SIZEOF (VPM),
(LPSTR) & VPM, 0, 0);
// ALWAYS RELEASE What You get.
ReleaseDC (NULL, HDC);
Return 0;
}
The previous code is called
The extectape and queryescsupport commands queries whether the transfer code is supported. The query command first handed over to the input buffer, if the setpowerManagement command is supported, the program fills the video_power_management structure and calls ExteScape again to set the power state.
Although these escape code allows the application to turn on or off, Windows CE does not have a unified way to control the brightness of the backlight. Each system has its own OEM unique way to control the backlight brightness. If there is a standard backlight brightness control method in the future, it will likely be placed in the extementcape function.
Open system power
When the system is suspended, the application will no longer run, so when the system wakes up, the application does not appear to be controlled. However, there are some ways to wake up a hang-up device. First, an application is given a given time and use
11 Received Message API (Notification API) Making a program that is awakened. Under normal circumstances, OEM vendors allocate some interrupt conditions to manage system power to open, or wake up. An example of this way is that a system is awakened when a synchronization cradle is prevented.
Prevent system shutdown
Conversely, it is also a problem that prevents system hanging. The Windows CE system is usually set to automatically hang when no user input is available for a while. To prevent automatically hang, an application can periodically call the function:
Void WinApi SystemIdletimerreset (Void);
This function is reset
Windows CE is used to monitor the timer input by the user. If the timer reaches a pre-user interval, the system will automatically hang. Since the pending timeout value can be changed, an application needs to know the timeout value, so it is necessary to call the SystemIdletimerreset. The system maintains three timeout values, which can be queried using SystemParametersInfo. Different performances passing to the constant of SystemParametersInfo, showing as follows:
SPI_GetBatteryIdletimeout
When the system runs in the battery power state, the time from the user is last input.
SPI_GETEXTERNALIDLETIMEOUT
When the system runs in the AC power state, the time from the user is last input.
SPI_GetwakeupIdletimeout
The time that is automatically woken away from the system when the system is hang again.
To prevent the power supply from being suspended, you need to query these three values and then call the SystemIdletimerReset before returning in the shortest time. If the timeout value is set to 0, the timeout value is disabled.
Power management program
A new, stand-alone power management component is
Windows CE .NET 4.0 is introduced. This power management program replaces a function of many GWes previously completed. The power management program defines a range of power states such as D0, D1, D2, and D3. These look mysterious names are corresponding to some friendly system level names.
For the embedded system,
OEM vendors define the power status of the system. For example, the power status may be open (ON), idle (IDLE) and suspend. Other power status is also defined, like Screenoff, Incradle, and Onbattery.
From the viewpoint of the application, the new power management program provides the ability to notify the power status change and the ability to change the power status through a series of functions.
The power state of the system is defined in the registry,
The SDK defines PWRMGR_REG_KEY, so that you have to know the string of the registry, but when the constant is not defined, the power management program registration data is kept in HKEY_LOCAL_MACHINE / System / CurrentControlSet / Control / Power. The power state is defined as a subkey, located in the key state.
Power notification
The power management program is very popular with that the application can be notified when the system power status changes. This allows the application from manually detecting the power state. An application can be called
RequestPowerNotifications Requests the Power Manager Send a notification to the application when the power status changes. The power management program sends a notification through a message queue created by the application.
RequestPowerNotifications
The prototype is as follows.
Handle RequestPowerNotifications (Handle HMSGQ, DWORD FLAGS);
The first parameter is a handle of an application in the previously established message queue. The second parameter is a series of parameters that represent the notification of the application wants to receive.
PBT_TRANSITION
Notification of accepting system power status changes. For example, when the system is from ON to SUSPEND.
PBT_RESUME
Receive notifications when system resumes.
PBT_POWERSTATUSCHANGE
The notification is received when the system is switched between AC and the battery.
PBT_POWERINFOCHANGE
Receive notifications when the system battery level varies.
Power_notify_all
Receive all notifications.
RequestPowerNotifications
The function returns a handle of a power notification to return NULL. When the message queue is established, the application must be read permissions because the application will read the power notification from the message queue.
To receive notifications, the application must use WaitForsingleObject to block the message handle. As discussed in Chapter 10, the handle will be Signaled when the notice is placed in the queue. The actual notification will be received in the structure Power_Broadcast table.
Typedef struct _power_broadcast {
DWORD Message;
DWORD FLAGS;
DWORD Length;
Wchar systempowerState [1];
} Power_Broadcast, * PPOWER_BROADCAST;
The first thing to note is that this structure length is variable. The last field,
SystemPowerstate
It is defined as a WCHARS type, but can fill in non-string data. The first field is to inform yourself, this field can fill the front.
One of the list of PBT_ logo. The FLAGS area can include the following flags depend on the notified notification:
Power_State_on
System
ON status.
Power_State_off
The system is in the OFF state.
Power_State_Critical
The system entered a critical OFF state.
Power_State_Boot
The system is starting.
Power_State_Idle
The system enters the IDLE state.
Power_State_Suspend
The system is hang.
Power_State_Reset
The system is reset.
The last two fields are interrelated. The Length field is the length of the SystemPowerState field data. The data contained in SystemPowerState depends on the notification sent. For PBT_Transition notifications, the SystemPowerState field contains a new power status identifier string. This string is ended in non-0. In order to end the string, use the Length field to indicate the length of the string. Note that the Length field is in bytes, when the character is a double-byte UNCODE character, you need to get the length of the string character, you need to remove TCHAR's size with the Length field.
For PBT_POWERINFOCHANGE notifications, the SystemPowerState field contains a PPOWER_BROADCAST_POWER_INFO structure:
Typedef struct _power_broadcast_power_info {
DWORD DWNUMLEVELS;
DWORD DWBATTERYLIFETIME;
DWORD DWBATTERYFULLLLEFETIME;
DWORD DWBACKUPBATTERYLIFETIME;
DWORD DWBACKUPBATTERYFULLL1IFETIME;
Byte baclineestatus;
Byte bbatteryflag;
BYTE BBATTERYLIFEPERCENT;
BYTE BBACKUPBATTERYFLAG;
BYTE BBACKUPBATTERYLIFEPERCENT;
} Power_Broadcast_power_info, * ppower_broadcast_power_info;
Note that there are some fields of fields and functions that are very similar to the previous discussion.
SYSTEM_POWER_STATUS_EX2
structure.
Set power status
The function provided by the power management program also allows the application to control the power status. There are two ways to control power. The first way is that the application gives a power supply setting. The second way is that the application requests the power state not below a given level. An application passes a call function
SetSystemPowerState can request a specific power state. This function prototype is as follows.
DWORD SETSYSTEMPOWERSTATE (LPCWSTR PSState, DWORD StateFlags,
DWORD OPTIONS);
The power state can be requested by specifying the first two parameters. If the first parameter is a non-zero value, it points to a string identifier being requested. This string must match one of the power status listed in the registry.
If psstate is
NULL, the second parameter stateFlags defines the requested power status. This parameter is from Power_State_on until one of the power_state_reset status, which is described in the Power_Broadcast structure in the previously mentioned.
The more special is the power_state_reset flag. This logo requests the system to restart, and the method of using the setsystempowerState will restart better than the method of calling Kerneliocontrol by using the ioctl_hal_reboot command directly. Calling SetSystemPowerState will allow the system to save the data in the buffer to the file system before the device is restarted.
Calling SetSystemPowerState is a method directly changing the power status. More clever ways is to request the system to maintain the minimum power status required for the system by calling SetPowerRequirement. SetSystemPowerState is assumed that the application knows the status, and the call setPowerRequirement is to allow the system to optimize the power settings to meet the needs of the application. An example of using SetPowerRequirement is relatively convenient, an application using a serial port requires a serial port to maintain a power status when communicating. SetPowerRequirement is defined as follows.
Handle SetPowerRequirement (PVOID PVDevice,
CEDEVICE_POWER_STATE DEVICESTATE,
Ulong DeviceFlags, PVOID PVSystemState,
Ulong StateFlags;
The first parameter specifies the application that the application needs to maintain the power status.
The DEVICESTATE parameter defines the power status of the device.
CEDEVICE_POWER_STATE
The specified state range is from D0 (meaning that the device is in the maximum power state) to D4 (the device is turned off) (the translator Note: In fact, the specific performance of the status of D0 to D4 is completely customizable by OEM vendors. For application developers, such as the D1 LCD backlight or in D2, it is uncertain, Microsoft only gives a standard definition, not an actual definition). The DeviceFlags parameter is merged by two signs: power_name, indicating that the device name is valid; Power_Force, indicating that the device should maintain the current state or even when the system hangs. If pvsystemState is not NULL, it means that it is valid only for power requests that have been named in pvsystemState. The device may not be able to change the status of the request.
The application should log out of the request, the prototype is as follows by calling ReleasePowerRequirement.
DWORD ReleasePowerRequirement (Handle HpowerReq);
The only parameter here is from
The handle returned in SetPowerRequirement.