device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm / 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 #include "nm-enum-types.h"
34
35 G_DEFINE_TYPE (NMDeviceModem, nm_device_modem, NM_TYPE_DEVICE)
36
37 #define NM_DEVICE_MODEM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_MODEM, NMDeviceModemPrivate))
38
39 typedef struct {
40         NMDeviceModemCapabilities caps;
41         NMDeviceModemCapabilities current_caps;
42 } NMDeviceModemPrivate;
43
44 enum {
45         PROP_0,
46         PROP_MODEM_CAPS,
47         PROP_CURRENT_CAPS,
48         LAST_PROP
49 };
50
51 /**
52  * nm_device_modem_get_modem_capabilities:
53  * @self: a #NMDeviceModem
54  *
55  * Returns a bitfield of the generic access technology families the modem
56  * supports.  Not all capabilities are available concurrently however; some
57  * may require a firmware reload or reinitialization.
58  *
59  * Returns: the generic access technology families the modem supports
60  **/
61 NMDeviceModemCapabilities
62 nm_device_modem_get_modem_capabilities (NMDeviceModem *self)
63 {
64         g_return_val_if_fail (NM_IS_DEVICE_MODEM (self), NM_DEVICE_MODEM_CAPABILITY_NONE);
65
66         return NM_DEVICE_MODEM_GET_PRIVATE (self)->caps;
67 }
68
69 /**
70  * nm_device_modem_get_current_capabilities:
71  * @self: a #NMDeviceModem
72  *
73  * Returns a bitfield of the generic access technology families the modem
74  * supports without a firmware reload or reinitialization.  This value
75  * represents the network types the modem can immediately connect to.
76  *
77  * Returns: the generic access technology families the modem supports without
78  * a firmware reload or other reinitialization
79  **/
80 NMDeviceModemCapabilities
81 nm_device_modem_get_current_capabilities (NMDeviceModem *self)
82 {
83         g_return_val_if_fail (NM_IS_DEVICE_MODEM (self), NM_DEVICE_MODEM_CAPABILITY_NONE);
84
85         return NM_DEVICE_MODEM_GET_PRIVATE (self)->current_caps;
86 }
87
88 static const char *
89 get_type_description (NMDevice *device)
90 {
91         NMDeviceModemCapabilities caps;
92
93         caps = nm_device_modem_get_current_capabilities (NM_DEVICE_MODEM (device));
94         if (caps & NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS)
95                 return "gsm";
96         else if (caps & NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO)
97                 return "cdma";
98         else
99                 return NULL;
100 }
101
102 #define MODEM_CAPS_3GPP(caps) (caps & (NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS |    \
103                                        NM_DEVICE_MODEM_CAPABILITY_LTE))
104
105 #define MODEM_CAPS_3GPP2(caps) (caps & (NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO))
106
107 static gboolean
108 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
109 {
110         NMSettingGsm *s_gsm;
111         NMSettingCdma *s_cdma;
112         NMDeviceModemCapabilities current_caps;
113
114         if (!NM_DEVICE_CLASS (nm_device_modem_parent_class)->connection_compatible (device, connection, error))
115                 return FALSE;
116
117         if (   !nm_connection_is_type (connection, NM_SETTING_GSM_SETTING_NAME)
118             && !nm_connection_is_type (connection, NM_SETTING_CDMA_SETTING_NAME)) {
119                 g_set_error_literal (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION,
120                                      _("The connection was not a modem connection."));
121                 return FALSE;
122         }
123
124         s_gsm = nm_connection_get_setting_gsm (connection);
125         s_cdma = nm_connection_get_setting_cdma (connection);
126         if (!s_cdma && !s_gsm) {
127                 g_set_error_literal (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INVALID_CONNECTION,
128                                      _("The connection was not a valid modem connection."));
129                 return FALSE;
130         }
131
132         current_caps = nm_device_modem_get_current_capabilities (NM_DEVICE_MODEM (device));
133         if (!(s_gsm && MODEM_CAPS_3GPP (current_caps)) && !(s_cdma && MODEM_CAPS_3GPP2 (current_caps))) {
134                 g_set_error_literal (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION,
135                                      _("The device is lacking capabilities required by the connection."));
136                 return FALSE;
137         }
138
139         return TRUE;
140 }
141
142 static GType
143 get_setting_type (NMDevice *device)
144 {
145         NMDeviceModemCapabilities caps;
146
147         caps = nm_device_modem_get_current_capabilities (NM_DEVICE_MODEM (device));
148         if (caps & (NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS | NM_DEVICE_MODEM_CAPABILITY_LTE))
149                 return NM_TYPE_SETTING_GSM;
150         else if (caps & NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO)
151                 return NM_TYPE_SETTING_CDMA;
152         else
153                 return G_TYPE_INVALID;
154 }
155
156 /*******************************************************************/
157
158 static void
159 nm_device_modem_init (NMDeviceModem *device)
160 {
161         _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_MODEM);
162 }
163
164 static void
165 init_dbus (NMObject *object)
166 {
167         NMDeviceModemPrivate *priv = NM_DEVICE_MODEM_GET_PRIVATE (object);
168         const NMPropertiesInfo property_info[] = {
169                 { NM_DEVICE_MODEM_MODEM_CAPABILITIES,   &priv->caps },
170                 { NM_DEVICE_MODEM_CURRENT_CAPABILITIES, &priv->current_caps },
171                 { NULL },
172         };
173
174         NM_OBJECT_CLASS (nm_device_modem_parent_class)->init_dbus (object);
175
176         _nm_object_register_properties (object,
177                                         NM_DBUS_INTERFACE_DEVICE_MODEM,
178                                         property_info);
179 }
180
181 static void
182 get_property (GObject *object,
183               guint prop_id,
184               GValue *value,
185               GParamSpec *pspec)
186 {
187         NMDeviceModem *self = NM_DEVICE_MODEM (object);
188
189         switch (prop_id) {
190         case PROP_MODEM_CAPS:
191                 g_value_set_flags (value, nm_device_modem_get_modem_capabilities (self));
192                 break;
193         case PROP_CURRENT_CAPS:
194                 g_value_set_flags (value, nm_device_modem_get_current_capabilities (self));
195                 break;
196         default:
197                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
198                 break;
199         }
200 }
201
202 static void
203 nm_device_modem_class_init (NMDeviceModemClass *modem_class)
204 {
205         GObjectClass *object_class = G_OBJECT_CLASS (modem_class);
206         NMObjectClass *nm_object_class = NM_OBJECT_CLASS (modem_class);
207         NMDeviceClass *device_class = NM_DEVICE_CLASS (modem_class);
208
209         g_type_class_add_private (modem_class, sizeof (NMDeviceModemPrivate));
210
211         _nm_object_class_add_interface (nm_object_class, NM_DBUS_INTERFACE_DEVICE_MODEM);
212
213         /* virtual methods */
214         object_class->get_property = get_property;
215
216         nm_object_class->init_dbus = init_dbus;
217
218         device_class->get_type_description = get_type_description;
219         device_class->connection_compatible = connection_compatible;
220         device_class->get_setting_type = get_setting_type;
221
222         /**
223          * NMDeviceModem:modem-capabilities:
224          *
225          * The generic family of access technologies the modem supports.  Not all
226          * capabilities are available at the same time however; some modems require
227          * a firmware reload or other reinitialization to switch between eg
228          * CDMA/EVDO and GSM/UMTS.
229          **/
230         g_object_class_install_property
231                 (object_class, PROP_MODEM_CAPS,
232                  g_param_spec_flags (NM_DEVICE_MODEM_MODEM_CAPABILITIES, "", "",
233                                      NM_TYPE_DEVICE_MODEM_CAPABILITIES,
234                                      NM_DEVICE_MODEM_CAPABILITY_NONE,
235                                      G_PARAM_READABLE |
236                                      G_PARAM_STATIC_STRINGS));
237
238         /**
239          * NMDeviceModem:current-capabilities:
240          *
241          * The generic family of access technologies the modem currently supports
242          * without a firmware reload or reinitialization.
243          **/
244         g_object_class_install_property
245                 (object_class, PROP_CURRENT_CAPS,
246                  g_param_spec_flags (NM_DEVICE_MODEM_CURRENT_CAPABILITIES, "", "",
247                                      NM_TYPE_DEVICE_MODEM_CAPABILITIES,
248                                      NM_DEVICE_MODEM_CAPABILITY_NONE,
249                                      G_PARAM_READABLE |
250                                      G_PARAM_STATIC_STRINGS));
251 }