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