{GPL, other}: Relicense to MIT license.
[collectd.git] / src / onewire.c
index c40d5ad..486e471 100644 (file)
@@ -1,6 +1,6 @@
 /**
- * collectd - src/owfs.c
- * Copyright (C) 2008  Florian octo Forster
+ * collectd - src/onewire.c
+ * Copyright (C) 2008  noris network AG
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
@@ -59,12 +59,14 @@ static ow_family_features_t ow_family_features[] =
 static int ow_family_features_num = STATIC_ARRAY_SIZE (ow_family_features);
 
 static char *device_g = NULL;
+static cdtime_t ow_interval = 0;
 
 static const char *config_keys[] =
 {
   "Device",
   "IgnoreSelected",
   "Sensor",
+  "Interval"
 };
 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
@@ -87,9 +89,7 @@ static int cow_load_config (const char *key, const char *value)
   else if (strcasecmp (key, "IgnoreSelected") == 0)
   {
     ignorelist_set_invert (sensor_list, 1);
-    if ((strcasecmp (value, "True") == 0)
-        || (strcasecmp (value, "Yes") == 0)
-        || (strcasecmp (value, "On") == 0))
+    if (IS_TRUE (value))
       ignorelist_set_invert (sensor_list, 0);
   }
   else if (strcasecmp (key, "Device") == 0)
@@ -104,33 +104,22 @@ static int cow_load_config (const char *key, const char *value)
     sfree (device_g);
     device_g = temp;
   }
-  else
+  else if (strcasecmp ("Interval", key) == 0)
   {
-    return (-1);
+    double tmp;
+    tmp = atof (value);
+    if (tmp > 0.0)
+      ow_interval = DOUBLE_TO_CDTIME_T (tmp);
+    else
+      ERROR ("onewire plugin: Invalid `Interval' setting: %s", value);
   }
-
-  return (0);
-}
-
-static int cow_init (void)
-{
-  int status;
-
-  if (device_g == NULL)
+  else
   {
-    ERROR ("onewire plugin: cow_init: No device configured.");
     return (-1);
   }
 
-  status = (int) OW_init (device_g);
-  if (status != 0)
-  {
-    ERROR ("onewire plugin: OW_init(%s) failed: %i.", device_g, status);
-    return (1);
-  }
-
   return (0);
-} /* int cow_init */
+}
 
 static int cow_read_values (const char *path, const char *name,
     const ow_family_features_t *family_info)
@@ -287,7 +276,7 @@ static int cow_read_bus (const char *path)
   return (0);
 } /* int cow_read_bus */
 
-static int cow_read (void)
+static int cow_read (user_data_t *ud __attribute__((unused)))
 {
   return (cow_read_bus ("/"));
 } /* int cow_read */
@@ -299,11 +288,37 @@ static int cow_shutdown (void)
   return (0);
 } /* int cow_shutdown */
 
+static int cow_init (void)
+{
+  int status;
+  struct timespec cb_interval;
+
+  if (device_g == NULL)
+  {
+    ERROR ("onewire plugin: cow_init: No device configured.");
+    return (-1);
+  }
+
+  status = (int) OW_init (device_g);
+  if (status != 0)
+  {
+    ERROR ("onewire plugin: OW_init(%s) failed: %i.", device_g, status);
+    return (1);
+  }
+
+  CDTIME_T_TO_TIMESPEC (ow_interval, &cb_interval);
+
+  plugin_register_complex_read (/* group = */ NULL, "onewire", cow_read,
+      (ow_interval != 0) ? &cb_interval : NULL,
+      /* user data = */ NULL);
+  plugin_register_shutdown ("onewire", cow_shutdown);
+
+  return (0);
+} /* int cow_init */
+
 void module_register (void)
 {
   plugin_register_init ("onewire", cow_init);
-  plugin_register_read ("onewire", cow_read);
-  plugin_register_shutdown ("onewire", cow_shutdown);
   plugin_register_config ("onewire", cow_load_config,
     config_keys, config_keys_num);
 }