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.
7 ## Guseynov Alexey (kibergus bark-bark gmail.com) 2010
11 from subprocess import *
14 if len(sys.argv) != 2:
15 print "Usage: ussdquery.py <ussd number>"
18 # Operations should timeout in 30 seconds.
19 # I'm not shure, that readline uses timeouts
20 child = pexpect.spawn('pnatd', [], 30);
22 # Read our "at" command
27 child.send('at+cusd=1,"'+(sys.argv[1])+'",15\r');
28 # Read our query echoed back
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();
40 # Check if we need to decode answer
41 # Compressed messages are not supported yet
43 reply = reply.decode("hex")
44 elif encoding == '72':
45 reply = reply.decode("hex")
46 p = Popen(['iconv', '-f', 'UCS-2BE', '-t', 'UTF8'], stdin=PIPE, stdout=PIPE);
47 reply = p.communicate(reply)[0]