device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-glib / nm-device-olpc-mesh.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 2012 Red Hat, Inc.
19  */
20
21 #include "nm-default.h"
22
23 #include <string.h>
24
25 #include "nm-setting-connection.h"
26 #include "nm-setting-olpc-mesh.h"
27
28 #include "nm-device-olpc-mesh.h"
29 #include "nm-device-private.h"
30 #include "nm-object-private.h"
31 #include "nm-device-wifi.h"
32
33 G_DEFINE_TYPE (NMDeviceOlpcMesh, nm_device_olpc_mesh, NM_TYPE_DEVICE)
34
35 #define NM_DEVICE_OLPC_MESH_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_OLPC_MESH, NMDeviceOlpcMeshPrivate))
36
37 typedef struct {
38         DBusGProxy *proxy;
39
40         char *hw_address;
41         NMDeviceWifi *companion;
42         guint32 active_channel;
43 } NMDeviceOlpcMeshPrivate;
44
45 enum {
46         PROP_0,
47         PROP_HW_ADDRESS,
48         PROP_COMPANION,
49         PROP_ACTIVE_CHANNEL,
50
51         LAST_PROP
52 };
53
54 /**
55  * nm_device_olpc_mesh_error_quark:
56  *
57  * Registers an error quark for #NMDeviceOlpcMesh if necessary.
58  *
59  * Returns: the error quark used for #NMDeviceOlpcMesh errors.
60  **/
61 GQuark
62 nm_device_olpc_mesh_error_quark (void)
63 {
64         static GQuark quark = 0;
65
66         if (G_UNLIKELY (quark == 0))
67                 quark = g_quark_from_static_string ("nm-device-olpc-mesh-error-quark");
68         return quark;
69 }
70
71 /**
72  * nm_device_olpc_mesh_new:
73  * @connection: the #DBusGConnection
74  * @path: the DBus object path of the device
75  *
76  * Creates a new #NMDeviceOlpcMesh.
77  *
78  * Returns: (transfer full): a new OlpcMesh device
79  **/
80 GObject *
81 nm_device_olpc_mesh_new (DBusGConnection *connection, const char *path)
82 {
83         GObject *device;
84
85         g_return_val_if_fail (connection != NULL, NULL);
86         g_return_val_if_fail (path != NULL, NULL);
87
88         device = g_object_new (NM_TYPE_DEVICE_OLPC_MESH,
89                                NM_OBJECT_DBUS_CONNECTION, connection,
90                                NM_OBJECT_DBUS_PATH, path,
91                                NULL);
92
93         _nm_object_ensure_inited (NM_OBJECT (device));
94         return device;
95 }
96
97 /**
98  * nm_device_olpc_mesh_get_hw_address:
99  * @device: a #NMDeviceOlpcMesh
100  *
101  * Gets the hardware (MAC) address of the #NMDeviceOlpcMesh
102  *
103  * Returns: the hardware address. This is the internal string used by the
104  * device, and must not be modified.
105  **/
106 const char *
107 nm_device_olpc_mesh_get_hw_address (NMDeviceOlpcMesh *device)
108 {
109         g_return_val_if_fail (NM_IS_DEVICE_OLPC_MESH (device), NULL);
110
111         _nm_object_ensure_inited (NM_OBJECT (device));
112         return NM_DEVICE_OLPC_MESH_GET_PRIVATE (device)->hw_address;
113 }
114
115 /**
116  * nm_device_olpc_mesh_get_companion:
117  * @device: a #NMDeviceOlpcMesh
118  *
119  * Gets the companion device of the #NMDeviceOlpcMesh.
120  *
121  * Returns: (transfer none): the companion of the device of %NULL
122  **/
123 NMDeviceWifi *
124 nm_device_olpc_mesh_get_companion (NMDeviceOlpcMesh *device)
125 {
126         g_return_val_if_fail (NM_IS_DEVICE_OLPC_MESH (device), NULL);
127
128         _nm_object_ensure_inited (NM_OBJECT (device));
129         return NM_DEVICE_OLPC_MESH_GET_PRIVATE (device)->companion;
130 }
131
132 /**
133  * nm_device_olpc_mesh_get_active_channel:
134  * @device: a #NMDeviceOlpcMesh
135  *
136  * Returns the active channel of the #NMDeviceOlpcMesh device.
137  *
138  * Returns: active channel of the device
139  **/
140 guint32
141 nm_device_olpc_mesh_get_active_channel (NMDeviceOlpcMesh *device)
142 {
143         g_return_val_if_fail (NM_IS_DEVICE_OLPC_MESH (device), 0);
144
145         _nm_object_ensure_inited (NM_OBJECT (device));
146         return NM_DEVICE_OLPC_MESH_GET_PRIVATE (device)->active_channel;
147 }
148
149 static const char *
150 get_hw_address (NMDevice *device)
151 {
152         return nm_device_olpc_mesh_get_hw_address (NM_DEVICE_OLPC_MESH (device));
153 }
154
155 static gboolean
156 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
157 {
158         NMSettingConnection *s_con;
159         NMSettingOlpcMesh *s_olpc_mesh;
160         const char *ctype;
161
162         s_con = nm_connection_get_setting_connection (connection);
163         g_assert (s_con);
164
165         ctype = nm_setting_connection_get_connection_type (s_con);
166         if (strcmp (ctype, NM_SETTING_OLPC_MESH_SETTING_NAME) != 0) {
167                 g_set_error (error, NM_DEVICE_OLPC_MESH_ERROR, NM_DEVICE_OLPC_MESH_ERROR_NOT_OLPC_MESH_CONNECTION,
168                              "The connection was not a Olpc Mesh connection.");
169                 return FALSE;
170         }
171
172         s_olpc_mesh = nm_connection_get_setting_olpc_mesh (connection);
173         if (!s_olpc_mesh) {
174                 g_set_error (error, NM_DEVICE_OLPC_MESH_ERROR, NM_DEVICE_OLPC_MESH_ERROR_INVALID_OLPC_MESH_CONNECTION,
175                              "The connection was not a valid Olpc Mesh connection.");
176                 return FALSE;
177         }
178
179         return NM_DEVICE_CLASS (nm_device_olpc_mesh_parent_class)->connection_compatible (device, connection, error);
180 }
181
182 static GType
183 get_setting_type (NMDevice *device)
184 {
185         return NM_TYPE_SETTING_OLPC_MESH;
186 }
187
188 /**************************************************************/
189
190 static void
191 nm_device_olpc_mesh_init (NMDeviceOlpcMesh *device)
192 {
193         _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_OLPC_MESH);
194 }
195
196 static void
197 register_properties (NMDeviceOlpcMesh *device)
198 {
199         NMDeviceOlpcMeshPrivate *priv = NM_DEVICE_OLPC_MESH_GET_PRIVATE (device);
200         const NMPropertiesInfo property_info[] = {
201                 { NM_DEVICE_OLPC_MESH_HW_ADDRESS,     &priv->hw_address },
202                 { NM_DEVICE_OLPC_MESH_COMPANION,      &priv->companion, NULL, NM_TYPE_DEVICE_WIFI },
203                 { NM_DEVICE_OLPC_MESH_ACTIVE_CHANNEL, &priv->active_channel },
204                 { NULL },
205         };
206
207         _nm_object_register_properties (NM_OBJECT (device),
208                                         priv->proxy,
209                                         property_info);
210 }
211
212 static void
213 constructed (GObject *object)
214 {
215         NMDeviceOlpcMeshPrivate *priv = NM_DEVICE_OLPC_MESH_GET_PRIVATE (object);
216
217         G_OBJECT_CLASS (nm_device_olpc_mesh_parent_class)->constructed (object);
218
219         priv->proxy = _nm_object_new_proxy (NM_OBJECT (object), NULL, NM_DBUS_INTERFACE_DEVICE_OLPC_MESH);
220         register_properties (NM_DEVICE_OLPC_MESH (object));
221 }
222
223 static void
224 dispose (GObject *object)
225 {
226         NMDeviceOlpcMeshPrivate *priv = NM_DEVICE_OLPC_MESH_GET_PRIVATE (object);
227
228         g_clear_object (&priv->companion);
229         g_clear_object (&priv->proxy);
230
231         G_OBJECT_CLASS (nm_device_olpc_mesh_parent_class)->dispose (object);
232 }
233
234 static void
235 finalize (GObject *object)
236 {
237         NMDeviceOlpcMeshPrivate *priv = NM_DEVICE_OLPC_MESH_GET_PRIVATE (object);
238
239         g_free (priv->hw_address);
240
241         G_OBJECT_CLASS (nm_device_olpc_mesh_parent_class)->finalize (object);
242 }
243
244 static void
245 get_property (GObject *object,
246               guint prop_id,
247               GValue *value,
248               GParamSpec *pspec)
249 {
250         NMDeviceOlpcMesh *device = NM_DEVICE_OLPC_MESH (object);
251
252         _nm_object_ensure_inited (NM_OBJECT (object));
253
254         switch (prop_id) {
255         case PROP_HW_ADDRESS:
256                 g_value_set_string (value, nm_device_olpc_mesh_get_hw_address (device));
257                 break;
258         case PROP_COMPANION:
259                 g_value_set_object (value, nm_device_olpc_mesh_get_companion (device));
260                 break;
261         case PROP_ACTIVE_CHANNEL:
262                 g_value_set_uint (value, nm_device_olpc_mesh_get_active_channel (device));
263                 break;
264         default:
265                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
266                 break;
267         }
268 }
269
270 static void
271 nm_device_olpc_mesh_class_init (NMDeviceOlpcMeshClass *olpc_mesh_class)
272 {
273         GObjectClass *object_class = G_OBJECT_CLASS (olpc_mesh_class);
274         NMDeviceClass *device_class = NM_DEVICE_CLASS (olpc_mesh_class);
275
276         g_type_class_add_private (olpc_mesh_class, sizeof (NMDeviceOlpcMeshPrivate));
277
278         /* virtual methods */
279         object_class->constructed = constructed;
280         object_class->dispose = dispose;
281         object_class->finalize = finalize;
282         object_class->get_property = get_property;
283         device_class->connection_compatible = connection_compatible;
284         device_class->get_setting_type = get_setting_type;
285         device_class->get_hw_address = get_hw_address;
286
287         /* properties */
288
289         /**
290          * NMDeviceOlpcMesh:hw-address:
291          *
292          * The hardware (MAC) address of the device.
293          **/
294         g_object_class_install_property
295                 (object_class, PROP_HW_ADDRESS,
296                  g_param_spec_string (NM_DEVICE_OLPC_MESH_HW_ADDRESS, "", "",
297                                       NULL,
298                                       G_PARAM_READABLE |
299                                       G_PARAM_STATIC_STRINGS));
300
301         /**
302          * NMDeviceOlpcMesh:companion:
303          *
304          * The companion device.
305          **/
306         g_object_class_install_property
307                 (object_class, PROP_COMPANION,
308                  g_param_spec_object (NM_DEVICE_OLPC_MESH_COMPANION, "", "",
309                                       NM_TYPE_DEVICE_WIFI,
310                                       G_PARAM_READABLE |
311                                       G_PARAM_STATIC_STRINGS));
312
313         /**
314          * NMDeviceOlpcMesh:active-channel:
315          *
316          * The device's active channel.
317          **/
318         g_object_class_install_property
319                 (object_class, PROP_ACTIVE_CHANNEL,
320                  g_param_spec_uint (NM_DEVICE_OLPC_MESH_ACTIVE_CHANNEL, "", "",
321                                     0, G_MAXUINT32, 0,
322                                     G_PARAM_READABLE |
323                                     G_PARAM_STATIC_STRINGS));
324
325 }