device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-glib / nm-device-modem.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 - 2012 Red Hat, Inc.
19  * Copyright 2008 Novell, Inc.
20  */
21
22 #include "nm-default.h"
23
24 #include <string.h>
25
26 #include "nm-setting-connection.h"
27 #include "nm-setting-gsm.h"
28 #include "nm-setting-cdma.h"
29
30 #include "nm-device-modem.h"
31 #include "nm-device-private.h"
32 #include "nm-object-private.h"
33
34 G_DEFINE_TYPE (NMDeviceModem, nm_device_modem, NM_TYPE_DEVICE)
35
36 #define NM_DEVICE_MODEM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_MODEM, NMDeviceModemPrivate))
37
38 typedef struct {
39         DBusGProxy *proxy;
40
41         NMDeviceModemCapabilities caps;
42         NMDeviceModemCapabilities current_caps;
43 } NMDeviceModemPrivate;
44
45 enum {
46         PROP_0,
47         PROP_MODEM_CAPS,
48         PROP_CURRENT_CAPS,
49         LAST_PROP
50 };
51
52 /**
53  * nm_device_modem_error_quark:
54  *
55  * Registers an error quark for #NMDeviceModem if necessary.
56  *
57  * Returns: the error quark used for #NMDeviceModem errors.
58  **/
59 GQuark
60 nm_device_modem_error_quark (void)
61 {
62         static GQuark quark = 0;
63
64         if (G_UNLIKELY (quark == 0))
65                 quark = g_quark_from_static_string ("nm-device-modem-error-quark");
66         return quark;
67 }
68
69 /**
70  * nm_device_modem_get_modem_capabilities:
71  * @self: a #NMDeviceModem
72  *
73  * Returns a bitfield of the generic access technology families the modem
74  * supports.  Not all capabilities are available concurrently however; some
75  * may require a firmware reload or reinitialization.
76  *
77  * Returns: the generic access technology families the modem supports
78  **/
79 NMDeviceModemCapabilities
80 nm_device_modem_get_modem_capabilities (NMDeviceModem *self)
81 {
82         g_return_val_if_fail (NM_IS_DEVICE_MODEM (self), NM_DEVICE_MODEM_CAPABILITY_NONE);
83
84         _nm_object_ensure_inited (NM_OBJECT (self));
85         return NM_DEVICE_MODEM_GET_PRIVATE (self)->caps;
86 }
87
88 /**
89  * nm_device_modem_get_current_capabilities:
90  * @self: a #NMDeviceModem
91  *
92  * Returns a bitfield of the generic access technology families the modem
93  * supports without a firmware reload or reinitialization.  This value
94  * represents the network types the modem can immediately connect to.
95  *
96  * Returns: the generic access technology families the modem supports without
97  * a firmware reload or other reinitialization
98  **/
99 NMDeviceModemCapabilities
100 nm_device_modem_get_current_capabilities (NMDeviceModem *self)
101 {
102         g_return_val_if_fail (NM_IS_DEVICE_MODEM (self), NM_DEVICE_MODEM_CAPABILITY_NONE);
103
104         _nm_object_ensure_inited (NM_OBJECT (self));
105         return NM_DEVICE_MODEM_GET_PRIVATE (self)->current_caps;
106 }
107
108 static const char *
109 get_type_description (NMDevice *device)
110 {
111         NMDeviceModemCapabilities caps;
112
113         caps = nm_device_modem_get_current_capabilities (NM_DEVICE_MODEM (device));
114         if (caps & NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS)
115                 return "gsm";
116         else if (caps & NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO)
117                 return "cdma";
118         else
119                 return NULL;
120 }
121
122 #define MODEM_CAPS_3GPP(caps) (caps & (NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS |    \
123                                        NM_DEVICE_MODEM_CAPABILITY_LTE))
124
125 #define MODEM_CAPS_3GPP2(caps) (caps & (NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO))
126
127 static gboolean
128 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
129 {
130         NMSettingConnection *s_con;
131         NMSettingGsm *s_gsm;
132         NMSettingCdma *s_cdma;
133         const char *ctype;
134         NMDeviceModemCapabilities current_caps;
135
136         s_con = nm_connection_get_setting_connection (connection);
137         g_assert (s_con);
138
139         ctype = nm_setting_connection_get_connection_type (s_con);
140         if (   strcmp (ctype, NM_SETTING_GSM_SETTING_NAME) != 0
141             && strcmp (ctype, NM_SETTING_CDMA_SETTING_NAME) != 0) {
142                 g_set_error (error, NM_DEVICE_MODEM_ERROR, NM_DEVICE_MODEM_ERROR_NOT_MODEM_CONNECTION,
143                              "The connection was not a modem connection.");
144                 return FALSE;
145         }
146
147         s_gsm = nm_connection_get_setting_gsm (connection);
148         s_cdma = nm_connection_get_setting_cdma (connection);
149         if (!s_cdma && !s_gsm) {
150                 g_set_error (error, NM_DEVICE_MODEM_ERROR, NM_DEVICE_MODEM_ERROR_INVALID_MODEM_CONNECTION,
151                              "The connection was not a valid modem connection.");
152                 return FALSE;
153         }
154
155         current_caps = nm_device_modem_get_current_capabilities (NM_DEVICE_MODEM (device));
156         if (!(s_gsm && MODEM_CAPS_3GPP (current_caps)) && !(s_cdma && MODEM_CAPS_3GPP2 (current_caps))) {
157                 g_set_error (error, NM_DEVICE_MODEM_ERROR, NM_DEVICE_MODEM_ERROR_MISSING_DEVICE_CAPS,
158                              "The device missed capabilities required by the GSM/CDMA connection.");
159                 return FALSE;
160         }
161
162         return NM_DEVICE_CLASS (nm_device_modem_parent_class)->connection_compatible (device, connection, error);
163 }
164
165 static GType
166 get_setting_type (NMDevice *device)
167 {
168         NMDeviceModemCapabilities caps;
169
170         caps = nm_device_modem_get_current_capabilities (NM_DEVICE_MODEM (device));
171         if (caps & (NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS | NM_DEVICE_MODEM_CAPABILITY_LTE))
172                 return NM_TYPE_SETTING_GSM;
173         else if (caps & NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO)
174                 return NM_TYPE_SETTING_CDMA;
175         else
176                 return G_TYPE_INVALID;
177 }
178
179 /*******************************************************************/
180
181 static void
182 nm_device_modem_init (NMDeviceModem *device)
183 {
184         _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_MODEM);
185 }
186
187 static void
188 register_properties (NMDeviceModem *device)
189 {
190         NMDeviceModemPrivate *priv = NM_DEVICE_MODEM_GET_PRIVATE (device);
191         const NMPropertiesInfo property_info[] = {
192                 { NM_DEVICE_MODEM_MODEM_CAPABILITIES,   &priv->caps },
193                 { NM_DEVICE_MODEM_CURRENT_CAPABILITIES, &priv->current_caps },
194                 { NULL },
195         };
196
197         _nm_object_register_properties (NM_OBJECT (device),
198                                         priv->proxy,
199                                         property_info);
200 }
201
202 static void
203 constructed (GObject *object)
204 {
205         NMDeviceModemPrivate *priv = NM_DEVICE_MODEM_GET_PRIVATE (object);
206
207         G_OBJECT_CLASS (nm_device_modem_parent_class)->constructed (object);
208
209         priv->proxy = _nm_object_new_proxy (NM_OBJECT (object), NULL, NM_DBUS_INTERFACE_DEVICE_MODEM);
210         register_properties (NM_DEVICE_MODEM (object));
211 }
212
213 static void
214 get_property (GObject *object,
215               guint prop_id,
216               GValue *value,
217               GParamSpec *pspec)
218 {
219         NMDeviceModem *self = NM_DEVICE_MODEM (object);
220
221         _nm_object_ensure_inited (NM_OBJECT (object));
222
223         switch (prop_id) {
224         case PROP_MODEM_CAPS:
225                 g_value_set_uint (value, nm_device_modem_get_modem_capabilities (self));
226                 break;
227         case PROP_CURRENT_CAPS:
228                 g_value_set_uint (value, nm_device_modem_get_current_capabilities (self));
229                 break;
230         default:
231                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
232                 break;
233         }
234 }
235
236 static void
237 dispose (GObject *object)
238 {
239         NMDeviceModemPrivate *priv = NM_DEVICE_MODEM_GET_PRIVATE (object);
240
241         g_clear_object (&priv->proxy);
242
243         G_OBJECT_CLASS (nm_device_modem_parent_class)->dispose (object);
244 }
245
246 static void
247 nm_device_modem_class_init (NMDeviceModemClass *modem_class)
248 {
249         GObjectClass *object_class = G_OBJECT_CLASS (modem_class);
250         NMDeviceClass *device_class = NM_DEVICE_CLASS (modem_class);
251
252         g_type_class_add_private (modem_class, sizeof (NMDeviceModemPrivate));
253
254         /* virtual methods */
255         object_class->constructed = constructed;
256         object_class->get_property = get_property;
257         object_class->dispose = dispose;
258
259         device_class->get_type_description = get_type_description;
260         device_class->connection_compatible = connection_compatible;
261         device_class->get_setting_type = get_setting_type;
262
263         /**
264          * NMDeviceModem:modem-capabilities:
265          *
266          * The generic family of access technologies the modem supports.  Not all
267          * capabilities are available at the same time however; some modems require
268          * a firmware reload or other reinitialization to switch between eg
269          * CDMA/EVDO and GSM/UMTS.
270          **/
271         g_object_class_install_property
272                 (object_class, PROP_MODEM_CAPS,
273                  g_param_spec_uint (NM_DEVICE_MODEM_MODEM_CAPABILITIES, "", "",
274                                     0, G_MAXUINT32, 0,
275                                     G_PARAM_READABLE |
276                                     G_PARAM_STATIC_STRINGS));
277
278         /**
279          * NMDeviceModem:current-capabilities:
280          *
281          * The generic family of access technologies the modem currently supports
282          * without a firmware reload or reinitialization.
283          **/
284         g_object_class_install_property
285                 (object_class, PROP_CURRENT_CAPS,
286                  g_param_spec_uint (NM_DEVICE_MODEM_CURRENT_CAPABILITIES, "", "",
287                                     0, G_MAXUINT32, 0,
288                                     G_PARAM_READABLE |
289                                     G_PARAM_STATIC_STRINGS));
290 }