cli: merge branch 'th/cli-fix-serial-properties'

https://github.com/NetworkManager/NetworkManager/pull/233

(cherry picked from commit 92696036d2)
This commit is contained in:
Thomas Haller 2018-10-17 16:28:20 +02:00
commit d237cec2f8
8 changed files with 2899 additions and 2067 deletions

View file

@ -783,7 +783,8 @@ _get_fcn_gobject_int (ARGS_GET_FCN)
{
GParamSpec *pspec;
nm_auto_unset_gvalue GValue gval = G_VALUE_INIT;
gint64 v;
gboolean is_uint64 = FALSE;
NMMetaSignUnsignInt64 v;
guint base = 10;
const NMMetaUtilsIntValueInfo *value_infos;
char *return_str;
@ -799,13 +800,18 @@ _get_fcn_gobject_int (ARGS_GET_FCN)
NM_SET_OUT (out_is_default, g_param_value_defaults (pspec, &gval));
switch (pspec->value_type) {
case G_TYPE_INT:
v = g_value_get_int (&gval);
v.i64 = g_value_get_int (&gval);
break;
case G_TYPE_UINT:
v = g_value_get_uint (&gval);
v.u64 = g_value_get_uint (&gval);
is_uint64 = TRUE;
break;
case G_TYPE_INT64:
v = g_value_get_int64 (&gval);
v.i64 = g_value_get_int64 (&gval);
break;
case G_TYPE_UINT64:
v.u64 = g_value_get_uint64 (&gval);
is_uint64 = TRUE;
break;
default:
g_return_val_if_reached (NULL);
@ -819,10 +825,16 @@ _get_fcn_gobject_int (ARGS_GET_FCN)
switch (base) {
case 10:
return_str = g_strdup_printf ("%"G_GINT64_FORMAT, v);
if (is_uint64)
return_str = g_strdup_printf ("%"G_GUINT64_FORMAT, v.u64);
else
return_str = g_strdup_printf ("%"G_GINT64_FORMAT, v.i64);
break;
case 16:
return_str = g_strdup_printf ("0x%"G_GINT64_MODIFIER"x", v);
if (is_uint64)
return_str = g_strdup_printf ("0x%"G_GINT64_MODIFIER"x", v.u64);
else
return_str = g_strdup_printf ("0x%"G_GINT64_MODIFIER"x", (guint64) v.i64);
break;
default:
return_str = NULL;
@ -833,7 +845,8 @@ _get_fcn_gobject_int (ARGS_GET_FCN)
&& property_info->property_typ_data
&& (value_infos = property_info->property_typ_data->subtype.gobject_int.value_infos)) {
for (; value_infos->nick; value_infos++) {
if (value_infos->value == v) {
if ( ( is_uint64 && value_infos->value.u64 == v.u64)
|| (!is_uint64 && value_infos->value.i64 == v.i64)) {
char *old_str = return_str;
return_str = g_strdup_printf ("%s (%s)", old_str, value_infos->nick);
@ -1060,16 +1073,21 @@ _set_fcn_gobject_int (ARGS_SET_FCN)
int errsv;
const GParamSpec *pspec;
nm_auto_unset_gvalue GValue gval = G_VALUE_INIT;
gint64 v = 0;
gboolean is_uint64;
NMMetaSignUnsignInt64 v;
gboolean has_minmax = FALSE;
gint64 min = G_MININT64;
gint64 max = G_MAXINT64;
NMMetaSignUnsignInt64 min = { 0 };
NMMetaSignUnsignInt64 max = { 0 };
guint base = 10;
const NMMetaUtilsIntValueInfo *value_infos = NULL;
gboolean has_value = FALSE;
const NMMetaUtilsIntValueInfo *value_infos;
pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (G_OBJECT (setting)), property_info->property_name);
if (!G_IS_PARAM_SPEC (pspec))
g_return_val_if_reached (FALSE);
is_uint64 = NM_IN_SET (pspec->value_type, G_TYPE_UINT, G_TYPE_UINT64);
if (property_info->property_typ_data) {
if ( value
&& (value_infos = property_info->property_typ_data->subtype.gobject_int.value_infos)) {
gs_free char *vv_stripped = NULL;
@ -1083,85 +1101,106 @@ _set_fcn_gobject_int (ARGS_SET_FCN)
for (; value_infos->nick; value_infos++) {
if (nm_streq (value_infos->nick, vv)) {
v = value_infos->value;
has_value = TRUE;
break;
goto have_value_from_nick;
}
}
}
if (property_info->property_typ_data->subtype.gobject_int.base > 0)
base = property_info->property_typ_data->subtype.gobject_int.base;
if ( property_info->property_typ_data->subtype.gobject_int.min
|| property_info->property_typ_data->subtype.gobject_int.max) {
if ( ( is_uint64
&& ( property_info->property_typ_data->subtype.gobject_int.min.u64
|| property_info->property_typ_data->subtype.gobject_int.max.u64))
|| ( !is_uint64
&& ( property_info->property_typ_data->subtype.gobject_int.min.i64
|| property_info->property_typ_data->subtype.gobject_int.max.i64))) {
min = property_info->property_typ_data->subtype.gobject_int.min;
max = property_info->property_typ_data->subtype.gobject_int.max;
has_minmax = TRUE;
}
}
pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (G_OBJECT (setting)), property_info->property_name);
if (!G_IS_PARAM_SPEC (pspec))
g_return_val_if_reached (FALSE);
switch (pspec->value_type) {
case G_TYPE_INT:
if (!has_minmax) {
const GParamSpecInt *p = (GParamSpecInt *) pspec;
if (!has_minmax) {
switch (pspec->value_type) {
case G_TYPE_INT:
{
const GParamSpecInt *p = (GParamSpecInt *) pspec;
min = p->minimum;
max = p->maximum;
}
break;
case G_TYPE_UINT:
if (!has_minmax) {
const GParamSpecUInt *p = (GParamSpecUInt *) pspec;
min.i64 = p->minimum;
max.i64 = p->maximum;
}
break;
case G_TYPE_UINT:
{
const GParamSpecUInt *p = (GParamSpecUInt *) pspec;
min = p->minimum;
max = p->maximum;
}
break;
case G_TYPE_INT64:
if (!has_minmax) {
const GParamSpecInt64 *p = (GParamSpecInt64 *) pspec;
min.u64 = p->minimum;
max.u64 = p->maximum;
}
break;
case G_TYPE_INT64:
{
const GParamSpecInt64 *p = (GParamSpecInt64 *) pspec;
min = p->minimum;
max = p->maximum;
min.i64 = p->minimum;
max.i64 = p->maximum;
}
break;
case G_TYPE_UINT64:
{
const GParamSpecUInt64 *p = (GParamSpecUInt64 *) pspec;
min.u64 = p->minimum;
max.u64 = p->maximum;
}
break;
default:
g_return_val_if_reached (FALSE);
}
break;
default:
g_return_val_if_reached (FALSE);
}
if (!has_value) {
v = _nm_utils_ascii_str_to_int64 (value, base, min, max, 0);
if (is_uint64)
v.u64 = _nm_utils_ascii_str_to_uint64 (value, base, min.u64, max.u64, 0);
else
v.i64 = _nm_utils_ascii_str_to_int64 (value, base, min.i64, max.i64, 0);
if ((errsv = errno) != 0) {
if (errsv == ERANGE) {
if ((errsv = errno) != 0) {
if (errsv == ERANGE) {
if (is_uint64) {
g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_INVALID_ARGUMENT,
_("'%s' is out of range [%lli, %lli]"),
value,
(long long) min,
(long long) max);
_("'%s' is out of range [%"G_GUINT64_FORMAT", %"G_GUINT64_FORMAT"]"),
value, min.u64, max.u64);
} else {
g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_INVALID_ARGUMENT,
_("'%s' is not a valid number"), value);
_("'%s' is out of range [%"G_GINT64_FORMAT", %"G_GINT64_FORMAT"]"),
value, min.i64, max.i64);
}
return FALSE;
} else {
g_set_error (error, NM_UTILS_ERROR, NM_UTILS_ERROR_INVALID_ARGUMENT,
_("'%s' is not a valid number"), value);
}
return FALSE;
}
have_value_from_nick:
g_value_init (&gval, pspec->value_type);
switch (pspec->value_type) {
case G_TYPE_INT:
g_value_set_int (&gval, v);
g_value_set_int (&gval, v.i64);
break;
case G_TYPE_UINT:
g_value_set_uint (&gval, v);
g_value_set_uint (&gval, v.u64);
break;
case G_TYPE_INT64:
g_value_set_int64 (&gval, v);
g_value_set_int64 (&gval, v.i64);
break;
case G_TYPE_UINT64:
g_value_set_uint64 (&gval, v.u64);
break;
default:
nm_assert_not_reached ();
g_return_val_if_reached (FALSE);
break;
}
@ -1302,12 +1341,13 @@ _set_fcn_gobject_enum (ARGS_SET_FCN)
g_value_set_int (&gval, v);
else if (gtype_prop == G_TYPE_UINT)
g_value_set_uint (&gval, v);
else if (G_IS_ENUM_CLASS (gtype_class))
g_value_set_enum (&gval, v);
else if (G_IS_FLAGS_CLASS (gtype_class))
else if (is_flags) {
nm_assert (G_IS_FLAGS_CLASS (gtype_class));
g_value_set_flags (&gval, v);
else
g_return_val_if_reached (FALSE);
} else {
nm_assert (G_IS_ENUM_CLASS (gtype_class));
g_value_set_enum (&gval, v);
}
if (!nm_g_object_set_property (G_OBJECT (setting), property_info->property_name, &gval, NULL))
goto fail;
@ -7120,7 +7160,6 @@ static const NMMetaPropertyInfo *const property_infos_SERIAL[] = {
.property_type = &_pt_gobject_enum,
.property_typ_data = DEFINE_PROPERTY_TYP_DATA (
PROPERTY_TYP_DATA_SUBTYPE (gobject_enum,
.get_gtype = nm_setting_serial_parity_get_type,
.value_infos = ENUM_VALUE_INFOS (
{
.value = NM_SETTING_SERIAL_PARITY_EVEN,

View file

@ -232,9 +232,14 @@ struct _NMMetaPropertyType {
struct _NMUtilsEnumValueInfo;
typedef union {
gint64 i64;
guint64 u64;
} NMMetaSignUnsignInt64;
typedef struct {
const char *nick;
gint64 value;
NMMetaSignUnsignInt64 value;
} NMMetaUtilsIntValueInfo;
struct _NMMetaPropertyTypData {
@ -255,8 +260,8 @@ struct _NMMetaPropertyTypData {
int value);
} gobject_enum;
struct {
gint64 min;
gint64 max;
NMMetaSignUnsignInt64 min;
NMMetaSignUnsignInt64 max;
guint base;
const NMMetaUtilsIntValueInfo *value_infos;
} gobject_int;

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -872,6 +872,11 @@ class TestNmcli(NmTestBase):
self.call_nmcli_l(['c', 's'],
replace_stdout = replace_stdout)
replace_stdout.append((Util.memoize_nullary(lambda: self.srv.findConnectionUuid('con-gsm1')), 'UUID-con-gsm1-REPLACED-REPLACED-REPL'))
self.call_nmcli(['connection', 'add', 'type', 'gsm', 'autoconnect', 'no', 'con-name', 'con-gsm1', 'ifname', '*', 'apn', 'xyz.con-gsm1', 'serial.baud', '5', 'serial.send-delay', '100', 'serial.pari', '1'],
replace_stdout = replace_stdout)
replace_stdout.append((Util.memoize_nullary(lambda: self.srv.findConnectionUuid('ethernet')), 'UUID-ethernet-REPLACED-REPLACED-REPL'))
self.call_nmcli(['c', 'add', 'type', 'ethernet', 'ifname', '*'],
@ -887,6 +892,9 @@ class TestNmcli(NmTestBase):
replace_stdout = replace_stdout,
sort_lines_stdout = True)
self.call_nmcli_l(['con', 's', 'con-gsm1'],
replace_stdout = replace_stdout)
# activate the same profile on multiple devices. Our stub-implmentation
# is fine with that... although NetworkManager service would reject
# such a configuration by deactivating the profile first. But note that

View file

@ -622,6 +622,50 @@ _nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 ma
return v;
}
guint64
_nm_utils_ascii_str_to_uint64 (const char *str, guint base, guint64 min, guint64 max, guint64 fallback)
{
guint64 v;
const char *s = NULL;
if (str) {
while (g_ascii_isspace (str[0]))
str++;
}
if (!str || !str[0]) {
errno = EINVAL;
return fallback;
}
errno = 0;
v = g_ascii_strtoull (str, (char **) &s, base);
if (errno != 0)
return fallback;
if (s[0] != '\0') {
while (g_ascii_isspace (s[0]))
s++;
if (s[0] != '\0') {
errno = EINVAL;
return fallback;
}
}
if (v > max || v < min) {
errno = ERANGE;
return fallback;
}
if ( v != 0
&& str[0] == '-') {
/* I don't know why, but g_ascii_strtoull() accepts minus signs ("-2" gives 18446744073709551614).
* For "-0" that is OK, but otherwise not. */
errno = ERANGE;
return fallback;
}
return v;
}
/*****************************************************************************/
/* like nm_strcmp_p(), suitable for g_ptr_array_sort_with_data().

View file

@ -374,7 +374,8 @@ gboolean nm_utils_parse_inaddr_prefix (int addr_family,
char **out_addr,
int *out_prefix);
gint64 _nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 max, gint64 fallback);
gint64 _nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 max, gint64 fallback);
guint64 _nm_utils_ascii_str_to_uint64 (const char *str, guint base, guint64 min, guint64 max, guint64 fallback);
int _nm_utils_ascii_str_to_bool (const char *str,
int default_value);

View file

@ -281,6 +281,8 @@ class Util:
return GLib.Variant('s', str(val))
if isinstance(val, dbus.UInt32):
return GLib.Variant('u', int(val))
if isinstance(val, dbus.UInt64):
return GLib.Variant('t', int(val))
if isinstance(val, dbus.Boolean):
return GLib.Variant('b', bool(val))
if isinstance(val, dbus.Byte):
@ -481,11 +483,12 @@ class NmUtil:
if not do_verify_strict:
return;
t = s_con[NM.SETTING_CONNECTION_TYPE]
if t not in [ NM.SETTING_WIRED_SETTING_NAME,
NM.SETTING_WIRELESS_SETTING_NAME,
if t not in [ NM.SETTING_GSM_SETTING_NAME,
NM.SETTING_VLAN_SETTING_NAME,
NM.SETTING_VPN_SETTING_NAME,
NM.SETTING_WIMAX_SETTING_NAME,
NM.SETTING_VPN_SETTING_NAME ]:
NM.SETTING_WIRED_SETTING_NAME,
NM.SETTING_WIRELESS_SETTING_NAME ]:
raise BusErr.InvalidPropertyException('connection.type: unsupported connection type "%s"' % (t))
try: