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