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