device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-glib / nm-device-bt.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /*
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.
7  *
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.
12  *
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.
17  *
18  * Copyright 2007 - 2008 Novell, Inc.
19  * Copyright 2007 - 2012 Red Hat, Inc.
20  */
21
22 #include "nm-default.h"
23
24 #include <string.h>
25 #include <netinet/ether.h>
26
27 #include "nm-setting-connection.h"
28 #include "nm-setting-bluetooth.h"
29
30 #include "nm-device-bt.h"
31 #include "nm-device-private.h"
32 #include "nm-object-private.h"
33
34 G_DEFINE_TYPE (NMDeviceBt, nm_device_bt, NM_TYPE_DEVICE)
35
36 #define NM_DEVICE_BT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_BT, NMDeviceBtPrivate))
37
38 typedef struct {
39         DBusGProxy *proxy;
40
41         char *hw_address;
42         char *name;
43         guint32 bt_capabilities;
44 } NMDeviceBtPrivate;
45
46 enum {
47         PROP_0,
48         PROP_HW_ADDRESS,
49         PROP_NAME,
50         PROP_BT_CAPABILITIES,
51
52         LAST_PROP
53 };
54
55 /**
56  * nm_device_bt_error_quark:
57  *
58  * Registers an error quark for #NMDeviceBt if necessary.
59  *
60  * Returns: the error quark used for #NMDeviceBt errors.
61  **/
62 GQuark
63 nm_device_bt_error_quark (void)
64 {
65         static GQuark quark = 0;
66
67         if (G_UNLIKELY (quark == 0))
68                 quark = g_quark_from_static_string ("nm-device-bt-error-quark");
69         return quark;
70 }
71
72 /**
73  * nm_device_bt_new:
74  * @connection: the #DBusGConnection
75  * @path: the DBus object path of the device
76  *
77  * Creates a new #NMDeviceBt.
78  *
79  * Returns: (transfer full): a new device
80  **/
81 GObject *
82 nm_device_bt_new (DBusGConnection *connection, const char *path)
83 {
84         g_return_val_if_fail (connection != NULL, NULL);
85         g_return_val_if_fail (path != NULL, NULL);
86
87         return g_object_new (NM_TYPE_DEVICE_BT,
88                              NM_OBJECT_DBUS_CONNECTION, connection,
89                              NM_OBJECT_DBUS_PATH, path,
90                              NULL);
91 }
92
93 /**
94  * nm_device_bt_get_hw_address:
95  * @device: a #NMDeviceBt
96  *
97  * Gets the hardware (MAC) address of the #NMDeviceBt
98  *
99  * Returns: the hardware address. This is the internal string used by the
100  * device, and must not be modified.
101  **/
102 const char *
103 nm_device_bt_get_hw_address (NMDeviceBt *device)
104 {
105         g_return_val_if_fail (NM_IS_DEVICE_BT (device), NULL);
106
107         _nm_object_ensure_inited (NM_OBJECT (device));
108         return NM_DEVICE_BT_GET_PRIVATE (device)->hw_address;
109 }
110
111 /**
112  * nm_device_bt_get_name:
113  * @device: a #NMDeviceBt
114  *
115  * Gets the name of the #NMDeviceBt.
116  *
117  * Returns: the name of the device
118  **/
119 const char *
120 nm_device_bt_get_name (NMDeviceBt *device)
121 {
122         g_return_val_if_fail (NM_IS_DEVICE_BT (device), NULL);
123
124         _nm_object_ensure_inited (NM_OBJECT (device));
125         return NM_DEVICE_BT_GET_PRIVATE (device)->name;
126 }
127
128 /**
129  * nm_device_bt_get_capabilities:
130  * @device: a #NMDeviceBt
131  *
132  * Returns the Bluetooth device's usable capabilities.
133  *
134  * Returns: a combination of #NMBluetoothCapabilities
135  **/
136 NMBluetoothCapabilities
137 nm_device_bt_get_capabilities (NMDeviceBt *device)
138 {
139         g_return_val_if_fail (NM_IS_DEVICE_BT (device), NM_BT_CAPABILITY_NONE);
140
141         _nm_object_ensure_inited (NM_OBJECT (device));
142         return NM_DEVICE_BT_GET_PRIVATE (device)->bt_capabilities;
143 }
144
145 static NMBluetoothCapabilities
146 get_connection_bt_type (NMConnection *connection)
147 {
148         NMSettingBluetooth *s_bt;
149         const char *bt_type;
150
151         s_bt = nm_connection_get_setting_bluetooth (connection);
152         if (!s_bt)
153                 return NM_BT_CAPABILITY_NONE;
154
155         bt_type = nm_setting_bluetooth_get_connection_type (s_bt);
156         g_assert (bt_type);
157
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;
162
163         return NM_BT_CAPABILITY_NONE;
164 }
165
166 static gboolean
167 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
168 {
169         NMSettingConnection *s_con;
170         NMSettingBluetooth *s_bt;
171         const char *ctype;
172         const GByteArray *mac;
173         const char *hw_str;
174         struct ether_addr *hw_mac;
175         NMBluetoothCapabilities dev_caps;
176         NMBluetoothCapabilities bt_type;
177
178         s_con = nm_connection_get_setting_connection (connection);
179         g_assert (s_con);
180
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.");
185                 return FALSE;
186         }
187
188         s_bt = nm_connection_get_setting_bluetooth (connection);
189         if (!s_bt) {
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.");
192                 return FALSE;
193         }
194
195         /* Check BT address */
196         hw_str = nm_device_bt_get_hw_address (NM_DEVICE_BT (device));
197         if (hw_str) {
198                 hw_mac = ether_aton (hw_str);
199                 if (!hw_mac) {
200                         g_set_error (error, NM_DEVICE_BT_ERROR, NM_DEVICE_BT_ERROR_INVALID_DEVICE_MAC,
201                                      "Invalid device MAC address.");
202                         return FALSE;
203                 }
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.");
208                         return FALSE;
209                 }
210         }
211
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.");
217                 return FALSE;
218         }
219
220         return NM_DEVICE_CLASS (nm_device_bt_parent_class)->connection_compatible (device, connection, error);
221 }
222
223 static GType
224 get_setting_type (NMDevice *device)
225 {
226         return NM_TYPE_SETTING_BLUETOOTH;
227 }
228
229 static const char *
230 get_hw_address (NMDevice *device)
231 {
232         return nm_device_bt_get_hw_address (NM_DEVICE_BT (device));
233 }
234
235 /************************************************************/
236
237 static void
238 nm_device_bt_init (NMDeviceBt *device)
239 {
240         _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_BT);
241 }
242
243 static void
244 register_properties (NMDeviceBt *device)
245 {
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 },
251                 { NULL },
252         };
253
254         _nm_object_register_properties (NM_OBJECT (device),
255                                         priv->proxy,
256                                         property_info);
257 }
258
259 static void
260 constructed (GObject *object)
261 {
262         NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (object);
263
264         G_OBJECT_CLASS (nm_device_bt_parent_class)->constructed (object);
265
266         priv->proxy = _nm_object_new_proxy (NM_OBJECT (object), NULL, NM_DBUS_INTERFACE_DEVICE_BLUETOOTH);
267         register_properties (NM_DEVICE_BT (object));
268 }
269
270 static void
271 dispose (GObject *object)
272 {
273         NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (object);
274
275         g_clear_object (&priv->proxy);
276
277         G_OBJECT_CLASS (nm_device_bt_parent_class)->dispose (object);
278 }
279
280 static void
281 finalize (GObject *object)
282 {
283         NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (object);
284
285         g_free (priv->hw_address);
286         g_free (priv->name);
287
288         G_OBJECT_CLASS (nm_device_bt_parent_class)->finalize (object);
289 }
290
291 static void
292 get_property (GObject *object,
293               guint prop_id,
294               GValue *value,
295               GParamSpec *pspec)
296 {
297         NMDeviceBt *device = NM_DEVICE_BT (object);
298
299         _nm_object_ensure_inited (NM_OBJECT (object));
300
301         switch (prop_id) {
302         case PROP_HW_ADDRESS:
303                 g_value_set_string (value, nm_device_bt_get_hw_address (device));
304                 break;
305         case PROP_NAME:
306                 g_value_set_string (value, nm_device_bt_get_name (device));
307                 break;
308         case PROP_BT_CAPABILITIES:
309                 g_value_set_uint (value, nm_device_bt_get_capabilities (device));
310                 break;
311         default:
312                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
313                 break;
314         }
315 }
316
317 static void
318 nm_device_bt_class_init (NMDeviceBtClass *bt_class)
319 {
320         GObjectClass *object_class = G_OBJECT_CLASS (bt_class);
321         NMDeviceClass *device_class = NM_DEVICE_CLASS (bt_class);
322
323         g_type_class_add_private (bt_class, sizeof (NMDeviceBtPrivate));
324
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;
333
334         /* properties */
335
336         /**
337          * NMDeviceBt:hw-address:
338          *
339          * The hardware (MAC) address of the device.
340          **/
341         g_object_class_install_property
342                 (object_class, PROP_HW_ADDRESS,
343                  g_param_spec_string (NM_DEVICE_BT_HW_ADDRESS, "", "",
344                                       NULL,
345                                       G_PARAM_READABLE |
346                                       G_PARAM_STATIC_STRINGS));
347
348         /**
349          * NMDeviceBt:name:
350          *
351          * The name of the bluetooth device.
352          **/
353         g_object_class_install_property
354                 (object_class, PROP_NAME,
355                  g_param_spec_string (NM_DEVICE_BT_NAME, "", "",
356                                       NULL,
357                                       G_PARAM_READABLE |
358                                       G_PARAM_STATIC_STRINGS));
359
360         /**
361          * NMDeviceBt:bt-capabilities:
362          *
363          * The device's bluetooth capabilities, a combination of #NMBluetoothCapabilities.
364          **/
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,
369                                     G_PARAM_READABLE |
370                                     G_PARAM_STATIC_STRINGS));
371
372 }