2 * collectd - src/modbus.c
3 * Copyright (C) 2010 noris network AG
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian Forster <octo at noris.net>
25 #include "configfile.h"
27 #include <modbus/modbus.h>
43 * Instance "foobar" # optional
52 enum mb_register_type_e /* {{{ */
58 typedef enum mb_register_type_e mb_register_type_t;
61 typedef struct mb_data_s mb_data_t;
62 struct mb_data_s /* {{{ */
66 mb_register_type_t register_type;
67 char type[DATA_MAX_NAME_LEN];
68 char instance[DATA_MAX_NAME_LEN];
73 struct mb_slave_s /* {{{ */
76 char instance[DATA_MAX_NAME_LEN];
79 typedef struct mb_slave_s mb_slave_t;
81 struct mb_host_s /* {{{ */
83 char host[DATA_MAX_NAME_LEN];
84 char node[NI_MAXHOST];
85 /* char service[NI_MAXSERV]; */
92 modbus_param_t connection;
94 _Bool have_reconnected;
96 typedef struct mb_host_s mb_host_t;
98 struct mb_data_group_s;
99 typedef struct mb_data_group_s mb_data_group_t;
100 struct mb_data_group_s /* {{{ */
102 mb_data_t *registers;
103 size_t registers_num;
105 mb_data_group_t *next;
111 static mb_data_t *data_definitions = NULL;
116 static mb_data_t *data_get_by_name (mb_data_t *src, /* {{{ */
124 for (ptr = src; ptr != NULL; ptr = ptr->next)
125 if (strcasecmp (ptr->name, name) == 0)
129 } /* }}} mb_data_t *data_get_by_name */
131 static int data_append (mb_data_t **dst, mb_data_t *src) /* {{{ */
135 if ((dst == NULL) || (src == NULL))
146 while (ptr->next != NULL)
152 } /* }}} int data_append */
154 /* Copy a single mb_data_t and append it to another list. */
155 static int data_copy (mb_data_t **dst, const mb_data_t *src) /* {{{ */
160 if ((dst == NULL) || (src == NULL))
163 tmp = malloc (sizeof (*tmp));
166 memcpy (tmp, src, sizeof (*tmp));
170 tmp->name = strdup (src->name);
171 if (tmp->name == NULL)
177 status = data_append (dst, tmp);
186 } /* }}} int data_copy */
188 /* Lookup a single mb_data_t instance, copy it and append the copy to another
190 static int data_copy_by_name (mb_data_t **dst, mb_data_t *src, /* {{{ */
195 if ((dst == NULL) || (src == NULL) || (name == NULL))
198 ptr = data_get_by_name (src, name);
202 return (data_copy (dst, ptr));
203 } /* }}} int data_copy_by_name */
207 static int mb_submit (mb_host_t *host, mb_slave_t *slave, /* {{{ */
208 mb_data_t *data, value_t value)
210 value_list_t vl = VALUE_LIST_INIT;
212 if ((host == NULL) || (slave == NULL) || (data == NULL))
215 if (host->interval <= 0)
216 host->interval = interval_g;
218 if (slave->instance[0] == 0)
219 ssnprintf (slave->instance, sizeof (slave->instance), "slave_%i",
224 vl.interval = host->interval;
225 sstrncpy (vl.host, host->host, sizeof (vl.host));
226 sstrncpy (vl.plugin, "modbus", sizeof (vl.plugin));
227 sstrncpy (vl.plugin_instance, slave->instance, sizeof (vl.plugin_instance));
228 sstrncpy (vl.type, data->type, sizeof (vl.type));
229 sstrncpy (vl.type_instance, data->instance, sizeof (vl.type_instance));
231 return (plugin_dispatch_values (&vl));
232 } /* }}} int mb_submit */
234 static float mb_register_to_float (uint16_t hi, uint16_t lo) /* {{{ */
242 #if BYTE_ORDER == LITTLE_ENDIAN
244 conv.b[0] = lo & 0x00ff;
245 conv.b[1] = (lo >> 8) & 0x00ff;
246 conv.b[2] = hi & 0x00ff;
247 conv.b[3] = (hi >> 8) & 0x00ff;
249 conv.b[3] = lo & 0x00ff;
250 conv.b[2] = (lo >> 8) & 0x00ff;
251 conv.b[1] = hi & 0x00ff;
252 conv.b[0] = (hi >> 8) & 0x00ff;
256 } /* }}} float mb_register_to_float */
258 static int mb_init_connection (mb_host_t *host) /* {{{ */
265 if (host->is_connected)
268 /* Only reconnect once per interval. */
269 if (host->have_reconnected)
272 modbus_set_debug (&host->connection, 1);
274 /* We'll do the error handling ourselves. */
275 modbus_set_error_handling (&host->connection, NOP_ON_ERROR);
277 if ((host->port < 1) || (host->port > 65535))
278 host->port = MODBUS_TCP_DEFAULT_PORT;
280 DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
281 host->node, host->port);
283 modbus_init_tcp (&host->connection,
284 /* host = */ host->node,
285 /* port = */ host->port);
287 status = modbus_connect (&host->connection);
290 ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
291 host->node, host->port, status);
295 host->is_connected = 1;
296 host->have_reconnected = 1;
298 } /* }}} int mb_init_connection */
300 #define CAST_TO_VALUE_T(ds,vt,raw) do { \
301 if ((ds)->ds[0].type == DS_TYPE_COUNTER) \
302 (vt).counter = (counter_t) (raw); \
303 else if ((ds)->ds[0].type == DS_TYPE_GAUGE) \
304 (vt).gauge = (gauge_t) (raw); \
305 else if ((ds)->ds[0].type == DS_TYPE_DERIVE) \
306 (vt).derive = (derive_t) (raw); \
307 else /* if (ds->ds[0].type == DS_TYPE_ABSOLUTE) */ \
308 (vt).absolute = (absolute_t) (raw); \
311 static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */
316 const data_set_t *ds;
320 if ((host == NULL) || (slave == NULL) || (data == NULL))
323 ds = plugin_get_ds (data->type);
326 ERROR ("Modbus plugin: Type \"%s\" is not defined.", data->type);
332 ERROR ("Modbus plugin: The type \"%s\" has %i data sources. "
333 "I can only handle data sets with only one data source.",
334 data->type, ds->ds_num);
338 if ((ds->ds[0].type != DS_TYPE_GAUGE)
339 && (data->register_type != REG_TYPE_UINT32))
341 NOTICE ("Modbus plugin: The data source of type \"%s\" is %s, not gauge. "
342 "This will most likely result in problems, because the register type "
343 "is not UINT32.", data->type, DS_TYPE_TO_STRING (ds->ds[0].type));
346 memset (values, 0, sizeof (values));
347 if ((data->register_type == REG_TYPE_UINT32)
348 || (data->register_type == REG_TYPE_FLOAT))
353 for (i = 0; i < 2; i++)
355 status = read_holding_registers (&host->connection,
356 /* slave = */ slave->id, /* start_addr = */ data->register_base,
357 /* num_registers = */ values_num, /* buffer = */ values);
361 if (host->is_connected)
362 modbus_close (&host->connection);
363 host->is_connected = 0;
365 /* If we already tried reconnecting this round, give up. */
366 if (host->have_reconnected)
368 ERROR ("Modbus plugin: read_holding_registers (%s) failed. "
369 "Reconnecting has already been tried. Giving up.", host->host);
373 /* Maybe the device closed the connection during the waiting interval.
374 * Try re-establishing the connection. */
375 status = mb_init_connection (host);
378 ERROR ("Modbus plugin: read_holding_registers (%s) failed. "
379 "While trying to reconnect, connecting to \"%s\" failed. "
381 host->host, host->node);
385 DEBUG ("Modbus plugin: Re-established connection to %s", host->host);
389 } /* for (i = 0, 1) */
391 DEBUG ("Modbus plugin: mb_read_data: Success! "
392 "read_holding_registers returned with status %i.", status);
394 if (data->register_type == REG_TYPE_FLOAT)
399 float_value = mb_register_to_float (values[0], values[1]);
400 DEBUG ("Modbus plugin: mb_read_data: "
401 "Returned float value is %g", (double) float_value);
403 CAST_TO_VALUE_T (ds, vt, float_value);
404 mb_submit (host, slave, data, vt);
406 else if (data->register_type == REG_TYPE_UINT32)
411 v32 = (values[0] << 16) | values[1];
412 DEBUG ("Modbus plugin: mb_read_data: "
413 "Returned uint32 value is %"PRIu32, v32);
415 CAST_TO_VALUE_T (ds, vt, v32);
416 mb_submit (host, slave, data, vt);
418 else /* if (data->register_type == REG_TYPE_UINT16) */
422 DEBUG ("Modbus plugin: mb_read_data: "
423 "Returned uint16 value is %"PRIu16, values[0]);
425 CAST_TO_VALUE_T (ds, vt, values[0]);
426 mb_submit (host, slave, data, vt);
430 } /* }}} int mb_read_data */
432 static int mb_read_slave (mb_host_t *host, mb_slave_t *slave) /* {{{ */
438 if ((host == NULL) || (slave == NULL))
442 for (data = slave->collect; data != NULL; data = data->next)
444 status = mb_read_data (host, slave, data);
453 } /* }}} int mb_read_slave */
455 static int mb_read (user_data_t *user_data) /* {{{ */
462 if ((user_data == NULL) || (user_data->data == NULL))
465 host = user_data->data;
467 /* Clear the reconnect flag. */
468 host->have_reconnected = 0;
471 for (i = 0; i < host->slaves_num; i++)
473 status = mb_read_slave (host, host->slaves + i);
482 } /* }}} int mb_read */
486 static void data_free_one (mb_data_t *data) /* {{{ */
493 } /* }}} void data_free_one */
495 static void data_free_all (mb_data_t *data) /* {{{ */
503 data_free_one (data);
505 data_free_all (next);
506 } /* }}} void data_free_all */
508 static void slaves_free_all (mb_slave_t *slaves, size_t slaves_num) /* {{{ */
515 for (i = 0; i < slaves_num; i++)
516 data_free_all (slaves[i].collect);
518 } /* }}} void slaves_free_all */
520 static void host_free (void *void_host) /* {{{ */
522 mb_host_t *host = void_host;
527 slaves_free_all (host->slaves, host->slaves_num);
529 } /* }}} void host_free */
531 /* Config functions */
533 static int mb_config_add_data (oconfig_item_t *ci) /* {{{ */
539 memset (&data, 0, sizeof (data));
541 data.register_type = REG_TYPE_UINT16;
544 status = cf_util_get_string (ci, &data.name);
548 for (i = 0; i < ci->children_num; i++)
550 oconfig_item_t *child = ci->children + i;
553 if (strcasecmp ("Type", child->key) == 0)
554 status = cf_util_get_string_buffer (child,
555 data.type, sizeof (data.type));
556 else if (strcasecmp ("Instance", child->key) == 0)
557 status = cf_util_get_string_buffer (child,
558 data.instance, sizeof (data.instance));
559 else if (strcasecmp ("RegisterBase", child->key) == 0)
560 status = cf_util_get_int (child, &data.register_base);
561 else if (strcasecmp ("RegisterType", child->key) == 0)
564 status = cf_util_get_string_buffer (child, tmp, sizeof (tmp));
567 else if (strcasecmp ("Uint16", tmp) == 0)
568 data.register_type = REG_TYPE_UINT16;
569 else if (strcasecmp ("Uint32", tmp) == 0)
570 data.register_type = REG_TYPE_UINT32;
571 else if (strcasecmp ("Float", tmp) == 0)
572 data.register_type = REG_TYPE_FLOAT;
575 ERROR ("Modbus plugin: The register type \"%s\" is unknown.", tmp);
581 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
587 } /* for (i = 0; i < ci->children_num; i++) */
589 assert (data.name != NULL);
590 if (data.type[0] == 0)
592 ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
598 data_copy (&data_definitions, &data);
603 } /* }}} int mb_config_add_data */
605 static int mb_config_set_host_address (mb_host_t *host, /* {{{ */
608 struct addrinfo *ai_list;
609 struct addrinfo *ai_ptr;
610 struct addrinfo ai_hints;
613 if ((host == NULL) || (address == NULL))
616 memset (&ai_hints, 0, sizeof (ai_hints));
618 ai_hints.ai_flags |= AI_ADDRCONFIG;
620 /* XXX: libmodbus can only handle IPv4 addresses. */
621 ai_hints.ai_family = AF_INET;
622 ai_hints.ai_addr = NULL;
623 ai_hints.ai_canonname = NULL;
624 ai_hints.ai_next = NULL;
627 status = getaddrinfo (address, /* service = */ NULL,
628 &ai_hints, &ai_list);
632 ERROR ("Modbus plugin: getaddrinfo failed: %s",
633 (status == EAI_SYSTEM)
634 ? sstrerror (errno, errbuf, sizeof (errbuf))
635 : gai_strerror (status));
639 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
641 status = getnameinfo (ai_ptr->ai_addr, ai_ptr->ai_addrlen,
642 host->node, sizeof (host->node),
643 /* service = */ NULL, /* length = */ 0,
644 /* flags = */ NI_NUMERICHOST);
649 freeaddrinfo (ai_list);
652 ERROR ("Modbus plugin: Unable to translate node name: \"%s\"", address);
653 else /* if (status == 0) */
655 DEBUG ("Modbus plugin: mb_config_set_host_address: %s -> %s",
656 address, host->node);
660 } /* }}} int mb_config_set_host_address */
662 static int mb_config_add_slave (mb_host_t *host, oconfig_item_t *ci) /* {{{ */
668 if ((host == NULL) || (ci == NULL))
671 slave = realloc (host->slaves, sizeof (*slave) * (host->slaves_num + 1));
674 host->slaves = slave;
675 slave = host->slaves + host->slaves_num;
676 memset (slave, 0, sizeof (*slave));
677 slave->collect = NULL;
679 status = cf_util_get_int (ci, &slave->id);
683 for (i = 0; i < ci->children_num; i++)
685 oconfig_item_t *child = ci->children + i;
688 if (strcasecmp ("Instance", child->key) == 0)
689 status = cf_util_get_string_buffer (child,
690 slave->instance, sizeof (slave->instance));
691 else if (strcasecmp ("Collect", child->key) == 0)
694 status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
696 data_copy_by_name (&slave->collect, data_definitions, buffer);
697 status = 0; /* continue after failure. */
701 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
709 if ((status == 0) && (slave->collect == NULL))
717 else /* if (status != 0) */
718 data_free_all (slave->collect);
721 } /* }}} int mb_config_add_slave */
723 static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */
729 host = malloc (sizeof (*host));
732 memset (host, 0, sizeof (*host));
735 status = cf_util_get_string_buffer (ci, host->host, sizeof (host->host));
738 if (host->host[0] == 0)
741 for (i = 0; i < ci->children_num; i++)
743 oconfig_item_t *child = ci->children + i;
746 if (strcasecmp ("Address", child->key) == 0)
748 char buffer[NI_MAXHOST];
749 status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
751 status = mb_config_set_host_address (host, buffer);
753 else if (strcasecmp ("Port", child->key) == 0)
755 host->port = cf_util_get_port_number (child);
759 else if (strcasecmp ("Interval", child->key) == 0)
760 status = cf_util_get_int (child, &host->interval);
761 else if (strcasecmp ("Slave", child->key) == 0)
762 /* Don't set status: Gracefully continue if a slave fails. */
763 mb_config_add_slave (host, child);
766 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
772 } /* for (i = 0; i < ci->children_num; i++) */
774 assert (host->host[0] != 0);
775 if (host->host[0] == 0)
777 ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
786 struct timespec interval;
789 ud.free_func = host_free;
791 ssnprintf (name, sizeof (name), "modbus-%s", host->host);
793 interval.tv_nsec = 0;
794 if (host->interval > 0)
795 interval.tv_sec = host->interval;
799 plugin_register_complex_read (name, mb_read,
800 (interval.tv_sec > 0) ? &interval : NULL,
809 } /* }}} int mb_config_add_host */
811 static int mb_config (oconfig_item_t *ci) /* {{{ */
818 for (i = 0; i < ci->children_num; i++)
820 oconfig_item_t *child = ci->children + i;
822 if (strcasecmp ("Data", child->key) == 0)
823 mb_config_add_data (child);
824 else if (strcasecmp ("Host", child->key) == 0)
825 mb_config_add_host (child);
827 ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
831 } /* }}} int mb_config */
836 static int foo (void) /* {{{ */
845 printf ("mb_read (addr = %i, float = %s);\n", register_addr,
846 is_float ? "true" : "false");
848 memset (values, 0, sizeof (values));
854 status = read_holding_registers (dev->connection,
855 /* slave = */ 1, /* start_addr = */ register_addr,
856 /* num_registers = */ values_num, /* buffer = */ values);
857 printf ("read_coil_status returned with status %i\n", status);
863 float value = mb_register_to_float (values[0], values[1]);
864 printf ("read_coil_status returned value %g (hi %#"PRIx16", lo %#"PRIx16")\n",
865 value, values[0], values[1]);
869 printf ("read_coil_status returned value %"PRIu16"\n", values[0]);
876 static int mb_shutdown (void) /* {{{ */
878 data_free_all (data_definitions);
879 data_definitions = NULL;
882 } /* }}} int mb_shutdown */
884 void module_register (void)
886 plugin_register_complex_config ("modbus", mb_config);
887 plugin_register_shutdown ("modbus", mb_shutdown);
888 } /* void module_register */
890 /* vim: set sw=2 sts=2 et fdm=marker : */