aboutsummaryrefslogtreecommitdiff
path: root/src/sensor/sensor_util_lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/sensor/sensor_util_lib.c')
-rw-r--r--src/sensor/sensor_util_lib.c179
1 files changed, 59 insertions, 120 deletions
diff --git a/src/sensor/sensor_util_lib.c b/src/sensor/sensor_util_lib.c
index 40cdfef30..b77ba48fc 100644
--- a/src/sensor/sensor_util_lib.c
+++ b/src/sensor/sensor_util_lib.c
@@ -87,10 +87,10 @@ version_parse(char *version, uint16_t *major, uint16_t *minor)
87 * @param cfg configuration handle 87 * @param cfg configuration handle
88 * @param sectionname configuration section containing definition 88 * @param sectionname configuration section containing definition
89 */ 89 */
90static struct SensorInfo * 90static struct GNUNET_SENSOR_SensorInfo *
91load_sensor_from_cfg(struct GNUNET_CONFIGURATION_Handle *cfg, const char *sectionname) 91load_sensor_from_cfg(struct GNUNET_CONFIGURATION_Handle *cfg, const char *sectionname)
92{ 92{
93 struct SensorInfo *sensor; 93 struct GNUNET_SENSOR_SensorInfo *sensor;
94 char *version_str; 94 char *version_str;
95 char *starttime_str; 95 char *starttime_str;
96 char *endtime_str; 96 char *endtime_str;
@@ -98,7 +98,7 @@ load_sensor_from_cfg(struct GNUNET_CONFIGURATION_Handle *cfg, const char *sectio
98 char *dummy; 98 char *dummy;
99 struct GNUNET_CRYPTO_EddsaPublicKey public_key; 99 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
100 100
101 sensor = GNUNET_new(struct SensorInfo); 101 sensor = GNUNET_new(struct GNUNET_SENSOR_SensorInfo);
102 //name 102 //name
103 sensor->name = GNUNET_strdup(sectionname); 103 sensor->name = GNUNET_strdup(sectionname);
104 //version 104 //version
@@ -260,12 +260,12 @@ load_sensor_from_cfg(struct GNUNET_CONFIGURATION_Handle *cfg, const char *sectio
260 * 260 *
261 * @param filename full path to file containing sensor definition 261 * @param filename full path to file containing sensor definition
262 */ 262 */
263static struct SensorInfo * 263static struct GNUNET_SENSOR_SensorInfo *
264load_sensor_from_file(const char *filename) 264load_sensor_from_file(const char *filename)
265{ 265{
266 struct GNUNET_CONFIGURATION_Handle *sensorcfg; 266 struct GNUNET_CONFIGURATION_Handle *sensorcfg;
267 const char *filebasename; 267 const char *filebasename;
268 struct SensorInfo *sensor; 268 struct GNUNET_SENSOR_SensorInfo *sensor;
269 269
270 //test file 270 //test file
271 if(GNUNET_YES != GNUNET_DISK_file_test(filename)) 271 if(GNUNET_YES != GNUNET_DISK_file_test(filename))
@@ -303,9 +303,10 @@ load_sensor_from_file(const char *filename)
303 * @return 1: s1 > s2, 0: s1 == s2, -1: s1 < s2 303 * @return 1: s1 > s2, 0: s1 == s2, -1: s1 < s2
304 */ 304 */
305static int 305static int
306sensor_version_compare(struct SensorInfo *s1, struct SensorInfo *s2) 306sensor_version_compare (struct GNUNET_SENSOR_SensorInfo *s1,
307 struct GNUNET_SENSOR_SensorInfo *s2)
307{ 308{
308 if(s1->version_major == s2->version_major) 309 if (s1->version_major == s2->version_major)
309 return (s1->version_minor < s2->version_minor) ? -1 : (s1->version_minor > s2->version_minor); 310 return (s1->version_minor < s2->version_minor) ? -1 : (s1->version_minor > s2->version_minor);
310 else 311 else
311 return (s1->version_major < s2->version_major) ? -1 : (s1->version_major > s2->version_major); 312 return (s1->version_major < s2->version_major) ? -1 : (s1->version_major > s2->version_major);
@@ -317,13 +318,15 @@ sensor_version_compare(struct SensorInfo *s1, struct SensorInfo *s2)
317 * 318 *
318 * @param sensor Sensor structure to add 319 * @param sensor Sensor structure to add
319 * @param map Hashmap to add to 320 * @param map Hashmap to add to
320 * @return #GNUNET_YES if added, #GNUNET_NO if not added which is not necessarily an error 321 * @return #GNUNET_YES if added
322 * #GNUNET_NO if not added which is not necessarily an error
321 */ 323 */
322static int 324static int
323add_sensor_to_hashmap(struct SensorInfo *sensor, struct GNUNET_CONTAINER_MultiHashMap *map) 325add_sensor_to_hashmap (struct GNUNET_SENSOR_SensorInfo *sensor,
326 struct GNUNET_CONTAINER_MultiHashMap *map)
324{ 327{
325 struct GNUNET_HashCode key; 328 struct GNUNET_HashCode key;
326 struct SensorInfo *existing; 329 struct GNUNET_SENSOR_SensorInfo *existing;
327 330
328 GNUNET_CRYPTO_hash(sensor->name, strlen(sensor->name) + 1, &key); 331 GNUNET_CRYPTO_hash(sensor->name, strlen(sensor->name) + 1, &key);
329 existing = GNUNET_CONTAINER_multihashmap_get(map, &key); 332 existing = GNUNET_CONTAINER_multihashmap_get(map, &key);
@@ -331,19 +334,25 @@ add_sensor_to_hashmap(struct SensorInfo *sensor, struct GNUNET_CONTAINER_MultiHa
331 { 334 {
332 if(sensor_version_compare(existing, sensor) >= 0) //same or newer version already exist 335 if(sensor_version_compare(existing, sensor) >= 0) //same or newer version already exist
333 { 336 {
334 LOG (GNUNET_ERROR_TYPE_INFO, _("Sensor `%s' already exists with same or newer version\n"), sensor->name); 337 LOG (GNUNET_ERROR_TYPE_INFO,
338 _("Sensor `%s' already exists with same or newer version\n"),
339 sensor->name);
335 return GNUNET_NO; 340 return GNUNET_NO;
336 } 341 }
337 else 342 else
338 { 343 {
339 GNUNET_CONTAINER_multihashmap_remove(map, &key, existing); //remove the old version 344 GNUNET_CONTAINER_multihashmap_remove(map, &key, existing); //remove the old version
340 GNUNET_free(existing); 345 GNUNET_free(existing);
341 LOG (GNUNET_ERROR_TYPE_INFO, "Upgrading sensor `%s' to a newer version\n", sensor->name); 346 LOG (GNUNET_ERROR_TYPE_INFO,
347 "Upgrading sensor `%s' to a newer version\n",
348 sensor->name);
342 } 349 }
343 } 350 }
344 if(GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put(map, &key, sensor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) 351 if(GNUNET_SYSERR == GNUNET_CONTAINER_multihashmap_put(map, &key, sensor, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
345 { 352 {
346 LOG (GNUNET_ERROR_TYPE_ERROR, _("Error adding new sensor `%s' to global hashmap, this should not happen\n"), sensor->name); 353 LOG (GNUNET_ERROR_TYPE_ERROR,
354 _("Error adding new sensor `%s' to global hashmap.\n"),
355 sensor->name);
347 return GNUNET_NO; 356 return GNUNET_NO;
348 } 357 }
349 358
@@ -361,7 +370,7 @@ static int
361reload_sensors_dir_cb(void *cls, const char *filename) 370reload_sensors_dir_cb(void *cls, const char *filename)
362{ 371{
363 struct GNUNET_CONTAINER_MultiHashMap *sensors = cls; 372 struct GNUNET_CONTAINER_MultiHashMap *sensors = cls;
364 struct SensorInfo *sensor; 373 struct GNUNET_SENSOR_SensorInfo *sensor;
365 374
366 if(GNUNET_YES != GNUNET_DISK_file_test(filename)) 375 if(GNUNET_YES != GNUNET_DISK_file_test(filename))
367 return GNUNET_OK; 376 return GNUNET_OK;
@@ -423,131 +432,61 @@ GNUNET_SENSOR_load_all_sensors ()
423 return sensors; 432 return sensors;
424} 433}
425 434
426/**
427 * Parses a sensor reading message struct
428 *
429 * @param msg message header received
430 * @param sensors multihashmap of loaded sensors
431 * @return sensor reading struct or NULL if error
432 */
433struct GNUNET_SENSOR_Reading *
434GNUNET_SENSOR_parse_reading_message (const struct GNUNET_MessageHeader *msg,
435 struct GNUNET_CONTAINER_MultiHashMap *sensors)
436{
437 uint16_t msg_size;
438 struct GNUNET_SENSOR_ReadingMessage *rm;
439 uint16_t sensorname_size;
440 uint16_t value_size;
441 void *dummy;
442 char *sensorname;
443 struct GNUNET_HashCode key;
444 struct SensorInfo *sensor;
445 struct GNUNET_SENSOR_Reading *reading;
446
447 msg_size = ntohs (msg->size);
448 if (msg_size < sizeof (struct GNUNET_SENSOR_ReadingMessage))
449 {
450 LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid reading message size.\n");
451 return NULL;
452 }
453 rm = (struct GNUNET_SENSOR_ReadingMessage *)msg;
454 sensorname_size = ntohs (rm->sensorname_size);
455 value_size = ntohs (rm->value_size);
456 if ((sizeof (struct GNUNET_SENSOR_ReadingMessage)
457 + sensorname_size + value_size) != msg_size)
458 {
459 LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid reading message size.\n");
460 return NULL;
461 }
462 dummy = &rm[1];
463 sensorname = GNUNET_malloc (sensorname_size);
464 memcpy (sensorname, dummy, sensorname_size);
465 GNUNET_CRYPTO_hash(sensorname, sensorname_size, &key);
466 GNUNET_free (sensorname);
467 sensor = GNUNET_CONTAINER_multihashmap_get (sensors, &key);
468 if (NULL == sensor)
469 {
470 LOG (GNUNET_ERROR_TYPE_WARNING,
471 "Unknown sensor name in reading message.\n");
472 return NULL;
473 }
474 if ((sensor->version_minor != ntohs (rm->sensorversion_minor)) ||
475 (sensor->version_major != ntohs (rm->sensorversion_major)))
476 {
477 LOG (GNUNET_ERROR_TYPE_WARNING,
478 "Sensor version mismatch in reading message.\n");
479 return NULL;
480 }
481 if (0 == strcmp (sensor->expected_datatype, "numeric") &&
482 sizeof (double) != value_size)
483 {
484 LOG (GNUNET_ERROR_TYPE_WARNING,
485 "Invalid value size for a numerical sensor.\n");
486 return NULL;
487 }
488 reading = GNUNET_new (struct GNUNET_SENSOR_Reading);
489 reading->sensor = sensor;
490 reading->timestamp = GNUNET_be64toh (rm->timestamp);
491 reading->value_size = value_size;
492 reading->value = GNUNET_malloc (value_size);
493 dummy += sensorname_size;
494 memcpy (reading->value, dummy, value_size);
495 return reading;
496}
497 435
498/** 436/**
499 * Remove sensor execution from scheduler 437 * Remove sensor execution from scheduler
500 * 438 *
501 * @param cls unused 439 * @param cls unused
502 * @param key hash of sensor name, key to hashmap 440 * @param key hash of sensor name, key to hashmap
503 * @param value a 'struct SensorInfo *' 441 * @param value a `struct GNUNET_SENSOR_SensorInfo *`
504 * @return #GNUNET_YES if we should continue to 442 * @return #GNUNET_YES if we should continue to
505 * iterate, 443 * iterate,
506 * #GNUNET_NO if not. 444 * #GNUNET_NO if not.
507 */ 445 */
508static int destroy_sensor(void *cls, 446static int destroy_sensor(void *cls,
509 const struct GNUNET_HashCode *key, void *value) 447 const struct GNUNET_HashCode *key,
448 void *value)
510{ 449{
511 struct SensorInfo *sensorinfo = value; 450 struct GNUNET_SENSOR_SensorInfo *sensor = value;
512 451
513 if(GNUNET_SCHEDULER_NO_TASK != sensorinfo->execution_task) 452 if(GNUNET_SCHEDULER_NO_TASK != sensor->execution_task)
514 { 453 {
515 GNUNET_SCHEDULER_cancel(sensorinfo->execution_task); 454 GNUNET_SCHEDULER_cancel(sensor->execution_task);
516 sensorinfo->execution_task = GNUNET_SCHEDULER_NO_TASK; 455 sensor->execution_task = GNUNET_SCHEDULER_NO_TASK;
517 } 456 }
518 if(NULL != sensorinfo->gnunet_stat_get_handle) 457 if(NULL != sensor->gnunet_stat_get_handle)
519 { 458 {
520 GNUNET_STATISTICS_get_cancel(sensorinfo->gnunet_stat_get_handle); 459 GNUNET_STATISTICS_get_cancel(sensor->gnunet_stat_get_handle);
521 sensorinfo->gnunet_stat_get_handle = NULL; 460 sensor->gnunet_stat_get_handle = NULL;
522 } 461 }
523 if(NULL != sensorinfo->ext_cmd) 462 if(NULL != sensor->ext_cmd)
524 { 463 {
525 GNUNET_OS_command_stop(sensorinfo->ext_cmd); 464 GNUNET_OS_command_stop(sensor->ext_cmd);
526 sensorinfo->ext_cmd = NULL; 465 sensor->ext_cmd = NULL;
527 } 466 }
528 if(NULL != sensorinfo->cfg) 467 if(NULL != sensor->cfg)
529 GNUNET_CONFIGURATION_destroy(sensorinfo->cfg); 468 GNUNET_CONFIGURATION_destroy(sensor->cfg);
530 if(NULL != sensorinfo->name) 469 if(NULL != sensor->name)
531 GNUNET_free(sensorinfo->name); 470 GNUNET_free (sensor->name);
532 if(NULL != sensorinfo->def_file) 471 if(NULL != sensor->def_file)
533 GNUNET_free(sensorinfo->def_file); 472 GNUNET_free (sensor->def_file);
534 if(NULL != sensorinfo->description) 473 if(NULL != sensor->description)
535 GNUNET_free(sensorinfo->description); 474 GNUNET_free (sensor->description);
536 if(NULL != sensorinfo->category) 475 if(NULL != sensor->category)
537 GNUNET_free(sensorinfo->category); 476 GNUNET_free (sensor->category);
538 if(NULL != sensorinfo->capabilities) 477 if(NULL != sensor->capabilities)
539 GNUNET_free(sensorinfo->capabilities); 478 GNUNET_free (sensor->capabilities);
540 if(NULL != sensorinfo->gnunet_stat_service) 479 if(NULL != sensor->gnunet_stat_service)
541 GNUNET_free(sensorinfo->gnunet_stat_service); 480 GNUNET_free (sensor->gnunet_stat_service);
542 if(NULL != sensorinfo->gnunet_stat_name) 481 if(NULL != sensor->gnunet_stat_name)
543 GNUNET_free(sensorinfo->gnunet_stat_name); 482 GNUNET_free (sensor->gnunet_stat_name);
544 if(NULL != sensorinfo->ext_process) 483 if(NULL != sensor->ext_process)
545 GNUNET_free(sensorinfo->ext_process); 484 GNUNET_free (sensor->ext_process);
546 if(NULL != sensorinfo->ext_args) 485 if(NULL != sensor->ext_args)
547 GNUNET_free(sensorinfo->ext_args); 486 GNUNET_free (sensor->ext_args);
548 if (NULL != sensorinfo->collection_point) 487 if (NULL != sensor->collection_point)
549 GNUNET_free (sensorinfo->collection_point); 488 GNUNET_free (sensor->collection_point);
550 GNUNET_free(sensorinfo); 489 GNUNET_free (sensor);
551 return GNUNET_YES; 490 return GNUNET_YES;
552} 491}
553 492