device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-glib / nm-device-adsl.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  * author: Pantelis Koukousoulas <pktoss@gmail.com>
19  * Copyright 2009 - 2011 Red Hat, Inc.
20  */
21
22 #include "nm-default.h"
23
24 #include "nm-device-adsl.h"
25
26 #include <string.h>
27
28 #include "nm-device-private.h"
29 #include "nm-object-private.h"
30
31 #include "nm-setting-adsl.h"
32
33 G_DEFINE_TYPE (NMDeviceAdsl, nm_device_adsl, NM_TYPE_DEVICE)
34
35 #define NM_DEVICE_ADSL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_ADSL, NMDeviceAdslPrivate))
36
37 typedef struct {
38         DBusGProxy *proxy;
39
40         gboolean carrier;
41
42         gboolean disposed;
43 } NMDeviceAdslPrivate;
44
45 enum {
46         PROP_0,
47         PROP_CARRIER,
48         LAST_PROP
49 };
50
51 /**
52  * nm_device_adsl_error_quark:
53  *
54  * Registers an error quark for #NMDeviceAdsl if necessary.
55  *
56  * Returns: the error quark used for #NMDeviceAdsl errors.
57  **/
58 GQuark
59 nm_device_adsl_error_quark (void)
60 {
61         static GQuark quark = 0;
62
63         if (G_UNLIKELY (quark == 0))
64                 quark = g_quark_from_static_string ("nm-device-adsl-error-quark");
65         return quark;
66 }
67
68 /**
69  * nm_device_adsl_new:
70  * @connection: the #DBusGConnection
71  * @path: the DBus object path of the device
72  *
73  * Creates a new #NMDeviceAdsl.
74  *
75  * Returns: (transfer full): a new device
76  **/
77 GObject *
78 nm_device_adsl_new (DBusGConnection *connection, const char *path)
79 {
80         GObject *device;
81
82         g_return_val_if_fail (connection != NULL, NULL);
83         g_return_val_if_fail (path != NULL, NULL);
84
85         device = g_object_new (NM_TYPE_DEVICE_ADSL,
86                                NM_OBJECT_DBUS_CONNECTION, connection,
87                                NM_OBJECT_DBUS_PATH, path,
88                                NULL);
89         _nm_object_ensure_inited (NM_OBJECT (device));
90         return device;
91 }
92
93 /**
94  * nm_device_adsl_get_carrier:
95  * @device: a #NMDeviceAdsl
96  *
97  * Whether the device has carrier.
98  *
99  * Returns: %TRUE if the device has carrier
100  **/
101 gboolean
102 nm_device_adsl_get_carrier (NMDeviceAdsl *device)
103 {
104         g_return_val_if_fail (NM_IS_DEVICE_ADSL (device), FALSE);
105
106         _nm_object_ensure_inited (NM_OBJECT (device));
107         return NM_DEVICE_ADSL_GET_PRIVATE (device)->carrier;
108 }
109
110 static gboolean
111 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
112 {
113         NMSettingConnection *s_con;
114         NMSettingAdsl *s_adsl;
115         const char *ctype;
116
117         s_con = nm_connection_get_setting_connection (connection);
118         g_assert (s_con);
119
120         ctype = nm_setting_connection_get_connection_type (s_con);
121         if (strcmp (ctype, NM_SETTING_ADSL_SETTING_NAME) != 0) {
122                 g_set_error (error, NM_DEVICE_ADSL_ERROR, NM_DEVICE_ADSL_ERROR_NOT_ADSL_CONNECTION,
123                              "The connection was not an ADSL connection.");
124                 return FALSE;
125         }
126
127         s_adsl = nm_connection_get_setting_adsl (connection);
128         if (!s_adsl) {
129                 g_set_error (error, NM_DEVICE_ADSL_ERROR, NM_DEVICE_ADSL_ERROR_INVALID_ADSL_CONNECTION,
130                              "The connection was not a valid ADSL connection.");
131                 return FALSE;
132         }
133
134         return NM_DEVICE_CLASS (nm_device_adsl_parent_class)->connection_compatible (device, connection, error);
135 }
136
137 static GType
138 get_setting_type (NMDevice *device)
139 {
140         return NM_TYPE_SETTING_ADSL;
141 }
142
143 /******************************************************************/
144
145 static void
146 nm_device_adsl_init (NMDeviceAdsl *device)
147 {
148         _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_ADSL);
149 }
150
151 static void
152 register_properties (NMDeviceAdsl *device)
153 {
154         NMDeviceAdslPrivate *priv = NM_DEVICE_ADSL_GET_PRIVATE (device);
155         const NMPropertiesInfo property_info[] = {
156                 { NM_DEVICE_ADSL_CARRIER,              &priv->carrier },
157                 { NULL },
158         };
159
160         _nm_object_register_properties (NM_OBJECT (device),
161                                         priv->proxy,
162                                         property_info);
163 }
164
165 static void
166 constructed (GObject *object)
167 {
168         NMDeviceAdslPrivate *priv = NM_DEVICE_ADSL_GET_PRIVATE (object);
169
170         G_OBJECT_CLASS (nm_device_adsl_parent_class)->constructed (object);
171
172         priv->proxy = _nm_object_new_proxy (NM_OBJECT (object), NULL, NM_DBUS_INTERFACE_DEVICE_ADSL);
173         register_properties (NM_DEVICE_ADSL (object));
174 }
175
176 static void
177 dispose (GObject *object)
178 {
179         NMDeviceAdslPrivate *priv = NM_DEVICE_ADSL_GET_PRIVATE (object);
180
181         if (priv->disposed) {
182                 G_OBJECT_CLASS (nm_device_adsl_parent_class)->dispose (object);
183                 return;
184         }
185
186         priv->disposed = TRUE;
187
188         g_object_unref (priv->proxy);
189
190         G_OBJECT_CLASS (nm_device_adsl_parent_class)->dispose (object);
191 }
192
193 static void
194 finalize (GObject *object)
195 {
196         G_OBJECT_CLASS (nm_device_adsl_parent_class)->finalize (object);
197 }
198
199 static void
200 get_property (GObject *object,
201               guint prop_id,
202               GValue *value,
203               GParamSpec *pspec)
204 {
205         NMDeviceAdsl *device = NM_DEVICE_ADSL (object);
206
207         switch (prop_id) {
208         case PROP_CARRIER:
209                 g_value_set_boolean (value, nm_device_adsl_get_carrier (device));
210                 break;
211         default:
212                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213                 break;
214         }
215 }
216
217 static void
218 nm_device_adsl_class_init (NMDeviceAdslClass *adsl_class)
219 {
220         GObjectClass *object_class = G_OBJECT_CLASS (adsl_class);
221         NMDeviceClass *device_class = NM_DEVICE_CLASS (adsl_class);
222
223         g_type_class_add_private (object_class, sizeof (NMDeviceAdslPrivate));
224
225         /* virtual methods */
226         object_class->constructed = constructed;
227         object_class->dispose = dispose;
228         object_class->finalize = finalize;
229         object_class->get_property = get_property;
230         device_class->connection_compatible = connection_compatible;
231         device_class->get_setting_type = get_setting_type;
232
233         /* properties */
234         /**
235          * NMDeviceAdsl:carrier:
236          *
237          * Whether the device has carrier.
238          **/
239         g_object_class_install_property
240                 (object_class, PROP_CARRIER,
241                  g_param_spec_boolean (NM_DEVICE_ADSL_CARRIER, "", "",
242                                        FALSE,
243                                        G_PARAM_READABLE |
244                                        G_PARAM_STATIC_STRINGS));
245 }