Adding new wifi without discarding existing

Hello,
I’m working on wlan with vintagenet.
I’m able to configure the interface for using multiple networks, however if i want to add new network, existing networks are discarded, is there any way to add new wifi without discarding existing ones?
This is how i configure 2 wifi networks, device is able to connect to whichever is available

VintageNet.configure("wlan0", %{
       type: VintageNetWiFi,
       vintage_net_wifi: %{
         networks: [
           %{
             key_mgmt: :wpa_psk,
             psk: <psk>,
             ssid: "TEST1"
           },
           %{
             key_mgmt: :wpa_psk,
             psk: <psk>,
             ssid: "Hotspot1"
           }
         ]
       },
       ipv4: %{method: :dhcp}
     })

Now if i try to add one more network using VintageNet.configure() existing networks are discarded. Any suggestion is welcomed
Regards,

Hi,
something like this should help:

 @wifi_ifname "wlan0"
 
 def get_wifi_config, do: VintageNet.get_configuration(@wifi_ifname)

 def configure_wifi(%{ssid: ssid, password: password}) do
    %{vintage_net_wifi: %{networks: networks}} = config = get_wifi_config()

    VintageNet.configure(
      @wifi_ifname,
      put_in(config, [:vintage_net_wifi, :networks], [
        %{key_mgmt: :wpa_psk, ssid: ssid, psk: password}
        | Enum.reject(networks, &(&1.ssid == ssid))
      ]),
      persist: false
    )
  end

You can of course remove persist: false based on your needs.

Cheers,
Sébastien.

3 Likes

VintageNet.configure/2 is a full config replacement. Anything preexisting that is not included in the new config will be overwritten. @seb3s solution or similar would work to fetch the current config, update it, and rewrite

Hi,

@seb3s Thank you for your help, with this i was able to add new network while keeping the existing configuration as is.

Regards

1 Like