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>
25 #include "configfile.h"
27 #include "utils/common/common.h"
31 #include <sys/socket.h>
33 #ifndef LIBMODBUS_VERSION_CHECK
34 /* Assume version 2.0.3 */
35 #define LEGACY_LIBMODBUS 1
37 /* Assume version 2.9.2 */
40 #ifndef MODBUS_TCP_DEFAULT_PORT
41 #ifdef MODBUS_TCP_PORT
42 #define MODBUS_TCP_DEFAULT_PORT MODBUS_TCP_PORT
44 #define MODBUS_TCP_DEFAULT_PORT 502
51 * RegisterCmd ReadHolding
61 * # Device "/dev/ttyUSB0"
67 * Instance "foobar" # optional
76 enum mb_register_type_e /* {{{ */
86 REG_TYPE_FLOAT_CDAB }; /* }}} */
88 enum mb_mreg_type_e /* {{{ */
90 MREG_INPUT }; /* }}} */
91 typedef enum mb_register_type_e mb_register_type_t;
92 typedef enum mb_mreg_type_e mb_mreg_type_t;
94 /* TCP or RTU depending on what is specified in host config block */
95 enum mb_conntype_e /* {{{ */
97 MBCONN_RTU }; /* }}} */
98 typedef enum mb_conntype_e mb_conntype_t;
100 enum mb_uarttype_e /* {{{ */
103 UARTTYPE_RS485 }; /* }}} */
104 typedef enum mb_uarttype_e mb_uarttype_t;
107 typedef struct mb_data_s mb_data_t;
108 struct mb_data_s /* {{{ */
112 mb_register_type_t register_type;
113 mb_mreg_type_t modbus_register_type;
114 char type[DATA_MAX_NAME_LEN];
115 char instance[DATA_MAX_NAME_LEN];
122 struct mb_slave_s /* {{{ */
125 char instance[DATA_MAX_NAME_LEN];
128 typedef struct mb_slave_s mb_slave_t;
130 struct mb_host_s /* {{{ */
132 char host[DATA_MAX_NAME_LEN];
133 char node[NI_MAXHOST]; /* TCP hostname or RTU serial device */
134 /* char service[NI_MAXSERV]; */
135 int port; /* for Modbus/TCP */
136 int baudrate; /* for Modbus/RTU */
137 mb_uarttype_t uarttype; /* UART type for Modbus/RTU */
138 mb_conntype_t conntype;
144 modbus_param_t connection;
146 modbus_t *connection;
150 typedef struct mb_host_s mb_host_t;
152 struct mb_data_group_s;
153 typedef struct mb_data_group_s mb_data_group_t;
154 struct mb_data_group_s /* {{{ */
156 mb_data_t *registers;
157 size_t registers_num;
159 mb_data_group_t *next;
165 static mb_data_t *data_definitions;
170 static mb_data_t *data_get_by_name(mb_data_t *src, /* {{{ */
175 for (mb_data_t *ptr = src; ptr != NULL; ptr = ptr->next)
176 if (strcasecmp(ptr->name, name) == 0)
180 } /* }}} mb_data_t *data_get_by_name */
182 static int data_append(mb_data_t **dst, mb_data_t *src) /* {{{ */
186 if ((dst == NULL) || (src == NULL))
196 while (ptr->next != NULL)
202 } /* }}} int data_append */
204 /* Copy a single mb_data_t and append it to another list. */
205 static int data_copy(mb_data_t **dst, const mb_data_t *src) /* {{{ */
210 if ((dst == NULL) || (src == NULL))
213 tmp = malloc(sizeof(*tmp));
216 memcpy(tmp, src, sizeof(*tmp));
220 tmp->name = strdup(src->name);
221 if (tmp->name == NULL) {
226 status = data_append(dst, tmp);
234 } /* }}} int data_copy */
236 /* Lookup a single mb_data_t instance, copy it and append the copy to another
238 static int data_copy_by_name(mb_data_t **dst, mb_data_t *src, /* {{{ */
242 if ((dst == NULL) || (src == NULL) || (name == NULL))
245 ptr = data_get_by_name(src, name);
249 return data_copy(dst, ptr);
250 } /* }}} int data_copy_by_name */
254 static int mb_submit(mb_host_t *host, mb_slave_t *slave, /* {{{ */
255 mb_data_t *data, value_t value) {
256 value_list_t vl = VALUE_LIST_INIT;
258 if ((host == NULL) || (slave == NULL) || (data == NULL))
261 if (slave->instance[0] == 0)
262 snprintf(slave->instance, sizeof(slave->instance), "slave_%i", slave->id);
266 sstrncpy(vl.host, host->host, sizeof(vl.host));
267 sstrncpy(vl.plugin, "modbus", sizeof(vl.plugin));
268 sstrncpy(vl.plugin_instance, slave->instance, sizeof(vl.plugin_instance));
269 sstrncpy(vl.type, data->type, sizeof(vl.type));
270 sstrncpy(vl.type_instance, data->instance, sizeof(vl.type_instance));
272 return plugin_dispatch_values(&vl);
273 } /* }}} int mb_submit */
275 static float mb_register_to_float(uint16_t hi, uint16_t lo) /* {{{ */
283 #if BYTE_ORDER == LITTLE_ENDIAN
285 conv.b[0] = lo & 0x00ff;
286 conv.b[1] = (lo >> 8) & 0x00ff;
287 conv.b[2] = hi & 0x00ff;
288 conv.b[3] = (hi >> 8) & 0x00ff;
290 conv.b[3] = lo & 0x00ff;
291 conv.b[2] = (lo >> 8) & 0x00ff;
292 conv.b[1] = hi & 0x00ff;
293 conv.b[0] = (hi >> 8) & 0x00ff;
297 } /* }}} float mb_register_to_float */
301 static int mb_init_connection(mb_host_t *host) /* {{{ */
309 modbus_set_debug(&host->connection, 1);
312 /* We'll do the error handling ourselves. */
313 modbus_set_error_handling(&host->connection, NOP_ON_ERROR);
315 if (host->conntype == MBCONN_TCP) {
316 if ((host->port < 1) || (host->port > 65535))
317 host->port = MODBUS_TCP_DEFAULT_PORT;
319 DEBUG("Modbus plugin: Trying to connect to \"%s\", port %i.", host->node,
322 modbus_init_tcp(&host->connection,
323 /* host = */ host->node,
324 /* port = */ host->port);
325 } else /* MBCONN_RTU */
327 DEBUG("Modbus plugin: Trying to connect to \"%s\".", host->node);
329 modbus_init_rtu(&host->connection,
330 /* device = */ host->node,
331 /* baudrate = */ host->baudrate, 'N', 8, 1, 0);
334 status = modbus_connect(&host->connection);
336 ERROR("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
337 host->node, host->port ? host->port : host->baudrate, status);
341 host->is_connected = true;
343 } /* }}} int mb_init_connection */
344 /* #endif LEGACY_LIBMODBUS */
346 #else /* if !LEGACY_LIBMODBUS */
348 static int mb_init_connection(mb_host_t *host) /* {{{ */
355 if (host->connection != NULL)
358 if (host->conntype == MBCONN_TCP) {
359 if ((host->port < 1) || (host->port > 65535))
360 host->port = MODBUS_TCP_DEFAULT_PORT;
362 DEBUG("Modbus plugin: Trying to connect to \"%s\", port %i.", host->node,
365 host->connection = modbus_new_tcp(host->node, host->port);
366 if (host->connection == NULL) {
367 ERROR("Modbus plugin: Creating new Modbus/TCP object failed.");
371 DEBUG("Modbus plugin: Trying to connect to \"%s\", baudrate %i.",
372 host->node, host->baudrate);
374 host->connection = modbus_new_rtu(host->node, host->baudrate, 'N', 8, 1);
375 if (host->connection == NULL) {
376 ERROR("Modbus plugin: Creating new Modbus/RTU object failed.");
382 modbus_set_debug(host->connection, 1);
385 /* We'll do the error handling ourselves. */
386 modbus_set_error_recovery(host->connection, 0);
388 status = modbus_connect(host->connection);
390 ERROR("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
391 host->node, host->port ? host->port : host->baudrate, status);
392 modbus_free(host->connection);
393 host->connection = NULL;
397 #if defined(linux) && LIBMODBUS_VERSION_CHECK(2, 9, 4)
398 switch (host->uarttype) {
400 if (modbus_rtu_set_serial_mode(host->connection, MODBUS_RTU_RS485))
401 DEBUG("Modbus plugin: Setting RS485 mode failed.");
404 /* libmodbus doesn't say anything about full-duplex symmetric RS422 UART */
409 DEBUG("Modbus plugin: Invalid UART type!.");
411 #endif /* defined(linux) && LIBMODBUS_VERSION_CHECK(2, 9, 4) */
414 } /* }}} int mb_init_connection */
415 #endif /* !LEGACY_LIBMODBUS */
417 #define CAST_TO_VALUE_T(ds, vt, raw, scale, shift) \
419 if ((ds)->ds[0].type == DS_TYPE_COUNTER) \
420 (vt).counter = (((counter_t)(raw)*scale) + shift); \
421 else if ((ds)->ds[0].type == DS_TYPE_GAUGE) \
422 (vt).gauge = (((gauge_t)(raw)*scale) + shift); \
423 else if ((ds)->ds[0].type == DS_TYPE_DERIVE) \
424 (vt).derive = (((derive_t)(raw)*scale) + shift); \
425 else /* if (ds->ds[0].type == DS_TYPE_ABSOLUTE) */ \
426 (vt).absolute = (((absolute_t)(raw)*scale) + shift); \
429 static int mb_read_data(mb_host_t *host, mb_slave_t *slave, /* {{{ */
431 uint16_t values[4] = {0};
433 const data_set_t *ds;
436 if ((host == NULL) || (slave == NULL) || (data == NULL))
439 ds = plugin_get_ds(data->type);
441 ERROR("Modbus plugin: Type \"%s\" is not defined.", data->type);
445 if (ds->ds_num != 1) {
446 ERROR("Modbus plugin: The type \"%s\" has %" PRIsz " data sources. "
447 "I can only handle data sets with only one data source.",
448 data->type, ds->ds_num);
452 if ((ds->ds[0].type != DS_TYPE_GAUGE) &&
453 (data->register_type != REG_TYPE_INT32) &&
454 (data->register_type != REG_TYPE_INT32_CDAB) &&
455 (data->register_type != REG_TYPE_UINT32) &&
456 (data->register_type != REG_TYPE_UINT32_CDAB) &&
457 (data->register_type != REG_TYPE_INT64) &&
458 (data->register_type != REG_TYPE_UINT64)) {
460 "Modbus plugin: The data source of type \"%s\" is %s, not gauge. "
461 "This will most likely result in problems, because the register type "
462 "is not UINT32 or UINT64.",
463 data->type, DS_TYPE_TO_STRING(ds->ds[0].type));
466 if ((data->register_type == REG_TYPE_INT32) ||
467 (data->register_type == REG_TYPE_INT32_CDAB) ||
468 (data->register_type == REG_TYPE_UINT32) ||
469 (data->register_type == REG_TYPE_UINT32_CDAB) ||
470 (data->register_type == REG_TYPE_FLOAT) ||
471 (data->register_type == REG_TYPE_FLOAT_CDAB))
473 else if ((data->register_type == REG_TYPE_INT64) ||
474 (data->register_type == REG_TYPE_UINT64))
479 if (host->connection == NULL) {
481 } else if (host->conntype == MBCONN_TCP) {
482 /* getpeername() is used only to determine if the socket is connected, not
483 * because we're really interested in the peer's IP address. */
484 if (getpeername(modbus_get_socket(host->connection),
485 (void *)&(struct sockaddr_storage){0},
486 &(socklen_t){sizeof(struct sockaddr_storage)}) != 0)
490 if ((status == EBADF) || (status == ENOTSOCK) || (status == ENOTCONN)) {
491 status = mb_init_connection(host);
493 ERROR("Modbus plugin: mb_init_connection (%s/%s) failed. ", host->host,
495 host->is_connected = false;
496 host->connection = NULL;
499 } else if (status != 0) {
501 modbus_close(&host->connection);
503 modbus_close(host->connection);
504 modbus_free(host->connection);
509 /* Version 2.0.3: Pass the connection struct as a pointer and pass the slave
510 * id to each call of "read_holding_registers". */
511 #define modbus_read_registers(ctx, addr, nb, dest) \
512 read_holding_registers(&(ctx), slave->id, (addr), (nb), (dest))
513 #else /* if !LEGACY_LIBMODBUS */
514 /* Version 2.9.2: Set the slave id once before querying the registers. */
515 status = modbus_set_slave(host->connection, slave->id);
517 ERROR("Modbus plugin: modbus_set_slave (%i) failed with status %i.",
522 if (data->modbus_register_type == MREG_INPUT) {
523 status = modbus_read_input_registers(host->connection,
524 /* start_addr = */ data->register_base,
525 /* num_registers = */ values_num,
526 /* buffer = */ values);
528 status = modbus_read_registers(host->connection,
529 /* start_addr = */ data->register_base,
530 /* num_registers = */ values_num,
531 /* buffer = */ values);
533 if (status != values_num) {
534 ERROR("Modbus plugin: modbus read function (%s/%s) failed. "
535 " status = %i, start_addr = %i, values_num = %i. Giving up.",
536 host->host, host->node, status, data->register_base, values_num);
538 modbus_close(&host->connection);
540 modbus_close(host->connection);
541 modbus_free(host->connection);
543 host->connection = NULL;
547 DEBUG("Modbus plugin: mb_read_data: Success! "
548 "modbus_read_registers returned with status %i.",
551 if (data->register_type == REG_TYPE_FLOAT) {
555 float_value = mb_register_to_float(values[0], values[1]);
556 DEBUG("Modbus plugin: mb_read_data: "
557 "Returned float value is %g",
558 (double)float_value);
560 CAST_TO_VALUE_T(ds, vt, float_value, data->scale, data->shift);
561 mb_submit(host, slave, data, vt);
562 } else if (data->register_type == REG_TYPE_FLOAT_CDAB) {
566 float_value = mb_register_to_float(values[1], values[0]);
567 DEBUG("Modbus plugin: mb_read_data: "
568 "Returned float value is %g",
569 (double)float_value);
571 CAST_TO_VALUE_T(ds, vt, float_value, data->scale, data->shift);
572 mb_submit(host, slave, data, vt);
573 } else if (data->register_type == REG_TYPE_INT32) {
580 v.u32 = (((uint32_t)values[0]) << 16) | ((uint32_t)values[1]);
581 DEBUG("Modbus plugin: mb_read_data: "
582 "Returned int32 value is %" PRIi32,
585 CAST_TO_VALUE_T(ds, vt, v.i32, data->scale, data->shift);
586 mb_submit(host, slave, data, vt);
587 } else if (data->register_type == REG_TYPE_INT32_CDAB) {
594 v.u32 = (((uint32_t)values[1]) << 16) | ((uint32_t)values[0]);
595 DEBUG("Modbus plugin: mb_read_data: "
596 "Returned int32 value is %" PRIi32,
599 CAST_TO_VALUE_T(ds, vt, v.i32, data->scale, data->shift);
600 mb_submit(host, slave, data, vt);
601 } else if (data->register_type == REG_TYPE_INT16) {
610 DEBUG("Modbus plugin: mb_read_data: "
611 "Returned int16 value is %" PRIi16,
614 CAST_TO_VALUE_T(ds, vt, v.i16, data->scale, data->shift);
615 mb_submit(host, slave, data, vt);
616 } else if (data->register_type == REG_TYPE_UINT32) {
620 v32 = (((uint32_t)values[0]) << 16) | ((uint32_t)values[1]);
621 DEBUG("Modbus plugin: mb_read_data: "
622 "Returned uint32 value is %" PRIu32,
625 CAST_TO_VALUE_T(ds, vt, v32, data->scale, data->shift);
626 mb_submit(host, slave, data, vt);
627 } else if (data->register_type == REG_TYPE_UINT32_CDAB) {
631 v32 = (((uint32_t)values[1]) << 16) | ((uint32_t)values[0]);
632 DEBUG("Modbus plugin: mb_read_data: "
633 "Returned uint32 value is %" PRIu32,
636 CAST_TO_VALUE_T(ds, vt, v32, data->scale, data->shift);
637 mb_submit(host, slave, data, vt);
638 } else if (data->register_type == REG_TYPE_UINT64) {
642 v64 = (((uint64_t)values[0]) << 48) | (((uint64_t)values[1]) << 32) |
643 (((uint64_t)values[2]) << 16) | (((uint64_t)values[3]));
644 DEBUG("Modbus plugin: mb_read_data: "
645 "Returned uint64 value is %" PRIu64,
648 CAST_TO_VALUE_T(ds, vt, v64, data->scale, data->shift);
649 mb_submit(host, slave, data, vt);
650 } else if (data->register_type == REG_TYPE_INT64) {
657 v.u64 = (((uint64_t)values[0]) << 48) | (((uint64_t)values[1]) << 32) |
658 (((uint64_t)values[2]) << 16) | ((uint64_t)values[3]);
659 DEBUG("Modbus plugin: mb_read_data: "
660 "Returned uint64 value is %" PRIi64,
663 CAST_TO_VALUE_T(ds, vt, v.i64, data->scale, data->shift);
664 mb_submit(host, slave, data, vt);
665 } else /* if (data->register_type == REG_TYPE_UINT16) */
669 DEBUG("Modbus plugin: mb_read_data: "
670 "Returned uint16 value is %" PRIu16,
673 CAST_TO_VALUE_T(ds, vt, values[0], data->scale, data->shift);
674 mb_submit(host, slave, data, vt);
678 } /* }}} int mb_read_data */
680 static int mb_read_slave(mb_host_t *host, mb_slave_t *slave) /* {{{ */
685 if ((host == NULL) || (slave == NULL))
689 for (mb_data_t *data = slave->collect; data != NULL; data = data->next) {
690 status = mb_read_data(host, slave, data);
699 } /* }}} int mb_read_slave */
701 static int mb_read(user_data_t *user_data) /* {{{ */
707 if ((user_data == NULL) || (user_data->data == NULL))
710 host = user_data->data;
713 for (size_t i = 0; i < host->slaves_num; i++) {
714 status = mb_read_slave(host, host->slaves + i);
723 } /* }}} int mb_read */
727 static void data_free_one(mb_data_t *data) /* {{{ */
734 } /* }}} void data_free_one */
736 static void data_free_all(mb_data_t *data) /* {{{ */
747 } /* }}} void data_free_all */
749 static void slaves_free_all(mb_slave_t *slaves, size_t slaves_num) /* {{{ */
754 for (size_t i = 0; i < slaves_num; i++)
755 data_free_all(slaves[i].collect);
757 } /* }}} void slaves_free_all */
759 static void host_free(void *void_host) /* {{{ */
761 mb_host_t *host = void_host;
766 slaves_free_all(host->slaves, host->slaves_num);
768 } /* }}} void host_free */
770 /* Config functions */
772 static int mb_config_add_data(oconfig_item_t *ci) /* {{{ */
774 mb_data_t data = {0};
778 data.register_type = REG_TYPE_UINT16;
783 status = cf_util_get_string(ci, &data.name);
787 for (int i = 0; i < ci->children_num; i++) {
788 oconfig_item_t *child = ci->children + i;
790 if (strcasecmp("Type", child->key) == 0)
791 status = cf_util_get_string_buffer(child, data.type, sizeof(data.type));
792 else if (strcasecmp("Instance", child->key) == 0)
793 status = cf_util_get_string_buffer(child, data.instance,
794 sizeof(data.instance));
795 else if (strcasecmp("Scale", child->key) == 0)
796 status = cf_util_get_double(child, &data.scale);
797 else if (strcasecmp("Shift", child->key) == 0)
798 status = cf_util_get_double(child, &data.shift);
799 else if (strcasecmp("RegisterBase", child->key) == 0)
800 status = cf_util_get_int(child, &data.register_base);
801 else if (strcasecmp("RegisterType", child->key) == 0) {
803 status = cf_util_get_string_buffer(child, tmp, sizeof(tmp));
806 else if (strcasecmp("Int16", tmp) == 0)
807 data.register_type = REG_TYPE_INT16;
808 else if (strcasecmp("Int32", tmp) == 0)
809 data.register_type = REG_TYPE_INT32;
810 else if (strcasecmp("Int32LE", tmp) == 0)
811 data.register_type = REG_TYPE_INT32_CDAB;
812 else if (strcasecmp("Uint16", tmp) == 0)
813 data.register_type = REG_TYPE_UINT16;
814 else if (strcasecmp("Uint32", tmp) == 0)
815 data.register_type = REG_TYPE_UINT32;
816 else if (strcasecmp("Uint32LE", tmp) == 0)
817 data.register_type = REG_TYPE_UINT32_CDAB;
818 else if (strcasecmp("Float", tmp) == 0)
819 data.register_type = REG_TYPE_FLOAT;
820 else if (strcasecmp("FloatLE", tmp) == 0)
821 data.register_type = REG_TYPE_FLOAT_CDAB;
822 else if (strcasecmp("Uint64", tmp) == 0)
823 data.register_type = REG_TYPE_UINT64;
824 else if (strcasecmp("Int64", tmp) == 0)
825 data.register_type = REG_TYPE_INT64;
827 ERROR("Modbus plugin: The register type \"%s\" is unknown.", tmp);
830 } else if (strcasecmp("RegisterCmd", child->key) == 0) {
832 ERROR("Modbus plugin: RegisterCmd parameter can not be used "
833 "with your libmodbus version");
836 status = cf_util_get_string_buffer(child, tmp, sizeof(tmp));
839 else if (strcasecmp("ReadHolding", tmp) == 0)
840 data.modbus_register_type = MREG_HOLDING;
841 else if (strcasecmp("ReadInput", tmp) == 0)
842 data.modbus_register_type = MREG_INPUT;
844 ERROR("Modbus plugin: The modbus_register_type \"%s\" is unknown.",
850 ERROR("Modbus plugin: Unknown configuration option: %s", child->key);
856 } /* for (i = 0; i < ci->children_num; i++) */
858 assert(data.name != NULL);
859 if (data.type[0] == 0) {
860 ERROR("Modbus plugin: Data block \"%s\": No type has been specified.",
866 data_copy(&data_definitions, &data);
871 } /* }}} int mb_config_add_data */
873 static int mb_config_set_host_address(mb_host_t *host, /* {{{ */
874 const char *address) {
875 struct addrinfo *ai_list;
878 if ((host == NULL) || (address == NULL))
881 struct addrinfo ai_hints = {
882 /* XXX: libmodbus can only handle IPv4 addresses. */
883 .ai_family = AF_INET,
884 .ai_flags = AI_ADDRCONFIG};
886 status = getaddrinfo(address, /* service = */ NULL, &ai_hints, &ai_list);
888 ERROR("Modbus plugin: getaddrinfo failed: %s",
889 (status == EAI_SYSTEM) ? STRERRNO : gai_strerror(status));
893 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL;
894 ai_ptr = ai_ptr->ai_next) {
895 status = getnameinfo(ai_ptr->ai_addr, ai_ptr->ai_addrlen, host->node,
897 /* service = */ NULL, /* length = */ 0,
898 /* flags = */ NI_NUMERICHOST);
903 freeaddrinfo(ai_list);
906 ERROR("Modbus plugin: Unable to translate node name: \"%s\"", address);
907 else /* if (status == 0) */
909 DEBUG("Modbus plugin: mb_config_set_host_address: %s -> %s", address,
914 } /* }}} int mb_config_set_host_address */
916 static int mb_config_add_slave(mb_host_t *host, oconfig_item_t *ci) /* {{{ */
921 if ((host == NULL) || (ci == NULL))
924 slave = realloc(host->slaves, sizeof(*slave) * (host->slaves_num + 1));
927 host->slaves = slave;
928 slave = host->slaves + host->slaves_num;
929 memset(slave, 0, sizeof(*slave));
930 slave->collect = NULL;
932 status = cf_util_get_int(ci, &slave->id);
936 for (int i = 0; i < ci->children_num; i++) {
937 oconfig_item_t *child = ci->children + i;
939 if (strcasecmp("Instance", child->key) == 0)
940 status = cf_util_get_string_buffer(child, slave->instance,
941 sizeof(slave->instance));
942 else if (strcasecmp("Collect", child->key) == 0) {
944 status = cf_util_get_string_buffer(child, buffer, sizeof(buffer));
946 data_copy_by_name(&slave->collect, data_definitions, buffer);
947 status = 0; /* continue after failure. */
949 ERROR("Modbus plugin: Unknown configuration option: %s", child->key);
957 if ((status == 0) && (slave->collect == NULL))
965 else /* if (status != 0) */
966 data_free_all(slave->collect);
969 } /* }}} int mb_config_add_slave */
971 static int mb_config_add_host(oconfig_item_t *ci) /* {{{ */
973 cdtime_t interval = 0;
977 host = calloc(1, sizeof(*host));
982 status = cf_util_get_string_buffer(ci, host->host, sizeof(host->host));
987 if (host->host[0] == 0) {
992 for (int i = 0; i < ci->children_num; i++) {
993 oconfig_item_t *child = ci->children + i;
996 if (strcasecmp("Address", child->key) == 0) {
997 char buffer[NI_MAXHOST];
998 status = cf_util_get_string_buffer(child, buffer, sizeof(buffer));
1000 status = mb_config_set_host_address(host, buffer);
1002 host->conntype = MBCONN_TCP;
1003 } else if (strcasecmp("Port", child->key) == 0) {
1004 host->port = cf_util_get_port_number(child);
1005 if (host->port <= 0)
1007 } else if (strcasecmp("Device", child->key) == 0) {
1008 status = cf_util_get_string_buffer(child, host->node, sizeof(host->node));
1010 host->conntype = MBCONN_RTU;
1011 host->uarttype = UARTTYPE_RS232;
1013 } else if (strcasecmp("Baudrate", child->key) == 0)
1014 status = cf_util_get_int(child, &host->baudrate);
1015 else if (strcasecmp("UARTType", child->key) == 0) {
1016 #if defined(linux) && !LEGACY_LIBMODBUS && LIBMODBUS_VERSION_CHECK(2, 9, 4)
1017 char buffer[NI_MAXHOST];
1018 status = cf_util_get_string_buffer(child, buffer, sizeof(buffer));
1021 if (strncmp(buffer, "RS485", 6) == 0)
1022 host->uarttype = UARTTYPE_RS485;
1023 else if (strncmp(buffer, "RS422", 6) == 0)
1024 host->uarttype = UARTTYPE_RS422;
1025 else if (strncmp(buffer, "RS232", 6) == 0)
1026 host->uarttype = UARTTYPE_RS232;
1028 ERROR("Modbus plugin: The UARTType \"%s\" is unknown.", buffer);
1033 ERROR("Modbus plugin: Option `UARTType' not supported. Please "
1034 "upgrade libmodbus to at least 2.9.4");
1037 } else if (strcasecmp("Interval", child->key) == 0)
1038 status = cf_util_get_cdtime(child, &interval);
1039 else if (strcasecmp("Slave", child->key) == 0)
1040 /* Don't set status: Gracefully continue if a slave fails. */
1041 mb_config_add_slave(host, child);
1043 ERROR("Modbus plugin: Unknown configuration option: %s", child->key);
1049 } /* for (i = 0; i < ci->children_num; i++) */
1051 assert(host->host[0] != 0);
1052 if (host->node[0] == 0) {
1053 ERROR("Modbus plugin: Data block \"%s\": No address or device has been "
1058 if (host->conntype == MBCONN_RTU && !host->baudrate) {
1059 ERROR("Modbus plugin: Data block \"%s\": No serial baudrate has been "
1064 if ((host->conntype == MBCONN_TCP && host->baudrate) ||
1065 (host->conntype == MBCONN_RTU && host->port)) {
1066 ERROR("Modbus plugin: Data block \"%s\": You've mixed up RTU and TCP "
1075 snprintf(name, sizeof(name), "modbus-%s", host->host);
1077 plugin_register_complex_read(/* group = */ NULL, name,
1078 /* callback = */ mb_read,
1079 /* interval = */ interval,
1081 .data = host, .free_func = host_free,
1088 } /* }}} int mb_config_add_host */
1090 static int mb_config(oconfig_item_t *ci) /* {{{ */
1095 for (int i = 0; i < ci->children_num; i++) {
1096 oconfig_item_t *child = ci->children + i;
1098 if (strcasecmp("Data", child->key) == 0)
1099 mb_config_add_data(child);
1100 else if (strcasecmp("Host", child->key) == 0)
1101 mb_config_add_host(child);
1103 ERROR("Modbus plugin: Unknown configuration option: %s", child->key);
1107 } /* }}} int mb_config */
1111 static int mb_shutdown(void) /* {{{ */
1113 data_free_all(data_definitions);
1114 data_definitions = NULL;
1117 } /* }}} int mb_shutdown */
1119 void module_register(void) {
1120 plugin_register_complex_config("modbus", mb_config);
1121 plugin_register_shutdown("modbus", mb_shutdown);
1122 } /* void module_register */