about summary refs log tree commit diff
path: root/libnm-glib/nm-device-bt.c
diff options
context:
space:
mode:
Diffstat (limited to 'libnm-glib/nm-device-bt.c')
-rw-r--r--libnm-glib/nm-device-bt.c141
1 files changed, 69 insertions, 72 deletions
diff --git a/libnm-glib/nm-device-bt.c b/libnm-glib/nm-device-bt.c
index dc60d258..8e0ff200 100644
--- a/libnm-glib/nm-device-bt.c
+++ b/libnm-glib/nm-device-bt.c
@@ -18,7 +18,7 @@
  * Boston, MA 02110-1301 USA.
  *
  * Copyright (C) 2007 - 2008 Novell, Inc.
- * Copyright (C) 2007 - 2011 Red Hat, Inc.
+ * Copyright (C) 2007 - 2012 Red Hat, Inc.
  */
 
 #include <config.h>
@@ -32,8 +32,6 @@
 #include "nm-device-private.h"
 #include "nm-object-private.h"
 
-#include "nm-device-bt-bindings.h"
-
 G_DEFINE_TYPE (NMDeviceBt, nm_device_bt, NM_TYPE_DEVICE)
 
 #define NM_DEVICE_BT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_BT, NMDeviceBtPrivate))
@@ -44,7 +42,6 @@ typedef struct {
 	char *hw_address;
 	char *name;
 	guint32 bt_capabilities;
-	gboolean bt_capabilities_valid;
 
 	gboolean disposed;
 } NMDeviceBtPrivate;
