f40258d09c4b3f0c472bc3e1ee343a108ed1e859
[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 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_INT16,
73   REG_TYPE_INT32,
74   REG_TYPE_UINT16,
75   REG_TYPE_UINT32,
76   REG_TYPE_FLOAT
77 }; /* }}} */
78 typedef enum mb_register_type_e mb_register_type_t;
79
80 struct mb_data_s;
81 typedef struct mb_data_s mb_data_t;
82 struct mb_data_s /* {{{ */
83 {
84   char *name;
85   int register_base;
86   mb_register_type_t register_type;
87   char type[DATA_MAX_NAME_LEN];
88   char instance[DATA_MAX_NAME_LEN];
89
90   mb_data_t *next;
91 }; /* }}} */
92
93 struct mb_slave_s /* {{{ */
94 {
95   int id;
96   char instance[DATA_MAX_NAME_LEN];
97   mb_data_t *collect;
98 }; /* }}} */
99 typedef struct mb_slave_s mb_slave_t;
100
101 struct mb_host_s /* {{{ */
102 {
103   char host[DATA_MAX_NAME_LEN];
104   char node[NI_MAXHOST];
105   /* char service[NI_MAXSERV]; */
106   int port;
107   cdtime_t interval;
108
109   mb_slave_t *slaves;
110   size_t slaves_num;
111
112 #if LEGACY_LIBMODBUS
113   modbus_param_t connection;
114 #else
115   modbus_t *connection;
116 #endif
117   _Bool is_connected;
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 = plugin_get_interval ();
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     uint16_t s[2];
263     float f;
264   } conv;
265
266 #if BYTE_ORDER == LITTLE_ENDIAN
267   /* little endian */
268   conv.b[0] = lo & 0x00ff;
269   conv.b[1] = (lo >> 8) & 0x00ff;
270   conv.b[2] = hi & 0x00ff;
271   conv.b[3] = (hi >> 8) & 0x00ff;
272 #else
273   conv.b[3] = lo & 0x00ff;
274   conv.b[2] = (lo >> 8) & 0x00ff;
275   conv.b[1] = hi & 0x00ff;
276   conv.b[0] = (hi >> 8) & 0x00ff;
277 #endif
278
279   return (conv.f);
280 } /* }}} float mb_register_to_float */
281
282 #if LEGACY_LIBMODBUS
283 /* Version 2.0.3 */
284 static int mb_init_connection (mb_host_t *host) /* {{{ */
285 {
286   int status;
287
288   if (host == NULL)
289     return (EINVAL);
290
291   modbus_set_debug (&host->connection, 1);
292
293   /* We'll do the error handling ourselves. */
294   modbus_set_error_handling (&host->connection, NOP_ON_ERROR);
295
296   if ((host->port < 1) || (host->port > 65535))
297     host->port = MODBUS_TCP_DEFAULT_PORT;
298
299   DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
300       host->node, host->port);
301
302   modbus_init_tcp (&host->connection,
303       /* host = */ host->node,
304       /* port = */ host->port);
305
306   status = modbus_connect (&host->connection);
307   if (status != 0)
308   {
309     ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
310         host->node, host->port, status);
311     return (status);
312   }
313
314   host->is_connected = 1;
315   return (0);
316 } /* }}} int mb_init_connection */
317 /* #endif LEGACY_LIBMODBUS */
318
319 #else /* if !LEGACY_LIBMODBUS */
320 /* Version 2.9.2 */
321 static int mb_init_connection (mb_host_t *host) /* {{{ */
322 {
323   int status;
324
325   if (host == NULL)
326     return (EINVAL);
327
328   if (host->connection != NULL)
329     return (0);
330
331   if ((host->port < 1) || (host->port > 65535))
332     host->port = MODBUS_TCP_DEFAULT_PORT;
333
334   DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
335       host->node, host->port);
336
337   host->connection = modbus_new_tcp (host->node, host->port);
338   if (host->connection == NULL)
339   {
340     ERROR ("Modbus plugin: Creating new Modbus/TCP object failed.");
341     return (-1);
342   }
343
344   modbus_set_debug (host->connection, 1);
345
346   /* We'll do the error handling ourselves. */
347   modbus_set_error_recovery (host->connection, 0);
348
349   status = modbus_connect (host->connection);
350   if (status != 0)
351   {
352     ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
353         host->node, host->port, status);
354     modbus_free (host->connection);
355     host->connection = NULL;
356     return (status);
357   }
358
359   return (0);
360 } /* }}} int mb_init_connection */
361 #endif /* !LEGACY_LIBMODBUS */
362
363 #define CAST_TO_VALUE_T(ds,vt,raw) do { \
364   if ((ds)->ds[0].type == DS_TYPE_COUNTER) \
365     (vt).counter = (counter_t) (raw); \
366   else if ((ds)->ds[0].type == DS_TYPE_GAUGE) \
367     (vt).gauge = (gauge_t) (raw); \
368   else if ((ds)->ds[0].type == DS_TYPE_DERIVE) \
369     (vt).derive = (derive_t) (raw); \
370   else /* if (ds->ds[0].type == DS_TYPE_ABSOLUTE) */ \
371     (vt).absolute = (absolute_t) (raw); \
372 } while (0)
373
374 static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */
375     mb_data_t *data)
376 {
377   uint16_t values[2];
378   int values_num;
379   const data_set_t *ds;
380   struct sockaddr sockaddr;
381   socklen_t saddrlen = sizeof(sockaddr);
382   int status;
383
384   if ((host == NULL) || (slave == NULL) || (data == NULL))
385     return (EINVAL);
386
387   ds = plugin_get_ds (data->type);
388   if (ds == NULL)
389   {
390     ERROR ("Modbus plugin: Type \"%s\" is not defined.", data->type);
391     return (-1);
392   }
393
394   if (ds->ds_num != 1)
395   {
396     ERROR ("Modbus plugin: The type \"%s\" has %i data sources. "
397         "I can only handle data sets with only one data source.",
398         data->type, ds->ds_num);
399     return (-1);
400   }
401
402   if ((ds->ds[0].type != DS_TYPE_GAUGE)
403       && (data->register_type != REG_TYPE_INT32)
404       && (data->register_type != REG_TYPE_UINT32))
405   {
406     NOTICE ("Modbus plugin: The data source of type \"%s\" is %s, not gauge. "
407         "This will most likely result in problems, because the register type "
408         "is not UINT32.", data->type, DS_TYPE_TO_STRING (ds->ds[0].type));
409   }
410
411   memset (values, 0, sizeof (values));
412   if ((data->register_type == REG_TYPE_INT32)
413       || (data->register_type == REG_TYPE_UINT32)
414       || (data->register_type == REG_TYPE_FLOAT))
415     values_num = 2;
416   else
417     values_num = 1;
418
419   if(host->connection == NULL)
420     goto try_connect;
421
422   if(getpeername(modbus_get_socket(host->connection),
423                  &sockaddr, &saddrlen) != 0)
424     switch(errno){
425     case EBADF:
426     case ENOTSOCK:
427     case ENOTCONN:
428     try_connect:
429       status = mb_init_connection (host);
430       if (status != 0)
431       {
432         ERROR ("Modbus plugin: mb_init_connection (%s/%s) failed. ",
433            host->host, host->node);
434         host->is_connected = 0;
435         host->connection = NULL;
436         return (-1);
437       }
438     break;
439     default:
440 #if LEGACY_LIBMODBUS
441       modbus_close (&host->connection);
442 #else
443       modbus_close (host->connection);
444       modbus_free (host->connection);
445 #endif
446   }
447  
448 #if LEGACY_LIBMODBUS
449   /* Version 2.0.3: Pass the connection struct as a pointer and pass the slave
450    * id to each call of "read_holding_registers". */
451 # define modbus_read_registers(ctx, addr, nb, dest) \
452   read_holding_registers (&(ctx), slave->id, (addr), (nb), (dest))
453 #else /* if !LEGACY_LIBMODBUS */
454   /* Version 2.9.2: Set the slave id once before querying the registers. */
455   status = modbus_set_slave (host->connection, slave->id);
456   if (status != 0)
457   {
458     ERROR ("Modbus plugin: modbus_set_slave (%i) failed with status %i.",
459         slave->id, status);
460     return (-1);
461   }
462 #endif
463
464   status = modbus_read_registers (host->connection,
465         /* start_addr = */ data->register_base,
466         /* num_registers = */ values_num, /* buffer = */ values);
467   if (status != values_num)
468   {
469     ERROR ("Modbus plugin: modbus_read_registers (%s/%s) failed. status = %i, values_num = %i "
470         "Giving up.", host->host, host->node, status, values_num);
471 #if LEGACY_LIBMODBUS
472     modbus_close (&host->connection);
473 #else
474     modbus_close (host->connection);
475     modbus_free (host->connection);
476 #endif
477     host->connection = NULL;
478     return (-1);
479   }
480
481   DEBUG ("Modbus plugin: mb_read_data: Success! "
482       "modbus_read_registers returned with status %i.", status);
483
484   if (data->register_type == REG_TYPE_FLOAT)
485   {
486     float float_value;
487     value_t vt;
488
489     float_value = mb_register_to_float (values[0], values[1]);
490     DEBUG ("Modbus plugin: mb_read_data: "
491         "Returned float value is %g", (double) float_value);
492
493     CAST_TO_VALUE_T (ds, vt, float_value);
494     mb_submit (host, slave, data, vt);
495   }
496   else if (data->register_type == REG_TYPE_INT32)
497   {
498     union
499     {
500       uint32_t u32;
501       int32_t  i32;
502     } v;
503     value_t vt;
504
505     v.u32 = (((uint32_t) values[0]) << 16)
506       | ((uint32_t) values[1]);
507     DEBUG ("Modbus plugin: mb_read_data: "
508         "Returned int32 value is %"PRIi32, v.i32);
509
510     CAST_TO_VALUE_T (ds, vt, v.i32);
511     mb_submit (host, slave, data, vt);
512   }
513   else if (data->register_type == REG_TYPE_INT16)
514   {
515     union
516     {
517       uint16_t u16;
518       int16_t  i16;
519     } v;
520     value_t vt;
521
522     v.u16 = values[0];
523
524     DEBUG ("Modbus plugin: mb_read_data: "
525         "Returned int16 value is %"PRIi16, v.i16);
526
527     CAST_TO_VALUE_T (ds, vt, v.i16);
528     mb_submit (host, slave, data, vt);
529   }
530   else if (data->register_type == REG_TYPE_UINT32)
531   {
532     uint32_t v32;
533     value_t vt;
534
535     v32 = (((uint32_t) values[0]) << 16)
536       | ((uint32_t) values[1]);
537     DEBUG ("Modbus plugin: mb_read_data: "
538         "Returned uint32 value is %"PRIu32, v32);
539
540     CAST_TO_VALUE_T (ds, vt, v32);
541     mb_submit (host, slave, data, vt);
542   }
543   else /* if (data->register_type == REG_TYPE_UINT16) */
544   {
545     value_t vt;
546
547     DEBUG ("Modbus plugin: mb_read_data: "
548         "Returned uint16 value is %"PRIu16, values[0]);
549
550     CAST_TO_VALUE_T (ds, vt, values[0]);
551     mb_submit (host, slave, data, vt);
552   }
553
554   return (0);
555 } /* }}} int mb_read_data */
556
557 static int mb_read_slave (mb_host_t *host, mb_slave_t *slave) /* {{{ */
558 {
559   mb_data_t *data;
560   int success;
561   int status;
562
563   if ((host == NULL) || (slave == NULL))
564     return (EINVAL);
565
566   success = 0;
567   for (data = slave->collect; data != NULL; data = data->next)
568   {
569     status = mb_read_data (host, slave, data);
570     if (status == 0)
571       success++;
572   }
573
574   if (success == 0)
575     return (-1);
576   else
577     return (0);
578 } /* }}} int mb_read_slave */
579
580 static int mb_read (user_data_t *user_data) /* {{{ */
581 {
582   mb_host_t *host;
583   size_t i;
584   int success;
585   int status;
586
587   if ((user_data == NULL) || (user_data->data == NULL))
588     return (EINVAL);
589
590   host = user_data->data;
591
592   success = 0;
593   for (i = 0; i < host->slaves_num; i++)
594   {
595     status = mb_read_slave (host, host->slaves + i);
596     if (status == 0)
597       success++;
598   }
599
600   if (success == 0)
601     return (-1);
602   else
603     return (0);
604 } /* }}} int mb_read */
605
606 /* Free functions */
607
608 static void data_free_one (mb_data_t *data) /* {{{ */
609 {
610   if (data == NULL)
611     return;
612
613   sfree (data->name);
614   sfree (data);
615 } /* }}} void data_free_one */
616
617 static void data_free_all (mb_data_t *data) /* {{{ */
618 {
619   mb_data_t *next;
620
621   if (data == NULL)
622     return;
623
624   next = data->next;
625   data_free_one (data);
626
627   data_free_all (next);
628 } /* }}} void data_free_all */
629
630 static void slaves_free_all (mb_slave_t *slaves, size_t slaves_num) /* {{{ */
631 {
632   size_t i;
633
634   if (slaves == NULL)
635     return;
636
637   for (i = 0; i < slaves_num; i++)
638     data_free_all (slaves[i].collect);
639   sfree (slaves);
640 } /* }}} void slaves_free_all */
641
642 static void host_free (void *void_host) /* {{{ */
643 {
644   mb_host_t *host = void_host;
645
646   if (host == NULL)
647     return;
648
649   slaves_free_all (host->slaves, host->slaves_num);
650   sfree (host);
651 } /* }}} void host_free */
652
653 /* Config functions */
654
655 static int mb_config_add_data (oconfig_item_t *ci) /* {{{ */
656 {
657   mb_data_t data;
658   int status;
659   int i;
660
661   memset (&data, 0, sizeof (data));
662   data.name = NULL;
663   data.register_type = REG_TYPE_UINT16;
664   data.next = NULL;
665
666   status = cf_util_get_string (ci, &data.name);
667   if (status != 0)
668     return (status);
669
670   for (i = 0; i < ci->children_num; i++)
671   {
672     oconfig_item_t *child = ci->children + i;
673     status = 0;
674
675     if (strcasecmp ("Type", child->key) == 0)
676       status = cf_util_get_string_buffer (child,
677           data.type, sizeof (data.type));
678     else if (strcasecmp ("Instance", child->key) == 0)
679       status = cf_util_get_string_buffer (child,
680           data.instance, sizeof (data.instance));
681     else if (strcasecmp ("RegisterBase", child->key) == 0)
682       status = cf_util_get_int (child, &data.register_base);
683     else if (strcasecmp ("RegisterType", child->key) == 0)
684     {
685       char tmp[16];
686       status = cf_util_get_string_buffer (child, tmp, sizeof (tmp));
687       if (status != 0)
688         /* do nothing */;
689       else if (strcasecmp ("Int16", tmp) == 0)
690         data.register_type = REG_TYPE_INT16;
691       else if (strcasecmp ("Int32", tmp) == 0)
692         data.register_type = REG_TYPE_INT32;
693       else if (strcasecmp ("Uint16", tmp) == 0)
694         data.register_type = REG_TYPE_UINT16;
695       else if (strcasecmp ("Uint32", tmp) == 0)
696         data.register_type = REG_TYPE_UINT32;
697       else if (strcasecmp ("Float", tmp) == 0)
698         data.register_type = REG_TYPE_FLOAT;
699       else
700       {
701         ERROR ("Modbus plugin: The register type \"%s\" is unknown.", tmp);
702         status = -1;
703       }
704     }
705     else
706     {
707       ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
708       status = -1;
709     }
710
711     if (status != 0)
712       break;
713   } /* for (i = 0; i < ci->children_num; i++) */
714
715   assert (data.name != NULL);
716   if (data.type[0] == 0)
717   {
718     ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
719         data.name);
720     status = -1;
721   }
722
723   if (status == 0)
724     data_copy (&data_definitions, &data);
725
726   sfree (data.name);
727
728   return (status);
729 } /* }}} int mb_config_add_data */
730
731 static int mb_config_set_host_address (mb_host_t *host, /* {{{ */
732     const char *address)
733 {
734   struct addrinfo *ai_list;
735   struct addrinfo *ai_ptr;
736   struct addrinfo  ai_hints;
737   int status;
738
739   if ((host == NULL) || (address == NULL))
740     return (EINVAL);
741
742   memset (&ai_hints, 0, sizeof (ai_hints));
743 #if AI_ADDRCONFIG
744   ai_hints.ai_flags |= AI_ADDRCONFIG;
745 #endif
746   /* XXX: libmodbus can only handle IPv4 addresses. */
747   ai_hints.ai_family = AF_INET;
748   ai_hints.ai_addr = NULL;
749   ai_hints.ai_canonname = NULL;
750   ai_hints.ai_next = NULL;
751
752   ai_list = NULL;
753   status = getaddrinfo (address, /* service = */ NULL,
754       &ai_hints, &ai_list);
755   if (status != 0)
756   {
757     char errbuf[1024];
758     ERROR ("Modbus plugin: getaddrinfo failed: %s",
759         (status == EAI_SYSTEM)
760         ? sstrerror (errno, errbuf, sizeof (errbuf))
761         : gai_strerror (status));
762     return (status);
763   }
764
765   for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
766   {
767     status = getnameinfo (ai_ptr->ai_addr, ai_ptr->ai_addrlen,
768         host->node, sizeof (host->node),
769         /* service = */ NULL, /* length = */ 0,
770         /* flags = */ NI_NUMERICHOST);
771     if (status == 0)
772       break;
773   } /* for (ai_ptr) */
774
775   freeaddrinfo (ai_list);
776
777   if (status != 0)
778     ERROR ("Modbus plugin: Unable to translate node name: \"%s\"", address);
779   else /* if (status == 0) */
780   {
781     DEBUG ("Modbus plugin: mb_config_set_host_address: %s -> %s",
782         address, host->node);
783   }
784
785   return (status);
786 } /* }}} int mb_config_set_host_address */
787
788 static int mb_config_add_slave (mb_host_t *host, oconfig_item_t *ci) /* {{{ */
789 {
790   mb_slave_t *slave;
791   int status;
792   int i;
793
794   if ((host == NULL) || (ci == NULL))
795     return (EINVAL);
796
797   slave = realloc (host->slaves, sizeof (*slave) * (host->slaves_num + 1));
798   if (slave == NULL)
799     return (ENOMEM);
800   host->slaves = slave;
801   slave = host->slaves + host->slaves_num;
802   memset (slave, 0, sizeof (*slave));
803   slave->collect = NULL;
804
805   status = cf_util_get_int (ci, &slave->id);
806   if (status != 0)
807     return (status);
808
809   for (i = 0; i < ci->children_num; i++)
810   {
811     oconfig_item_t *child = ci->children + i;
812     status = 0;
813
814     if (strcasecmp ("Instance", child->key) == 0)
815       status = cf_util_get_string_buffer (child,
816           slave->instance, sizeof (slave->instance));
817     else if (strcasecmp ("Collect", child->key) == 0)
818     {
819       char buffer[1024];
820       status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
821       if (status == 0)
822         data_copy_by_name (&slave->collect, data_definitions, buffer);
823       status = 0; /* continue after failure. */
824     }
825     else
826     {
827       ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
828       status = -1;
829     }
830
831     if (status != 0)
832       break;
833   }
834
835   if ((status == 0) && (slave->collect == NULL))
836     status = EINVAL;
837
838   if (slave->id < 0)
839     status = EINVAL;
840
841   if (status == 0)
842     host->slaves_num++;
843   else /* if (status != 0) */
844     data_free_all (slave->collect);
845
846   return (status);
847 } /* }}} int mb_config_add_slave */
848
849 static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */
850 {
851   mb_host_t *host;
852   int status;
853   int i;
854
855   host = malloc (sizeof (*host));
856   if (host == NULL)
857     return (ENOMEM);
858   memset (host, 0, sizeof (*host));
859   host->slaves = NULL;
860
861   status = cf_util_get_string_buffer (ci, host->host, sizeof (host->host));
862   if (status != 0)
863     return (status);
864   if (host->host[0] == 0)
865     return (EINVAL);
866
867   for (i = 0; i < ci->children_num; i++)
868   {
869     oconfig_item_t *child = ci->children + i;
870     status = 0;
871
872     if (strcasecmp ("Address", child->key) == 0)
873     {
874       char buffer[NI_MAXHOST];
875       status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
876       if (status == 0)
877         status = mb_config_set_host_address (host, buffer);
878     }
879     else if (strcasecmp ("Port", child->key) == 0)
880     {
881       host->port = cf_util_get_port_number (child);
882       if (host->port <= 0)
883         status = -1;
884     }
885     else if (strcasecmp ("Interval", child->key) == 0)
886       status = cf_util_get_cdtime (child, &host->interval);
887     else if (strcasecmp ("Slave", child->key) == 0)
888       /* Don't set status: Gracefully continue if a slave fails. */
889       mb_config_add_slave (host, child);
890     else
891     {
892       ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
893       status = -1;
894     }
895
896     if (status != 0)
897       break;
898   } /* for (i = 0; i < ci->children_num; i++) */
899
900   assert (host->host[0] != 0);
901   if (host->host[0] == 0)
902   {
903     ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
904         host->host);
905     status = -1;
906   }
907
908   if (status == 0)
909   {
910     user_data_t ud;
911     char name[1024];
912     struct timespec interval = { 0, 0 };
913
914     ud.data = host;
915     ud.free_func = host_free;
916
917     ssnprintf (name, sizeof (name), "modbus-%s", host->host);
918
919     CDTIME_T_TO_TIMESPEC (host->interval, &interval);
920
921     plugin_register_complex_read (/* group = */ NULL, name,
922         /* callback = */ mb_read,
923         /* interval = */ (host->interval > 0) ? &interval : NULL,
924         &ud);
925   }
926   else
927   {
928     host_free (host);
929   }
930
931   return (status);
932 } /* }}} int mb_config_add_host */
933
934 static int mb_config (oconfig_item_t *ci) /* {{{ */
935 {
936   int i;
937
938   if (ci == NULL)
939     return (EINVAL);
940
941   for (i = 0; i < ci->children_num; i++)
942   {
943     oconfig_item_t *child = ci->children + i;
944
945     if (strcasecmp ("Data", child->key) == 0)
946       mb_config_add_data (child);
947     else if (strcasecmp ("Host", child->key) == 0)
948       mb_config_add_host (child);
949     else
950       ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
951   }
952
953   return (0);
954 } /* }}} int mb_config */
955
956 /* ========= */
957
958 static int mb_shutdown (void) /* {{{ */
959 {
960   data_free_all (data_definitions);
961   data_definitions = NULL;
962
963   return (0);
964 } /* }}} int mb_shutdown */
965
966 void module_register (void)
967 {
968   plugin_register_complex_config ("modbus", mb_config);
969   plugin_register_shutdown ("modbus", mb_shutdown);
970 } /* void module_register */
971
972 /* vim: set sw=2 sts=2 et fdm=marker : */