modbus plugin: Relicense under the LGPLv2.1.
[collectd.git] / src / modbus.c
1 /**
2  * collectd - src/modbus.c
3  * Copyright (C) 2010  noris network AG
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation; only version 2.1 of the License is
8  * applicable.
9  *
10  * This program 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  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors:
20  *   Florian Forster <octo at noris.net>
21  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
27
28 #include <netdb.h>
29
30 #include <modbus/modbus.h>
31
32 #ifndef LIBMODBUS_VERSION_CHECK
33 /* Assume version 2.0.3 */
34 # define LEGACY_LIBMODBUS 1
35 #else
36 /* Assume version 2.9.2 */
37 #endif
38
39 #ifndef MODBUS_TCP_DEFAULT_PORT
40 # ifdef MODBUS_TCP_PORT
41 #  define MODBUS_TCP_DEFAULT_PORT MODBUS_TCP_PORT
42 # else
43 #  define MODBUS_TCP_DEFAULT_PORT 502
44 # endif
45 #endif
46
47 /*
48  * <Data "data_name">
49  *   RegisterBase 1234
50  *   RegisterType float
51  *   Type gauge
52  *   Instance "..."
53  * </Data>
54  *
55  * <Host "name">
56  *   Address "addr"
57  *   Port "1234"
58  *   Interval 60
59  *
60  *   <Slave 1>
61  *     Instance "foobar" # optional
62  *     Collect "data_name"
63  *   </Slave>
64  * </Host>
65  */
66
67 /*
68  * Data structures
69  */
70 enum mb_register_type_e /* {{{ */
71 {
72   REG_TYPE_UINT16,
73   REG_TYPE_UINT32,
74   REG_TYPE_FLOAT
75 }; /* }}} */
76 typedef enum mb_register_type_e mb_register_type_t;
77
78 struct mb_data_s;
79 typedef struct mb_data_s mb_data_t;
80 struct mb_data_s /* {{{ */
81 {
82   char *name;
83   int register_base;
84   mb_register_type_t register_type;
85   char type[DATA_MAX_NAME_LEN];
86   char instance[DATA_MAX_NAME_LEN];
87
88   mb_data_t *next;
89 }; /* }}} */
90
91 struct mb_slave_s /* {{{ */
92 {
93   int id;
94   char instance[DATA_MAX_NAME_LEN];
95   mb_data_t *collect;
96 }; /* }}} */
97 typedef struct mb_slave_s mb_slave_t;
98
99 struct mb_host_s /* {{{ */
100 {
101   char host[DATA_MAX_NAME_LEN];
102   char node[NI_MAXHOST];
103   /* char service[NI_MAXSERV]; */
104   int port;
105   cdtime_t interval;
106
107   mb_slave_t *slaves;
108   size_t slaves_num;
109
110 #if LEGACY_LIBMODBUS
111   modbus_param_t connection;
112 #else
113   modbus_t *connection;
114 #endif
115   _Bool is_connected;
116   _Bool have_reconnected;
117 }; /* }}} */
118 typedef struct mb_host_s mb_host_t;
119
120 struct mb_data_group_s;
121 typedef struct mb_data_group_s mb_data_group_t;
122 struct mb_data_group_s /* {{{ */
123 {
124   mb_data_t *registers;
125   size_t registers_num;
126
127   mb_data_group_t *next;
128 }; /* }}} */
129
130 /*
131  * Global variables
132  */
133 static mb_data_t *data_definitions = NULL;
134
135 /*
136  * Functions
137  */
138 static mb_data_t *data_get_by_name (mb_data_t *src, /* {{{ */
139     const char *name)
140 {
141   mb_data_t *ptr;
142
143   if (name == NULL)
144     return (NULL);
145
146   for (ptr = src; ptr != NULL; ptr = ptr->next)
147     if (strcasecmp (ptr->name, name) == 0)
148       return (ptr);
149
150   return (NULL);
151 } /* }}} mb_data_t *data_get_by_name */
152
153 static int data_append (mb_data_t **dst, mb_data_t *src) /* {{{ */
154 {
155   mb_data_t *ptr;
156
157   if ((dst == NULL) || (src == NULL))
158     return (EINVAL);
159
160   ptr = *dst;
161
162   if (ptr == NULL)
163   {
164     *dst = src;
165     return (0);
166   }
167
168   while (ptr->next != NULL)
169     ptr = ptr->next;
170
171   ptr->next = src;
172
173   return (0);
174 } /* }}} int data_append */
175
176 /* Copy a single mb_data_t and append it to another list. */
177 static int data_copy (mb_data_t **dst, const mb_data_t *src) /* {{{ */
178 {
179   mb_data_t *tmp;
180   int status;
181
182   if ((dst == NULL) || (src == NULL))
183     return (EINVAL);
184
185   tmp = malloc (sizeof (*tmp));
186   if (tmp == NULL)
187     return (ENOMEM);
188   memcpy (tmp, src, sizeof (*tmp));
189   tmp->name = NULL;
190   tmp->next = NULL;
191
192   tmp->name = strdup (src->name);
193   if (tmp->name == NULL)
194   {
195     sfree (tmp);
196     return (ENOMEM);
197   }
198
199   status = data_append (dst, tmp);
200   if (status != 0)
201   {
202     sfree (tmp->name);
203     sfree (tmp);
204     return (status);
205   }
206
207   return (0);
208 } /* }}} int data_copy */
209
210 /* Lookup a single mb_data_t instance, copy it and append the copy to another
211  * list. */
212 static int data_copy_by_name (mb_data_t **dst, mb_data_t *src, /* {{{ */
213     const char *name)
214 {
215   mb_data_t *ptr;
216
217   if ((dst == NULL) || (src == NULL) || (name == NULL))
218     return (EINVAL);
219
220   ptr = data_get_by_name (src, name);
221   if (ptr == NULL)
222     return (ENOENT);
223
224   return (data_copy (dst, ptr));
225 } /* }}} int data_copy_by_name */
226
227 /* Read functions */
228
229 static int mb_submit (mb_host_t *host, mb_slave_t *slave, /* {{{ */
230     mb_data_t *data, value_t value)
231 {
232   value_list_t vl = VALUE_LIST_INIT;
233
234   if ((host == NULL) || (slave == NULL) || (data == NULL))
235     return (EINVAL);
236
237   if (host->interval <= 0)
238     host->interval = interval_g;
239
240   if (slave->instance[0] == 0)
241     ssnprintf (slave->instance, sizeof (slave->instance), "slave_%i",
242         slave->id);
243
244   vl.values = &value;
245   vl.values_len = 1;
246   vl.interval = host->interval;
247   sstrncpy (vl.host, host->host, sizeof (vl.host));
248   sstrncpy (vl.plugin, "modbus", sizeof (vl.plugin));
249   sstrncpy (vl.plugin_instance, slave->instance, sizeof (vl.plugin_instance));
250   sstrncpy (vl.type, data->type, sizeof (vl.type));
251   sstrncpy (vl.type_instance, data->instance, sizeof (vl.type_instance));
252
253   return (plugin_dispatch_values (&vl));
254 } /* }}} int mb_submit */
255
256 static float mb_register_to_float (uint16_t hi, uint16_t lo) /* {{{ */
257 {
258   union
259   {
260     uint8_t b[4];
261     float f;
262   } conv;
263
264 #if BYTE_ORDER == LITTLE_ENDIAN
265   /* little endian */
266   conv.b[0] = lo & 0x00ff;
267   conv.b[1] = (lo >> 8) & 0x00ff;
268   conv.b[2] = hi & 0x00ff;
269   conv.b[3] = (hi >> 8) & 0x00ff;
270 #else
271   conv.b[3] = lo & 0x00ff;
272   conv.b[2] = (lo >> 8) & 0x00ff;
273   conv.b[1] = hi & 0x00ff;
274   conv.b[0] = (hi >> 8) & 0x00ff;
275 #endif
276
277   return (conv.f);
278 } /* }}} float mb_register_to_float */
279
280 #if LEGACY_LIBMODBUS
281 /* Version 2.0.3 */
282 static int mb_init_connection (mb_host_t *host) /* {{{ */
283 {
284   int status;
285
286   if (host == NULL)
287     return (EINVAL);
288
289   if (host->is_connected)
290     return (0);
291
292   /* Only reconnect once per interval. */
293   if (host->have_reconnected)
294     return (-1);
295
296   modbus_set_debug (&host->connection, 1);
297
298   /* We'll do the error handling ourselves. */
299   modbus_set_error_handling (&host->connection, NOP_ON_ERROR);
300
301   if ((host->port < 1) || (host->port > 65535))
302     host->port = MODBUS_TCP_DEFAULT_PORT;
303
304   DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
305       host->node, host->port);
306
307   modbus_init_tcp (&host->connection,
308       /* host = */ host->node,
309       /* port = */ host->port);
310
311   status = modbus_connect (&host->connection);
312   if (status != 0)
313   {
314     ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
315         host->node, host->port, status);
316     return (status);
317   }
318
319   host->is_connected = 1;
320   host->have_reconnected = 1;
321   return (0);
322 } /* }}} int mb_init_connection */
323 /* #endif LEGACY_LIBMODBUS */
324
325 #else /* if !LEGACY_LIBMODBUS */
326 /* Version 2.9.2 */
327 static int mb_init_connection (mb_host_t *host) /* {{{ */
328 {
329   int status;
330
331   if (host == NULL)
332     return (EINVAL);
333
334   if (host->connection != NULL)
335     return (0);
336
337   /* Only reconnect once per interval. */
338   if (host->have_reconnected)
339     return (-1);
340
341   if ((host->port < 1) || (host->port > 65535))
342     host->port = MODBUS_TCP_DEFAULT_PORT;
343
344   DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
345       host->node, host->port);
346
347   host->connection = modbus_new_tcp (host->node, host->port);
348   if (host->connection == NULL)
349   {
350     host->have_reconnected = 1;
351     ERROR ("Modbus plugin: Creating new Modbus/TCP object failed.");
352     return (-1);
353   }
354
355   modbus_set_debug (host->connection, 1);
356
357   /* We'll do the error handling ourselves. */
358   modbus_set_error_recovery (host->connection, 0);
359
360   status = modbus_connect (host->connection);
361   if (status != 0)
362   {
363     ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
364         host->node, host->port, status);
365     modbus_free (host->connection);
366     host->connection = NULL;
367     return (status);
368   }
369
370   host->have_reconnected = 1;
371   return (0);
372 } /* }}} int mb_init_connection */
373 #endif /* !LEGACY_LIBMODBUS */
374
375 #define CAST_TO_VALUE_T(ds,vt,raw) do { \
376   if ((ds)->ds[0].type == DS_TYPE_COUNTER) \
377     (vt).counter = (counter_t) (raw); \
378   else if ((ds)->ds[0].type == DS_TYPE_GAUGE) \
379     (vt).gauge = (gauge_t) (raw); \
380   else if ((ds)->ds[0].type == DS_TYPE_DERIVE) \
381     (vt).derive = (derive_t) (raw); \
382   else /* if (ds->ds[0].type == DS_TYPE_ABSOLUTE) */ \
383     (vt).absolute = (absolute_t) (raw); \
384 } while (0)
385
386 static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */
387     mb_data_t *data)
388 {
389   uint16_t values[2];
390   int values_num;
391   const data_set_t *ds;
392   int status;
393   int i;
394
395   if ((host == NULL) || (slave == NULL) || (data == NULL))
396     return (EINVAL);
397
398   ds = plugin_get_ds (data->type);
399   if (ds == NULL)
400   {
401     ERROR ("Modbus plugin: Type \"%s\" is not defined.", data->type);
402     return (-1);
403   }
404
405   if (ds->ds_num != 1)
406   {
407     ERROR ("Modbus plugin: The type \"%s\" has %i data sources. "
408         "I can only handle data sets with only one data source.",
409         data->type, ds->ds_num);
410     return (-1);
411   }
412
413   if ((ds->ds[0].type != DS_TYPE_GAUGE)
414       && (data->register_type != REG_TYPE_UINT32))
415   {
416     NOTICE ("Modbus plugin: The data source of type \"%s\" is %s, not gauge. "
417         "This will most likely result in problems, because the register type "
418         "is not UINT32.", data->type, DS_TYPE_TO_STRING (ds->ds[0].type));
419   }
420
421   memset (values, 0, sizeof (values));
422   if ((data->register_type == REG_TYPE_UINT32)
423       || (data->register_type == REG_TYPE_FLOAT))
424     values_num = 2;
425   else
426     values_num = 1;
427
428 #if LEGACY_LIBMODBUS
429   /* Version 2.0.3: Pass the connection struct as a pointer and pass the slave
430    * id to each call of "read_holding_registers". */
431 # define modbus_read_registers(ctx, addr, nb, dest) \
432   read_holding_registers (&(ctx), slave->id, (addr), (nb), (dest))
433 #else /* if !LEGACY_LIBMODBUS */
434   /* Version 2.9.2: Set the slave id once before querying the registers. */
435   status = modbus_set_slave (host->connection, slave->id);
436   if (status != 0)
437   {
438     ERROR ("Modbus plugin: modbus_set_slave (%i) failed with status %i.",
439         slave->id, status);
440     return (-1);
441   }
442 #endif
443
444   for (i = 0; i < 2; i++)
445   {
446     status = modbus_read_registers (host->connection,
447         /* start_addr = */ data->register_base,
448         /* num_registers = */ values_num, /* buffer = */ values);
449     if (status > 0)
450       break;
451
452     if (host->is_connected)
453     {
454 #if LEGACY_LIBMODBUS
455       modbus_close (&host->connection);
456       host->is_connected = 0;
457 #else
458       modbus_close (host->connection);
459       modbus_free (host->connection);
460       host->connection = NULL;
461 #endif
462     }
463
464     /* If we already tried reconnecting this round, give up. */
465     if (host->have_reconnected)
466     {
467       ERROR ("Modbus plugin: modbus_read_registers (%s) failed. "
468           "Reconnecting has already been tried. Giving up.", host->host);
469       return (-1);
470     }
471
472     /* Maybe the device closed the connection during the waiting interval.
473      * Try re-establishing the connection. */
474     status = mb_init_connection (host);
475     if (status != 0)
476     {
477       ERROR ("Modbus plugin: modbus_read_registers (%s) failed. "
478           "While trying to reconnect, connecting to \"%s\" failed. "
479           "Giving up.",
480           host->host, host->node);
481       return (-1);
482     }
483
484     DEBUG ("Modbus plugin: Re-established connection to %s", host->host);
485
486     /* try again */
487     continue;
488   } /* for (i = 0, 1) */
489
490   DEBUG ("Modbus plugin: mb_read_data: Success! "
491       "modbus_read_registers returned with status %i.", status);
492
493   if (data->register_type == REG_TYPE_FLOAT)
494   {
495     float float_value;
496     value_t vt;
497
498     float_value = mb_register_to_float (values[0], values[1]);
499     DEBUG ("Modbus plugin: mb_read_data: "
500         "Returned float value is %g", (double) float_value);
501
502     CAST_TO_VALUE_T (ds, vt, float_value);
503     mb_submit (host, slave, data, vt);
504   }
505   else if (data->register_type == REG_TYPE_UINT32)
506   {
507     uint32_t v32;
508     value_t vt;
509
510     v32 = (values[0] << 16) | values[1];
511     DEBUG ("Modbus plugin: mb_read_data: "
512         "Returned uint32 value is %"PRIu32, v32);
513
514     CAST_TO_VALUE_T (ds, vt, v32);
515     mb_submit (host, slave, data, vt);
516   }
517   else /* if (data->register_type == REG_TYPE_UINT16) */
518   {
519     value_t vt;
520
521     DEBUG ("Modbus plugin: mb_read_data: "
522         "Returned uint16 value is %"PRIu16, values[0]);
523
524     CAST_TO_VALUE_T (ds, vt, values[0]);
525     mb_submit (host, slave, data, vt);
526   }
527
528   return (0);
529 } /* }}} int mb_read_data */
530
531 static int mb_read_slave (mb_host_t *host, mb_slave_t *slave) /* {{{ */
532 {
533   mb_data_t *data;
534   int success;
535   int status;
536
537   if ((host == NULL) || (slave == NULL))
538     return (EINVAL);
539
540   success = 0;
541   for (data = slave->collect; data != NULL; data = data->next)
542   {
543     status = mb_read_data (host, slave, data);
544     if (status == 0)
545       success++;
546   }
547
548   if (success == 0)
549     return (-1);
550   else
551     return (0);
552 } /* }}} int mb_read_slave */
553
554 static int mb_read (user_data_t *user_data) /* {{{ */
555 {
556   mb_host_t *host;
557   size_t i;
558   int success;
559   int status;
560
561   if ((user_data == NULL) || (user_data->data == NULL))
562     return (EINVAL);
563
564   host = user_data->data;
565
566   /* Clear the reconnect flag. */
567   host->have_reconnected = 0;
568
569   success = 0;
570   for (i = 0; i < host->slaves_num; i++)
571   {
572     status = mb_read_slave (host, host->slaves + i);
573     if (status == 0)
574       success++;
575   }
576
577   if (success == 0)
578     return (-1);
579   else
580     return (0);
581 } /* }}} int mb_read */
582
583 /* Free functions */
584
585 static void data_free_one (mb_data_t *data) /* {{{ */
586 {
587   if (data == NULL)
588     return;
589
590   sfree (data->name);
591   sfree (data);
592 } /* }}} void data_free_one */
593
594 static void data_free_all (mb_data_t *data) /* {{{ */
595 {
596   mb_data_t *next;
597
598   if (data == NULL)
599     return;
600
601   next = data->next;
602   data_free_one (data);
603
604   data_free_all (next);
605 } /* }}} void data_free_all */
606
607 static void slaves_free_all (mb_slave_t *slaves, size_t slaves_num) /* {{{ */
608 {
609   size_t i;
610
611   if (slaves == NULL)
612     return;
613
614   for (i = 0; i < slaves_num; i++)
615     data_free_all (slaves[i].collect);
616   sfree (slaves);
617 } /* }}} void slaves_free_all */
618
619 static void host_free (void *void_host) /* {{{ */
620 {
621   mb_host_t *host = void_host;
622
623   if (host == NULL)
624     return;
625
626   slaves_free_all (host->slaves, host->slaves_num);
627   sfree (host);
628 } /* }}} void host_free */
629
630 /* Config functions */
631
632 static int mb_config_add_data (oconfig_item_t *ci) /* {{{ */
633 {
634   mb_data_t data;
635   int status;
636   int i;
637
638   memset (&data, 0, sizeof (data));
639   data.name = NULL;
640   data.register_type = REG_TYPE_UINT16;
641   data.next = NULL;
642
643   status = cf_util_get_string (ci, &data.name);
644   if (status != 0)
645     return (status);
646
647   for (i = 0; i < ci->children_num; i++)
648   {
649     oconfig_item_t *child = ci->children + i;
650     status = 0;
651
652     if (strcasecmp ("Type", child->key) == 0)
653       status = cf_util_get_string_buffer (child,
654           data.type, sizeof (data.type));
655     else if (strcasecmp ("Instance", child->key) == 0)
656       status = cf_util_get_string_buffer (child,
657           data.instance, sizeof (data.instance));
658     else if (strcasecmp ("RegisterBase", child->key) == 0)
659       status = cf_util_get_int (child, &data.register_base);
660     else if (strcasecmp ("RegisterType", child->key) == 0)
661     {
662       char tmp[16];
663       status = cf_util_get_string_buffer (child, tmp, sizeof (tmp));
664       if (status != 0)
665         /* do nothing */;
666       else if (strcasecmp ("Uint16", tmp) == 0)
667         data.register_type = REG_TYPE_UINT16;
668       else if (strcasecmp ("Uint32", tmp) == 0)
669         data.register_type = REG_TYPE_UINT32;
670       else if (strcasecmp ("Float", tmp) == 0)
671         data.register_type = REG_TYPE_FLOAT;
672       else
673       {
674         ERROR ("Modbus plugin: The register type \"%s\" is unknown.", tmp);
675         status = -1;
676       }
677     }
678     else
679     {
680       ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
681       status = -1;
682     }
683
684     if (status != 0)
685       break;
686   } /* for (i = 0; i < ci->children_num; i++) */
687
688   assert (data.name != NULL);
689   if (data.type[0] == 0)
690   {
691     ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
692         data.name);
693     status = -1;
694   }
695
696   if (status == 0)
697     data_copy (&data_definitions, &data);
698
699   sfree (data.name);
700
701   return (status);
702 } /* }}} int mb_config_add_data */
703
704 static int mb_config_set_host_address (mb_host_t *host, /* {{{ */
705     const char *address)
706 {
707   struct addrinfo *ai_list;
708   struct addrinfo *ai_ptr;
709   struct addrinfo  ai_hints;
710   int status;
711
712   if ((host == NULL) || (address == NULL))
713     return (EINVAL);
714
715   memset (&ai_hints, 0, sizeof (ai_hints));
716 #if AI_ADDRCONFIG
717   ai_hints.ai_flags |= AI_ADDRCONFIG;
718 #endif
719   /* XXX: libmodbus can only handle IPv4 addresses. */
720   ai_hints.ai_family = AF_INET;
721   ai_hints.ai_addr = NULL;
722   ai_hints.ai_canonname = NULL;
723   ai_hints.ai_next = NULL;
724
725   ai_list = NULL;
726   status = getaddrinfo (address, /* service = */ NULL,
727       &ai_hints, &ai_list);
728   if (status != 0)
729   {
730     char errbuf[1024];
731     ERROR ("Modbus plugin: getaddrinfo failed: %s",
732         (status == EAI_SYSTEM)
733         ? sstrerror (errno, errbuf, sizeof (errbuf))
734         : gai_strerror (status));
735     return (status);
736   }
737
738   for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
739   {
740     status = getnameinfo (ai_ptr->ai_addr, ai_ptr->ai_addrlen,
741         host->node, sizeof (host->node),
742         /* service = */ NULL, /* length = */ 0,
743         /* flags = */ NI_NUMERICHOST);
744     if (status == 0)
745       break;
746   } /* for (ai_ptr) */
747
748   freeaddrinfo (ai_list);
749
750   if (status != 0)
751     ERROR ("Modbus plugin: Unable to translate node name: \"%s\"", address);
752   else /* if (status == 0) */
753   {
754     DEBUG ("Modbus plugin: mb_config_set_host_address: %s -> %s",
755         address, host->node);
756   }
757
758   return (status);
759 } /* }}} int mb_config_set_host_address */
760
761 static int mb_config_add_slave (mb_host_t *host, oconfig_item_t *ci) /* {{{ */
762 {
763   mb_slave_t *slave;
764   int status;
765   int i;
766
767   if ((host == NULL) || (ci == NULL))
768     return (EINVAL);
769
770   slave = realloc (host->slaves, sizeof (*slave) * (host->slaves_num + 1));
771   if (slave == NULL)
772     return (ENOMEM);
773   host->slaves = slave;
774   slave = host->slaves + host->slaves_num;
775   memset (slave, 0, sizeof (*slave));
776   slave->collect = NULL;
777
778   status = cf_util_get_int (ci, &slave->id);
779   if (status != 0)
780     return (status);
781
782   for (i = 0; i < ci->children_num; i++)
783   {
784     oconfig_item_t *child = ci->children + i;
785     status = 0;
786
787     if (strcasecmp ("Instance", child->key) == 0)
788       status = cf_util_get_string_buffer (child,
789           slave->instance, sizeof (slave->instance));
790     else if (strcasecmp ("Collect", child->key) == 0)
791     {
792       char buffer[1024];
793       status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
794       if (status == 0)
795         data_copy_by_name (&slave->collect, data_definitions, buffer);
796       status = 0; /* continue after failure. */
797     }
798     else
799     {
800       ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
801       status = -1;
802     }
803
804     if (status != 0)
805       break;
806   }
807
808   if ((status == 0) && (slave->collect == NULL))
809     status = EINVAL;
810
811   if (slave->id < 0)
812     status = EINVAL;
813
814   if (status == 0)
815     host->slaves_num++;
816   else /* if (status != 0) */
817     data_free_all (slave->collect);
818
819   return (status);
820 } /* }}} int mb_config_add_slave */
821
822 static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */
823 {
824   mb_host_t *host;
825   int status;
826   int i;
827
828   host = malloc (sizeof (*host));
829   if (host == NULL)
830     return (ENOMEM);
831   memset (host, 0, sizeof (*host));
832   host->slaves = NULL;
833
834   status = cf_util_get_string_buffer (ci, host->host, sizeof (host->host));
835   if (status != 0)
836     return (status);
837   if (host->host[0] == 0)
838     return (EINVAL);
839
840   for (i = 0; i < ci->children_num; i++)
841   {
842     oconfig_item_t *child = ci->children + i;
843     status = 0;
844
845     if (strcasecmp ("Address", child->key) == 0)
846     {
847       char buffer[NI_MAXHOST];
848       status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
849       if (status == 0)
850         status = mb_config_set_host_address (host, buffer);
851     }
852     else if (strcasecmp ("Port", child->key) == 0)
853     {
854       host->port = cf_util_get_port_number (child);
855       if (host->port <= 0)
856         status = -1;
857     }
858     else if (strcasecmp ("Interval", child->key) == 0)
859       status = cf_util_get_cdtime (child, &host->interval);
860     else if (strcasecmp ("Slave", child->key) == 0)
861       /* Don't set status: Gracefully continue if a slave fails. */
862       mb_config_add_slave (host, child);
863     else
864     {
865       ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
866       status = -1;
867     }
868
869     if (status != 0)
870       break;
871   } /* for (i = 0; i < ci->children_num; i++) */
872
873   assert (host->host[0] != 0);
874   if (host->host[0] == 0)
875   {
876     ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
877         host->host);
878     status = -1;
879   }
880
881   if (status == 0)
882   {
883     user_data_t ud;
884     char name[1024];
885     struct timespec interval = { 0, 0 };
886
887     ud.data = host;
888     ud.free_func = host_free;
889
890     ssnprintf (name, sizeof (name), "modbus-%s", host->host);
891
892     CDTIME_T_TO_TIMESPEC (host->interval, &interval);
893
894     plugin_register_complex_read (/* group = */ NULL, name,
895         /* callback = */ mb_read,
896         /* interval = */ (host->interval > 0) ? &interval : NULL,
897         &ud);
898   }
899   else
900   {
901     host_free (host);
902   }
903
904   return (status);
905 } /* }}} int mb_config_add_host */
906
907 static int mb_config (oconfig_item_t *ci) /* {{{ */
908 {
909   int i;
910
911   if (ci == NULL)
912     return (EINVAL);
913
914   for (i = 0; i < ci->children_num; i++)
915   {
916     oconfig_item_t *child = ci->children + i;
917
918     if (strcasecmp ("Data", child->key) == 0)
919       mb_config_add_data (child);
920     else if (strcasecmp ("Host", child->key) == 0)
921       mb_config_add_host (child);
922     else
923       ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
924   }
925
926   return (0);
927 } /* }}} int mb_config */
928
929 /* ========= */
930
931 static int mb_shutdown (void) /* {{{ */
932 {
933   data_free_all (data_definitions);
934   data_definitions = NULL;
935
936   return (0);
937 } /* }}} int mb_shutdown */
938
939 void module_register (void)
940 {
941   plugin_register_complex_config ("modbus", mb_config);
942   plugin_register_shutdown ("modbus", mb_shutdown);
943 } /* void module_register */
944
945 /* vim: set sw=2 sts=2 et fdm=marker : */