NetworkManager/libnm-glib/nm-device.c
Tambet Ingo 636b1140c5 2007-06-21 Tambet Ingo <tambet@ximian.com>
* libnm-glib/Makefile.am: Add NMObject to build, remove nm-utils.[ch].

	* nm-utils.[ch]: Remove.

	* libnm-glib/nm-object.c: Implement a base class for all libnm-glib dbus-aware
	objects for easy property access and dbus connection handling.

	* libnm-glib/nm-client.c: Derive from NMObject.

	* libnm-glib/nm-device.c: Ditto.

	* libnm-glib/nm-device-802-3-ethernet.c: Changes for being based on NMObject.

	* libnm-glib/nm-device-802-11-wireless.c: Ditto.

	* libnm-glib/nm-ip4-config.c: Ditto.

	* libnm-glib/nm-access-point.c: Ditto.

	* libnm-util/nm-connection.c (nm_connection_compare): Add a stub for connection
	comparision. Currently used by the device activation code to determine if the new
	activation is the same as the old one.

	* src/nm-dbus-nmi.c (nm_dbus_get_user_key_for_network): Don't use the obsolete and
	wrong way of getting the dbus path for AP. Fixes the issue where the applet isn't
	able to ask password for the AP.

	* src/nm-device.c (nm_device_activate): Change the logic here - instead of giving
	up if the device is already connected, tear down it's connection (if it isn't the
	same as new one) and start the activation.

	* src/nm-manager.c: Add the beginnings of NMConnection storage and signals.

	* src/NetworkManagerAP.c (nm_ap_init): Set the default values to AP memebers, fixes
	the issue where all APs are always listed as encrypted.

	* src/NetworkManagerDbus.c (nm_dbus_get_object_path_for_network): Remove. APs have
	their own registered paths.

	* test/nm-tool.c (detail_device): Don't try to get active network from wireless
	device if it's not connected - dbus-glib will happily crash trying to marshal NULL.




git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@2615 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
2007-06-22 15:09:02 +00:00

340 lines
8.1 KiB
C

