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