Commit 09aa6c53 authored by Christian Kellner's avatar Christian Kellner
Browse files

device: move device identification to sysfs

Move the device identification, i.e. name and vendor detection, to
bolt-sysfs.c as 'bolt_sysfs_device_ident'. This uses the existing
helper function to first check `${attr}_name` for vendor and name
attributes and then fall back to plain `${attr}`.
parent f653fee2
......@@ -604,28 +604,6 @@ device_set_status_internal (BoltDevice *dev,
g_object_notify_by_pspec (G_OBJECT (dev), props[PROP_STATUS]);
}
static const char *
read_sysattr_name (struct udev_device *udev, const char *attr, GError **error)
{
g_autofree char *s = NULL;
const char *v;
s = g_strdup_printf ("%s_name", attr);
v = udev_device_get_sysattr_value (udev, s);
if (v != NULL)
return v;
v = udev_device_get_sysattr_value (udev, attr);
if (v == NULL)
g_set_error (error,
BOLT_ERROR, BOLT_ERROR_UDEV,
"failed to get sysfs attr: %s", attr);
return v;
}
static BoltStatus
bolt_status_from_info (BoltDevInfo *info)
{
......@@ -1213,15 +1191,11 @@ bolt_device_new_for_udev (struct udev_device *udev,
if (uid == NULL)
return NULL;
name = read_sysattr_name (udev, "device", error);
if (name == NULL)
return NULL;
vendor = read_sysattr_name (udev, "vendor", error);
if (vendor == NULL)
ok = bolt_sysfs_info_for_device (udev, TRUE, &info, error);
if (!ok)
return NULL;
ok = bolt_sysfs_info_for_device (udev, TRUE, &info, error);
ok = bolt_sysfs_device_ident (udev, &name, &vendor, error);
if (!ok)
return NULL;
......
......@@ -174,6 +174,65 @@ bolt_sysfs_security_for_device (struct udev_device *udev,
return s;
}
static const char *
read_sysattr_name (struct udev_device *udev,
const char *attr)
{
g_autofree char *s = NULL;
const char *v;
s = g_strdup_printf ("%s_name", attr);
v = udev_device_get_sysattr_value (udev, s);
if (v != NULL)
return v;
return udev_device_get_sysattr_value (udev, attr);
}
gboolean
bolt_sysfs_device_ident (struct udev_device *udev,
const char **name,
const char **vendor,
GError **error)
{
const char *val;
struct
{
const char *attr;
const char **ptr;
} fields[] = {
{"device", name},
{"vendor", vendor}
};
g_return_val_if_fail (udev != NULL, FALSE);
g_return_val_if_fail (name != NULL, FALSE);
g_return_val_if_fail (vendor != NULL, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
for (size_t i = 0; i < G_N_ELEMENTS (fields); i++)
{
const char *key = fields[i].attr;
val = read_sysattr_name (udev, key);
if (val == NULL)
{
g_set_error (error,
BOLT_ERROR, BOLT_ERROR_UDEV,
"'%s' information missing",
key);
return FALSE;
}
*(fields[i].ptr) = val;
}
return TRUE;
}
static int
bolt_syfs_count_tb_devices (struct udev *udev,
struct udev_device *parent,
......
......@@ -52,6 +52,11 @@ struct udev_device * bolt_sysfs_domain_for_device (struct udev_device *udev,
BoltSecurity bolt_sysfs_security_for_device (struct udev_device *udev,
GError **error);
gboolean bolt_sysfs_device_ident (struct udev_device *udev,
const char **name,
const char **vendor,
GError **error);
int bolt_sysfs_count_hosts (struct udev *udev,
GError **error);
......
Supports Markdown
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