device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-core / nm-setting-bluetooth.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
3 /*
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301 USA.
18  *
19  * Copyright 2007 - 2013 Red Hat, Inc.
20  * Copyright 2007 - 2008 Novell, Inc.
21  */
22
23 #include "nm-default.h"
24
25 #include <string.h>
26 #include <net/ethernet.h>
27
28 #include "nm-setting-bluetooth.h"
29 #include "nm-setting-cdma.h"
30 #include "nm-setting-gsm.h"
31 #include "nm-setting-private.h"
32 #include "nm-utils.h"
33 #include "nm-utils-private.h"
34
35 /**
36  * SECTION:nm-setting-bluetooth
37  * @short_description: Describes Bluetooth connection properties
38  *
39  * The #NMSettingBluetooth object is a #NMSetting subclass that describes
40  * properties necessary for connection to devices that provide network
41  * connections via the Bluetooth Dial-Up Networking (DUN) and Network Access
42  * Point (NAP) profiles.
43  **/
44
45 G_DEFINE_TYPE_WITH_CODE (NMSettingBluetooth, nm_setting_bluetooth, NM_TYPE_SETTING,
46                          _nm_register_setting (BLUETOOTH, 1))
47 NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_BLUETOOTH)
48
49 #define NM_SETTING_BLUETOOTH_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_BLUETOOTH, NMSettingBluetoothPrivate))
50
51 typedef struct {
52         char *bdaddr;
53         char *type;
54 } NMSettingBluetoothPrivate;
55
56 enum {
57         PROP_0,
58         PROP_BDADDR,
59         PROP_TYPE,
60
61         LAST_PROP
62 };
63
64 /**
65  * nm_setting_bluetooth_new:
66  *
67  * Creates a new #NMSettingBluetooth object with default values.
68  *
69  * Returns: (transfer full): the new empty #NMSettingBluetooth object
70  **/
71 NMSetting *nm_setting_bluetooth_new (void)
72 {
73         return (NMSetting *) g_object_new (NM_TYPE_SETTING_BLUETOOTH, NULL);
74 }
75
76 /**
77  * nm_setting_bluetooth_get_connection_type:
78  * @setting: the #NMSettingBluetooth
79  *
80  * Returns the connection method for communicating with the remote device (i.e.
81  * either DUN to a DUN-capable device or PANU to a NAP-capable device).
82  *
83  * Returns: the type, either %NM_SETTING_BLUETOOTH_TYPE_PANU or
84  * %NM_SETTING_BLUETOOTH_TYPE_DUN
85  **/
86 const char *
87 nm_setting_bluetooth_get_connection_type (NMSettingBluetooth *setting)
88 {
89         g_return_val_if_fail (NM_IS_SETTING_BLUETOOTH (setting), NULL);
90
91         return NM_SETTING_BLUETOOTH_GET_PRIVATE (setting)->type;
92 }
93
94 /**
95  * nm_setting_bluetooth_get_bdaddr:
96  * @setting: the #NMSettingBluetooth
97  *
98  * Gets the Bluetooth address of the remote device which this setting
99  * describes a connection to.
100  *
101  * Returns: the Bluetooth address
102  **/
103 const char *
104 nm_setting_bluetooth_get_bdaddr (NMSettingBluetooth *setting)
105 {
106         g_return_val_if_fail (NM_IS_SETTING_BLUETOOTH (setting), NULL);
107
108         return NM_SETTING_BLUETOOTH_GET_PRIVATE (setting)->bdaddr;
109 }
110
111 static gboolean
112 verify (NMSetting *setting, NMConnection *connection, GError **error)
113 {
114         NMSettingBluetoothPrivate *priv = NM_SETTING_BLUETOOTH_GET_PRIVATE (setting);
115
116         if (!priv->bdaddr) {
117                 g_set_error_literal (error,
118                                      NM_CONNECTION_ERROR,
119                                      NM_CONNECTION_ERROR_MISSING_PROPERTY,
120                                      _("property is missing"));
121                 g_prefix_error (error, "%s.%s: ", NM_SETTING_BLUETOOTH_SETTING_NAME, NM_SETTING_BLUETOOTH_BDADDR);
122                 return FALSE;
123         }
124
125         if (!nm_utils_hwaddr_valid (priv->bdaddr, ETH_ALEN)) {
126                 g_set_error_literal (error,
127                                      NM_CONNECTION_ERROR,
128                                      NM_CONNECTION_ERROR_INVALID_PROPERTY,
129                                      _("property is invalid"));
130                 g_prefix_error (error, "%s.%s: ", NM_SETTING_BLUETOOTH_SETTING_NAME, NM_SETTING_BLUETOOTH_BDADDR);
131                 return FALSE;
132         }
133
134         if (!priv->type) {
135                 g_set_error_literal (error,
136                                      NM_CONNECTION_ERROR,
137                                      NM_CONNECTION_ERROR_MISSING_PROPERTY,
138                                      _("property is missing"));
139                 g_prefix_error (error, "%s.%s: ", NM_SETTING_BLUETOOTH_SETTING_NAME, NM_SETTING_BLUETOOTH_TYPE);
140                 return FALSE;
141         } else if (!g_str_equal (priv->type, NM_SETTING_BLUETOOTH_TYPE_DUN) &&
142                    !g_str_equal (priv->type, NM_SETTING_BLUETOOTH_TYPE_PANU)) {
143                 g_set_error (error,
144                              NM_CONNECTION_ERROR,
145                              NM_CONNECTION_ERROR_INVALID_PROPERTY,
146                              _("'%s' is not a valid value for the property"),
147                              priv->type);
148                 g_prefix_error (error, "%s.%s: ", NM_SETTING_BLUETOOTH_SETTING_NAME, NM_SETTING_BLUETOOTH_TYPE);
149                 return FALSE;
150         }
151
152         /* Make sure the corresponding 'type' setting is present */
153         if (   connection
154             && !strcmp (priv->type, NM_SETTING_BLUETOOTH_TYPE_DUN)) {
155                 gboolean gsm = FALSE, cdma = FALSE;
156
157                 gsm = !!nm_connection_get_setting_gsm (connection);
158                 cdma = !!nm_connection_get_setting_cdma (connection);
159
160                 if (!gsm && !cdma) {
161                         /* We can't return MISSING_SETTING here, because we don't know
162                          * whether to prefix the message with NM_SETTING_GSM_SETTING_NAME or
163                          * NM_SETTING_CDMA_SETTING_NAME.
164                          */
165                         g_set_error (error,
166                                      NM_CONNECTION_ERROR,
167                                      NM_CONNECTION_ERROR_INVALID_SETTING,
168                                      _("'%s' connection requires '%s' or '%s' setting"),
169                                      NM_SETTING_BLUETOOTH_TYPE_DUN,
170                                      NM_SETTING_GSM_SETTING_NAME, NM_SETTING_CDMA_SETTING_NAME);
171                         g_prefix_error (error, "%s: ", NM_SETTING_BLUETOOTH_SETTING_NAME);
172                         return FALSE;
173                 }
174         }
175         /* PANU doesn't need a 'type' setting since no further configuration
176          * is required at the interface level.
177          */
178
179         return TRUE;
180 }
181
182 static void
183 nm_setting_bluetooth_init (NMSettingBluetooth *setting)
184 {
185 }
186
187 static void
188 finalize (GObject *object)
189 {
190         NMSettingBluetoothPrivate *priv = NM_SETTING_BLUETOOTH_GET_PRIVATE (object);
191
192         g_free (priv->bdaddr);
193         g_free (priv->type);
194
195         G_OBJECT_CLASS (nm_setting_bluetooth_parent_class)->finalize (object);
196 }
197
198 static void
199 set_property (GObject *object, guint prop_id,
200               const GValue *value, GParamSpec *pspec)
201 {
202         NMSettingBluetoothPrivate *priv = NM_SETTING_BLUETOOTH_GET_PRIVATE (object);
203
204         switch (prop_id) {
205         case PROP_BDADDR:
206                 g_free (priv->bdaddr);
207                 priv->bdaddr = g_value_dup_string (value);
208                 break;
209         case PROP_TYPE:
210                 g_free (priv->type);
211                 priv->type = g_value_dup_string (value);
212                 break;
213         default:
214                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215                 break;
216         }
217 }
218
219 static void
220 get_property (GObject *object, guint prop_id,
221               GValue *value, GParamSpec *pspec)
222 {
223         NMSettingBluetooth *setting = NM_SETTING_BLUETOOTH (object);
224
225         switch (prop_id) {
226         case PROP_BDADDR:
227                 g_value_set_string (value, nm_setting_bluetooth_get_bdaddr (setting));
228                 break;
229         case PROP_TYPE:
230                 g_value_set_string (value, nm_setting_bluetooth_get_connection_type (setting));
231                 break;
232         default:
233                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
234                 break;
235         }
236 }
237
238 static void
239 nm_setting_bluetooth_class_init (NMSettingBluetoothClass *setting_class)
240 {
241         GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
242         NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);
243
244         g_type_class_add_private (setting_class, sizeof (NMSettingBluetoothPrivate));
245
246         /* virtual methods */
247         object_class->set_property = set_property;
248         object_class->get_property = get_property;
249         object_class->finalize     = finalize;
250         parent_class->verify       = verify;
251
252         /* Properties */
253
254         /**
255          * NMSettingBluetooth:bdaddr:
256          *
257          * The Bluetooth address of the device.
258          **/
259         g_object_class_install_property
260                 (object_class, PROP_BDADDR,
261                  g_param_spec_string (NM_SETTING_BLUETOOTH_BDADDR, "", "",
262                                       NULL,
263                                       G_PARAM_READWRITE |
264                                       NM_SETTING_PARAM_INFERRABLE |
265                                       G_PARAM_STATIC_STRINGS));
266         _nm_setting_class_transform_property (parent_class, NM_SETTING_BLUETOOTH_BDADDR,
267                                               G_VARIANT_TYPE_BYTESTRING,
268                                               _nm_utils_hwaddr_to_dbus,
269                                               _nm_utils_hwaddr_from_dbus);
270
271         /**
272          * NMSettingBluetooth:type:
273          *
274          * Either "dun" for Dial-Up Networking connections or "panu" for Personal
275          * Area Networking connections to devices supporting the NAP profile.
276          **/
277         g_object_class_install_property
278                 (object_class, PROP_TYPE,
279                  g_param_spec_string (NM_SETTING_BLUETOOTH_TYPE, "", "",
280                                       NULL,
281                                       G_PARAM_READWRITE |
282                                       NM_SETTING_PARAM_INFERRABLE |
283                                       G_PARAM_STATIC_STRINGS));
284 }