$search
00001 #! /usr/bin/env python 00002 import roslib; roslib.load_manifest('wpa_supplicant_node') 00003 import rospy 00004 import actionlib 00005 import wpa_supplicant_node.msg 00006 from actionlib_msgs.msg import GoalStatus 00007 import os 00008 00009 frequencylist = [5240, 5260, 5805, 2437] 00010 bssidlist = [ 00011 "00:24:6C:81:D5:E8", # willow blaise-office 00012 "00:24:6C:82:4E:F8", # willow lab 00013 #"00:24:6C:81:4E:FA", # willow-wpa2 00014 #"00:24:6C:81:D5:E0", # willow 00015 #"00:24:6C:81:D5:EA", # willow-wpa2 00016 ] 00017 essidlist = [ 00018 #"willow-wpa2", 00019 "blaise-test", 00020 #"willow", 00021 ] 00022 00023 def bssid_to_str(bssid): 00024 return ":".join(["%02X"%ord(c) for c in bssid]) 00025 00026 class Test: 00027 def __init__(self, iface): 00028 self.assoc_tries = 0 00029 self.assoc_successes = 0 00030 self.scan_action = actionlib.SimpleActionClient('wifi/'+iface+"/scan", wpa_supplicant_node.msg.ScanAction) 00031 rospy.loginfo("Waiting for scan server.") 00032 self.scan_action.wait_for_server() 00033 00034 self.assoc_action = actionlib.SimpleActionClient('wifi/'+iface+"/associate", wpa_supplicant_node.msg.AssociateAction) 00035 rospy.loginfo("Waiting for associate server.") 00036 self.assoc_action.wait_for_server() 00037 00038 def scan(self, ssid = [], freq = []): 00039 goal = wpa_supplicant_node.msg.ScanGoal() 00040 for s in ssid: 00041 goal.ssids.append(s) 00042 for f in freq: 00043 goal.frequencies.append(f) 00044 00045 print "Requesting scan..." 00046 rospy.sleep(rospy.Duration(1)) 00047 start = rospy.get_time() 00048 #rospy.loginfo("Sending goal.") 00049 self.scan_action.send_goal(goal) 00050 #print "Requesting scan..." 00051 #rospy.loginfo("Waiting for result.") 00052 self.scan_action.wait_for_result() 00053 end = rospy.get_time() 00054 00055 rospy.loginfo("Scan took %.3f seconds."%(end-start)) 00056 return self.scan_action.get_result() 00057 00058 def associated(self, feedback): 00059 if feedback.associated: 00060 end = rospy.get_time() 00061 print "Association took %.3f seconds."%(end - self.assoc_start), 00062 self.assoc_count += 1 00063 00064 def associate(self, bss): 00065 self.assoc_start = rospy.get_time() 00066 self.assoc_count = 0 00067 self.assoc_tries += 1 00068 00069 #rospy.loginfo("Associating to %s:"%bssid_to_str(bss.bssid)) 00070 print "Associating to %12s %s %4i"%(bss.ssid, bssid_to_str(bss.bssid), bss.frequency), 00071 sys.stdout.flush() 00072 self.assoc_action.send_goal(wpa_supplicant_node.msg.AssociateGoal(bssid = bss.bssid, ssid = bss.ssid), feedback_cb=self.associated) 00073 failed = False 00074 while not self.assoc_count: 00075 state = self.assoc_action.get_state() 00076 if state == GoalStatus.ABORTED or state == GoalStatus.REJECTED: 00077 failed = True 00078 break 00079 rospy.sleep(rospy.Duration(0.1)) 00080 00081 rospy.sleep(rospy.Duration(0.5)) 00082 00083 if not failed: 00084 #rospy.sleep(rospy.Duration(2.2)) 00085 00086 #os.system('ifconfig %s down'%iface) 00087 #os.system('ifconfig %s up'%iface) 00088 self.assoc_action.cancel_goal() 00089 rospy.sleep(rospy.Duration(0.25)) 00090 00091 if self.assoc_count > 1: 00092 print 00093 rospy.logerr("Got multiple association feedbacks.") 00094 00095 if self.assoc_count == 1: 00096 self.assoc_successes += 1 00097 return True 00098 00099 return False 00100 00101 if __name__ == '__main__': 00102 try: 00103 # Initializes a rospy node so that the SimpleActionClient can 00104 # publish and subscribe over ROS. 00105 import sys 00106 iface = sys.argv[1] 00107 rospy.init_node('test_scan'+iface) 00108 t = Test(iface) 00109 count = 0 00110 while not rospy.is_shutdown(): 00111 count += 1 00112 result = t.scan([], frequencylist) 00113 print "Scanned %i bsses."%len(result.bss) 00114 for i in range(0,5): 00115 for bss in result.bss: 00116 if rospy.is_shutdown(): 00117 raise KeyboardInterrupt 00118 if bssid_to_str(bss.bssid) in bssidlist or \ 00119 bss.ssid in essidlist: 00120 #print "Network found. Associating... %s %s %i"%(bss.ssid, bssid_to_str(bss.bssid), bss.frequency) 00121 t.associate(bss) 00122 #if not t.associate(bss): 00123 # raise KeyboardInterrupt 00124 #while t.associate(bss): 00125 # pass 00126 print "Successes: %i/%i"%(t.assoc_successes, t.assoc_tries) 00127 sys.stdout.flush() 00128 #if t.assoc_tries >= 1000: 00129 # raise KeyboardInterrupt 00130 #else: 00131 # print "skipping", bssid_to_str(bss.bssid) 00132 00133 except KeyboardInterrupt: 00134 print "program interrupted before completion" 00135 00136