device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm / nm-device-generic.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 2013 Red Hat, Inc.
19  */
20
21 #include "nm-default.h"
22
23 #include <string.h>
24
25 #include "nm-device-generic.h"
26 #include "nm-device-private.h"
27 #include "nm-object-private.h"
28 #include "nm-setting-generic.h"
29 #include "nm-setting-connection.h"
30
31 G_DEFINE_TYPE (NMDeviceGeneric, nm_device_generic, NM_TYPE_DEVICE)
32
33 #define NM_DEVICE_GENERIC_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_GENERIC, NMDeviceGenericPrivate))
34
35 typedef struct {
36         char *hw_address;
37         char *type_description;
38 } NMDeviceGenericPrivate;
39
40 enum {
41         PROP_0,
42         PROP_HW_ADDRESS,
43         PROP_TYPE_DESCRIPTION,
44
45         LAST_PROP
46 };
47
48 /**
49  * nm_device_generic_get_hw_address:
50  * @device: a #NMDeviceGeneric
51  *
52  * Gets the hardware address of the #NMDeviceGeneric
53  *
54  * Returns: the hardware address. This is the internal string used by the
55  * device, and must not be modified.
56  **/
57 const char *
58 nm_device_generic_get_hw_address (NMDeviceGeneric *device)
59 {
60         g_return_val_if_fail (NM_IS_DEVICE_GENERIC (device), NULL);
61
62         return NM_DEVICE_GENERIC_GET_PRIVATE (device)->hw_address;
63 }
64
65 /***********************************************************/
66
67 static const char *
68 get_type_description (NMDevice *device)
69 {
70         NMDeviceGenericPrivate *priv = NM_DEVICE_GENERIC_GET_PRIVATE (device);
71
72         return priv->type_description;
73 }
74
75 static const char *
76 get_hw_address (NMDevice *device)
77 {
78         return nm_device_generic_get_hw_address (NM_DEVICE_GENERIC (device));
79 }
80
81 static gboolean
82 connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
83 {
84         const char *iface_name;
85
86         if (!NM_DEVICE_CLASS (nm_device_generic_parent_class)->connection_compatible (device, connection, error))
87                 return FALSE;
88
89         if (!nm_connection_is_type (connection, NM_SETTING_GENERIC_SETTING_NAME)) {
90                 g_set_error_literal (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INCOMPATIBLE_CONNECTION,
91                                      _("The connection was not a generic connection."));
92                 return FALSE;
93         }
94
95         iface_name = nm_connection_get_interface_name (connection);
96         if (!iface_name) {
97                 g_set_error_literal (error, NM_DEVICE_ERROR, NM_DEVICE_ERROR_INVALID_CONNECTION,
98                                      _("The connection did not specify an interface name."));
99                 return FALSE;
100         }
101
102         return TRUE;
103 }
104
105 static GType
106 get_setting_type (NMDevice *device)
107 {
108         return NM_TYPE_SETTING_GENERIC;
109 }
110
111 /***********************************************************/
112
113 static void
114 nm_device_generic_init (NMDeviceGeneric *device)
115 {
116         _nm_device_set_device_type (NM_DEVICE (device), NM_DEVICE_TYPE_GENERIC);
117 }
118
119 static void
120 init_dbus (NMObject *object)
121 {
122         NMDeviceGenericPrivate *priv = NM_DEVICE_GENERIC_GET_PRIVATE (object);
123         const NMPropertiesInfo property_info[] = {
124                 { NM_DEVICE_GENERIC_HW_ADDRESS, &priv->hw_address },
125                 { NM_DEVICE_GENERIC_TYPE_DESCRIPTION, &priv->type_description },
126                 { NULL },
127         };
128
129         NM_OBJECT_CLASS (nm_device_generic_parent_class)->init_dbus (object);
130
131         _nm_object_register_properties (object,
132                                         NM_DBUS_INTERFACE_DEVICE_GENERIC,
133                                         property_info);
134 }
135
136 static void
137 finalize (GObject *object)
138 {
139         NMDeviceGenericPrivate *priv = NM_DEVICE_GENERIC_GET_PRIVATE (object);
140
141         g_free (priv->hw_address);
142         g_free (priv->type_description);
143
144         G_OBJECT_CLASS (nm_device_generic_parent_class)->finalize (object);
145 }
146
147 static void
148 get_property (GObject *object,
149               guint prop_id,
150               GValue *value,
151               GParamSpec *pspec)
152 {
153         NMDeviceGenericPrivate *priv = NM_DEVICE_GENERIC_GET_PRIVATE (object);
154
155         switch (prop_id) {
156         case PROP_HW_ADDRESS:
157                 g_value_set_string (value, priv->hw_address);
158                 break;
159         case PROP_TYPE_DESCRIPTION:
160                 g_value_set_string (value, priv->type_description);
161                 break;
162         default:
163                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
164                 break;
165         }
166 }
167
168 static void
169 nm_device_generic_class_init (NMDeviceGenericClass *klass)
170 {
171         GObjectClass *object_class = G_OBJECT_CLASS (klass);
172         NMObjectClass *nm_object_class = NM_OBJECT_CLASS (klass);
173         NMDeviceClass *device_class = NM_DEVICE_CLASS (klass);
174
175         g_type_class_add_private (klass, sizeof (NMDeviceGenericPrivate));
176
177         _nm_object_class_add_interface (nm_object_class, NM_DBUS_INTERFACE_DEVICE_GENERIC);
178
179         object_class->finalize = finalize;
180         object_class->get_property = get_property;
181
182         nm_object_class->init_dbus = init_dbus;
183
184         device_class->get_type_description = get_type_description;
185         device_class->get_hw_address = get_hw_address;
186         device_class->connection_compatible = connection_compatible;
187         device_class->get_setting_type = get_setting_type;
188
189         /**
190          * NMDeviceGeneric:hw-address:
191          *
192          * The hardware address of the device.
193          **/
194         g_object_class_install_property
195                 (object_class, PROP_HW_ADDRESS,
196                  g_param_spec_string (NM_DEVICE_GENERIC_HW_ADDRESS, "", "",
197                                       NULL,
198                                       G_PARAM_READABLE |
199                                       G_PARAM_STATIC_STRINGS));
200
201         /**
202          * NMDeviceGeneric:type-description:
203          *
204          * A description of the specific type of device this is, or %NULL
205          * if not known.
206          **/
207         g_object_class_install_property
208                 (object_class, PROP_TYPE_DESCRIPTION,
209                  g_param_spec_string (NM_DEVICE_GENERIC_TYPE_DESCRIPTION, "", "",
210                                       NULL,
211                                       G_PARAM_READABLE |
212                                       G_PARAM_STATIC_STRINGS));
213 }