Main Page | Modules | Data Structures | File List | Data Fields | Related Pages

hal_get_property.c

00001 /***************************************************************************
00002  * CVSID: $Id: hal_get_property.c,v 1.4 2004/03/03 17:56:56 david Exp $
00003  *
00004  * hal_get_property.c : Get property for a device
00005  *
00006  * Copyright (C) 2003 David Zeuthen, <david@fubar.dk>
00007  *
00008  * Licensed under the Academic Free License version 2.0
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  **************************************************************************/
00025 
00026 
00027 #ifdef HAVE_CONFIG_H
00028 #  include <config.h>
00029 #endif
00030 
00031 #include <stdio.h>
00032 #include <string.h>
00033 #include <unistd.h>
00034 #include <getopt.h>
00035 
00036 #include <libhal/libhal.h>
00037 
00053 static void
00054 usage (int argc, char *argv[])
00055 {
00056     fprintf (stderr,
00057  "\n"
00058  "usage : %s --udi <udi> --key <key> [--hex] [--quiet] [--help]\n",
00059          argv[0]);
00060     fprintf (stderr,
00061  "\n"
00062  "        --udi            Unique Device Id\n"
00063  "        --key            Key of the property to get\n"
00064  "        --hex            Show integer values in hex (without leading 0x)\n"
00065  "        --quiet          Be quiet\n"
00066  "        --help           Show this information and exit\n"
00067  "\n"
00068  "This program retrieves a property from a device. If the property exist\n"
00069  "then it is printed on stdout and this program exits with exit code 0.\n"
00070  "On error, the program exits with an exit code different from 0\n"
00071  "\n");
00072 }
00073 
00080 int
00081 main (int argc, char *argv[])
00082 {
00083     char *udi = NULL;
00084     char *key = NULL;
00085     int type;
00086     dbus_bool_t is_hex = FALSE;
00087     dbus_bool_t is_quiet = FALSE;
00088     char *str;
00089 
00090     if (argc <= 1) {
00091         usage (argc, argv);
00092         return 1;
00093     }
00094 
00095     while (1) {
00096         int c;
00097         int option_index = 0;
00098         const char *opt;
00099         static struct option long_options[] = {
00100             {"udi", 1, NULL, 0},
00101             {"key", 1, NULL, 0},
00102             {"hex", 0, NULL, 0},
00103             {"quiet", 0, NULL, 0},
00104             {"help", 0, NULL, 0},
00105             {NULL, 0, NULL, 0}
00106         };
00107 
00108         c = getopt_long (argc, argv, "",
00109                  long_options, &option_index);
00110         if (c == -1)
00111             break;
00112 
00113         switch (c) {
00114         case 0:
00115             opt = long_options[option_index].name;
00116 
00117             if (strcmp (opt, "help") == 0) {
00118                 usage (argc, argv);
00119                 return 0;
00120             } else if (strcmp (opt, "hex") == 0) {
00121                 is_hex = TRUE;
00122             } else if (strcmp (opt, "quiet") == 0) {
00123                 is_quiet = TRUE;
00124             } else if (strcmp (opt, "key") == 0) {
00125                 key = strdup (optarg);
00126             } else if (strcmp (opt, "udi") == 0) {
00127                 udi = strdup (optarg);
00128             }
00129             break;
00130 
00131         default:
00132             usage (argc, argv);
00133             return 1;
00134             break;
00135         }
00136     }
00137 
00138     if (!is_quiet)
00139         fprintf (stderr, "%s " PACKAGE_VERSION "\n", argv[0]);
00140 
00141     if (!is_quiet)
00142         fprintf (stderr, "\n");
00143 
00144     if (udi == NULL || key == NULL) {
00145         usage (argc, argv);
00146         return 1;
00147     }
00148 
00149     if (hal_initialize (NULL, FALSE)) {
00150         fprintf (stderr, "error: hal_initialize failed\n");
00151         return 1;
00152     }
00153 
00154     type = hal_device_get_property_type (udi, key);
00155     if (type == DBUS_TYPE_NIL) {
00156         return 1;
00157     }
00158     // emit the value to stdout
00159     switch (type) {
00160     case DBUS_TYPE_STRING:
00161         str = hal_device_get_property_string (udi, key);
00162         if (!is_quiet)
00163             fprintf (stderr, "Type is string\n");
00164         fprintf (stdout, "%s", str);
00165         hal_free_string (str);
00166         break;
00167     case DBUS_TYPE_INT32:
00168         if (!is_quiet)
00169             fprintf (stderr, "Type is integer (shown in %s)\n",
00170                  (is_hex ? "hexadecimal" : "decimal"));
00171         fprintf (stdout, (is_hex ? "%x" : "%d"),
00172              hal_device_get_property_int (udi, key));
00173         break;
00174     case DBUS_TYPE_DOUBLE:
00175         if (!is_quiet)
00176             fprintf (stderr, "Type is double\n");
00177         fprintf (stdout, "%f",
00178              hal_device_get_property_double (udi, key));
00179         break;
00180     case DBUS_TYPE_BOOLEAN:
00181         if (!is_quiet)
00182             fprintf (stderr, "Type is boolean\n");
00183         fprintf (stdout, "%s",
00184              hal_device_get_property_bool (udi,
00185                                key) ? "true" :
00186              "false");
00187         break;
00188 
00189     default:
00190         fprintf (stderr, "Unknown type %d='%c'\n", type, type);
00191         return 1;
00192         break;
00193     }
00194 
00195     return 0;
00196 }
00197 

Generated on Thu Mar 11 21:32:22 2004 for HAL by doxygen 1.3.6-20040222