1 | """ |
---|
2 | Util classes/methods for managing different hardware platforms supported |
---|
3 | by wlan_exp and the 802.11 ref design |
---|
4 | """ |
---|
5 | |
---|
6 | registered_platforms = [] |
---|
7 | |
---|
8 | class WlanExpPlatform(object): |
---|
9 | """Container class describing wlan_exp hardware platforms""" |
---|
10 | |
---|
11 | name = None |
---|
12 | platform_id = None |
---|
13 | sn_regexp = None |
---|
14 | sn_str_fmt = None |
---|
15 | |
---|
16 | def __init__(self, name, platform_id, sn_regexp, sn_str_fmt): |
---|
17 | self.name = name |
---|
18 | self.platform_id = platform_id |
---|
19 | self.sn_regexp = sn_regexp |
---|
20 | self.sn_str_fmt = sn_str_fmt |
---|
21 | |
---|
22 | def __repr__(self): |
---|
23 | return 'wlan_exp Platform {0} ({1})'.format(self.name, self.get_serial_number_str(12345)) |
---|
24 | |
---|
25 | def get_serial_number_str(self, serial_num_int): |
---|
26 | """ Returns serial number string for numeric serial number |
---|
27 | serial_num_int. This method probably belongs in the top-level |
---|
28 | node object. It's here for now as we work on platform code. |
---|
29 | """ |
---|
30 | return self.sn_str_fmt.format(serial_num_int) |
---|
31 | |
---|
32 | def check_serial_numer(self, sn_str): |
---|
33 | """ |
---|
34 | Returns true if sn_str is a valid serial number for this platform |
---|
35 | Platforms can override this method if they need something more than |
---|
36 | the regexp check implemented here |
---|
37 | """ |
---|
38 | if(self.get_serial_number(sn_str)): |
---|
39 | return True |
---|
40 | else: |
---|
41 | return False |
---|
42 | |
---|
43 | def get_serial_number(self, sn_str): |
---|
44 | """ |
---|
45 | Returns the numeric part of the serial number in sn_str. This is probably |
---|
46 | a temporary method that will be replaced by a better platform-agnostic scheme |
---|
47 | when the transport.node() init and node discovery processes are imrpvoed |
---|
48 | """ |
---|
49 | import re |
---|
50 | |
---|
51 | expr = re.compile(self.sn_regexp) |
---|
52 | m = expr.match(sn_str) |
---|
53 | |
---|
54 | if(m and 'sn' in m.groupdict()): |
---|
55 | # Returns tuple (a,b) |
---|
56 | # a: serial number int |
---|
57 | # b: serial number string in preferred format |
---|
58 | sn_int = int(m.group('sn')) |
---|
59 | return (sn_int, self.get_serial_number_str(sn_int)) |
---|
60 | |
---|
61 | return None |
---|
62 | |
---|
63 | def get_node_info_format(self, platform_config=None): |
---|
64 | """ Returns the platform-specific InfoStruct for the platform node info |
---|
65 | struct returned by nodes in the CMDID_NODE_INFO handshake. |
---|
66 | |
---|
67 | Each platform instance must override or replace this function with |
---|
68 | one that returns an InfoStruct instance. |
---|
69 | """ |
---|
70 | |
---|
71 | raise NotImplementedError('ERROR: platform {} does not define required get_node_info_format() method!'.format(self.name)) |
---|
72 | |
---|
73 | |
---|
74 | def register_platform(new_platform): |
---|
75 | """ Adds new platform description to the global list of registered platforms""" |
---|
76 | |
---|
77 | registered_platforms.append(new_platform) |
---|
78 | |
---|
79 | def lookup_platform_by_serial_num(serial_num): |
---|
80 | """ Checks if serial_num is valid serial number for any registered platforms |
---|
81 | If so returns matching WlanExpPlatform object |
---|
82 | """ |
---|
83 | |
---|
84 | for p in registered_platforms: |
---|
85 | if(p.check_serial_numer(serial_num)): |
---|
86 | return p |
---|
87 | |
---|
88 | return None |
---|
89 | |
---|
90 | def get_platform_node_info_format(platform_id, platform_config): |
---|
91 | """ Searches registered platforms for the platform_id, then returns |
---|
92 | the InfoStruct defined by the matching platform for its Node Info |
---|
93 | """ |
---|
94 | |
---|
95 | for p in registered_platforms: |
---|
96 | if p.platform_id == platform_id: |
---|
97 | return p.get_node_info_format(platform_config) |
---|
98 | |
---|
99 | print('WARNING: no platform node_info found for platform_id {}'.format(platform_id)) |
---|
100 | return None |
---|
101 | |
---|
102 | def get_serial_number(serial_num): |
---|
103 | """ Temporary method to replace old wlan_exp.transport.util.get_serial_num() helper |
---|
104 | This is a half-step towards a more general platform structure |
---|
105 | """ |
---|
106 | p = lookup_platform_by_serial_num(serial_num) |
---|
107 | if(p): |
---|
108 | return p.get_serial_number(serial_num) |
---|
109 | else: |
---|
110 | raise Exception('No platform found for serial number {0}'.format(serial_num)) |
---|