#include "nm-device.h"
#include "nm-device-private.h"
#include "nm-device-bindings.h"
G_DEFINE_TYPE (NMDevice, nm_device, NM_TYPE_OBJECT)
#define NM_DEVICE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE, NMDevicePrivate))
typedef struct {
DBusGProxy *device_proxy;
NMDeviceState state;
gboolean disposed;
} NMDevicePrivate;
enum {
STATE_CHANGED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
enum {
PROP_0,
PROP_CONNECTION,
PROP_PATH,
LAST_PROP
};
static void device_state_change_proxy (DBusGProxy *proxy, guint state, gpointer user_data);
static void
nm_device_init (NMDevice *device)
{
NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (device);
priv->state = NM_DEVICE_STATE_UNKNOWN;
priv->disposed = FALSE;
}
static GObject*
constructor (GType type,
guint n_construct_params,
GObjectConstructParam *construct_params)
{
NMObject *object;
NMDevicePrivate *priv;
object = (NMObject *) G_OBJECT_CLASS (nm_device_parent_class)->constructor (type,
n_construct_params,
construct_params);
if (!object)
return NULL;
priv = NM_DEVICE_GET_PRIVATE (object);
priv->device_proxy = dbus_g_proxy_new_for_name (nm_object_get_connection (object),
NM_DBUS_SERVICE,
nm_object_get_path (object),
NM_DBUS_INTERFACE_DEVICE);
dbus_g_proxy_add_signal (priv->device_proxy, "StateChanged", G_TYPE_UINT, G_TYPE_INVALID);
dbus_g_proxy_connect_signal (priv->device_proxy, "StateChanged",
G_CALLBACK (device_state_change_proxy),
object, NULL);
return G_OBJECT (object);
}
static void
dispose (GObject *object)
{
NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (object);
if (priv->disposed)
return;
priv->disposed = TRUE;
g_object_unref (priv->device_proxy);
G_OBJECT_CLASS (nm_device_parent_class)->dispose (object);
}
static void
nm_device_class_init (NMDeviceClass *device_class)
{
GObjectClass *object_class = G_OBJECT_CLASS (device_class);
g_type_class_add_private (device_class, sizeof (NMDevicePrivate));
/* virtual methods */
object_class->constructor = constructor;
object_class->dispose = dispose;
/* signals */
signals[STATE_CHANGED] =
g_signal_new ("state-changed",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (NMDeviceClass, state_changed),
NULL, NULL,
g_cclosure_marshal_VOID__UINT,
G_TYPE_NONE, 1,
G_TYPE_UINT);
}
static void
device_state_change_proxy (DBusGProxy *proxy, guint state, gpointer user_data)
{
NMDevice *device = NM_DEVICE (user_data);
NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (device);
if (priv->state != state) {
priv->state = state;
g_signal_emit (device, signals[STATE_CHANGED], 0, state);
}
}
NMDevice *
nm_device_new (DBusGConnection *connection, const char *path)
{
return (NMDevice *) g_object_new (NM_TYPE_DEVICE,
NM_OBJECT_CONNECTION, connection,
NM_OBJECT_PATH, path,
NULL);
}
void
nm_device_activate (NMDevice *device, NMConnection *connection)
{
GError *err = NULL;
g_return_if_fail (NM_IS_DEVICE (device));
g_return_if_fail (connection != NULL);
if (!org_freedesktop_NetworkManager_Device_activate (NM_DEVICE_GET_PRIVATE (device)->device_proxy,
nm_connection_to_hash (connection),
&err)) {
g_warning ("Cannot deactivate device: %s", err->message);
g_error_free (err);
}
}
void
nm_device_deactivate (NMDevice *device)
{
GError *err = NULL;
g_return_if_fail (NM_IS_DEVICE (device));
if (!org_freedesktop_NetworkManager_Device_deactivate (NM_DEVICE_GET_PRIVATE (device)->device_proxy, &err)) {
g_warning ("Cannot deactivate device: %s", err->message);
g_error_free (err);
}
}
char *
nm_device_get_iface (NMDevice *device)
{
g_return_val_if_fail (NM_IS_DEVICE (device), NULL);
return nm_object_get_string_property (NM_OBJECT (device), NM_DBUS_INTERFACE_DEVICE, "Interface");
}
char *
nm_device_get_udi (NMDevice *device)
{
g_return_val_if_fail (NM_IS_DEVICE (device), NULL);
return nm_object_get_string_property (NM_OBJECT (device), NM_DBUS_INTERFACE_DEVICE, "Udi");
}
char *
nm_device_get_driver (NMDevice *device)
{
g_return_val_if_fail (NM_IS_DEVICE (device), NULL);
return nm_object_get_string_property (NM_OBJECT (device), NM_DBUS_INTERFACE_DEVICE, "Driver");
}
guint32
nm_device_get_capabilities (NMDevice *device)
{
g_return_val_if_fail (NM_IS_DEVICE (device), 0);
return nm_object_get_uint_property (NM_OBJECT (device), NM_DBUS_INTERFACE_DEVICE, "Capabilities");
}
guint32
nm_device_get_ip4_address (NMDevice *device)
{
g_return_val_if_fail (NM_IS_DEVICE (device), 0);
return nm_object_get_uint_property (NM_OBJECT (device), NM_DBUS_INTERFACE_DEVICE, "Ip4Address");
}
NMIP4Config *
nm_device_get_ip4_config (NMDevice *device)
{
char *path;
NMIP4Config *config = NULL;
g_return_val_if_fail (NM_IS_DEVICE (device), NULL);
path = nm_object_get_object_path_property (NM_OBJECT (device), NM_DBUS_INTERFACE_DEVICE, "Ip4Config");
if (path) {
config = nm_ip4_config_new (nm_object_get_connection (NM_OBJECT (device)), path);
g_free (path);
}
return config;
}
NMDeviceState
nm_device_get_state (NMDevice *device)
{
NMDevicePrivate *priv;
g_return_val_if_fail (NM_IS_DEVICE (device), NM_DEVICE_STATE_UNKNOWN);
priv = NM_DEVICE_GET_PRIVATE (device);
if (priv->state == NM_DEVICE_STATE_UNKNOWN)
priv->state = nm_object_get_uint_property (NM_OBJECT (device), NM_DBUS_INTERFACE_DEVICE, "State");
return priv->state;
}
char *
nm_device_get_description (NMDevice *device)
{
DBusGProxy *proxy;
GError *err = NULL;
char *udi;
char *physical_device_udi = NULL;
char *vendor = NULL;
char *product = NULL;
char *description = NULL;
g_return_val_if_fail (NM_IS_DEVICE (device), NULL);
/* First, get the physical device info */
udi = nm_device_get_udi (device);
proxy = dbus_g_proxy_new_for_name (nm_object_get_connection (NM_OBJECT (device)),
"org.freedesktop.Hal",
udi,
"org.freedesktop.Hal.Device");
g_free (udi);
if (!dbus_g_proxy_call (proxy, "GetPropertyString", &err,
G_TYPE_STRING, "net.physical_device",
G_TYPE_INVALID,
G_TYPE_STRING, &physical_device_udi,
G_TYPE_INVALID)) {
g_warning ("Error getting physical device info from HAL: %s", err->message);
g_error_free (err);
goto out;
}
g_object_unref (proxy);
/* Now get the vendor and product info from the physical device */
proxy = dbus_g_proxy_new_for_name (nm_object_get_connection (NM_OBJECT (device)),
"org.freedesktop.Hal",
physical_device_udi,
"org.freedesktop.Hal.Device");
if (!dbus_g_proxy_call (proxy, "GetPropertyString", &err,
G_TYPE_STRING, "info.vendor",
G_TYPE_INVALID,
G_TYPE_STRING, &vendor,
G_TYPE_INVALID)) {
g_warning ("Error getting vendor info from HAL: %s", err->message);
g_error_free (err);
goto out;
}
if (!dbus_g_proxy_call (proxy, "GetPropertyString", &err,
G_TYPE_STRING, "info.product",
G_TYPE_INVALID,
G_TYPE_STRING, &product,
G_TYPE_INVALID)) {
g_warning ("Error getting product info from HAL: %s", err->message);
g_error_free (err);
goto out;
}
description = g_strdup_printf ("%s %s", vendor, product);
out:
g_object_unref (proxy);
g_free (physical_device_udi);
g_free (vendor);
g_free (product);
return description;
}
NMDeviceType
nm_device_type_for_path (DBusGConnection *connection,
const char *path)
{
DBusGProxy *proxy;
GError *err = NULL;
GValue value = {0,};
NMDeviceType type = DEVICE_TYPE_UNKNOWN;
g_return_val_if_fail (connection != NULL, type);
g_return_val_if_fail (path != NULL, type);
proxy = dbus_g_proxy_new_for_name (connection,
NM_DBUS_SERVICE,
path,
"org.freedesktop.DBus.Properties");
if (dbus_g_proxy_call (proxy,
"Get", &err,
G_TYPE_STRING, NM_DBUS_INTERFACE_DEVICE,
G_TYPE_STRING, "DeviceType",
G_TYPE_INVALID,
G_TYPE_VALUE, &value,
G_TYPE_INVALID)) {
type = (NMDeviceType) g_value_get_uint (&value);
} else {
g_warning ("Error in get_property: %s\n", err->message);
g_error_free (err);
}
g_object_unref (proxy);
return type;
}