00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 import sys, os, pdb, atexit
00089 import traceback, getopt
00090
00091
00092 from ieee802154_base import ProgramContext
00093 from ieee802154_io import FileIn, PortIn
00094 from ieee802154_socket import SocketOut
00095 from ieee802154_pipe import PipeOut
00096 from ieee802154_gui import *
00097
00098
00099 VERSION = "0.1"
00100 CTX = ProgramContext()
00101 DEVOUT = PipeOut()
00102 DEVIN = None
00103
00104
00105
00106
00107
00108
00109
00110 def do_quit(exclude=[]):
00111 global CTX,DEVIN,DEVOUT,gui
00112 print "quit: exclude:",exclude
00113 if gui != None and 'gui' not in exclude:
00114 print "quit_gui"
00115 gui.cancel()
00116 gui = None
00117 if DEVIN != None:
00118 print "quit_DEVIN"
00119 DEVIN.reset()
00120 DEVIN.close()
00121 DEVIN = None
00122 if DEVOUT != None:
00123 print "quit_socket"
00124 DEVOUT.close()
00125 DEVOUT = None
00126
00127 def print_options():
00128 print """\
00129 Usage:
00130 %s [OPTIONS]
00131 Options:
00132 -p PORT Name of the serial port where the sniffer plattform
00133 is connected, e.g. /dev/ttyUSB0 or COM1.
00134 -r CAPFILE Name of a capture file which is read @c mytrace.dat.
00135 -i PIPE Name of the named pipe.
00136 On a Linux system, PIPE can be a absolut path name, e.g.
00137 /tmp/ieee802154. On a Windows system PIPE is a name in a
00138 special pipe directory, e.g. foo maps to \\\\.\\pipe\\foo.
00139 [ieee802154]
00140 -c CHAN IEEE802.15.4 channel number
00141 -R RATE IEEE802.15.4 data rate
00142 -v increase verbose level
00143 -h show help and exit.
00144 -V print version and exit.
00145
00146 Note:
00147 * -r and -p can be used only exclusively,
00148 * -c works only with -p.
00149 """ % sys.argv[0]
00150
00151
00152
00153
00154
00155 def print_help():
00156 print """\
00157 Commands:
00158 reopen - reopens the named pipe and places a pcap header,
00159 so that wireshark can reconnect.
00160 chan <n> - set IEEE 802.15.4 channel.
00161 rate <r> - set IEEE 802.15.4 data rate (e.g. BPSK20, OQPSK100, ...)
00162 devrst - reinitialize the sniffer device
00163 debug - start python debugger
00164 info - show status of sniffer DEVIN
00165 quit - end session and terminate all threads
00166 help - show command help
00167 """
00168
00169
00170
00171
00172
00173 def print_banner():
00174 global VERSION, DEVIN
00175 fmtstr = {'file':'%(file)s, frames: %(frames)s',
00176 'port':'%(port)s, %(platform)s, channel=%(chan)s, rate=%(crate)s',
00177 }
00178 print "\nuracoli sniffer to wireshark bridge V%s" % VERSION
00179 inf = DEVIN.info()
00180 fmt = fmtstr.get(inf['type'],"UNKNOWN INTERFACE TYPE")
00181 print ' OPTS: ', " ".join(sys.argv[1:])
00182 print ' INPUT: ', fmt % inf
00183 print ' OUTPUT:', str(DEVOUT.TxPipe),'\n'
00184
00185
00186
00187
00188 def process_command_line_args(argv):
00189 global CTX
00190 opts,args = getopt.getopt(argv,"i:hp:r:vVNR::c:x")
00191 for o,v in opts:
00192 if o == "-i":
00193 v = v.strip()
00194 if os.path.isfile(v) or os.path.islink(v):
00195 print "Can't overwrite existing file or link with a pipe"
00196 sys.exit(1)
00197 CTX.SOCKNAME = v
00198 if o == "-r":
00199 CTX.CAPFILE = v.strip()
00200 CTX.PORT = None
00201 if o == "-p":
00202 CTX.CAPFILE = None
00203 CTX.PORT = v.strip()
00204 if o == "-v":
00205 CTX.VERBOSE += 1
00206 if o == "-N":
00207 CTX.MAXPACKETS = eval(v)
00208 if o == "-c":
00209 CTX.CHANNEL = eval(v)
00210 if o == "-R":
00211 CTX.RATE = v.strip()
00212 if o == "-x":
00213 CTX.GUI = True
00214 if o == "-h":
00215 print_options()
00216 print_help()
00217 sys.exit(0)
00218 if o == "-V":
00219 print "%s V%s" % (sys.argv[0],VERSION)
00220 sys.exit(0)
00221
00222 if not CTX.CAPFILE and not CTX.PORT:
00223 CTX.PORT = raw_input('Enter Capture Port:')
00224
00225
00226
00227
00228 def process_command(cmd):
00229 ret = 1
00230 cmd = cmd.strip()
00231 if len(cmd) == 0:
00232 pass
00233 elif cmd == "help":
00234 print_help()
00235 elif cmd == "quit":
00236 do_quit()
00237 ret = 0
00238 elif cmd == "info":
00239 inf = DEVIN.info()
00240 ks = inf.keys()
00241 ks.sort()
00242 fmt = " % 9s : %s"
00243 print "INPUT:"
00244 for k in ks:
00245 print fmt % (k,inf[k])
00246 print "OUTPUT:"
00247 print fmt % ("pipe",DEVOUT.TxPipe)
00248 elif cmd == "reopen":
00249 DEVOUT.reopen()
00250 elif cmd.find("chan")>=0:
00251 try:
00252 CTX.CHANNEL = eval(cmd.split(" ")[1])
00253 except:
00254 CTX.CHANNEL = eval(raw_input("channel:"))
00255 DEVIN.set_channel(CTX.CHANNEL)
00256 elif cmd.find("rate")>=0:
00257 CTX.RATE = cmd.split(" ")[1].strip()
00258 DEVIN.set_rate(CTX.RATE)
00259 elif cmd.find("debug")>=0:
00260 pdb.set_trace()
00261 elif cmd.find("N")>=0:
00262 CTX.MAXPACKETS = eval(cmd.split(" ")[1])
00263 DEVIN.FCNT=0
00264 elif cmd.find("devrst")>=0:
00265 DEVIN.init()
00266 DEVIN.set_channel(CTX.CHANNEL)
00267 else:
00268 print "\n Unknown Command: \"%s\"," %cmd
00269 print " type \"help\" to see a list of available commands.\n"
00270 return ret
00271
00272
00273
00274 def do_exit(*argv,**argd):
00275 do_quit()
00276 if os.name != 'posix':
00277 raw_input("Type Enter to Exit...")
00278
00279
00280 if __name__ == "__main__":
00281 try:
00282 import rlcompleter
00283 import readline
00284 readline.parse_and_bind("tab:complete")
00285 except:
00286 pass
00287
00288 atexit.register(do_exit)
00289
00290
00291 process_command_line_args(sys.argv[1:])
00292
00293
00294 if CTX.CAPFILE:
00295 DEVIN = FileIn()
00296 DEVIN.open(CTX.CAPFILE)
00297 elif CTX.PORT:
00298 DEVIN = PortIn()
00299 DEVIN.open(CTX.PORT)
00300 if CTX.CHANNEL != None:
00301 DEVIN.set_channel(CTX.CHANNEL)
00302 if CTX.RATE != None:
00303 DEVIN.set_rate(CTX.RATE)
00304 else:
00305 raise Exception, 'no port or capture file given'
00306
00307
00308 DEVOUT.open(CTX.SOCKNAME, DEVIN)
00309 DEVOUT.setverbose(CTX.VERBOSE)
00310 DEVIN.setverbose(CTX.VERBOSE)
00311 DEVIN.maxpackets = CTX.MAXPACKETS
00312
00313 if CTX.GUI:
00314 gui = CtrlGui(lambda : do_quit(['gui']))
00315 gui.start()
00316 gui.setDEVIN(DEVIN)
00317 else:
00318 gui = None
00319
00320 run = 1
00321 print_banner()
00322 while run:
00323 try:
00324 cmd = raw_input("cmd:")
00325 run = process_command(cmd)
00326 except:
00327 traceback.print_exc()
00328 break