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 2007 - 2008 Novell, Inc.
19 * Copyright 2007 - 2012 Red Hat, Inc.
22 #include "nm-default.h"
25 #include <netinet/ether.h>
27 #include "nm-setting-connection.h"
28 #include "nm-setting-bluetooth.h"
30 #include "nm-device-bt.h"
31 #include "nm-device-private.h"
32 #include "nm-object-private.h"
34 G_DEFINE_TYPE (NMDeviceBt, nm_device_bt, NM_TYPE_DEVICE)
36 #define NM_DEVICE_BT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_BT, NMDeviceBtPrivate))
43 guint32 bt_capabilities;
56 * nm_device_bt_error_quark:
58 * Registers an error quark for #NMDeviceBt if necessary.
60 * Returns: the error quark used for #NMDeviceBt errors.
63 nm_device_bt_error_quark (void)
65 static GQuark quark = 0;
67 if (G_UNLIKELY (quark == 0))
68 quark = g_quark_from_static_string ("nm-device-bt-error-quark");
74 * @connection: the #DBusGConnection
75 * @path: the DBus object path of the device
77 * Creates a new #NMDeviceBt.
79 * Returns: (transfer full): a new device
82 nm_device_bt_new (DBusGConnection *connection, const char *path)
84 g_return_val_if_fail (connection != NULL, NULL);
85 g_return_val_if_fail (path != NULL, NULL);
87 return g_object_new (NM_TYPE_DEVICE_BT,
88 NM_OBJECT_DBUS_CONNECTION, connection,
89 NM_OBJECT_DBUS_PATH, path,
94 * nm_device_bt_get_hw_address:
95 * @device: a #NMDeviceBt
97 * Gets the hardware (MAC) address of the #NMDeviceBt
99 * Returns: the hardware address. This is the internal string used by the
100 * device, and must not be modified.
103 nm_device_bt_get_hw_address (NMDeviceBt *device)
105 g_return_val_if_fail (NM_IS_DEVICE_BT (device), NULL);
107 _nm_object_ensure_inited (NM_OBJECT (device));
108 return NM_DEVICE_BT_GET_PRIVATE (device)->hw_address;
112 * nm_device_bt_get_name:
113 * @device: a #NMDeviceBt
115 * Gets the name of the #NMDeviceBt.
117 * Returns: the name of the device
120 nm_device_bt_get_name (NMDeviceBt *device)
122 g_return_val_if_fail (NM_IS_DEVICE_BT (device), NULL);
124 _nm_object_ensure_inited (NM_OBJECT (device));
125 return NM_DEVICE_BT_GET_PRIVATE (device)->name;
129 * nm_device_bt_get_capabilities:
130 * @device: a #NMDeviceBt
132 * Returns the Bluetooth device's usable capabilities.
134 * Returns: a combination of #NMBluetoothCapabilities
136 NMBluetoothCapabilities
137 nm_device_bt_get_capabilities (NMDeviceBt *device)
139 g_return_val_if_fail (NM_IS_DEVICE_BT (device), NM_BT_CAPABILITY_NONE);
141 _nm_object_ensure_inited (NM_OBJECT (device));
142 return NM_DEVICE_BT_GET_PRIVATE (device)->bt_capabilities;
145 static NMBluetoothCapabilities
146 get_connection_bt_type (NMConnection *connection)
148 NMSettingBluetooth *s_bt;
151 s_bt = nm_connection_get_setting_bluetooth (connection);
153 return NM_BT_CAPABILITY_NONE;
155 bt_type = nm_setting_bluetooth_get_connection_type (s_bt);
158 if (!strcmp (bt_type, NM_SETTING_BLUETOOTH_TYPE_DUN))
159 return NM_BT_CAPABILITY_DUN;
160 else if (!strcmp (bt_type, NM_SETTING_BLUETOOTH_TYPE_PANU))
161 return NM_BT_CAPABILITY_NAP;
163 return NM_BT_CAPABILITY_NONE;
167 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
169 NMSettingConnection *s_con;
170 NMSettingBluetooth *s_bt;
172 const GByteArray *mac;
174 struct ether_addr *hw_mac;
175 NMBluetoothCapabilities dev_caps;
176 NMBluetoothCapabilities bt_type;
178 s_con = nm_connection_get_setting_connection (connection);
181 ctype = nm_setting_connection_get_connection_type (s_con);
182 if (strcmp (ctype, NM_SETTING_BLUETOOTH_SETTING_NAME) != 0) {
183 g_set_error (error, NM_DEVICE_BT_ERROR, NM_DEVICE_BT_ERROR_NOT_BT_CONNECTION,
184 "The connection was not a Bluetooth connection.");
188 s_bt = nm_connection_get_setting_bluetooth (connection);
190 g_set_error (error, NM_DEVICE_BT_ERROR, NM_DEVICE_BT_ERROR_INVALID_BT_CONNECTION,
191 "The connection was not a valid Bluetooth connection.");
195 /* Check BT address */
196 hw_str = nm_device_bt_get_hw_address (NM_DEVICE_BT (device));
198 hw_mac = ether_aton (hw_str);
200 g_set_error (error, NM_DEVICE_BT_ERROR, NM_DEVICE_BT_ERROR_INVALID_DEVICE_MAC,
201 "Invalid device MAC address.");
204 mac = nm_setting_bluetooth_get_bdaddr (s_bt);
205 if (mac && hw_mac && memcmp (mac->data, hw_mac->ether_addr_octet, ETH_ALEN)) {
206 g_set_error (error, NM_DEVICE_BT_ERROR, NM_DEVICE_BT_ERROR_MAC_MISMATCH,
207 "The MACs of the device and the connection didn't match.");
212 dev_caps = nm_device_bt_get_capabilities (NM_DEVICE_BT (device));
213 bt_type = get_connection_bt_type (connection);
214 if (!(bt_type & dev_caps)) {
215 g_set_error (error, NM_DEVICE_BT_ERROR, NM_DEVICE_BT_ERROR_MISSING_DEVICE_CAPS,
216 "The device missed BT capabilities required by the connection.");
220 return NM_DEVICE_CLASS (nm_device_bt_parent_class)->connection_compatible (device, connection, error);
224 get_setting_type (NMDevice *device)
226 return NM_TYPE_SETTING_BLUETOOTH;
230 get_hw_address (NMDevice *device)
232 return nm_device_bt_get_hw_address (NM_DEVICE_BT (device));
235 /************************************************************/
238 nm_device_bt_init (NMDeviceBt *device)
240 _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_BT);
244 register_properties (NMDeviceBt *device)
246 NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (device);
247 const NMPropertiesInfo property_info[] = {
248 { NM_DEVICE_BT_HW_ADDRESS, &priv->hw_address },
249 { NM_DEVICE_BT_NAME, &priv->name },
250 { NM_DEVICE_BT_CAPABILITIES, &priv->bt_capabilities },
254 _nm_object_register_properties (NM_OBJECT (device),
260 constructed (GObject *object)
262 NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (object);
264 G_OBJECT_CLASS (nm_device_bt_parent_class)->constructed (object);
266 priv->proxy = _nm_object_new_proxy (NM_OBJECT (object), NULL, NM_DBUS_INTERFACE_DEVICE_BLUETOOTH);
267 register_properties (NM_DEVICE_BT (object));
271 dispose (GObject *object)
273 NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (object);
275 g_clear_object (&priv->proxy);
277 G_OBJECT_CLASS (nm_device_bt_parent_class)->dispose (object);
281 finalize (GObject *object)
283 NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (object);
285 g_free (priv->hw_address);
288 G_OBJECT_CLASS (nm_device_bt_parent_class)->finalize (object);
292 get_property (GObject *object,
297 NMDeviceBt *device = NM_DEVICE_BT (object);
299 _nm_object_ensure_inited (NM_OBJECT (object));
302 case PROP_HW_ADDRESS:
303 g_value_set_string (value, nm_device_bt_get_hw_address (device));
306 g_value_set_string (value, nm_device_bt_get_name (device));
308 case PROP_BT_CAPABILITIES:
309 g_value_set_uint (value, nm_device_bt_get_capabilities (device));
312 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
318 nm_device_bt_class_init (NMDeviceBtClass *bt_class)
320 GObjectClass *object_class = G_OBJECT_CLASS (bt_class);
321 NMDeviceClass *device_class = NM_DEVICE_CLASS (bt_class);
323 g_type_class_add_private (bt_class, sizeof (NMDeviceBtPrivate));
325 /* virtual methods */
326 object_class->constructed = constructed;
327 object_class->dispose = dispose;
328 object_class->finalize = finalize;
329 object_class->get_property = get_property;
330 device_class->connection_compatible = connection_compatible;
331 device_class->get_setting_type = get_setting_type;
332 device_class->get_hw_address = get_hw_address;
337 * NMDeviceBt:hw-address:
339 * The hardware (MAC) address of the device.
341 g_object_class_install_property
342 (object_class, PROP_HW_ADDRESS,
343 g_param_spec_string (NM_DEVICE_BT_HW_ADDRESS, "", "",
346 G_PARAM_STATIC_STRINGS));
351 * The name of the bluetooth device.
353 g_object_class_install_property
354 (object_class, PROP_NAME,
355 g_param_spec_string (NM_DEVICE_BT_NAME, "", "",
358 G_PARAM_STATIC_STRINGS));
361 * NMDeviceBt:bt-capabilities:
363 * The device's bluetooth capabilities, a combination of #NMBluetoothCapabilities.
365 g_object_class_install_property
366 (object_class, PROP_BT_CAPABILITIES,
367 g_param_spec_uint (NM_DEVICE_BT_CAPABILITIES, "", "",
368 NM_BT_CAPABILITY_NONE, G_MAXUINT32, NM_BT_CAPABILITY_NONE,
370 G_PARAM_STATIC_STRINGS));