aboutsummaryrefslogtreecommitdiff
path: root/src/sensor/gnunet-service-sensor_analysis.c
blob: 6412031cb06db043a49802872a5b84cf0e5452b2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
     This file is part of GNUnet.
     Copyright (C)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file sensor/gnunet-service-sensor_analysis.c
 * @brief sensor service analysis functionality
 * @author Omar Tarabai
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "sensor.h"
#include "gnunet_peerstore_service.h"
#include "gnunet_sensor_model_plugin.h"

#define LOG(kind,...) GNUNET_log_from (kind, "sensor-analysis",__VA_ARGS__)

/**
 * Carries information about the analysis model
 * corresponding to one sensor
 */
struct SensorModel
{

  /**
   * DLL
   */
  struct SensorModel *prev;

  /**
   * DLL
   */
  struct SensorModel *next;

  /**
   * Pointer to sensor info structure
   */
  struct GNUNET_SENSOR_SensorInfo *sensor;

  /**
   * Watcher of sensor values
   */
  struct GNUNET_PEERSTORE_WatchContext *wc;

  /**
   * State of sensor. #GNUNET_YES if anomalous, #GNUNET_NO otherwise.
   */
  int anomalous;

  /**
   * Number of anomalous readings (positive) received in a row.
   */
  int positive_count;

  /**
   * Number of non-anomalous (negative) readings received in a row.
   */
  int negative_count;

  /**
   * Closure for model plugin.
   * Usually, the instance of the model created for this sensor.
   */
  void *cls;

};

/**
 * Our configuration.
 */
static const struct GNUNET_CONFIGURATION_Handle *cfg;

/**
 * Hashmap of loaded sensors
 */
static struct GNUNET_CONTAINER_MultiHashMap *sensors;

/*
 * Model library name
 */
static char *model_lib_name;

/**
 * Model handle
 */
static struct GNUNET_SENSOR_ModelFunctions *model_api;

/**
 * Handle to peerstore service
 */
static struct GNUNET_PEERSTORE_Handle *peerstore;

/**
 * Head of DLL of created models
 */
static struct SensorModel *models_head;

/**
 * Tail of DLL of created models
 */
static struct SensorModel *models_tail;

/**
 * My peer id
 */
static struct GNUNET_PeerIdentity peerid;

/**
 * How many subsequent values required to flip anomaly label.
 * E.g. After 3 subsequent anomaly reports, status change to anomalous.
 */
static unsigned long long confirmation_count;

/**
 * Destroy a created model
 */
static void
destroy_sensor_model (struct SensorModel *sensor_model)
{
  GNUNET_assert (NULL != sensor_model);
  LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying sensor model for `%s'.\n",
       sensor_model->sensor->name);
  if (NULL != sensor_model->wc)
  {
    GNUNET_PEERSTORE_watch_cancel (sensor_model->wc);
    sensor_model->wc = NULL;
  }
  if (NULL != sensor_model->cls)
  {
    model_api->destroy_model (sensor_model->cls);
    sensor_model->cls = NULL;
  }
  GNUNET_free (sensor_model);
  sensor_model = NULL;
}


/**
 * Stop the sensor analysis module
 */
void
SENSOR_analysis_stop ()
{
  struct SensorModel *sm;

  LOG (GNUNET_ERROR_TYPE_DEBUG, "Stopping sensor analysis module.\n");
  while (NULL != models_head)
  {
    sm = models_head;
    GNUNET_CONTAINER_DLL_remove (models_head, models_tail, sm);
    destroy_sensor_model (sm);
  }
  if (NULL != peerstore)
  {
    GNUNET_PEERSTORE_disconnect (peerstore, GNUNET_YES);
    peerstore = NULL;
  }
  if (NULL != model_api)
  {
    GNUNET_break (NULL == GNUNET_PLUGIN_unload (model_lib_name, model_api));
    GNUNET_free (model_lib_name);
    model_lib_name = NULL;
  }
}


/**
 * Sensor value watch callback
 *
 * @param cls Sensor model struct
 * @param record Received record from peerstore, should contain new sensor value
 * @param emsg Error message from peerstore if any, NULL if no errors
 * @return #GNUNET_YES
 */
static int
sensor_watcher (void *cls,
                const struct GNUNET_PEERSTORE_Record *record,
                const char *emsg)
{
  struct SensorModel *model = cls;
  double *val;
  int anomalous;

  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Received a sensor value, will feed to sensor model.\n");
  if (sizeof (double) != record->value_size)
  {
    LOG (GNUNET_ERROR_TYPE_ERROR, _("Received an invalid sensor value."));
    return GNUNET_YES;
  }
  val = (double *) (record->value);
  anomalous = model_api->feed_model (model->cls, *val);
  if (GNUNET_YES == anomalous)
  {
    model->positive_count++;
    model->negative_count = 0;
    if (GNUNET_NO == model->anomalous &&
        model->positive_count >= confirmation_count)
    {
      model->anomalous = GNUNET_YES;
      LOG (GNUNET_ERROR_TYPE_WARNING,
           "Anomaly state started for sensor `%s', value: %f.\n",
           model->sensor->name, val);
      SENSOR_reporting_anomaly_update (model->sensor, model->anomalous);
    }
  }
  else
  {
    model->negative_count++;
    model->positive_count = 0;
    if (GNUNET_YES == model->anomalous &&
        model->negative_count >= confirmation_count)
    {
      model->anomalous = GNUNET_NO;
      LOG (GNUNET_ERROR_TYPE_INFO,
          "Anomaly state stopped for sensor `%s', value: %f.\n",
           model->sensor->name, val);
      SENSOR_reporting_anomaly_update (model->sensor, model->anomalous);
    }
  }
  return GNUNET_YES;
}


