|
| 1 | +from ....utils import get_copy |
| 2 | +from .base import RaspbianConverter |
| 3 | + |
| 4 | + |
| 5 | +class Wireless(RaspbianConverter): |
| 6 | + netjson_key = 'interfaces' |
| 7 | + |
| 8 | + def to_intermediate(self): |
| 9 | + result = [] |
| 10 | + interfaces = get_copy(self.netjson, self.netjson_key) |
| 11 | + for interface in interfaces: |
| 12 | + if interface['type'] == 'wireless' and interface.get('wireless').get('mode') is not 'adhoc': |
| 13 | + new_interface = { |
| 14 | + 'ifname': interface.get('name'), |
| 15 | + 'iftype': interface.get('type'), |
| 16 | + 'ssid': interface['wireless'].get('ssid') |
| 17 | + } |
| 18 | + wireless = interface.get('wireless') |
| 19 | + radio_num = interface['wireless'].get('radio') |
| 20 | + radios = get_copy(self.netjson, 'radios') |
| 21 | + if radios is not None: |
| 22 | + req_radio = [radio for radio in radios if radio['name'] == radio_num][0] |
| 23 | + hwmode = self._get_hwmode(req_radio) |
| 24 | + channel = req_radio['channel'] |
| 25 | + protocol = req_radio['protocol'].replace(".", "") |
| 26 | + new_interface.update({ |
| 27 | + 'hwmode': hwmode, |
| 28 | + 'channel': channel, |
| 29 | + 'protocol': protocol |
| 30 | + }) |
| 31 | + new_interface.update({'encryption': self._get_encryption(wireless)}) |
| 32 | + result.append(new_interface) |
| 33 | + return (('wireless', result),) |
| 34 | + |
| 35 | + def _get_hwmode(self, radio): |
| 36 | + protocol = radio['protocol'] |
| 37 | + if protocol in ['802.11a', '802.11b', '802.11g']: |
| 38 | + return protocol[1:] |
| 39 | + if radio['channel'] is 0: |
| 40 | + return radio.get('hwmode') |
| 41 | + elif radio['channel'] <= 13: |
| 42 | + return 'g' |
| 43 | + else: |
| 44 | + return 'a' |
| 45 | + |
| 46 | + def _get_encryption(self, wireless): |
| 47 | + encryption = wireless.get('encryption', None) |
| 48 | + if encryption is None: |
| 49 | + return {} |
| 50 | + disabled = encryption.get('disabled', False) |
| 51 | + new_encryption = {} |
| 52 | + if encryption.get('protocol') is not 'none' and disabled is not True: |
| 53 | + protocol, method = encryption.get('protocol').split("_") |
| 54 | + if protocol in ['wpa', 'wpa2']: |
| 55 | + auth_algs = '1' |
| 56 | + wpa = '1' if protocol == 'wpa' else '2' |
| 57 | + wpa_key_mgmt = 'WPA-PSK' if method == 'personal' else 'WPA-EAP' |
| 58 | + wpa_passphrase = encryption.get('key') |
| 59 | + new_encryption.update({ |
| 60 | + 'auth_algs': auth_algs, |
| 61 | + 'wpa': wpa, |
| 62 | + 'wpa_key_mgmt': wpa_key_mgmt, |
| 63 | + 'wpa_passphrase': wpa_passphrase |
| 64 | + }) |
| 65 | + if encryption.get('cipher', None) is not None or 'auto': |
| 66 | + wpa_pairwise = str(encryption.get('cipher').replace('+', ' ')).upper() |
| 67 | + new_encryption.update({'wpa_pairwise': wpa_pairwise}) |
| 68 | + return new_encryption |
0 commit comments