]> git.neil.brown.name Git - plato.git/commitdiff
atchan: retry if open fails.
authorNeilBrown <neilb@suse.de>
Fri, 13 Dec 2013 09:28:53 +0000 (20:28 +1100)
committerNeilBrown <neilb@suse.de>
Fri, 13 Dec 2013 09:28:53 +0000 (20:28 +1100)
This is useful for USB attached TTY which resets occasionally
for some reason.

gsm/atchan.py

index bc92b23d6006ec3a43698c32fe12775f7ca017f1..a1c6ed190d23c933d9fdf58214d54c5f970bb101 100644 (file)
@@ -36,11 +36,20 @@ class AtChannel:
             self.sock = None
         self.connected = False
 
-    def connect(self):
+    def connect(self, retry=10000):
         if self.sock != None:
             return
         log("connect to", self.path)
-        s = open(self.path,"r+")
+        s = None
+        while s == None and retry > 0:
+            retry -= 1
+            try:
+                s = open(self.path,"r+")
+            except IOError:
+                time.sleep(0.4)
+                s = None
+        if not s:
+            return False
         #s.setblocking(0)
         fd = s.fileno()
         attr = termios.tcgetattr(fd)
@@ -48,10 +57,11 @@ class AtChannel:
         attr[6][termios.VMIN] = 0
         attr[6][termios.VTIME] = 0
         termios.tcsetattr(fd, termios.TCSANOW, attr)
-        
+
         self.watcher = gobject.io_add_watch(s, gobject.IO_IN, self.readdata)
         self.sock = s
         self.connected = True
+        return True
 
     def readdata(self, io, arg):
         try:
@@ -116,7 +126,7 @@ class AtChannel:
         self.timer = None
         self.timedout()
         return False
-    
+
     def set_timeout(self, delay):
         if self.pending:
             raise ValueError
@@ -127,7 +137,7 @@ class AtChannel:
         if self.pending:
             gobject.source_remove(self.timer)
             self.pending = False
-            
+
     def abort_timeout(self):
         if self.pending:
             self.cancel_timeout()