778551136d92cbb34c72bd215fcc41ff962bd338
[ussd-widget] / ussd-common / src / usr / bin / ussdquery.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published
5 ## by the Free Software Foundation; version 2 and higer.
6 ##
7 ## Guseynov Alexey (kibergus bark-bark gmail.com) 2010
8
9 import pexpect
10 import time
11 from subprocess import *
12 import sys
13 import gsmdecode
14
15 if len(sys.argv) != 2:
16     print "Usage: ussdquery.py <ussd number>"
17     sys.exit()
18
19 # Operations should timeout in 30 seconds.
20 # I'm not shure, that readline uses timeouts
21 child = pexpect.spawn('pnatd', [], 30);
22 child.send('at\r');
23 # Read our "at" command
24 child.readline();
25 # Read OK response
26 child.readline();
27
28 child.send('at+cusd=1,"'+(sys.argv[1])+'",15\r');
29 # Read our query echoed back
30 child.readline();
31
32 #Read and parse reply
33 replystring = child.readline();
34 start = replystring.find('"');
35 end = replystring.find('"', start+1);
36 reply = replystring[start+1:end];
37 encoding = replystring[end+2:].strip();
38
39 child.sendeof();
40
41 # Decoding ansver
42 reply = gsmdecode.decode(reply, int(encoding))
43
44 print reply;
45