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 " [--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 " --version Show version and exit\n"
92 " --help Show this information and exit\n"
94 "This program attempts to set property for a device. Note that, due to\n"
95 "security considerations, it may not be possible to set a property; on\n"
96 "success this program exits with exit code 0. On error, the program exits\n"
97 "with an exit code different from 0\n" "\n");
102 * @param argc Number of arguments given to program
103 * @param argv Arguments given to program
104 * @return Return code
107 main (int argc
, char *argv
[])
112 char *str_value
= NULL
;
113 dbus_int32_t int_value
= 0;
114 dbus_uint64_t uint64_value
= 0;
115 double double_value
= 0.0f
;
116 dbus_bool_t bool_value
= TRUE
;
117 dbus_bool_t remove
= FALSE
;
118 dbus_bool_t is_version
= FALSE
;
119 dbus_bool_t udi_exists
= FALSE
;
120 int type
= PROP_INVALID
;
130 int option_index
= 0;
132 static struct option long_options
[] = {
136 {"uint64", 1, NULL
, 0},
137 {"string", 1, NULL
, 0},
138 {"double", 1, NULL
, 0},
139 {"bool", 1, NULL
, 0},
140 {"strlist-pre", 1, NULL
, 0},
141 {"strlist-post", 1, NULL
, 0},
142 {"strlist-rem", 1, NULL
, 0},
143 {"remove", 0, NULL
, 0},
144 {"version", 0, NULL
, 0},
145 {"help", 0, NULL
, 0},
149 c
= getopt_long (argc
, argv
, "",
150 long_options
, &option_index
);
156 opt
= long_options
[option_index
].name
;
158 if (strcmp (opt
, "help") == 0) {
161 } else if (strcmp (opt
, "key") == 0) {
162 key
= strdup (optarg
);
163 } else if (strcmp (opt
, "string") == 0) {
164 str_value
= strdup (optarg
);
166 } else if (strcmp (opt
, "int") == 0) {
167 int_value
= strtol (optarg
, NULL
, 0);
169 } else if (strcmp (opt
, "uint64") == 0) {
170 uint64_value
= strtoull (optarg
, NULL
, 0);
172 } else if (strcmp (opt
, "double") == 0) {
173 double_value
= (double) atof (optarg
);
175 } else if (strcmp (opt
, "bool") == 0) {
176 if (strcmp (optarg
, "true") == 0)
178 else if (strcmp (optarg
, "false") == 0)
185 } else if (strcmp (opt
, "strlist-pre") == 0) {
186 str_value
= strdup (optarg
);
187 type
= PROP_STRLIST_PRE
;
188 } else if (strcmp (opt
, "strlist-post") == 0) {
189 str_value
= strdup (optarg
);
190 type
= PROP_STRLIST_POST
;
191 } else if (strcmp (opt
, "strlist-rem") == 0) {
192 str_value
= strdup (optarg
);
193 type
= PROP_STRLIST_REM
;
194 } else if (strcmp (opt
, "remove") == 0) {
196 } else if (strcmp (opt
, "udi") == 0) {
197 udi
= strdup (optarg
);
198 } else if (strcmp (opt
, "version") == 0) {
211 printf ("hal-set-property " PACKAGE_VERSION
"\n");
215 /* must have at least one, but not neither or both */
216 if ((remove
&& type
!= PROP_INVALID
) || ((!remove
) && type
== PROP_INVALID
)) {
221 fprintf (stderr
, "\n");
223 dbus_error_init (&error
);
224 if ((hal_ctx
= libhal_ctx_new ()) == NULL
) {
225 fprintf (stderr
, "error: libhal_ctx_new\n");
228 if (!libhal_ctx_set_dbus_connection (hal_ctx
, dbus_bus_get (DBUS_BUS_SYSTEM
, &error
))) {
229 fprintf (stderr
, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error
.name
, error
.message
);
230 LIBHAL_FREE_DBUS_ERROR (&error
);
233 if (!libhal_ctx_init (hal_ctx
, &error
)) {
234 if (dbus_error_is_set(&error
)) {
235 fprintf (stderr
, "error: libhal_ctx_init: %s: %s\n", error
.name
, error
.message
);
236 LIBHAL_FREE_DBUS_ERROR (&error
);
238 fprintf (stderr
, "Could not initialise connection to hald.\n"
239 "Normally this means the HAL daemon (hald) is not running or not ready.\n");
243 /* check UDI exists */
244 udi_exists
= libhal_device_exists (hal_ctx
, udi
, &error
);
246 fprintf (stderr
, "error: UDI %s does not exist\n", udi
);
249 if (dbus_error_is_set(&error
)) {
250 fprintf (stderr
, "error: libhal_device_exists: %s: %s\n", error
.name
, error
.message
);
251 dbus_error_free (&error
);
256 rc
= libhal_device_remove_property (hal_ctx
, udi
, key
, &error
);
258 if (dbus_error_is_set(&error
)) {
259 fprintf (stderr
, "error: libhal_device_remove_property: %s: %s\n", error
.name
, error
.message
);
260 dbus_error_free (&error
);
262 fprintf (stderr
, "error: libhal_device_remove_property: invalid params.\n");
269 rc
= libhal_device_set_property_string (hal_ctx
, udi
, key
, str_value
, &error
);
272 rc
= libhal_device_set_property_int (hal_ctx
, udi
, key
, int_value
, &error
);
275 rc
= libhal_device_set_property_uint64 (hal_ctx
, udi
, key
, uint64_value
, &error
);
278 rc
= libhal_device_set_property_double (hal_ctx
, udi
, key
, double_value
, &error
);
281 rc
= libhal_device_set_property_bool (hal_ctx
, udi
, key
, bool_value
, &error
);
283 case PROP_STRLIST_PRE
:
284 rc
= libhal_device_property_strlist_prepend (hal_ctx
, udi
, key
, str_value
, &error
);
286 case PROP_STRLIST_POST
:
287 rc
= libhal_device_property_strlist_append (hal_ctx
, udi
, key
, str_value
, &error
);
289 case PROP_STRLIST_REM
:
290 rc
= libhal_device_property_strlist_remove (hal_ctx
, udi
, key
, str_value
, &error
);
294 if (dbus_error_is_set(&error
)) {
295 fprintf (stderr
, "error: libhal_device_set_property: %s: %s\n", error
.name
, error
.message
);
296 dbus_error_free (&error
);
298 fprintf (stderr
, "error: libhal_device_set_property: invalid params.\n");