/**
 * Iterator for defined sensors
 * Creates sensor model for numeric sensors
 *
 * @param cls unused
 * @param key unused
 * @param value a 'struct GNUNET_SENSOR_SensorInfo *' with sensor information
 * @return #GNUNET_YES to continue iterations
 */
static int
init_sensor_model (void *cls, const struct GNUNET_HashCode *key, void *value)
{
  struct GNUNET_SENSOR_SensorInfo *sensor = value;
  struct SensorModel *sensor_model;

  if (0 != strcmp ("numeric", sensor->expected_datatype))
    return GNUNET_YES;
  sensor_model = GNUNET_new (struct SensorModel);
  sensor_model->sensor = sensor;
  sensor_model->wc =
      GNUNET_PEERSTORE_watch (peerstore, "sensor", &peerid, sensor->name,
                              &sensor_watcher, sensor_model);
  sensor_model->anomalous = GNUNET_NO;
  sensor_model->positive_count = 0;
  sensor_model->negative_count = 0;
  sensor_model->cls = model_api->create_model (model_api->cls);
  GNUNET_CONTAINER_DLL_insert (models_head, models_tail, sensor_model);
  LOG (GNUNET_ERROR_TYPE_DEBUG, "Created sensor model for `%s'.\n",
       sensor->name);
  return GNUNET_YES;
}


/**
 * Start the sensor analysis module
 *
 * @param c our service configuration
 * @param sensors multihashmap of loaded sensors
 * @return #GNUNET_OK if started successfully, #GNUNET_SYSERR otherwise
 */
int
SENSOR_analysis_start (const struct GNUNET_CONFIGURATION_Handle *c,
                       struct GNUNET_CONTAINER_MultiHashMap *s)
{
  char *model_name;

  GNUNET_assert (NULL != s);
  cfg = c;
  sensors = s;
  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_string (cfg, "sensor-analysis", "MODEL",
                                             &model_name))
  {
    LOG (GNUNET_ERROR_TYPE_ERROR,
         _("Analysis model not defined in configuration.\n"));
    return GNUNET_SYSERR;
  }
  GNUNET_asprintf (&model_lib_name, "libgnunet_plugin_sensor_model_%s",
                   model_name);
  model_api = GNUNET_PLUGIN_load (model_lib_name, (void *) cfg);
  GNUNET_free (model_name);
  if (NULL == model_api)
  {
    LOG (GNUNET_ERROR_TYPE_ERROR, _("Could not load analysis model `%s'.\n"),
         model_lib_name);
    return GNUNET_SYSERR;
  }
  peerstore = GNUNET_PEERSTORE_connect (cfg);
  if (NULL == peerstore)
  {
    LOG (GNUNET_ERROR_TYPE_ERROR,
         _("Could not connect to peerstore service.\n"));
    SENSOR_analysis_stop ();
    return GNUNET_SYSERR;
  }
  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_number (cfg, "sensor-analysis",
                                             "CONFIRMATION_COUNT",
                                             &confirmation_count))
    confirmation_count = 1;
  GNUNET_CRYPTO_get_peer_identity (cfg, &peerid);
  GNUNET_CONTAINER_multihashmap_iterate (sensors, &init_sensor_model, NULL);
  return GNUNET_OK;
}

/* end of gnunet-service-sensor_analysis.c */