minor fix
[oweals/gnunet.git] / src / sensor / gnunet-sensor.c
1 /*
2      This file is part of GNUnet.
3      (C)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file sensor/gnunet-sensor.c
23  * @brief sensor tool
24  * @author Omar Tarabai
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_sensor_service.h"
29
30 static int ret;
31
32 /**
33  * option '-a'
34  */
35 static int get_all;
36
37 /**
38  * option '-g'
39  */
40 static char *get_sensor;
41
42 /*
43  * Handle to sensor service
44  */
45 static struct GNUNET_SENSOR_Handle *sensor_handle;
46
47 /**
48  * Run on shutdown
49  *
50  * @param cls unused
51  * @param tc scheduler context
52  */
53 static void
54 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
55 {
56   if (NULL != sensor_handle)
57   {
58     GNUNET_SENSOR_disconnect (sensor_handle);
59     sensor_handle = NULL;
60   }
61 }
62
63
64 /**
65  * Callback for getting sensor info from service
66  *
67  * @param cls not used
68  * @param sensor brief information about sensor (NULL means end of transmission)
69  * @param err_msg contains error string if any
70  */
71 void
72 print_sensor_info (void *cls, const struct SensorInfoShort *sensor,
73                    const char *err_msg)
74 {
75   if (NULL != err_msg)
76   {
77     printf ("Error: %s\n", err_msg);
78     GNUNET_SCHEDULER_shutdown ();
79     return;
80   }
81   if (NULL == sensor)           /* no more sensors from service */
82     return;
83   printf ("Name: %s\nVersion: %d.%d\n", sensor->name, sensor->version_major,
84           sensor->version_minor);
85   if (NULL != sensor->description)
86     printf ("Description: %s\n", sensor->description);
87   printf ("\n");
88 }
89
90
91 /**
92  * Main function that will be run by the scheduler.
93  *
94  * @param cls closure
95  * @param args remaining command-line arguments
96  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
97  * @param cfg configuration
98  */
99 static void
100 run (void *cls, char *const *args, const char *cfgfile,
101      const struct GNUNET_CONFIGURATION_Handle *cfg)
102 {
103   sensor_handle = NULL;
104   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
105                                 NULL);
106   sensor_handle = GNUNET_SENSOR_connect (cfg);
107   GNUNET_assert (NULL != sensor_handle);
108   if (GNUNET_YES == get_all)
109   {
110     GNUNET_SENSOR_iterate_sensors (sensor_handle, GNUNET_TIME_UNIT_FOREVER_REL,
111                                    NULL, 0, &print_sensor_info, NULL);
112   }
113   else if (NULL != get_sensor)
114   {
115     GNUNET_SENSOR_iterate_sensors (sensor_handle, GNUNET_TIME_UNIT_FOREVER_REL,
116                                    get_sensor, strlen (get_sensor),
117                                    &print_sensor_info, NULL);
118   }
119
120   GNUNET_SCHEDULER_shutdown ();
121   ret = 0;
122 }
123
124
125 /**
126  * The main function to sensor.
127  *
128  * @param argc number of arguments from the command line
129  * @param argv command line arguments
130  * @return 0 ok, 1 on error
131  */
132 int
133 main (int argc, char *const *argv)
134 {
135   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
136     {'a', "all", NULL,
137      gettext_noop ("Retrieve information about all defined sensors"),
138      0, &GNUNET_GETOPT_set_one, &get_all},
139     {'g', "get-sensor", NULL,
140      gettext_noop ("Retrieve information about a single sensor"),
141      1, &GNUNET_GETOPT_set_string, &get_sensor},
142     GNUNET_GETOPT_OPTION_END
143   };
144
145   return (GNUNET_OK ==
146           GNUNET_PROGRAM_run (argc, argv, "gnunet-sensor [options [value]]",
147                               gettext_noop ("sensor"), options, &run,
148                               NULL)) ? ret : 1;
149 }
150
151 /* end of gnunet-sensor.c */