libnm: add direct property type "enum"

This commit is contained in:
Thomas Haller 2021-10-20 10:53:39 +02:00
parent 1059b60873
commit 37967ad717
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
6 changed files with 155 additions and 3 deletions

View file

@ -293,6 +293,7 @@ extern const NMSettInfoPropertType nm_sett_info_propert_type_direct_int32;
extern const NMSettInfoPropertType nm_sett_info_propert_type_direct_uint32;
extern const NMSettInfoPropertType nm_sett_info_propert_type_direct_uint64;
extern const NMSettInfoPropertType nm_sett_info_propert_type_direct_string;
extern const NMSettInfoPropertType nm_sett_info_propert_type_direct_enum;
extern const NMSettInfoPropertType nm_sett_info_propert_type_direct_flags;
extern const NMSettInfoPropertType nm_sett_info_propert_type_direct_mac_address;
@ -706,6 +707,47 @@ _nm_properties_override(GArray *properties_override, const NMSettInfoProperty *p
/*****************************************************************************/
#define _nm_setting_property_define_direct_enum(properties_override, \
obj_properties, \
prop_name, \
prop_id, \
gtype_enum, \
default_value, \
param_flags, \
private_struct_type, \
private_struct_field, \
... /* extra NMSettInfoProperty fields */) \
G_STMT_START \
{ \
GParamSpec *_param_spec; \
\
G_STATIC_ASSERT( \
!NM_FLAGS_ANY((param_flags), \
~(NM_SETTING_PARAM_REAPPLY_IMMEDIATELY | NM_SETTING_PARAM_FUZZY_IGNORE \
| NM_SETTING_PARAM_INFERRABLE))); \
\
_param_spec = \
g_param_spec_enum("" prop_name "", \
"", \
"", \
(gtype_enum), \
(default_value), \
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | (param_flags)); \
\
(obj_properties)[(prop_id)] = _param_spec; \
\
_nm_properties_override_gobj( \
(properties_override), \
_param_spec, \
&nm_sett_info_propert_type_direct_enum, \
.direct_offset = \
NM_STRUCT_OFFSET_ENSURE_TYPE(int, private_struct_type, private_struct_field), \
__VA_ARGS__); \
} \
G_STMT_END
/*****************************************************************************/
#define _nm_setting_property_define_direct_flags(properties_override, \
obj_properties, \
prop_name, \

View file

