Wireless Connection Help



Hello all,
I am looking for some help with using NetworkManager in a c++ application.

I am able to use network manager to scan the list of ap's and also go through the list of connections.  I think if the network is already listed in the connection list I need to activate it instead of trying to add it.  

However what I am missing is how do I activate the wireless connection and how do I set the WPA2 key (or other key depending on the security used) I have not found any code examples on how to use ActivateConnection or AddAndActivateConnection2 methods.  

Below was an attempt I made at setting up the connection (borrowed heavily from
https://github.com/lcp/NetworkManager/blob/master/examples/C/glib/add-connection-libnm-glib.c )
 
gboolean NetworkSettings::add_connection(NMRemoteSettings *settingsGMainLoop *loopconst char *con_namestd::string SSIDstd::string sKey)
{
   NMConnection *connection;
   NMSettingConnection *s_con;
   NMSettingWireless *s_wireless;
   NMSettingIP4Config *s_ip4;
   char *uuid;
   gboolean success;

   // Create a new connection object 
   connection = nm_connection_new();

   // Build up the 'connection' Setting 
   s_con = (NMSettingConnection *)nm_setting_connection_new();
   uuid = nm_utils_uuid_generate();
   g_object_set(G_OBJECT(s_con),
                 NM_SETTING_CONNECTION_UUID, uuid,
                 NM_SETTING_CONNECTION_ID, con_name,
                 NM_SETTING_CONNECTION_TYPE, "802-11-wireless",
                 NULL);
   g_free(uuid);
   nm_connection_add_setting(connection, NM_SETTING(s_con));

   // Build up the 'wireless' Setting 
   s_wireless = (NMSettingWireless *)nm_setting_wireless_new();
   g_object_set(G_OBJECT(s_wireless),
      NM_SETTING_WIRELESS_SSID, SSID.c_str(),
      NM_SETTING_WIRELESS_SEC, sKey.c_str(),
      NULL);
   nm_connection_add_setting(connection, NM_SETTING(s_wireless));

   // Build up the 'ipv4' Setting
   s_ip4 = (NMSettingIP4Config *)nm_setting_ip4_config_new();
   g_object_set(G_OBJECT(s_ip4),
                 NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO,
                 NULL);
   nm_connection_add_setting(connection, NM_SETTING(s_ip4));

   // Ask the settings service to add the new connection; we'll quit the
   // mainloop and exit when the callback is called.
   success = nm_remote_settings_add_connection(settings, connection, NetworkSettings::added_cb, loop);
   if(!success)
      g_print("Error adding connection\n");

   g_object_unref(connection);
   return success;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]