Commit 486f52f7 authored by Christian Kellner's avatar Christian Kellner
Browse files

device: 'type' prop to discern host/peripherals

This includes:
 - BoltDeviceType enum (HOST/PERIPHERAL)
 - "Type" (dbus) & "type" (gobject) property for the BoltDevice
 - Store them alongside name, vendor in the device database
parent c0dd8eaa
...@@ -43,13 +43,14 @@ struct _BoltDevice ...@@ -43,13 +43,14 @@ struct _BoltDevice
BoltDBusDeviceSkeleton object; BoltDBusDeviceSkeleton object;
/* device props */ /* device props */
char *dbus_path; char *dbus_path;
char *uid; char *uid;
char *name; char *name;
char *vendor; char *vendor;
BoltStatus status; BoltDeviceType type;
BoltStatus status;
/* when device is attached */ /* when device is attached */
char *syspath; char *syspath;
...@@ -74,6 +75,7 @@ enum { ...@@ -74,6 +75,7 @@ enum {
PROP_UID, PROP_UID,
PROP_NAME, PROP_NAME,
PROP_VENDOR, PROP_VENDOR,
PROP_TYPE,
PROP_STATUS, PROP_STATUS,
PROP_PARENT, PROP_PARENT,
...@@ -146,6 +148,10 @@ bolt_device_get_property (GObject *object, ...@@ -146,6 +148,10 @@ bolt_device_get_property (GObject *object,
g_value_set_string (value, dev->name); g_value_set_string (value, dev->name);
break; break;
case PROP_TYPE:
g_value_set_uint (value, dev->type);
break;
case PROP_VENDOR: case PROP_VENDOR:
g_value_set_string (value, dev->vendor); g_value_set_string (value, dev->vendor);
break; break;
...@@ -214,6 +220,10 @@ bolt_device_set_property (GObject *object, ...@@ -214,6 +220,10 @@ bolt_device_set_property (GObject *object,
dev->vendor = g_value_dup_string (value); dev->vendor = g_value_dup_string (value);
break; break;
case PROP_TYPE:
dev->type = g_value_get_uint (value);
break;
case PROP_STATUS: case PROP_STATUS:
{ {
BoltStatus old = dev->status; BoltStatus old = dev->status;
...@@ -328,6 +338,10 @@ bolt_device_class_init (BoltDeviceClass *klass) ...@@ -328,6 +338,10 @@ bolt_device_class_init (BoltDeviceClass *klass)
PROP_VENDOR, PROP_VENDOR,
"vendor"); "vendor");
g_object_class_override_property (gobject_class,
PROP_TYPE,
"type");
g_object_class_override_property (gobject_class, g_object_class_override_property (gobject_class,
PROP_STATUS, PROP_STATUS,
"status"); "status");
...@@ -425,6 +439,19 @@ string_nonzero (const char *str) ...@@ -425,6 +439,19 @@ string_nonzero (const char *str)
return str != NULL && str[0] != '\0'; return str != NULL && str[0] != '\0';
} }
static struct udev_device *
bolt_sysfs_get_parent (struct udev_device *udev,
GError **error)
{
struct udev_device * parent = udev_device_get_parent (udev);
if (parent == NULL)
g_set_error (error, BOLT_ERROR, BOLT_ERROR_UDEV,
"could not get parent udev device");
return parent;
}
static const char * static const char *
bolt_sysfs_get_parent_uid (struct udev_device *udev) bolt_sysfs_get_parent_uid (struct udev_device *udev)
{ {
...@@ -467,6 +494,13 @@ bolt_status_from_udev (struct udev_device *udev) ...@@ -467,6 +494,13 @@ bolt_status_from_udev (struct udev_device *udev)
return BOLT_STATUS_CONNECTED; return BOLT_STATUS_CONNECTED;
} }
static gboolean
bolt_sysfs_device_is_domain (struct udev_device *udev)
{
const char *devtype = udev_device_get_devtype (udev);
return bolt_streq (devtype, "thunderbolt_domain");
}
static struct udev_device * static struct udev_device *
bolt_sysfs_domain_for_device (struct udev_device *udev) bolt_sysfs_domain_for_device (struct udev_device *udev)
...@@ -478,14 +512,11 @@ bolt_sysfs_domain_for_device (struct udev_device *udev) ...@@ -478,14 +512,11 @@ bolt_sysfs_domain_for_device (struct udev_device *udev)
parent = udev; parent = udev;
do do
{ {
const char *devtype;
parent = udev_device_get_parent (parent); parent = udev_device_get_parent (parent);
if (!parent) if (!parent)
break; break;
devtype = udev_device_get_devtype (parent); found = bolt_sysfs_device_is_domain (parent);
found = bolt_streq (devtype, "thunderbolt_domain");
} }
while (!found); while (!found);
...@@ -765,11 +796,13 @@ BoltDevice * ...@@ -765,11 +796,13 @@ BoltDevice *
bolt_device_new_for_udev (struct udev_device *udev, bolt_device_new_for_udev (struct udev_device *udev,
GError **error) GError **error)
{ {
struct udev_device *parent_dev;
const char *uid; const char *uid;
const char *name; const char *name;
const char *vendor; const char *vendor;
const char *syspath; const char *syspath;
const char *parent; const char *parent;
BoltDeviceType type;
BoltDevice *dev; BoltDevice *dev;
uid = udev_device_get_sysattr_value (udev, "unique_id"); uid = udev_device_get_sysattr_value (udev, "unique_id");
...@@ -795,11 +828,27 @@ bolt_device_new_for_udev (struct udev_device *udev, ...@@ -795,11 +828,27 @@ bolt_device_new_for_udev (struct udev_device *udev,
if (name == NULL) if (name == NULL)
return NULL; return NULL;
parent_dev = bolt_sysfs_get_parent (udev, error);
if (parent_dev == NULL)
return NULL;
if (bolt_sysfs_device_is_domain (parent_dev))
{
parent = NULL;
type = BOLT_DEVICE_HOST;
}
else
{
parent = udev_device_get_sysattr_value (parent_dev, "unique_id");
type = BOLT_DEVICE_PERIPHERAL;
}
parent = bolt_sysfs_get_parent_uid (udev); parent = bolt_sysfs_get_parent_uid (udev);
dev = g_object_new (BOLT_TYPE_DEVICE, dev = g_object_new (BOLT_TYPE_DEVICE,
"uid", uid, "uid", uid,
"name", name, "name", name,
"vendor", vendor, "vendor", vendor,
"type", type,
"sysfs-path", syspath, "sysfs-path", syspath,
"parent", parent, "parent", parent,
NULL); NULL);
...@@ -972,3 +1021,9 @@ bolt_device_get_vendor (const BoltDevice *dev) ...@@ -972,3 +1021,9 @@ bolt_device_get_vendor (const BoltDevice *dev)
{ {
return dev->vendor; return dev->vendor;
} }
BoltDeviceType
bolt_device_get_device_type (const BoltDevice *dev)
{
return dev->type;
}
...@@ -76,4 +76,6 @@ const char * bolt_device_get_syspath (const BoltDevice *dev); ...@@ -76,4 +76,6 @@ const char * bolt_device_get_syspath (const BoltDevice *dev);
const char * bolt_device_get_vendor (const BoltDevice *dev); const char * bolt_device_get_vendor (const BoltDevice *dev);
BoltDeviceType bolt_device_get_device_type (const BoltDevice *dev);
G_END_DECLS G_END_DECLS
...@@ -246,6 +246,7 @@ bolt_store_put_device (BoltStore *store, ...@@ -246,6 +246,7 @@ bolt_store_put_device (BoltStore *store,
g_autoptr(GFile) entry = NULL; g_autoptr(GFile) entry = NULL;
g_autoptr(GKeyFile) kf = NULL; g_autoptr(GKeyFile) kf = NULL;
g_autofree char *data = NULL; g_autofree char *data = NULL;
BoltDeviceType type;
const char *uid; const char *uid;
gboolean ok; gboolean ok;
gsize len; gsize len;
...@@ -268,6 +269,9 @@ bolt_store_put_device (BoltStore *store, ...@@ -268,6 +269,9 @@ bolt_store_put_device (BoltStore *store,
g_key_file_set_string (kf, DEVICE_GROUP, "name", bolt_device_get_name (device)); g_key_file_set_string (kf, DEVICE_GROUP, "name", bolt_device_get_name (device));
g_key_file_set_string (kf, DEVICE_GROUP, "vendor", bolt_device_get_vendor (device)); g_key_file_set_string (kf, DEVICE_GROUP, "vendor", bolt_device_get_vendor (device));
type = bolt_device_get_device_type (device);
g_key_file_set_string (kf, DEVICE_GROUP, "type", bolt_device_type_to_string (type));
if (policy != BOLT_POLICY_INVALID) if (policy != BOLT_POLICY_INVALID)
{ {
const char *str = bolt_policy_to_string (policy); const char *str = bolt_policy_to_string (policy);
...@@ -323,7 +327,9 @@ bolt_store_get_device (BoltStore *store, const char *uid, GError **error) ...@@ -323,7 +327,9 @@ bolt_store_get_device (BoltStore *store, const char *uid, GError **error)
g_autofree char *name = NULL; g_autofree char *name = NULL;
g_autofree char *vendor = NULL; g_autofree char *vendor = NULL;
g_autofree char *data = NULL; g_autofree char *data = NULL;
g_autofree char *typestr = NULL;
g_autofree char *polstr = NULL; g_autofree char *polstr = NULL;
BoltDeviceType type;
BoltPolicy policy; BoltPolicy policy;
BoltKeyState key; BoltKeyState key;
gboolean ok; gboolean ok;
...@@ -349,9 +355,17 @@ bolt_store_get_device (BoltStore *store, const char *uid, GError **error) ...@@ -349,9 +355,17 @@ bolt_store_get_device (BoltStore *store, const char *uid, GError **error)
name = g_key_file_get_string (kf, DEVICE_GROUP, "name", NULL); name = g_key_file_get_string (kf, DEVICE_GROUP, "name", NULL);
vendor = g_key_file_get_string (kf, DEVICE_GROUP, "vendor", NULL); vendor = g_key_file_get_string (kf, DEVICE_GROUP, "vendor", NULL);
typestr = g_key_file_get_string (kf, DEVICE_GROUP, "type", NULL);
type = bolt_device_type_from_string (typestr);
polstr = g_key_file_get_string (kf, USER_GROUP, "policy", NULL); polstr = g_key_file_get_string (kf, USER_GROUP, "policy", NULL);
policy = bolt_policy_from_string (polstr); policy = bolt_policy_from_string (polstr);
if (!bolt_device_type_validate (type))
{
g_warning ("[%s] invalid type in store: %s", uid, typestr);
type = BOLT_DEVICE_PERIPHERAL;
}
if (!bolt_policy_validate (policy)) if (!bolt_policy_validate (policy))
{ {
g_warning ("[%s] invalid policy in store: %s", uid, polstr); g_warning ("[%s] invalid policy in store: %s", uid, polstr);
...@@ -367,6 +381,7 @@ bolt_store_get_device (BoltStore *store, const char *uid, GError **error) ...@@ -367,6 +381,7 @@ bolt_store_get_device (BoltStore *store, const char *uid, GError **error)
"uid", uid, "uid", uid,
"name", name, "name", name,
"vendor", vendor, "vendor", vendor,
"type", type,
"status", BOLT_STATUS_DISCONNECTED, "status", BOLT_STATUS_DISCONNECTED,
"store", store, "store", store,
"policy", policy, "policy", policy,
......
...@@ -39,6 +39,7 @@ enum { ...@@ -39,6 +39,7 @@ enum {
PROP_UID, PROP_UID,
PROP_NAME, PROP_NAME,
PROP_VENDOR, PROP_VENDOR,
PROP_TYPE,
PROP_STATUS, PROP_STATUS,
PROP_PARENT, PROP_PARENT,
PROP_SYSPATH, PROP_SYSPATH,
...@@ -73,6 +74,7 @@ bolt_device_get_dbus_props (guint *n) ...@@ -73,6 +74,7 @@ bolt_device_get_dbus_props (guint *n)
{"Uid", "uid", PROP_UID, NULL}, {"Uid", "uid", PROP_UID, NULL},
{"Name", "name", PROP_NAME, NULL}, {"Name", "name", PROP_NAME, NULL},
{"Vendor", "vendor", PROP_VENDOR, NULL}, {"Vendor", "vendor", PROP_VENDOR, NULL},
{"Type", "type", PROP_TYPE, NULL},
{"Status", "status", PROP_STATUS, NULL}, {"Status", "status", PROP_STATUS, NULL},
{"Parent", "parent", PROP_PARENT, NULL}, {"Parent", "parent", PROP_PARENT, NULL},
{"SysfsPath", "syspath", PROP_SYSPATH, NULL}, {"SysfsPath", "syspath", PROP_SYSPATH, NULL},
...@@ -119,6 +121,14 @@ bolt_device_class_init (BoltDeviceClass *klass) ...@@ -119,6 +121,14 @@ bolt_device_class_init (BoltDeviceClass *klass)
G_PARAM_READABLE | G_PARAM_READABLE |
G_PARAM_STATIC_NICK); G_PARAM_STATIC_NICK);
props[PROP_TYPE] =
g_param_spec_enum ("type",
NULL, NULL,
BOLT_TYPE_DEVICE_TYPE,
BOLT_DEVICE_PERIPHERAL,
G_PARAM_READABLE |
G_PARAM_STATIC_NICK);
props[PROP_STATUS] = props[PROP_STATUS] =
g_param_spec_enum ("status", g_param_spec_enum ("status",
NULL, NULL, NULL, NULL,
......
...@@ -136,3 +136,48 @@ bolt_policy_validate (BoltPolicy policy) ...@@ -136,3 +136,48 @@ bolt_policy_validate (BoltPolicy policy)
{ {
return policy < BOLT_POLICY_INVALID && policy >= 0; return policy < BOLT_POLICY_INVALID && policy >= 0;
} }
BoltDeviceType
bolt_device_type_from_string (const char *str)
{
g_autoptr(GEnumClass) klass = NULL;
GEnumValue *value;
if (str == NULL)
return BOLT_DEVICE_PERIPHERAL;
klass = g_type_class_ref (BOLT_TYPE_DEVICE_TYPE);
value = g_enum_get_value_by_nick (klass, str);
if (value == NULL)
return BOLT_DEVICE_TYPE_INVALID;
return value->value;
}
const char *
bolt_device_type_to_string (BoltDeviceType type)
{
g_autoptr(GEnumClass) klass = NULL;
GEnumValue *value;
if (!bolt_device_type_validate (type))
return NULL;
klass = g_type_class_ref (BOLT_TYPE_DEVICE_TYPE);
value = g_enum_get_value (klass, type);
return value->value_nick;
}
gboolean
bolt_device_type_validate (BoltDeviceType type)
{
return type < BOLT_DEVICE_TYPE_INVALID && type >= 0;
}
gboolean
bolt_device_type_is_host (BoltDeviceType type)
{
return type == BOLT_DEVICE_HOST;
}
...@@ -140,3 +140,25 @@ typedef enum { ...@@ -140,3 +140,25 @@ typedef enum {
BOLT_AUTH_FLAGS_INVALID = BOLT_AUTH_FLAGS_LAST BOLT_AUTH_FLAGS_INVALID = BOLT_AUTH_FLAGS_LAST
} BoltAuthFlags; } BoltAuthFlags;
/**
* BoltDeviceType:
* @BOLT_DEVICE_HOST: The device representing the host
* @BOLT_DEVICE_PERIPHERAL: A generic thunderbolt peripheral
*
* The type of the device.
*/
typedef enum {
BOLT_DEVICE_HOST,
BOLT_DEVICE_PERIPHERAL,
BOLT_DEVICE_TYPE_LAST,
BOLT_DEVICE_TYPE_INVALID = BOLT_DEVICE_TYPE_LAST
} BoltDeviceType;
BoltDeviceType bolt_device_type_from_string (const char *str);
const char * bolt_device_type_to_string (BoltDeviceType type);
gboolean bolt_device_type_validate (BoltDeviceType type);
gboolean bolt_device_type_is_host (BoltDeviceType type);
...@@ -160,6 +160,12 @@ ...@@ -160,6 +160,12 @@
</doc:para></doc:description></doc:doc> </doc:para></doc:description></doc:doc>
</property> </property>
<property name="Type" type="u" access="read">
<doc:doc><doc:description><doc:para>
The type of the device: 0 = Host, 1 = Peripheral
</doc:para></doc:description></doc:doc>
</property>
<property name="Status" type="u" access="read"> <property name="Status" type="u" access="read">
<doc:doc><doc:description><doc:para> <doc:doc><doc:description><doc:para>
The current status of the device. The current status of the device.
......
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