$search
00001 #! /usr/bin/env python 00002 00003 import re 00004 00005 def same(s1, s2): 00006 return to_packed(s1) == to_packed(s2) 00007 00008 def is_packed(pk): 00009 return type(pk) == str and len(pk) == 6 00010 00011 def to_packed(s): 00012 if is_packed(s): 00013 return s 00014 return str_to_packed(s) 00015 00016 def to_str(s): 00017 if is_str(s): 00018 return s 00019 return packed_to_str(s) 00020 00021 def packed_to_str(pk): 00022 if is_packed(pk): 00023 return ":".join(("%02x"%ord(pk[i]) for i in range(6))) 00024 raise ValueError("A packed MAC address is a 6 character string.") 00025 00026 str_re_str = '^%s$'%(':'.join(6*[2*'[0-9A-Fa-f]'])) 00027 str_re = re.compile(str_re_str) 00028 00029 def is_str(str): 00030 return str_re.search(str) is not None 00031 00032 def str_to_packed(str): 00033 if is_str(str): 00034 return "".join(chr(int(h,16)) for h in str.split(':')) 00035 raise ValueError("A MAC address string is 6 two digit hex numbers separated by colons.") 00036 00037 def make_special(): 00038 out = {} 00039 base_macs = { 00040 '00:24:6C:81:C2:2' : "01 Bike Rack", 00041 '00:24:6C:82:3C:9' : "02 Research Mid", 00042 '00:24:6C:82:48:1' : "03 Research Corner", 00043 '00:24:6C:82:3A:0' : "04 Pool Room", 00044 '00:24:6C:82:2F:3' : "05 Sanford Office", 00045 '00:24:6C:81:B9:F' : "06 Boondocks", 00046 '00:24:6C:81:BF:8' : "07 Kevin Corner", 00047 '00:24:6C:82:54:B' : "08 Front Lobby", 00048 '00:24:6C:82:45:E' : "09 Cafe", 00049 '00:24:6C:82:24:8' : "10 Steve Office", 00050 '00:24:6C:81:D5:E' : "11 Green Room", 00051 '00:24:6C:82:4E:F' : "12 Server Room", 00052 } 00053 variants = { 00054 "0" : "B", 00055 "1" : "B-WEP", 00056 "2" : "B-WPA2", 00057 "8" : "A", 00058 "9" : "A-WEP", 00059 "A" : "A-WPA2", 00060 } 00061 for mac, descr in base_macs.iteritems(): 00062 for var, vdescr in variants.iteritems(): 00063 out[mac+var] = descr + " " + vdescr 00064 return out 00065 00066 special_macs = make_special() 00067 00068 def pretty(str): 00069 if is_str(str): 00070 mac = str 00071 elif is_packed(str): 00072 mac = packed_to_str(str) 00073 else: 00074 mac = None 00075 00076 if mac: 00077 mac = mac.upper() 00078 if mac in special_macs: 00079 out = special_macs[mac] 00080 else: 00081 out = mac 00082 else: 00083 out = "Invalid MAC" 00084 00085 return out 00086 00087 if __name__ == "__main__": 00088 import unittest 00089 import sys 00090 00091 class BasicTest(unittest.TestCase): 00092 def test_bad_packed(self): 00093 self.assertRaises(ValueError, packed_to_str, "12345") 00094 self.assertRaises(ValueError, packed_to_str, "1234567") 00095 self.assertRaises(ValueError, packed_to_str, 12) 00096 self.assertRaises(ValueError, packed_to_str, []) 00097 00098 def test_bad_str(self): 00099 self.assertRaises(ValueError, str_to_packed, "12:34:56:78:90") 00100 self.assertRaises(ValueError, str_to_packed, "12:34:56:78:90:ab:cd") 00101 self.assertRaises(ValueError, str_to_packed, "aoeu") 00102 self.assertRaises(ValueError, str_to_packed, "gg:12:34:56:78:90") 00103 00104 def test_conv(self): 00105 self.assertEqual("blaise", str_to_packed("62:6c:61:69:73:65")) 00106 self.assertEqual("blaise", str_to_packed("62:6C:61:69:73:65")) 00107 self.assertEqual("blaise", str_to_packed(packed_to_str("blaise"))) 00108 self.assertEqual("ct,cu ", str_to_packed(packed_to_str("ct,cu "))) 00109 00110 if len(sys.argv) > 1 and sys.argv[1].startswith("--gtest_output="): 00111 import roslib; roslib.load_manifest('multi_interface_roam') 00112 import rostest 00113 rostest.unitrun('multi_interface_roam', 'addr_basic', BasicTest) 00114 else: 00115 unittest.main()