power.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "power.h"
00022
00023 #if defined(QT_QWS_SL5XXX) || defined(QT_QWS_RAMSES)
00024 #include "custom.h"
00025 #endif
00026
00027 #include <unistd.h>
00028 #include <stdlib.h>
00029 #include <stdio.h>
00030 #include <fcntl.h>
00031 #include <sys/ioctl.h>
00032
00033 #ifdef QT_QWS_IPAQ_NO_APM
00034 #include <linux/h3600_ts.h>
00035 #endif
00036
00037 PowerStatusManager *PowerStatusManager::powerManager = 0;
00038 PowerStatus *PowerStatusManager::ps = 0;
00039
00040 static bool haveProcApm = false;
00041
00042 PowerStatusManager::PowerStatusManager()
00043 {
00044 powerManager = this;
00045 ps = new PowerStatus;
00046 FILE *f = fopen("/proc/apm", "r");
00047 if ( f ) {
00048 fclose(f);
00049 haveProcApm = TRUE;
00050 }
00051 }
00052
00053 const PowerStatus &PowerStatusManager::readStatus()
00054 {
00055 if ( !powerManager )
00056 (void)new PowerStatusManager;
00057
00058 powerManager->getStatus();
00059
00060 return *ps;
00061 }
00062
00063
00064 bool PowerStatusManager::getProcApmStatus( int &ac, int &bs, int &bf, int &pc, int &sec )
00065 {
00066 bool ok = false;
00067
00068 ac = 0xff;
00069 bs = 0xff;
00070 bf = 0xff;
00071 pc = -1;
00072 sec = -1;
00073
00074 FILE *f = fopen("/proc/apm", "r");
00075 if ( f ) {
00076
00077 char u;
00078 fscanf(f, "%*[^ ] %*d.%*d 0x%*x 0x%x 0x%x 0x%x %d%% %i %c",
00079 &ac, &bs, &bf, &pc, &sec, &u);
00080 fclose(f);
00081 switch ( u ) {
00082 case 'm': sec *= 60;
00083 case 's': break;
00084 default: sec = -1;
00085 }
00086
00087
00088 switch ( bs ) {
00089 case 0x00:
00090 ps->bs = PowerStatus::High;
00091 break;
00092 case 0x01:
00093 ps->bs = PowerStatus::Low;
00094 break;
00095 case 0x7f:
00096 ps->bs = PowerStatus::VeryLow;
00097 break;
00098 case 0x02:
00099 ps->bs = PowerStatus::Critical;
00100 break;
00101 case 0x03:
00102 ps->bs = PowerStatus::Charging;
00103 break;
00104 case 0x04:
00105 case 0xff:
00106 default:
00107 ps->bs = PowerStatus::NotPresent;
00108 break;
00109 }
00110
00111 switch ( ac ) {
00112 case 0x00:
00113 ps->ac = PowerStatus::Offline;
00114 break;
00115 case 0x01:
00116 ps->ac = PowerStatus::Online;
00117 break;
00118 case 0x02:
00119 ps->ac = PowerStatus::Backup;
00120 break;
00121 }
00122
00123 if ( pc > 100 ) pc = 100;
00124 if ( pc < 0 ) pc = 0;
00125
00126 ps->percentRemain = pc;
00127 ps->secsRemain = sec;
00128
00129 ok = true;
00130 }
00131
00132 return ok;
00133 }
00134
00135 void PowerStatusManager::getStatus()
00136 {
00137 bool usedApm = FALSE;
00138
00139 ps->percentAccurate = TRUE;
00140
00141
00142
00143
00144 int ac, bs, bf, pc, sec;
00145 if ( haveProcApm )
00146 usedApm = getProcApmStatus( ac, bs, bf, pc, sec );
00147
00148 if ( !usedApm ) {
00149 #ifdef QT_QWS_IPAQ_NO_APM
00150 int fd;
00151 int err;
00152 struct bat_dev batt_info;
00153
00154 memset(&batt_info, 0, sizeof(batt_info));
00155
00156 fd = ::open("/dev/ts",O_RDONLY);
00157 if( fd < 0 )
00158 return;
00159
00160 ioctl(fd, GET_BATTERY_STATUS, &batt_info);
00161 ac_status = batt_info.ac_status;
00162 ps->percentRemain = ( 425 * batt_info.batt1_voltage ) / 1000 - 298;
00163 ps->secsRemain = -1;
00164 ::close (fd);
00165 #else
00166 ps->percentRemain = 100;
00167 ps->secsRemain = -1;
00168 ps->percentAccurate = FALSE;
00169 #endif
00170 }
00171 }
This file is part of the documentation for OPIE Version 1.5.5.