@@ -63,6 +60,23 @@ enum {
 #define DBUS_PROP_BT_CAPABILITIES "BtCapabilities"
 
 /**
+ * nm_device_bt_error_quark:
+ *
+ * Registers an error quark for #NMDeviceBt if necessary.
+ *
+ * Returns: the error quark used for #NMDeviceBt errors.
+ **/
+GQuark
+nm_device_bt_error_quark (void)
+{
+	static GQuark quark = 0;
+
+	if (G_UNLIKELY (quark == 0))
+		quark = g_quark_from_static_string ("nm-device-bt-error-quark");
+	return quark;
+}
+
+/**
  * nm_device_bt_new:
  * @connection: the #DBusGConnection
  * @path: the DBus object path of the device
@@ -95,19 +109,10 @@ nm_device_bt_new (DBusGConnection *connection, const char *path)
 const char *
 nm_device_bt_get_hw_address (NMDeviceBt *device)
 {
-	NMDeviceBtPrivate *priv;
-
 	g_return_val_if_fail (NM_IS_DEVICE_BT (device), NULL);
 
-	priv = NM_DEVICE_BT_GET_PRIVATE (device);
-	if (!priv->hw_address) {
-		priv->hw_address = _nm_object_get_string_property (NM_OBJECT (device),
-		                                                   NM_DBUS_INTERFACE_DEVICE_BLUETOOTH,
-		                                                   DBUS_PROP_HW_ADDRESS,
-		                                                   NULL);
-	}
-
-	return priv->hw_address;
+	_nm_object_ensure_inited (NM_OBJECT (device));
+	return NM_DEVICE_BT_GET_PRIVATE (device)->hw_address;
 }
 
 /**
@@ -121,19 +126,10 @@ nm_device_bt_get_hw_address (NMDeviceBt *device)
 const char *
 nm_device_bt_get_name (NMDeviceBt *device)
 {
-	NMDeviceBtPrivate *priv;
-
 	g_return_val_if_fail (NM_IS_DEVICE_BT (device), NULL);
 
-	priv = NM_DEVICE_BT_GET_PRIVATE (device);
-	if (!priv->name) {
-		priv->name = _nm_object_get_string_property (NM_OBJECT (device),
-		                                             NM_DBUS_INTERFACE_DEVICE_BLUETOOTH,
-		                                             DBUS_PROP_NAME,
-		                                             NULL);
-	}
-
-	return priv->name;
+	_nm_object_ensure_inited (NM_OBJECT (device));
+	return NM_DEVICE_BT_GET_PRIVATE (device)->name;
 }
 
 /**
@@ -147,20 +143,10 @@ nm_device_bt_get_name (NMDeviceBt *device)
 NMBluetoothCapabilities
 nm_device_bt_get_capabilities (NMDeviceBt *device)
 {
-	NMDeviceBtPrivate *priv;
-
 	g_return_val_if_fail (NM_IS_DEVICE_BT (device), NM_BT_CAPABILITY_NONE);
 
-	priv = NM_DEVICE_BT_GET_PRIVATE (device);
-	if (!priv->bt_capabilities_valid) {
-		priv->bt_capabilities = _nm_object_get_uint_property (NM_OBJECT (device),
-		                                                      NM_DBUS_INTERFACE_DEVICE_BLUETOOTH,
-		                                                      DBUS_PROP_BT_CAPABILITIES,
-		                                                      NULL);
-		priv->bt_capabilities_valid = TRUE;
-	}
-
-	return priv->bt_capabilities;
+	_nm_object_ensure_inited (NM_OBJECT (device));
+	return NM_DEVICE_BT_GET_PRIVATE (device)->bt_capabilities;
 }
 
 static NMBluetoothCapabilities
@@ -169,7 +155,7 @@ get_connection_bt_type (NMConnection *connection)
 	NMSettingBluetooth *s_bt;
 	const char *bt_type;
 
-	s_bt = (NMSettingBluetooth *) nm_connection_get_setting (connection, NM_TYPE_SETTING_BLUETOOTH);
+	s_bt = nm_connection_get_setting_bluetooth (connection);
 	if (!s_bt)
 		return NM_BT_CAPABILITY_NONE;
 
@@ -185,7 +171,7 @@ get_connection_bt_type (NMConnection *connection)
 }
 
 static gboolean
-connection_valid (NMDevice *device, NMConnection *connection)
+connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
 {
 	NMSettingConnection *s_con;
 	NMSettingBluetooth *s_bt;
@@ -196,30 +182,49 @@ connection_valid (NMDevice *device, NMConnection *connection)
 	NMBluetoothCapabilities dev_caps;
 	NMBluetoothCapabilities bt_type;
 
+	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
 	s_con = nm_connection_get_setting_connection (connection);
 	g_assert (s_con);
 
 	ctype = nm_setting_connection_get_connection_type (s_con);
-	if (strcmp (ctype, NM_SETTING_BLUETOOTH_SETTING_NAME) != 0)
+	if (strcmp (ctype, NM_SETTING_BLUETOOTH_SETTING_NAME) != 0) {
+		g_set_error (error, NM_DEVICE_BT_ERROR, NM_DEVICE_BT_ERROR_NOT_BT_CONNECTION,
+		             "The connection was not a Bluetooth connection.");
 		return FALSE;
+	}
 
 	s_bt = nm_connection_get_setting_bluetooth (connection);
-	if (!s_bt)
+	if (!s_bt) {
+		g_set_error (error, NM_DEVICE_BT_ERROR, NM_DEVICE_BT_ERROR_INVALID_BT_CONNECTION,
+		             "The connection was not a valid Bluetooth connection.");
 		return FALSE;
+	}
 
 	/* Check BT address */
 	hw_str = nm_device_bt_get_hw_address (NM_DEVICE_BT (device));
 	if (hw_str) {
 		hw_mac = ether_aton (hw_str);
+		if (!hw_mac) {
+			g_set_error (error, NM_DEVICE_BT_ERROR, NM_DEVICE_BT_ERROR_INVALID_DEVICE_MAC,
+			             "Invalid device MAC address.");
+			return FALSE;
+		}
 		mac = nm_setting_bluetooth_get_bdaddr (s_bt);
-		if (mac && hw_mac && memcmp (mac->data, hw_mac->ether_addr_octet, ETH_ALEN))
+		if (mac && hw_mac && memcmp (mac->data, hw_mac->ether_addr_octet, ETH_ALEN)) {
+			g_set_error (error, NM_DEVICE_BT_ERROR, NM_DEVICE_BT_ERROR_MAC_MISMATCH,
+			             "The MACs of the device and the connection didn't match.");
 			return FALSE;
+		}
 	}
 
 	dev_caps = nm_device_bt_get_capabilities (NM_DEVICE_BT (device));
 	bt_type = get_connection_bt_type (connection);
-	if (!(bt_type & dev_caps))
+	if (!(bt_type & dev_caps)) {
+		g_set_error (error, NM_DEVICE_BT_ERROR, NM_DEVICE_BT_ERROR_MISSING_DEVICE_CAPS,
+		             "The device missed BT capabilities required by the connection.");
 		return FALSE;
+	}
 
 	return TRUE;
 }
