Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create hotspot #5577

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion supervisor/api/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
ATTR_ACCESSPOINTS,
ATTR_ADDRESS,
ATTR_AUTH,
ATTR_BAND,
ATTR_CHANNEL,
ATTR_CONNECTED,
ATTR_DNS,
ATTR_DOCKER,
Expand Down Expand Up @@ -52,7 +54,7 @@
VlanConfig,
WifiConfig,
)
from ..host.const import AuthMethod, InterfaceType, WifiMode
from ..host.const import AuthMethod, InterfaceType, WifiBand, WifiMode
from .utils import api_process, api_validate

_SCHEMA_IPV4_CONFIG = vol.Schema(
Expand All @@ -79,6 +81,8 @@
vol.Optional(ATTR_AUTH): vol.Coerce(AuthMethod),
vol.Optional(ATTR_SSID): str,
vol.Optional(ATTR_PSK): str,
vol.Optional(ATTR_BAND): vol.Coerce(WifiBand),
vol.Optional(ATTR_CHANNEL): vol.Coerce(int),
jkt628 marked this conversation as resolved.
Show resolved Hide resolved
}
)

Expand Down Expand Up @@ -112,6 +116,8 @@ def wifi_struct(config: WifiConfig) -> dict[str, Any]:
ATTR_AUTH: config.auth,
ATTR_SSID: config.ssid,
ATTR_SIGNAL: config.signal,
ATTR_BAND: config.band,
ATTR_CHANNEL: config.channel,
}


Expand Down Expand Up @@ -227,6 +233,8 @@ async def interface_update(self, request: web.Request) -> None:
config.get(ATTR_AUTH, AuthMethod.OPEN),
config.get(ATTR_PSK, None),
None,
config.get(ATTR_BAND, None),
config.get(ATTR_CHANNEL, None),
jkt628 marked this conversation as resolved.
Show resolved Hide resolved
)
elif key == ATTR_ENABLED:
interface.enabled = config
Expand Down
1 change: 1 addition & 0 deletions supervisor/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
ATTR_BACKUP_PRE = "backup_pre"
ATTR_BACKUPS = "backups"
ATTR_BACKUPS_EXCLUDE_DATABASE = "backups_exclude_database"
ATTR_BAND = "band"
ATTR_BLK_READ = "blk_read"
ATTR_BLK_WRITE = "blk_write"
ATTR_BOARD = "board"
Expand Down
1 change: 1 addition & 0 deletions supervisor/dbus/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ class InterfaceMethod(StrEnum):
MANUAL = "manual"
DISABLED = "disabled"
LINK_LOCAL = "link-local"
SHARED = "shared"


class ConnectionType(StrEnum):
Expand Down
2 changes: 2 additions & 0 deletions supervisor/dbus/network/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class WirelessProperties:
assigned_mac: str | None
mode: str | None
powersave: int | None
band: str | None
channel: int | None


@dataclass(slots=True)
Expand Down
4 changes: 4 additions & 0 deletions supervisor/dbus/network/setting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
CONF_ATTR_802_WIRELESS_ASSIGNED_MAC = "assigned-mac-address"
CONF_ATTR_802_WIRELESS_SSID = "ssid"
CONF_ATTR_802_WIRELESS_POWERSAVE = "powersave"
CONF_ATTR_802_WIRELESS_BAND = "band"
CONF_ATTR_802_WIRELESS_CHANNEL = "channel"
CONF_ATTR_802_WIRELESS_SECURITY_AUTH_ALG = "auth-alg"
CONF_ATTR_802_WIRELESS_SECURITY_KEY_MGMT = "key-mgmt"
CONF_ATTR_802_WIRELESS_SECURITY_PSK = "psk"
Expand Down Expand Up @@ -234,6 +236,8 @@ async def reload(self):
data[CONF_ATTR_802_WIRELESS].get(CONF_ATTR_802_WIRELESS_ASSIGNED_MAC),
data[CONF_ATTR_802_WIRELESS].get(CONF_ATTR_802_WIRELESS_MODE),
data[CONF_ATTR_802_WIRELESS].get(CONF_ATTR_802_WIRELESS_POWERSAVE),
data[CONF_ATTR_802_WIRELESS].get(CONF_ATTR_802_WIRELESS_BAND),
data[CONF_ATTR_802_WIRELESS].get(CONF_ATTR_802_WIRELESS_CHANNEL),
)

