device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm / 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         char *hw_address;
39         NMDeviceWifi *companion;
40         guint32 active_channel;
41 } NMDeviceOlpcMeshPrivate;
42
43 enum {
44         PROP_0,
45         PROP_HW_ADDRESS,
46         PROP_COMPANION,
47         PROP_ACTIVE_CHANNEL,
48
49         LAST_PROP
50 };
51
52 /**
53  * nm_device_olpc_mesh_get_hw_address:
54  * @device: a #NMDeviceOlpcMesh
55  *
56  * Gets the hardware (MAC) address of the #NMDeviceOlpcMesh
57  *
58  * Returns: the hardware address. This is the internal string used by the
59  * device, and must not be modified.
60  **/
61 const char *
62 nm_device_olpc_mesh_get_hw_address (NMDeviceOlpcMesh *device)
63 {
64         g_return_val_if_fail (NM_IS_DEVICE_OLPC_MESH (device), NULL);
65
66         return NM_DEVICE_OLPC_MESH_GET_PRIVATE (device)->hw_address;
67 }
68
69 /**
70  * nm_device_olpc_mesh_get_companion:
71  * @device: a #NMDeviceOlpcMesh
72  *
73  * Gets the companion device of the #NMDeviceOlpcMesh.
74  *
75  * Returns: (transfer none): the companion of the device of %NULL
76  **/
77 NMDeviceWifi *
78 nm_device_olpc_mesh_get_companion (NMDeviceOlpcMesh *device)
79 {
80         g_return_val_if_fail (NM_IS_DEVICE_OLPC_MESH (device), NULL);
81
82         return NM_DEVICE_OLPC_MESH_GET_PRIVATE (device)->companion;
83 }
84
85 /**
86  * nm_device_olpc_mesh_get_active_channel:
87  * @device: a #NMDeviceOlpcMesh
88  *
89  * Returns the active channel of the #NMDeviceOlpcMesh device.
90  *
91  * Returns: active channel of the device
92  **/
93 guint32
94 nm_device_olpc_mesh_get_active_channel (NMDeviceOlpcMesh *device)
95 {
96         g_return_val_if_fail (NM_IS_DEVICE_OLPC_MESH (device), 0);
97
98         return NM_DEVICE_OLPC_MESH_GET_PRIVATE (device)->active_channel;
99 }
100
101 static const char *
102 get_hw_address (NMDevice *device)
103 {
104         return nm_device_olpc_mesh_get_hw_address (NM_DEVICE_OLPC_MESH (device));
105 }
106
107 static gboolean
108 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
109 {
110         if (!NM_DEVICE_CLASS (nm_device_olpc_mesh_parent_class)->connection_compatible (device, connection, error))
111                 return FALSE;
112
113         if (!nm_connection_is_type (connection, NM_SETTING_OLPC_MESH_SETTING_NAME)) {
114                 g_set_error_literal (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION,
115                                      _("The connection was not an OLPC Mesh connection."));
116                 return FALSE;
117         }
118
119         return TRUE;
120 }
121
122 static GType
123 get_setting_type (NMDevice *device)
124 {
125         return NM_TYPE_SETTING_OLPC_MESH;
126 }
127
128 /**************************************************************/
129
130 static void
131 nm_device_olpc_mesh_init (NMDeviceOlpcMesh *device)
132 {
133         _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_OLPC_MESH);
134 }
135
136 static void
137 init_dbus (NMObject *object)
138 {
139         NMDeviceOlpcMeshPrivate *priv = NM_DEVICE_OLPC_MESH_GET_PRIVATE (object);
140         const NMPropertiesInfo property_info[] = {
141                 { NM_DEVICE_OLPC_MESH_HW_ADDRESS,     &priv->hw_address },
142                 { NM_DEVICE_OLPC_MESH_COMPANION,      &priv->companion, NULL, NM_TYPE_DEVICE_WIFI },
143                 { NM_DEVICE_OLPC_MESH_ACTIVE_CHANNEL, &priv->active_channel },
144                 { NULL },
145         };
146
147         NM_OBJECT_CLASS (nm_device_olpc_mesh_parent_class)->init_dbus (object);
148
149         _nm_object_register_properties (object,
150                                         NM_DBUS_INTERFACE_DEVICE_OLPC_MESH,
151                                         property_info);
152 }
153
154 static void
155 dispose (GObject *object)
156 {
157         NMDeviceOlpcMeshPrivate *priv = NM_DEVICE_OLPC_MESH_GET_PRIVATE (object);
158
159         g_clear_object (&priv->companion);
160
161         G_OBJECT_CLASS (nm_device_olpc_mesh_parent_class)->dispose (object);
162 }
163
164 static void
165 finalize (GObject *object)
166 {
167         NMDeviceOlpcMeshPrivate *priv = NM_DEVICE_OLPC_MESH_GET_PRIVATE (object);
168
169         g_free (priv->hw_address);
170
171         G_OBJECT_CLASS (nm_device_olpc_mesh_parent_class)->finalize (object);
172 }
173
174 static void
175 get_property (GObject *object,
176               guint prop_id,
177               GValue *value,
178               GParamSpec *pspec)
179 {
180         NMDeviceOlpcMesh *device = NM_DEVICE_OLPC_MESH (object);
181
182         switch (prop_id) {
183         case PROP_HW_ADDRESS:
184                 g_value_set_string (value, nm_device_olpc_mesh_get_hw_address (device));
185                 break;
186         case PROP_COMPANION:
187                 g_value_set_object (value, nm_device_olpc_mesh_get_companion (device));
188                 break;
189         case PROP_ACTIVE_CHANNEL:
190                 g_value_set_uint (value, nm_device_olpc_mesh_get_active_channel (device));
191                 break;
192         default:
193                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194                 break;
195         }
196 }
197
198 static void
199 nm_device_olpc_mesh_class_init (NMDeviceOlpcMeshClass *olpc_mesh_class)
200 {
201         GObjectClass *object_class = G_OBJECT_CLASS (olpc_mesh_class);
202         NMObjectClass *nm_object_class = NM_OBJECT_CLASS (olpc_mesh_class);
203         NMDeviceClass *device_class = NM_DEVICE_CLASS (olpc_mesh_class);
204
205         g_type_class_add_private (olpc_mesh_class, sizeof (NMDeviceOlpcMeshPrivate));
206
207         _nm_object_class_add_interface (nm_object_class, NM_DBUS_INTERFACE_DEVICE_OLPC_MESH);
208
209         /* virtual methods */
210         object_class->dispose = dispose;
211         object_class->finalize = finalize;
212         object_class->get_property = get_property;
213
214         nm_object_class->init_dbus = init_dbus;
215
216         device_class->connection_compatible = connection_compatible;
217         device_class->get_setting_type = get_setting_type;
218         device_class->get_hw_address = get_hw_address;
219
220         /* properties */
221
222         /**
223          * NMDeviceOlpcMesh:hw-address:
224          *
225          * The hardware (MAC) address of the device.
226          **/
227         g_object_class_install_property
228                 (object_class, PROP_HW_ADDRESS,
229                  g_param_spec_string (NM_DEVICE_OLPC_MESH_HW_ADDRESS, "", "",
230                                       NULL,
231                                       G_PARAM_READABLE |
232                                       G_PARAM_STATIC_STRINGS));
233
234         /**
235          * NMDeviceOlpcMesh:companion:
236          *
237          * The companion device.
238          **/
239         g_object_class_install_property
240                 (object_class, PROP_COMPANION,
241                  g_param_spec_object (NM_DEVICE_OLPC_MESH_COMPANION, "", "",
242                                       NM_TYPE_DEVICE_WIFI,
243                                       G_PARAM_READABLE |
244                                       G_PARAM_STATIC_STRINGS));
245
246         /**
247          * NMDeviceOlpcMesh:active-channel:
248          *
249          * The device's active channel.
250          **/
251         g_object_class_install_property
252                 (object_class, PROP_ACTIVE_CHANNEL,
253                  g_param_spec_uint (NM_DEVICE_OLPC_MESH_ACTIVE_CHANNEL, "", "",
254                                     0, G_MAXUINT32, 0,
255                                     G_PARAM_READABLE |
256                                     G_PARAM_STATIC_STRINGS));
257
258 }