1*2912Sartem /*************************************************************************** 2*2912Sartem * CVSID: $Id$ 3*2912Sartem * 4*2912Sartem * hal_get_property.c : Get property for a device 5*2912Sartem * 6*2912Sartem * Copyright (C) 2003 David Zeuthen, <david@fubar.dk> 7*2912Sartem * 8*2912Sartem * Licensed under the Academic Free License version 2.1 9*2912Sartem * 10*2912Sartem * This program is free software; you can redistribute it and/or modify 11*2912Sartem * it under the terms of the GNU General Public License as published by 12*2912Sartem * the Free Software Foundation; either version 2 of the License, or 13*2912Sartem * (at your option) any later version. 14*2912Sartem * 15*2912Sartem * This program is distributed in the hope that it will be useful, 16*2912Sartem * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*2912Sartem * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*2912Sartem * GNU General Public License for more details. 19*2912Sartem * 20*2912Sartem * You should have received a copy of the GNU General Public License 21*2912Sartem * along with this program; if not, write to the Free Software 22*2912Sartem * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 23*2912Sartem * 24*2912Sartem **************************************************************************/ 25*2912Sartem 26*2912Sartem 27*2912Sartem #ifdef HAVE_CONFIG_H 28*2912Sartem # include <config.h> 29*2912Sartem #endif 30*2912Sartem 31*2912Sartem #include <stdio.h> 32*2912Sartem #include <string.h> 33*2912Sartem #include <unistd.h> 34*2912Sartem #include <getopt.h> 35*2912Sartem 36*2912Sartem #include <libhal.h> 37*2912Sartem 38*2912Sartem /** 39*2912Sartem * @defgroup HalGetProperty Get HAL device property 40*2912Sartem * @ingroup HalMisc 41*2912Sartem * 42*2912Sartem * @brief A commandline tool getting a property of a device. Uses libhal 43*2912Sartem * 44*2912Sartem * @{ 45*2912Sartem */ 46*2912Sartem 47*2912Sartem static LibHalContext *hal_ctx; 48*2912Sartem 49*2912Sartem /** Print out program usage. 50*2912Sartem * 51*2912Sartem * @param argc Number of arguments given to program 52*2912Sartem * @param argv Arguments given to program 53*2912Sartem */ 54*2912Sartem static void 55*2912Sartem usage (int argc, char *argv[]) 56*2912Sartem { 57*2912Sartem fprintf (stderr, 58*2912Sartem "\n" 59*2912Sartem "usage : hal-get-property --udi <udi> --key <key> \n" 60*2912Sartem " [--hex] [--help] [--verbose] [--version]\n"); 61*2912Sartem fprintf (stderr, 62*2912Sartem "\n" 63*2912Sartem " --udi Unique Device Id\n" 64*2912Sartem " --key Key of the property to get\n" 65*2912Sartem " --hex Show integer values in hex (without leading 0x)\n" 66*2912Sartem " --verbose Be verbose\n" 67*2912Sartem " --version Show version and exit\n" 68*2912Sartem " --help Show this information and exit\n" 69*2912Sartem "\n" 70*2912Sartem "This program retrieves a property from a device. If the property exist\n" 71*2912Sartem "then it is printed on stdout and this program exits with exit code 0.\n" 72*2912Sartem "On error, the program exits with an exit code different from 0\n" 73*2912Sartem "\n"); 74*2912Sartem } 75*2912Sartem 76*2912Sartem /** Entry point 77*2912Sartem * 78*2912Sartem * @param argc Number of arguments given to program 79*2912Sartem * @param argv Arguments given to program 80*2912Sartem * @return Return code 81*2912Sartem */ 82*2912Sartem int 83*2912Sartem main (int argc, char *argv[]) 84*2912Sartem { 85*2912Sartem char *udi = NULL; 86*2912Sartem char *key = NULL; 87*2912Sartem int type; 88*2912Sartem dbus_bool_t is_hex = FALSE; 89*2912Sartem dbus_bool_t is_verbose = FALSE; 90*2912Sartem dbus_bool_t is_version = FALSE; 91*2912Sartem char *str; 92*2912Sartem DBusError error; 93*2912Sartem 94*2912Sartem if (argc <= 1) { 95*2912Sartem usage (argc, argv); 96*2912Sartem return 1; 97*2912Sartem } 98*2912Sartem 99*2912Sartem while (1) { 100*2912Sartem int c; 101*2912Sartem int option_index = 0; 102*2912Sartem const char *opt; 103*2912Sartem static struct option long_options[] = { 104*2912Sartem {"udi", 1, NULL, 0}, 105*2912Sartem {"key", 1, NULL, 0}, 106*2912Sartem {"hex", 0, NULL, 0}, 107*2912Sartem {"verbose", 0, NULL, 0}, 108*2912Sartem {"version", 0, NULL, 0}, 109*2912Sartem {"help", 0, NULL, 0}, 110*2912Sartem {NULL, 0, NULL, 0} 111*2912Sartem }; 112*2912Sartem 113*2912Sartem c = getopt_long (argc, argv, "", 114*2912Sartem long_options, &option_index); 115*2912Sartem if (c == -1) 116*2912Sartem break; 117*2912Sartem 118*2912Sartem switch (c) { 119*2912Sartem case 0: 120*2912Sartem opt = long_options[option_index].name; 121*2912Sartem 122*2912Sartem if (strcmp (opt, "help") == 0) { 123*2912Sartem usage (argc, argv); 124*2912Sartem return 0; 125*2912Sartem } else if (strcmp (opt, "hex") == 0) { 126*2912Sartem is_hex = TRUE; 127*2912Sartem } else if (strcmp (opt, "verbose") == 0) { 128*2912Sartem is_verbose = TRUE; 129*2912Sartem } else if (strcmp (opt, "version") == 0) { 130*2912Sartem is_version = TRUE; 131*2912Sartem } else if (strcmp (opt, "key") == 0) { 132*2912Sartem key = strdup (optarg); 133*2912Sartem } else if (strcmp (opt, "udi") == 0) { 134*2912Sartem udi = strdup (optarg); 135*2912Sartem } 136*2912Sartem break; 137*2912Sartem 138*2912Sartem default: 139*2912Sartem usage (argc, argv); 140*2912Sartem return 1; 141*2912Sartem break; 142*2912Sartem } 143*2912Sartem } 144*2912Sartem 145*2912Sartem if (is_version) { 146*2912Sartem printf ("hal-get-property " PACKAGE_VERSION "\n"); 147*2912Sartem return 0; 148*2912Sartem } 149*2912Sartem 150*2912Sartem if (udi == NULL || key == NULL) { 151*2912Sartem usage (argc, argv); 152*2912Sartem return 1; 153*2912Sartem } 154*2912Sartem 155*2912Sartem dbus_error_init (&error); 156*2912Sartem if ((hal_ctx = libhal_ctx_new ()) == NULL) { 157*2912Sartem fprintf (stderr, "error: libhal_ctx_new\n"); 158*2912Sartem return 1; 159*2912Sartem } 160*2912Sartem if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) { 161*2912Sartem fprintf (stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error.name, error.message); 162*2912Sartem LIBHAL_FREE_DBUS_ERROR (&error); 163*2912Sartem return 1; 164*2912Sartem } 165*2912Sartem if (!libhal_ctx_init (hal_ctx, &error)) { 166*2912Sartem if (dbus_error_is_set(&error)) { 167*2912Sartem fprintf (stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message); 168*2912Sartem LIBHAL_FREE_DBUS_ERROR (&error); 169*2912Sartem } 170*2912Sartem fprintf (stderr, "Could not initialise connection to hald.\n" 171*2912Sartem "Normally this means the HAL daemon (hald) is not running or not ready.\n"); 172*2912Sartem return 1; 173*2912Sartem } 174*2912Sartem 175*2912Sartem type = libhal_device_get_property_type (hal_ctx, udi, key, &error); 176*2912Sartem if (type == LIBHAL_PROPERTY_TYPE_INVALID) { 177*2912Sartem fprintf (stderr, "error: libhal_device_get_property_type: %s: %s\n", error.name, error.message); 178*2912Sartem LIBHAL_FREE_DBUS_ERROR (&error); 179*2912Sartem return 1; 180*2912Sartem } 181*2912Sartem /* emit the value to stdout */ 182*2912Sartem switch (type) { 183*2912Sartem case LIBHAL_PROPERTY_TYPE_STRING: 184*2912Sartem str = libhal_device_get_property_string (hal_ctx, udi, key, &error); 185*2912Sartem if (is_verbose) 186*2912Sartem printf ("Type is string\n"); 187*2912Sartem printf ("%s\n", str); 188*2912Sartem libhal_free_string (str); 189*2912Sartem break; 190*2912Sartem case LIBHAL_PROPERTY_TYPE_INT32: 191*2912Sartem if (is_verbose) 192*2912Sartem printf ("Type is integer (shown in %s)\n", 193*2912Sartem (is_hex ? "hexadecimal" : "decimal")); 194*2912Sartem printf ((is_hex ? "%x\n" : "%d\n"), 195*2912Sartem libhal_device_get_property_int (hal_ctx, udi, key, &error)); 196*2912Sartem break; 197*2912Sartem case LIBHAL_PROPERTY_TYPE_UINT64: 198*2912Sartem if (is_verbose) 199*2912Sartem printf ("Type is uint64 (shown in %s)\n", 200*2912Sartem (is_hex ? "hexadecimal" : "decimal")); 201*2912Sartem printf ((is_hex ? "%llx\n" : "%llu\n"), 202*2912Sartem (long long unsigned int) libhal_device_get_property_uint64 (hal_ctx, udi, key, &error)); 203*2912Sartem break; 204*2912Sartem case LIBHAL_PROPERTY_TYPE_DOUBLE: 205*2912Sartem if (is_verbose) 206*2912Sartem printf ("Type is double\n"); 207*2912Sartem printf ("%f\n", 208*2912Sartem libhal_device_get_property_double (hal_ctx, udi, key, &error)); 209*2912Sartem break; 210*2912Sartem case LIBHAL_PROPERTY_TYPE_BOOLEAN: 211*2912Sartem if (is_verbose) 212*2912Sartem printf ("Type is boolean\n"); 213*2912Sartem printf ("%s\n", 214*2912Sartem libhal_device_get_property_bool (hal_ctx, udi, key, &error) ? "true" : "false"); 215*2912Sartem break; 216*2912Sartem 217*2912Sartem case LIBHAL_PROPERTY_TYPE_STRLIST: 218*2912Sartem { 219*2912Sartem unsigned int i; 220*2912Sartem char **strlist; 221*2912Sartem 222*2912Sartem if ((strlist = libhal_device_get_property_strlist (hal_ctx, udi, key, &error)) != NULL) { 223*2912Sartem 224*2912Sartem for (i = 0; strlist[i] != 0; i++) { 225*2912Sartem printf ("%s", strlist[i]); 226*2912Sartem if (strlist[i+1] != NULL) 227*2912Sartem printf (" "); 228*2912Sartem } 229*2912Sartem } 230*2912Sartem break; 231*2912Sartem } 232*2912Sartem default: 233*2912Sartem printf ("Unknown type %d='%c'\n", type, type); 234*2912Sartem return 1; 235*2912Sartem break; 236*2912Sartem } 237*2912Sartem 238*2912Sartem if (dbus_error_is_set (&error)) { 239*2912Sartem fprintf (stderr, "error: %s: %s\n", error.name, error.message); 240*2912Sartem dbus_error_free (&error); 241*2912Sartem return 1; 242*2912Sartem } 243*2912Sartem 244*2912Sartem 245*2912Sartem return 0; 246*2912Sartem } 247*2912Sartem 248*2912Sartem /** 249*2912Sartem * @} 250*2912Sartem */ 251