1 /***************************************************************************
4 * hal_get_property.c : Get property for a device
6 * Copyright (C) 2003 David Zeuthen, <david@fubar.dk>
8 * Licensed under the Academic Free License version 2.1
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 **************************************************************************/
39 * @defgroup HalGetProperty Get HAL device property
42 * @brief A commandline tool getting a property of a device. Uses libhal
47 static LibHalContext
*hal_ctx
;
49 /** Print out program usage.
51 * @param argc Number of arguments given to program
52 * @param argv Arguments given to program
55 usage (int argc
, char *argv
[])
59 "usage : hal-get-property --udi <udi> --key <key> \n"
60 " [--hex] [--help] [--verbose] [--version]\n");
63 " --udi Unique Device Id\n"
64 " --key Key of the property to get\n"
65 " --hex Show integer values in hex (without leading 0x)\n"
66 " --verbose Be verbose\n"
67 " --version Show version and exit\n"
68 " --help Show this information and exit\n"
70 "This program retrieves a property from a device. If the property exist\n"
71 "then it is printed on stdout and this program exits with exit code 0.\n"
72 "On error, the program exits with an exit code different from 0\n"
78 * @param argc Number of arguments given to program
79 * @param argv Arguments given to program
83 main (int argc
, char *argv
[])
88 dbus_bool_t is_hex
= FALSE
;
89 dbus_bool_t is_verbose
= FALSE
;
90 dbus_bool_t is_version
= FALSE
;
91 dbus_bool_t udi_exists
= FALSE
;
102 int option_index
= 0;
104 static struct option long_options
[] = {
108 {"verbose", 0, NULL
, 0},
109 {"version", 0, NULL
, 0},
110 {"help", 0, NULL
, 0},
114 c
= getopt_long (argc
, argv
, "",
115 long_options
, &option_index
);
121 opt
= long_options
[option_index
].name
;
123 if (strcmp (opt
, "help") == 0) {
126 } else if (strcmp (opt
, "hex") == 0) {
128 } else if (strcmp (opt
, "verbose") == 0) {
130 } else if (strcmp (opt
, "version") == 0) {
132 } else if (strcmp (opt
, "key") == 0) {
133 key
= strdup (optarg
);
134 } else if (strcmp (opt
, "udi") == 0) {
135 udi
= strdup (optarg
);
147 printf ("hal-get-property " PACKAGE_VERSION
"\n");
151 if (udi
== NULL
|| key
== NULL
) {
156 dbus_error_init (&error
);
157 if ((hal_ctx
= libhal_ctx_new ()) == NULL
) {
158 fprintf (stderr
, "error: libhal_ctx_new\n");
161 if (!libhal_ctx_set_dbus_connection (hal_ctx
, dbus_bus_get (DBUS_BUS_SYSTEM
, &error
))) {
162 fprintf (stderr
, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error
.name
, error
.message
);
163 LIBHAL_FREE_DBUS_ERROR (&error
);
166 if (!libhal_ctx_init (hal_ctx
, &error
)) {
167 if (dbus_error_is_set(&error
)) {
168 fprintf (stderr
, "error: libhal_ctx_init: %s: %s\n", error
.name
, error
.message
);
169 dbus_error_free (&error
);
171 fprintf (stderr
, "Could not initialise connection to hald.\n"
172 "Normally this means the HAL daemon (hald) is not running or not ready.\n");
176 /* check UDI exists */
177 udi_exists
= libhal_device_exists (hal_ctx
, udi
, &error
);
179 fprintf (stderr
, "error: UDI %s does not exist\n", udi
);
182 if (dbus_error_is_set(&error
)) {
183 fprintf (stderr
, "error: libhal_device_exists: %s: %s\n", error
.name
, error
.message
);
184 dbus_error_free (&error
);
189 type
= libhal_device_get_property_type (hal_ctx
, udi
, key
, &error
);
190 if (type
== LIBHAL_PROPERTY_TYPE_INVALID
) {
191 if (dbus_error_is_set(&error
)) {
192 fprintf (stderr
, "error: libhal_device_get_property_type: %s: %s\n", error
.name
, error
.message
);
193 dbus_error_free (&error
);
195 fprintf (stderr
, "error: libhal_device_get_property_type: invalid params.\n");
199 /* emit the value to stdout */
201 case LIBHAL_PROPERTY_TYPE_STRING
:
202 str
= libhal_device_get_property_string (hal_ctx
, udi
, key
, &error
);
204 printf ("Type is string\n");
205 printf ("%s\n", str
);
206 libhal_free_string (str
);
208 case LIBHAL_PROPERTY_TYPE_INT32
:
210 printf ("Type is integer (shown in %s)\n",
211 (is_hex
? "hexadecimal" : "decimal"));
212 printf ((is_hex
? "%x\n" : "%d\n"),
213 libhal_device_get_property_int (hal_ctx
, udi
, key
, &error
));
215 case LIBHAL_PROPERTY_TYPE_UINT64
:
217 printf ("Type is uint64 (shown in %s)\n",
218 (is_hex
? "hexadecimal" : "decimal"));
219 printf ((is_hex
? "%llx\n" : "%llu\n"),
220 (long long unsigned int) libhal_device_get_property_uint64 (hal_ctx
, udi
, key
, &error
));
222 case LIBHAL_PROPERTY_TYPE_DOUBLE
:
224 printf ("Type is double\n");
226 libhal_device_get_property_double (hal_ctx
, udi
, key
, &error
));
228 case LIBHAL_PROPERTY_TYPE_BOOLEAN
:
230 printf ("Type is boolean\n");
232 libhal_device_get_property_bool (hal_ctx
, udi
, key
, &error
) ? "true" : "false");
235 case LIBHAL_PROPERTY_TYPE_STRLIST
:
240 if ((strlist
= libhal_device_get_property_strlist (hal_ctx
, udi
, key
, &error
)) != NULL
) {
242 for (i
= 0; strlist
[i
] != 0; i
++) {
243 printf ("%s", strlist
[i
]);
244 if (strlist
[i
+1] != NULL
)
251 printf ("Unknown type %d='%c'\n", type
, type
);
256 if (dbus_error_is_set (&error
)) {
257 fprintf (stderr
, "error: %s: %s\n", error
.name
, error
.message
);
258 dbus_error_free (&error
);