Files
NetworkManager/test/nmclienttest.c
Dan Williams f9b8cb84d1 2004-08-11 Dan Williams <dcbw@redhat.com>
* panel-applet/NMWirelessApplet.[ch]
		- Fix up copyright and credits to include Bastien and Eskil,
			who created the gnome-applets wireless applet, from whose
			skeleton this one was created
		- Rework nmwa_update_state()/nmwa_draw() so that state and which
			pixmap to draw is computed during nmwa_update_state()
		- Applet now shows itself all the time due to panel packing issues
			which caused the applet to previously never come back after hiding.
			When a wired device is the active device, the applet shows "not connected"

	* panel-applet/NMWirelessAppletDbus.[ch]
		- Clean up error messages and show what function they are from
		- nmwa_dbus_get_active_wireless_device()->nmwa_dbus_get_active_device()
		- Add new device type getters, and a status getter

	* src/NetworkManagerDbus.c
		- (nm_dbus_devices_handle_request): Don't return an active network unless that
			network is actually in the device's ap list
		- (nm_dbus_nm_message_handler): Fix silly mistake returning status

	* src/NetworkManagerDevice.c
		- (nm_device_update_best_ap): If the best AP is NULL, clear out the ESSID of the
			card

	* test/nmclienttest.c
		- Report status of NetworkManager too


git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@47 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
2004-08-11 19:31:41 +00:00

508 lines
12 KiB
C

