From 05557176061a0f04bb17d1e65614128b703b3bcf Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Sun, 6 Feb 2011 20:20:12 +1100 Subject: [PATCH] lib: various updates Signed-off-by: NeilBrown --- lib/dnotify.py | 51 ++++++++++++++++++++++++-------- lib/suspend.py | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 12 deletions(-) diff --git a/lib/dnotify.py b/lib/dnotify.py index 1cd8c41..12d3574 100644 --- a/lib/dnotify.py +++ b/lib/dnotify.py @@ -25,6 +25,7 @@ class dir(): self.dname = dname self.fd = os.open(dname, 0) self.files = [] + self.callbacks = [] fcntl.fcntl(self.fd, fcntl.F_NOTIFY, (fcntl.DN_MODIFY|fcntl.DN_RENAME| fcntl.DN_CREATE|fcntl.DN_DELETE)) if not dirlist: @@ -32,9 +33,20 @@ class dir(): dirlist.append(self) def watch(self, fname, callback): - self.files.append(file(os.path.join(self.dname, fname), callback)) + f = file(os.path.join(self.dname, fname), callback) + self.files.append(f) + return f + + def watchall(self, callback): + self.callbacks.append(callback) def check(self): + newlist = [] + for c in self.callbacks: + if c(): + newlist.append(c) + self.callbacks = newlist + for f in self.files: f.check() @@ -45,20 +57,35 @@ class dir(): class file(): def __init__(self, fname, callback): self.name = fname - stat = os.stat(self.name) - self.ino = stat.st_ino - self.size = stat.st_size - self.mtime = stat.st_mtime + try: + stat = os.stat(self.name) + except OSError: + self.ino = 0 + self.size = 0 + self.mtime = 0 + else: + self.ino = stat.st_ino + self.size = stat.st_size + self.mtime = stat.st_mtime self.callback = callback def check(self): - stat = os.stat(self.name) - if stat.st_size == self.size and stat.st_mtime == self.mtime \ - and stat.st_ino == self.ino: - return False - self.size = stat.st_size - self.mtime = stat.st_mtime - self.ino = stat.st_ino + try: + stat = os.stat(self.name) + except OSError: + if self.ino == 0: + return False + self.size = 0 + self.mtime = 0 + self.ino = 0 + else: + if stat.st_size == self.size and stat.st_mtime == self.mtime \ + and stat.st_ino == self.ino: + return False + self.size = stat.st_size + self.mtime = stat.st_mtime + self.ino = stat.st_ino + self.callback(self) return True diff --git a/lib/suspend.py b/lib/suspend.py index c84fbfd..49e4de3 100644 --- a/lib/suspend.py +++ b/lib/suspend.py @@ -2,6 +2,8 @@ # # interact with apm/events.d/interlock to provide # suspend notification +# +# Also parse resume_reason files to get the reason import dnotify, fcntl, os @@ -62,6 +64,83 @@ class monitor: fcntl.flock(old, fcntl.LOCK_UN) old.close() +# PMU reasons +# experimentally +# 4000000000 (38) alarm +# 0400000000 (34) usb power insert +# 0002000000 (25) power button + +# From some random document ??? +# adpins 34 +# adprem +# usbins +# usbrem +# rtcalarm 38 +# second +# onkeyr +# onkeyf +# exton1r +# exton1f +# exton2r +# exton2f +# exton3r +# exton3f +# batfull +# chghalt +# thlimon +# thlimoff +# usblimon +# usblimoff +# adcrdy +# onkey1s +# lowsys +# lowbat +# hightmp +# autopwrfail +# dwn1pwrfail +# dwn2pwrfail +# ledpwrfail +# ledovp +# ldo1pwrfail +# ldo2pwrfail +# ldo3pwrfail +# ldo4pwrfail +# ldo5pwrfail +# ldo6pwrfail +# hcidopwrfail +# hcidoovl + +minor_reason = {} +minor_reason['4000000000'] = 'rtc-alarm' +minor_reason['0400000000'] = 'adapter-insert' +minor_reason['0002000000'] = 'power-button-pressed' + + +def resume_reason(): + f = open("/sys/class/i2c-adapter/i2c-0/0-0073/neo1973-resume.0/resume_reason") + major = "" + for i in f: + i = i.strip() + if i[0] != '*': + continue + if i[2:6] == "EINT" and i[8] == '_': + i = i[9:] + major = i + break + + if major == 'PMU': + f = open("/sys/class/i2c-adapter/i2c-0/0-0073/resume_reason") + i = f.readline().strip() + if i in minor_reason: + minor = minor_reason[i] + else: + minor = i + return major + ' ' + minor + else: + return major + + + if __name__ == '__main__': import signal -- 2.39.5