Skip to content

Commit

Permalink
Improved wireless definition #27
Browse files Browse the repository at this point in the history
The wireless section should be an object, not an array (the NetJSON spec needs to be fixed).
Multiple SSIDs should be defined by defining more wireless interfaces.
Wireless interfaces in /etc/config/wireless should contain the ifname attribute.

Closes #27
  • Loading branch information
nemesifier committed Nov 4, 2015
1 parent d5ea68d commit 493a3ac
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 285 deletions.
89 changes: 44 additions & 45 deletions netjsonconfig/backends/openwrt/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,51 +271,50 @@ def _get_wifi_interfaces(self):
# results container
uci_wifi_ifaces = []
for wifi_interface in wifi_interfaces:
# each wireless interface
# can have multiple SSIDs
wireless_interfaces = wifi_interface['wireless']
for wireless in wireless_interfaces:
# prepare UCI wifi-iface directive
uci_wifi = wireless.copy()
# rename radio to device
uci_wifi['device'] = wireless['radio']
del uci_wifi['radio']
# map netjson wifi modes to uci wifi modes
modes = {
'access_point': 'ap',
'station': 'sta',
'adhoc': 'adhoc',
'wds': 'wds',
'monitor': 'monitor',
'802.11s': 'mesh'
}
uci_wifi['mode'] = modes[wireless['mode']]
# map advanced 802.11 netjson attributes to UCI
wifi_options = {
'ack_distance': 'distance',
'rts_threshold': 'rts',
'frag_threshold': 'frag'
}
for netjson_key, uci_key in wifi_options.items():
if wireless.get(netjson_key) is not None:
uci_wifi[uci_key] = wireless[netjson_key]
del uci_wifi[netjson_key]
# determine encryption for wifi
if uci_wifi.get('encryption'):
del uci_wifi['encryption']
uci_encryption = self.__get_encryption(wireless)
uci_wifi.update(uci_encryption)
# attached networks (openwrt specific)
# by default the wifi interface is attached
# to its defining interface
# but this behaviour can be overridden
if not uci_wifi.get('network'):
# get network, default to ifname
network = wifi_interface.get('network', wifi_interface['name'])
uci_wifi['network'] = [network]
uci_wifi['network'] = ' '.join(uci_wifi['network'])\
.replace('.', '_')
uci_wifi_ifaces.append(sorted_dict(uci_wifi))
wireless = wifi_interface['wireless']
# prepare UCI wifi-iface directive
uci_wifi = wireless.copy()
# add ifname
uci_wifi['ifname'] = wifi_interface['name']
# rename radio to device
uci_wifi['device'] = wireless['radio']
del uci_wifi['radio']
# map netjson wifi modes to uci wifi modes
modes = {
'access_point': 'ap',
'station': 'sta',
'adhoc': 'adhoc',
'wds': 'wds',
'monitor': 'monitor',
'802.11s': 'mesh'
}
uci_wifi['mode'] = modes[wireless['mode']]
# map advanced 802.11 netjson attributes to UCI
wifi_options = {
'ack_distance': 'distance',
'rts_threshold': 'rts',
'frag_threshold': 'frag'
}
for netjson_key, uci_key in wifi_options.items():
if wireless.get(netjson_key) is not None:
uci_wifi[uci_key] = wireless[netjson_key]
del uci_wifi[netjson_key]
# determine encryption for wifi
if uci_wifi.get('encryption'):
del uci_wifi['encryption']
uci_encryption = self.__get_encryption(wireless)
uci_wifi.update(uci_encryption)
# attached networks (openwrt specific)
# by default the wifi interface is attached
# to its defining interface
# but this behaviour can be overridden
if not uci_wifi.get('network'):
# get network, default to ifname
network = wifi_interface.get('network', wifi_interface['name'])
uci_wifi['network'] = [network]
uci_wifi['network'] = ' '.join(uci_wifi['network'])\
.replace('.', '_')
uci_wifi_ifaces.append(sorted_dict(uci_wifi))
return uci_wifi_ifaces

