More robust and stable
authorkibergus <kibergus@gmail.com>
Fri, 22 Jan 2010 18:44:14 +0000 (18:44 +0000)
committerkibergus <kibergus@gmail.com>
Fri, 22 Jan 2010 18:44:14 +0000 (18:44 +0000)
git-svn-id: file:///svnroot/ussd-widget/trunk@16 d197f4d6-dc93-42ad-8354-0da1f58e353f

ussd-common/build_ussd-common.py
ussd-common/src/usr/bin/ussdquery.py
ussd-common/src/usr/lib/python2.5/gsmdecode.py

index c2c91e3..4c4684f 100644 (file)
@@ -38,9 +38,9 @@ if __name__ == "__main__":
 chmod +s /usr/bin/pnatd
 """ #Set here your post install script
 
-    version = "0.0.0"
-    build = "3" 
-    changeloginformation = "First release" 
+    version = "0.0.1"
+    build = "0" 
+    changeloginformation = "More robust" 
    
     dir_name = "src"     
 
index 7785511..e374680 100755 (executable)
@@ -11,35 +11,65 @@ import time
 from subprocess import *
 import sys
 import gsmdecode
-
+#print "argh"
+#time.sleep(3000)
+#sys.exit()
 if len(sys.argv) != 2:
     print "Usage: ussdquery.py <ussd number>"
     sys.exit()
 
 # Operations should timeout in 30 seconds.
 # I'm not shure, that readline uses timeouts
-child = pexpect.spawn('pnatd', [], 30);
-child.send('at\r');
-# Read our "at" command
-child.readline();
-# Read OK response
-child.readline();
-
-child.send('at+cusd=1,"'+(sys.argv[1])+'",15\r');
-# Read our query echoed back
-child.readline();
-
-#Read and parse reply
-replystring = child.readline();
-start = replystring.find('"');
-end = replystring.find('"', start+1);
-reply = replystring[start+1:end];
-encoding = replystring[end+2:].strip();
-
-child.sendeof();
+child = None
+response = ""
+retry = 5
+while response != "OK" and retry > 0 :
+       if child == None :
+               # OK response should be recieved shortly
+               child = pexpect.spawn('pnatd', [], 2)
+       try :
+               child.send('at\r');
+               # Read our "at" command
+               child.readline();
+               # Read OK response
+               response = child.readline().strip()
+       except pexpect.TIMEOUT:
+               child.kill(9)
+               child = None
+               response = ""
+       if response != "OK" :
+               time.sleep(0.5)
+               retry -= 1
+
+child.timeout = 30
+
+if response != "OK" :
+       print >> sys.stderr, "Couldn't init modem."
+       sys.exit (-1)
+
+try :
+       child.send('at+cusd=1,"'+(sys.argv[1])+'",15\r')
+       # Read our query echoed back
+       child.readline()
+
+       #Read and parse reply
+       replystring = child.readline()
+       child.sendeof()
+except pexpect.TIMEOUT:
+       print >> sys.stderr, "Timeout. Modem didn't reply."
+       sys.exit (-2)
+
+if replystring.strip() == "ERROR" :
+       print >> sys.stderr, "Modem returned ERROR. Query not executed."
+       sys.exit (-3)
+
+start = replystring.find('"')
+end = replystring.find('"', start+1)
+reply = replystring[start+1:end]
+encoding = replystring[end+2:].strip()
 
 # Decoding ansver
 reply = gsmdecode.decode(reply, int(encoding))
 
-print reply;
+print reply
 
index 20f0b35..924c24a 100644 (file)
@@ -1,12 +1,3 @@
-#!/usr/bin/python
-# -*- coding: utf-8 -*-
-## This program is free software; you can redistribute it and/or modify
-## it under the terms of the GNU General Public License as published
-## by the Free Software Foundation; version 2 and higer.
-##
-## Martin Grimme (martin.grimme # gmail.com) 2010
-
-
 LANG_DE = 0x0
 LANG_EN = 0x1
 LANG_IT = 0x2
@@ -240,7 +231,6 @@ def _decode_general_data_coding(s, h, l):
     elif (alphabet == 0x2):
         # USC2 (16 bit, BE)
         return _decode_usc2(s)
-        
     elif (alphabet == 0x3):
         # reserved
         return s