00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifdef HAVE_CONFIG_H
00028 # include <config.h>
00029 #endif
00030
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <unistd.h>
00035 #include <getopt.h>
00036
00037 #include <libhal/libhal.h>
00038
00053 static void usage(int argc, char* argv[])
00054 {
00055 fprintf(stderr,
00056 "\n"
00057 "usage : %s --udi <udi> --key <key>\n"
00058 " (--int <value> | --string <value> | --bool <value> |"
00059 " --double <value> | --remove) [--help]\n", argv[0]);
00060 fprintf(stderr,
00061 "\n"
00062 " --udi Unique Device Id\n"
00063 " --key Key of the property to set\n"
00064 " --int Set value to an integer. Accepts decimal and "
00065 " hexadecimal prefixed with 0x or x\n"
00066 " --string Set value to a string\n"
00067 " --double Set value to a floating point number\n"
00068 " --bool Set value to a boolean, ie. true or false\n"
00069 " --remove Indicates that the property should be removed\n"
00070 " --help Show this information and exit\n"
00071 "\n"
00072 "This program attempts to set property for a device. Note that, due to\n"
00073 "security considerations, it may not be possible to set a property; on\n"
00074 "success this program exits with exit code 0. On error, the program exits\n"
00075 "with an exit code different from 0\n"
00076 "\n");
00077 }
00078
00085 int main(int argc, char* argv[])
00086 {
00087 int rc = 0;
00088 char* udi = NULL;
00089 char* key = NULL;
00090 char* str_value = NULL;
00091 dbus_int32_t int_value = 0;
00092 double double_value = 0.0f;
00093 dbus_bool_t bool_value = TRUE;
00094 dbus_bool_t remove = FALSE;
00095 int type = DBUS_TYPE_NIL;
00096
00097 fprintf(stderr, "%s " PACKAGE_VERSION "\n", argv[0]);
00098
00099 if( argc<=1 )
00100 {
00101 usage(argc, argv);
00102 return 1;
00103 }
00104
00105 while(1)
00106 {
00107 int c;
00108 int option_index = 0;
00109 const char* opt;
00110 static struct option long_options[] =
00111 {
00112 {"udi", 1, NULL, 0},
00113 {"key", 1, NULL, 0},
00114 {"int", 1, NULL, 0},
00115 {"string", 1, NULL, 0},
00116 {"double", 1, NULL, 0},
00117 {"bool", 1, NULL, 0},
00118 {"remove", 0, NULL, 0},
00119 {"help", 0, NULL, 0},
00120 {NULL, 0, NULL, 0}
00121 };
00122
00123 c = getopt_long(argc, argv, "",
00124 long_options, &option_index);
00125 if (c == -1)
00126 break;
00127
00128 switch(c)
00129 {
00130 case 0:
00131 opt = long_options[option_index].name;
00132
00133 if( strcmp(opt, "help")==0 )
00134 {
00135 usage(argc, argv);
00136 return 0;
00137 }
00138 else if( strcmp(opt, "key")==0 )
00139 {
00140 key = strdup(optarg);
00141 }
00142 else if( strcmp(opt, "string")==0 )
00143 {
00144 str_value = strdup(optarg);
00145 type = DBUS_TYPE_STRING;
00146 }
00147 else if( strcmp(opt, "int")==0 )
00148 {
00149 int_value = strtol(optarg, NULL, 0);
00150 type = DBUS_TYPE_INT32;
00151 }
00152 else if( strcmp(opt, "double")==0 )
00153 {
00154 double_value = (double) atof(optarg);
00155 type = DBUS_TYPE_DOUBLE;
00156 }
00157 else if( strcmp(opt, "bool")==0 )
00158 {
00159 if( strcmp(optarg, "true")==0 )
00160 bool_value = TRUE;
00161 else if( strcmp(optarg, "false")==0 )
00162 bool_value = FALSE;
00163 else
00164 {
00165 usage(argc, argv);
00166 return 1;
00167 }
00168 type = DBUS_TYPE_BOOLEAN;
00169 }
00170 else if( strcmp(opt, "remove")==0 )
00171 {
00172 remove = TRUE;
00173 }
00174 else if( strcmp(opt, "udi")==0 )
00175 {
00176 udi = strdup(optarg);
00177 }
00178 break;
00179
00180 default:
00181 usage(argc, argv);
00182 return 1;
00183 break;
00184 }
00185 }
00186
00187
00188 if( (remove && type!=DBUS_TYPE_NIL) ||
00189 ((!remove) && type==DBUS_TYPE_NIL ) )
00190 {
00191 usage(argc, argv);
00192 return 1;
00193 }
00194
00195 fprintf(stderr, "\n");
00196
00197 if( hal_initialize(NULL, FALSE) )
00198 {
00199 fprintf(stderr, "error: hal_initialize failed\n");
00200 return 1;
00201 }
00202
00203 if( remove )
00204 {
00205 rc = hal_device_remove_property(udi, key);
00206 if( rc!=0 )
00207 return 1;
00208 }
00209 else
00210 {
00211 switch( type )
00212 {
00213 case DBUS_TYPE_STRING:
00214 rc = hal_device_set_property_string(udi, key, str_value);
00215 break;
00216 case DBUS_TYPE_INT32:
00217 rc = hal_device_set_property_int(udi, key, int_value);
00218 break;
00219 case DBUS_TYPE_DOUBLE:
00220 rc = hal_device_set_property_double(udi, key, double_value);
00221 break;
00222 case DBUS_TYPE_BOOLEAN:
00223 rc = hal_device_set_property_bool(udi, key, bool_value);
00224 break;
00225 }
00226 if( rc!=0 )
00227 return 1;
00228 }
00229
00230 return 0;
00231 }
00232