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