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.5 2004/04/10 16:51: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 
00039 static LibHalContext *hal_ctx;
00040 
00055 static void
00056 usage (int argc, char *argv[])
00057 {
00058     fprintf (stderr,
00059  "\n"
00060  "usage : %s --udi <udi> --key <key>\n"
00061  "           (--int <value> | --string <value> | --bool <value> |"
00062  "            --double <value> | --remove) [--help]\n",
00063          argv[0]);
00064     fprintf (stderr,
00065  "\n" "        --udi            Unique Device Id\n"
00066  "        --key            Key of the property to set\n"
00067  "        --int            Set value to an integer. Accepts decimal and "
00068  "                         hexadecimal prefixed with 0x or x\n"
00069  "        --string         Set value to a string\n"
00070  "        --double         Set value to a floating point number\n"
00071  "        --bool           Set value to a boolean, ie. true or false\n"
00072  "        --remove         Indicates that the property should be removed\n"
00073  "        --help           Show this information and exit\n"
00074  "\n"
00075  "This program attempts to set property for a device. Note that, due to\n"
00076  "security considerations, it may not be possible to set a property; on\n"
00077  "success this program exits with exit code 0. On error, the program exits\n"
00078  "with an exit code different from 0\n" "\n");
00079 }
00080 
00087 int
00088 main (int argc, char *argv[])
00089 {
00090     int rc = 0;
00091     char *udi = NULL;
00092     char *key = NULL;
00093     char *str_value = NULL;
00094     dbus_int32_t int_value = 0;
00095     double double_value = 0.0f;
00096     dbus_bool_t bool_value = TRUE;
00097     dbus_bool_t remove = FALSE;
00098     int type = DBUS_TYPE_NIL;
00099 
00100     fprintf (stderr, "%s " PACKAGE_VERSION "\n", argv[0]);
00101 
00102     if (argc <= 1) {
00103         usage (argc, argv);
00104         return 1;
00105     }
00106 
00107     while (1) {
00108         int c;
00109         int option_index = 0;
00110         const char *opt;
00111         static struct option long_options[] = {
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         case 0:
00130             opt = long_options[option_index].name;
00131 
00132             if (strcmp (opt, "help") == 0) {
00133                 usage (argc, argv);
00134                 return 0;
00135             } else if (strcmp (opt, "key") == 0) {
00136                 key = strdup (optarg);
00137             } else if (strcmp (opt, "string") == 0) {
00138                 str_value = strdup (optarg);
00139                 type = DBUS_TYPE_STRING;
00140             } else if (strcmp (opt, "int") == 0) {
00141                 int_value = strtol (optarg, NULL, 0);
00142                 type = DBUS_TYPE_INT32;
00143             } else if (strcmp (opt, "double") == 0) {
00144                 double_value = (double) atof (optarg);
00145                 type = DBUS_TYPE_DOUBLE;
00146             } else if (strcmp (opt, "bool") == 0) {
00147                 if (strcmp (optarg, "true") == 0)
00148                     bool_value = TRUE;
00149                 else if (strcmp (optarg, "false") == 0)
00150                     bool_value = FALSE;
00151                 else {
00152                     usage (argc, argv);
00153                     return 1;
00154                 }
00155                 type = DBUS_TYPE_BOOLEAN;
00156             } else if (strcmp (opt, "remove") == 0) {
00157                 remove = TRUE;
00158             } else if (strcmp (opt, "udi") == 0) {
00159                 udi = strdup (optarg);
00160             }
00161             break;
00162 
00163         default:
00164             usage (argc, argv);
00165             return 1;
00166             break;
00167         }
00168     }
00169 
00170     /* must have at least one, but not neither or both */
00171     if ((remove && type != DBUS_TYPE_NIL) ||
00172         ((!remove) && type == DBUS_TYPE_NIL)) {
00173         usage (argc, argv);
00174         return 1;
00175     }
00176 
00177     fprintf (stderr, "\n");
00178 
00179     if ((hal_ctx = hal_initialize (NULL, FALSE)) == NULL) {
00180         fprintf (stderr, "error: hal_initialize failed\n");
00181         return 1;
00182     }
00183 
00184     if (remove) {
00185         rc = hal_device_remove_property (hal_ctx, udi, key);
00186         if (rc != 0)
00187             return 1;
00188     } else {
00189         switch (type) {
00190         case DBUS_TYPE_STRING:
00191             rc = hal_device_set_property_string (hal_ctx, udi, key,
00192                                  str_value);
00193             break;
00194         case DBUS_TYPE_INT32:
00195             rc = hal_device_set_property_int (hal_ctx, udi, key,
00196                               int_value);
00197             break;
00198         case DBUS_TYPE_DOUBLE:
00199             rc = hal_device_set_property_double (hal_ctx, udi, key,
00200                                  double_value);
00201             break;
00202         case DBUS_TYPE_BOOLEAN:
00203             rc = hal_device_set_property_bool (hal_ctx, udi, key,
00204                                bool_value);
00205             break;
00206         }
00207         if (rc != 0)
00208             return 1;
00209     }
00210 
00211     return 0;
00212 }
00213 

Generated on Sat Apr 24 19:57:44 2004 for HAL by doxygen 1.3.6-20040222