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 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
28 #include <netdb.h>
30 #include <modbus/modbus.h>
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
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
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 */
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;
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];
90 mb_data_t *next;
91 }; /* }}} */
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;
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;
109 mb_slave_t *slaves;
110 size_t slaves_num;
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;
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;
129 mb_data_group_t *next;
130 }; /* }}} */
132 /*
133 * Global variables
134 */
135 static mb_data_t *data_definitions = NULL;
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;
145 if (name == NULL)
146 return (NULL);
148 for (ptr = src; ptr != NULL; ptr = ptr->next)
149 if (strcasecmp (ptr->name, name) == 0)
150 return (ptr);
152 return (NULL);
153 } /* }}} mb_data_t *data_get_by_name */
155 static int data_append (mb_data_t **dst, mb_data_t *src) /* {{{ */
156 {
157 mb_data_t *ptr;
159 if ((dst == NULL) || (src == NULL))
160 return (EINVAL);
162 ptr = *dst;
164 if (ptr == NULL)
165 {
166 *dst = src;
167 return (0);
168 }
170 while (ptr->next != NULL)
171 ptr = ptr->next;
173 ptr->next = src;
175 return (0);
176 } /* }}} int data_append */
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;
184 if ((dst == NULL) || (src == NULL))
185 return (EINVAL);
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;
194 tmp->name = strdup (src->name);
195 if (tmp->name == NULL)
196 {
197 sfree (tmp);
198 return (ENOMEM);
199 }
201 status = data_append (dst, tmp);
202 if (status != 0)
203 {
204 sfree (tmp->name);
205 sfree (tmp);
206 return (status);
207 }
209 return (0);
210 } /* }}} int data_copy */
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;
219 if ((dst == NULL) || (src == NULL) || (name == NULL))
220 return (EINVAL);
222 ptr = data_get_by_name (src, name);
223 if (ptr == NULL)
224 return (ENOENT);
226 return (data_copy (dst, ptr));
227 } /* }}} int data_copy_by_name */
229 /* Read functions */
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;
236 if ((host == NULL) || (slave == NULL) || (data == NULL))
237 return (EINVAL);
239 if (host->interval <= 0)
240 host->interval = interval_g;
242 if (slave->instance[0] == 0)
243 ssnprintf (slave->instance, sizeof (slave->instance), "slave_%i",
244 slave->id);
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));
255 return (plugin_dispatch_values (&vl));
256 } /* }}} int mb_submit */
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;
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
279 return (conv.f);
280 } /* }}} float mb_register_to_float */
282 #if LEGACY_LIBMODBUS
283 /* Version 2.0.3 */
284 static int mb_init_connection (mb_host_t *host) /* {{{ */
285 {
286 int status;
288 if (host == NULL)
289 return (EINVAL);
291 if (host->is_connected)
292 return (0);
294 /* Only reconnect once per interval. */
295 if (host->have_reconnected)
296 return (-1);
298 modbus_set_debug (&host->connection, 1);
300 /* We'll do the error handling ourselves. */
301 modbus_set_error_handling (&host->connection, NOP_ON_ERROR);
303 if ((host->port < 1) || (host->port > 65535))
304 host->port = MODBUS_TCP_DEFAULT_PORT;
306 DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
307 host->node, host->port);
309 modbus_init_tcp (&host->connection,
310 /* host = */ host->node,
311 /* port = */ host->port);
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 }
321 host->is_connected = 1;
322 host->have_reconnected = 1;
323 return (0);
324 } /* }}} int mb_init_connection */
325 /* #endif LEGACY_LIBMODBUS */
327 #else /* if !LEGACY_LIBMODBUS */
328 /* Version 2.9.2 */
329 static int mb_init_connection (mb_host_t *host) /* {{{ */
330 {
331 int status;
333 if (host == NULL)
334 return (EINVAL);
336 if (host->connection != NULL)
337 return (0);
339 /* Only reconnect once per interval. */
340 if (host->have_reconnected)
341 return (-1);
343 if ((host->port < 1) || (host->port > 65535))
344 host->port = MODBUS_TCP_DEFAULT_PORT;
346 DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
347 host->node, host->port);
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 }
357 modbus_set_debug (host->connection, 1);
359 /* We'll do the error handling ourselves. */
360 modbus_set_error_recovery (host->connection, 0);
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 }
372 host->have_reconnected = 1;
373 return (0);
374 } /* }}} int mb_init_connection */
375 #endif /* !LEGACY_LIBMODBUS */
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)
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;
397 if ((host == NULL) || (slave == NULL) || (data == NULL))
398 return (EINVAL);
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 }
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 }
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 }
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;
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
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;
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 }
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 }
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 }
488 DEBUG ("Modbus plugin: Re-established connection to %s", host->host);
490 /* try again */
491 continue;
492 } /* for (i = 0, 1) */
494 DEBUG ("Modbus plugin: mb_read_data: Success! "
495 "modbus_read_registers returned with status %i.", status);
497 if (data->register_type == REG_TYPE_FLOAT)
498 {
499 float float_value;
500 value_t vt;
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);
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;
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);
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;
535 v.u16 = values[0];
537 DEBUG ("Modbus plugin: mb_read_data: "
538 "Returned int16 value is %"PRIi16, v.i16);
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;
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);
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;
560 DEBUG ("Modbus plugin: mb_read_data: "
561 "Returned uint16 value is %"PRIu16, values[0]);
563 CAST_TO_VALUE_T (ds, vt, values[0]);
564 mb_submit (host, slave, data, vt);
565 }
567 return (0);
568 } /* }}} int mb_read_data */
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;
576 if ((host == NULL) || (slave == NULL))
577 return (EINVAL);
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 }
587 if (success == 0)
588 return (-1);
589 else
590 return (0);
591 } /* }}} int mb_read_slave */
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;
600 if ((user_data == NULL) || (user_data->data == NULL))
601 return (EINVAL);
603 host = user_data->data;
605 /* Clear the reconnect flag. */
606 host->have_reconnected = 0;
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 }
616 if (success == 0)
617 return (-1);
618 else
619 return (0);
620 } /* }}} int mb_read */
622 /* Free functions */
624 static void data_free_one (mb_data_t *data) /* {{{ */
625 {
626 if (data == NULL)
627 return;
629 sfree (data->name);
630 sfree (data);
631 } /* }}} void data_free_one */
633 static void data_free_all (mb_data_t *data) /* {{{ */
634 {
635 mb_data_t *next;
637 if (data == NULL)
638 return;
640 next = data->next;
641 data_free_one (data);
643 data_free_all (next);
644 } /* }}} void data_free_all */
646 static void slaves_free_all (mb_slave_t *slaves, size_t slaves_num) /* {{{ */
647 {
648 size_t i;
650 if (slaves == NULL)
651 return;
653 for (i = 0; i < slaves_num; i++)
654 data_free_all (slaves[i].collect);
655 sfree (slaves);
656 } /* }}} void slaves_free_all */
658 static void host_free (void *void_host) /* {{{ */
659 {
660 mb_host_t *host = void_host;
662 if (host == NULL)
663 return;
665 slaves_free_all (host->slaves, host->slaves_num);
666 sfree (host);
667 } /* }}} void host_free */
669 /* Config functions */
671 static int mb_config_add_data (oconfig_item_t *ci) /* {{{ */
672 {
673 mb_data_t data;
674 int status;
675 int i;
677 memset (&data, 0, sizeof (data));
678 data.name = NULL;
679 data.register_type = REG_TYPE_UINT16;
680 data.next = NULL;
682 status = cf_util_get_string (ci, &data.name);
683 if (status != 0)
684 return (status);
686 for (i = 0; i < ci->children_num; i++)
687 {
688 oconfig_item_t *child = ci->children + i;
689 status = 0;
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 }
727 if (status != 0)
728 break;
729 } /* for (i = 0; i < ci->children_num; i++) */
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 }
739 if (status == 0)
740 data_copy (&data_definitions, &data);
742 sfree (data.name);
744 return (status);
745 } /* }}} int mb_config_add_data */
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;
755 if ((host == NULL) || (address == NULL))
756 return (EINVAL);
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;
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 }
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) */
791 freeaddrinfo (ai_list);
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 }
801 return (status);
802 } /* }}} int mb_config_set_host_address */
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;
810 if ((host == NULL) || (ci == NULL))
811 return (EINVAL);
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;
821 status = cf_util_get_int (ci, &slave->id);
822 if (status != 0)
823 return (status);
825 for (i = 0; i < ci->children_num; i++)
826 {
827 oconfig_item_t *child = ci->children + i;
828 status = 0;
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 }
847 if (status != 0)
848 break;
849 }
851 if ((status == 0) && (slave->collect == NULL))
852 status = EINVAL;
854 if (slave->id < 0)
855 status = EINVAL;
857 if (status == 0)
858 host->slaves_num++;
859 else /* if (status != 0) */
860 data_free_all (slave->collect);
862 return (status);
863 } /* }}} int mb_config_add_slave */
865 static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */
866 {
867 mb_host_t *host;
868 int status;
869 int i;
871 host = malloc (sizeof (*host));
872 if (host == NULL)
873 return (ENOMEM);
874 memset (host, 0, sizeof (*host));
875 host->slaves = NULL;
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);
883 for (i = 0; i < ci->children_num; i++)
884 {
885 oconfig_item_t *child = ci->children + i;
886 status = 0;
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 }
912 if (status != 0)
913 break;
914 } /* for (i = 0; i < ci->children_num; i++) */
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 }
924 if (status == 0)
925 {
926 user_data_t ud;
927 char name[1024];
928 struct timespec interval = { 0, 0 };
930 ud.data = host;
931 ud.free_func = host_free;
933 ssnprintf (name, sizeof (name), "modbus-%s", host->host);
935 CDTIME_T_TO_TIMESPEC (host->interval, &interval);
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 }
947 return (status);
948 } /* }}} int mb_config_add_host */
950 static int mb_config (oconfig_item_t *ci) /* {{{ */
951 {
952 int i;
954 if (ci == NULL)
955 return (EINVAL);
957 for (i = 0; i < ci->children_num; i++)
958 {
959 oconfig_item_t *child = ci->children + i;
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 }
969 return (0);
970 } /* }}} int mb_config */
972 /* ========= */
974 static int mb_shutdown (void) /* {{{ */
975 {
976 data_free_all (data_definitions);
977 data_definitions = NULL;
979 return (0);
980 } /* }}} int mb_shutdown */
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 */
988 /* vim: set sw=2 sts=2 et fdm=marker : */