1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
#include "nm-device-ethernet.h"
#include "nm-device-private.h"
#include "nm-object-private.h"
#include "nm-device-ethernet-bindings.h"
G_DEFINE_TYPE (NMDeviceEthernet, nm_device_ethernet, NM_TYPE_DEVICE)
#define NM_DEVICE_ETHERNET_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_ETHERNET, NMDeviceEthernetPrivate))
typedef struct {
DBusGProxy *proxy;
char * hw_address;
guint32 speed;
gboolean carrier;
gboolean carrier_valid;
gboolean disposed;
} NMDeviceEthernetPrivate;
enum {
PROP_0,
PROP_HW_ADDRESS,
PROP_SPEED,
PROP_CARRIER,
LAST_PROP
};
#define DBUS_PROP_HW_ADDRESS "HwAddress"
#define DBUS_PROP_SPEED "Speed"
#define DBUS_PROP_CARRIER "Carrier"
/**
* nm_device_ethernet_new:
* @connection: the #DBusGConnection
* @path: the DBus object path of the device
*
* Creates a new #NMDeviceEthernet.
*
* Returns: a new device
**/
GObject *
nm_device_ethernet_new (DBusGConnection *connection, const char *path)
{
g_return_val_if_fail (connection != NULL, NULL);
g_return_val_if_fail (path != NULL, NULL);
return g_object_new (NM_TYPE_DEVICE_ETHERNET,
NM_OBJECT_DBUS_CONNECTION, connection,
NM_OBJECT_DBUS_PATH, path,
NULL);
}
/**
* nm_device_ethernet_get_hw_address:
* @device: a #NMDeviceEthernet
*
* Gets the hardware (MAC) address of the #NMDeviceEthernet
*
* Returns: the hardware address. This is the internal string used by the
* device, and must not be modified.
**/
const char *
nm_device_ethernet_get_hw_address (NMDeviceEthernet *device)
{
NMDeviceEthernetPrivate *priv;
g_return_val_if_fail (NM_IS_DEVICE_ETHERNET (device), NULL);
priv = NM_DEVICE_ETHERNET_GET_PRIVATE (device);
if (!priv->hw_address) {
priv->hw_address = nm_object_get_string_property (NM_OBJECT (device),
NM_DBUS_INTERFACE_DEVICE_WIRED,
DBUS_PROP_HW_ADDRESS);
}
return priv->hw_address;
}
/**
* nm_device_ethernet_get_speed:
* @device: a #NMDeviceEthernet
*
* Gets the speed of the #NMDeviceEthernet.
*
* Returns: the speed of the device
**/
guint32
nm_device_ethernet_get_speed (NMDeviceEthernet *device)
{
NMDeviceEthernetPrivate *priv;
g_return_val_if_fail (NM_IS_DEVICE_ETHERNET (device), 0);
priv = NM_DEVICE_ETHERNET_GET_PRIVATE (device);
if (!priv->speed) {
priv->speed = nm_object_get_uint_property (NM_OBJECT (device),
NM_DBUS_INTERFACE_DEVICE_WIRED,
DBUS_PROP_SPEED);
}
return priv->speed;
}
/**
* nm_device_ethernet_get_carrier:
* @device: a #NMDeviceEthernet
*
* Whether the device has carrier.
*
* Returns: %TRUE if the device has carrier
**/
gboolean
nm_device_ethernet_get_carrier (NMDeviceEthernet *device)
{
NMDeviceEthernetPrivate *priv;
g_return_val_if_fail (NM_IS_DEVICE_ETHERNET (device), FALSE);
priv = NM_DEVICE_ETHERNET_GET_PRIVATE (device);
if (!priv->carrier_valid) {
priv->carrier = nm_object_get_boolean_property (NM_OBJECT (device),
NM_DBUS_INTERFACE_DEVICE_WIRED,
DBUS_PROP_CARRIER);
priv->carrier_valid = TRUE;
}
return priv->carrier;
}
static void
nm_device_ethernet_init (NMDeviceEthernet *device)
{
NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (device);
priv->disposed = FALSE;
priv->carrier = FALSE;
priv->carrier_valid = FALSE;
}
static void
register_for_property_changed (NMDeviceEthernet *device)
{
NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (device);
const NMPropertiesChangedInfo property_changed_info[] = {
{ NM_DEVICE_ETHERNET_HW_ADDRESS, nm_object_demarshal_generic, &priv->hw_address },
{ NM_DEVICE_ETHERNET_SPEED, nm_object_demarshal_generic, &priv->speed },
{ NM_DEVICE_ETHERNET_CARRIER, nm_object_demarshal_generic, &priv->carrier },
{ NULL },
};
nm_object_handle_properties_changed (NM_OBJECT (device),
priv->proxy,
property_changed_info);
}
static GObject*
constructor (GType type,
guint n_construct_params,
GObjectConstructParam *construct_params)
{
GObject *object;
NMDeviceEthernetPrivate *priv;
object = G_OBJECT_CLASS (nm_device_ethernet_parent_class)->constructor (type,
n_construct_params,
construct_params);
if (!object)
return NULL;
priv = NM_DEVICE_ETHERNET_GET_PRIVATE (object);
priv->proxy = dbus_g_proxy_new_for_name (nm_object_get_connection (NM_OBJECT (object)),
NM_DBUS_SERVICE,
nm_object_get_path (NM_OBJECT (object)),
NM_DBUS_INTERFACE_DEVICE_WIRED);
register_for_property_changed (NM_DEVICE_ETHERNET (object));
return object;
}
static void
dispose (GObject *object)
{
NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (object);
if (priv->disposed) {
G_OBJECT_CLASS (nm_device_ethernet_parent_class)->dispose (object);
return;
}
priv->disposed = TRUE;
g_object_unref (priv->proxy);
G_OBJECT_CLASS (nm_device_ethernet_parent_class)->dispose (object);
}
static void
finalize (GObject *object)
{
NMDeviceEthernetPrivate *priv = NM_DEVICE_ETHERNET_GET_PRIVATE (object);
if (priv->hw_address)
g_free (priv->hw_address);
G_OBJECT_CLASS (nm_device_ethernet_parent_class)->finalize (object);
}
static void
get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
NMDeviceEthernet *device = NM_DEVICE_ETHERNET (object);
switch (prop_id) {
case PROP_HW_ADDRESS:
g_value_set_string (value, nm_device_ethernet_get_hw_address (device));
break;
case PROP_SPEED:
g_value_set_uint (value, nm_device_ethernet_get_speed (device));
break;
case PROP_CARRIER:
g_value_set_boolean (value, nm_device_ethernet_get_carrier (device));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
nm_device_ethernet_class_init (NMDeviceEthernetClass *device_class)
{
GObjectClass *object_class = G_OBJECT_CLASS (device_class);
g_type_class_add_private (device_class, sizeof (NMDeviceEthernetPrivate));
/* virtual methods */
object_class->constructor = constructor;
object_class->dispose = dispose;
object_class->finalize = finalize;
object_class->get_property = get_property;
/* properties */
/**
* NMDeviceEthernet:hw-address:
*
* The hardware (MAC) address of the device.
**/
g_object_class_install_property
(object_class, PROP_HW_ADDRESS,
g_param_spec_string (NM_DEVICE_ETHERNET_HW_ADDRESS,
"MAC Address",
"Hardware MAC address",
NULL,
G_PARAM_READABLE));
/**
* NMDeviceEthernet:speed:
*
* The speed of the device.
**/
g_object_class_install_property
(object_class, PROP_SPEED,
g_param_spec_uint (NM_DEVICE_ETHERNET_SPEED,
"Speed",
"Speed",
0, G_MAXUINT32, 0,
G_PARAM_READABLE));
/**
* NMDeviceEthernet:carrier:
*
* Whether the device has carrier.
**/
g_object_class_install_property
(object_class, PROP_CARRIER,
g_param_spec_boolean (NM_DEVICE_ETHERNET_CARRIER,
"Carrier",
"Carrier",
FALSE,
G_PARAM_READABLE));
}
|