sensor: unified numeric datatypes
[oweals/gnunet.git] / src / sensor / gnunet-service-sensor-analysis.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-service-sensor-analysis.c
23  * @brief sensor service analysis functionality
24  * @author Omar Tarabai
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "sensor.h"
29 #include "gnunet_peerstore_service.h"
30
31 #define LOG(kind,...) GNUNET_log_from (kind, "sensor-analysis",__VA_ARGS__)
32
33 /*
34  * Carries information about the analysis model
35  * corresponding to one sensor
36  */
37 struct SensorModel
38 {
39
40   /*
41    * DLL
42    */
43   struct SensorModel *prev;
44
45   /*
46    * DLL
47    */
48   struct SensorModel *next;
49
50   /*
51    * Pointer to sensor info structure
52    */
53   struct SensorInfo *sensor;
54
55   /*
56    * Watcher of sensor values
57    */
58   struct GNUNET_PEERSTORE_WatchContext *wc;
59
60 };
61
62 /**
63  * Our configuration.
64  */
65 static const struct GNUNET_CONFIGURATION_Handle *cfg;
66
67 /*
68  * Model library name
69  */
70 static char *model_lib_name;
71
72 /*
73  * Model handle
74  */
75 static struct GNUNET_SENSOR_ModelFunctions *model_api;
76
77 /*
78  * Handle to peerstore service
79  */
80 static struct GNUNET_PEERSTORE_Handle *peerstore;
81
82 /*
83  * Head of DLL of created models
84  */
85 static struct SensorModel *models_head;
86
87 /*
88  * Tail of DLL of created models
89  */
90 static struct SensorModel *models_tail;
91
92 /**
93  * My peer id
94  */
95 struct GNUNET_PeerIdentity peerid;
96
97 /*
98  * Destroy a created model
99  */
100 static void
101 destroy_sensor_model (struct SensorModel *sensor_model)
102 {
103   GNUNET_assert (NULL != sensor_model);
104   LOG (GNUNET_ERROR_TYPE_DEBUG,
105         "Destroying sensor model for `%s'.\n",
106         sensor_model->sensor->name);
107   if (NULL != sensor_model->wc)
108   {
109     GNUNET_PEERSTORE_watch_cancel(sensor_model->wc);
110     sensor_model->wc = NULL;
111   }
112   sensor_model = NULL;
113 }
114
115 /*
116  * Stop the sensor analysis module
117  */
118 void SENSOR_analysis_stop()
119 {
120   struct SensorModel *sm;
121
122   LOG (GNUNET_ERROR_TYPE_DEBUG, "Stopping sensor analysis module.\n");
123   if (NULL != model_api)
124   {
125     GNUNET_break (NULL == GNUNET_PLUGIN_unload (model_lib_name, model_api));
126     GNUNET_free (model_lib_name);
127     model_lib_name = NULL;
128   }
129   while (NULL != models_head)
130   {
131     sm = models_head;
132     destroy_sensor_model(sm);
133     GNUNET_CONTAINER_DLL_remove(models_head, models_tail, sm);
134   }
135   if (NULL != peerstore)
136   {
137     GNUNET_PEERSTORE_disconnect(peerstore);
138     peerstore = NULL;
139   }
140 }
141
142 /*
143  * Sensor value watch callback
144  */
145 static int
146 sensor_watcher (void *cls,
147     struct GNUNET_PEERSTORE_Record *record,
148     char *emsg)
149 {
150   LOG (GNUNET_ERROR_TYPE_DEBUG,
151       "Received a sensor value, will feed to sensor model.\n");
152   return GNUNET_YES;
153 }
154
155 /*
156  * Iterator for defined sensors
157  * Creates sensor model for numeric sensors
158  *
159  * @param cls unused
160  * @param key unused
161  * @param value a 'struct SensorInfo *' with sensor information
162  * @return #GNUNET_YES to continue iterations
163  */
164 static int
165 init_sensor_model (void *cls,
166     const struct GNUNET_HashCode *key,
167     void *value)
168 {
169   struct SensorInfo *sensor = value;
170   struct SensorModel *sensor_model;
171
172   if (0 != strcmp("numeric", sensor->expected_datatype))
173     return GNUNET_YES;
174   sensor_model = GNUNET_new(struct SensorModel);
175   sensor_model->sensor = sensor;
176   sensor_model->wc = GNUNET_PEERSTORE_watch(peerstore,
177           "sensor", &peerid, sensor->name,
178           &sensor_watcher, sensor_model);
179   GNUNET_CONTAINER_DLL_insert(models_head, models_tail, sensor_model);
180   LOG (GNUNET_ERROR_TYPE_DEBUG,
181       "Created sensor model for `%s'.\n", sensor->name);
182   return GNUNET_YES;
183 }
184
185 /*
186  * Start the sensor analysis module
187  *
188  * @param c our service configuration
189  * @param sensors multihashmap of loaded sensors
190  * @return #GNUNET_OK if started successfully, #GNUNET_SYSERR otherwise
191  */
192 int
193 SENSOR_analysis_start(const struct GNUNET_CONFIGURATION_Handle *c,
194     struct GNUNET_CONTAINER_MultiHashMap *sensors)
195 {
196   char *model_name;
197
198   GNUNET_assert(NULL != sensors);
199   cfg = c;
200   if (GNUNET_OK !=
201       GNUNET_CONFIGURATION_get_value_string (cfg, "sensor-analysis", "MODEL",
202                                                  &model_name))
203   {
204     LOG (GNUNET_ERROR_TYPE_ERROR, _("Analysis model not defined in configuration.\n"));
205     return GNUNET_SYSERR;
206   }
207   GNUNET_asprintf (&model_lib_name, "libgnunet_plugin_sensor_model_%s", model_name);
208   model_api = GNUNET_PLUGIN_load(model_lib_name, (void *) cfg);
209   GNUNET_free(model_name);
210   if(NULL == model_api)
211   {
212     LOG (GNUNET_ERROR_TYPE_ERROR, _("Could not load analysis model `%s'.\n"), model_lib_name);
213     return GNUNET_SYSERR;
214   }
215   peerstore = GNUNET_PEERSTORE_connect(cfg);
216   if (NULL == peerstore)
217   {
218     LOG (GNUNET_ERROR_TYPE_ERROR, _("Could not connect to peerstore service.\n"));
219     SENSOR_analysis_stop();
220     return GNUNET_SYSERR;
221   }
222   GNUNET_CRYPTO_get_peer_identity(cfg, &peerid);
223   GNUNET_CONTAINER_multihashmap_iterate(sensors, &init_sensor_model, NULL);
224
225   return GNUNET_OK;
226 }
227
228 /* end of gnunet-service-sensor-analysis.c */