118c2aff7Sartem /***************************************************************************
218c2aff7Sartem  * CVSID: $Id$
318c2aff7Sartem  *
418c2aff7Sartem  * hal_get_property.c : Get property for a device
518c2aff7Sartem  *
618c2aff7Sartem  * Copyright (C) 2003 David Zeuthen, <david@fubar.dk>
718c2aff7Sartem  *
818c2aff7Sartem  * Licensed under the Academic Free License version 2.1
918c2aff7Sartem  *
1018c2aff7Sartem  * This program is free software; you can redistribute it and/or modify
1118c2aff7Sartem  * it under the terms of the GNU General Public License as published by
1218c2aff7Sartem  * the Free Software Foundation; either version 2 of the License, or
1318c2aff7Sartem  * (at your option) any later version.
1418c2aff7Sartem  *
1518c2aff7Sartem  * This program is distributed in the hope that it will be useful,
1618c2aff7Sartem  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1718c2aff7Sartem  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1818c2aff7Sartem  * GNU General Public License for more details.
1918c2aff7Sartem  *
2018c2aff7Sartem  * You should have received a copy of the GNU General Public License
2118c2aff7Sartem  * along with this program; if not, write to the Free Software
2218c2aff7Sartem  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2318c2aff7Sartem  *
2418c2aff7Sartem  **************************************************************************/
2518c2aff7Sartem 
2618c2aff7Sartem 
2718c2aff7Sartem #ifdef HAVE_CONFIG_H
2818c2aff7Sartem #  include <config.h>
2918c2aff7Sartem #endif
3018c2aff7Sartem 
3118c2aff7Sartem #include <stdio.h>
3218c2aff7Sartem #include <string.h>
3318c2aff7Sartem #include <unistd.h>
3418c2aff7Sartem #include <getopt.h>
3518c2aff7Sartem 
3618c2aff7Sartem #include <libhal.h>
3718c2aff7Sartem 
3818c2aff7Sartem /**
3918c2aff7Sartem  * @defgroup HalGetProperty  Get HAL device property
4018c2aff7Sartem  * @ingroup HalMisc
4118c2aff7Sartem  *
4218c2aff7Sartem  * @brief A commandline tool getting a property of a device. Uses libhal
4318c2aff7Sartem  *
4418c2aff7Sartem  * @{
4518c2aff7Sartem  */
4618c2aff7Sartem 
4718c2aff7Sartem static LibHalContext *hal_ctx;
4818c2aff7Sartem 
4918c2aff7Sartem /** Print out program usage.
5018c2aff7Sartem  *
5118c2aff7Sartem  *  @param  argc                Number of arguments given to program
5218c2aff7Sartem  *  @param  argv                Arguments given to program
5318c2aff7Sartem  */
5418c2aff7Sartem static void
usage(int argc,char * argv[])5518c2aff7Sartem usage (int argc, char *argv[])
5618c2aff7Sartem {
5718c2aff7Sartem 	fprintf (stderr,
5818c2aff7Sartem  "\n"
5918c2aff7Sartem  "usage : hal-get-property --udi <udi> --key <key> \n"
6018c2aff7Sartem  "                        [--hex] [--help] [--verbose] [--version]\n");
6118c2aff7Sartem 	fprintf (stderr,
6218c2aff7Sartem  "\n"
6318c2aff7Sartem  "        --udi            Unique Device Id\n"
6418c2aff7Sartem  "        --key            Key of the property to get\n"
6518c2aff7Sartem  "        --hex            Show integer values in hex (without leading 0x)\n"
6618c2aff7Sartem  "        --verbose        Be verbose\n"
6718c2aff7Sartem  "        --version        Show version and exit\n"
6818c2aff7Sartem  "        --help           Show this information and exit\n"
6918c2aff7Sartem  "\n"
7018c2aff7Sartem  "This program retrieves a property from a device. If the property exist\n"
7118c2aff7Sartem  "then it is printed on stdout and this program exits with exit code 0.\n"
7218c2aff7Sartem  "On error, the program exits with an exit code different from 0\n"
7318c2aff7Sartem  "\n");
7418c2aff7Sartem }
7518c2aff7Sartem 
7618c2aff7Sartem /** Entry point
7718c2aff7Sartem  *
7818c2aff7Sartem  *  @param  argc                Number of arguments given to program
7918c2aff7Sartem  *  @param  argv                Arguments given to program
8018c2aff7Sartem  *  @return                     Return code
8118c2aff7Sartem  */
8218c2aff7Sartem int
main(int argc,char * argv[])8318c2aff7Sartem main (int argc, char *argv[])
8418c2aff7Sartem {
8518c2aff7Sartem 	char *udi = NULL;
8618c2aff7Sartem 	char *key = NULL;
8718c2aff7Sartem 	int type;
8818c2aff7Sartem 	dbus_bool_t is_hex = FALSE;
8918c2aff7Sartem 	dbus_bool_t is_verbose = FALSE;
9018c2aff7Sartem 	dbus_bool_t is_version = FALSE;
91de7d23d8SLin Guo - Sun Microsystems 	dbus_bool_t udi_exists = FALSE;
9218c2aff7Sartem 	char *str;
9318c2aff7Sartem 	DBusError error;
9418c2aff7Sartem 
9518c2aff7Sartem 	if (argc <= 1) {
9618c2aff7Sartem 		usage (argc, argv);
9718c2aff7Sartem 		return 1;
9818c2aff7Sartem 	}
9918c2aff7Sartem 
10018c2aff7Sartem 	while (1) {
10118c2aff7Sartem 		int c;
10218c2aff7Sartem 		int option_index = 0;
10318c2aff7Sartem 		const char *opt;
10418c2aff7Sartem 		static struct option long_options[] = {
10518c2aff7Sartem 			{"udi", 1, NULL, 0},
10618c2aff7Sartem 			{"key", 1, NULL, 0},
10718c2aff7Sartem 			{"hex", 0, NULL, 0},
10818c2aff7Sartem 			{"verbose", 0, NULL, 0},
10918c2aff7Sartem 			{"version", 0, NULL, 0},
11018c2aff7Sartem 			{"help", 0, NULL, 0},
11118c2aff7Sartem 			{NULL, 0, NULL, 0}
11218c2aff7Sartem 		};
11318c2aff7Sartem 
11418c2aff7Sartem 		c = getopt_long (argc, argv, "",
11518c2aff7Sartem 				 long_options, &option_index);
11618c2aff7Sartem 		if (c == -1)
11718c2aff7Sartem 			break;
11818c2aff7Sartem 
11918c2aff7Sartem 		switch (c) {
12018c2aff7Sartem 		case 0:
12118c2aff7Sartem 			opt = long_options[option_index].name;
12218c2aff7Sartem 
12318c2aff7Sartem 			if (strcmp (opt, "help") == 0) {
12418c2aff7Sartem 				usage (argc, argv);
12518c2aff7Sartem 				return 0;
12618c2aff7Sartem 			} else if (strcmp (opt, "hex") == 0) {
12718c2aff7Sartem 				is_hex = TRUE;
12818c2aff7Sartem 			} else if (strcmp (opt, "verbose") == 0) {
12918c2aff7Sartem 				is_verbose = TRUE;
13018c2aff7Sartem 			} else if (strcmp (opt, "version") == 0) {
13118c2aff7Sartem 				is_version = TRUE;
13218c2aff7Sartem 			} else if (strcmp (opt, "key") == 0) {
13318c2aff7Sartem 				key = strdup (optarg);
13418c2aff7Sartem 			} else if (strcmp (opt, "udi") == 0) {
13518c2aff7Sartem 				udi = strdup (optarg);
13618c2aff7Sartem 			}
13718c2aff7Sartem 			break;
13818c2aff7Sartem 
13918c2aff7Sartem 		default:
14018c2aff7Sartem 			usage (argc, argv);
14118c2aff7Sartem 			return 1;
14218c2aff7Sartem 			break;
14318c2aff7Sartem 		}
14418c2aff7Sartem 	}
14518c2aff7Sartem 
14618c2aff7Sartem 	if (is_version) {
14718c2aff7Sartem 		printf ("hal-get-property " PACKAGE_VERSION "\n");
14818c2aff7Sartem 		return 0;
14918c2aff7Sartem 	}
15018c2aff7Sartem 
15118c2aff7Sartem 	if (udi == NULL || key == NULL) {
15218c2aff7Sartem 		usage (argc, argv);
15318c2aff7Sartem 		return 1;
15418c2aff7Sartem 	}
15518c2aff7Sartem 
156*55fea89dSDan Cross 	dbus_error_init (&error);
15718c2aff7Sartem 	if ((hal_ctx = libhal_ctx_new ()) == NULL) {
15818c2aff7Sartem 		fprintf (stderr, "error: libhal_ctx_new\n");
15918c2aff7Sartem 		return 1;
16018c2aff7Sartem 	}
16118c2aff7Sartem 	if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) {
16218c2aff7Sartem 		fprintf (stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error.name, error.message);
16318c2aff7Sartem 		LIBHAL_FREE_DBUS_ERROR (&error);
16418c2aff7Sartem 		return 1;
16518c2aff7Sartem 	}
16618c2aff7Sartem 	if (!libhal_ctx_init (hal_ctx, &error)) {
16718c2aff7Sartem 		if (dbus_error_is_set(&error)) {
16818c2aff7Sartem 			fprintf (stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message);
169de7d23d8SLin Guo - Sun Microsystems 			dbus_error_free (&error);
17018c2aff7Sartem 		}
17118c2aff7Sartem 		fprintf (stderr, "Could not initialise connection to hald.\n"
17218c2aff7Sartem 				 "Normally this means the HAL daemon (hald) is not running or not ready.\n");
17318c2aff7Sartem 		return 1;
17418c2aff7Sartem 	}
17518c2aff7Sartem 
176de7d23d8SLin Guo - Sun Microsystems         /* check UDI exists */
177de7d23d8SLin Guo - Sun Microsystems 	udi_exists = libhal_device_exists (hal_ctx, udi, &error);
178de7d23d8SLin Guo - Sun Microsystems 	if (!udi_exists) {
179de7d23d8SLin Guo - Sun Microsystems 		fprintf (stderr, "error: UDI %s does not exist\n", udi);
180de7d23d8SLin Guo - Sun Microsystems 		return 1;
181de7d23d8SLin Guo - Sun Microsystems 	}
182de7d23d8SLin Guo - Sun Microsystems         if (dbus_error_is_set(&error)) {
183de7d23d8SLin Guo - Sun Microsystems 		fprintf (stderr, "error: libhal_device_exists: %s: %s\n", error.name, error.message);
184de7d23d8SLin Guo - Sun Microsystems 		dbus_error_free (&error);
185de7d23d8SLin Guo - Sun Microsystems 		return 1;
186de7d23d8SLin Guo - Sun Microsystems 	}
187de7d23d8SLin Guo - Sun Microsystems 
188de7d23d8SLin Guo - Sun Microsystems 
18918c2aff7Sartem 	type = libhal_device_get_property_type (hal_ctx, udi, key, &error);
19018c2aff7Sartem 	if (type == LIBHAL_PROPERTY_TYPE_INVALID) {
191de7d23d8SLin Guo - Sun Microsystems 		if (dbus_error_is_set(&error)) {
192de7d23d8SLin Guo - Sun Microsystems 		        fprintf (stderr, "error: libhal_device_get_property_type: %s: %s\n", error.name, error.message);
193de7d23d8SLin Guo - Sun Microsystems 			dbus_error_free (&error);
194de7d23d8SLin Guo - Sun Microsystems 		} else {
195de7d23d8SLin Guo - Sun Microsystems 		        fprintf (stderr, "error: libhal_device_get_property_type: invalid params.\n");
196de7d23d8SLin Guo - Sun Microsystems 		}
19718c2aff7Sartem 		return 1;
19818c2aff7Sartem 	}
19918c2aff7Sartem 	/* emit the value to stdout */
20018c2aff7Sartem 	switch (type) {
20118c2aff7Sartem 	case LIBHAL_PROPERTY_TYPE_STRING:
20218c2aff7Sartem 		str = libhal_device_get_property_string (hal_ctx, udi, key, &error);
20318c2aff7Sartem 		if (is_verbose)
20418c2aff7Sartem 			printf ("Type is string\n");
20518c2aff7Sartem 		printf ("%s\n", str);
20618c2aff7Sartem 		libhal_free_string (str);
20718c2aff7Sartem 		break;
20818c2aff7Sartem 	case LIBHAL_PROPERTY_TYPE_INT32:
20918c2aff7Sartem 		if (is_verbose)
21018c2aff7Sartem 			printf ("Type is integer (shown in %s)\n",
21118c2aff7Sartem 				(is_hex ? "hexadecimal" : "decimal"));
21218c2aff7Sartem 		printf ((is_hex ? "%x\n" : "%d\n"),
21318c2aff7Sartem 			libhal_device_get_property_int (hal_ctx, udi, key, &error));
21418c2aff7Sartem 		break;
21518c2aff7Sartem 	case LIBHAL_PROPERTY_TYPE_UINT64:
21618c2aff7Sartem 		if (is_verbose)
21718c2aff7Sartem 			printf ("Type is uint64 (shown in %s)\n",
21818c2aff7Sartem 				(is_hex ? "hexadecimal" : "decimal"));
21918c2aff7Sartem 		printf ((is_hex ? "%llx\n" : "%llu\n"),
22018c2aff7Sartem 			(long long unsigned int) libhal_device_get_property_uint64 (hal_ctx, udi, key, &error));
22118c2aff7Sartem 		break;
22218c2aff7Sartem 	case LIBHAL_PROPERTY_TYPE_DOUBLE:
22318c2aff7Sartem 		if (is_verbose)
22418c2aff7Sartem 			printf ("Type is double\n");
22518c2aff7Sartem 		printf ("%f\n",
22618c2aff7Sartem 			libhal_device_get_property_double (hal_ctx, udi, key, &error));
22718c2aff7Sartem 		break;
22818c2aff7Sartem 	case LIBHAL_PROPERTY_TYPE_BOOLEAN:
22918c2aff7Sartem 		if (is_verbose)
23018c2aff7Sartem 			printf ("Type is boolean\n");
23118c2aff7Sartem 		printf ("%s\n",
23218c2aff7Sartem 			libhal_device_get_property_bool (hal_ctx, udi, key, &error) ? "true" : "false");
23318c2aff7Sartem 		break;
23418c2aff7Sartem 
23518c2aff7Sartem 	case LIBHAL_PROPERTY_TYPE_STRLIST:
23618c2aff7Sartem 	{
23718c2aff7Sartem 		unsigned int i;
23818c2aff7Sartem 		char **strlist;
239*55fea89dSDan Cross 
24018c2aff7Sartem 		if ((strlist = libhal_device_get_property_strlist (hal_ctx, udi, key, &error)) != NULL) {
241*55fea89dSDan Cross 
24218c2aff7Sartem 			for (i = 0; strlist[i] != 0; i++) {
24318c2aff7Sartem 				printf ("%s", strlist[i]);
24418c2aff7Sartem 				if (strlist[i+1] != NULL)
24518c2aff7Sartem 					printf (" ");
24618c2aff7Sartem 			}
247*55fea89dSDan Cross 		}
24818c2aff7Sartem 		break;
24918c2aff7Sartem 	}
25018c2aff7Sartem 	default:
25118c2aff7Sartem 		printf ("Unknown type %d='%c'\n", type, type);
25218c2aff7Sartem 		return 1;
25318c2aff7Sartem 		break;
25418c2aff7Sartem 	}
25518c2aff7Sartem 
25618c2aff7Sartem 	if (dbus_error_is_set (&error)) {
25718c2aff7Sartem 		fprintf (stderr, "error: %s: %s\n", error.name, error.message);
25818c2aff7Sartem 		dbus_error_free (&error);
25918c2aff7Sartem 		return 1;
26018c2aff7Sartem 	}
26118c2aff7Sartem 
26218c2aff7Sartem 
26318c2aff7Sartem 	return 0;
26418c2aff7Sartem }
26518c2aff7Sartem 
26618c2aff7Sartem /**
26718c2aff7Sartem  * @}
26818c2aff7Sartem  */
269