def __get_encryption(self, wireless):
Expand Down
18 changes: 8 additions & 10 deletions netjsonconfig/backends/openwrt/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,14 @@
"wireless_interface": {
"properties": {
"wireless": {
"items": {
"properties": {
"network": {
"type": "array",
"uniqueItems": True,
"additionalItems": True,
"minItems": 1,
"items": {
"type": "string"
}
"properties": {
"network": {
"type": "array",
"uniqueItems": True,
"additionalItems": True,
"minItems": 1,
"items": {
"type": "string"
}
}
}
Expand Down
165 changes: 79 additions & 86 deletions netjsonconfig/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,92 +109,85 @@
{
"properties": {
"wireless": {
"type": "array",
"title": "Wireless interfaces",
"uniqueItems": True,
"additionalItems": True,
"items": {
"type": "object",
"title": "Wireless Interface",
"additionalProperties": True,
"required": [
"radio",
"mode",
"ssid"
],
"properties": {
"radio": {
"type": "string"
},
"mode": {
"type": "string",
"enum": [
"access_point",
"station",
"adhoc",
"wds",
"monitor",
"802.11s"
]
},
"ssid": {
"type": "string",
"maxLength": 32
},
"bssid": {

"type": "string"
},
"hidden": {
"type": "boolean",
"default": False
},
"ack_distance": {
"type": "integer",
"minimum": 1
},
"rts_threshold": {
"type": "integer",
"minimum": 0,
"maximum": 2346
},
"frag_threshold": {
"type": "integer",
"minimum": 0,
"maximum": 2346
},
"encryption": {
"type": "object",
"title": "Encryption",
"required": [
"enabled",
"protocol",
"key"
],
"properties": {
"enabled": {
"type": "boolean"
},
"protocol": {
"type": "string",
"enum": [
"wep_open",
"wep_shared",
"wpa_personal",
"wpa2_personal",
"wpa_personal_mixed",
"wpa_enterprise",
"wpa2_enterprise",
"wpa_enterprise_mixed",
"wps"
]
},
"ciphers": {
"type": "array"
},
"key": {
"type": "string"
}
"type": "object",
"title": "Wireless Interface",
"additionalProperties": True,
"required": [
"radio",
"mode",
"ssid"
],
"properties": {
"radio": {
"type": "string"
},
"mode": {
"type": "string",
"enum": [
"access_point",
"station",
"adhoc",
"wds",
"monitor",
"802.11s"
]
},
"ssid": {
"type": "string",
"maxLength": 32
},
"bssid": {
"type": "string"
},
"hidden": {
"type": "boolean",
"default": False
},
"ack_distance": {
"type": "integer",
"minimum": 1
},
"rts_threshold": {
"type": "integer",
"minimum": 0,
"maximum": 2346
},
"frag_threshold": {
"type": "integer",
"minimum": 0,
"maximum": 2346
},
"encryption": {
"type": "object",
"title": "Encryption",
"required": [
"enabled",
"protocol",
"key"
],
"properties": {
"enabled": {
"type": "boolean"
},
"protocol": {
"type": "string",
"enum": [
"wep_open",
"wep_shared",
"wpa_personal",
"wpa2_personal",
"wpa_personal_mixed",
"wpa_enterprise",
"wpa2_enterprise",
"wpa_enterprise_mixed",
"wps"
]
},
"ciphers": {
"type": "array"
},
"key": {
"type": "string"
}
}
}
Expand Down
29 changes: 13 additions & 16 deletions tests/openwrt/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,12 @@ def test_generate(self):
"family": "ipv4"
}
],
"wireless": [
{
"radio": "radio0",
"mode": "access_point",
"ssid": "MyWifiAP",
"hidden": True
}
]
"wireless": {
"radio": "radio0",
"mode": "access_point",
"ssid": "MyWifiAP",
"hidden": True
}
}
],
"radios": [
Expand Down Expand Up @@ -165,6 +163,7 @@ def test_generate(self):
config wifi-iface
option device 'radio0'
option hidden '1'
option ifname 'wlan0'
option mode 'ap'
option network 'wlan0'
option ssid 'MyWifiAP'
Expand Down Expand Up @@ -222,14 +221,12 @@ def test_templates(self):
"family": "ipv4"
}
],
"wireless": [
{
"radio": "radio0",
"mode": "access_point",
"ssid": "MyWifiAP",
"hidden": True
}
]
"wireless": {
"radio": "radio0",
"mode": "access_point",
"ssid": "MyWifiAP",
"hidden": True
}
}
],
"radios": [
Expand Down
Loading

0 comments on commit 493a3ac

Please sign in to comment.