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"
26 #include "nm-setting-connection.h"
27 #include "nm-setting-bluetooth.h"
30 #include "nm-device-bt.h"
31 #include "nm-device-private.h"
32 #include "nm-object-private.h"
33 #include "nm-enum-types.h"
35 G_DEFINE_TYPE (NMDeviceBt, nm_device_bt, NM_TYPE_DEVICE)
37 #define NM_DEVICE_BT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_BT, NMDeviceBtPrivate))
42 guint32 bt_capabilities;
55 * nm_device_bt_get_hw_address:
56 * @device: a #NMDeviceBt
58 * Gets the hardware (MAC) address of the #NMDeviceBt
60 * Returns: the hardware address. This is the internal string used by the
61 * device, and must not be modified.
64 nm_device_bt_get_hw_address (NMDeviceBt *device)
66 g_return_val_if_fail (NM_IS_DEVICE_BT (device), NULL);
68 return NM_DEVICE_BT_GET_PRIVATE (device)->hw_address;
72 * nm_device_bt_get_name:
73 * @device: a #NMDeviceBt
75 * Gets the name of the #NMDeviceBt.
77 * Returns: the name of the device
80 nm_device_bt_get_name (NMDeviceBt *device)
82 g_return_val_if_fail (NM_IS_DEVICE_BT (device), NULL);
84 return NM_DEVICE_BT_GET_PRIVATE (device)->name;
88 * nm_device_bt_get_capabilities:
89 * @device: a #NMDeviceBt
91 * Returns the Bluetooth device's usable capabilities.
93 * Returns: a combination of #NMBluetoothCapabilities
95 NMBluetoothCapabilities
96 nm_device_bt_get_capabilities (NMDeviceBt *device)
98 g_return_val_if_fail (NM_IS_DEVICE_BT (device), NM_BT_CAPABILITY_NONE);
100 return NM_DEVICE_BT_GET_PRIVATE (device)->bt_capabilities;
103 static NMBluetoothCapabilities
104 get_connection_bt_type (NMConnection *connection)
106 NMSettingBluetooth *s_bt;
109 s_bt = nm_connection_get_setting_bluetooth (connection);
111 return NM_BT_CAPABILITY_NONE;
113 bt_type = nm_setting_bluetooth_get_connection_type (s_bt);
116 if (!strcmp (bt_type, NM_SETTING_BLUETOOTH_TYPE_DUN))
117 return NM_BT_CAPABILITY_DUN;
118 else if (!strcmp (bt_type, NM_SETTING_BLUETOOTH_TYPE_PANU))
119 return NM_BT_CAPABILITY_NAP;
121 return NM_BT_CAPABILITY_NONE;
125 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
127 NMSettingBluetooth *s_bt;
128 const char *hw_addr, *setting_addr;
129 NMBluetoothCapabilities dev_caps;
130 NMBluetoothCapabilities bt_type;
132 if (!NM_DEVICE_CLASS (nm_device_bt_parent_class)->connection_compatible (device, connection, error))
135 if (!nm_connection_is_type (connection, NM_SETTING_BLUETOOTH_SETTING_NAME)) {
136 g_set_error (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION,
137 _("The connection was not a Bluetooth connection."));
141 /* Check BT address */
142 hw_addr = nm_device_bt_get_hw_address (NM_DEVICE_BT (device));
144 if (!nm_utils_hwaddr_valid (hw_addr, ETH_ALEN)) {
145 g_set_error_literal (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_FAILED,
146 _("Invalid device Bluetooth address."));
149 s_bt = nm_connection_get_setting_bluetooth (connection);
150 setting_addr = nm_setting_bluetooth_get_bdaddr (s_bt);
151 if (setting_addr && !nm_utils_hwaddr_matches (setting_addr, -1, hw_addr, -1)) {
152 g_set_error_literal (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION,
153 _("The Bluetooth addresses of the device and the connection didn't match."));
158 dev_caps = nm_device_bt_get_capabilities (NM_DEVICE_BT (device));
159 bt_type = get_connection_bt_type (connection);
160 if (!(bt_type & dev_caps)) {
161 g_set_error_literal (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION,
162 _("The device is lacking Bluetooth capabilities required by the connection."));
170 get_setting_type (NMDevice *device)
172 return NM_TYPE_SETTING_BLUETOOTH;
176 get_hw_address (NMDevice *device)
178 return nm_device_bt_get_hw_address (NM_DEVICE_BT (device));
181 /************************************************************/
184 nm_device_bt_init (NMDeviceBt *device)
186 _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_BT);
190 init_dbus (NMObject *object)
192 NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (object);
193 const NMPropertiesInfo property_info[] = {
194 { NM_DEVICE_BT_HW_ADDRESS, &priv->hw_address },
195 { NM_DEVICE_BT_NAME, &priv->name },
196 { NM_DEVICE_BT_CAPABILITIES, &priv->bt_capabilities },
200 NM_OBJECT_CLASS (nm_device_bt_parent_class)->init_dbus (object);
202 _nm_object_register_properties (object,
203 NM_DBUS_INTERFACE_DEVICE_BLUETOOTH,
208 finalize (GObject *object)
210 NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (object);
212 g_free (priv->hw_address);
215 G_OBJECT_CLASS (nm_device_bt_parent_class)->finalize (object);
219 get_property (GObject *object,
224 NMDeviceBt *device = NM_DEVICE_BT (object);
227 case PROP_HW_ADDRESS:
228 g_value_set_string (value, nm_device_bt_get_hw_address (device));
231 g_value_set_string (value, nm_device_bt_get_name (device));
233 case PROP_BT_CAPABILITIES:
234 g_value_set_flags (value, nm_device_bt_get_capabilities (device));
237 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
243 nm_device_bt_class_init (NMDeviceBtClass *bt_class)
245 GObjectClass *object_class = G_OBJECT_CLASS (bt_class);
246 NMObjectClass *nm_object_class = NM_OBJECT_CLASS (bt_class);
247 NMDeviceClass *device_class = NM_DEVICE_CLASS (bt_class);
249 g_type_class_add_private (bt_class, sizeof (NMDeviceBtPrivate));
251 _nm_object_class_add_interface (nm_object_class, NM_DBUS_INTERFACE_DEVICE_BLUETOOTH);
253 /* virtual methods */
254 object_class->finalize = finalize;
255 object_class->get_property = get_property;
257 nm_object_class->init_dbus = init_dbus;
259 device_class->connection_compatible = connection_compatible;
260 device_class->get_setting_type = get_setting_type;
261 device_class->get_hw_address = get_hw_address;
266 * NMDeviceBt:hw-address:
268 * The hardware (MAC) address of the device.
270 g_object_class_install_property
271 (object_class, PROP_HW_ADDRESS,
272 g_param_spec_string (NM_DEVICE_BT_HW_ADDRESS, "", "",
275 G_PARAM_STATIC_STRINGS));
280 * The name of the bluetooth device.
282 g_object_class_install_property
283 (object_class, PROP_NAME,
284 g_param_spec_string (NM_DEVICE_BT_NAME, "", "",
287 G_PARAM_STATIC_STRINGS));
290 * NMDeviceBt:bt-capabilities:
292 * The device's bluetooth capabilities, a combination of #NMBluetoothCapabilities.
294 g_object_class_install_property
295 (object_class, PROP_BT_CAPABILITIES,
296 g_param_spec_flags (NM_DEVICE_BT_CAPABILITIES, "", "",
297 NM_TYPE_BLUETOOTH_CAPABILITIES,
298 NM_BT_CAPABILITY_NONE,
300 G_PARAM_STATIC_STRINGS));