device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-glib / nm-wimax-nsp.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 2011 Red Hat, Inc.
19  */
20
21 #include "nm-default.h"
22
23 #include <string.h>
24
25 #include "nm-connection.h"
26 #include "nm-setting-connection.h"
27 #include "nm-setting-wimax.h"
28 #include "nm-wimax-nsp.h"
29 #include "NetworkManager.h"
30 #include "nm-types-private.h"
31 #include "nm-object-private.h"
32
33 G_DEFINE_TYPE (NMWimaxNsp, nm_wimax_nsp, NM_TYPE_OBJECT)
34
35 #define NM_WIMAX_NSP_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_WIMAX_NSP, NMWimaxNspPrivate))
36
37 typedef struct {
38         DBusGProxy *proxy;
39
40         char *name;
41         guint32 signal_quality;
42         NMWimaxNspNetworkType network_type;
43 } NMWimaxNspPrivate;
44
45 enum {
46         PROP_0,
47         PROP_NAME,
48         PROP_SIGNAL_QUALITY,
49         PROP_NETWORK_TYPE,
50
51         LAST_PROP
52 };
53
54 /**
55  * nm_wimax_nsp_new:
56  * @connection: the #DBusGConnection
57  * @path: the D-Bus object path of the WiMAX NSP
58  *
59  * Creates a new #NMWimaxNsp.
60  *
61  * Returns: (transfer full): a new WiMAX NSP
62  **/
63 GObject *
64 nm_wimax_nsp_new (DBusGConnection *connection, const char *path)
65 {
66         g_return_val_if_fail (connection != NULL, NULL);
67         g_return_val_if_fail (path != NULL, NULL);
68
69         return (GObject *) g_object_new (NM_TYPE_WIMAX_NSP,
70                                          NM_OBJECT_DBUS_CONNECTION, connection,
71                                          NM_OBJECT_DBUS_PATH, path,
72                                          NULL);
73 }
74
75 /**
76  * nm_wimax_nsp_get_name:
77  * @nsp: a #NMWimaxNsp
78  *
79  * Gets the name of the wimax NSP
80  *
81  * Returns: the name
82  **/
83 const char *
84 nm_wimax_nsp_get_name (NMWimaxNsp *nsp)
85 {
86         g_return_val_if_fail (NM_IS_WIMAX_NSP (nsp), NULL);
87
88         _nm_object_ensure_inited (NM_OBJECT (nsp));
89         return NM_WIMAX_NSP_GET_PRIVATE (nsp)->name;
90 }
91
92 /**
93  * nm_wimax_nsp_get_signal_quality:
94  * @nsp: a #NMWimaxNsp
95  *
96  * Gets the WPA signal quality of the wimax NSP.
97  *
98  * Returns: the signal quality
99  **/
100 guint32
101 nm_wimax_nsp_get_signal_quality (NMWimaxNsp *nsp)
102 {
103         g_return_val_if_fail (NM_IS_WIMAX_NSP (nsp), 0);
104
105         _nm_object_ensure_inited (NM_OBJECT (nsp));
106         return NM_WIMAX_NSP_GET_PRIVATE (nsp)->signal_quality;
107 }
108
109 /**
110  * nm_wimax_nsp_get_network_type:
111  * @nsp: a #NMWimaxNsp
112  *
113  * Gets the network type of the wimax NSP.
114  *
115  * Returns: the network type
116  **/
117 NMWimaxNspNetworkType
118 nm_wimax_nsp_get_network_type (NMWimaxNsp *nsp)
119 {
120         g_return_val_if_fail (NM_IS_WIMAX_NSP (nsp), NM_WIMAX_NSP_NETWORK_TYPE_UNKNOWN);
121
122         _nm_object_ensure_inited (NM_OBJECT (nsp));
123         return NM_WIMAX_NSP_GET_PRIVATE (nsp)->network_type;
124 }
125
126 /**
127  * nm_wimax_nsp_connection_valid:
128  * @nsp: an #NMWimaxNsp to validate @connection against
129  * @connection: an #NMConnection to validate against @nsp
130  *
131  * Validates a given connection against a given WiMAX NSP to ensure that the
132  * connection may be activated with that NSP.  The connection must match the
133  * @nsp's network name and other attributes.
134  *
135  * Returns: %TRUE if the connection may be activated with this WiMAX NSP,
136  * %FALSE if it cannot be.
137  **/
138 gboolean
139 nm_wimax_nsp_connection_valid (NMWimaxNsp *nsp, NMConnection *connection)
140 {
141         NMSettingConnection *s_con;
142         NMSettingWimax *s_wimax;
143         const char *ctype;
144         const char *nsp_name;
145         const char *setting_name;
146
147         s_con = nm_connection_get_setting_connection (connection);
148         g_assert (s_con);
149         ctype = nm_setting_connection_get_connection_type (s_con);
150         if (strcmp (ctype, NM_SETTING_WIMAX_SETTING_NAME) != 0)
151                 return FALSE;
152
153         s_wimax = nm_connection_get_setting_wimax (connection);
154         if (!s_wimax)
155                 return FALSE;
156
157         setting_name = nm_setting_wimax_get_network_name (s_wimax);
158         if (!setting_name)
159                 return FALSE;
160
161         nsp_name = nm_wimax_nsp_get_name (nsp);
162         g_warn_if_fail (nsp_name != NULL);
163         if (g_strcmp0 (nsp_name, setting_name) != 0)
164                 return FALSE;
165
166         return TRUE;
167 }
168
169 /**
170  * nm_wimax_nsp_filter_connections:
171  * @nsp: an #NMWimaxNsp to filter connections for
172  * @connections: (element-type NMConnection): a list of
173  * #NMConnection objects to filter
174  *
175  * Filters a given list of connections for a given #NMWimaxNsp object and
176  * return connections which may be activated with the access point.  Any
177  * returned connections will match the @nsp's network name and other attributes.
178  *
179  * Returns: (transfer container) (element-type NMConnection): a
180  * list of #NMConnection objects that could be activated with the given @nsp.
181  * The elements of the list are owned by their creator and should not be freed
182  * by the caller, but the returned list itself is owned by the caller and should
183  * be freed with g_slist_free() when it is no longer required.
184  **/
185 GSList *
186 nm_wimax_nsp_filter_connections (NMWimaxNsp *nsp, const GSList *connections)
187 {
188         GSList *filtered = NULL;
189         const GSList *iter;
190
191         for (iter = connections; iter; iter = g_slist_next (iter)) {
192                 NMConnection *candidate = NM_CONNECTION (iter->data);
193
194                 if (nm_wimax_nsp_connection_valid (nsp, candidate))
195                         filtered = g_slist_prepend (filtered, candidate);
196         }
197
198         return g_slist_reverse (filtered);
199 }
200
201 /************************************************************/
202
203 static void
204 nm_wimax_nsp_init (NMWimaxNsp *nsp)
205 {
206 }
207
208 static void
209 dispose (GObject *object)
210 {
211         NMWimaxNspPrivate *priv = NM_WIMAX_NSP_GET_PRIVATE (object);
212
213         g_clear_object (&priv->proxy);
214
215         G_OBJECT_CLASS (nm_wimax_nsp_parent_class)->dispose (object);
216 }
217
218 static void
219 finalize (GObject *object)
220 {
221         NMWimaxNspPrivate *priv = NM_WIMAX_NSP_GET_PRIVATE (object);
222
223         g_free (priv->name);
224
225         G_OBJECT_CLASS (nm_wimax_nsp_parent_class)->finalize (object);
226 }
227
228 static void
229 get_property (GObject *object,
230               guint prop_id,
231               GValue *value,
232               GParamSpec *pspec)
233 {
234         NMWimaxNsp *nsp = NM_WIMAX_NSP (object);
235
236         _nm_object_ensure_inited (NM_OBJECT (object));
237
238         switch (prop_id) {
239         case PROP_NAME:
240                 g_value_set_string (value, nm_wimax_nsp_get_name (nsp));
241                 break;
242         case PROP_SIGNAL_QUALITY:
243                 g_value_set_uint (value, nm_wimax_nsp_get_signal_quality (nsp));
244                 break;
245         case PROP_NETWORK_TYPE:
246                 g_value_set_uint (value, nm_wimax_nsp_get_network_type (nsp));
247                 break;
248         default:
249                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
250                 break;
251         }
252 }
253
254 static void
255 register_properties (NMWimaxNsp *nsp)
256 {
257         NMWimaxNspPrivate *priv = NM_WIMAX_NSP_GET_PRIVATE (nsp);
258         const NMPropertiesInfo property_info[] = {
259                 { NM_WIMAX_NSP_NAME,           &priv->name },
260                 { NM_WIMAX_NSP_SIGNAL_QUALITY, &priv->signal_quality },
261                 { NM_WIMAX_NSP_NETWORK_TYPE,   &priv->network_type },
262                 { NULL },
263         };
264
265         _nm_object_register_properties (NM_OBJECT (nsp),
266                                         priv->proxy,
267                                         property_info);
268 }
269
270 static void
271 constructed (GObject *object)
272 {
273         NMWimaxNspPrivate *priv = NM_WIMAX_NSP_GET_PRIVATE (object);
274
275         G_OBJECT_CLASS (nm_wimax_nsp_parent_class)->constructed (object);
276
277         priv->proxy = _nm_object_new_proxy (NM_OBJECT (object), NULL, NM_DBUS_INTERFACE_WIMAX_NSP);
278         register_properties (NM_WIMAX_NSP (object));
279 }
280
281
282 static void
283 nm_wimax_nsp_class_init (NMWimaxNspClass *nsp_class)
284 {
285         GObjectClass *object_class = G_OBJECT_CLASS (nsp_class);
286
287         g_type_class_add_private (nsp_class, sizeof (NMWimaxNspPrivate));
288
289         /* virtual methods */
290         object_class->constructed = constructed;
291         object_class->get_property = get_property;
292         object_class->dispose = dispose;
293         object_class->finalize = finalize;
294
295         /* properties */
296
297         /**
298          * NMWimaxNsp:name:
299          *
300          * The name of the WiMAX NSP.
301          **/
302         g_object_class_install_property
303                 (object_class, PROP_NAME,
304                  g_param_spec_string (NM_WIMAX_NSP_NAME, "", "",
305                                       NULL,
306                                       G_PARAM_READABLE |
307                                       G_PARAM_STATIC_STRINGS));
308
309         /**
310          * NMWimaxNsp:signal-quality:
311          *
312          * The signal quality of the WiMAX NSP.
313          **/
314         g_object_class_install_property
315                 (object_class, PROP_SIGNAL_QUALITY,
316                  g_param_spec_uint (NM_WIMAX_NSP_SIGNAL_QUALITY, "", "",
317                                     0, 100, 0,
318                                     G_PARAM_READABLE |
319                                     G_PARAM_STATIC_STRINGS));
320
321         /**
322          * NMWimaxNsp:network-type:
323          *
324          * The network type of the WiMAX NSP.
325          **/
326         g_object_class_install_property
327                 (object_class, PROP_NETWORK_TYPE,
328                  g_param_spec_uint (NM_WIMAX_NSP_NETWORK_TYPE, "", "",
329                                     0, G_MAXUINT32, 0,
330                                     G_PARAM_READABLE |
331                                     G_PARAM_STATIC_STRINGS));
332 }