Various plugins: Use the IS_TRUE and IS_FALSE macros everywhere.
[collectd.git] / src / onewire.c
index 2b670de..cae0d63 100644 (file)
 
 #include <owcapi.h>
 
+#define OW_FAMILY_LENGTH 8
 #define OW_FAMILY_MAX_FEATURES 2
 struct ow_family_features_s
 {
-  char *family;
+  char family[OW_FAMILY_LENGTH];
   struct
   {
     char filename[DATA_MAX_NAME_LEN];
@@ -44,7 +45,7 @@ typedef struct ow_family_features_s ow_family_features_t;
 static ow_family_features_t ow_family_features[] =
 {
   {
-    /* family = */ "10",
+    /* family = */ "10.",
     {
       {
         /* filename = */ "temperature",
@@ -58,13 +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 int   ow_interval = 0;
 
 static const char *config_keys[] =
 {
-  "Alias",
   "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,9 +104,14 @@ static int cow_load_config (const char *key, const char *value)
     sfree (device_g);
     device_g = temp;
   }
-  else if (strcasecmp (key, "Alias") == 0)
+  else if (strcasecmp ("Interval", key) == 0)
   {
-    /* azogtodo alias-list */
+    int tmp;
+    tmp = atoi (value);
+    if (tmp > 0)
+      ow_interval = tmp;
+    else
+      ERROR ("onewire plugin: Invalid `Interval' setting: %s", value);
   }
   else
   {
@@ -116,26 +121,6 @@ static int cow_load_config (const char *key, const char *value)
   return (0);
 }
 
-static int cow_init (void)
-{
-  int status;
-
-  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);
-  }
-
-  return (0);
-} /* int cow_init */
-
 static int cow_read_values (const char *path, const char *name,
     const ow_family_features_t *family_info)
 {
@@ -153,7 +138,6 @@ static int cow_read_values (const char *path, const char *name,
 
   vl.values = values;
   vl.values_len = 1;
-  vl.time = time (NULL);
 
   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
   sstrncpy (vl.plugin, "onewire", sizeof (vl.plugin));
@@ -204,6 +188,31 @@ static int cow_read_values (const char *path, const char *name,
   return ((success > 0) ? 0 : -1);
 } /* int cow_read_values */
 
+/* Forward declaration so the recursion below works */
+static int cow_read_bus (const char *path);
+
+/*
+ * cow_read_ds2409
+ *
+ * Handles:
+ * - DS2409 - MicroLAN Coupler
+ */
+static int cow_read_ds2409 (const char *path)
+{
+  char subpath[4096];
+  int status;
+
+  status = ssnprintf (subpath, sizeof (subpath), "%s/main", path);
+  if ((status > 0) && (status < sizeof (subpath)))
+    cow_read_bus (subpath);
+
+  status = ssnprintf (subpath, sizeof (subpath), "%s/aux", path);
+  if ((status > 0) && (status < sizeof (subpath)))
+    cow_read_bus (subpath);
+
+  return (0);
+} /* int cow_read_ds2409 */
+
 static int cow_read_bus (const char *path)
 {
   char *buffer;
@@ -214,7 +223,6 @@ static int cow_read_bus (const char *path)
   char *dummy;
   char *saveptr;
   char subpath[4096];
-  char family_dummy[3]; /* a family only has 2 digits */
 
   status = OW_get (path, &buffer, &buffer_size);
   if (status < 0)
@@ -223,6 +231,8 @@ static int cow_read_bus (const char *path)
         path, status);
     return (-1);
   }
+  DEBUG ("onewire plugin: OW_get (%s) returned: %s",
+      path, buffer);
 
   dummy = buffer;
   saveptr = NULL;
@@ -232,16 +242,33 @@ static int cow_read_bus (const char *path)
 
     dummy = NULL;
 
-    snprintf (subpath, sizeof (subpath), "%s/%s", path, buffer_ptr);
-    subpath[sizeof (subpath) - 1] = 0;
+    if (strcmp ("/", path) == 0)
+      status = ssnprintf (subpath, sizeof (subpath), "/%s", buffer_ptr);
+    else
+      status = ssnprintf (subpath, sizeof (subpath), "%s/%s",
+          path, buffer_ptr);
+    if ((status <= 0) || (status >= sizeof (subpath)))
+      continue;
 
     for (i = 0; i < ow_family_features_num; i++)
     {
-      snprintf (family_dummy, sizeof (family_dummy), "%s%s", ow_family_features[i].family, ".");
-      if (strncmp (family_dummy, buffer_ptr, strlen (family_dummy)) != 0)
+      if (strncmp (ow_family_features[i].family, buffer_ptr,
+            strlen (ow_family_features[i].family)) != 0)
         continue;
 
-      cow_read_values (subpath, buffer_ptr + 3, ow_family_features + i);
+      cow_read_values (subpath,
+          buffer_ptr + strlen (ow_family_features[i].family),
+          ow_family_features + i);
+      break;
+    }
+    if (i < ow_family_features_num)
+      continue;
+
+    /* DS2409 */
+    if (strncmp ("1F.", buffer_ptr, strlen ("1F.")) == 0)
+    {
+      cow_read_ds2409 (subpath);
+      continue;
     }
   } /* while (strtok_r) */
 
@@ -249,56 +276,50 @@ 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)))
 {
-  char *buffer;
-  size_t buffer_size;
-  int status;
+  return (cow_read_bus ("/"));
+} /* int cow_read */
 
-  char *buffer_ptr;
-  char *dummy;
-  char *saveptr;
+static int cow_shutdown (void)
+{
+  OW_finish ();
+  ignorelist_free (sensor_list);
+  return (0);
+} /* int cow_shutdown */
 
-  status = OW_get ("/uncached/", &buffer, &buffer_size);
-  if (status < 0)
+static int cow_init (void)
+{
+  int status;
+  struct timespec cb_interval;
+
+  if (device_g == NULL)
   {
-    ERROR ("onewire plugin: OW_get (\"/\") failed. status = %#x;",
-        status);
+    ERROR ("onewire plugin: cow_init: No device configured.");
     return (-1);
   }
 
-  printf ("-- 8< --\n");
-
-  dummy = buffer;
-  saveptr = NULL;
-  while ((buffer_ptr = strtok_r (dummy, ",/", &saveptr)) != NULL)
+  status = (int) OW_init (device_g);
+  if (status != 0)
   {
-    dummy = NULL;
-    if (strncmp ("bus", buffer_ptr, strlen ("bus")) == 0)
-    {
-      cow_read_bus (buffer_ptr);
-    }
-  } /* while (strtok_r) */
-
-  printf ("-- >8 --\n");
+    ERROR ("onewire plugin: OW_init(%s) failed: %i.", device_g, status);
+    return (1);
+  }
 
-  free (buffer);
+  memset (&cb_interval, 0, sizeof (cb_interval));
+  if (ow_interval > 0)
+    cb_interval.tv_sec = (time_t) ow_interval;
 
-  return (0);
-} /* int cow_read */
+  plugin_register_complex_read ("onewire", cow_read,
+      &cb_interval, /* user data = */ NULL);
+  plugin_register_shutdown ("onewire", cow_shutdown);
 
-static int cow_shutdown (void)
-{
-  OW_finish ();
-  ignorelist_free (sensor_list);
   return (0);
-} /* int cow_shutdown */
+} /* 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);
 }