1 ##############################################################################
3 ## $Id: pmu.py,v 1.2 2003/04/10 22:13:55 riemer Exp $
5 ## Copyright (C) 2002-2003 Tilo Riemer <riemer@lincvs.org>
6 ## All rights reserved.
8 ## Redistribution and use in source and binary forms, with or without
9 ## modification, are permitted provided that the following conditions
12 ## 1. Redistributions of source code must retain the above copyright
13 ## notice, this list of conditions and the following disclaimer.
14 ## 2. Redistributions in binary form must reproduce the above copyright
15 ## notice, this list of conditions and the following disclaimer in the
16 ## documentation and/or other materials provided with the distribution.
17 ## 3. The name of the author may not be used to endorse or promote products
18 ## derived from this software without specific prior written permission.
20 ## THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 ## IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 ## OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 ## IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 ## INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 ## NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 ## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 ## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 ## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 ## THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ###############################################################################
41 CHARGING
= 2 #implies ONLINE
47 """Interface class for PMU"""
51 if res
.find("linux2") > -1:
55 self
.pmu
= None #throw exception (os unknown)
60 """Updates the PMU state"""
65 """Returns percentage capacity of all batteries"""
66 return self
.pmu
.percent()
70 """Returns time of all batteries (in minutes)"""
71 return self
.pmu
.time()
74 def charging_state(self
):
75 """Returns ac state (off-/online/charging)"""
76 return self
.pmu
.charging_state()
82 #base class for os dependent pmu classes
86 self
.ac_line_state
= OFFLINE
89 self
.life_time
= 0 #0 seconds
91 #initial reading of pmu info
95 #returns percentage capacity of all batteries
96 return self
.life_percent
100 #returns time of all batteries (in minutes)
101 return self
.life_time
104 def charging_state(self
):
105 return self
.ac_line_state
110 #implementation for Linux
111 #if we want support more than battery info we need a low level module implemented using C
113 class PmuLinux(PmuBase
):
115 #read /proc/pmu and extract needed infos
118 pmu_proc
= open("/proc/pmu/info")
120 raise EnvironmentError
123 lines
= pmu_proc
.readlines()
125 token
= line
.split(':')
126 data
[token
[0].strip()] = token
[1].strip()
129 pmu_battery
= open("/proc/pmu/battery_0")
131 self
.ac_line_state
= ONLINE
134 lines
= pmu_battery
.readlines()
137 token
= line
.split(':')
138 data
[token
[0].strip()] = token
[1].strip()
142 if data
['Battery count'] != "0" and data
['AC Power'] == "1" and \
143 (int(data
['charge']) < int(data
['max_charge'])):
144 self
.ac_line_state
= CHARGING
145 elif data
['Battery count'] == "0" or data
['AC Power'] == "1":
146 self
.ac_line_state
= ONLINE
148 self
.ac_line_state
= OFFLINE
150 self
.life_percent
= int(100 * int(data
['charge']) / int(data
['max_charge']))
152 self
.life_time
= int(int(data
['time rem.']) / 60)