Can't reapply changes to '802-11-wireless.ssid' setting

72 Views Asked by At

I am using GObject's python introspection for libnm to write a program that changes connection settings via NetworkManager. In this case, the user will enter the SSID and password of an Access Point that the machine needs to connect to, and the program should create a new connection profile and configure it to connect to the destined Access Point, or, if an existing connection profile is found, modify that connection profile with the new credentials (SSID and password) and reapply the configurations.

    def update_wireless_connection(self, device):
        def reapply_cb(device, result, data):
            device.reapply_finish(result)
            self.main_loop.quit()

        def get_applied_profile_cb(device, result, data):
            (profile, v) = device.get_applied_connection_finish(result)

            ssid = GLib.Bytes.new(self.target_ap_ssid.encode("utf-8"))

            s_wireless = NM.SettingWireless.new()
            s_wireless.set_property(NM.SETTING_WIRELESS_SSID, ssid)
            s_wireless.set_property(NM.SETTING_WIRELESS_MODE, "infrastructure")

            s_wsec = NM.SettingWirelessSecurity.new()
            s_wsec.set_property(NM.SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-psk")
            s_wsec.set_property(NM.SETTING_WIRELESS_SECURITY_PSK, self.target_ap_pw)

            profile.add_setting(s_wireless)
            profile.add_setting(s_wsec)

            device.reapply_async(profile, 0, 0, None, reapply_cb, None)
            print("waiting for settings to be applied...")
        
        def deactivate_con_cb(device, result, data):
            device.deactivate_connection_finish(result)
            self.main_loop.quit()
       
        device.get_applied_connection_async(0, None, get_applied_profile_cb, None)
        self.main_loop.run()

The above code intends to apply a new connection configuration into an existing connection profile. However this error always arise:

Traceback (most recent call last):
  File "src/wifi-settings/main.py", line 149, in reapply_cb
    device.reapply_finish(result)
gi.repository.GLib.Error: nm-device-error-quark: Can't reapply changes to '802-11-wireless.ssid' setting (3)

For the record, I can create a new profile, apply all the settings and connect to the Access Point with no problem at all.

I would like to understand the correct way of modifying an existing NetworkManager connection, e.g. do I need to disconnect/deactivate the connection before I modify it? From other examples online and experience with nmcli this should not be necessary.

0

There are 0 best solutions below