device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm / 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
26 #include "nm-setting-connection.h"
27 #include "nm-setting-bluetooth.h"
28 #include "nm-utils.h"
29
30 #include "nm-device-bt.h"
31 #include "nm-device-private.h"
32 #include "nm-object-private.h"
33 #include "nm-enum-types.h"
34
35 G_DEFINE_TYPE (NMDeviceBt, nm_device_bt, NM_TYPE_DEVICE)
36
37 #define NM_DEVICE_BT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_BT, NMDeviceBtPrivate))
38
39 typedef struct {
40         char *hw_address;
41         char *name;
42         guint32 bt_capabilities;
43 } NMDeviceBtPrivate;
44
45 enum {
46         PROP_0,
47         PROP_HW_ADDRESS,
48         PROP_NAME,
49         PROP_BT_CAPABILITIES,
50
51         LAST_PROP
52 };
53
54 /**
55  * nm_device_bt_get_hw_address:
56  * @device: a #NMDeviceBt
57  *
58  * Gets the hardware (MAC) address of the #NMDeviceBt
59  *
60  * Returns: the hardware address. This is the internal string used by the
61  * device, and must not be modified.
62  **/
63 const char *
64 nm_device_bt_get_hw_address (NMDeviceBt *device)
65 {
66         g_return_val_if_fail (NM_IS_DEVICE_BT (device), NULL);
67
68         return NM_DEVICE_BT_GET_PRIVATE (device)->hw_address;
69 }
70
71 /**
72  * nm_device_bt_get_name:
73  * @device: a #NMDeviceBt
74  *
75  * Gets the name of the #NMDeviceBt.
76  *
77  * Returns: the name of the device
78  **/
79 const char *
80 nm_device_bt_get_name (NMDeviceBt *device)
81 {
82         g_return_val_if_fail (NM_IS_DEVICE_BT (device), NULL);
83
84         return NM_DEVICE_BT_GET_PRIVATE (device)->name;
85 }
86
87 /**
88  * nm_device_bt_get_capabilities:
89  * @device: a #NMDeviceBt
90  *
91  * Returns the Bluetooth device's usable capabilities.
92  *
93  * Returns: a combination of #NMBluetoothCapabilities
94  **/
95 NMBluetoothCapabilities
96 nm_device_bt_get_capabilities (NMDeviceBt *device)
97 {
98         g_return_val_if_fail (NM_IS_DEVICE_BT (device), NM_BT_CAPABILITY_NONE);
99
100         return NM_DEVICE_BT_GET_PRIVATE (device)->bt_capabilities;
101 }
102
103 static NMBluetoothCapabilities
104 get_connection_bt_type (NMConnection *connection)
105 {
106         NMSettingBluetooth *s_bt;
107         const char *bt_type;
108
109         s_bt = nm_connection_get_setting_bluetooth (connection);
110         if (!s_bt)
111                 return NM_BT_CAPABILITY_NONE;
112
113         bt_type = nm_setting_bluetooth_get_connection_type (s_bt);
114         g_assert (bt_type);
115
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;
120
121         return NM_BT_CAPABILITY_NONE;
122 }
123
124 static gboolean
125 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
126 {
127         NMSettingBluetooth *s_bt;
128         const char *hw_addr, *setting_addr;
129         NMBluetoothCapabilities dev_caps;
130         NMBluetoothCapabilities bt_type;
131
132         if (!NM_DEVICE_CLASS (nm_device_bt_parent_class)->connection_compatible (device, connection, error))
133                 return FALSE;
134
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."));
138                 return FALSE;
139         }
140
141         /* Check BT address */
142         hw_addr = nm_device_bt_get_hw_address (NM_DEVICE_BT (device));
143         if (hw_addr) {
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."));
147                         return FALSE;
148                 }
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."));
154                         return FALSE;
155                 }
156         }
157
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."));
163                 return FALSE;
164         }
165
166         return TRUE;
167 }
168
169 static GType
170 get_setting_type (NMDevice *device)
171 {
172         return NM_TYPE_SETTING_BLUETOOTH;
173 }
174
175 static const char *
176 get_hw_address (NMDevice *device)
177 {
178         return nm_device_bt_get_hw_address (NM_DEVICE_BT (device));
179 }
180
181 /************************************************************/
182
183 static void
184 nm_device_bt_init (NMDeviceBt *device)
185 {
186         _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_BT);
187 }
188
189 static void
190 init_dbus (NMObject *object)
191 {
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 },
197                 { NULL },
198         };
199
200         NM_OBJECT_CLASS (nm_device_bt_parent_class)->init_dbus (object);
201
202         _nm_object_register_properties (object,
203                                         NM_DBUS_INTERFACE_DEVICE_BLUETOOTH,
204                                         property_info);
205 }
206
207 static void
208 finalize (GObject *object)
209 {
210         NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (object);
211
212         g_free (priv->hw_address);
213         g_free (priv->name);
214
215         G_OBJECT_CLASS (nm_device_bt_parent_class)->finalize (object);
216 }
217
218 static void
219 get_property (GObject *object,
220               guint prop_id,
221               GValue *value,
222               GParamSpec *pspec)
223 {
224         NMDeviceBt *device = NM_DEVICE_BT (object);
225
226         switch (prop_id) {
227         case PROP_HW_ADDRESS:
228                 g_value_set_string (value, nm_device_bt_get_hw_address (device));
229                 break;
230         case PROP_NAME:
231                 g_value_set_string (value, nm_device_bt_get_name (device));
232                 break;
233         case PROP_BT_CAPABILITIES:
234                 g_value_set_flags (value, nm_device_bt_get_capabilities (device));
235                 break;
236         default:
237                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238                 break;
239         }
240 }
241
242 static void
243 nm_device_bt_class_init (NMDeviceBtClass *bt_class)
244 {
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);
248
249         g_type_class_add_private (bt_class, sizeof (NMDeviceBtPrivate));
250
251         _nm_object_class_add_interface (nm_object_class, NM_DBUS_INTERFACE_DEVICE_BLUETOOTH);
252
253         /* virtual methods */
254         object_class->finalize = finalize;
255         object_class->get_property = get_property;
256
257         nm_object_class->init_dbus = init_dbus;
258
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;
262
263         /* properties */
264
265         /**
266          * NMDeviceBt:hw-address:
267          *
268          * The hardware (MAC) address of the device.
269          **/
270         g_object_class_install_property
271                 (object_class, PROP_HW_ADDRESS,
272                  g_param_spec_string (NM_DEVICE_BT_HW_ADDRESS, "", "",
273                                       NULL,
274                                       G_PARAM_READABLE |
275                                       G_PARAM_STATIC_STRINGS));
276
277         /**
278          * NMDeviceBt:name:
279          *
280          * The name of the bluetooth device.
281          **/
282         g_object_class_install_property
283                 (object_class, PROP_NAME,
284                  g_param_spec_string (NM_DEVICE_BT_NAME, "", "",
285                                       NULL,
286                                       G_PARAM_READABLE |
287                                       G_PARAM_STATIC_STRINGS));
288
289         /**
290          * NMDeviceBt:bt-capabilities:
291          *
292          * The device's bluetooth capabilities, a combination of #NMBluetoothCapabilities.
293          **/
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,
299                                      G_PARAM_READABLE |
300                                      G_PARAM_STATIC_STRINGS));
301
302 }