Qtopia library API Documentation

power.cpp

00001 /**********************************************************************
00002 ** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of the Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
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 // Standard /proc/apm reader
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     //I 1.13 1.2 0x02 0x00 0xff 0xff 49% 147 sec
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; // ok
00084         default: sec = -1; // unknown
00085     }
00086 
00087     // extract data
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: // 0xff is Unknown but we map to NotPresent
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     // Some iPAQ kernel builds don't have APM. If this is not the case we
00142     // save ourselves an ioctl by testing if /proc/apm exists in the
00143     // constructor and we use /proc/apm instead
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; // from h3600_ts.c
00163     ps->secsRemain = -1; // seconds is bogus on iPAQ
00164     ::close (fd);
00165 #else
00166     ps->percentRemain = 100;
00167     ps->secsRemain = -1;
00168     ps->percentAccurate = FALSE;
00169 #endif
00170     }
00171 }
KDE Logo
This file is part of the documentation for OPIE Version 1.5.5.
Documentation copyright © 1997-2003 the KDE developers. 2003 OPIE developers
Generated on Tue Feb 10 20:24:06 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001