kerneldevice: allow loading port attributes

In addition to loading port and device properties, we now also allow
loading sysfs properties that are assumed to be static (i.e. their
values won't change since loaded the first time).
This commit is contained in:
Aleksander Morgado
2020-10-30 10:05:24 +01:00
parent 399a042dad
commit 3d12272d18
4 changed files with 130 additions and 0 deletions

View File

@@ -963,6 +963,41 @@ kernel_device_get_property (MMKernelDevice *self,
/*****************************************************************************/
static gchar *
build_attribute_data_key (const gchar *attribute)
{
return g_strdup_printf ("ATTR:%s", attribute);
}
static gboolean
kernel_device_has_attribute (MMKernelDevice *self,
const gchar *attribute)
{
return has_sysfs_attribute (MM_KERNEL_DEVICE_GENERIC (self)->priv->sysfs_path, attribute);
}
static const gchar *
kernel_device_get_attribute (MMKernelDevice *_self,
const gchar *attribute)
{
MMKernelDeviceGeneric *self;
g_autofree gchar *key = NULL;
gchar *value = NULL;
self = MM_KERNEL_DEVICE_GENERIC (_self);
key = build_attribute_data_key (attribute);
value = g_object_get_data (G_OBJECT (self), key);
if (!value) {
value = read_sysfs_attribute_as_string (self->priv->sysfs_path, attribute);
if (value)
g_object_set_data_full (G_OBJECT (self), key, value, g_free);
}
return (const gchar *) value;
}
/*****************************************************************************/
MMKernelDevice *
mm_kernel_device_generic_new_with_rules (MMKernelEventProperties *props,
GArray *rules,
@@ -1145,6 +1180,8 @@ mm_kernel_device_generic_class_init (MMKernelDeviceGenericClass *klass)
kernel_device_class->cmp = kernel_device_cmp;
kernel_device_class->has_property = kernel_device_has_property;
kernel_device_class->get_property = kernel_device_get_property;
kernel_device_class->has_attribute = kernel_device_has_attribute;
kernel_device_class->get_attribute = kernel_device_get_attribute;
/* Device-wide properties are stored per-port in the generic backend */
kernel_device_class->has_global_property = kernel_device_has_property;

View File

@@ -527,6 +527,34 @@ kernel_device_get_global_property (MMKernelDevice *_self,
/*****************************************************************************/
static gboolean
kernel_device_has_attribute (MMKernelDevice *_self,
const gchar *attribute)
{
MMKernelDeviceUdev *self;
self = MM_KERNEL_DEVICE_UDEV (_self);
if (!self->priv->device)
return FALSE;
return g_udev_device_has_sysfs_attr (self->priv->device, attribute);
}
static const gchar *
kernel_device_get_attribute (MMKernelDevice *_self,
const gchar *attribute)
{
MMKernelDeviceUdev *self;
self = MM_KERNEL_DEVICE_UDEV (_self);
if (!self->priv->device)
return NULL;
return g_udev_device_get_sysfs_attr (self->priv->device, attribute);
}
/*****************************************************************************/
MMKernelDevice *
mm_kernel_device_udev_new (GUdevDevice *udev_device)
{
@@ -725,6 +753,8 @@ mm_kernel_device_udev_class_init (MMKernelDeviceUdevClass *klass)
kernel_device_class->get_property = kernel_device_get_property;
kernel_device_class->has_global_property = kernel_device_has_global_property;
kernel_device_class->get_global_property = kernel_device_get_global_property;
kernel_device_class->has_attribute = kernel_device_has_attribute;
kernel_device_class->get_attribute = kernel_device_get_attribute;
properties[PROP_UDEV_DEVICE] =
g_param_spec_object ("udev-device",

View File

@@ -277,6 +277,60 @@ mm_kernel_device_get_global_property_as_int_hex (MMKernelDevice *self,
return ((value && mm_get_uint_from_hex_str (value, &aux)) ? aux : 0);
}
gboolean
mm_kernel_device_has_attribute (MMKernelDevice *self,
const gchar *attribute)
{
g_return_val_if_fail (MM_IS_KERNEL_DEVICE (self), FALSE);
return (MM_KERNEL_DEVICE_GET_CLASS (self)->has_attribute ?
MM_KERNEL_DEVICE_GET_CLASS (self)->has_attribute (self, attribute) :
FALSE);
}
const gchar *
mm_kernel_device_get_attribute (MMKernelDevice *self,
const gchar *attribute)
{
g_return_val_if_fail (MM_IS_KERNEL_DEVICE (self), NULL);
return (MM_KERNEL_DEVICE_GET_CLASS (self)->get_attribute ?
MM_KERNEL_DEVICE_GET_CLASS (self)->get_attribute (self, attribute) :
NULL);
}
gboolean
mm_kernel_device_get_attribute_as_boolean (MMKernelDevice *self,
const gchar *attribute)
{
const gchar *value;
value = mm_kernel_device_get_attribute (self, attribute);
return (value && mm_common_get_boolean_from_string (value, NULL));
}
gint
mm_kernel_device_get_attribute_as_int (MMKernelDevice *self,
const gchar *attribute)
{
const gchar *value;
gint aux;
value = mm_kernel_device_get_attribute (self, attribute);
return ((value && mm_get_int_from_str (value, &aux)) ? aux : 0);
}
guint
mm_kernel_device_get_attribute_as_int_hex (MMKernelDevice *self,
const gchar *attribute)
{
const gchar *value;
guint aux;
value = mm_kernel_device_get_attribute (self, attribute);
return ((value && mm_get_uint_from_hex_str (value, &aux)) ? aux : 0);
}
/*****************************************************************************/
static gchar *

View File

@@ -62,6 +62,8 @@ struct _MMKernelDeviceClass {
const gchar * (* get_property) (MMKernelDevice *self, const gchar *property);
gboolean (* has_global_property) (MMKernelDevice *self, const gchar *property);
const gchar * (* get_global_property) (MMKernelDevice *self, const gchar *property);
gboolean (* has_attribute) (MMKernelDevice *self, const gchar *attribute);
const gchar * (* get_attribute) (MMKernelDevice *self, const gchar *attribute);
};
GType mm_kernel_device_get_type (void);
@@ -103,4 +105,11 @@ gboolean mm_kernel_device_get_global_property_as_boolean (MMKernelDevice *se
gint mm_kernel_device_get_global_property_as_int (MMKernelDevice *self, const gchar *property);
guint mm_kernel_device_get_global_property_as_int_hex (MMKernelDevice *self, const gchar *property);
/* Attributes in sysfs */
gboolean mm_kernel_device_has_attribute (MMKernelDevice *self, const gchar *attribute);
const gchar *mm_kernel_device_get_attribute (MMKernelDevice *self, const gchar *attribute);
gboolean mm_kernel_device_get_attribute_as_boolean (MMKernelDevice *self, const gchar *attribute);
gint mm_kernel_device_get_attribute_as_int (MMKernelDevice *self, const gchar *attribute);
guint mm_kernel_device_get_attribute_as_int_hex (MMKernelDevice *self, const gchar *attribute);
#endif /* MM_KERNEL_DEVICE_H */