device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-util / 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-param-spec-specialized.h"
29 #include "nm-dbus-glib-types.h"
30 #include "nm-setting-bluetooth.h"
31 #include "nm-setting-cdma.h"
32 #include "nm-setting-gsm.h"
33 #include "nm-setting-private.h"
34
35 /**
36  * SECTION:nm-setting-bluetooth
37  * @short_description: Describes Bluetooth connection properties
38  * @include: nm-setting-bluetooth.h
39  *
40  * The #NMSettingBluetooth object is a #NMSetting subclass that describes
41  * properties necessary for connection to devices that provide network
42  * connections via the Bluetooth Dial-Up Networking (DUN) and Network Access
43  * Point (NAP) profiles.
44  **/
45
46 /**
47  * nm_setting_bluetooth_error_quark:
48  *
49  * Registers an error quark for #NMSettingBluetooth if necessary.
50  *
51  * Returns: the error quark used for #NMSettingBluetooth errors.
52  **/
53 GQuark
54 nm_setting_bluetooth_error_quark (void)
55 {
56         static GQuark quark;
57
58         if (G_UNLIKELY (!quark))
59                 quark = g_quark_from_static_string ("nm-setting-bluetooth-error-quark");
60         return quark;
61 }
62
63
64 G_DEFINE_TYPE_WITH_CODE (NMSettingBluetooth, nm_setting_bluetooth, NM_TYPE_SETTING,
65                          _nm_register_setting (NM_SETTING_BLUETOOTH_SETTING_NAME,
66                                                g_define_type_id,
67                                                1,
68                                                NM_SETTING_BLUETOOTH_ERROR))
69 NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_BLUETOOTH)
70
71 #define NM_SETTING_BLUETOOTH_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_BLUETOOTH, NMSettingBluetoothPrivate))
72
73 typedef struct {
74         GByteArray *bdaddr;
75         char *type;
76 } NMSettingBluetoothPrivate;
77
78 enum {
79         PROP_0,
80         PROP_BDADDR,
81         PROP_TYPE,
82
83         LAST_PROP
84 };
85
86 /**
87  * nm_setting_bluetooth_new:
88  *
89  * Creates a new #NMSettingBluetooth object with default values.
90  *
91  * Returns: (transfer full): the new empty #NMSettingBluetooth object
92  **/
93 NMSetting *nm_setting_bluetooth_new (void)
94 {
95         return (NMSetting *) g_object_new (NM_TYPE_SETTING_BLUETOOTH, NULL);
96 }
97
98 /**
99  * nm_setting_bluetooth_get_connection_type:
100  * @setting: the #NMSettingBluetooth
101  *
102  * Returns the connection method for communicating with the remote device (i.e.
103  * either DUN to a DUN-capable device or PANU to a NAP-capable device).
104  *
105  * Returns: the type, either %NM_SETTING_BLUETOOTH_TYPE_PANU or
106  *   %NM_SETTING_BLUETOOTH_TYPE_DUN
107  **/
108 const char *
109 nm_setting_bluetooth_get_connection_type (NMSettingBluetooth *setting)
110 {
111         g_return_val_if_fail (NM_IS_SETTING_BLUETOOTH (setting), NULL);
112
113         return NM_SETTING_BLUETOOTH_GET_PRIVATE (setting)->type;
114 }
115
116 /**
117  * nm_setting_bluetooth_get_bdaddr:
118  * @setting: the #NMSettingBluetooth
119  *
120  * Gets the Bluetooth address of the remote device which this setting
121  * describes a connection to.
122  *
123  * Returns: the Bluetooth address
124  **/
125 const GByteArray *
126 nm_setting_bluetooth_get_bdaddr (NMSettingBluetooth *setting)
127 {
128         g_return_val_if_fail (NM_IS_SETTING_BLUETOOTH (setting), NULL);
129
130         return NM_SETTING_BLUETOOTH_GET_PRIVATE (setting)->bdaddr;
131 }
132
133 static gboolean
134 verify (NMSetting *setting, GSList *all_settings, GError **error)
135 {
136         NMSettingBluetoothPrivate *priv = NM_SETTING_BLUETOOTH_GET_PRIVATE (setting);
137
138         if (!priv->bdaddr) {
139                 g_set_error_literal (error,
140                                      NM_SETTING_BLUETOOTH_ERROR,
141                                      NM_SETTING_BLUETOOTH_ERROR_MISSING_PROPERTY,
142                                      _("property is missing"));
143                 g_prefix_error (error, "%s.%s: ", NM_SETTING_BLUETOOTH_SETTING_NAME, NM_SETTING_BLUETOOTH_BDADDR);
144                 return FALSE;
145         }
146
147         if (priv->bdaddr && priv->bdaddr->len != ETH_ALEN) {
148                 g_set_error_literal (error,
149                                      NM_SETTING_BLUETOOTH_ERROR,
150                                      NM_SETTING_BLUETOOTH_ERROR_INVALID_PROPERTY,
151                                      _("property is invalid"));
152                 g_prefix_error (error, "%s.%s: ", NM_SETTING_BLUETOOTH_SETTING_NAME, NM_SETTING_BLUETOOTH_BDADDR);
153                 return FALSE;
154         }
155
156         if (!priv->type) {
157                 g_set_error_literal (error,
158                                      NM_SETTING_BLUETOOTH_ERROR,
159                                      NM_SETTING_BLUETOOTH_ERROR_MISSING_PROPERTY,
160                                      _("property is missing"));
161                 g_prefix_error (error, "%s.%s: ", NM_SETTING_BLUETOOTH_SETTING_NAME, NM_SETTING_BLUETOOTH_TYPE);
162                 return FALSE;
163         } else if (!g_str_equal (priv->type, NM_SETTING_BLUETOOTH_TYPE_DUN) &&
164                    !g_str_equal (priv->type, NM_SETTING_BLUETOOTH_TYPE_PANU)) {
165                 g_set_error (error,
166                              NM_SETTING_BLUETOOTH_ERROR,
167                              NM_SETTING_BLUETOOTH_ERROR_INVALID_PROPERTY,
168                              _("'%s' is not a valid value for the property"),
169                              priv->type);
170                 g_prefix_error (error, "%s.%s: ", NM_SETTING_BLUETOOTH_SETTING_NAME, NM_SETTING_BLUETOOTH_TYPE);
171                 return FALSE;
172         }
173
174         /* Make sure the corresponding 'type' setting is present */
175         if (   all_settings
176             && !strcmp (priv->type, NM_SETTING_BLUETOOTH_TYPE_DUN)) {
177                 gboolean gsm = FALSE, cdma = FALSE;
178
179                 gsm = !!nm_setting_find_in_list (all_settings, NM_SETTING_GSM_SETTING_NAME);
180                 cdma = !!nm_setting_find_in_list (all_settings, NM_SETTING_CDMA_SETTING_NAME);
181
182                 if (!gsm && !cdma) {
183                         g_set_error (error,
184                                      NM_SETTING_BLUETOOTH_ERROR,
185                                      NM_SETTING_BLUETOOTH_ERROR_TYPE_SETTING_NOT_FOUND,
186                                      _("requires '%s' or '%s' setting"),
187                                      NM_SETTING_GSM_SETTING_NAME, NM_SETTING_CDMA_SETTING_NAME);
188                         g_prefix_error (error, "%s.%s: ", NM_SETTING_BLUETOOTH_SETTING_NAME, NM_SETTING_BLUETOOTH_TYPE);
189                         return FALSE;
190                 }
191         }
192         /* PANU doesn't need a 'type' setting since no further configuration
193          * is required at the interface level.
194          */
195
196         return TRUE;
197 }
198
199 static void
200 nm_setting_bluetooth_init (NMSettingBluetooth *setting)
201 {
202 }
203
204 static void
205 finalize (GObject *object)
206 {
207         NMSettingBluetoothPrivate *priv = NM_SETTING_BLUETOOTH_GET_PRIVATE (object);
208
209         if (priv->bdaddr)
210                 g_byte_array_free (priv->bdaddr, TRUE);
211         g_free (priv->type);
212
213         G_OBJECT_CLASS (nm_setting_bluetooth_parent_class)->finalize (object);
214 }
215
216 static void
217 set_property (GObject *object, guint prop_id,
218               const GValue *value, GParamSpec *pspec)
219 {
220         NMSettingBluetoothPrivate *priv = NM_SETTING_BLUETOOTH_GET_PRIVATE (object);
221
222         switch (prop_id) {
223         case PROP_BDADDR:
224                 if (priv->bdaddr)
225                         g_byte_array_free (priv->bdaddr, TRUE);
226                 priv->bdaddr = g_value_dup_boxed (value);
227                 break;
228         case PROP_TYPE:
229                 g_free (priv->type);
230                 priv->type = g_value_dup_string (value);
231                 break;
232         default:
233                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
234                 break;
235         }
236 }
237
238 static void
239 get_property (GObject *object, guint prop_id,
240               GValue *value, GParamSpec *pspec)
241 {
242         NMSettingBluetooth *setting = NM_SETTING_BLUETOOTH (object);
243
244         switch (prop_id) {
245         case PROP_BDADDR:
246                 g_value_set_boxed (value, nm_setting_bluetooth_get_bdaddr (setting));
247                 break;
248         case PROP_TYPE:
249                 g_value_set_string (value, nm_setting_bluetooth_get_connection_type (setting));
250                 break;
251         default:
252                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
253                 break;
254         }
255 }
256
257 static void
258 nm_setting_bluetooth_class_init (NMSettingBluetoothClass *setting_class)
259 {
260         GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
261         NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);
262
263         g_type_class_add_private (setting_class, sizeof (NMSettingBluetoothPrivate));
264
265         /* virtual methods */
266         object_class->set_property = set_property;
267         object_class->get_property = get_property;
268         object_class->finalize     = finalize;
269         parent_class->verify       = verify;
270
271         /* Properties */
272
273         /**
274          * NMSettingBluetooth:bdaddr:
275          *
276          * The Bluetooth address of the device.
277          **/
278         g_object_class_install_property
279                 (object_class, PROP_BDADDR,
280                  _nm_param_spec_specialized (NM_SETTING_BLUETOOTH_BDADDR, "", "",
281                                              DBUS_TYPE_G_UCHAR_ARRAY,
282                                              G_PARAM_READWRITE |
283                                              NM_SETTING_PARAM_INFERRABLE |
284                                              G_PARAM_STATIC_STRINGS));
285
286         /**
287          * NMSettingBluetooth:type:
288          *
289          * Either "dun" for Dial-Up Networking connections or "panu" for Personal
290          * Area Networking connections to devices supporting the NAP profile.
291          **/
292         g_object_class_install_property
293                 (object_class, PROP_TYPE,
294                  g_param_spec_string (NM_SETTING_BLUETOOTH_TYPE, "", "",
295                                       NULL,
296                                       G_PARAM_READWRITE |
297                                       NM_SETTING_PARAM_INFERRABLE |
298                                       G_PARAM_STATIC_STRINGS));
299 }