00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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 usage(int argc, char* argv[])
00054 {
00055 fprintf(stderr,
00056 "\n"
00057 "usage : %s --udi <udi> --key <key> [--hex] [--quiet] [--help]\n",
00058 argv[0]);
00059 fprintf(stderr,
00060 "\n"
00061 " --udi Unique Device Id\n"
00062 " --key Key of the property to get\n"
00063 " --hex Show integer values in hex (without leading 0x)\n"
00064 " --quiet Be quiet\n"
00065 " --help Show this information and exit\n"
00066 "\n"
00067 "This program retrieves a property from a device. If the property exist\n"
00068 "then it is printed on stdout and this program exits with exit code 0.\n"
00069 "On error, the program exits with an exit code different from 0\n"
00070 "\n");
00071 }
00072
00079 int main(int argc, char* argv[])
00080 {
00081 char* udi = NULL;
00082 char* key = NULL;
00083 int type;
00084 dbus_bool_t is_hex = FALSE;
00085 dbus_bool_t is_quiet = FALSE;
00086 char* str;
00087
00088 if( argc<=1 )
00089 {
00090 usage(argc, argv);
00091 return 1;
00092 }
00093
00094 while(1)
00095 {
00096 int c;
00097 int option_index = 0;
00098 const char* opt;
00099 static struct option long_options[] =
00100 {
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 {
00116 case 0:
00117 opt = long_options[option_index].name;
00118
00119 if( strcmp(opt, "help")==0 )
00120 {
00121 usage(argc, argv);
00122 return 0;
00123 }
00124 else if( strcmp(opt, "hex")==0 )
00125 {
00126 is_hex = TRUE;
00127 }
00128 else if( strcmp(opt, "quiet")==0 )
00129 {
00130 is_quiet = TRUE;
00131 }
00132 else if( strcmp(opt, "key")==0 )
00133 {
00134 key = strdup(optarg);
00135 }
00136 else if( strcmp(opt, "udi")==0 )
00137 {
00138 udi = strdup(optarg);
00139 }
00140 break;
00141
00142 default:
00143 usage(argc, argv);
00144 return 1;
00145 break;
00146 }
00147 }
00148
00149 if( !is_quiet )
00150 fprintf(stderr, "%s " PACKAGE_VERSION "\n", argv[0]);
00151
00152 if( !is_quiet )
00153 fprintf(stderr, "\n");
00154
00155 if( udi==NULL || key==NULL )
00156 {
00157 usage(argc, argv);
00158 return 1;
00159 }
00160
00161 if( hal_initialize(NULL, FALSE) )
00162 {
00163 fprintf(stderr, "error: hal_initialize failed\n");
00164 return 1;
00165 }
00166
00167 type = hal_device_get_property_type(udi, key);
00168 if( type==DBUS_TYPE_NIL )
00169 {
00170 return 1;
00171 }
00172
00173
00174 switch( type )
00175 {
00176 case DBUS_TYPE_STRING:
00177 str = hal_device_get_property_string(udi, key);
00178 if( !is_quiet )
00179 fprintf(stderr, "Type is string\n");
00180 fprintf(stdout, "%s", str);
00181 hal_free_string(str);
00182 break;
00183 case DBUS_TYPE_INT32:
00184 if( !is_quiet )
00185 fprintf(stderr, "Type is integer (shown in %s)\n",
00186 (is_hex ? "hexadecimal" : "decimal"));
00187 fprintf(stdout, (is_hex ? "%x" : "%d"),
00188 hal_device_get_property_int(udi, key));
00189 break;
00190 case DBUS_TYPE_DOUBLE:
00191 if( !is_quiet )
00192 fprintf(stderr, "Type is double\n");
00193 fprintf(stdout, "%f", hal_device_get_property_double(udi, key));
00194 break;
00195 case DBUS_TYPE_BOOLEAN:
00196 if( !is_quiet )
00197 fprintf(stderr, "Type is boolean\n");
00198 fprintf(stdout, "%s",
00199 hal_device_get_property_bool(udi, key) ? "true" : "false");
00200 break;
00201
00202 default:
00203 fprintf(stderr, "Unknown type %d='%c'\n", type, type);
00204 return 1;
00205 break;
00206 }
00207
00208 return 0;
00209 }
00210