From a7bbb73da607eeac5177ac900ce0e65467690ba2 Mon Sep 17 00:00:00 2001 From: Masatoshi Nishiguchi <7563926+mnishiguchi@users.noreply.github.com> Date: Thu, 21 Mar 2024 16:26:28 +0900 Subject: [PATCH] Add configured?/1 --- lib/vintage_net_wifi.ex | 13 +++++++++++++ test/vintage_net_wifi_test.exs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/lib/vintage_net_wifi.ex b/lib/vintage_net_wifi.ex index c845fad..e249f90 100644 --- a/lib/vintage_net_wifi.ex +++ b/lib/vintage_net_wifi.ex @@ -995,4 +995,17 @@ defmodule VintageNetWiFi do end defp trim_orphan_backslash(s), do: s + + @doc """ + Check if a wlan network interface is configured. + """ + @spec configured?(map | binary) :: boolean + def configured?(wlan_ifname) when is_binary(wlan_ifname) do + wlan_ifname |> VintageNet.get_configuration() |> configured?() + end + + def configured?(wlan_config) when wlan_config == %{type: VintageNetWiFi}, do: false + def configured?(%{vintage_net_wifi: %{networks: []}}), do: false + def configured?(%{vintage_net_wifi: %{networks: [_ | _]}}), do: true + def configured?(_), do: false end diff --git a/test/vintage_net_wifi_test.exs b/test/vintage_net_wifi_test.exs index 89ed118..642bd88 100644 --- a/test/vintage_net_wifi_test.exs +++ b/test/vintage_net_wifi_test.exs @@ -2661,4 +2661,36 @@ defmodule VintageNetWiFiTest do assert expected_output == VintageNetWiFi.summarize_access_points(input) end + + test "Check if wlan is already configured" do + configured = %{ + type: VintageNetWiFi, + ipv4: %{method: :dhcp}, + vintage_net_wifi: %{ + networks: [ + %{ + key_mgmt: :wpa_psk, + ssid: "IEEE", + psk: "F42C6FC52DF0EBEF9EBB4B90B38A5F902E83FE1B135A70E23AED762E9710A12E", + mode: :infrastructure + } + ] + } + } + + empty1 = %{ + type: VintageNetWiFi + } + + empty2 = %{ + type: VintageNetWiFi, + vintage_net_wifi: %{networks: []}, + ipv4: %{method: :disabled} + } + + assert VintageNetWiFi.configured?(configured) + refute VintageNetWiFi.configured?(empty1) + refute VintageNetWiFi.configured?(empty2) + refute VintageNetWiFi.configured?(%{}) + end end