getting single sensor information
[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 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,
55          const struct GNUNET_SCHEDULER_TaskContext *tc)
56 {
57   if(NULL != sensor_handle)
58   {
59     GNUNET_SENSOR_disconnect(sensor_handle);
60     sensor_handle = NULL;
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 print_sensor_info(void *cls,
72     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   {
83     GNUNET_SCHEDULER_shutdown();
84     return;
85   }
86   printf("Name: %s\nVersion: %d.%d\n",
87       sensor->name,
88       sensor->version_major,
89       sensor->version_minor);
90   if(NULL != sensor->description)
91     printf("Description: %s\n", sensor->description);
92   printf("\n");
93 }
94
95 /**
96  * Main function that will be run by the scheduler.
97  *
98  * @param cls closure
99  * @param args remaining command-line arguments
100  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
101  * @param cfg configuration
102  */
103 static void
104 run (void *cls,
105      char *const *args,
106      const char *cfgfile,
107      const struct GNUNET_CONFIGURATION_Handle *cfg)
108 {
109
110   sensor_handle = NULL;
111   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
112                                   &shutdown_task,
113                                   NULL);
114   sensor_handle = GNUNET_SENSOR_connect(cfg);
115   GNUNET_assert(NULL != sensor_handle);
116   if(GNUNET_YES == get_all)
117   {
118     GNUNET_SENSOR_iterate_sensors(sensor_handle,
119         GNUNET_TIME_UNIT_FOREVER_REL,
120         NULL,
121         0,
122         &print_sensor_info,
123         NULL);
124   }
125   else if(NULL != get_sensor)
126   {
127     GNUNET_SENSOR_iterate_sensors(sensor_handle,
128         GNUNET_TIME_UNIT_FOREVER_REL,
129         get_sensor,
130         strlen(get_sensor),
131         &print_sensor_info,
132         NULL);
133   }
134
135   ret = 0;
136 }
137
138 /**
139  * The main function to sensor.
140  *
141  * @param argc number of arguments from the command line
142  * @param argv command line arguments
143  * @return 0 ok, 1 on error
144  */
145 int
146 main (int argc, char *const *argv)
147 {
148   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
149       {'a', "all", NULL,
150           gettext_noop("Retrieve information about all defined sensors"),
151       0, &GNUNET_GETOPT_set_one, &get_all},
152       {'g', "get-sensor", NULL,
153           gettext_noop("Retrieve information about a single sensor"),
154       1, &GNUNET_GETOPT_set_string, &get_sensor},
155     GNUNET_GETOPT_OPTION_END
156   };
157   return (GNUNET_OK ==
158           GNUNET_PROGRAM_run (argc,
159                               argv,
160                               "gnunet-sensor [options [value]]",
161                               gettext_noop
162                               ("sensor"),
163                               options, &run, NULL)) ? ret : 1;
164 }
165
166 /* end of gnunet-sensor.c */