@@ -229,46 +234,38 @@ connection_valid (NMDevice *device, NMConnection *connection)
 static void
 nm_device_bt_init (NMDeviceBt *device)
 {
+	_nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_BT);
 }
 
 static void
-register_for_property_changed (NMDeviceBt *device)
+register_properties (NMDeviceBt *device)
 {
 	NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (device);
-	const NMPropertiesChangedInfo property_changed_info[] = {
-		{ NM_DEVICE_BT_HW_ADDRESS,   _nm_object_demarshal_generic, &priv->hw_address },
-		{ NM_DEVICE_BT_NAME,         _nm_object_demarshal_generic, &priv->name },
-		{ NM_DEVICE_BT_CAPABILITIES, _nm_object_demarshal_generic, &priv->bt_capabilities },
+	const NMPropertiesInfo property_info[] = {
+		{ NM_DEVICE_BT_HW_ADDRESS,   &priv->hw_address },
+		{ NM_DEVICE_BT_NAME,         &priv->name },
+		{ NM_DEVICE_BT_CAPABILITIES, &priv->bt_capabilities },
 		{ NULL },
 	};
 
-	_nm_object_handle_properties_changed (NM_OBJECT (device),
-	                                     priv->proxy,
-	                                     property_changed_info);
+	_nm_object_register_properties (NM_OBJECT (device),
+	                                priv->proxy,
+	                                property_info);
 }
 
-static GObject*
-constructor (GType type,
-			 guint n_construct_params,
-			 GObjectConstructParam *construct_params)
+static void
+constructed (GObject *object)
 {
-	GObject *object;
-
-	object = G_OBJECT_CLASS (nm_device_bt_parent_class)->constructor (type,
-	                                                                  n_construct_params,
-	                                                                  construct_params);
-	if (object) {
-			NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (object);
+	NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (object);
 
-		priv->proxy = dbus_g_proxy_new_for_name (nm_object_get_connection (NM_OBJECT (object)),
-		                                         NM_DBUS_SERVICE,
-		                                         nm_object_get_path (NM_OBJECT (object)),
-		                                         NM_DBUS_INTERFACE_DEVICE_BLUETOOTH);
+	G_OBJECT_CLASS (nm_device_bt_parent_class)->constructed (object);
 
-		register_for_property_changed (NM_DEVICE_BT (object));
-	}
+	priv->proxy = dbus_g_proxy_new_for_name (nm_object_get_connection (NM_OBJECT (object)),
+	                                         NM_DBUS_SERVICE,
+	                                         nm_object_get_path (NM_OBJECT (object)),
+	                                         NM_DBUS_INTERFACE_DEVICE_BLUETOOTH);
 
-	return object;
+	register_properties (NM_DEVICE_BT (object));
 }
 
 static void
@@ -331,11 +328,11 @@ nm_device_bt_class_init (NMDeviceBtClass *bt_class)
 	g_type_class_add_private (bt_class, sizeof (NMDeviceBtPrivate));
 
 	/* virtual methods */
-	object_class->constructor = constructor;
+	object_class->constructed = constructed;
 	object_class->dispose = dispose;
 	object_class->finalize = finalize;
 	object_class->get_property = get_property;
-	device_class->connection_valid = connection_valid;
+	device_class->connection_compatible = connection_compatible;
 
 	/* properties */