2 * collectd - src/modbus.c
3 * Copyright (C) 2010,2011 noris network AG
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
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.
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
20 * Florian Forster <octo at noris.net>
26 #include "configfile.h"
30 #include <modbus/modbus.h>
32 #ifndef LIBMODBUS_VERSION_CHECK
33 /* Assume version 2.0.3 */
34 # define LEGACY_LIBMODBUS 1
36 /* Assume version 2.9.2 */
39 #ifndef MODBUS_TCP_DEFAULT_PORT
40 # ifdef MODBUS_TCP_PORT
41 # define MODBUS_TCP_DEFAULT_PORT MODBUS_TCP_PORT
43 # define MODBUS_TCP_DEFAULT_PORT 502
52 * ModbusRegisterType holding
62 * Instance "foobar" # optional
71 enum mb_register_type_e /* {{{ */
79 enum mb_mreg_type_e /* {{{ */
84 typedef enum mb_register_type_e mb_register_type_t;
85 typedef enum mb_mreg_type_e mb_mreg_type_t;
88 typedef struct mb_data_s mb_data_t;
89 struct mb_data_s /* {{{ */
93 mb_register_type_t register_type;
94 mb_mreg_type_t modbus_register_type;
95 char type[DATA_MAX_NAME_LEN];
96 char instance[DATA_MAX_NAME_LEN];
101 struct mb_slave_s /* {{{ */
104 char instance[DATA_MAX_NAME_LEN];
107 typedef struct mb_slave_s mb_slave_t;
109 struct mb_host_s /* {{{ */
111 char host[DATA_MAX_NAME_LEN];
112 char node[NI_MAXHOST];
113 /* char service[NI_MAXSERV]; */
121 modbus_param_t connection;
123 modbus_t *connection;
127 typedef struct mb_host_s mb_host_t;
129 struct mb_data_group_s;
130 typedef struct mb_data_group_s mb_data_group_t;
131 struct mb_data_group_s /* {{{ */
133 mb_data_t *registers;
134 size_t registers_num;
136 mb_data_group_t *next;
142 static mb_data_t *data_definitions = NULL;
147 static mb_data_t *data_get_by_name (mb_data_t *src, /* {{{ */
155 for (ptr = src; ptr != NULL; ptr = ptr->next)
156 if (strcasecmp (ptr->name, name) == 0)
160 } /* }}} mb_data_t *data_get_by_name */
162 static int data_append (mb_data_t **dst, mb_data_t *src) /* {{{ */
166 if ((dst == NULL) || (src == NULL))
177 while (ptr->next != NULL)
183 } /* }}} int data_append */
185 /* Copy a single mb_data_t and append it to another list. */
186 static int data_copy (mb_data_t **dst, const mb_data_t *src) /* {{{ */
191 if ((dst == NULL) || (src == NULL))
194 tmp = malloc (sizeof (*tmp));
197 memcpy (tmp, src, sizeof (*tmp));
201 tmp->name = strdup (src->name);
202 if (tmp->name == NULL)
208 status = data_append (dst, tmp);
217 } /* }}} int data_copy */
219 /* Lookup a single mb_data_t instance, copy it and append the copy to another
221 static int data_copy_by_name (mb_data_t **dst, mb_data_t *src, /* {{{ */
226 if ((dst == NULL) || (src == NULL) || (name == NULL))
229 ptr = data_get_by_name (src, name);
233 return (data_copy (dst, ptr));
234 } /* }}} int data_copy_by_name */
238 static int mb_submit (mb_host_t *host, mb_slave_t *slave, /* {{{ */
239 mb_data_t *data, value_t value)
241 value_list_t vl = VALUE_LIST_INIT;
243 if ((host == NULL) || (slave == NULL) || (data == NULL))
246 if (host->interval <= 0)
247 host->interval = plugin_get_interval ();
249 if (slave->instance[0] == 0)
250 ssnprintf (slave->instance, sizeof (slave->instance), "slave_%i",
255 vl.interval = host->interval;
256 sstrncpy (vl.host, host->host, sizeof (vl.host));
257 sstrncpy (vl.plugin, "modbus", sizeof (vl.plugin));
258 sstrncpy (vl.plugin_instance, slave->instance, sizeof (vl.plugin_instance));
259 sstrncpy (vl.type, data->type, sizeof (vl.type));
260 sstrncpy (vl.type_instance, data->instance, sizeof (vl.type_instance));
262 return (plugin_dispatch_values (&vl));
263 } /* }}} int mb_submit */
265 static float mb_register_to_float (uint16_t hi, uint16_t lo) /* {{{ */
274 #if BYTE_ORDER == LITTLE_ENDIAN
276 conv.b[0] = lo & 0x00ff;
277 conv.b[1] = (lo >> 8) & 0x00ff;
278 conv.b[2] = hi & 0x00ff;
279 conv.b[3] = (hi >> 8) & 0x00ff;
281 conv.b[3] = lo & 0x00ff;
282 conv.b[2] = (lo >> 8) & 0x00ff;
283 conv.b[1] = hi & 0x00ff;
284 conv.b[0] = (hi >> 8) & 0x00ff;
288 } /* }}} float mb_register_to_float */
292 static int mb_init_connection (mb_host_t *host) /* {{{ */
299 modbus_set_debug (&host->connection, 1);
301 /* We'll do the error handling ourselves. */
302 modbus_set_error_handling (&host->connection, NOP_ON_ERROR);
304 if ((host->port < 1) || (host->port > 65535))
305 host->port = MODBUS_TCP_DEFAULT_PORT;
307 DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
308 host->node, host->port);
310 modbus_init_tcp (&host->connection,
311 /* host = */ host->node,
312 /* port = */ host->port);
314 status = modbus_connect (&host->connection);
317 ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
318 host->node, host->port, status);
322 host->is_connected = 1;
324 } /* }}} int mb_init_connection */
325 /* #endif LEGACY_LIBMODBUS */
327 #else /* if !LEGACY_LIBMODBUS */
329 static int mb_init_connection (mb_host_t *host) /* {{{ */
336 if (host->connection != NULL)
339 if ((host->port < 1) || (host->port > 65535))
340 host->port = MODBUS_TCP_DEFAULT_PORT;
342 DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
343 host->node, host->port);
345 host->connection = modbus_new_tcp (host->node, host->port);
346 if (host->connection == NULL)
348 ERROR ("Modbus plugin: Creating new Modbus/TCP object failed.");
352 modbus_set_debug (host->connection, 1);
354 /* We'll do the error handling ourselves. */
355 modbus_set_error_recovery (host->connection, 0);
357 status = modbus_connect (host->connection);
360 ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
361 host->node, host->port, status);
362 modbus_free (host->connection);
363 host->connection = NULL;
368 } /* }}} int mb_init_connection */
369 #endif /* !LEGACY_LIBMODBUS */
371 #define CAST_TO_VALUE_T(ds,vt,raw) do { \
372 if ((ds)->ds[0].type == DS_TYPE_COUNTER) \
373 (vt).counter = (counter_t) (raw); \
374 else if ((ds)->ds[0].type == DS_TYPE_GAUGE) \
375 (vt).gauge = (gauge_t) (raw); \
376 else if ((ds)->ds[0].type == DS_TYPE_DERIVE) \
377 (vt).derive = (derive_t) (raw); \
378 else /* if (ds->ds[0].type == DS_TYPE_ABSOLUTE) */ \
379 (vt).absolute = (absolute_t) (raw); \
382 static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */
387 const data_set_t *ds;
390 if ((host == NULL) || (slave == NULL) || (data == NULL))
393 ds = plugin_get_ds (data->type);
396 ERROR ("Modbus plugin: Type \"%s\" is not defined.", data->type);
402 ERROR ("Modbus plugin: The type \"%s\" has %i data sources. "
403 "I can only handle data sets with only one data source.",
404 data->type, ds->ds_num);
408 if ((ds->ds[0].type != DS_TYPE_GAUGE)
409 && (data->register_type != REG_TYPE_INT32)
410 && (data->register_type != REG_TYPE_UINT32))
412 NOTICE ("Modbus plugin: The data source of type \"%s\" is %s, not gauge. "
413 "This will most likely result in problems, because the register type "
414 "is not UINT32.", data->type, DS_TYPE_TO_STRING (ds->ds[0].type));
417 memset (values, 0, sizeof (values));
418 if ((data->register_type == REG_TYPE_INT32)
419 || (data->register_type == REG_TYPE_UINT32)
420 || (data->register_type == REG_TYPE_FLOAT))
426 if (host->connection == NULL)
432 struct sockaddr sockaddr;
433 socklen_t saddrlen = sizeof (sockaddr);
435 status = getpeername (modbus_get_socket (host->connection),
436 &sockaddr, &saddrlen);
441 if ((status == EBADF) || (status == ENOTSOCK) || (status == ENOTCONN))
443 status = mb_init_connection (host);
446 ERROR ("Modbus plugin: mb_init_connection (%s/%s) failed. ",
447 host->host, host->node);
448 host->is_connected = 0;
449 host->connection = NULL;
453 else if (status != 0)
456 modbus_close (&host->connection);
458 modbus_close (host->connection);
459 modbus_free (host->connection);
464 /* Version 2.0.3: Pass the connection struct as a pointer and pass the slave
465 * id to each call of "read_holding_registers". */
466 # define modbus_read_registers(ctx, addr, nb, dest) \
467 read_holding_registers (&(ctx), slave->id, (addr), (nb), (dest))
468 #else /* if !LEGACY_LIBMODBUS */
469 /* Version 2.9.2: Set the slave id once before querying the registers. */
470 status = modbus_set_slave (host->connection, slave->id);
473 ERROR ("Modbus plugin: modbus_set_slave (%i) failed with status %i.",
478 if (data->modbus_register_type == MREG_INPUT){
479 status = modbus_read_input_registers (host->connection,
480 /* start_addr = */ data->register_base,
481 /* num_registers = */ values_num, /* buffer = */ values);
484 status = modbus_read_registers (host->connection,
485 /* start_addr = */ data->register_base,
486 /* num_registers = */ values_num, /* buffer = */ values);
488 if (status != values_num)
490 ERROR ("Modbus plugin: modbus read function (%s/%s) failed. "
491 " status = %i, values_num = %i. Giving up.",
492 host->host, host->node, status, values_num);
494 modbus_close (&host->connection);
496 modbus_close (host->connection);
497 modbus_free (host->connection);
499 host->connection = NULL;
503 DEBUG ("Modbus plugin: mb_read_data: Success! "
504 "modbus_read_registers returned with status %i.", status);
506 if (data->register_type == REG_TYPE_FLOAT)
511 float_value = mb_register_to_float (values[0], values[1]);
512 DEBUG ("Modbus plugin: mb_read_data: "
513 "Returned float value is %g", (double) float_value);
515 CAST_TO_VALUE_T (ds, vt, float_value);
516 mb_submit (host, slave, data, vt);
518 else if (data->register_type == REG_TYPE_INT32)
527 v.u32 = (((uint32_t) values[0]) << 16)
528 | ((uint32_t) values[1]);
529 DEBUG ("Modbus plugin: mb_read_data: "
530 "Returned int32 value is %"PRIi32, v.i32);
532 CAST_TO_VALUE_T (ds, vt, v.i32);
533 mb_submit (host, slave, data, vt);
535 else if (data->register_type == REG_TYPE_INT16)
546 DEBUG ("Modbus plugin: mb_read_data: "
547 "Returned int16 value is %"PRIi16, v.i16);
549 CAST_TO_VALUE_T (ds, vt, v.i16);
550 mb_submit (host, slave, data, vt);
552 else if (data->register_type == REG_TYPE_UINT32)
557 v32 = (((uint32_t) values[0]) << 16)
558 | ((uint32_t) values[1]);
559 DEBUG ("Modbus plugin: mb_read_data: "
560 "Returned uint32 value is %"PRIu32, v32);
562 CAST_TO_VALUE_T (ds, vt, v32);
563 mb_submit (host, slave, data, vt);
565 else /* if (data->register_type == REG_TYPE_UINT16) */
569 DEBUG ("Modbus plugin: mb_read_data: "
570 "Returned uint16 value is %"PRIu16, values[0]);
572 CAST_TO_VALUE_T (ds, vt, values[0]);
573 mb_submit (host, slave, data, vt);
577 } /* }}} int mb_read_data */
579 static int mb_read_slave (mb_host_t *host, mb_slave_t *slave) /* {{{ */
585 if ((host == NULL) || (slave == NULL))
589 for (data = slave->collect; data != NULL; data = data->next)
591 status = mb_read_data (host, slave, data);
600 } /* }}} int mb_read_slave */
602 static int mb_read (user_data_t *user_data) /* {{{ */
609 if ((user_data == NULL) || (user_data->data == NULL))
612 host = user_data->data;
615 for (i = 0; i < host->slaves_num; i++)
617 status = mb_read_slave (host, host->slaves + i);
626 } /* }}} int mb_read */
630 static void data_free_one (mb_data_t *data) /* {{{ */
637 } /* }}} void data_free_one */
639 static void data_free_all (mb_data_t *data) /* {{{ */
647 data_free_one (data);
649 data_free_all (next);
650 } /* }}} void data_free_all */
652 static void slaves_free_all (mb_slave_t *slaves, size_t slaves_num) /* {{{ */
659 for (i = 0; i < slaves_num; i++)
660 data_free_all (slaves[i].collect);
662 } /* }}} void slaves_free_all */
664 static void host_free (void *void_host) /* {{{ */
666 mb_host_t *host = void_host;
671 slaves_free_all (host->slaves, host->slaves_num);
673 } /* }}} void host_free */
675 /* Config functions */
677 static int mb_config_add_data (oconfig_item_t *ci) /* {{{ */
683 memset (&data, 0, sizeof (data));
685 data.register_type = REG_TYPE_UINT16;
688 status = cf_util_get_string (ci, &data.name);
692 for (i = 0; i < ci->children_num; i++)
694 oconfig_item_t *child = ci->children + i;
697 if (strcasecmp ("Type", child->key) == 0)
698 status = cf_util_get_string_buffer (child,
699 data.type, sizeof (data.type));
700 else if (strcasecmp ("Instance", child->key) == 0)
701 status = cf_util_get_string_buffer (child,
702 data.instance, sizeof (data.instance));
703 else if (strcasecmp ("RegisterBase", child->key) == 0)
704 status = cf_util_get_int (child, &data.register_base);
705 else if (strcasecmp ("RegisterType", child->key) == 0)
708 status = cf_util_get_string_buffer (child, tmp, sizeof (tmp));
711 else if (strcasecmp ("Int16", tmp) == 0)
712 data.register_type = REG_TYPE_INT16;
713 else if (strcasecmp ("Int32", tmp) == 0)
714 data.register_type = REG_TYPE_INT32;
715 else if (strcasecmp ("Uint16", tmp) == 0)
716 data.register_type = REG_TYPE_UINT16;
717 else if (strcasecmp ("Uint32", tmp) == 0)
718 data.register_type = REG_TYPE_UINT32;
719 else if (strcasecmp ("Float", tmp) == 0)
720 data.register_type = REG_TYPE_FLOAT;
723 ERROR ("Modbus plugin: The register type \"%s\" is unknown.", tmp);
727 else if (strcasecmp ("ModbusRegisterType", child->key) == 0)
730 ERROR("Modbus plugin: ModbusRegisterType parameter can not be used "
731 "with your libmodbus version");
734 status = cf_util_get_string_buffer (child, tmp, sizeof (tmp));
737 else if (strcasecmp ("holding", tmp) == 0)
738 data.modbus_register_type = MREG_HOLDING;
739 else if (strcasecmp ("input", tmp) == 0)
740 data.modbus_register_type = MREG_INPUT;
743 ERROR ("Modbus plugin: The modbus_register_type \"%s\" is unknown.",
751 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
757 } /* for (i = 0; i < ci->children_num; i++) */
759 assert (data.name != NULL);
760 if (data.type[0] == 0)
762 ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
768 data_copy (&data_definitions, &data);
773 } /* }}} int mb_config_add_data */
775 static int mb_config_set_host_address (mb_host_t *host, /* {{{ */
778 struct addrinfo *ai_list;
779 struct addrinfo *ai_ptr;
780 struct addrinfo ai_hints;
783 if ((host == NULL) || (address == NULL))
786 memset (&ai_hints, 0, sizeof (ai_hints));
788 ai_hints.ai_flags |= AI_ADDRCONFIG;
790 /* XXX: libmodbus can only handle IPv4 addresses. */
791 ai_hints.ai_family = AF_INET;
792 ai_hints.ai_addr = NULL;
793 ai_hints.ai_canonname = NULL;
794 ai_hints.ai_next = NULL;
797 status = getaddrinfo (address, /* service = */ NULL,
798 &ai_hints, &ai_list);
802 ERROR ("Modbus plugin: getaddrinfo failed: %s",
803 (status == EAI_SYSTEM)
804 ? sstrerror (errno, errbuf, sizeof (errbuf))
805 : gai_strerror (status));
809 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
811 status = getnameinfo (ai_ptr->ai_addr, ai_ptr->ai_addrlen,
812 host->node, sizeof (host->node),
813 /* service = */ NULL, /* length = */ 0,
814 /* flags = */ NI_NUMERICHOST);
819 freeaddrinfo (ai_list);
822 ERROR ("Modbus plugin: Unable to translate node name: \"%s\"", address);
823 else /* if (status == 0) */
825 DEBUG ("Modbus plugin: mb_config_set_host_address: %s -> %s",
826 address, host->node);
830 } /* }}} int mb_config_set_host_address */
832 static int mb_config_add_slave (mb_host_t *host, oconfig_item_t *ci) /* {{{ */
838 if ((host == NULL) || (ci == NULL))
841 slave = realloc (host->slaves, sizeof (*slave) * (host->slaves_num + 1));
844 host->slaves = slave;
845 slave = host->slaves + host->slaves_num;
846 memset (slave, 0, sizeof (*slave));
847 slave->collect = NULL;
849 status = cf_util_get_int (ci, &slave->id);
853 for (i = 0; i < ci->children_num; i++)
855 oconfig_item_t *child = ci->children + i;
858 if (strcasecmp ("Instance", child->key) == 0)
859 status = cf_util_get_string_buffer (child,
860 slave->instance, sizeof (slave->instance));
861 else if (strcasecmp ("Collect", child->key) == 0)
864 status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
866 data_copy_by_name (&slave->collect, data_definitions, buffer);
867 status = 0; /* continue after failure. */
871 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
879 if ((status == 0) && (slave->collect == NULL))
887 else /* if (status != 0) */
888 data_free_all (slave->collect);
891 } /* }}} int mb_config_add_slave */
893 static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */
899 host = malloc (sizeof (*host));
902 memset (host, 0, sizeof (*host));
905 status = cf_util_get_string_buffer (ci, host->host, sizeof (host->host));
908 if (host->host[0] == 0)
911 for (i = 0; i < ci->children_num; i++)
913 oconfig_item_t *child = ci->children + i;
916 if (strcasecmp ("Address", child->key) == 0)
918 char buffer[NI_MAXHOST];
919 status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
921 status = mb_config_set_host_address (host, buffer);
923 else if (strcasecmp ("Port", child->key) == 0)
925 host->port = cf_util_get_port_number (child);
929 else if (strcasecmp ("Interval", child->key) == 0)
930 status = cf_util_get_cdtime (child, &host->interval);
931 else if (strcasecmp ("Slave", child->key) == 0)
932 /* Don't set status: Gracefully continue if a slave fails. */
933 mb_config_add_slave (host, child);
936 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
942 } /* for (i = 0; i < ci->children_num; i++) */
944 assert (host->host[0] != 0);
945 if (host->host[0] == 0)
947 ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
956 struct timespec interval = { 0, 0 };
959 ud.free_func = host_free;
961 ssnprintf (name, sizeof (name), "modbus-%s", host->host);
963 CDTIME_T_TO_TIMESPEC (host->interval, &interval);
965 plugin_register_complex_read (/* group = */ NULL, name,
966 /* callback = */ mb_read,
967 /* interval = */ (host->interval > 0) ? &interval : NULL,
976 } /* }}} int mb_config_add_host */
978 static int mb_config (oconfig_item_t *ci) /* {{{ */
985 for (i = 0; i < ci->children_num; i++)
987 oconfig_item_t *child = ci->children + i;
989 if (strcasecmp ("Data", child->key) == 0)
990 mb_config_add_data (child);
991 else if (strcasecmp ("Host", child->key) == 0)
992 mb_config_add_host (child);
994 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
998 } /* }}} int mb_config */
1002 static int mb_shutdown (void) /* {{{ */
1004 data_free_all (data_definitions);
1005 data_definitions = NULL;
1008 } /* }}} int mb_shutdown */
1010 void module_register (void)
1012 plugin_register_complex_config ("modbus", mb_config);
1013 plugin_register_shutdown ("modbus", mb_shutdown);
1014 } /* void module_register */
1016 /* vim: set sw=2 sts=2 et fdm=marker : */