12912Sartem /***************************************************************************
22912Sartem * CVSID: $Id$
32912Sartem *
42912Sartem * hal_get_property.c : Get property for a device
52912Sartem *
62912Sartem * Copyright (C) 2003 David Zeuthen, <david@fubar.dk>
72912Sartem *
82912Sartem * Licensed under the Academic Free License version 2.1
92912Sartem *
102912Sartem * This program is free software; you can redistribute it and/or modify
112912Sartem * it under the terms of the GNU General Public License as published by
122912Sartem * the Free Software Foundation; either version 2 of the License, or
132912Sartem * (at your option) any later version.
142912Sartem *
152912Sartem * This program is distributed in the hope that it will be useful,
162912Sartem * but WITHOUT ANY WARRANTY; without even the implied warranty of
172912Sartem * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
182912Sartem * GNU General Public License for more details.
192912Sartem *
202912Sartem * You should have received a copy of the GNU General Public License
212912Sartem * along with this program; if not, write to the Free Software
222912Sartem * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
232912Sartem *
242912Sartem **************************************************************************/
252912Sartem
262912Sartem
272912Sartem #ifdef HAVE_CONFIG_H
282912Sartem # include <config.h>
292912Sartem #endif
302912Sartem
312912Sartem #include <stdio.h>
322912Sartem #include <string.h>
332912Sartem #include <unistd.h>
342912Sartem #include <getopt.h>
352912Sartem
362912Sartem #include <libhal.h>
372912Sartem
382912Sartem /**
392912Sartem * @defgroup HalGetProperty Get HAL device property
402912Sartem * @ingroup HalMisc
412912Sartem *
422912Sartem * @brief A commandline tool getting a property of a device. Uses libhal
432912Sartem *
442912Sartem * @{
452912Sartem */
462912Sartem
472912Sartem static LibHalContext *hal_ctx;
482912Sartem
492912Sartem /** Print out program usage.
502912Sartem *
512912Sartem * @param argc Number of arguments given to program
522912Sartem * @param argv Arguments given to program
532912Sartem */
542912Sartem static void
usage(int argc,char * argv[])552912Sartem usage (int argc, char *argv[])
562912Sartem {
572912Sartem fprintf (stderr,
582912Sartem "\n"
592912Sartem "usage : hal-get-property --udi <udi> --key <key> \n"
602912Sartem " [--hex] [--help] [--verbose] [--version]\n");
612912Sartem fprintf (stderr,
622912Sartem "\n"
632912Sartem " --udi Unique Device Id\n"
642912Sartem " --key Key of the property to get\n"
652912Sartem " --hex Show integer values in hex (without leading 0x)\n"
662912Sartem " --verbose Be verbose\n"
672912Sartem " --version Show version and exit\n"
682912Sartem " --help Show this information and exit\n"
692912Sartem "\n"
702912Sartem "This program retrieves a property from a device. If the property exist\n"
712912Sartem "then it is printed on stdout and this program exits with exit code 0.\n"
722912Sartem "On error, the program exits with an exit code different from 0\n"
732912Sartem "\n");
742912Sartem }
752912Sartem
762912Sartem /** Entry point
772912Sartem *
782912Sartem * @param argc Number of arguments given to program
792912Sartem * @param argv Arguments given to program
802912Sartem * @return Return code
812912Sartem */
822912Sartem int
main(int argc,char * argv[])832912Sartem main (int argc, char *argv[])
842912Sartem {
852912Sartem char *udi = NULL;
862912Sartem char *key = NULL;
872912Sartem int type;
882912Sartem dbus_bool_t is_hex = FALSE;
892912Sartem dbus_bool_t is_verbose = FALSE;
902912Sartem dbus_bool_t is_version = FALSE;
91*9823SLin.Guo@Sun.COM dbus_bool_t udi_exists = FALSE;
922912Sartem char *str;
932912Sartem DBusError error;
942912Sartem
952912Sartem if (argc <= 1) {
962912Sartem usage (argc, argv);
972912Sartem return 1;
982912Sartem }
992912Sartem
1002912Sartem while (1) {
1012912Sartem int c;
1022912Sartem int option_index = 0;
1032912Sartem const char *opt;
1042912Sartem static struct option long_options[] = {
1052912Sartem {"udi", 1, NULL, 0},
1062912Sartem {"key", 1, NULL, 0},
1072912Sartem {"hex", 0, NULL, 0},
1082912Sartem {"verbose", 0, NULL, 0},
1092912Sartem {"version", 0, NULL, 0},
1102912Sartem {"help", 0, NULL, 0},
1112912Sartem {NULL, 0, NULL, 0}
1122912Sartem };
1132912Sartem
1142912Sartem c = getopt_long (argc, argv, "",
1152912Sartem long_options, &option_index);
1162912Sartem if (c == -1)
1172912Sartem break;
1182912Sartem
1192912Sartem switch (c) {
1202912Sartem case 0:
1212912Sartem opt = long_options[option_index].name;
1222912Sartem
1232912Sartem if (strcmp (opt, "help") == 0) {
1242912Sartem usage (argc, argv);
1252912Sartem return 0;
1262912Sartem } else if (strcmp (opt, "hex") == 0) {
1272912Sartem is_hex = TRUE;
1282912Sartem } else if (strcmp (opt, "verbose") == 0) {
1292912Sartem is_verbose = TRUE;
1302912Sartem } else if (strcmp (opt, "version") == 0) {
1312912Sartem is_version = TRUE;
1322912Sartem } else if (strcmp (opt, "key") == 0) {
1332912Sartem key = strdup (optarg);
1342912Sartem } else if (strcmp (opt, "udi") == 0) {
1352912Sartem udi = strdup (optarg);
1362912Sartem }
1372912Sartem break;
1382912Sartem
1392912Sartem default:
1402912Sartem usage (argc, argv);
1412912Sartem return 1;
1422912Sartem break;
1432912Sartem }
1442912Sartem }
1452912Sartem
1462912Sartem if (is_version) {
1472912Sartem printf ("hal-get-property " PACKAGE_VERSION "\n");
1482912Sartem return 0;
1492912Sartem }
1502912Sartem
1512912Sartem if (udi == NULL || key == NULL) {
1522912Sartem usage (argc, argv);
1532912Sartem return 1;
1542912Sartem }
1552912Sartem
1562912Sartem dbus_error_init (&error);
1572912Sartem if ((hal_ctx = libhal_ctx_new ()) == NULL) {
1582912Sartem fprintf (stderr, "error: libhal_ctx_new\n");
1592912Sartem return 1;
1602912Sartem }
1612912Sartem if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) {
1622912Sartem fprintf (stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error.name, error.message);
1632912Sartem LIBHAL_FREE_DBUS_ERROR (&error);
1642912Sartem return 1;
1652912Sartem }
1662912Sartem if (!libhal_ctx_init (hal_ctx, &error)) {
1672912Sartem if (dbus_error_is_set(&error)) {
1682912Sartem fprintf (stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message);
169*9823SLin.Guo@Sun.COM dbus_error_free (&error);
1702912Sartem }
1712912Sartem fprintf (stderr, "Could not initialise connection to hald.\n"
1722912Sartem "Normally this means the HAL daemon (hald) is not running or not ready.\n");
1732912Sartem return 1;
1742912Sartem }
1752912Sartem
176*9823SLin.Guo@Sun.COM /* check UDI exists */
177*9823SLin.Guo@Sun.COM udi_exists = libhal_device_exists (hal_ctx, udi, &error);
178*9823SLin.Guo@Sun.COM if (!udi_exists) {
179*9823SLin.Guo@Sun.COM fprintf (stderr, "error: UDI %s does not exist\n", udi);
180*9823SLin.Guo@Sun.COM return 1;
181*9823SLin.Guo@Sun.COM }
182*9823SLin.Guo@Sun.COM if (dbus_error_is_set(&error)) {
183*9823SLin.Guo@Sun.COM fprintf (stderr, "error: libhal_device_exists: %s: %s\n", error.name, error.message);
184*9823SLin.Guo@Sun.COM dbus_error_free (&error);
185*9823SLin.Guo@Sun.COM return 1;
186*9823SLin.Guo@Sun.COM }
187*9823SLin.Guo@Sun.COM
188*9823SLin.Guo@Sun.COM
1892912Sartem type = libhal_device_get_property_type (hal_ctx, udi, key, &error);
1902912Sartem if (type == LIBHAL_PROPERTY_TYPE_INVALID) {
191*9823SLin.Guo@Sun.COM if (dbus_error_is_set(&error)) {
192*9823SLin.Guo@Sun.COM fprintf (stderr, "error: libhal_device_get_property_type: %s: %s\n", error.name, error.message);
193*9823SLin.Guo@Sun.COM dbus_error_free (&error);
194*9823SLin.Guo@Sun.COM } else {
195*9823SLin.Guo@Sun.COM fprintf (stderr, "error: libhal_device_get_property_type: invalid params.\n");
196*9823SLin.Guo@Sun.COM }
1972912Sartem return 1;
1982912Sartem }
1992912Sartem /* emit the value to stdout */
2002912Sartem switch (type) {
2012912Sartem case LIBHAL_PROPERTY_TYPE_STRING:
2022912Sartem str = libhal_device_get_property_string (hal_ctx, udi, key, &error);
2032912Sartem if (is_verbose)
2042912Sartem printf ("Type is string\n");
2052912Sartem printf ("%s\n", str);
2062912Sartem libhal_free_string (str);
2072912Sartem break;
2082912Sartem case LIBHAL_PROPERTY_TYPE_INT32:
2092912Sartem if (is_verbose)
2102912Sartem printf ("Type is integer (shown in %s)\n",
2112912Sartem (is_hex ? "hexadecimal" : "decimal"));
2122912Sartem printf ((is_hex ? "%x\n" : "%d\n"),
2132912Sartem libhal_device_get_property_int (hal_ctx, udi, key, &error));
2142912Sartem break;
2152912Sartem case LIBHAL_PROPERTY_TYPE_UINT64:
2162912Sartem if (is_verbose)
2172912Sartem printf ("Type is uint64 (shown in %s)\n",
2182912Sartem (is_hex ? "hexadecimal" : "decimal"));
2192912Sartem printf ((is_hex ? "%llx\n" : "%llu\n"),
2202912Sartem (long long unsigned int) libhal_device_get_property_uint64 (hal_ctx, udi, key, &error));
2212912Sartem break;
2222912Sartem case LIBHAL_PROPERTY_TYPE_DOUBLE:
2232912Sartem if (is_verbose)
2242912Sartem printf ("Type is double\n");
2252912Sartem printf ("%f\n",
2262912Sartem libhal_device_get_property_double (hal_ctx, udi, key, &error));
2272912Sartem break;
2282912Sartem case LIBHAL_PROPERTY_TYPE_BOOLEAN:
2292912Sartem if (is_verbose)
2302912Sartem printf ("Type is boolean\n");
2312912Sartem printf ("%s\n",
2322912Sartem libhal_device_get_property_bool (hal_ctx, udi, key, &error) ? "true" : "false");
2332912Sartem break;
2342912Sartem
2352912Sartem case LIBHAL_PROPERTY_TYPE_STRLIST:
2362912Sartem {
2372912Sartem unsigned int i;
2382912Sartem char **strlist;
2392912Sartem
2402912Sartem if ((strlist = libhal_device_get_property_strlist (hal_ctx, udi, key, &error)) != NULL) {
2412912Sartem
2422912Sartem for (i = 0; strlist[i] != 0; i++) {
2432912Sartem printf ("%s", strlist[i]);
2442912Sartem if (strlist[i+1] != NULL)
2452912Sartem printf (" ");
2462912Sartem }
2472912Sartem }
2482912Sartem break;
2492912Sartem }
2502912Sartem default:
2512912Sartem printf ("Unknown type %d='%c'\n", type, type);
2522912Sartem return 1;
2532912Sartem break;
2542912Sartem }
2552912Sartem
2562912Sartem if (dbus_error_is_set (&error)) {
2572912Sartem fprintf (stderr, "error: %s: %s\n", error.name, error.message);
2582912Sartem dbus_error_free (&error);
2592912Sartem return 1;
2602912Sartem }
2612912Sartem
2622912Sartem
2632912Sartem return 0;
2642912Sartem }
2652912Sartem
2662912Sartem /**
2672912Sartem * @}
2682912Sartem */
269