/* nmclienttest - test app for NetworkManager
*
* Dan Williams <dcbw@redhat.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* (C) Copyright 2004 Red Hat, Inc.
*/
#include <glib.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <stdio.h>
char * get_active_device (DBusConnection *connection)
{
DBusMessage *message;
DBusMessage *reply;
DBusMessageIter iter;
DBusError error;
char *device_path;
message = dbus_message_new_method_call ("org.freedesktop.NetworkManager",
"/org/freedesktop/NetworkManager",
"org.freedesktop.NetworkManager",
"getActiveDevice");
if (message == NULL)
{
fprintf (stderr, "Couldn't allocate the dbus message\n");
return;
}
dbus_error_init (&error);
reply = dbus_connection_send_with_reply_and_block (connection, message, -1, &error);
if (dbus_error_is_set (&error))
{
fprintf (stderr, "%s raised:\n %s\n\n", error.name, error.message);
dbus_message_unref (message);
return;
}
if (reply == NULL)
{
fprintf( stderr, "dbus reply message was NULL\n" );
dbus_message_unref (message);
return;
}
/* now analyze reply */
dbus_message_iter_init (reply, &iter);
char *string;
string = dbus_message_iter_get_string (&iter);
if (!string)
{
fprintf (stderr, "NetworkManager returned a NULL active device object path" );
return;
}
fprintf (stderr, "Active device: '%s'\n", string );
dbus_message_unref (reply);
dbus_message_unref (message);
device_path = g_strdup (string);
return (device_path);
}
void get_device_name (DBusConnection *connection, char *path)
{
DBusMessage *message;
DBusMessage *reply;
DBusMessageIter iter;
DBusError error;
message = dbus_message_new_method_call ("org.freedesktop.NetworkManager",
path,
"org.freedesktop.NetworkManager",
"getName");
if (message == NULL)
{
fprintf (stderr, "Couldn't allocate the dbus message\n");
return;
}
dbus_error_init (&error);
reply = dbus_connection_send_with_reply_and_block (connection, message, -1, &error);
if (dbus_error_is_set (&error))
{
fprintf (stderr, "%s raised:\n %s\n\n", error.name, error.message);
dbus_message_unref (message);
return;
}
if (reply == NULL)
{
fprintf( stderr, "dbus reply message was NULL\n" );
dbus_message_unref (message);
return;
}
/* now analyze reply */
dbus_message_iter_init (reply, &iter);
char *string;
string = dbus_message_iter_get_string (&iter);
if (!string)
{
fprintf (stderr, "NetworkManager returned a NULL active device object path" );
return;
}
fprintf (stderr, "Active device name: '%s'\n", string );
dbus_message_unref (reply);
dbus_message_unref (message);
}
void get_nm_status (DBusConnection *connection)
{
DBusMessage *message;
DBusMessage *reply;
DBusMessageIter iter;
DBusError error;
message = dbus_message_new_method_call ("org.freedesktop.NetworkManager",
"/org/freedesktop/NetworkManager",
"org.freedesktop.NetworkManager",
"status");
if (message == NULL)
{
fprintf (stderr, "Couldn't allocate the dbus message\n");
return;
}
dbus_error_init (&error);
reply = dbus_connection_send_with_reply_and_block (connection, message, -1, &error);
if (dbus_error_is_set (&error))
{
fprintf (stderr, "%s raised:\n %s\n\n", error.name, error.message);
dbus_message_unref (message);
return;
}
if (reply == NULL)
{
fprintf( stderr, "dbus reply message was NULL\n" );
dbus_message_unref (message);
return;
}
/* now analyze reply */
dbus_message_iter_init (reply, &iter);
char *string;
string = dbus_message_iter_get_string (&iter);
if (!string)
{
fprintf (stderr, "NetworkManager returned a NULL status" );
return;
}
fprintf (stderr, "NM Status: '%s'\n", string );
dbus_message_unref (reply);
dbus_message_unref (message);
}
void get_device_active_network (DBusConnection *connection, char *path)
{
DBusMessage *message;
DBusMessage *reply;
DBusMessageIter iter;
DBusError error;
message = dbus_message_new_method_call ("org.freedesktop.NetworkManager",
path,
"org.freedesktop.NetworkManager",
"getActiveNetwork");
if (message == NULL)
{
fprintf (stderr, "Couldn't allocate the dbus message\n");
return;
}
dbus_error_init (&error);
reply = dbus_connection_send_with_reply_and_block (connection, message, -1, &error);
if (dbus_error_is_set (&error))
{
fprintf (stderr, "%s raised:\n %s\n\n", error.name, error.message);
dbus_message_unref (message);
return;
}
if (reply == NULL)
{
fprintf( stderr, "dbus reply message was NULL\n" );
dbus_message_unref (message);
return;
}
/* now analyze reply */
dbus_message_iter_init (reply, &iter);
char *string;
string = dbus_message_iter_get_string (&iter);
if (!string)
{
fprintf (stderr, "NetworkManager returned a NULL active device object path" );
return;
}
fprintf (stderr, "Active device's Network: '%s' ", string );
dbus_message_unref (reply);
dbus_message_unref (message);
message = dbus_message_new_method_call ("org.freedesktop.NetworkManager",
string,
"org.freedesktop.NetworkManager",
"getName");
if (message == NULL)
{
fprintf (stderr, "Couldn't allocate the dbus message\n");
return;
}
dbus_error_init (&error);
reply = dbus_connection_send_with_reply_and_block (connection, message, -1, &error);
if (dbus_error_is_set (&error))
{
fprintf (stderr, "%s raised:\n %s\n\n", error.name, error.message);
dbus_message_unref (message);
return;
}
if (reply == NULL)
{
fprintf( stderr, "dbus reply message was NULL\n" );
dbus_message_unref (message);
return;
}
/* now analyze reply */
dbus_message_iter_init (reply, &iter);
string = dbus_message_iter_get_string (&iter);
if (!string)
{
fprintf (stderr, "NetworkManager returned a NULL active device object path" );
return;
}
fprintf (stderr, " (%s)\n", string );
dbus_message_unref (reply);
dbus_message_unref (message);
}
int get_device_type (DBusConnection *connection, char *path)
{
DBusMessage *message;
DBusMessage *reply;
DBusMessageIter iter;
DBusError error;
message = dbus_message_new_method_call ("org.freedesktop.NetworkManager",
path,
"org.freedesktop.NetworkManager",
"getType");
if (message == NULL)
{
fprintf (stderr, "Couldn't allocate the dbus message\n");
return (-1);
}
dbus_error_init (&error);
reply = dbus_connection_send_with_reply_and_block (connection, message, -1, &error);
if (dbus_error_is_set (&error))
{
fprintf (stderr, "%s raised:\n %s\n\n", error.name, error.message);
dbus_message_unref (message);
return (-1);
}
if (reply == NULL)
{
fprintf( stderr, "dbus reply message was NULL\n" );
dbus_message_unref (message);
return (-1);
}
/* now analyze reply */
dbus_message_iter_init (reply, &iter);
int type;
type = dbus_message_iter_get_int32 (&iter);
fprintf (stderr, "Active device type: '%d'\n", type );
dbus_message_unref (reply);
dbus_message_unref (message);
return (type);
}
void get_device_networks (DBusConnection *connection, const char *path)
{
DBusMessage *message;
DBusMessage *reply;
DBusMessageIter iter;
DBusError error;
message = dbus_message_new_method_call ("org.freedesktop.NetworkManager",
path,
"org.freedesktop.NetworkManager",
"getNetworks");
if (message == NULL)
{
fprintf (stderr, "Couldn't allocate the dbus message\n");
return;
}
dbus_error_init (&error);
reply = dbus_connection_send_with_reply_and_block (connection, message, -1, &error);
if (dbus_error_is_set (&error))
{
fprintf (stderr, "%s raised:\n %s\n\n", error.name, error.message);
dbus_message_unref (message);
return;
}
if (reply == NULL)
{
fprintf( stderr, "dbus reply message was NULL\n" );
dbus_message_unref (message);
return;
}
/* now analyze reply */
dbus_message_iter_init (reply, &iter);
char **networks;
int num_networks;
if (!dbus_message_iter_get_string_array (&iter, &networks, &num_networks))
{
fprintf (stderr, "NetworkManager returned no device list" );
return;
}
dbus_message_unref (reply);
dbus_message_unref (message);
int i;
fprintf( stderr, "Networks:\n" );
for (i = 0; i < num_networks; i++)
{
DBusMessage *message2;
DBusMessage *reply2;
DBusMessageIter iter2;
DBusError error2;
message2 = dbus_message_new_method_call ("org.freedesktop.NetworkManager",
networks[i],
"org.freedesktop.NetworkManager",
"getName");
if (message2 == NULL)
{
fprintf (stderr, "Couldn't allocate the dbus message\n");
return;
}
dbus_error_init (&error2);
reply2 = dbus_connection_send_with_reply_and_block (connection, message2, -1, &error2);
if (dbus_error_is_set (&error2))
{
fprintf (stderr, "%s raised:\n %s\n\n", error2.name, error2.message);
dbus_message_unref (message2);
return;
}
if (reply2 == NULL)
{
fprintf( stderr, "dbus reply message was NULL\n" );
dbus_message_unref (message2);
return;
}
/* now analyze reply */
dbus_message_iter_init (reply2, &iter2);
const char *string2 = dbus_message_iter_get_string (&iter2);
if (!string2)
{
fprintf (stderr, "NetworkManager returned a NULL active device object path" );
return;
}
dbus_message_unref (reply2);
dbus_message_unref (message2);
fprintf( stderr, " %s (%s)\n", networks[i], string2 );
}
dbus_free_string_array (networks);
}
void get_devices (DBusConnection *connection)
{
DBusMessage *message;
DBusMessage *reply;
DBusMessageIter iter;
DBusError error;
message = dbus_message_new_method_call ("org.freedesktop.NetworkManager",
"/org/freedesktop/NetworkManager",
"org.freedesktop.NetworkManager",
"getDevices");
if (message == NULL)
{
fprintf (stderr, "Couldn't allocate the dbus message\n");
return;
}
dbus_error_init (&error);
reply = dbus_connection_send_with_reply_and_block (connection, message, -1, &error);
if (dbus_error_is_set (&error))
{
fprintf (stderr, "%s raised:\n %s\n\n", error.name, error.message);
dbus_message_unref (message);
return;
}
if (reply == NULL)
{
fprintf( stderr, "dbus reply message was NULL\n" );
dbus_message_unref (message);
return;
}
/* now analyze reply */
dbus_message_iter_init (reply, &iter);
char **devices;
int num_devices;
if (!dbus_message_iter_get_string_array (&iter, &devices, &num_devices))
{
fprintf (stderr, "NetworkManager returned no device list" );
return;
}
dbus_message_unref (reply);
dbus_message_unref (message);
int i;
fprintf( stderr, "Devices:\n" );
for (i = 0; i < num_devices; i++)
fprintf( stderr, " %s\n", devices[i] );
dbus_free_string_array (devices);
}
int main( int argc, char *argv[] )
{
DBusConnection *connection;
DBusError error;
g_type_init ();
dbus_error_init (&error);
connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
if (connection == NULL)
{
fprintf (stderr, "Error connecting to system bus: %s\n", error.message);
dbus_error_free (&error);
return 1;
}
char *path;
int type;
get_nm_status (connection);
path = get_active_device (connection);
get_device_name (connection, path);
type = get_device_type (connection, path);
if (type == 2)
{
get_device_active_network (connection, path);
get_device_networks (connection, path);
}
get_devices (connection);
g_free (path);
return 0;
}