gudevdevice.c revision 3e2147858f21943d5f4a781c60f33ac22c6096ed
/* -*- Mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2008 David Zeuthen <davidz@redhat.com>
*
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include "gudevdevice.h"
#include "gudevprivate.h"
/**
* SECTION:gudevdevice
* @short_description: Get information about a device
*
* The #GUdevDevice class is used to get information about a specific
* device. Note that you cannot instantiate a #GUdevDevice object
* yourself. Instead you must use #GUdevClient to obtain #GUdevDevice
* objects.
*
* To get basic information about a device, use
* g_udev_device_get_subsystem(), g_udev_device_get_devtype(),
* g_udev_device_get_name(), g_udev_device_get_number(),
* g_udev_device_get_sysfs_path(), g_udev_device_get_driver(),
* g_udev_device_get_action(), g_udev_device_get_seqnum(),
* g_udev_device_get_device_type(), g_udev_device_get_device_number(),
* g_udev_device_get_device_file(),
* g_udev_device_get_device_file_symlinks().
*
* To navigate the device tree, use g_udev_device_get_parent() and
* g_udev_device_get_parent_with_subsystem().
*
* To access udev properties for the device, use
* g_udev_device_get_property_keys(),
* g_udev_device_has_property(),
* g_udev_device_get_property(),
* g_udev_device_get_property_as_int(),
* g_udev_device_get_property_as_uint64(),
* g_udev_device_get_property_as_double(),
* g_udev_device_get_property_as_boolean() and
* g_udev_device_get_property_as_strv().
*
* To access sysfs attributes for the device, use
* g_udev_device_get_sysfs_attr(),
* g_udev_device_get_sysfs_attr_as_int(),
* g_udev_device_get_sysfs_attr_as_uint64(),
* g_udev_device_get_sysfs_attr_as_double(),
* g_udev_device_get_sysfs_attr_as_boolean() and
* g_udev_device_get_sysfs_attr_as_strv().
*
* Note that all getters on #GUdevDevice are non-reffing – returned
* values are owned by the object, should not be freed and are only
* valid as long as the object is alive.
*
* By design, #GUdevDevice will not react to changes for a device – it
* only contains a snapshot of information when the #GUdevDevice
* object was created. To work with changes, you typically connect to
* the #GUdevClient::uevent signal on a #GUdevClient and get a new
* #GUdevDevice whenever an event happens.
*/
struct _GUdevDevicePrivate
{
struct udev_device *udevice;
/* computed ondemand and cached */
};
static void
{
}
static void
{
}
static void
{
}
{
return device;
}
/**
* g_udev_device_get_subsystem:
* @device: A #GUdevDevice.
*
* Gets the subsystem for @device.
*
* Returns: The subsystem for @device.
*/
const gchar *
{
}
/**
* g_udev_device_get_devtype:
* @device: A #GUdevDevice.
*
* Gets the device type for @device.
*
* Returns: The devtype for @device.
*/
const gchar *
{
}
/**
* g_udev_device_get_name:
* @device: A #GUdevDevice.
*
* Gets the name of @device, e.g. "sda3".
*
* Returns: The name of @device.
*/
const gchar *
{
}
/**
* g_udev_device_get_number:
* @device: A #GUdevDevice.
*
* Gets the number of @device, e.g. "3" if g_udev_device_get_name() returns "sda3".
*
* Returns: The number of @device.
*/
const gchar *
{
}
/**
* g_udev_device_get_sysfs_path:
* @device: A #GUdevDevice.
*
* Gets the sysfs path for @device.
*
* Returns: The sysfs path for @device.
*/
const gchar *
{
}
/**
* g_udev_device_get_driver:
* @device: A #GUdevDevice.
*
* Gets the name of the driver used for @device.
*
* Returns: The name of the driver for @device or %NULL if unknown.
*/
const gchar *
{
}
/**
* g_udev_device_get_action:
* @device: A #GUdevDevice.
*
* Gets the most recent action (e.g. "add", "remove", "change", etc.) for @device.
*
* Returns: An action string.
*/
const gchar *
{
}
/**
* g_udev_device_get_seqnum:
* @device: A #GUdevDevice.
*
* Gets the most recent sequence number for @device.
*
* Returns: A sequence number.
*/
{
}
/**
* g_udev_device_get_device_type:
* @device: A #GUdevDevice.
*
* Gets the type of the device file, if any, for @device.
*
* Returns: The device number for @device or #G_UDEV_DEVICE_TYPE_NONE if the device does not have a device file.
*/
{
const gchar *device_file;
/* TODO: would be better to have support for this in libudev... */
if (device_file == NULL)
goto out;
goto out;
out:
return type;
}
/**
* g_udev_device_get_device_number:
* @device: A #GUdevDevice.
*
* Gets the device number, if any, for @device.
*
* Returns: The device number for @device or 0 if unknown.
*/
{
}
/**
* g_udev_device_get_device_file:
* @device: A #GUdevDevice.
*
* Gets the device file for @device.
*
* Returns: The device file for @device or %NULL if no device file
* exists.
*/
const gchar *
{
}
/**
* g_udev_device_get_device_file_symlinks:
* @device: A #GUdevDevice.
*
* Gets a list of symlinks (in <literal>/dev</literal>) that points to
* the device file for @device.
*
* Returns: (transfer none) (array zero-terminated=1) (element-type utf8): A %NULL terminated string array of symlinks. This array is owned by @device and should not be freed by the caller.
*/
const gchar * const *
{
struct udev_list_entry *l;
GPtrArray *p;
goto out;
p = g_ptr_array_new ();
for (l = udev_device_get_devlinks_list_entry (device->priv->udevice); l != NULL; l = udev_list_entry_get_next (l))
{
}
g_ptr_array_add (p, NULL);
out:
}
/* ---------------------------------------------------------------------------------------------------- */
/**
* g_udev_device_get_parent:
* @device: A #GUdevDevice.
*
* Gets the immediate parent of @device, if any.
*
* Returns: (transfer full): A #GUdevDevice or %NULL if @device has no parent. Free with g_object_unref().
*/
{
struct udev_device *udevice;
goto out;
out:
return ret;
}
/**
* g_udev_device_get_parent_with_subsystem:
* @device: A #GUdevDevice.
* @subsystem: The subsystem of the parent to get.
* @devtype: (allow-none): The devtype of the parent to get or %NULL.
*
* Walks up the chain of parents of @device and returns the first
* device encountered where @subsystem and @devtype matches, if any.
*
* Returns: (transfer full): A #GUdevDevice or %NULL if @device has no parent with @subsystem and @devtype. Free with g_object_unref().
*/
{
struct udev_device *udevice;
devtype);
goto out;
out:
return ret;
}
/* ---------------------------------------------------------------------------------------------------- */
/**
* g_udev_device_get_property_keys:
* @device: A #GUdevDevice.
*
* Gets all keys for properties on @device.
*
* Returns: (transfer none) (array zero-terminated=1) (element-type utf8): A %NULL terminated string array of property keys. This array is owned by @device and should not be freed by the caller.
*/
const gchar* const *
{
struct udev_list_entry *l;
GPtrArray *p;
goto out;
p = g_ptr_array_new ();
for (l = udev_device_get_properties_list_entry (device->priv->udevice); l != NULL; l = udev_list_entry_get_next (l))
{
}
g_ptr_array_add (p, NULL);
out:
}
/**
* g_udev_device_has_property:
* @device: A #GUdevDevice.
* @key: Name of property.
*
* Check if a the property with the given key exists.
*
* Returns: %TRUE only if the value for @key exist.
*/
{
}
/**
* g_udev_device_get_property:
* @device: A #GUdevDevice.
* @key: Name of property.
*
* Look up the value for @key on @device.
*
* Returns: The value for @key or %NULL if @key doesn't exist on @device. Do not free this string, it is owned by @device.
*/
const gchar *
{
}
/**
* g_udev_device_get_property_as_int:
* @device: A #GUdevDevice.
* @key: Name of property.
*
* Look up the value for @key on @device and convert it to an integer
* using strtol().
*
* Returns: The value for @key or 0 if @key doesn't exist or
* isn't an integer.
*/
{
const gchar *s;
result = 0;
if (s == NULL)
goto out;
out:
return result;
}
/**
* g_udev_device_get_property_as_uint64:
* @device: A #GUdevDevice.
* @key: Name of property.
*
* Look up the value for @key on @device and convert it to an unsigned
* 64-bit integer using g_ascii_strtoull().
*
* Returns: The value for @key or 0 if @key doesn't exist or isn't a
* #guint64.
*/
{
const gchar *s;
result = 0;
if (s == NULL)
goto out;
out:
return result;
}
/**
* g_udev_device_get_property_as_double:
* @device: A #GUdevDevice.
* @key: Name of property.
*
* Look up the value for @key on @device and convert it to a double
* precision floating point number using strtod().
*
* Returns: The value for @key or 0.0 if @key doesn't exist or isn't a
* #gdouble.
*/
{
const gchar *s;
result = 0.0;
if (s == NULL)
goto out;
out:
return result;
}
/**
* g_udev_device_get_property_as_boolean:
* @device: A #GUdevDevice.
* @key: Name of property.
*
* Look up the value for @key on @device and convert it to an
* boolean. This is done by doing a case-insensitive string comparison
* on the string value against "1" and "true".
*
* Returns: The value for @key or %FALSE if @key doesn't exist or
* isn't a #gboolean.
*/
{
const gchar *s;
if (s == NULL)
goto out;
out:
return result;
}
static gchar **
split_at_whitespace (const gchar *s)
{
guint n;
guint m;
/* remove empty strings, thanks GLib */
{
{
n--;
}
}
return result;
}
/**
* g_udev_device_get_property_as_strv:
* @device: A #GUdevDevice.
* @key: Name of property.
*
* Look up the value for @key on @device and return the result of
* splitting it into non-empty tokens split at white space (only space
* (' '), form-feed ('\f'), newline ('\n'), carriage return ('\r'),
* horizontal tab ('\t'), and vertical tab ('\v') are considered; the
* locale is not taken into account).
*
* Returns: (transfer none) (array zero-terminated=1) (element-type utf8): The value of @key on @device split into tokens or %NULL if @key doesn't exist. This array is owned by @device and should not be freed by the caller.
*/
const gchar* const *
{
const gchar *s;
{
goto out;
}
if (s == NULL)
goto out;
result = split_at_whitespace (s);
goto out;
device->priv->prop_strvs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_strfreev);
out:
}
/* ---------------------------------------------------------------------------------------------------- */
/**
* g_udev_device_get_sysfs_attr:
* @device: A #GUdevDevice.
* @name: Name of the sysfs attribute.
*
* Look up the sysfs attribute with @name on @device.
*
* Returns: The value of the sysfs attribute or %NULL if there is no
* such attribute. Do not free this string, it is owned by @device.
*/
const gchar *
{
}
/**
* g_udev_device_get_sysfs_attr_as_int:
* @device: A #GUdevDevice.
* @name: Name of the sysfs attribute.
*
* Look up the sysfs attribute with @name on @device and convert it to an integer
* using strtol().
*
* Returns: The value of the sysfs attribute or 0 if there is no such
* attribute.
*/
{
const gchar *s;
result = 0;
if (s == NULL)
goto out;
out:
return result;
}
/**
* g_udev_device_get_sysfs_attr_as_uint64:
* @device: A #GUdevDevice.
* @name: Name of the sysfs attribute.
*
* Look up the sysfs attribute with @name on @device and convert it to an unsigned
* 64-bit integer using g_ascii_strtoull().
*
* Returns: The value of the sysfs attribute or 0 if there is no such
* attribute.
*/
{
const gchar *s;
result = 0;
if (s == NULL)
goto out;
out:
return result;
}
/**
* g_udev_device_get_sysfs_attr_as_double:
* @device: A #GUdevDevice.
* @name: Name of the sysfs attribute.
*
* Look up the sysfs attribute with @name on @device and convert it to a double
* precision floating point number using strtod().
*
* Returns: The value of the sysfs attribute or 0.0 if there is no such
* attribute.
*/
{
const gchar *s;
result = 0.0;
if (s == NULL)
goto out;
out:
return result;
}
/**
* g_udev_device_get_sysfs_attr_as_boolean:
* @device: A #GUdevDevice.
* @name: Name of the sysfs attribute.
*
* Look up the sysfs attribute with @name on @device and convert it to an
* boolean. This is done by doing a case-insensitive string comparison
* on the string value against "1" and "true".
*
* Returns: The value of the sysfs attribute or %FALSE if there is no such
* attribute.
*/
{
const gchar *s;
if (s == NULL)
goto out;
out:
return result;
}
/**
* g_udev_device_get_sysfs_attr_as_strv:
* @device: A #GUdevDevice.
* @name: Name of the sysfs attribute.
*
* Look up the sysfs attribute with @name on @device and return the result of
* splitting it into non-empty tokens split at white space (only space (' '),
* form-feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal
* tab ('\t'), and vertical tab ('\v') are considered; the locale is
* not taken into account).
*
* Returns: (transfer none) (array zero-terminated=1) (element-type utf8): The value of the sysfs attribute split into tokens or %NULL if there is no such attribute. This array is owned by @device and should not be freed by the caller.
*/
const gchar * const *
{
const gchar *s;
{
goto out;
}
if (s == NULL)
goto out;
result = split_at_whitespace (s);
goto out;
device->priv->sysfs_attr_strvs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_strfreev);
out:
}
/**
* g_udev_device_get_tags:
* @device: A #GUdevDevice.
*
* Gets all tags for @device.
*
* Returns: (transfer none) (array zero-terminated=1) (element-type utf8): A %NULL terminated string array of tags. This array is owned by @device and should not be freed by the caller.
*
* Since: 165
*/
const gchar* const *
{
struct udev_list_entry *l;
GPtrArray *p;
goto out;
p = g_ptr_array_new ();
for (l = udev_device_get_tags_list_entry (device->priv->udevice); l != NULL; l = udev_list_entry_get_next (l))
{
}
g_ptr_array_add (p, NULL);
out:
}
/**
* g_udev_device_get_is_initialized:
* @device: A #GUdevDevice.
*
* Gets whether @device has been initalized.
*
* Returns: Whether @device has been initialized.
*
* Since: 165
*/
{
}
/**
* g_udev_device_get_usec_since_initialized:
* @device: A #GUdevDevice.
*
* Gets number of micro-seconds since @device was initialized.
*
* This only works for devices with properties in the udev
* database. All other devices return 0.
*
* Returns: Number of micro-seconds since @device was initialized or 0 if unknown.
*
* Since: 165
*/
{
}