Move the osso wrapper out of the wifimap module
[wifihood] / wifimap / wifiscan.py
1
2 import osso
3
4 import time
5
6 import gobject
7
8 import os
9
10 class WifiScanner ( gobject.GObject ) :
11
12     def __init__ ( self , ifname="wlan0" ) :
13         gobject.GObject.__init__( self )
14         self.osso_context = osso.Context("wifi_scanner", "2.0", False)
15         osso_rpc = osso.Rpc(self.osso_context)
16         osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "wakeup", wait_reply = True)
17         self.scan_timeout = 0
18
19         # Values to be set by wireless scans
20         self.scanlist = {}
21         self.tstamp = 0
22         self.nscan = 0
23         self.nfp = 0
24
25     def start ( self , timeout=5000 ) :
26         osso_rpc = osso.Rpc(self.osso_context)
27         osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "start")
28         self.scan_timeout = timeout
29
30     def stop ( self ) :
31         osso_rpc = osso.Rpc(self.osso_context)
32         osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "stop")
33         self.scan_timeout = 0
34
35     def scan ( self ) :
36         osso_rpc = osso.Rpc(self.osso_context)
37         try :
38             scan_out = osso_rpc.rpc_run("org.javiplx.wifiscan", "/org/javiplx/wifiscan", "org.javiplx.wifiscan", "scan", wait_reply = True)
39             if self.scan_timeout :
40                 gobject.timeout_add( self.scan_timeout , self.scan )
41             # BUG : if scan is called after stop (maybe in other cases), 'ERROR' is returned and split raises exception
42             self.nscan +=1
43             self.scanlist.clear()
44             self.tstamp = time.time()
45             for net in scan_out.split() :
46                 self.nfp += 1
47                 items = net.rsplit(":", 1)
48                 self.scanlist[ items[0] ] = int(items[1])
49         except Exception , ex :
50             osso.SystemNote(self.osso_context).system_note_infoprint("Exception scanning %s" % ex )
51
52     def report ( self ) :
53         return "%d scan\t%d fp" % ( self.nscan , self.nfp )
54
55
56 gobject.type_register(WifiScanner)
57
58 if __name__ == "__main__" :
59     loop = gobject.MainLoop()
60     sample = WifiScanner()
61     sample.start()                                                        
62     def show_scan(sample):
63         gobject.timeout_add( 5000 , show_scan , sample )
64         print "scan results : %s" % sample.report()
65         print "  tstamp %s" % sample.tstamp
66         c = 0
67         for k,v in sample.scanlist.iteritems() :
68            c += 1
69            print "    %s %s" % ( k , v )
70            if c > 5 :
71                print "    ..."
72                break
73         print
74     sample.scan()                                                        
75     gobject.timeout_add( 5100 , show_scan , sample )
76     loop.run()                                       
77     sample.stop()                                 
78