if CONF_ATTR_802_WIRELESS_SECURITY in data:
Expand Down
43 changes: 31 additions & 12 deletions supervisor/dbus/network/setting/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
CONF_ATTR_802_ETHERNET_ASSIGNED_MAC,
CONF_ATTR_802_WIRELESS,
CONF_ATTR_802_WIRELESS_ASSIGNED_MAC,
CONF_ATTR_802_WIRELESS_BAND,
CONF_ATTR_802_WIRELESS_CHANNEL,
CONF_ATTR_802_WIRELESS_MODE,
CONF_ATTR_802_WIRELESS_POWERSAVE,
CONF_ATTR_802_WIRELESS_SECURITY,
Expand Down Expand Up @@ -50,6 +52,19 @@
from ....host.configuration import Interface


def _get_address_data(ipv4setting) -> Variant:
address_data = []
for address in ipv4setting.address:
address_data.append(
{
"address": Variant("s", str(address.ip)),
"prefix": Variant("u", int(address.with_prefixlen.split("/")[-1])),
}
)

return Variant("aa{sv}", address_data)


def _get_ipv4_connection_settings(ipv4setting) -> dict:
ipv4 = {}
if not ipv4setting or ipv4setting.method == InterfaceMethod.AUTO:
Expand All @@ -58,19 +73,12 @@ def _get_ipv4_connection_settings(ipv4setting) -> dict:
ipv4[CONF_ATTR_IPV4_METHOD] = Variant("s", "disabled")
elif ipv4setting.method == InterfaceMethod.STATIC:
ipv4[CONF_ATTR_IPV4_METHOD] = Variant("s", "manual")

address_data = []
for address in ipv4setting.address:
address_data.append(
{
"address": Variant("s", str(address.ip)),
"prefix": Variant("u", int(address.with_prefixlen.split("/")[-1])),
}
)

ipv4[CONF_ATTR_IPV4_ADDRESS_DATA] = Variant("aa{sv}", address_data)
ipv4[CONF_ATTR_IPV4_ADDRESS_DATA] = _get_address_data(ipv4setting)
if ipv4setting.gateway:
ipv4[CONF_ATTR_IPV4_GATEWAY] = Variant("s", str(ipv4setting.gateway))
elif ipv4setting.method == InterfaceMethod.SHARED:
ipv4[CONF_ATTR_IPV4_METHOD] = Variant("s", "shared")
ipv4[CONF_ATTR_IPV4_ADDRESS_DATA] = _get_address_data(ipv4setting)
else:
raise RuntimeError("Invalid IPv4 InterfaceMethod")

Expand Down Expand Up @@ -199,13 +207,24 @@ def get_connection_from_interface(
elif interface.type == InterfaceType.WIRELESS:
wireless = {
CONF_ATTR_802_WIRELESS_ASSIGNED_MAC: Variant("s", "preserve"),
CONF_ATTR_802_WIRELESS_MODE: Variant("s", "infrastructure"),
CONF_ATTR_802_WIRELESS_MODE: Variant(
"s",
interface.wifi.mode
if interface.wifi and interface.wifi.mode
else "infrastructure",
),
CONF_ATTR_802_WIRELESS_POWERSAVE: Variant("i", 1),
}
if interface.wifi and interface.wifi.ssid:
wireless[CONF_ATTR_802_WIRELESS_SSID] = Variant(
"ay", interface.wifi.ssid.encode("UTF-8")
)
if interface.wifi and interface.wifi.band:
wireless[CONF_ATTR_802_WIRELESS_BAND] = Variant("s", interface.wifi.band)
if interface.wifi and interface.wifi.channel:
wireless[CONF_ATTR_802_WIRELESS_CHANNEL] = Variant(
"u", interface.wifi.channel
)
jkt628 marked this conversation as resolved.
Show resolved Hide resolved

conn[CONF_ATTR_802_WIRELESS] = wireless

Expand Down
15 changes: 13 additions & 2 deletions supervisor/host/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)
from ..dbus.network.connection import NetworkConnection
from ..dbus.network.interface import NetworkInterface
from .const import AuthMethod, InterfaceMethod, InterfaceType, WifiMode
from .const import AuthMethod, InterfaceMethod, InterfaceType, WifiBand, WifiMode


