1 /***************************************************************************
4 * hal_set_property.c : Set 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 static LibHalContext
*hal_ctx
;
54 * @defgroup HalSetProperty Set HAL device property
57 * @brief A commandline tool setting a property of a device. Uses libhal
62 /** Print out program usage.
64 * @param argc Number of arguments given to program
65 * @param argv Arguments given to program
68 usage (int argc
, char *argv
[])
72 "usage : hal-set-property --udi <udi> --key <key>\n"
73 " (--int <value> | --string <value> | --bool <value> |\n"
74 " --strlist-pre <value> | --strlist-post <value> |\n"
75 " --strlist-rem <value> | --double <value> | --remove)\n"
76 " [--direct] [--help] [--version]\n");
78 "\n" " --udi Unique Device Id\n"
79 " --key Key of the property to set\n"
80 " --int Set value to an integer. Accepts decimal and "
81 " hexadecimal prefixed with 0x or x\n"
82 " --uint64 Set value to an integer. Accepts decimal and "
83 " hexadecimal prefixed with 0x or x\n"
84 " --string Set value to a string\n"
85 " --double Set value to a floating point number\n"
86 " --bool Set value to a boolean, ie. true or false\n"
87 " --strlist-pre Prepend a string to a list\n"
88 " --strlist-post Append a string to a list\n"
89 " --strlist-rem Remove a string from a list\n"
90 " --remove Indicates that the property should be removed\n"
91 " --direct Use direct HAL connection\n"
92 " --version Show version and exit\n"
93 " --help Show this information and exit\n"
95 "This program attempts to set property for a device. Note that, due to\n"
96 "security considerations, it may not be possible to set a property; on\n"
97 "success this program exits with exit code 0. On error, the program exits\n"
98 "with an exit code different from 0\n" "\n");
103 * @param argc Number of arguments given to program
104 * @param argv Arguments given to program
105 * @return Return code
108 main (int argc
, char *argv
[])
113 char *str_value
= NULL
;
114 dbus_int32_t int_value
= 0;
115 dbus_uint64_t uint64_value
= 0;
116 double double_value
= 0.0f
;
117 dbus_bool_t bool_value
= TRUE
;
118 dbus_bool_t remove
= FALSE
;
119 dbus_bool_t is_version
= FALSE
;
120 dbus_bool_t udi_exists
= FALSE
;
121 int type
= PROP_INVALID
;
123 dbus_bool_t direct
= FALSE
;
132 int option_index
= 0;
134 static struct option long_options
[] = {
138 {"uint64", 1, NULL
, 0},
139 {"string", 1, NULL
, 0},
140 {"double", 1, NULL
, 0},
141 {"bool", 1, NULL
, 0},
142 {"strlist-pre", 1, NULL
, 0},
143 {"strlist-post", 1, NULL
, 0},
144 {"strlist-rem", 1, NULL
, 0},
145 {"direct", 0, NULL
, 0},
146 {"remove", 0, NULL
, 0},
147 {"version", 0, NULL
, 0},
148 {"help", 0, NULL
, 0},
152 c
= getopt_long (argc
, argv
, "",
153 long_options
, &option_index
);
159 opt
= long_options
[option_index
].name
;
161 if (strcmp (opt
, "help") == 0) {
164 } else if (strcmp (opt
, "key") == 0) {
165 key
= strdup (optarg
);
166 } else if (strcmp (opt
, "string") == 0) {
167 str_value
= strdup (optarg
);
169 } else if (strcmp (opt
, "int") == 0) {
170 int_value
= strtol (optarg
, NULL
, 0);
172 } else if (strcmp (opt
, "uint64") == 0) {
173 uint64_value
= strtoull (optarg
, NULL
, 0);
175 } else if (strcmp (opt
, "double") == 0) {
176 double_value
= (double) atof (optarg
);
178 } else if (strcmp (opt
, "bool") == 0) {
179 if (strcmp (optarg
, "true") == 0)
181 else if (strcmp (optarg
, "false") == 0)
188 } else if (strcmp (opt
, "strlist-pre") == 0) {
189 str_value
= strdup (optarg
);
190 type
= PROP_STRLIST_PRE
;
191 } else if (strcmp (opt
, "strlist-post") == 0) {
192 str_value
= strdup (optarg
);
193 type
= PROP_STRLIST_POST
;
194 } else if (strcmp (opt
, "strlist-rem") == 0) {
195 str_value
= strdup (optarg
);
196 type
= PROP_STRLIST_REM
;
197 } else if (strcmp (opt
, "remove") == 0) {
199 } else if (strcmp (opt
, "direct") == 0) {
201 } else if (strcmp (opt
, "udi") == 0) {
202 udi
= strdup (optarg
);
203 } else if (strcmp (opt
, "version") == 0) {
216 printf ("hal-set-property " PACKAGE_VERSION
"\n");
220 /* must have at least one, but not neither or both */
221 if ((remove
&& type
!= PROP_INVALID
) || ((!remove
) && type
== PROP_INVALID
)) {
226 fprintf (stderr
, "\n");
228 dbus_error_init (&error
);
230 if ((hal_ctx
= libhal_ctx_init_direct (&error
)) == NULL
) {
231 fprintf (stderr
, "error: libhal_ctx_init_direct\n");
232 LIBHAL_FREE_DBUS_ERROR (&error
);
236 if ((hal_ctx
= libhal_ctx_new ()) == NULL
) {
237 fprintf (stderr
, "error: libhal_ctx_new\n");
240 if (!libhal_ctx_set_dbus_connection (hal_ctx
, dbus_bus_get (DBUS_BUS_SYSTEM
, &error
))) {
241 fprintf (stderr
, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error
.name
, error
.message
);
242 LIBHAL_FREE_DBUS_ERROR (&error
);
245 if (!libhal_ctx_init (hal_ctx
, &error
)) {
246 if (dbus_error_is_set(&error
)) {
247 fprintf (stderr
, "error: libhal_ctx_init: %s: %s\n", error
.name
, error
.message
);
248 LIBHAL_FREE_DBUS_ERROR (&error
);
250 fprintf (stderr
, "Could not initialise connection to hald.\n"
251 "Normally this means the HAL daemon (hald) is not running or not ready.\n");
256 /* check UDI exists */
257 udi_exists
= libhal_device_exists (hal_ctx
, udi
, &error
);
259 fprintf (stderr
, "error: UDI %s does not exist\n", udi
);
262 if (dbus_error_is_set(&error
)) {
263 fprintf (stderr
, "error: libhal_device_exists: %s: %s\n", error
.name
, error
.message
);
264 dbus_error_free (&error
);
269 rc
= libhal_device_remove_property (hal_ctx
, udi
, key
, &error
);
271 if (dbus_error_is_set(&error
)) {
272 fprintf (stderr
, "error: libhal_device_remove_property: %s: %s\n", error
.name
, error
.message
);
273 dbus_error_free (&error
);
275 fprintf (stderr
, "error: libhal_device_remove_property: invalid params.\n");
282 rc
= libhal_device_set_property_string (hal_ctx
, udi
, key
, str_value
, &error
);
285 rc
= libhal_device_set_property_int (hal_ctx
, udi
, key
, int_value
, &error
);
288 rc
= libhal_device_set_property_uint64 (hal_ctx
, udi
, key
, uint64_value
, &error
);
291 rc
= libhal_device_set_property_double (hal_ctx
, udi
, key
, double_value
, &error
);
294 rc
= libhal_device_set_property_bool (hal_ctx
, udi
, key
, bool_value
, &error
);
296 case PROP_STRLIST_PRE
:
297 rc
= libhal_device_property_strlist_prepend (hal_ctx
, udi
, key
, str_value
, &error
);
299 case PROP_STRLIST_POST
:
300 rc
= libhal_device_property_strlist_append (hal_ctx
, udi
, key
, str_value
, &error
);
302 case PROP_STRLIST_REM
:
303 rc
= libhal_device_property_strlist_remove (hal_ctx
, udi
, key
, str_value
, &error
);
307 if (dbus_error_is_set(&error
)) {
308 fprintf (stderr
, "error: libhal_device_set_property: %s: %s\n", error
.name
, error
.message
);
309 dbus_error_free (&error
);
311 fprintf (stderr
, "error: libhal_device_set_property: invalid params.\n");