51fa6f088f1cd293c9857d95d5f783ac8f38b534
[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
14 if len(sys.argv) != 2:
15     print "Usage: ussdquery.py <ussd number>"
16     sys.exit()
17
18 # Operations should timeout in 30 seconds.
19 # I'm not shure, that readline uses timeouts
20 child = pexpect.spawn('pnatd', [], 30);
21 child.send('at\r');
22 # Read our "at" command
23 child.readline();
24 # Read OK response
25 child.readline();
26
27 child.send('at+cusd=1,"'+(sys.argv[1])+'",15\r');
28 # Read our query echoed back
29 child.readline();
30
31 #Read and parse reply
32 replystring = child.readline();
33 start = replystring.find('"');
34 end = replystring.find('"', start+1);
35 reply = replystring[start+1:end];
36 encoding = replystring[end+2:].strip();
37
38 child.sendeof();
39
40 # Check if we need to decode answer
41 # Compressed messages are not supported yet
42 if encoding == '86':
43     reply = reply.decode("hex")
44 elif encoding == '72':
45     reply = reply.decode("hex")
46     reply = reply.decode("UTF-16 BE")
47
48 print reply;
49