@dataclass(slots=True)
Expand Down Expand Up @@ -55,6 +55,8 @@ class WifiConfig:
auth: AuthMethod
psk: str | None
signal: int | None
band: WifiBand | None
channel: int | None


@dataclass(slots=True)
Expand Down Expand Up @@ -191,6 +193,7 @@ def _map_nm_method(method: str) -> InterfaceMethod:
NMInterfaceMethod.DISABLED: InterfaceMethod.DISABLED,
NMInterfaceMethod.MANUAL: InterfaceMethod.STATIC,
NMInterfaceMethod.LINK_LOCAL: InterfaceMethod.DISABLED,
NMInterfaceMethod.SHARED: InterfaceMethod.SHARED,
}

return mapping.get(method, InterfaceMethod.DISABLED)
Expand Down Expand Up @@ -237,6 +240,12 @@ def _map_nm_wifi(inet: NetworkInterface) -> WifiConfig | None:
if inet.settings.wireless.mode:
mode = WifiMode(inet.settings.wireless.mode)

# Band and Channel
band = channel = None
if mode == WifiMode.AP:
band = WifiBand(inet.settings.wireless.band)
channel = inet.settings.wireless.channel

jkt628 marked this conversation as resolved.
Show resolved Hide resolved
# Signal
if inet.wireless and inet.wireless.active:
signal = inet.wireless.active.strength
Expand All @@ -249,10 +258,12 @@ def _map_nm_wifi(inet: NetworkInterface) -> WifiConfig | None:
auth,
psk,
signal,
band,
channel,
)

@staticmethod
def _map_nm_vlan(inet: NetworkInterface) -> WifiConfig | None:
def _map_nm_vlan(inet: NetworkInterface) -> VlanConfig | None:
"""Create mapping to nm vlan property."""
if inet.type != DeviceType.VLAN or not inet.settings:
return None
Expand Down
8 changes: 8 additions & 0 deletions supervisor/host/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class InterfaceMethod(StrEnum):
DISABLED = "disabled"
STATIC = "static"
AUTO = "auto"
SHARED = "shared"


class InterfaceType(StrEnum):
Expand All @@ -31,6 +32,13 @@ class AuthMethod(StrEnum):
WPA_PSK = "wpa-psk"


class WifiBand(StrEnum):
"""Wifi band."""

A = "a"
BG = "bg"


class WifiMode(StrEnum):
"""Wifi mode."""

Expand Down
25 changes: 25 additions & 0 deletions tests/api/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,31 @@ async def test_api_network_interface_update_wifi_error(api_client: TestClient):
)


async def test_api_network_interface_update_wifi_bad_channel(api_client: TestClient):
"""Test network interface WiFi API error handling for bad channel."""
# Simulate frontend WiFi interface edit where the user selects a bad channel.
resp = await api_client.post(
f"/network/interface/{TEST_INTERFACE_WLAN_NAME}/update",
json={
"enabled": True,
"ipv4": {
"method": "shared",
"address": ["10.42.0.1/24"],
},
"ipv6": {
"method": "auto",
},
"wifi": {"mode": "ap", "ssid": "HotSpot", "band": "bg", "channel": 17},
},
)
result = await resp.json()
assert result["result"] == "error"
assert (
result["message"]
== "Can't create config and activate wlan0: 802-11-wireless.channel: '17' is not a valid channel"
)


async def test_api_network_interface_update_remove(api_client: TestClient):
"""Test network manager api."""
resp = await api_client.post(
Expand Down
9 changes: 9 additions & 0 deletions tests/dbus_service_mocks/network_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,15 @@ def AddAndActivateConnection(
"org.freedesktop.NetworkManager.Device.InvalidConnection",
"A 'wireless' setting with a valid SSID is required if no AP path was given.",
)
if (
"channel" in connection["802-11-wireless"]
and connection["802-11-wireless"]["channel"].value > 14
):
raise DBusError(
"org.freedesktop.NetworkManager.Device.InvalidConnection",
# this is the actual error from NetworkManager
f"802-11-wireless.channel: '{connection['802-11-wireless']['channel'].value}' is not a valid channel",
)
jkt628 marked this conversation as resolved.
Show resolved Hide resolved

return [
"/org/freedesktop/NetworkManager/Settings/1",
Expand Down