Main Page | Modules | Data Structures | File List | Data Fields | Related Pages

hal_set_property.c

00001 /***************************************************************************
00002  * CVSID: $Id: hal_set_property.c,v 1.4 2004/03/03 17:56:56 david Exp $
00003  *
00004  * hal_set_property.c : Set property for a device
00005  *
00006  * Copyright (C) 2003 David Zeuthen, <david@fubar.dk>
00007  *
00008  * Licensed under the Academic Free License version 2.0
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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
00054 usage (int argc, char *argv[])
00055 {
00056     fprintf (stderr,
00057  "\n"
00058  "usage : %s --udi <udi> --key <key>\n"
00059  "           (--int <value> | --string <value> | --bool <value> |"
00060  "            --double <value> | --remove) [--help]\n",
00061          argv[0]);
00062     fprintf (stderr,
00063  "\n" "        --udi            Unique Device Id\n"
00064  "        --key            Key of the property to set\n"
00065  "        --int            Set value to an integer. Accepts decimal and "
00066  "                         hexadecimal prefixed with 0x or x\n"
00067  "        --string         Set value to a string\n"
00068  "        --double         Set value to a floating point number\n"
00069  "        --bool           Set value to a boolean, ie. true or false\n"
00070  "        --remove         Indicates that the property should be removed\n"
00071  "        --help           Show this information and exit\n"
00072  "\n"
00073  "This program attempts to set property for a device. Note that, due to\n"
00074  "security considerations, it may not be possible to set a property; on\n"
00075  "success this program exits with exit code 0. On error, the program exits\n"
00076  "with an exit code different from 0\n" "\n");
00077 }
00078 
00085 int
00086 main (int argc, char *argv[])
00087 {
00088     int rc = 0;
00089     char *udi = NULL;
00090     char *key = NULL;
00091     char *str_value = NULL;
00092     dbus_int32_t int_value = 0;
00093     double double_value = 0.0f;
00094     dbus_bool_t bool_value = TRUE;
00095     dbus_bool_t remove = FALSE;
00096     int type = DBUS_TYPE_NIL;
00097 
00098     fprintf (stderr, "%s " PACKAGE_VERSION "\n", argv[0]);
00099 
00100     if (argc <= 1) {
00101         usage (argc, argv);
00102         return 1;
00103     }
00104 
00105     while (1) {
00106         int c;
00107         int option_index = 0;
00108         const char *opt;
00109         static struct option long_options[] = {
00110             {"udi", 1, NULL, 0},
00111             {"key", 1, NULL, 0},
00112             {"int", 1, NULL, 0},
00113             {"string", 1, NULL, 0},
00114             {"double", 1, NULL, 0},
00115             {"bool", 1, NULL, 0},
00116             {"remove", 0, NULL, 0},
00117             {"help", 0, NULL, 0},
00118             {NULL, 0, NULL, 0}
00119         };
00120 
00121         c = getopt_long (argc, argv, "",
00122                  long_options, &option_index);
00123         if (c == -1)
00124             break;
00125 
00126         switch (c) {
00127         case 0:
00128             opt = long_options[option_index].name;
00129 
00130             if (strcmp (opt, "help") == 0) {
00131                 usage (argc, argv);
00132                 return 0;
00133             } else if (strcmp (opt, "key") == 0) {
00134                 key = strdup (optarg);
00135             } else if (strcmp (opt, "string") == 0) {
00136                 str_value = strdup (optarg);
00137                 type = DBUS_TYPE_STRING;
00138             } else if (strcmp (opt, "int") == 0) {
00139                 int_value = strtol (optarg, NULL, 0);
00140                 type = DBUS_TYPE_INT32;
00141             } else if (strcmp (opt, "double") == 0) {
00142                 double_value = (double) atof (optarg);
00143                 type = DBUS_TYPE_DOUBLE;
00144             } else if (strcmp (opt, "bool") == 0) {
00145                 if (strcmp (optarg, "true") == 0)
00146                     bool_value = TRUE;
00147                 else if (strcmp (optarg, "false") == 0)
00148                     bool_value = FALSE;
00149                 else {
00150                     usage (argc, argv);
00151                     return 1;
00152                 }
00153                 type = DBUS_TYPE_BOOLEAN;
00154             } else if (strcmp (opt, "remove") == 0) {
00155                 remove = TRUE;
00156             } else if (strcmp (opt, "udi") == 0) {
00157                 udi = strdup (optarg);
00158             }
00159             break;
00160 
00161         default:
00162             usage (argc, argv);
00163             return 1;
00164             break;
00165         }
00166     }
00167 
00168     /* must have at least one, but not neither or both */
00169     if ((remove && type != DBUS_TYPE_NIL) ||
00170         ((!remove) && type == DBUS_TYPE_NIL)) {
00171         usage (argc, argv);
00172         return 1;
00173     }
00174 
00175     fprintf (stderr, "\n");
00176 
00177     if (hal_initialize (NULL, FALSE)) {
00178         fprintf (stderr, "error: hal_initialize failed\n");
00179         return 1;
00180     }
00181 
00182     if (remove) {
00183         rc = hal_device_remove_property (udi, key);
00184         if (rc != 0)
00185             return 1;
00186     } else {
00187         switch (type) {
00188         case DBUS_TYPE_STRING:
00189             rc = hal_device_set_property_string (udi, key,
00190                                  str_value);
00191             break;
00192         case DBUS_TYPE_INT32:
00193             rc = hal_device_set_property_int (udi, key,
00194                               int_value);
00195             break;
00196         case DBUS_TYPE_DOUBLE:
00197             rc = hal_device_set_property_double (udi, key,
00198                                  double_value);
00199             break;
00200         case DBUS_TYPE_BOOLEAN:
00201             rc = hal_device_set_property_bool (udi, key,
00202                                bool_value);
00203             break;
00204         }
00205         if (rc != 0)
00206             return 1;
00207     }
00208 
00209     return 0;
00210 }
00211 

Generated on Thu Mar 11 21:32:22 2004 for HAL by doxygen 1.3.6-20040222