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.5 2004/04/10 16:51: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 
00047 static LibHalContext *hal_ctx;
00048 
00054 static void
00055 usage (int argc, char *argv[])
00056 {
00057     fprintf (stderr,
00058  "\n"
00059  "usage : %s --udi <udi> --key <key> [--hex] [--quiet] [--help]\n",
00060          argv[0]);
00061     fprintf (stderr,
00062  "\n"
00063  "        --udi            Unique Device Id\n"
00064  "        --key            Key of the property to get\n"
00065  "        --hex            Show integer values in hex (without leading 0x)\n"
00066  "        --quiet          Be quiet\n"
00067  "        --help           Show this information and exit\n"
00068  "\n"
00069  "This program retrieves a property from a device. If the property exist\n"
00070  "then it is printed on stdout and this program exits with exit code 0.\n"
00071  "On error, the program exits with an exit code different from 0\n"
00072  "\n");
00073 }
00074 
00081 int
00082 main (int argc, char *argv[])
00083 {
00084     char *udi = NULL;
00085     char *key = NULL;
00086     int type;
00087     dbus_bool_t is_hex = FALSE;
00088     dbus_bool_t is_quiet = FALSE;
00089     char *str;
00090 
00091     if (argc <= 1) {
00092         usage (argc, argv);
00093         return 1;
00094     }
00095 
00096     while (1) {
00097         int c;
00098         int option_index = 0;
00099         const char *opt;
00100         static struct option long_options[] = {
00101             {"udi", 1, NULL, 0},
00102             {"key", 1, NULL, 0},
00103             {"hex", 0, NULL, 0},
00104             {"quiet", 0, NULL, 0},
00105             {"help", 0, NULL, 0},
00106             {NULL, 0, NULL, 0}
00107         };
00108 
00109         c = getopt_long (argc, argv, "",
00110                  long_options, &option_index);
00111         if (c == -1)
00112             break;
00113 
00114         switch (c) {
00115         case 0:
00116             opt = long_options[option_index].name;
00117 
00118             if (strcmp (opt, "help") == 0) {
00119                 usage (argc, argv);
00120                 return 0;
00121             } else if (strcmp (opt, "hex") == 0) {
00122                 is_hex = TRUE;
00123             } else if (strcmp (opt, "quiet") == 0) {
00124                 is_quiet = TRUE;
00125             } else if (strcmp (opt, "key") == 0) {
00126                 key = strdup (optarg);
00127             } else if (strcmp (opt, "udi") == 0) {
00128                 udi = strdup (optarg);
00129             }
00130             break;
00131 
00132         default:
00133             usage (argc, argv);
00134             return 1;
00135             break;
00136         }
00137     }
00138 
00139     if (!is_quiet)
00140         fprintf (stderr, "%s " PACKAGE_VERSION "\n", argv[0]);
00141 
00142     if (!is_quiet)
00143         fprintf (stderr, "\n");
00144 
00145     if (udi == NULL || key == NULL) {
00146         usage (argc, argv);
00147         return 1;
00148     }
00149 
00150     if ((hal_ctx = hal_initialize (NULL, FALSE)) == NULL) {
00151         fprintf (stderr, "error: hal_initialize failed\n");
00152         return 1;
00153     }
00154 
00155     type = hal_device_get_property_type (hal_ctx, udi, key);
00156     if (type == DBUS_TYPE_NIL) {
00157         return 1;
00158     }
00159     // emit the value to stdout
00160     switch (type) {
00161     case DBUS_TYPE_STRING:
00162         str = hal_device_get_property_string (hal_ctx, udi, key);
00163         if (!is_quiet)
00164             fprintf (stderr, "Type is string\n");
00165         fprintf (stdout, "%s", str);
00166         hal_free_string (str);
00167         break;
00168     case DBUS_TYPE_INT32:
00169         if (!is_quiet)
00170             fprintf (stderr, "Type is integer (shown in %s)\n",
00171                  (is_hex ? "hexadecimal" : "decimal"));
00172         fprintf (stdout, (is_hex ? "%x" : "%d"),
00173              hal_device_get_property_int (hal_ctx, udi, key));
00174         break;
00175     case DBUS_TYPE_DOUBLE:
00176         if (!is_quiet)
00177             fprintf (stderr, "Type is double\n");
00178         fprintf (stdout, "%f",
00179              hal_device_get_property_double (hal_ctx, udi, key));
00180         break;
00181     case DBUS_TYPE_BOOLEAN:
00182         if (!is_quiet)
00183             fprintf (stderr, "Type is boolean\n");
00184         fprintf (stdout, "%s",
00185              hal_device_get_property_bool (hal_ctx, udi,
00186                                key) ? "true" :
00187              "false");
00188         break;
00189 
00190     default:
00191         fprintf (stderr, "Unknown type %d='%c'\n", type, type);
00192         return 1;
00193         break;
00194     }
00195 
00196     return 0;
00197 }
00198 

Generated on Sat Apr 24 19:57:44 2004 for HAL by doxygen 1.3.6-20040222