device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-core / nm-setting-serial.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
3 /*
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301 USA.
18  *
19  * Copyright 2007 - 2011 Red Hat, Inc.
20  * Copyright 2007 - 2008 Novell, Inc.
21  */
22
23 #include "nm-default.h"
24
25 #include <string.h>
26
27 #include "nm-setting-serial.h"
28 #include "nm-setting-private.h"
29
30 /**
31  * SECTION:nm-setting-serial
32  * @short_description: Describes connection properties for devices that use
33  * serial communications
34  *
35  * The #NMSettingSerial object is a #NMSetting subclass that describes
36  * properties necessary for connections that may use serial communications,
37  * such as mobile broadband or analog telephone connections.
38  **/
39
40 G_DEFINE_TYPE_WITH_CODE (NMSettingSerial, nm_setting_serial, NM_TYPE_SETTING,
41                          _nm_register_setting (SERIAL, 2))
42 NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_SERIAL)
43
44 #define NM_SETTING_SERIAL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_SERIAL, NMSettingSerialPrivate))
45
46 typedef struct {
47         guint baud;
48         guint bits;
49         char parity;
50         guint stopbits;
51         guint64 send_delay;
52 } NMSettingSerialPrivate;
53
54
55 enum {
56         PROP_0,
57         PROP_BAUD,
58         PROP_BITS,
59         PROP_PARITY,
60         PROP_STOPBITS,
61         PROP_SEND_DELAY,
62
63         LAST_PROP
64 };
65
66 /**
67  * nm_setting_serial_new:
68  *
69  * Creates a new #NMSettingSerial object with default values.
70  *
71  * Returns: (transfer full): the new empty #NMSettingSerial object
72  **/
73 NMSetting *
74 nm_setting_serial_new (void)
75 {
76         return (NMSetting *) g_object_new (NM_TYPE_SETTING_SERIAL, NULL);
77 }
78
79 /**
80  * nm_setting_serial_get_baud:
81  * @setting: the #NMSettingSerial
82  *
83  * Returns: the #NMSettingSerial:baud property of the setting
84  **/
85 guint
86 nm_setting_serial_get_baud (NMSettingSerial *setting)
87 {
88         g_return_val_if_fail (NM_IS_SETTING_SERIAL (setting), 0);
89
90         return NM_SETTING_SERIAL_GET_PRIVATE (setting)->baud;
91 }
92
93 /**
94  * nm_setting_serial_get_bits:
95  * @setting: the #NMSettingSerial
96  *
97  * Returns: the #NMSettingSerial:bits property of the setting
98  **/
99 guint
100 nm_setting_serial_get_bits (NMSettingSerial *setting)
101 {
102         g_return_val_if_fail (NM_IS_SETTING_SERIAL (setting), 0);
103
104         return NM_SETTING_SERIAL_GET_PRIVATE (setting)->bits;
105 }
106
107 /**
108  * nm_setting_serial_get_parity:
109  * @setting: the #NMSettingSerial
110  *
111  * Returns: the #NMSettingSerial:parity property of the setting
112  **/
113 NMSettingSerialParity
114 nm_setting_serial_get_parity (NMSettingSerial *setting)
115 {
116         g_return_val_if_fail (NM_IS_SETTING_SERIAL (setting), 0);
117
118         return NM_SETTING_SERIAL_GET_PRIVATE (setting)->parity;
119 }
120
121 /**
122  * nm_setting_serial_get_stopbits:
123  * @setting: the #NMSettingSerial
124  *
125  * Returns: the #NMSettingSerial:stopbits property of the setting
126  **/
127 guint
128 nm_setting_serial_get_stopbits (NMSettingSerial *setting)
129 {
130         g_return_val_if_fail (NM_IS_SETTING_SERIAL (setting), 0);
131
132         return NM_SETTING_SERIAL_GET_PRIVATE (setting)->stopbits;
133 }
134
135 /**
136  * nm_setting_serial_get_send_delay:
137  * @setting: the #NMSettingSerial
138  *
139  * Returns: the #NMSettingSerial:send-delay property of the setting
140  **/
141 guint64
142 nm_setting_serial_get_send_delay (NMSettingSerial *setting)
143 {
144         g_return_val_if_fail (NM_IS_SETTING_SERIAL (setting), 0);
145
146         return NM_SETTING_SERIAL_GET_PRIVATE (setting)->send_delay;
147 }
148
149 static gboolean
150 verify (NMSetting *setting, NMConnection *connection, GError **error)
151 {
152         return TRUE;
153 }
154
155 static void
156 nm_setting_serial_init (NMSettingSerial *setting)
157 {
158 }
159
160 static GVariant *
161 parity_to_dbus (const GValue *from)
162 {
163         switch (g_value_get_enum (from)) {
164         case NM_SETTING_SERIAL_PARITY_EVEN:
165                 return g_variant_new_byte ('E');
166         case NM_SETTING_SERIAL_PARITY_ODD:
167                 return g_variant_new_byte ('o');
168         case NM_SETTING_SERIAL_PARITY_NONE:
169         default:
170                 return g_variant_new_byte ('n');
171         }
172 }
173
174 static void
175 parity_from_dbus (GVariant *from, GValue *to)
176 {
177         switch (g_variant_get_byte (from)) {
178         case 'E':
179                 g_value_set_enum (to, NM_SETTING_SERIAL_PARITY_EVEN);
180                 break;
181         case 'o':
182                 g_value_set_enum (to, NM_SETTING_SERIAL_PARITY_ODD);
183                 break;
184         case 'n':
185         default:
186                 g_value_set_enum (to, NM_SETTING_SERIAL_PARITY_NONE);
187                 break;
188         }
189 }
190
191 static void
192 set_property (GObject *object, guint prop_id,
193               const GValue *value, GParamSpec *pspec)
194 {
195         NMSettingSerialPrivate *priv = NM_SETTING_SERIAL_GET_PRIVATE (object);
196
197         switch (prop_id) {
198         case PROP_BAUD:
199                 priv->baud = g_value_get_uint (value);
200                 break;
201         case PROP_BITS:
202                 priv->bits = g_value_get_uint (value);
203                 break;
204         case PROP_PARITY:
205                 priv->parity = g_value_get_enum (value);
206                 break;
207         case PROP_STOPBITS:
208                 priv->stopbits = g_value_get_uint (value);
209                 break;
210         case PROP_SEND_DELAY:
211                 priv->send_delay = g_value_get_uint64 (value);
212                 break;
213         default:
214                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215                 break;
216         }
217 }
218
219 static void
220 get_property (GObject *object, guint prop_id,
221               GValue *value, GParamSpec *pspec)
222 {
223         NMSettingSerial *setting = NM_SETTING_SERIAL (object);
224
225         switch (prop_id) {
226         case PROP_BAUD:
227                 g_value_set_uint (value, nm_setting_serial_get_baud (setting));
228                 break;
229         case PROP_BITS:
230                 g_value_set_uint (value, nm_setting_serial_get_bits (setting));
231                 break;
232         case PROP_PARITY:
233                 g_value_set_enum (value, nm_setting_serial_get_parity (setting));
234                 break;
235         case PROP_STOPBITS:
236                 g_value_set_uint (value, nm_setting_serial_get_stopbits (setting));
237                 break;
238         case PROP_SEND_DELAY:
239                 g_value_set_uint64 (value, nm_setting_serial_get_send_delay (setting));
240                 break;
241         default:
242                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
243                 break;
244         }
245 }
246
247 static void
248 nm_setting_serial_class_init (NMSettingSerialClass *setting_class)
249 {
250         GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
251         NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);
252
253         g_type_class_add_private (setting_class, sizeof (NMSettingSerialPrivate));
254
255         /* virtual methods */
256         object_class->set_property = set_property;
257         object_class->get_property = get_property;
258         parent_class->verify       = verify;
259
260         /* Properties */
261
262         /**
263          * NMSettingSerial:baud:
264          *
265          * Speed to use for communication over the serial port.  Note that this
266          * value usually has no effect for mobile broadband modems as they generally
267          * ignore speed settings and use the highest available speed.
268          **/
269         g_object_class_install_property
270                 (object_class, PROP_BAUD,
271                  g_param_spec_uint (NM_SETTING_SERIAL_BAUD, "", "",
272                                     0, G_MAXUINT, 57600,
273                                     G_PARAM_READWRITE |
274                                     G_PARAM_CONSTRUCT |
275                                     G_PARAM_STATIC_STRINGS));
276
277         /**
278          * NMSettingSerial:bits:
279          *
280          * Byte-width of the serial communication. The 8 in "8n1" for example.
281          **/
282         g_object_class_install_property
283                 (object_class, PROP_BITS,
284                  g_param_spec_uint (NM_SETTING_SERIAL_BITS, "", "",
285                                     5, 8, 8,
286                                     G_PARAM_READWRITE |
287                                     G_PARAM_CONSTRUCT |
288                                     G_PARAM_STATIC_STRINGS));
289
290         /**
291          * NMSettingSerial:parity:
292          *
293          * Parity setting of the serial port.
294          **/
295         /* ---keyfile---
296          * property: parity
297          * format: 'e', 'o', or 'n'
298          * description: The connection parity; even, odd, or none. Note that older
299          *   versions of NetworkManager stored this as an integer: 69 ('E') for even,
300          *   111 ('o') for odd, or 110 ('n') for none.
301          * example: parity=n
302          * ---end---
303          * ---dbus---
304          * property: parity
305          * format: byte
306          * description: The connection parity: 69 (ASCII 'E') for even parity,
307          *   111 (ASCII 'o') for odd, 110 (ASCII 'n') for none.
308          * ---end---
309          */
310         g_object_class_install_property
311                 (object_class, PROP_PARITY,
312                  g_param_spec_enum (NM_SETTING_SERIAL_PARITY, "", "",
313                                     NM_TYPE_SETTING_SERIAL_PARITY,
314                                     NM_SETTING_SERIAL_PARITY_NONE,
315                                     G_PARAM_READWRITE |
316                                     G_PARAM_CONSTRUCT |
317                                     G_PARAM_STATIC_STRINGS));
318         _nm_setting_class_transform_property (parent_class,
319                                               NM_SETTING_SERIAL_PARITY,
320                                               G_VARIANT_TYPE_BYTE,
321                                               parity_to_dbus,
322                                               parity_from_dbus);
323
324         /**
325          * NMSettingSerial:stopbits:
326          *
327          * Number of stop bits for communication on the serial port.  Either 1 or 2.
328          * The 1 in "8n1" for example.
329          **/
330         g_object_class_install_property
331                 (object_class, PROP_STOPBITS,
332                  g_param_spec_uint (NM_SETTING_SERIAL_STOPBITS, "", "",
333                                     1, 2, 1,
334                                     G_PARAM_READWRITE |
335                                     G_PARAM_CONSTRUCT |
336                                     G_PARAM_STATIC_STRINGS));
337
338         /**
339          * NMSettingSerial:send-delay:
340          *
341          * Time to delay between each byte sent to the modem, in microseconds.
342          **/
343         g_object_class_install_property
344                 (object_class, PROP_SEND_DELAY,
345                  g_param_spec_uint64 (NM_SETTING_SERIAL_SEND_DELAY, "", "",
346                                       0, G_MAXUINT64, 0,
347                                       G_PARAM_READWRITE |
348                                       G_PARAM_CONSTRUCT |
349                                       G_PARAM_STATIC_STRINGS));
350 }