device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-util / 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  * @include: nm-setting-serial.h
35  *
36  * The #NMSettingSerial object is a #NMSetting subclass that describes
37  * properties necessary for connections that may use serial communications,
38  * such as mobile broadband or analog telephone connections.
39  **/
40
41 /**
42  * nm_setting_serial_error_quark:
43  *
44  * Registers an error quark for #NMSettingSerial if necessary.
45  *
46  * Returns: the error quark used for #NMSettingSerial errors.
47  **/
48 GQuark
49 nm_setting_serial_error_quark (void)
50 {
51         static GQuark quark;
52
53         if (G_UNLIKELY (!quark))
54                 quark = g_quark_from_static_string ("nm-setting-serial-error-quark");
55         return quark;
56 }
57
58
59 G_DEFINE_TYPE_WITH_CODE (NMSettingSerial, nm_setting_serial, NM_TYPE_SETTING,
60                          _nm_register_setting (NM_SETTING_SERIAL_SETTING_NAME,
61                                                g_define_type_id,
62                                                2,
63                                                NM_SETTING_SERIAL_ERROR))
64 NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_SERIAL)
65
66 #define NM_SETTING_SERIAL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_SERIAL, NMSettingSerialPrivate))
67
68 typedef struct {
69         guint baud;
70         guint bits;
71         char parity;
72         guint stopbits;
73         guint64 send_delay;
74 } NMSettingSerialPrivate;
75
76
77 enum {
78         PROP_0,
79         PROP_BAUD,
80         PROP_BITS,
81         PROP_PARITY,
82         PROP_STOPBITS,
83         PROP_SEND_DELAY,
84
85         LAST_PROP
86 };
87
88 /**
89  * nm_setting_serial_new:
90  *
91  * Creates a new #NMSettingSerial object with default values.
92  *
93  * Returns: (transfer full): the new empty #NMSettingSerial object
94  **/
95 NMSetting *
96 nm_setting_serial_new (void)
97 {
98         return (NMSetting *) g_object_new (NM_TYPE_SETTING_SERIAL, NULL);
99 }
100
101 /**
102  * nm_setting_serial_get_baud:
103  * @setting: the #NMSettingSerial
104  *
105  * Returns: the #NMSettingSerial:baud property of the setting
106  **/
107 guint
108 nm_setting_serial_get_baud (NMSettingSerial *setting)
109 {
110         g_return_val_if_fail (NM_IS_SETTING_SERIAL (setting), 0);
111
112         return NM_SETTING_SERIAL_GET_PRIVATE (setting)->baud;
113 }
114
115 /**
116  * nm_setting_serial_get_bits:
117  * @setting: the #NMSettingSerial
118  *
119  * Returns: the #NMSettingSerial:bits property of the setting
120  **/
121 guint
122 nm_setting_serial_get_bits (NMSettingSerial *setting)
123 {
124         g_return_val_if_fail (NM_IS_SETTING_SERIAL (setting), 0);
125
126         return NM_SETTING_SERIAL_GET_PRIVATE (setting)->bits;
127 }
128
129 /**
130  * nm_setting_serial_get_parity:
131  * @setting: the #NMSettingSerial
132  *
133  * Returns: the #NMSettingSerial:parity property of the setting
134  **/
135 char
136 nm_setting_serial_get_parity (NMSettingSerial *setting)
137 {
138         g_return_val_if_fail (NM_IS_SETTING_SERIAL (setting), 0);
139
140         return NM_SETTING_SERIAL_GET_PRIVATE (setting)->parity;
141 }
142
143 /**
144  * nm_setting_serial_get_stopbits:
145  * @setting: the #NMSettingSerial
146  *
147  * Returns: the #NMSettingSerial:stopbits property of the setting
148  **/
149 guint
150 nm_setting_serial_get_stopbits (NMSettingSerial *setting)
151 {
152         g_return_val_if_fail (NM_IS_SETTING_SERIAL (setting), 0);
153
154         return NM_SETTING_SERIAL_GET_PRIVATE (setting)->stopbits;
155 }
156
157 /**
158  * nm_setting_serial_get_send_delay:
159  * @setting: the #NMSettingSerial
160  *
161  * Returns: the #NMSettingSerial:send-delay property of the setting
162  **/
163 guint64
164 nm_setting_serial_get_send_delay (NMSettingSerial *setting)
165 {
166         g_return_val_if_fail (NM_IS_SETTING_SERIAL (setting), 0);
167
168         return NM_SETTING_SERIAL_GET_PRIVATE (setting)->send_delay;
169 }
170
171 static gboolean
172 verify (NMSetting *setting, GSList *all_settings, GError **error)
173 {
174         return TRUE;
175 }
176
177 static void
178 nm_setting_serial_init (NMSettingSerial *setting)
179 {
180 }
181
182 static void
183 set_property (GObject *object, guint prop_id,
184               const GValue *value, GParamSpec *pspec)
185 {
186         NMSettingSerialPrivate *priv = NM_SETTING_SERIAL_GET_PRIVATE (object);
187
188         switch (prop_id) {
189         case PROP_BAUD:
190                 priv->baud = g_value_get_uint (value);
191                 break;
192         case PROP_BITS:
193                 priv->bits = g_value_get_uint (value);
194                 break;
195         case PROP_PARITY:
196                 priv->parity = g_value_get_schar (value);
197                 break;
198         case PROP_STOPBITS:
199                 priv->stopbits = g_value_get_uint (value);
200                 break;
201         case PROP_SEND_DELAY:
202                 priv->send_delay = g_value_get_uint64 (value);
203                 break;
204         default:
205                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
206                 break;
207         }
208 }
209
210 static void
211 get_property (GObject *object, guint prop_id,
212               GValue *value, GParamSpec *pspec)
213 {
214         NMSettingSerial *setting = NM_SETTING_SERIAL (object);
215
216         switch (prop_id) {
217         case PROP_BAUD:
218                 g_value_set_uint (value, nm_setting_serial_get_baud (setting));
219                 break;
220         case PROP_BITS:
221                 g_value_set_uint (value, nm_setting_serial_get_bits (setting));
222                 break;
223         case PROP_PARITY:
224                 g_value_set_schar (value, nm_setting_serial_get_parity (setting));
225                 break;
226         case PROP_STOPBITS:
227                 g_value_set_uint (value, nm_setting_serial_get_stopbits (setting));
228                 break;
229         case PROP_SEND_DELAY:
230                 g_value_set_uint64 (value, nm_setting_serial_get_send_delay (setting));
231                 break;
232         default:
233                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
234                 break;
235         }
236 }
237
238 static void
239 nm_setting_serial_class_init (NMSettingSerialClass *setting_class)
240 {
241         GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
242         NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);
243
244         g_type_class_add_private (setting_class, sizeof (NMSettingSerialPrivate));
245
246         /* virtual methods */
247         object_class->set_property = set_property;
248         object_class->get_property = get_property;
249         parent_class->verify       = verify;
250
251         /* Properties */
252
253         /**
254          * NMSettingSerial:baud:
255          *
256          * Speed to use for communication over the serial port.  Note that this
257          * value usually has no effect for mobile broadband modems as they generally
258          * ignore speed settings and use the highest available speed.
259          **/
260         g_object_class_install_property
261                 (object_class, PROP_BAUD,
262                  g_param_spec_uint (NM_SETTING_SERIAL_BAUD, "", "",
263                                     0, G_MAXUINT, 57600,
264                                     G_PARAM_READWRITE |
265                                     G_PARAM_CONSTRUCT |
266                                     G_PARAM_STATIC_STRINGS));
267
268         /**
269          * NMSettingSerial:bits:
270          *
271          * Byte-width of the serial communication. The 8 in "8n1" for example.
272          **/
273         g_object_class_install_property
274                 (object_class, PROP_BITS,
275                  g_param_spec_uint (NM_SETTING_SERIAL_BITS, "", "",
276                                     5, 8, 8,
277                                     G_PARAM_READWRITE |
278                                     G_PARAM_CONSTRUCT |
279                                     G_PARAM_STATIC_STRINGS));
280
281         /**
282          * NMSettingSerial:parity:
283          *
284          * Parity setting of the serial port.  Either 'E' for even parity, 'o' for
285          * odd parity, or 'n' for no parity.
286          **/
287         g_object_class_install_property
288                 (object_class, PROP_PARITY,
289                  g_param_spec_char (NM_SETTING_SERIAL_PARITY, "", "",
290                                     'E', 'o', 'n',
291                                     G_PARAM_READWRITE |
292                                     G_PARAM_CONSTRUCT |
293                                     G_PARAM_STATIC_STRINGS));
294
295         /**
296          * NMSettingSerial:stopbits:
297          *
298          * Number of stop bits for communication on the serial port.  Either 1 or 2.
299          * The 1 in "8n1" for example.
300          **/
301         g_object_class_install_property
302                 (object_class, PROP_STOPBITS,
303                  g_param_spec_uint (NM_SETTING_SERIAL_STOPBITS, "", "",
304                                     1, 2, 1,
305                                     G_PARAM_READWRITE |
306                                     G_PARAM_CONSTRUCT |
307                                     G_PARAM_STATIC_STRINGS));
308
309         /**
310          * NMSettingSerial:send-delay:
311          *
312          * Time to delay between each byte sent to the modem, in microseconds.
313          **/
314         g_object_class_install_property
315                 (object_class, PROP_SEND_DELAY,
316                  g_param_spec_uint64 (NM_SETTING_SERIAL_SEND_DELAY, "", "",
317                                       0, G_MAXUINT64, 0,
318                                       G_PARAM_READWRITE |
319                                       G_PARAM_CONSTRUCT |
320                                       G_PARAM_STATIC_STRINGS));
321 }