Commit dac4642c authored by Christian Kellner's avatar Christian Kellner
Browse files

device: add status property (and enum)

parent 75eae0ee
......@@ -20,6 +20,7 @@
#include "config.h"
#include "bolt-dbus.h"
#include "bolt-device.h"
#include "bolt-error.h"
#include "bolt-io.h"
......@@ -41,6 +42,8 @@ struct _BoltDevice
char *name;
char *vendor;
BoltStatus status;
/* when device is attached */
char *syspath;
DIR *devdir;
......@@ -53,6 +56,7 @@ enum {
PROP_UID,
PROP_NAME,
PROP_VENDOR,
PROP_STATUS,
PROP_SYSFS,
PROP_LAST
......@@ -109,6 +113,10 @@ bolt_device_get_property (GObject *object,
g_value_set_string (value, dev->vendor);
break;
case PROP_STATUS:
g_value_set_uint (value, dev->status);
break;
case PROP_SYSFS:
g_value_set_string (value, dev->syspath);
break;
......@@ -140,6 +148,10 @@ bolt_device_set_property (GObject *object,
dev->name = g_value_dup_string (value);
break;
case PROP_STATUS:
dev->status = g_value_get_uint (value);
break;
case PROP_SYSFS:
dev->syspath = g_value_dup_string (value);
break;
......@@ -172,6 +184,10 @@ bolt_device_class_init (BoltDeviceClass *klass)
PROP_VENDOR,
"vendor");
g_object_class_override_property (gobject_class,
PROP_STATUS,
"status");
g_object_class_override_property (gobject_class,
PROP_SYSFS,
"sysfs-path");
......@@ -195,6 +211,68 @@ read_sysattr_name (int fd, const char *attr, GError **error)
return v;
}
static gint
read_sysfs_attr_int (struct udev_device *device, const char *attr)
{
const char *str;
char *end;
gint64 val;
str = udev_device_get_sysattr_value (device, attr);
if (str == NULL)
return 0;
val = g_ascii_strtoull (str, &end, 0);
if (str == end)
return 0;
if (val > G_MAXINT || val < G_MININT)
{
g_warning ("value read from sysfs outside of guint's range.");
val = 0;
}
return (gint) val;
}
static gboolean
string_nonzero (const char *str)
{
return str != NULL && str[0] != '\0';
}
static BoltStatus
bolt_status_from_udev (struct udev_device *udev)
{
gint authorized;
const char *key;
gboolean have_key;
authorized = read_sysfs_attr_int (udev, "authorized");
if (authorized == 2)
return BOLT_STATUS_AUTHORIZED_SECURE;
key = udev_device_get_sysattr_value (udev, "key");
have_key = string_nonzero (key);
if (authorized == 1)
{
if (have_key)
return BOLT_STATUS_AUTHORIZED_NEWKEY;
else
return BOLT_STATUS_AUTHORIZED;
}
else if (authorized == 0 && have_key)
{
return BOLT_STATUS_AUTH_ERROR;
}
return BOLT_STATUS_CONNECTED;
}
/* public methods */
BoltDevice *
......@@ -239,6 +317,7 @@ bolt_device_new_for_udev (BoltManager *mgr,
dev->vendor = g_steal_pointer (&vendor);
dev->syspath = g_strdup (sysfs);
dev->devdir = g_steal_pointer (&devdir);
dev->status = bolt_status_from_udev (udev);
g_object_add_weak_pointer (G_OBJECT (mgr),
(gpointer *) &dev->mgr);
......
......@@ -23,3 +23,38 @@
#define BOLT_DBUS_NAME "org.freedesktop.Bolt"
#define BOLT_DBUS_PATH "/org/freedesktop/Bolt"
#define BOLT_DBUS_INTERFACE "org.freedesktop.Bolt1.Manager"
#define BOLT_DBUS_DEVICE_INTERFACE "org.freedesktop.Bolt1.Device"
/**
* BoltStatus
* @BOLT_STATUS_DISCONNECTED: Device is not connected.
* @BOLT_STATUS_CONNECTING: Device is currently connecting.
* @BOLT_STATUS_CONNECTED: Device is connected, but not authorized.
* @BOLT_STATUS_AUTHORIZING: Device is currently authorizing.
* @BOLT_STATUS_AUTH_ERROR: Failed to authorize a device via a key.
* @BOLT_STATUS_AUTHORIZED: Device connected and authorized.
* @BOLT_STATUS_AUTHORIZED_SECURE: Device connected and securely authorized via a key.
* @BOLT_STATUS_AUTHORIZED_NEWKEY: Device connected and authorized via a new key.
* @BOLT_STATUS_GHOST: A parent device is authorizing and therefore the status
* of the device is not know, but most likely it will reappear after the parent
* device is done authenticating.
*
* The current status of the device.
*/
typedef enum {
BOLT_STATUS_DISCONNECTED = 0,
BOLT_STATUS_CONNECTING,
BOLT_STATUS_CONNECTED,
BOLT_STATUS_AUTHORIZING,
BOLT_STATUS_AUTH_ERROR,
BOLT_STATUS_AUTHORIZED,
BOLT_STATUS_AUTHORIZED_SECURE,
BOLT_STATUS_AUTHORIZED_NEWKEY,
BOLT_STATUS_GHOST,
BOLT_STATUS_LAST
} BoltStatus;
......@@ -88,6 +88,12 @@
</doc:para></doc:description></doc:doc>
</property>
<property name="Status" type="u" access="read">
<doc:doc><doc:description><doc:para>
The current status of the device.
</doc:para></doc:description></doc:doc>
</property>
<property name="SysfsPath" type="s" access="read">
<doc:doc><doc:description><doc:para>
The sysfs path of the device, if it is connected.
......
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