Commit a91e6090 authored by Dan Winship's avatar Dan Winship

libnm-core: make NMSettingSerial:parity an enum

NMSettingSerial:parity was defined as a char-typed property that could
have the (case-sensitive!) values 'n', 'E', or 'o'. This is zany. Add
an NMSettingSerialParity enum, and use that instead.
parent fcfb4b40
......@@ -1425,7 +1425,6 @@ DEFINE_SECRET_FLAGS_GETTER (nmc_property_pppoe_get_password_flags, NM_SETTING_PP
/* --- NM_SETTING_SERIAL_SETTING_NAME property get functions --- */
DEFINE_GETTER (nmc_property_serial_get_baud, NM_SETTING_SERIAL_BAUD)
DEFINE_GETTER (nmc_property_serial_get_bits, NM_SETTING_SERIAL_BITS)
DEFINE_GETTER (nmc_property_serial_get_parity, NM_SETTING_SERIAL_PARITY)
DEFINE_GETTER (nmc_property_serial_get_stopbits, NM_SETTING_SERIAL_STOPBITS)
DEFINE_GETTER (nmc_property_serial_get_send_delay, NM_SETTING_SERIAL_SEND_DELAY)
......@@ -3680,17 +3679,33 @@ nmc_property_olpc_set_channel (NMSetting *setting, const char *prop, const char
/* --- NM_SETTING_SERIAL_SETTING_NAME property setter functions --- */
static char *
nmc_property_serial_get_parity (NMSetting *setting)
{
NMSettingSerial *s_serial = NM_SETTING_SERIAL (setting);
switch (nm_setting_serial_get_parity (s_serial)) {
case NM_SETTING_SERIAL_PARITY_EVEN:
return g_strdup ("even");
case NM_SETTING_SERIAL_PARITY_ODD:
return g_strdup ("odd");
default:
case NM_SETTING_SERIAL_PARITY_NONE:
return g_strdup ("none");
}
}
static gboolean
nmc_property_serial_set_parity (NMSetting *setting, const char *prop, const char *val, GError **error)
{
char parity;
NMSettingSerialParity parity;
if (val[0] == 'E' || val[0] == 'e')
parity = 'E';
parity = NM_SETTING_SERIAL_PARITY_EVEN;
else if (val[0] == 'O' || val[0] == 'o')
parity = 'o';
parity = NM_SETTING_SERIAL_PARITY_ODD;
else if (val[0] == 'N' || val[0] == 'n')
parity = 'n';
parity = NM_SETTING_SERIAL_PARITY_NONE;
else {
g_set_error (error, 1, 0, _("'%s' is not valid; use [e, o, n]"), val);
return FALSE;
......
......@@ -127,7 +127,7 @@ nm_setting_serial_get_bits (NMSettingSerial *setting)
*
* Returns: the #NMSettingSerial:parity property of the setting
**/
char
NMSettingSerialParity
nm_setting_serial_get_parity (NMSettingSerial *setting)
{
g_return_val_if_fail (NM_IS_SETTING_SERIAL (setting), 0);
......@@ -174,6 +174,37 @@ nm_setting_serial_init (NMSettingSerial *setting)
{
}
static GVariant *
parity_to_dbus (const GValue *from)
{
switch (g_value_get_enum (from)) {
case NM_SETTING_SERIAL_PARITY_EVEN:
return g_variant_new_byte ('E');
case NM_SETTING_SERIAL_PARITY_ODD:
return g_variant_new_byte ('o');
case NM_SETTING_SERIAL_PARITY_NONE:
default:
return g_variant_new_byte ('n');
}
}
static void
parity_from_dbus (GVariant *from, GValue *to)
{
switch (g_variant_get_byte (from)) {
case 'E':
g_value_set_enum (to, NM_SETTING_SERIAL_PARITY_EVEN);
break;
case 'o':
g_value_set_enum (to, NM_SETTING_SERIAL_PARITY_ODD);
break;
case 'n':
default:
g_value_set_enum (to, NM_SETTING_SERIAL_PARITY_NONE);
break;
}
}
static void
set_property (GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec)
......@@ -188,7 +219,7 @@ set_property (GObject *object, guint prop_id,
priv->bits = g_value_get_uint (value);
break;
case PROP_PARITY:
priv->parity = g_value_get_schar (value);
priv->parity = g_value_get_enum (value);
break;
case PROP_STOPBITS:
priv->stopbits = g_value_get_uint (value);
......@@ -216,7 +247,7 @@ get_property (GObject *object, guint prop_id,
g_value_set_uint (value, nm_setting_serial_get_bits (setting));
break;
case PROP_PARITY:
g_value_set_schar (value, nm_setting_serial_get_parity (setting));
g_value_set_enum (value, nm_setting_serial_get_parity (setting));
break;
case PROP_STOPBITS:
g_value_set_uint (value, nm_setting_serial_get_stopbits (setting));
......@@ -276,16 +307,21 @@ nm_setting_serial_class_init (NMSettingSerialClass *setting_class)
/**
* NMSettingSerial:parity:
*
* Parity setting of the serial port. Either 'E' for even parity, 'o' for
* odd parity, or 'n' for no parity.
* Parity setting of the serial port.
**/
g_object_class_install_property
(object_class, PROP_PARITY,
g_param_spec_char (NM_SETTING_SERIAL_PARITY, "", "",
'E', 'o', 'n',
g_param_spec_enum (NM_SETTING_SERIAL_PARITY, "", "",
NM_TYPE_SETTING_SERIAL_PARITY,
NM_SETTING_SERIAL_PARITY_NONE,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
_nm_setting_class_transform_property (parent_class,
NM_SETTING_SERIAL_PARITY,
G_VARIANT_TYPE_BYTE,
parity_to_dbus,
parity_from_dbus);
/**
* NMSettingSerial:stopbits:
......
......@@ -59,6 +59,20 @@ typedef enum {
#define NM_SETTING_SERIAL_ERROR nm_setting_serial_error_quark ()
GQuark nm_setting_serial_error_quark (void);
/**
* NMSettingSerialParity:
* @NM_SETTING_SERIAL_PARITY_NONE: No parity bits (default)
* @NM_SETTING_SERIAL_PARITY_EVEN: Even parity
* @NM_SETTING_SERIAL_PARITY_ODD: Odd parity
*
* The parity setting of a serial port.
*/
typedef enum {
NM_SETTING_SERIAL_PARITY_NONE = 0,
NM_SETTING_SERIAL_PARITY_EVEN,
NM_SETTING_SERIAL_PARITY_ODD
} NMSettingSerialParity;
#define NM_SETTING_SERIAL_BAUD "baud"
#define NM_SETTING_SERIAL_BITS "bits"
#define NM_SETTING_SERIAL_PARITY "parity"
......@@ -78,12 +92,12 @@ typedef struct {
GType nm_setting_serial_get_type (void);
NMSetting *nm_setting_serial_new (void);
guint nm_setting_serial_get_baud (NMSettingSerial *setting);
guint nm_setting_serial_get_bits (NMSettingSerial *setting);
char nm_setting_serial_get_parity (NMSettingSerial *setting);
guint nm_setting_serial_get_stopbits (NMSettingSerial *setting);
guint64 nm_setting_serial_get_send_delay (NMSettingSerial *setting);
NMSetting *nm_setting_serial_new (void);
guint nm_setting_serial_get_baud (NMSettingSerial *setting);
guint nm_setting_serial_get_bits (NMSettingSerial *setting);
NMSettingSerialParity nm_setting_serial_get_parity (NMSettingSerial *setting);
guint nm_setting_serial_get_stopbits (NMSettingSerial *setting);
guint64 nm_setting_serial_get_send_delay (NMSettingSerial *setting);
G_END_DECLS
......
......@@ -710,7 +710,7 @@ test_setting_to_dbus_transform (void)
static void
test_setting_to_dbus_enum (void)
{
NMSetting *s_ip6, *s_wsec;
NMSetting *s_ip6, *s_wsec, *s_serial;
GVariant *dict, *val;
/* enum */
......@@ -754,6 +754,23 @@ test_setting_to_dbus_enum (void)
g_variant_unref (dict);
g_object_unref (s_wsec);
/* another transformed enum */
s_serial = nm_setting_serial_new ();
g_object_set (s_serial,
NM_SETTING_SERIAL_PARITY, NM_SETTING_SERIAL_PARITY_ODD,
NULL);
dict = _nm_setting_to_dbus (s_serial, NULL, NM_CONNECTION_SERIALIZE_ALL);
g_assert (dict != NULL);
val = g_variant_lookup_value (dict, NM_SETTING_SERIAL_PARITY, G_VARIANT_TYPE_BYTE);
g_assert (val != NULL);
g_assert_cmpint (g_variant_get_byte (val), ==, 'o');
g_variant_unref (val);
g_variant_unref (dict);
g_object_unref (s_serial);
}
static void
......@@ -887,6 +904,7 @@ test_setting_new_from_dbus_enum (void)
{
NMSettingIP6Config *s_ip6;
NMSettingWirelessSecurity *s_wsec;
NMSettingSerial *s_serial;
GVariant *dict;
GVariantBuilder builder;
GError *error = NULL;
......@@ -926,6 +944,21 @@ test_setting_new_from_dbus_enum (void)
g_variant_unref (dict);
g_object_unref (s_wsec);
/* another transformed enum */
g_variant_builder_init (&builder, NM_VARIANT_TYPE_SETTING);
g_variant_builder_add (&builder, "{sv}",
NM_SETTING_SERIAL_PARITY,
g_variant_new_byte ('E'));
dict = g_variant_builder_end (&builder);
s_serial = (NMSettingSerial *) _nm_setting_new_from_dbus (NM_TYPE_SETTING_SERIAL, dict, NULL, &error);
g_assert_no_error (error);
g_assert_cmpint (nm_setting_serial_get_parity (s_serial), ==, NM_SETTING_SERIAL_PARITY_EVEN);
g_variant_unref (dict);
g_object_unref (s_serial);
}
static NMConnection *
......
......@@ -283,6 +283,16 @@ nm_setting_serial_class_init (NMSettingSerialClass *setting_class)
* Parity setting of the serial port. Either 'E' for even parity, 'o' for
* odd parity, or 'n' for no parity.
**/
/* plugins docs
* ---keyfile---
* property: parity
* format: 'e', 'o', or 'n'
* description: The connection parity; even, odd, or none. Note that older
* versions of NetworkManager stored this as an integer: 69 ('E') for even,
* 111 ('o') for odd, or 110 ('n') for none.
* example: parity=n
* ---end---
*/
g_object_class_install_property
(object_class, PROP_PARITY,
g_param_spec_char (NM_SETTING_SERIAL_PARITY, "", "",
......
......@@ -742,6 +742,7 @@ global:
nm_setting_serial_get_stopbits;
nm_setting_serial_get_type;
nm_setting_serial_new;
nm_setting_serial_parity_get_type;
nm_setting_set_secret_flags;
nm_setting_team_error_get_type;
nm_setting_team_error_quark;
......
......@@ -11331,7 +11331,7 @@ test_write_mobile_broadband (gboolean gsm)
g_object_set (s_serial,
NM_SETTING_SERIAL_BAUD, 115200,
NM_SETTING_SERIAL_BITS, 8,
NM_SETTING_SERIAL_PARITY, 'n',
NM_SETTING_SERIAL_PARITY, NM_SETTING_SERIAL_PARITY_NONE,
NM_SETTING_SERIAL_STOPBITS, 1,
NULL);
......
......@@ -893,6 +893,56 @@ cert_parser (NMSetting *setting, const char *key, GKeyFile *keyfile, const char
g_bytes_unref (bytes);
}
static void
parity_parser (NMSetting *setting, const char *key, GKeyFile *keyfile, const char *keyfile_path)
{
const char *setting_name = nm_setting_get_name (setting);
NMSettingSerialParity parity;
int int_val;
char *str_val;
/* Keyfile traditionally stored this as the ASCII value for 'E', 'o', or 'n'.
* We now accept either that or the (case-insensitive) character itself (but
* still always write it the old way, for backward compatibility).
*/
int_val = nm_keyfile_plugin_kf_get_integer (keyfile, setting_name, key, NULL);
if (!int_val) {
str_val = nm_keyfile_plugin_kf_get_string (keyfile, setting_name, key, NULL);
if (str_val) {
if (str_val[0] && !str_val[1])
int_val = str_val[0];
else {
/* This will hit the warning below */
int_val = 'X';
}
}
}
if (!int_val)
return;
switch (int_val) {
case 'E':
case 'e':
parity = NM_SETTING_SERIAL_PARITY_EVEN;
break;
case 'O':
case 'o':
parity = NM_SETTING_SERIAL_PARITY_ODD;
break;
case 'N':
case 'n':
parity = NM_SETTING_SERIAL_PARITY_NONE;
break;
default:
nm_log_warn (LOGD_SETTINGS, "%s: ignoring invalid value for %s / %s",
__func__, setting_name, key);
return;
}
g_object_set (setting, key, parity, NULL);
}
typedef struct {
const char *setting_name;
const char *key;
......@@ -1003,6 +1053,10 @@ static KeyParser key_parsers[] = {
NM_SETTING_802_1X_PHASE2_PRIVATE_KEY,
TRUE,
cert_parser },
{ NM_SETTING_SERIAL_SETTING_NAME,
NM_SETTING_SERIAL_PARITY,
TRUE,
parity_parser },
{ NULL, NULL, FALSE }
};
......
......@@ -16,6 +16,7 @@ apn=ISP.CINGULAR
[serial]
baud=115200
parity=o
[bluetooth]
bdaddr=00:11:22:33:44:55
......
......@@ -18,3 +18,4 @@ pin=2345
[serial]
baud=115200
parity=111
......@@ -1724,6 +1724,7 @@ test_read_bt_dun_connection (void)
const char *bdaddr;
const guint8 expected_bdaddr[ETH_ALEN] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 };
const char *tmp;
NMSettingSerialParity parity;
const char *expected_id = "AT&T Data Connect BT";
const char *expected_uuid = "089130ab-ce28-46e4-ad77-d44869b03d19";
const char *expected_apn = "ISP.CINGULAR";
......@@ -1860,6 +1861,13 @@ test_read_bt_dun_connection (void)
TEST_BT_DUN_FILE,
NM_SETTING_SERIAL_SETTING_NAME);
parity = nm_setting_serial_get_parity (s_serial);
ASSERT (parity == NM_SETTING_SERIAL_PARITY_ODD,
"connection-verify-serial", "failed to verify %s: unexpected %s / %s key value",
TEST_BT_DUN_FILE,
NM_SETTING_SERIAL_SETTING_NAME,
NM_SETTING_SERIAL_PARITY);
g_object_unref (connection);
}
......@@ -1965,6 +1973,7 @@ test_read_gsm_connection (void)
NMSettingBluetooth *s_bluetooth;
GError *error = NULL;
const char *tmp;
NMSettingSerialParity parity;
const char *expected_id = "AT&T Data Connect";
const char *expected_apn = "ISP.CINGULAR";
const char *expected_username = "ISP@CINGULARGPRS.COM";
......@@ -2102,6 +2111,13 @@ test_read_gsm_connection (void)
TEST_GSM_FILE,
NM_SETTING_SERIAL_SETTING_NAME);
parity = nm_setting_serial_get_parity (s_serial);
ASSERT (parity == NM_SETTING_SERIAL_PARITY_ODD,
"connection-verify-serial", "failed to verify %s: unexpected %s / %s key value",
TEST_GSM_FILE,
NM_SETTING_SERIAL_SETTING_NAME,
NM_SETTING_SERIAL_PARITY);
g_object_unref (connection);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment