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:
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()
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
#
# interact with apm/events.d/interlock to provide
# suspend notification
+#
+# Also parse resume_reason files to get the reason
import dnotify, fcntl, os
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