]> git.neil.brown.name Git - freerunner.git/commitdiff
lib: various updates
authorNeilBrown <neilb@suse.de>
Sun, 6 Feb 2011 09:20:12 +0000 (20:20 +1100)
committerNeilBrown <neilb@suse.de>
Sun, 6 Feb 2011 09:20:12 +0000 (20:20 +1100)
Signed-off-by: NeilBrown <neilb@suse.de>
lib/dnotify.py
lib/suspend.py

index 1cd8c4115cc7b05011e402bb2c87f66ab1846ae9..12d3574697cb070b4c2db539657841c892c4c7b5 100644 (file)
@@ -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
 
index c84fbfdf7c1251eb9a4394ec11af80e1d14df032..49e4de30d1bba8c2e595f264e2c51b7c44fa2653 100644 (file)
@@ -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