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