device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm / 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
29 #include "nm-wimax-nsp.h"
30 #include "nm-dbus-interface.h"
31 #include "nm-object-private.h"
32 #include "nm-enum-types.h"
33
34 G_DEFINE_TYPE (NMWimaxNsp, nm_wimax_nsp, NM_TYPE_OBJECT)
35
36 #define NM_WIMAX_NSP_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_WIMAX_NSP, NMWimaxNspPrivate))
37
38 typedef struct {
39         char *name;
40         guint32 signal_quality;
41         NMWimaxNspNetworkType network_type;
42 } NMWimaxNspPrivate;
43
44 enum {
45         PROP_0,
46         PROP_NAME,
47         PROP_SIGNAL_QUALITY,
48         PROP_NETWORK_TYPE,
49
50         LAST_PROP
51 };
52
53 /**
54  * nm_wimax_nsp_get_name:
55  * @nsp: a #NMWimaxNsp
56  *
57  * Gets the name of the wimax NSP
58  *
59  * Returns: the name
60  **/
61 const char *
62 nm_wimax_nsp_get_name (NMWimaxNsp *nsp)
63 {
64         g_return_val_if_fail (NM_IS_WIMAX_NSP (nsp), NULL);
65
66         return NM_WIMAX_NSP_GET_PRIVATE (nsp)->name;
67 }
68
69 /**
70  * nm_wimax_nsp_get_signal_quality:
71  * @nsp: a #NMWimaxNsp
72  *
73  * Gets the WPA signal quality of the wimax NSP.
74  *
75  * Returns: the signal quality
76  **/
77 guint32
78 nm_wimax_nsp_get_signal_quality (NMWimaxNsp *nsp)
79 {
80         g_return_val_if_fail (NM_IS_WIMAX_NSP (nsp), 0);
81
82         return NM_WIMAX_NSP_GET_PRIVATE (nsp)->signal_quality;
83 }
84
85 /**
86  * nm_wimax_nsp_get_network_type:
87  * @nsp: a #NMWimaxNsp
88  *
89  * Gets the network type of the wimax NSP.
90  *
91  * Returns: the network type
92  **/
93 NMWimaxNspNetworkType
94 nm_wimax_nsp_get_network_type (NMWimaxNsp *nsp)
95 {
96         g_return_val_if_fail (NM_IS_WIMAX_NSP (nsp), NM_WIMAX_NSP_NETWORK_TYPE_UNKNOWN);
97
98         return NM_WIMAX_NSP_GET_PRIVATE (nsp)->network_type;
99 }
100
101 /**
102  * nm_wimax_nsp_connection_valid:
103  * @nsp: an #NMWimaxNsp to validate @connection against
104  * @connection: an #NMConnection to validate against @nsp
105  *
106  * Validates a given connection against a given WiMAX NSP to ensure that the
107  * connection may be activated with that NSP.  The connection must match the
108  * @nsp's network name and other attributes.
109  *
110  * Returns: %TRUE if the connection may be activated with this WiMAX NSP,
111  * %FALSE if it cannot be.
112  **/
113 gboolean
114 nm_wimax_nsp_connection_valid (NMWimaxNsp *nsp, NMConnection *connection)
115 {
116         NMSettingConnection *s_con;
117         NMSettingWimax *s_wimax;
118         const char *ctype;
119         const char *nsp_name;
120         const char *setting_name;
121
122         s_con = nm_connection_get_setting_connection (connection);
123         g_assert (s_con);
124         ctype = nm_setting_connection_get_connection_type (s_con);
125         if (strcmp (ctype, NM_SETTING_WIMAX_SETTING_NAME) != 0)
126                 return FALSE;
127
128         s_wimax = nm_connection_get_setting_wimax (connection);
129         if (!s_wimax)
130                 return FALSE;
131
132         setting_name = nm_setting_wimax_get_network_name (s_wimax);
133         if (!setting_name)
134                 return FALSE;
135
136         nsp_name = nm_wimax_nsp_get_name (nsp);
137         g_warn_if_fail (nsp_name != NULL);
138         if (g_strcmp0 (nsp_name, setting_name) != 0)
139                 return FALSE;
140
141         return TRUE;
142 }
143
144 /**
145  * nm_wimax_nsp_filter_connections:
146  * @nsp: an #NMWimaxNsp to filter connections for
147  * @connections: (element-type NMConnection): an array of #NMConnections to
148  * filter
149  *
150  * Filters a given array of connections for a given #NMWimaxNsp object and
151  * return connections which may be activated with the NSP.  Any returned
152  * connections will match the @nsp's network name and other attributes.
153  *
154  * Returns: (transfer container) (element-type NMConnection): an array of
155  * #NMConnections that could be activated with the given @nsp.  The array should
156  * be freed with g_ptr_array_unref() when it is no longer required.
157  **/
158 GPtrArray *
159 nm_wimax_nsp_filter_connections (NMWimaxNsp *nsp, const GPtrArray *connections)
160 {
161         GPtrArray *filtered;
162         int i;
163
164         filtered = g_ptr_array_new_with_free_func (g_object_unref);
165         for (i = 0; i < connections->len; i++) {
166                 NMConnection *candidate = connections->pdata[i];
167
168                 if (nm_wimax_nsp_connection_valid (nsp, candidate))
169                         g_ptr_array_add (filtered, g_object_ref (candidate));
170         }
171
172         return filtered;
173 }
174
175 /************************************************************/
176
177 static void
178 nm_wimax_nsp_init (NMWimaxNsp *nsp)
179 {
180 }
181
182 static void
183 finalize (GObject *object)
184 {
185         NMWimaxNspPrivate *priv = NM_WIMAX_NSP_GET_PRIVATE (object);
186
187         g_free (priv->name);
188
189         G_OBJECT_CLASS (nm_wimax_nsp_parent_class)->finalize (object);
190 }
191
192 static void
193 get_property (GObject *object,
194               guint prop_id,
195               GValue *value,
196               GParamSpec *pspec)
197 {
198         NMWimaxNsp *nsp = NM_WIMAX_NSP (object);
199
200         switch (prop_id) {
201         case PROP_NAME:
202                 g_value_set_string (value, nm_wimax_nsp_get_name (nsp));
203                 break;
204         case PROP_SIGNAL_QUALITY:
205                 g_value_set_uint (value, nm_wimax_nsp_get_signal_quality (nsp));
206                 break;
207         case PROP_NETWORK_TYPE:
208                 g_value_set_enum (value, nm_wimax_nsp_get_network_type (nsp));
209                 break;
210         default:
211                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212                 break;
213         }
214 }
215
216 static void
217 init_dbus (NMObject *object)
218 {
219         NMWimaxNspPrivate *priv = NM_WIMAX_NSP_GET_PRIVATE (object);
220         const NMPropertiesInfo property_info[] = {
221                 { NM_WIMAX_NSP_NAME,           &priv->name },
222                 { NM_WIMAX_NSP_SIGNAL_QUALITY, &priv->signal_quality },
223                 { NM_WIMAX_NSP_NETWORK_TYPE,   &priv->network_type },
224                 { NULL },
225         };
226
227         NM_OBJECT_CLASS (nm_wimax_nsp_parent_class)->init_dbus (object);
228
229         _nm_object_register_properties (object,
230                                         NM_DBUS_INTERFACE_WIMAX_NSP,
231                                         property_info);
232 }
233
234 static void
235 nm_wimax_nsp_class_init (NMWimaxNspClass *nsp_class)
236 {
237         GObjectClass *object_class = G_OBJECT_CLASS (nsp_class);
238         NMObjectClass *nm_object_class = NM_OBJECT_CLASS (nsp_class);
239
240         g_type_class_add_private (nsp_class, sizeof (NMWimaxNspPrivate));
241
242         _nm_object_class_add_interface (nm_object_class, NM_DBUS_INTERFACE_WIMAX_NSP);
243
244         /* virtual methods */
245         object_class->get_property = get_property;
246         object_class->finalize = finalize;
247
248         nm_object_class->init_dbus = init_dbus;
249
250         /* properties */
251
252         /**
253          * NMWimaxNsp:name:
254          *
255          * The name of the WiMAX NSP.
256          **/
257         g_object_class_install_property
258                 (object_class, PROP_NAME,
259                  g_param_spec_string (NM_WIMAX_NSP_NAME, "", "",
260                                       NULL,
261                                       G_PARAM_READABLE |
262                                       G_PARAM_STATIC_STRINGS));
263
264         /**
265          * NMWimaxNsp:signal-quality:
266          *
267          * The signal quality of the WiMAX NSP.
268          **/
269         g_object_class_install_property
270                 (object_class, PROP_SIGNAL_QUALITY,
271                  g_param_spec_uint (NM_WIMAX_NSP_SIGNAL_QUALITY, "", "",
272                                     0, 100, 0,
273                                     G_PARAM_READABLE |
274                                     G_PARAM_STATIC_STRINGS));
275
276         /**
277          * NMWimaxNsp:network-type:
278          *
279          * The network type of the WiMAX NSP.
280          **/
281         g_object_class_install_property
282                 (object_class, PROP_NETWORK_TYPE,
283                  g_param_spec_enum (NM_WIMAX_NSP_NETWORK_TYPE, "", "",
284                                     NM_TYPE_WIMAX_NSP_NETWORK_TYPE,
285                                     NM_WIMAX_NSP_NETWORK_TYPE_UNKNOWN,
286                                     G_PARAM_READABLE |
287                                     G_PARAM_STATIC_STRINGS));
288 }