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