1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the
15 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 * Boston, MA 02110-1301 USA.
18 * Copyright 2012 Red Hat, Inc.
21 #include "nm-default.h"
24 #include <netinet/ether.h>
26 #include "nm-setting-connection.h"
27 #include "nm-setting-bond.h"
30 #include "nm-device-bond.h"
31 #include "nm-device-private.h"
32 #include "nm-object-private.h"
35 G_DEFINE_TYPE (NMDeviceBond, nm_device_bond, NM_TYPE_DEVICE)
37 #define NM_DEVICE_BOND_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_BOND, NMDeviceBondPrivate))
45 } NMDeviceBondPrivate;
57 * nm_device_bond_error_quark:
59 * Registers an error quark for #NMDeviceBond if necessary.
61 * Returns: the error quark used for #NMDeviceBond errors.
64 nm_device_bond_error_quark (void)
66 static GQuark quark = 0;
68 if (G_UNLIKELY (quark == 0))
69 quark = g_quark_from_static_string ("nm-device-bond-error-quark");
75 * @connection: the #DBusGConnection
76 * @path: the DBus object path of the device
78 * Creates a new #NMDeviceBond.
80 * Returns: (transfer full): a new device
83 nm_device_bond_new (DBusGConnection *connection, const char *path)
87 g_return_val_if_fail (connection != NULL, NULL);
88 g_return_val_if_fail (path != NULL, NULL);
90 device = g_object_new (NM_TYPE_DEVICE_BOND,
91 NM_OBJECT_DBUS_CONNECTION, connection,
92 NM_OBJECT_DBUS_PATH, path,
94 _nm_object_ensure_inited (NM_OBJECT (device));
99 * nm_device_bond_get_hw_address:
100 * @device: a #NMDeviceBond
102 * Gets the hardware (MAC) address of the #NMDeviceBond
104 * Returns: the hardware address. This is the internal string used by the
105 * device, and must not be modified.
108 nm_device_bond_get_hw_address (NMDeviceBond *device)
110 g_return_val_if_fail (NM_IS_DEVICE_BOND (device), NULL);
112 _nm_object_ensure_inited (NM_OBJECT (device));
113 return NM_DEVICE_BOND_GET_PRIVATE (device)->hw_address;
117 * nm_device_bond_get_carrier:
118 * @device: a #NMDeviceBond
120 * Whether the device has carrier.
122 * Returns: %TRUE if the device has carrier
125 nm_device_bond_get_carrier (NMDeviceBond *device)
127 g_return_val_if_fail (NM_IS_DEVICE_BOND (device), FALSE);
129 _nm_object_ensure_inited (NM_OBJECT (device));
130 return NM_DEVICE_BOND_GET_PRIVATE (device)->carrier;
134 * nm_device_bond_get_slaves:
135 * @device: a #NMDeviceBond
137 * Gets the devices currently slaved to @device.
139 * Returns: (element-type NMDevice): the #GPtrArray containing
140 * #NMDevices that are slaves of @device. This is the internal
141 * copy used by the device, and must not be modified.
146 nm_device_bond_get_slaves (NMDeviceBond *device)
148 g_return_val_if_fail (NM_IS_DEVICE_BOND (device), FALSE);
150 _nm_object_ensure_inited (NM_OBJECT (device));
151 return handle_ptr_array_return (NM_DEVICE_BOND_GET_PRIVATE (device)->slaves);
155 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
157 NMSettingConnection *s_con;
158 NMSettingBond *s_bond;
159 const char *ctype, *dev_iface_name, *bond_iface_name;
161 s_con = nm_connection_get_setting_connection (connection);
164 ctype = nm_setting_connection_get_connection_type (s_con);
165 if (strcmp (ctype, NM_SETTING_BOND_SETTING_NAME) != 0) {
166 g_set_error (error, NM_DEVICE_BOND_ERROR, NM_DEVICE_BOND_ERROR_NOT_BOND_CONNECTION,
167 "The connection was not a bond connection.");
171 s_bond = nm_connection_get_setting_bond (connection);
173 g_set_error (error, NM_DEVICE_BOND_ERROR, NM_DEVICE_BOND_ERROR_INVALID_BOND_CONNECTION,
174 "The connection was not a valid bond connection.");
178 dev_iface_name = nm_device_get_iface (device);
179 bond_iface_name = nm_setting_bond_get_interface_name (s_bond);
180 if (g_strcmp0 (dev_iface_name, bond_iface_name) != 0) {
181 g_set_error (error, NM_DEVICE_BOND_ERROR, NM_DEVICE_BOND_ERROR_INTERFACE_MISMATCH,
182 "The interfaces of the device and the connection didn't match.");
186 /* FIXME: check slaves? */
188 return NM_DEVICE_CLASS (nm_device_bond_parent_class)->connection_compatible (device, connection, error);
192 get_setting_type (NMDevice *device)
194 return NM_TYPE_SETTING_BOND;
198 get_hw_address (NMDevice *device)
200 return nm_device_bond_get_hw_address (NM_DEVICE_BOND (device));
203 /***********************************************************/
206 nm_device_bond_init (NMDeviceBond *device)
208 _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_BOND);
212 register_properties (NMDeviceBond *device)
214 NMDeviceBondPrivate *priv = NM_DEVICE_BOND_GET_PRIVATE (device);
215 const NMPropertiesInfo property_info[] = {
216 { NM_DEVICE_BOND_HW_ADDRESS, &priv->hw_address },
217 { NM_DEVICE_BOND_CARRIER, &priv->carrier },
218 { NM_DEVICE_BOND_SLAVES, &priv->slaves, NULL, NM_TYPE_DEVICE },
222 _nm_object_register_properties (NM_OBJECT (device),
228 constructed (GObject *object)
230 NMDeviceBondPrivate *priv = NM_DEVICE_BOND_GET_PRIVATE (object);
232 G_OBJECT_CLASS (nm_device_bond_parent_class)->constructed (object);
234 priv->proxy = _nm_object_new_proxy (NM_OBJECT (object), NULL, NM_DBUS_INTERFACE_DEVICE_BOND);
235 register_properties (NM_DEVICE_BOND (object));
239 dispose (GObject *object)
241 NMDeviceBondPrivate *priv = NM_DEVICE_BOND_GET_PRIVATE (object);
243 g_clear_object (&priv->proxy);
246 g_ptr_array_set_free_func (priv->slaves, g_object_unref);
247 g_ptr_array_free (priv->slaves, TRUE);
251 G_OBJECT_CLASS (nm_device_bond_parent_class)->dispose (object);
255 finalize (GObject *object)
257 NMDeviceBondPrivate *priv = NM_DEVICE_BOND_GET_PRIVATE (object);
259 g_free (priv->hw_address);
261 G_OBJECT_CLASS (nm_device_bond_parent_class)->finalize (object);
265 get_property (GObject *object,
270 NMDeviceBond *device = NM_DEVICE_BOND (object);
272 _nm_object_ensure_inited (NM_OBJECT (object));
275 case PROP_HW_ADDRESS:
276 g_value_set_string (value, nm_device_bond_get_hw_address (device));
279 g_value_set_boolean (value, nm_device_bond_get_carrier (device));
282 g_value_set_boxed (value, nm_device_bond_get_slaves (device));
285 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
291 nm_device_bond_class_init (NMDeviceBondClass *bond_class)
293 GObjectClass *object_class = G_OBJECT_CLASS (bond_class);
294 NMDeviceClass *device_class = NM_DEVICE_CLASS (bond_class);
296 g_type_class_add_private (bond_class, sizeof (NMDeviceBondPrivate));
298 /* virtual methods */
299 object_class->constructed = constructed;
300 object_class->dispose = dispose;
301 object_class->finalize = finalize;
302 object_class->get_property = get_property;
303 device_class->connection_compatible = connection_compatible;
304 device_class->get_setting_type = get_setting_type;
305 device_class->get_hw_address = get_hw_address;
310 * NMDeviceBond:hw-address:
312 * The hardware (MAC) address of the device.
314 g_object_class_install_property
315 (object_class, PROP_HW_ADDRESS,
316 g_param_spec_string (NM_DEVICE_BOND_HW_ADDRESS, "", "",
319 G_PARAM_STATIC_STRINGS));
322 * NMDeviceBond:carrier:
324 * Whether the device has carrier.
326 g_object_class_install_property
327 (object_class, PROP_CARRIER,
328 g_param_spec_boolean (NM_DEVICE_BOND_CARRIER, "", "",
331 G_PARAM_STATIC_STRINGS));
334 * NMDeviceBond:slaves:
336 * The devices (#NMDevice) slaved to the bond device.
340 g_object_class_install_property
341 (object_class, PROP_SLAVES,
342 g_param_spec_boxed (NM_DEVICE_BOND_SLAVES, "", "",
343 NM_TYPE_OBJECT_ARRAY,
345 G_PARAM_STATIC_STRINGS));