@ -752,6 +752,14 @@ _nm_setting_property_get_property_direct(GObject * object,
g_value_set_uint64(value, *p_val);
return;
}
case NM_VALUE_TYPE_ENUM:
{
const int *p_val =
_nm_setting_get_private(setting, sett_info, property_info->direct_offset);
g_value_set_enum(value, *p_val);
return;
}
case NM_VALUE_TYPE_FLAGS:
{
const guint *p_val =
@ -850,6 +858,17 @@ _nm_setting_property_set_property_direct(GObject * object,
*p_val = v;
goto out_notify;
}
case NM_VALUE_TYPE_ENUM:
{
int *p_val = _nm_setting_get_private(setting, sett_info, property_info->direct_offset);
int v;
v = g_value_get_enum(value);
if (*p_val == v)
return;
*p_val = v;
goto out_notify;
}
case NM_VALUE_TYPE_FLAGS:
{
guint *p_val = _nm_setting_get_private(setting, sett_info, property_info->direct_offset);
@ -949,6 +968,16 @@ _init_direct(NMSetting *setting)
*p_val = def_val;
break;
}
case NM_VALUE_TYPE_ENUM:
{
int *p_val = _nm_setting_get_private(setting, sett_info, property_info->direct_offset);
int def_val;
def_val = NM_G_PARAM_SPEC_GET_DEFAULT_ENUM(property_info->param_spec);
nm_assert(*p_val == 0);
*p_val = def_val;
break;
}
case NM_VALUE_TYPE_FLAGS:
{
guint *p_val =
@ -998,6 +1027,7 @@ _finalize_direct(NMSetting *setting)
case NM_VALUE_TYPE_INT32:
case NM_VALUE_TYPE_UINT32:
case NM_VALUE_TYPE_UINT64:
case NM_VALUE_TYPE_ENUM:
case NM_VALUE_TYPE_FLAGS:
break;
case NM_VALUE_TYPE_STRING:
@ -1040,7 +1070,7 @@ _nm_setting_property_to_dbus_fcn_direct(_NM_SETT_INFO_PROP_TO_DBUS_FCN_ARGS _nm_
if (!property_info->to_dbus_including_default
&& val == NM_G_PARAM_SPEC_GET_DEFAULT_INT(property_info->param_spec))
return NULL;
return g_variant_new_int32(val);
return nm_g_variant_maybe_singleton_i(val);
}
case NM_VALUE_TYPE_UINT32:
{
@ -1064,6 +1094,16 @@ _nm_setting_property_to_dbus_fcn_direct(_NM_SETT_INFO_PROP_TO_DBUS_FCN_ARGS _nm_
return NULL;
return g_variant_new_uint64(val);
}
case NM_VALUE_TYPE_ENUM:
{
int val;
val = *((int *) _nm_setting_get_private(setting, sett_info, property_info->direct_offset));
if (!property_info->to_dbus_including_default
&& val == NM_G_PARAM_SPEC_GET_DEFAULT_ENUM(property_info->param_spec))
return NULL;
return nm_g_variant_maybe_singleton_i(val);
}
case NM_VALUE_TYPE_FLAGS:
{
guint val;
@ -1149,7 +1189,7 @@ _nm_setting_property_to_dbus_fcn_gprop(_NM_SETT_INFO_PROP_TO_DBUS_FCN_ARGS _nm_n
nm_assert(G_VALUE_HOLDS(&prop_value, G_TYPE_BYTES));
return nm_g_bytes_to_variant_ay(g_value_get_boxed(&prop_value));
case NM_SETTING_PROPERTY_TO_DBUS_FCN_GPROP_TYPE_ENUM:
return g_variant_new_int32(g_value_get_enum(&prop_value));
return nm_g_variant_maybe_singleton_i(g_value_get_enum(&prop_value));
case NM_SETTING_PROPERTY_TO_DBUS_FCN_GPROP_TYPE_FLAGS:
return g_variant_new_uint32(g_value_get_flags(&prop_value));
case NM_SETTING_PROPERTY_TO_DBUS_FCN_GPROP_TYPE_GARRAY_UINT:
@ -1335,6 +1375,35 @@ _nm_setting_property_from_dbus_fcn_direct(_NM_SETT_INFO_PROP_FROM_DBUS_FCN_ARGS
*p_val = v;
goto out_notify;
}
case NM_VALUE_TYPE_ENUM:
{
const GParamSpecEnum *param_spec;
int * p_val;
int v;
param_spec = NM_G_PARAM_SPEC_CAST_ENUM(property_info->param_spec);
if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT32)) {
G_STATIC_ASSERT(sizeof(int) >= sizeof(gint32));
v = g_variant_get_int32(value);
} else {
if (!_variant_get_value_transform(property_info,
value,
G_TYPE_FROM_CLASS(param_spec->enum_class),
g_value_get_flags,
&v))
goto out_error_wrong_dbus_type;
}
p_val = _nm_setting_get_private(setting, sett_info, property_info->direct_offset);
if (*p_val == v)
goto out_unchanged;
if (!g_enum_get_value(param_spec->enum_class, v))
goto out_error_param_spec_validation;
*p_val = v;
goto out_notify;
}
case NM_VALUE_TYPE_FLAGS:
{
const GParamSpecFlags *param_spec;
@ -2286,6 +2355,8 @@ _nm_setting_property_compare_fcn_direct(_NM_SETT_INFO_PROP_COMPARE_FCN_ARGS _nm_
return *((const guint32 *) p_a) == *((const guint32 *) p_b);
case NM_VALUE_TYPE_UINT64:
return *((const guint64 *) p_a) == *((const guint64 *) p_b);
case NM_VALUE_TYPE_ENUM:
return *((const int *) p_a) == *((const int *) p_b);
case NM_VALUE_TYPE_FLAGS:
return *((const guint *) p_a) == *((const guint *) p_b);
case NM_VALUE_TYPE_STRING:
@ -3351,6 +3422,15 @@ const NMSettInfoPropertType nm_sett_info_propert_type_direct_string =
.from_dbus_is_full = TRUE,
.from_dbus_direct_allow_transform = TRUE);
const NMSettInfoPropertType nm_sett_info_propert_type_direct_enum =
NM_SETT_INFO_PROPERT_TYPE_DBUS_INIT(G_VARIANT_TYPE_INT32,
.direct_type = NM_VALUE_TYPE_ENUM,
.compare_fcn = _nm_setting_property_compare_fcn_direct,
.to_dbus_fcn = _nm_setting_property_to_dbus_fcn_direct,
.from_dbus_fcn = _nm_setting_property_from_dbus_fcn_direct,
.from_dbus_is_full = TRUE,
.from_dbus_direct_allow_transform = TRUE);
const NMSettInfoPropertType nm_sett_info_propert_type_direct_flags =
NM_SETT_INFO_PROPERT_TYPE_DBUS_INIT(G_VARIANT_TYPE_UINT32,
.direct_type = NM_VALUE_TYPE_FLAGS,

View file

@ -4507,6 +4507,22 @@ test_setting_metadata(void)
g_assert_cmpuint(pspec->maximum, <=, G_MAXUINT64);
can_set_including_default = TRUE;
} else if (sip->property_type->direct_type == NM_VALUE_TYPE_ENUM) {
const GParamSpecEnum *pspec;
g_assert(sip->property_type == &nm_sett_info_propert_type_direct_enum);
g_assert(g_variant_type_equal(sip->property_type->dbus_type, "i"));
g_assert(sip->property_type->to_dbus_fcn
== _nm_setting_property_to_dbus_fcn_direct);
g_assert(sip->param_spec);
g_assert(g_type_is_a(sip->param_spec->value_type, G_TYPE_ENUM));
g_assert(sip->param_spec->value_type != G_TYPE_ENUM);
pspec = NM_G_PARAM_SPEC_CAST_ENUM(sip->param_spec);
g_assert(G_TYPE_FROM_CLASS(pspec->enum_class) == sip->param_spec->value_type);
g_assert(g_enum_get_value(pspec->enum_class, pspec->default_value));
can_set_including_default = TRUE;
} else if (sip->property_type->direct_type == NM_VALUE_TYPE_FLAGS) {
const GParamSpecFlags *pspec;

View file

@ -371,6 +371,7 @@ nm_value_type_to_json(NMValueType value_type, GString *gstr, gconstpointer p_fie
nm_json_gstr_append_int64(gstr, *((const gint32 *) p_field));
return;
case NM_VALUE_TYPE_INT:
case NM_VALUE_TYPE_ENUM:
nm_json_gstr_append_int64(gstr, *((const int *) p_field));
return;
case NM_VALUE_TYPE_INT64:
@ -408,6 +409,7 @@ nm_value_type_from_json(const NMJsonVt * vt,
case NM_VALUE_TYPE_INT32:
return (nm_jansson_json_as_int32(vt, elem, out_val) > 0);
case NM_VALUE_TYPE_INT:
case NM_VALUE_TYPE_ENUM:
return (nm_jansson_json_as_int(vt, elem, out_val) > 0);
case NM_VALUE_TYPE_INT64:
return (nm_jansson_json_as_int64(vt, elem, out_val) > 0);

View file

@ -1466,6 +1466,8 @@ GParamSpec *nm_g_object_class_find_property_from_gtype(GType gtype, const char *
_NM_G_PARAM_SPEC_CAST(param_spec, G_TYPE_UINT, GParamSpecUInt)
#define NM_G_PARAM_SPEC_CAST_UINT64(param_spec) \
_NM_G_PARAM_SPEC_CAST(param_spec, G_TYPE_UINT64, GParamSpecUInt64)
#define NM_G_PARAM_SPEC_CAST_ENUM(param_spec) \
_NM_G_PARAM_SPEC_CAST_IS_A(param_spec, G_TYPE_ENUM, GParamSpecEnum)
#define NM_G_PARAM_SPEC_CAST_FLAGS(param_spec) \
_NM_G_PARAM_SPEC_CAST_IS_A(param_spec, G_TYPE_FLAGS, GParamSpecFlags)
#define NM_G_PARAM_SPEC_CAST_STRING(param_spec) \
@ -1479,6 +1481,8 @@ GParamSpec *nm_g_object_class_find_property_from_gtype(GType gtype, const char *
(NM_G_PARAM_SPEC_CAST_UINT(NM_ENSURE_NOT_NULL(param_spec))->default_value)
#define NM_G_PARAM_SPEC_GET_DEFAULT_UINT64(param_spec) \
(NM_G_PARAM_SPEC_CAST_UINT64(NM_ENSURE_NOT_NULL(param_spec))->default_value)
#define NM_G_PARAM_SPEC_GET_DEFAULT_ENUM(param_spec) \
(NM_G_PARAM_SPEC_CAST_ENUM(NM_ENSURE_NOT_NULL(param_spec))->default_value)
#define NM_G_PARAM_SPEC_GET_DEFAULT_FLAGS(param_spec) \
(NM_G_PARAM_SPEC_CAST_FLAGS(NM_ENSURE_NOT_NULL(param_spec))->default_value)
#define NM_G_PARAM_SPEC_GET_DEFAULT_STRING(param_spec) \

View file

@ -22,7 +22,10 @@ typedef enum _nm_packed {
* serialized on D-Bus as "u". */
NM_VALUE_TYPE_FLAGS = 9,
NM_VALUE_TYPE_STRING = 10,
/* G_TYPE_ENUM */
NM_VALUE_TYPE_ENUM = 10,
NM_VALUE_TYPE_STRING = 11,
} NMValueType;
/*****************************************************************************/
@ -89,6 +92,7 @@ nm_value_type_cmp(NMValueType value_type, gconstpointer p_a, gconstpointer p_b)
NM_CMP_DIRECT(*((const gint32 *) p_a), *((const gint32 *) p_b));
return 0;
case NM_VALUE_TYPE_INT:
case NM_VALUE_TYPE_ENUM:
NM_CMP_DIRECT(*((const int *) p_a), *((const int *) p_b));
return 0;
case NM_VALUE_TYPE_INT64:
@ -131,6 +135,7 @@ nm_value_type_copy(NMValueType value_type, gpointer dst, gconstpointer src)
(*((gint32 *) dst) = *((const gint32 *) src));
return;
case NM_VALUE_TYPE_INT:
case NM_VALUE_TYPE_ENUM:
(*((int *) dst) = *((const int *) src));
return;
case NM_VALUE_TYPE_INT64:
@ -194,6 +199,7 @@ nm_value_type_get_from_variant(NMValueType value_type,
case NM_VALUE_TYPE_INT:
case NM_VALUE_TYPE_UINT:
case NM_VALUE_TYPE_ENUM:
case NM_VALUE_TYPE_FLAGS:
/* These types don't have a defined variant type, because it's not
* clear how many bits we would need. */
@ -228,6 +234,7 @@ nm_value_type_to_variant(NMValueType value_type, gconstpointer src)
case NM_VALUE_TYPE_INT:
case NM_VALUE_TYPE_UINT:
case NM_VALUE_TYPE_ENUM:
case NM_VALUE_TYPE_FLAGS:
/* These types don't have a defined variant type, because it's not
* clear how many bits we would need. */
@ -260,6 +267,7 @@ nm_value_type_get_variant_type(NMValueType value_type)
case NM_VALUE_TYPE_INT:
case NM_VALUE_TYPE_UINT:
case NM_VALUE_TYPE_ENUM:
case NM_VALUE_TYPE_FLAGS:
/* These types don't have a defined variant type, because it's not
* clear how many bits we would need. */