199767f8919635c4928607450d9e0abb932109ceToomas Soome/***************************************************************************
199767f8919635c4928607450d9e0abb932109ceToomas Soome * CVSID: $Id$
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * hal_get_property.c : Get property for a device
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (C) 2003 David Zeuthen, <david@fubar.dk>
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Licensed under the Academic Free License version 2.1
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This program is free software; you can redistribute it and/or modify
199767f8919635c4928607450d9e0abb932109ceToomas Soome * it under the terms of the GNU General Public License as published by
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the Free Software Foundation; either version 2 of the License, or
199767f8919635c4928607450d9e0abb932109ceToomas Soome * (at your option) any later version.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This program is distributed in the hope that it will be useful,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * but WITHOUT ANY WARRANTY; without even the implied warranty of
199767f8919635c4928607450d9e0abb932109ceToomas Soome * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * GNU General Public License for more details.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * You should have received a copy of the GNU General Public License
199767f8919635c4928607450d9e0abb932109ceToomas Soome * along with this program; if not, write to the Free Software
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome **************************************************************************/
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef HAVE_CONFIG_H
199767f8919635c4928607450d9e0abb932109ceToomas Soome# include <config.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stdio.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <string.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <unistd.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <getopt.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <libhal.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/**
199767f8919635c4928607450d9e0abb932109ceToomas Soome * @defgroup HalGetProperty Get HAL device property
199767f8919635c4928607450d9e0abb932109ceToomas Soome * @ingroup HalMisc
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * @brief A commandline tool getting a property of a device. Uses libhal
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * @{
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic LibHalContext *hal_ctx;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/** Print out program usage.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * @param argc Number of arguments given to program
199767f8919635c4928607450d9e0abb932109ceToomas Soome * @param argv Arguments given to program
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomeusage (int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf (stderr,
199767f8919635c4928607450d9e0abb932109ceToomas Soome "\n"
199767f8919635c4928607450d9e0abb932109ceToomas Soome "usage : hal-get-property --udi <udi> --key <key> \n"
199767f8919635c4928607450d9e0abb932109ceToomas Soome " [--hex] [--help] [--verbose] [--version]\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf (stderr,
199767f8919635c4928607450d9e0abb932109ceToomas Soome "\n"
199767f8919635c4928607450d9e0abb932109ceToomas Soome " --udi Unique Device Id\n"
199767f8919635c4928607450d9e0abb932109ceToomas Soome " --key Key of the property to get\n"
199767f8919635c4928607450d9e0abb932109ceToomas Soome " --hex Show integer values in hex (without leading 0x)\n"
199767f8919635c4928607450d9e0abb932109ceToomas Soome " --verbose Be verbose\n"
199767f8919635c4928607450d9e0abb932109ceToomas Soome " --version Show version and exit\n"
199767f8919635c4928607450d9e0abb932109ceToomas Soome " --help Show this information and exit\n"
199767f8919635c4928607450d9e0abb932109ceToomas Soome "\n"
199767f8919635c4928607450d9e0abb932109ceToomas Soome "This program retrieves a property from a device. If the property exist\n"
199767f8919635c4928607450d9e0abb932109ceToomas Soome "then it is printed on stdout and this program exits with exit code 0.\n"
199767f8919635c4928607450d9e0abb932109ceToomas Soome "On error, the program exits with an exit code different from 0\n"
199767f8919635c4928607450d9e0abb932109ceToomas Soome "\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/** Entry point
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * @param argc Number of arguments given to program
199767f8919635c4928607450d9e0abb932109ceToomas Soome * @param argv Arguments given to program
199767f8919635c4928607450d9e0abb932109ceToomas Soome * @return Return code
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
int
main (int argc, char *argv[])
{
char *udi = NULL;
char *key = NULL;
int type;
dbus_bool_t is_hex = FALSE;
dbus_bool_t is_verbose = FALSE;
dbus_bool_t is_version = FALSE;
dbus_bool_t udi_exists = FALSE;
char *str;
DBusError error;
if (argc <= 1) {
usage (argc, argv);
return 1;
}
while (1) {
int c;
int option_index = 0;
const char *opt;
static struct option long_options[] = {
{"udi", 1, NULL, 0},
{"key", 1, NULL, 0},
{"hex", 0, NULL, 0},
{"verbose", 0, NULL, 0},
{"version", 0, NULL, 0},
{"help", 0, NULL, 0},
{NULL, 0, NULL, 0}
};
c = getopt_long (argc, argv, "",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0:
opt = long_options[option_index].name;
if (strcmp (opt, "help") == 0) {
usage (argc, argv);
return 0;
} else if (strcmp (opt, "hex") == 0) {
is_hex = TRUE;
} else if (strcmp (opt, "verbose") == 0) {
is_verbose = TRUE;
} else if (strcmp (opt, "version") == 0) {
is_version = TRUE;
} else if (strcmp (opt, "key") == 0) {
key = strdup (optarg);
} else if (strcmp (opt, "udi") == 0) {
udi = strdup (optarg);
}
break;
default:
usage (argc, argv);
return 1;
break;
}
}
if (is_version) {
printf ("hal-get-property " PACKAGE_VERSION "\n");
return 0;
}
if (udi == NULL || key == NULL) {
usage (argc, argv);
return 1;
}
dbus_error_init (&error);
if ((hal_ctx = libhal_ctx_new ()) == NULL) {
fprintf (stderr, "error: libhal_ctx_new\n");
return 1;
}
if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) {
fprintf (stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error.name, error.message);
LIBHAL_FREE_DBUS_ERROR (&error);
return 1;
}
if (!libhal_ctx_init (hal_ctx, &error)) {
if (dbus_error_is_set(&error)) {
fprintf (stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message);
dbus_error_free (&error);
}
fprintf (stderr, "Could not initialise connection to hald.\n"
"Normally this means the HAL daemon (hald) is not running or not ready.\n");
return 1;
}
/* check UDI exists */
udi_exists = libhal_device_exists (hal_ctx, udi, &error);
if (!udi_exists) {
fprintf (stderr, "error: UDI %s does not exist\n", udi);
return 1;
}
if (dbus_error_is_set(&error)) {
fprintf (stderr, "error: libhal_device_exists: %s: %s\n", error.name, error.message);
dbus_error_free (&error);
return 1;
}
type = libhal_device_get_property_type (hal_ctx, udi, key, &error);
if (type == LIBHAL_PROPERTY_TYPE_INVALID) {
if (dbus_error_is_set(&error)) {
fprintf (stderr, "error: libhal_device_get_property_type: %s: %s\n", error.name, error.message);
dbus_error_free (&error);
} else {
fprintf (stderr, "error: libhal_device_get_property_type: invalid params.\n");
}
return 1;
}
/* emit the value to stdout */
switch (type) {
case LIBHAL_PROPERTY_TYPE_STRING:
str = libhal_device_get_property_string (hal_ctx, udi, key, &error);
if (is_verbose)
printf ("Type is string\n");
printf ("%s\n", str);
libhal_free_string (str);
break;
case LIBHAL_PROPERTY_TYPE_INT32:
if (is_verbose)
printf ("Type is integer (shown in %s)\n",
(is_hex ? "hexadecimal" : "decimal"));
printf ((is_hex ? "%x\n" : "%d\n"),
libhal_device_get_property_int (hal_ctx, udi, key, &error));
break;
case LIBHAL_PROPERTY_TYPE_UINT64:
if (is_verbose)
printf ("Type is uint64 (shown in %s)\n",
(is_hex ? "hexadecimal" : "decimal"));
printf ((is_hex ? "%llx\n" : "%llu\n"),
(long long unsigned int) libhal_device_get_property_uint64 (hal_ctx, udi, key, &error));
break;
case LIBHAL_PROPERTY_TYPE_DOUBLE:
if (is_verbose)
printf ("Type is double\n");
printf ("%f\n",
libhal_device_get_property_double (hal_ctx, udi, key, &error));
break;
case LIBHAL_PROPERTY_TYPE_BOOLEAN:
if (is_verbose)
printf ("Type is boolean\n");
printf ("%s\n",
libhal_device_get_property_bool (hal_ctx, udi, key, &error) ? "true" : "false");
break;
case LIBHAL_PROPERTY_TYPE_STRLIST:
{
unsigned int i;
char **strlist;
if ((strlist = libhal_device_get_property_strlist (hal_ctx, udi, key, &error)) != NULL) {
for (i = 0; strlist[i] != 0; i++) {
printf ("%s", strlist[i]);
if (strlist[i+1] != NULL)
printf (" ");
}
}
break;
}
default:
printf ("Unknown type %d='%c'\n", type, type);
return 1;
break;
}
if (dbus_error_is_set (&error)) {
fprintf (stderr, "error: %s: %s\n", error.name, error.message);
dbus_error_free (&error);
return 1;
}
return 0;
}
/**
* @}
*/