Console failed gracefully when SmartRF isn't found.
[goodfet] / client / GoodFETConsole.py
1 #!/usr/bin/env python
2 # GoodFET Client Library
3
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
5 #
6 # This code is being rewritten and refactored.  You've been warned!
7
8 import sys, os;
9 import binascii;
10
11 from GoodFET import GoodFET;
12 from intelhex import IntelHex;
13
14 class GoodFETConsole():
15     """An interactive goodfet driver."""
16     
17     def __init__(self, client):
18         self.client=client;
19         client.serInit();
20         client.setup();
21         client.start();
22         client.loadsymbols();
23     def prompt(self):
24         sys.stdout.write("gf% ");
25         sys.stdout.flush();
26     def run(self):
27         self.prompt();
28         #for cmd in sys.stdin:
29         while 1:
30             cmd=sys.stdin.readline();
31             if not cmd: break;
32             if(cmd.strip()!=""):
33                 self.handle(cmd);
34             self.prompt();
35     def handle(self, str):
36         """Handle a command string.  First word is command."""
37         #Lines beginning with # are comments.
38         if(str[0]=="#"):  return;
39         #Lines beginning with ! are Python.
40         if(str[0]=="!"):
41             try:
42                 exec(str.lstrip('!'));
43             except:
44                 print sys.exc_info()[0];
45             return;
46         #Backtick (`) indicates shell commands.
47         if(str[0]=='`'):
48             os.system(str.lstrip('`'));
49             return;
50         #By this point, we're looking at a GoodFET command.
51         args=str.split();
52         if len(args)==0:
53             return;
54         try:
55             eval("self.CMD%s(args)" % args[0])
56         except:
57             print sys.exc_info()[0];
58             #print "Unknown command '%s'." % args[0];
59     def CMDinfo(self,args):
60         print self.client.infostring()
61     def CMDlock(self,args):
62         print "Locking.";
63         self.client.lock();
64     def CMDerase(self,args):
65         print "Erasing.";
66         self.client.erase();
67     def CMDtest(self,args):
68         self.client.test();
69         return;
70     def CMDstatus(self,args):
71         print self.client.status();
72         return;
73     def CMDhalt(self,args):
74         print self.client.halt();
75     def CMDresume(self,args):
76         print self.client.resume();
77     def CMDpeek(self,args):
78         adr=args[1];
79         memory="vn";
80         if(len(args)>2):
81             memory=args[2];
82         adr= self.client.name2adr(adr);
83         #print "%i" % adr;
84         print "0x%08x:= 0x%04x" % (
85             adr, self.client.peek16(adr,
86                                     memory));
87     def CMDflash(self,args):
88         file=args[1];
89         self.client.flash(self.expandfilename(file));
90     def CMDdump(self,args):
91         file=args[1];
92         self.client.dump(self.expandfilename(file));
93     def CMDwhere(self,args):
94         pc=self.client.getpc();
95         print "PC=0x%04X" % pc;
96     def CMDchip(self,args):
97         cmd="self.client.CMD%s()" % args[1];
98         print cmd;
99         try:
100             eval(cmd);
101         except:
102             print sys.exc_info()[0];
103             print "Chip-specific command failed.";
104     def expandfilename(self,filename):
105         if(filename[0]=='~'):
106             return "%s%s" % (os.environ.get("HOME"),filename.lstrip('~'));
107         return filename;
108     
109