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