118c2aff7Sartem /***************************************************************************
218c2aff7Sartem * CVSID: $Id$
318c2aff7Sartem *
418c2aff7Sartem * hal_set_property.c : Set 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 <stdlib.h>
3318c2aff7Sartem #include <string.h>
3418c2aff7Sartem #include <unistd.h>
3518c2aff7Sartem #include <getopt.h>
3618c2aff7Sartem
3718c2aff7Sartem #include <libhal.h>
3818c2aff7Sartem
3918c2aff7Sartem static LibHalContext *hal_ctx;
4018c2aff7Sartem
4118c2aff7Sartem enum property_op {
4218c2aff7Sartem PROP_INT,
4318c2aff7Sartem PROP_UINT64,
4418c2aff7Sartem PROP_STRING,
4518c2aff7Sartem PROP_DOUBLE,
4618c2aff7Sartem PROP_BOOL,
4718c2aff7Sartem PROP_STRLIST_PRE,
4818c2aff7Sartem PROP_STRLIST_POST,
4918c2aff7Sartem PROP_STRLIST_REM,
5018c2aff7Sartem PROP_INVALID
5118c2aff7Sartem };
5218c2aff7Sartem
5318c2aff7Sartem /**
5418c2aff7Sartem * @defgroup HalSetProperty Set HAL device property
5518c2aff7Sartem * @ingroup HalMisc
5618c2aff7Sartem *
5718c2aff7Sartem * @brief A commandline tool setting a property of a device. Uses libhal
5818c2aff7Sartem *
5918c2aff7Sartem * @{
6018c2aff7Sartem */
6118c2aff7Sartem
6218c2aff7Sartem /** Print out program usage.
6318c2aff7Sartem *
6418c2aff7Sartem * @param argc Number of arguments given to program
6518c2aff7Sartem * @param argv Arguments given to program
6618c2aff7Sartem */
6718c2aff7Sartem static void
usage(int argc,char * argv[])6818c2aff7Sartem usage (int argc, char *argv[])
6918c2aff7Sartem {
7018c2aff7Sartem fprintf (stderr,
7118c2aff7Sartem "\n"
7218c2aff7Sartem "usage : hal-set-property --udi <udi> --key <key>\n"
7318c2aff7Sartem " (--int <value> | --string <value> | --bool <value> |\n"
7418c2aff7Sartem " --strlist-pre <value> | --strlist-post <value> |\n"
7518c2aff7Sartem " --strlist-rem <value> | --double <value> | --remove)\n"
76*002c14a1SAlexander Pyhalov " [--direct] [--help] [--version]\n");
7718c2aff7Sartem fprintf (stderr,
7818c2aff7Sartem "\n" " --udi Unique Device Id\n"
7918c2aff7Sartem " --key Key of the property to set\n"
8018c2aff7Sartem " --int Set value to an integer. Accepts decimal and "
8118c2aff7Sartem " hexadecimal prefixed with 0x or x\n"
8218c2aff7Sartem " --uint64 Set value to an integer. Accepts decimal and "
8318c2aff7Sartem " hexadecimal prefixed with 0x or x\n"
8418c2aff7Sartem " --string Set value to a string\n"
8518c2aff7Sartem " --double Set value to a floating point number\n"
8618c2aff7Sartem " --bool Set value to a boolean, ie. true or false\n"
8718c2aff7Sartem " --strlist-pre Prepend a string to a list\n"
8818c2aff7Sartem " --strlist-post Append a string to a list\n"
8918c2aff7Sartem " --strlist-rem Remove a string from a list\n"
9018c2aff7Sartem " --remove Indicates that the property should be removed\n"
91*002c14a1SAlexander Pyhalov " --direct Use direct HAL connection\n"
9218c2aff7Sartem " --version Show version and exit\n"
9318c2aff7Sartem " --help Show this information and exit\n"
9418c2aff7Sartem "\n"
9518c2aff7Sartem "This program attempts to set property for a device. Note that, due to\n"
9618c2aff7Sartem "security considerations, it may not be possible to set a property; on\n"
9718c2aff7Sartem "success this program exits with exit code 0. On error, the program exits\n"
9818c2aff7Sartem "with an exit code different from 0\n" "\n");
9918c2aff7Sartem }
10018c2aff7Sartem
10118c2aff7Sartem /** Entry point
10218c2aff7Sartem *
10318c2aff7Sartem * @param argc Number of arguments given to program
10418c2aff7Sartem * @param argv Arguments given to program
10518c2aff7Sartem * @return Return code
10618c2aff7Sartem */
10718c2aff7Sartem int
main(int argc,char * argv[])10818c2aff7Sartem main (int argc, char *argv[])
10918c2aff7Sartem {
11018c2aff7Sartem dbus_bool_t rc = 0;
11118c2aff7Sartem char *udi = NULL;
11218c2aff7Sartem char *key = NULL;
11318c2aff7Sartem char *str_value = NULL;
11418c2aff7Sartem dbus_int32_t int_value = 0;
11518c2aff7Sartem dbus_uint64_t uint64_value = 0;
11618c2aff7Sartem double double_value = 0.0f;
11718c2aff7Sartem dbus_bool_t bool_value = TRUE;
11818c2aff7Sartem dbus_bool_t remove = FALSE;
11918c2aff7Sartem dbus_bool_t is_version = FALSE;
120de7d23d8SLin Guo - Sun Microsystems dbus_bool_t udi_exists = FALSE;
12118c2aff7Sartem int type = PROP_INVALID;
12218c2aff7Sartem DBusError error;
123*002c14a1SAlexander Pyhalov dbus_bool_t direct = FALSE;
12418c2aff7Sartem
12518c2aff7Sartem if (argc <= 1) {
12618c2aff7Sartem usage (argc, argv);
12718c2aff7Sartem return 1;
12818c2aff7Sartem }
12918c2aff7Sartem
13018c2aff7Sartem while (1) {
13118c2aff7Sartem int c;
13218c2aff7Sartem int option_index = 0;
13318c2aff7Sartem const char *opt;
13418c2aff7Sartem static struct option long_options[] = {
13518c2aff7Sartem {"udi", 1, NULL, 0},
13618c2aff7Sartem {"key", 1, NULL, 0},
13718c2aff7Sartem {"int", 1, NULL, 0},
13818c2aff7Sartem {"uint64", 1, NULL, 0},
13918c2aff7Sartem {"string", 1, NULL, 0},
14018c2aff7Sartem {"double", 1, NULL, 0},
14118c2aff7Sartem {"bool", 1, NULL, 0},
14218c2aff7Sartem {"strlist-pre", 1, NULL, 0},
14318c2aff7Sartem {"strlist-post", 1, NULL, 0},
14418c2aff7Sartem {"strlist-rem", 1, NULL, 0},
145*002c14a1SAlexander Pyhalov {"direct", 0, NULL, 0},
14618c2aff7Sartem {"remove", 0, NULL, 0},
14718c2aff7Sartem {"version", 0, NULL, 0},
14818c2aff7Sartem {"help", 0, NULL, 0},
14918c2aff7Sartem {NULL, 0, NULL, 0}
15018c2aff7Sartem };
151*002c14a1SAlexander Pyhalov
15218c2aff7Sartem c = getopt_long (argc, argv, "",
15318c2aff7Sartem long_options, &option_index);
15418c2aff7Sartem if (c == -1)
15518c2aff7Sartem break;
15618c2aff7Sartem
15718c2aff7Sartem switch (c) {
15818c2aff7Sartem case 0:
15918c2aff7Sartem opt = long_options[option_index].name;
16018c2aff7Sartem
16118c2aff7Sartem if (strcmp (opt, "help") == 0) {
16218c2aff7Sartem usage (argc, argv);
16318c2aff7Sartem return 0;
16418c2aff7Sartem } else if (strcmp (opt, "key") == 0) {
16518c2aff7Sartem key = strdup (optarg);
16618c2aff7Sartem } else if (strcmp (opt, "string") == 0) {
16718c2aff7Sartem str_value = strdup (optarg);
16818c2aff7Sartem type = PROP_STRING;
16918c2aff7Sartem } else if (strcmp (opt, "int") == 0) {
17018c2aff7Sartem int_value = strtol (optarg, NULL, 0);
17118c2aff7Sartem type = PROP_INT;
17218c2aff7Sartem } else if (strcmp (opt, "uint64") == 0) {
17318c2aff7Sartem uint64_value = strtoull (optarg, NULL, 0);
17418c2aff7Sartem type = PROP_UINT64;
17518c2aff7Sartem } else if (strcmp (opt, "double") == 0) {
17618c2aff7Sartem double_value = (double) atof (optarg);
17718c2aff7Sartem type = PROP_DOUBLE;
17818c2aff7Sartem } else if (strcmp (opt, "bool") == 0) {
17918c2aff7Sartem if (strcmp (optarg, "true") == 0)
18018c2aff7Sartem bool_value = TRUE;
18118c2aff7Sartem else if (strcmp (optarg, "false") == 0)
18218c2aff7Sartem bool_value = FALSE;
18318c2aff7Sartem else {
18418c2aff7Sartem usage (argc, argv);
18518c2aff7Sartem return 1;
18618c2aff7Sartem }
18718c2aff7Sartem type = PROP_BOOL;
18818c2aff7Sartem } else if (strcmp (opt, "strlist-pre") == 0) {
18918c2aff7Sartem str_value = strdup (optarg);
19018c2aff7Sartem type = PROP_STRLIST_PRE;
19118c2aff7Sartem } else if (strcmp (opt, "strlist-post") == 0) {
19218c2aff7Sartem str_value = strdup (optarg);
19318c2aff7Sartem type = PROP_STRLIST_POST;
19418c2aff7Sartem } else if (strcmp (opt, "strlist-rem") == 0) {
19518c2aff7Sartem str_value = strdup (optarg);
19618c2aff7Sartem type = PROP_STRLIST_REM;
19718c2aff7Sartem } else if (strcmp (opt, "remove") == 0) {
19818c2aff7Sartem remove = TRUE;
199*002c14a1SAlexander Pyhalov } else if (strcmp (opt, "direct") == 0) {
200*002c14a1SAlexander Pyhalov direct = TRUE;
20118c2aff7Sartem } else if (strcmp (opt, "udi") == 0) {
20218c2aff7Sartem udi = strdup (optarg);
20318c2aff7Sartem } else if (strcmp (opt, "version") == 0) {
20418c2aff7Sartem is_version = TRUE;
20518c2aff7Sartem }
20618c2aff7Sartem break;
20718c2aff7Sartem
20818c2aff7Sartem default:
20918c2aff7Sartem usage (argc, argv);
21018c2aff7Sartem return 1;
21118c2aff7Sartem break;
21218c2aff7Sartem }
21318c2aff7Sartem }
21418c2aff7Sartem
21518c2aff7Sartem if (is_version) {
21618c2aff7Sartem printf ("hal-set-property " PACKAGE_VERSION "\n");
21718c2aff7Sartem return 0;
21818c2aff7Sartem }
21918c2aff7Sartem
22018c2aff7Sartem /* must have at least one, but not neither or both */
22118c2aff7Sartem if ((remove && type != PROP_INVALID) || ((!remove) && type == PROP_INVALID)) {
22218c2aff7Sartem usage (argc, argv);
22318c2aff7Sartem return 1;
22418c2aff7Sartem }
225*002c14a1SAlexander Pyhalov
22618c2aff7Sartem fprintf (stderr, "\n");
227*002c14a1SAlexander Pyhalov
228*002c14a1SAlexander Pyhalov dbus_error_init (&error);
229*002c14a1SAlexander Pyhalov if (direct) {
230*002c14a1SAlexander Pyhalov if ((hal_ctx = libhal_ctx_init_direct (&error)) == NULL) {
231*002c14a1SAlexander Pyhalov fprintf (stderr, "error: libhal_ctx_init_direct\n");
23218c2aff7Sartem LIBHAL_FREE_DBUS_ERROR (&error);
233*002c14a1SAlexander Pyhalov return 1;
234*002c14a1SAlexander Pyhalov }
235*002c14a1SAlexander Pyhalov } else {
236*002c14a1SAlexander Pyhalov if ((hal_ctx = libhal_ctx_new ()) == NULL) {
237*002c14a1SAlexander Pyhalov fprintf (stderr, "error: libhal_ctx_new\n");
238*002c14a1SAlexander Pyhalov return 1;
239*002c14a1SAlexander Pyhalov }
240*002c14a1SAlexander Pyhalov if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) {
241*002c14a1SAlexander Pyhalov fprintf (stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error.name, error.message);
242*002c14a1SAlexander Pyhalov LIBHAL_FREE_DBUS_ERROR (&error);
243*002c14a1SAlexander Pyhalov return 1;
244*002c14a1SAlexander Pyhalov }
245*002c14a1SAlexander Pyhalov if (!libhal_ctx_init (hal_ctx, &error)) {
246*002c14a1SAlexander Pyhalov if (dbus_error_is_set(&error)) {
247*002c14a1SAlexander Pyhalov fprintf (stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message);
248*002c14a1SAlexander Pyhalov LIBHAL_FREE_DBUS_ERROR (&error);
249*002c14a1SAlexander Pyhalov }
250*002c14a1SAlexander Pyhalov fprintf (stderr, "Could not initialise connection to hald.\n"
251*002c14a1SAlexander Pyhalov "Normally this means the HAL daemon (hald) is not running or not ready.\n");
252*002c14a1SAlexander Pyhalov return 1;
25318c2aff7Sartem }
25418c2aff7Sartem }
25518c2aff7Sartem
256de7d23d8SLin Guo - Sun Microsystems /* check UDI exists */
257de7d23d8SLin Guo - Sun Microsystems udi_exists = libhal_device_exists (hal_ctx, udi, &error);
258de7d23d8SLin Guo - Sun Microsystems if (!udi_exists) {
259de7d23d8SLin Guo - Sun Microsystems fprintf (stderr, "error: UDI %s does not exist\n", udi);
260de7d23d8SLin Guo - Sun Microsystems return 1;
261de7d23d8SLin Guo - Sun Microsystems }
262de7d23d8SLin Guo - Sun Microsystems if (dbus_error_is_set(&error)) {
263de7d23d8SLin Guo - Sun Microsystems fprintf (stderr, "error: libhal_device_exists: %s: %s\n", error.name, error.message);
264de7d23d8SLin Guo - Sun Microsystems dbus_error_free (&error);
265*002c14a1SAlexander Pyhalov return 1;
266de7d23d8SLin Guo - Sun Microsystems }
267de7d23d8SLin Guo - Sun Microsystems
26818c2aff7Sartem if (remove) {
26918c2aff7Sartem rc = libhal_device_remove_property (hal_ctx, udi, key, &error);
27018c2aff7Sartem if (!rc) {
271de7d23d8SLin Guo - Sun Microsystems if (dbus_error_is_set(&error)) {
272de7d23d8SLin Guo - Sun Microsystems fprintf (stderr, "error: libhal_device_remove_property: %s: %s\n", error.name, error.message);
273de7d23d8SLin Guo - Sun Microsystems dbus_error_free (&error);
274de7d23d8SLin Guo - Sun Microsystems } else {
275de7d23d8SLin Guo - Sun Microsystems fprintf (stderr, "error: libhal_device_remove_property: invalid params.\n");
276de7d23d8SLin Guo - Sun Microsystems }
27718c2aff7Sartem return 1;
27818c2aff7Sartem }
27918c2aff7Sartem } else {
28018c2aff7Sartem switch (type) {
28118c2aff7Sartem case PROP_STRING:
28218c2aff7Sartem rc = libhal_device_set_property_string (hal_ctx, udi, key, str_value, &error);
28318c2aff7Sartem break;
28418c2aff7Sartem case PROP_INT:
28518c2aff7Sartem rc = libhal_device_set_property_int (hal_ctx, udi, key, int_value, &error);
28618c2aff7Sartem break;
28718c2aff7Sartem case PROP_UINT64:
28818c2aff7Sartem rc = libhal_device_set_property_uint64 (hal_ctx, udi, key, uint64_value, &error);
28918c2aff7Sartem break;
29018c2aff7Sartem case PROP_DOUBLE:
29118c2aff7Sartem rc = libhal_device_set_property_double (hal_ctx, udi, key, double_value, &error);
29218c2aff7Sartem break;
29318c2aff7Sartem case PROP_BOOL:
29418c2aff7Sartem rc = libhal_device_set_property_bool (hal_ctx, udi, key, bool_value, &error);
29518c2aff7Sartem break;
29618c2aff7Sartem case PROP_STRLIST_PRE:
29718c2aff7Sartem rc = libhal_device_property_strlist_prepend (hal_ctx, udi, key, str_value, &error);
29818c2aff7Sartem break;
29918c2aff7Sartem case PROP_STRLIST_POST:
30018c2aff7Sartem rc = libhal_device_property_strlist_append (hal_ctx, udi, key, str_value, &error);
30118c2aff7Sartem break;
30218c2aff7Sartem case PROP_STRLIST_REM:
30318c2aff7Sartem rc = libhal_device_property_strlist_remove (hal_ctx, udi, key, str_value, &error);
30418c2aff7Sartem break;
30518c2aff7Sartem }
30618c2aff7Sartem if (!rc) {
307de7d23d8SLin Guo - Sun Microsystems if (dbus_error_is_set(&error)) {
308de7d23d8SLin Guo - Sun Microsystems fprintf (stderr, "error: libhal_device_set_property: %s: %s\n", error.name, error.message);
309de7d23d8SLin Guo - Sun Microsystems dbus_error_free (&error);
310de7d23d8SLin Guo - Sun Microsystems } else {
311de7d23d8SLin Guo - Sun Microsystems fprintf (stderr, "error: libhal_device_set_property: invalid params.\n");
312de7d23d8SLin Guo - Sun Microsystems }
31318c2aff7Sartem return 1;
31418c2aff7Sartem }
31518c2aff7Sartem }
316*002c14a1SAlexander Pyhalov
31718c2aff7Sartem return rc ? 0 : 1;
31818c2aff7Sartem }
31918c2aff7Sartem
32018c2aff7Sartem /**
32118c2aff7Sartem * @}
32218c2aff7Sartem */
323