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
61 * Instance "foobar" # optional
70 enum mb_register_type_e /* {{{ */
78 typedef enum mb_register_type_e mb_register_type_t;
81 typedef struct mb_data_s mb_data_t;
82 struct mb_data_s /* {{{ */
86 mb_register_type_t register_type;
87 char type[DATA_MAX_NAME_LEN];
88 char instance[DATA_MAX_NAME_LEN];
93 struct mb_slave_s /* {{{ */
96 char instance[DATA_MAX_NAME_LEN];
99 typedef struct mb_slave_s mb_slave_t;
101 struct mb_host_s /* {{{ */
103 char host[DATA_MAX_NAME_LEN];
104 char node[NI_MAXHOST];
105 /* char service[NI_MAXSERV]; */
113 modbus_param_t connection;
115 modbus_t *connection;
119 typedef struct mb_host_s mb_host_t;
121 struct mb_data_group_s;
122 typedef struct mb_data_group_s mb_data_group_t;
123 struct mb_data_group_s /* {{{ */
125 mb_data_t *registers;
126 size_t registers_num;
128 mb_data_group_t *next;
134 static mb_data_t *data_definitions = NULL;
139 static mb_data_t *data_get_by_name (mb_data_t *src, /* {{{ */
147 for (ptr = src; ptr != NULL; ptr = ptr->next)
148 if (strcasecmp (ptr->name, name) == 0)
152 } /* }}} mb_data_t *data_get_by_name */
154 static int data_append (mb_data_t **dst, mb_data_t *src) /* {{{ */
158 if ((dst == NULL) || (src == NULL))
169 while (ptr->next != NULL)
175 } /* }}} int data_append */
177 /* Copy a single mb_data_t and append it to another list. */
178 static int data_copy (mb_data_t **dst, const mb_data_t *src) /* {{{ */
183 if ((dst == NULL) || (src == NULL))
186 tmp = malloc (sizeof (*tmp));
189 memcpy (tmp, src, sizeof (*tmp));
193 tmp->name = strdup (src->name);
194 if (tmp->name == NULL)
200 status = data_append (dst, tmp);
209 } /* }}} int data_copy */
211 /* Lookup a single mb_data_t instance, copy it and append the copy to another
213 static int data_copy_by_name (mb_data_t **dst, mb_data_t *src, /* {{{ */
218 if ((dst == NULL) || (src == NULL) || (name == NULL))
221 ptr = data_get_by_name (src, name);
225 return (data_copy (dst, ptr));
226 } /* }}} int data_copy_by_name */
230 static int mb_submit (mb_host_t *host, mb_slave_t *slave, /* {{{ */
231 mb_data_t *data, value_t value)
233 value_list_t vl = VALUE_LIST_INIT;
235 if ((host == NULL) || (slave == NULL) || (data == NULL))
238 if (host->interval <= 0)
239 host->interval = plugin_get_interval ();
241 if (slave->instance[0] == 0)
242 ssnprintf (slave->instance, sizeof (slave->instance), "slave_%i",
247 vl.interval = host->interval;
248 sstrncpy (vl.host, host->host, sizeof (vl.host));
249 sstrncpy (vl.plugin, "modbus", sizeof (vl.plugin));
250 sstrncpy (vl.plugin_instance, slave->instance, sizeof (vl.plugin_instance));
251 sstrncpy (vl.type, data->type, sizeof (vl.type));
252 sstrncpy (vl.type_instance, data->instance, sizeof (vl.type_instance));
254 return (plugin_dispatch_values (&vl));
255 } /* }}} int mb_submit */
257 static float mb_register_to_float (uint16_t hi, uint16_t lo) /* {{{ */
266 #if BYTE_ORDER == 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;
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;
280 } /* }}} float mb_register_to_float */
284 static int mb_init_connection (mb_host_t *host) /* {{{ */
291 modbus_set_debug (&host->connection, 1);
293 /* We'll do the error handling ourselves. */
294 modbus_set_error_handling (&host->connection, NOP_ON_ERROR);
296 if ((host->port < 1) || (host->port > 65535))
297 host->port = MODBUS_TCP_DEFAULT_PORT;
299 DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
300 host->node, host->port);
302 modbus_init_tcp (&host->connection,
303 /* host = */ host->node,
304 /* port = */ host->port);
306 status = modbus_connect (&host->connection);
309 ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
310 host->node, host->port, status);
314 host->is_connected = 1;
316 } /* }}} int mb_init_connection */
317 /* #endif LEGACY_LIBMODBUS */
319 #else /* if !LEGACY_LIBMODBUS */
321 static int mb_init_connection (mb_host_t *host) /* {{{ */
328 if (host->connection != NULL)
331 if ((host->port < 1) || (host->port > 65535))
332 host->port = MODBUS_TCP_DEFAULT_PORT;
334 DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
335 host->node, host->port);
337 host->connection = modbus_new_tcp (host->node, host->port);
338 if (host->connection == NULL)
340 ERROR ("Modbus plugin: Creating new Modbus/TCP object failed.");
344 modbus_set_debug (host->connection, 1);
346 /* We'll do the error handling ourselves. */
347 modbus_set_error_recovery (host->connection, 0);
349 status = modbus_connect (host->connection);
352 ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
353 host->node, host->port, status);
354 modbus_free (host->connection);
355 host->connection = NULL;
360 } /* }}} int mb_init_connection */
361 #endif /* !LEGACY_LIBMODBUS */
363 #define CAST_TO_VALUE_T(ds,vt,raw) do { \
364 if ((ds)->ds[0].type == DS_TYPE_COUNTER) \
365 (vt).counter = (counter_t) (raw); \
366 else if ((ds)->ds[0].type == DS_TYPE_GAUGE) \
367 (vt).gauge = (gauge_t) (raw); \
368 else if ((ds)->ds[0].type == DS_TYPE_DERIVE) \
369 (vt).derive = (derive_t) (raw); \
370 else /* if (ds->ds[0].type == DS_TYPE_ABSOLUTE) */ \
371 (vt).absolute = (absolute_t) (raw); \
374 static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */
379 const data_set_t *ds;
380 struct sockaddr sockaddr;
381 socklen_t saddrlen = sizeof(sockaddr);
384 if ((host == NULL) || (slave == NULL) || (data == NULL))
387 ds = plugin_get_ds (data->type);
390 ERROR ("Modbus plugin: Type \"%s\" is not defined.", data->type);
396 ERROR ("Modbus plugin: The type \"%s\" has %i data sources. "
397 "I can only handle data sets with only one data source.",
398 data->type, ds->ds_num);
402 if ((ds->ds[0].type != DS_TYPE_GAUGE)
403 && (data->register_type != REG_TYPE_INT32)
404 && (data->register_type != REG_TYPE_UINT32))
406 NOTICE ("Modbus plugin: The data source of type \"%s\" is %s, not gauge. "
407 "This will most likely result in problems, because the register type "
408 "is not UINT32.", data->type, DS_TYPE_TO_STRING (ds->ds[0].type));
411 memset (values, 0, sizeof (values));
412 if ((data->register_type == REG_TYPE_INT32)
413 || (data->register_type == REG_TYPE_UINT32)
414 || (data->register_type == REG_TYPE_FLOAT))
419 if(host->connection == NULL)
422 if(getpeername(modbus_get_socket(host->connection),
423 &sockaddr, &saddrlen) != 0)
429 status = mb_init_connection (host);
432 ERROR ("Modbus plugin: mb_init_connection (%s/%s) failed. ",
433 host->host, host->node);
434 host->is_connected = 0;
435 host->connection = NULL;
441 modbus_close (&host->connection);
443 modbus_close (host->connection);
444 modbus_free (host->connection);
449 /* Version 2.0.3: Pass the connection struct as a pointer and pass the slave
450 * id to each call of "read_holding_registers". */
451 # define modbus_read_registers(ctx, addr, nb, dest) \
452 read_holding_registers (&(ctx), slave->id, (addr), (nb), (dest))
453 #else /* if !LEGACY_LIBMODBUS */
454 /* Version 2.9.2: Set the slave id once before querying the registers. */
455 status = modbus_set_slave (host->connection, slave->id);
458 ERROR ("Modbus plugin: modbus_set_slave (%i) failed with status %i.",
464 status = modbus_read_registers (host->connection,
465 /* start_addr = */ data->register_base,
466 /* num_registers = */ values_num, /* buffer = */ values);
467 if (status != values_num)
469 ERROR ("Modbus plugin: modbus_read_registers (%s) failed. "
470 "Giving up.", host->host);
474 DEBUG ("Modbus plugin: mb_read_data: Success! "
475 "modbus_read_registers returned with status %i.", status);
477 if (data->register_type == REG_TYPE_FLOAT)
482 float_value = mb_register_to_float (values[0], values[1]);
483 DEBUG ("Modbus plugin: mb_read_data: "
484 "Returned float value is %g", (double) float_value);
486 CAST_TO_VALUE_T (ds, vt, float_value);
487 mb_submit (host, slave, data, vt);
489 else if (data->register_type == REG_TYPE_INT32)
498 v.u32 = (((uint32_t) values[0]) << 16)
499 | ((uint32_t) values[1]);
500 DEBUG ("Modbus plugin: mb_read_data: "
501 "Returned int32 value is %"PRIi32, v.i32);
503 CAST_TO_VALUE_T (ds, vt, v.i32);
504 mb_submit (host, slave, data, vt);
506 else if (data->register_type == REG_TYPE_INT16)
517 DEBUG ("Modbus plugin: mb_read_data: "
518 "Returned int16 value is %"PRIi16, v.i16);
520 CAST_TO_VALUE_T (ds, vt, v.i16);
521 mb_submit (host, slave, data, vt);
523 else if (data->register_type == REG_TYPE_UINT32)
528 v32 = (((uint32_t) values[0]) << 16)
529 | ((uint32_t) values[1]);
530 DEBUG ("Modbus plugin: mb_read_data: "
531 "Returned uint32 value is %"PRIu32, v32);
533 CAST_TO_VALUE_T (ds, vt, v32);
534 mb_submit (host, slave, data, vt);
536 else /* if (data->register_type == REG_TYPE_UINT16) */
540 DEBUG ("Modbus plugin: mb_read_data: "
541 "Returned uint16 value is %"PRIu16, values[0]);
543 CAST_TO_VALUE_T (ds, vt, values[0]);
544 mb_submit (host, slave, data, vt);
548 } /* }}} int mb_read_data */
550 static int mb_read_slave (mb_host_t *host, mb_slave_t *slave) /* {{{ */
556 if ((host == NULL) || (slave == NULL))
560 for (data = slave->collect; data != NULL; data = data->next)
562 status = mb_read_data (host, slave, data);
571 } /* }}} int mb_read_slave */
573 static int mb_read (user_data_t *user_data) /* {{{ */
580 if ((user_data == NULL) || (user_data->data == NULL))
583 host = user_data->data;
586 for (i = 0; i < host->slaves_num; i++)
588 status = mb_read_slave (host, host->slaves + i);
597 } /* }}} int mb_read */
601 static void data_free_one (mb_data_t *data) /* {{{ */
608 } /* }}} void data_free_one */
610 static void data_free_all (mb_data_t *data) /* {{{ */
618 data_free_one (data);
620 data_free_all (next);
621 } /* }}} void data_free_all */
623 static void slaves_free_all (mb_slave_t *slaves, size_t slaves_num) /* {{{ */
630 for (i = 0; i < slaves_num; i++)
631 data_free_all (slaves[i].collect);
633 } /* }}} void slaves_free_all */
635 static void host_free (void *void_host) /* {{{ */
637 mb_host_t *host = void_host;
642 slaves_free_all (host->slaves, host->slaves_num);
644 } /* }}} void host_free */
646 /* Config functions */
648 static int mb_config_add_data (oconfig_item_t *ci) /* {{{ */
654 memset (&data, 0, sizeof (data));
656 data.register_type = REG_TYPE_UINT16;
659 status = cf_util_get_string (ci, &data.name);
663 for (i = 0; i < ci->children_num; i++)
665 oconfig_item_t *child = ci->children + i;
668 if (strcasecmp ("Type", child->key) == 0)
669 status = cf_util_get_string_buffer (child,
670 data.type, sizeof (data.type));
671 else if (strcasecmp ("Instance", child->key) == 0)
672 status = cf_util_get_string_buffer (child,
673 data.instance, sizeof (data.instance));
674 else if (strcasecmp ("RegisterBase", child->key) == 0)
675 status = cf_util_get_int (child, &data.register_base);
676 else if (strcasecmp ("RegisterType", child->key) == 0)
679 status = cf_util_get_string_buffer (child, tmp, sizeof (tmp));
682 else if (strcasecmp ("Int16", tmp) == 0)
683 data.register_type = REG_TYPE_INT16;
684 else if (strcasecmp ("Int32", tmp) == 0)
685 data.register_type = REG_TYPE_INT32;
686 else if (strcasecmp ("Uint16", tmp) == 0)
687 data.register_type = REG_TYPE_UINT16;
688 else if (strcasecmp ("Uint32", tmp) == 0)
689 data.register_type = REG_TYPE_UINT32;
690 else if (strcasecmp ("Float", tmp) == 0)
691 data.register_type = REG_TYPE_FLOAT;
694 ERROR ("Modbus plugin: The register type \"%s\" is unknown.", tmp);
700 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
706 } /* for (i = 0; i < ci->children_num; i++) */
708 assert (data.name != NULL);
709 if (data.type[0] == 0)
711 ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
717 data_copy (&data_definitions, &data);
722 } /* }}} int mb_config_add_data */
724 static int mb_config_set_host_address (mb_host_t *host, /* {{{ */
727 struct addrinfo *ai_list;
728 struct addrinfo *ai_ptr;
729 struct addrinfo ai_hints;
732 if ((host == NULL) || (address == NULL))
735 memset (&ai_hints, 0, sizeof (ai_hints));
737 ai_hints.ai_flags |= AI_ADDRCONFIG;
739 /* XXX: libmodbus can only handle IPv4 addresses. */
740 ai_hints.ai_family = AF_INET;
741 ai_hints.ai_addr = NULL;
742 ai_hints.ai_canonname = NULL;
743 ai_hints.ai_next = NULL;
746 status = getaddrinfo (address, /* service = */ NULL,
747 &ai_hints, &ai_list);
751 ERROR ("Modbus plugin: getaddrinfo failed: %s",
752 (status == EAI_SYSTEM)
753 ? sstrerror (errno, errbuf, sizeof (errbuf))
754 : gai_strerror (status));
758 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
760 status = getnameinfo (ai_ptr->ai_addr, ai_ptr->ai_addrlen,
761 host->node, sizeof (host->node),
762 /* service = */ NULL, /* length = */ 0,
763 /* flags = */ NI_NUMERICHOST);
768 freeaddrinfo (ai_list);
771 ERROR ("Modbus plugin: Unable to translate node name: \"%s\"", address);
772 else /* if (status == 0) */
774 DEBUG ("Modbus plugin: mb_config_set_host_address: %s -> %s",
775 address, host->node);
779 } /* }}} int mb_config_set_host_address */
781 static int mb_config_add_slave (mb_host_t *host, oconfig_item_t *ci) /* {{{ */
787 if ((host == NULL) || (ci == NULL))
790 slave = realloc (host->slaves, sizeof (*slave) * (host->slaves_num + 1));
793 host->slaves = slave;
794 slave = host->slaves + host->slaves_num;
795 memset (slave, 0, sizeof (*slave));
796 slave->collect = NULL;
798 status = cf_util_get_int (ci, &slave->id);
802 for (i = 0; i < ci->children_num; i++)
804 oconfig_item_t *child = ci->children + i;
807 if (strcasecmp ("Instance", child->key) == 0)
808 status = cf_util_get_string_buffer (child,
809 slave->instance, sizeof (slave->instance));
810 else if (strcasecmp ("Collect", child->key) == 0)
813 status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
815 data_copy_by_name (&slave->collect, data_definitions, buffer);
816 status = 0; /* continue after failure. */
820 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
828 if ((status == 0) && (slave->collect == NULL))
836 else /* if (status != 0) */
837 data_free_all (slave->collect);
840 } /* }}} int mb_config_add_slave */
842 static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */
848 host = malloc (sizeof (*host));
851 memset (host, 0, sizeof (*host));
854 status = cf_util_get_string_buffer (ci, host->host, sizeof (host->host));
857 if (host->host[0] == 0)
860 for (i = 0; i < ci->children_num; i++)
862 oconfig_item_t *child = ci->children + i;
865 if (strcasecmp ("Address", child->key) == 0)
867 char buffer[NI_MAXHOST];
868 status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
870 status = mb_config_set_host_address (host, buffer);
872 else if (strcasecmp ("Port", child->key) == 0)
874 host->port = cf_util_get_port_number (child);
878 else if (strcasecmp ("Interval", child->key) == 0)
879 status = cf_util_get_cdtime (child, &host->interval);
880 else if (strcasecmp ("Slave", child->key) == 0)
881 /* Don't set status: Gracefully continue if a slave fails. */
882 mb_config_add_slave (host, child);
885 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
891 } /* for (i = 0; i < ci->children_num; i++) */
893 assert (host->host[0] != 0);
894 if (host->host[0] == 0)
896 ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
905 struct timespec interval = { 0, 0 };
908 ud.free_func = host_free;
910 ssnprintf (name, sizeof (name), "modbus-%s", host->host);
912 CDTIME_T_TO_TIMESPEC (host->interval, &interval);
914 plugin_register_complex_read (/* group = */ NULL, name,
915 /* callback = */ mb_read,
916 /* interval = */ (host->interval > 0) ? &interval : NULL,
925 } /* }}} int mb_config_add_host */
927 static int mb_config (oconfig_item_t *ci) /* {{{ */
934 for (i = 0; i < ci->children_num; i++)
936 oconfig_item_t *child = ci->children + i;
938 if (strcasecmp ("Data", child->key) == 0)
939 mb_config_add_data (child);
940 else if (strcasecmp ("Host", child->key) == 0)
941 mb_config_add_host (child);
943 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
947 } /* }}} int mb_config */
951 static int mb_shutdown (void) /* {{{ */
953 data_free_all (data_definitions);
954 data_definitions = NULL;
957 } /* }}} int mb_shutdown */
959 void module_register (void)
961 plugin_register_complex_config ("modbus", mb_config);
962 plugin_register_shutdown ("modbus", mb_shutdown);
963 } /* void module_register */
965 /* vim: set sw=2 sts=2 et fdm=marker : */