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"
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 /* {{{ */
84 REG_TYPE_FLOAT_CDAB }; /* }}} */
86 enum mb_mreg_type_e /* {{{ */
88 MREG_INPUT }; /* }}} */
89 typedef enum mb_register_type_e mb_register_type_t;
90 typedef enum mb_mreg_type_e mb_mreg_type_t;
92 /* TCP or RTU depending on what is specified in host config block */
93 enum mb_conntype_e /* {{{ */
95 MBCONN_RTU }; /* }}} */
96 typedef enum mb_conntype_e mb_conntype_t;
99 typedef struct mb_data_s mb_data_t;
100 struct mb_data_s /* {{{ */
104 mb_register_type_t register_type;
105 mb_mreg_type_t modbus_register_type;
106 char type[DATA_MAX_NAME_LEN];
107 char instance[DATA_MAX_NAME_LEN];
114 struct mb_slave_s /* {{{ */
117 char instance[DATA_MAX_NAME_LEN];
120 typedef struct mb_slave_s mb_slave_t;
122 struct mb_host_s /* {{{ */
124 char host[DATA_MAX_NAME_LEN];
125 char node[NI_MAXHOST]; /* TCP hostname or RTU serial device */
126 /* char service[NI_MAXSERV]; */
127 int port; /* for Modbus/TCP */
128 int baudrate; /* for Modbus/RTU */
129 mb_conntype_t conntype;
136 modbus_param_t connection;
138 modbus_t *connection;
142 typedef struct mb_host_s mb_host_t;
144 struct mb_data_group_s;
145 typedef struct mb_data_group_s mb_data_group_t;
146 struct mb_data_group_s /* {{{ */
148 mb_data_t *registers;
149 size_t registers_num;
151 mb_data_group_t *next;
157 static mb_data_t *data_definitions = NULL;
162 static mb_data_t *data_get_by_name(mb_data_t *src, /* {{{ */
167 for (mb_data_t *ptr = src; ptr != NULL; ptr = ptr->next)
168 if (strcasecmp(ptr->name, name) == 0)
172 } /* }}} mb_data_t *data_get_by_name */
174 static int data_append(mb_data_t **dst, mb_data_t *src) /* {{{ */
178 if ((dst == NULL) || (src == NULL))
188 while (ptr->next != NULL)
194 } /* }}} int data_append */
196 /* Copy a single mb_data_t and append it to another list. */
197 static int data_copy(mb_data_t **dst, const mb_data_t *src) /* {{{ */
202 if ((dst == NULL) || (src == NULL))
205 tmp = malloc(sizeof(*tmp));
208 memcpy(tmp, src, sizeof(*tmp));
212 tmp->name = strdup(src->name);
213 if (tmp->name == NULL) {
218 status = data_append(dst, tmp);
226 } /* }}} int data_copy */
228 /* Lookup a single mb_data_t instance, copy it and append the copy to another
230 static int data_copy_by_name(mb_data_t **dst, mb_data_t *src, /* {{{ */
234 if ((dst == NULL) || (src == NULL) || (name == NULL))
237 ptr = data_get_by_name(src, name);
241 return data_copy(dst, ptr);
242 } /* }}} int data_copy_by_name */
246 static int mb_submit(mb_host_t *host, mb_slave_t *slave, /* {{{ */
247 mb_data_t *data, value_t value) {
248 value_list_t vl = VALUE_LIST_INIT;
250 if ((host == NULL) || (slave == NULL) || (data == NULL))
253 if (host->interval == 0)
254 host->interval = plugin_get_interval();
256 if (slave->instance[0] == 0)
257 snprintf(slave->instance, sizeof(slave->instance), "slave_%i", slave->id);
261 vl.interval = host->interval;
262 sstrncpy(vl.host, host->host, sizeof(vl.host));
263 sstrncpy(vl.plugin, "modbus", sizeof(vl.plugin));
264 sstrncpy(vl.plugin_instance, slave->instance, sizeof(vl.plugin_instance));
265 sstrncpy(vl.type, data->type, sizeof(vl.type));
266 sstrncpy(vl.type_instance, data->instance, sizeof(vl.type_instance));
268 return plugin_dispatch_values(&vl);
269 } /* }}} int mb_submit */
271 static float mb_register_to_float(uint16_t hi, uint16_t lo) /* {{{ */
279 #if BYTE_ORDER == LITTLE_ENDIAN
281 conv.b[0] = lo & 0x00ff;
282 conv.b[1] = (lo >> 8) & 0x00ff;
283 conv.b[2] = hi & 0x00ff;
284 conv.b[3] = (hi >> 8) & 0x00ff;
286 conv.b[3] = lo & 0x00ff;
287 conv.b[2] = (lo >> 8) & 0x00ff;
288 conv.b[1] = hi & 0x00ff;
289 conv.b[0] = (hi >> 8) & 0x00ff;
293 } /* }}} float mb_register_to_float */
297 static int mb_init_connection(mb_host_t *host) /* {{{ */
305 modbus_set_debug(&host->connection, 1);
308 /* We'll do the error handling ourselves. */
309 modbus_set_error_handling(&host->connection, NOP_ON_ERROR);
311 if (host->conntype == MBCONN_TCP) {
312 if ((host->port < 1) || (host->port > 65535))
313 host->port = MODBUS_TCP_DEFAULT_PORT;
315 DEBUG("Modbus plugin: Trying to connect to \"%s\", port %i.", host->node,
318 modbus_init_tcp(&host->connection,
319 /* host = */ host->node,
320 /* port = */ host->port);
321 } else /* MBCONN_RTU */
323 DEBUG("Modbus plugin: Trying to connect to \"%s\".", host->node);
325 modbus_init_rtu(&host->connection,
326 /* device = */ host->node,
327 /* baudrate = */ host->baudrate, 'N', 8, 1, 0);
330 status = modbus_connect(&host->connection);
332 ERROR("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
333 host->node, host->port ? host->port : host->baudrate, status);
337 host->is_connected = 1;
339 } /* }}} int mb_init_connection */
340 /* #endif LEGACY_LIBMODBUS */
342 #else /* if !LEGACY_LIBMODBUS */
344 static int mb_init_connection(mb_host_t *host) /* {{{ */
351 if (host->connection != NULL)
354 if (host->conntype == MBCONN_TCP) {
355 if ((host->port < 1) || (host->port > 65535))
356 host->port = MODBUS_TCP_DEFAULT_PORT;
358 DEBUG("Modbus plugin: Trying to connect to \"%s\", port %i.", host->node,
361 host->connection = modbus_new_tcp(host->node, host->port);
362 if (host->connection == NULL) {
363 ERROR("Modbus plugin: Creating new Modbus/TCP object failed.");
367 DEBUG("Modbus plugin: Trying to connect to \"%s\", baudrate %i.",
368 host->node, host->baudrate);
370 host->connection = modbus_new_rtu(host->node, host->baudrate, 'N', 8, 1);
371 if (host->connection == NULL) {
372 ERROR("Modbus plugin: Creating new Modbus/RTU object failed.");
378 modbus_set_debug(host->connection, 1);
381 /* We'll do the error handling ourselves. */
382 modbus_set_error_recovery(host->connection, 0);
384 status = modbus_connect(host->connection);
386 ERROR("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
387 host->node, host->port ? host->port : host->baudrate, status);
388 modbus_free(host->connection);
389 host->connection = NULL;
394 } /* }}} int mb_init_connection */
395 #endif /* !LEGACY_LIBMODBUS */
397 #define CAST_TO_VALUE_T(ds, vt, raw, scale, shift) \
399 if ((ds)->ds[0].type == DS_TYPE_COUNTER) \
400 (vt).counter = (((counter_t)(raw) * scale) + shift); \
401 else if ((ds)->ds[0].type == DS_TYPE_GAUGE) \
402 (vt).gauge = (((gauge_t)(raw) * scale) + shift); \
403 else if ((ds)->ds[0].type == DS_TYPE_DERIVE) \
404 (vt).derive = (((derive_t)(raw) * scale) + shift); \
405 else /* if (ds->ds[0].type == DS_TYPE_ABSOLUTE) */ \
406 (vt).absolute = (((absolute_t)(raw) * scale) + shift); \
409 static int mb_read_data(mb_host_t *host, mb_slave_t *slave, /* {{{ */
411 uint16_t values[2] = {0};
413 const data_set_t *ds;
416 if ((host == NULL) || (slave == NULL) || (data == NULL))
419 ds = plugin_get_ds(data->type);
421 ERROR("Modbus plugin: Type \"%s\" is not defined.", data->type);
425 if (ds->ds_num != 1) {
426 ERROR("Modbus plugin: The type \"%s\" has %" PRIsz " data sources. "
427 "I can only handle data sets with only one data source.",
428 data->type, ds->ds_num);
432 if ((ds->ds[0].type != DS_TYPE_GAUGE) &&
433 (data->register_type != REG_TYPE_INT32) &&
434 (data->register_type != REG_TYPE_INT32_CDAB) &&
435 (data->register_type != REG_TYPE_UINT32) &&
436 (data->register_type != REG_TYPE_UINT32_CDAB)) {
438 "Modbus plugin: The data source of type \"%s\" is %s, not gauge. "
439 "This will most likely result in problems, because the register type "
441 data->type, DS_TYPE_TO_STRING(ds->ds[0].type));
444 if ((data->register_type == REG_TYPE_INT32) ||
445 (data->register_type == REG_TYPE_INT32_CDAB) ||
446 (data->register_type == REG_TYPE_UINT32) ||
447 (data->register_type == REG_TYPE_UINT32_CDAB) ||
448 (data->register_type == REG_TYPE_FLOAT) ||
449 (data->register_type == REG_TYPE_FLOAT_CDAB))
454 if (host->connection == NULL) {
456 } else if (host->conntype == MBCONN_TCP) {
457 /* getpeername() is used only to determine if the socket is connected, not
458 * because we're really interested in the peer's IP address. */
459 if (getpeername(modbus_get_socket(host->connection),
460 (void *)&(struct sockaddr_storage){0},
461 &(socklen_t){sizeof(struct sockaddr_storage)}) != 0)
465 if ((status == EBADF) || (status == ENOTSOCK) || (status == ENOTCONN)) {
466 status = mb_init_connection(host);
468 ERROR("Modbus plugin: mb_init_connection (%s/%s) failed. ", host->host,
470 host->is_connected = 0;
471 host->connection = NULL;
474 } else if (status != 0) {
476 modbus_close(&host->connection);
478 modbus_close(host->connection);
479 modbus_free(host->connection);
484 /* Version 2.0.3: Pass the connection struct as a pointer and pass the slave
485 * id to each call of "read_holding_registers". */
486 #define modbus_read_registers(ctx, addr, nb, dest) \
487 read_holding_registers(&(ctx), slave->id, (addr), (nb), (dest))
488 #else /* if !LEGACY_LIBMODBUS */
489 /* Version 2.9.2: Set the slave id once before querying the registers. */
490 status = modbus_set_slave(host->connection, slave->id);
492 ERROR("Modbus plugin: modbus_set_slave (%i) failed with status %i.",
497 if (data->modbus_register_type == MREG_INPUT) {
498 status = modbus_read_input_registers(host->connection,
499 /* start_addr = */ data->register_base,
500 /* num_registers = */ values_num,
501 /* buffer = */ values);
503 status = modbus_read_registers(host->connection,
504 /* start_addr = */ data->register_base,
505 /* num_registers = */ values_num,
506 /* buffer = */ values);
508 if (status != values_num) {
509 ERROR("Modbus plugin: modbus read function (%s/%s) failed. "
510 " status = %i, start_addr = %i, values_num = %i. Giving up.",
511 host->host, host->node, status, data->register_base, values_num);
513 modbus_close(&host->connection);
515 modbus_close(host->connection);
516 modbus_free(host->connection);
518 host->connection = NULL;
522 DEBUG("Modbus plugin: mb_read_data: Success! "
523 "modbus_read_registers returned with status %i.",
526 if (data->register_type == REG_TYPE_FLOAT) {
530 float_value = mb_register_to_float(values[0], values[1]);
531 DEBUG("Modbus plugin: mb_read_data: "
532 "Returned float value is %g",
533 (double)float_value);
535 CAST_TO_VALUE_T(ds, vt, float_value, data->scale, data->shift);
536 mb_submit(host, slave, data, vt);
537 } else if (data->register_type == REG_TYPE_FLOAT_CDAB) {
541 float_value = mb_register_to_float(values[1], values[0]);
542 DEBUG("Modbus plugin: mb_read_data: "
543 "Returned float value is %g",
544 (double)float_value);
546 CAST_TO_VALUE_T(ds, vt, float_value, data->scale, data->shift);
547 mb_submit(host, slave, data, vt);
548 } else if (data->register_type == REG_TYPE_INT32) {
555 v.u32 = (((uint32_t)values[0]) << 16) | ((uint32_t)values[1]);
556 DEBUG("Modbus plugin: mb_read_data: "
557 "Returned int32 value is %" PRIi32,
560 CAST_TO_VALUE_T(ds, vt, v.i32, data->scale, data->shift);
561 mb_submit(host, slave, data, vt);
562 } else if (data->register_type == REG_TYPE_INT32_CDAB) {
569 v.u32 = (((uint32_t)values[1]) << 16) | ((uint32_t)values[0]);
570 DEBUG("Modbus plugin: mb_read_data: "
571 "Returned int32 value is %" PRIi32,
574 CAST_TO_VALUE_T(ds, vt, v.i32, data->scale, data->shift);
575 mb_submit(host, slave, data, vt);
576 } else if (data->register_type == REG_TYPE_INT16) {
585 DEBUG("Modbus plugin: mb_read_data: "
586 "Returned int16 value is %" PRIi16,
589 CAST_TO_VALUE_T(ds, vt, v.i16, data->scale, data->shift);
590 mb_submit(host, slave, data, vt);
591 } else if (data->register_type == REG_TYPE_UINT32) {
595 v32 = (((uint32_t)values[0]) << 16) | ((uint32_t)values[1]);
596 DEBUG("Modbus plugin: mb_read_data: "
597 "Returned uint32 value is %" PRIu32,
600 CAST_TO_VALUE_T(ds, vt, v32, data->scale, data->shift);
601 mb_submit(host, slave, data, vt);
602 } else if (data->register_type == REG_TYPE_UINT32_CDAB) {
606 v32 = (((uint32_t)values[1]) << 16) | ((uint32_t)values[0]);
607 DEBUG("Modbus plugin: mb_read_data: "
608 "Returned uint32 value is %" PRIu32,
611 CAST_TO_VALUE_T(ds, vt, v32, data->scale, data->shift);
612 mb_submit(host, slave, data, vt);
613 } else /* if (data->register_type == REG_TYPE_UINT16) */
617 DEBUG("Modbus plugin: mb_read_data: "
618 "Returned uint16 value is %" PRIu16,
621 CAST_TO_VALUE_T(ds, vt, values[0], data->scale, data->shift);
622 mb_submit(host, slave, data, vt);
626 } /* }}} int mb_read_data */
628 static int mb_read_slave(mb_host_t *host, mb_slave_t *slave) /* {{{ */
633 if ((host == NULL) || (slave == NULL))
637 for (mb_data_t *data = slave->collect; data != NULL; data = data->next) {
638 status = mb_read_data(host, slave, data);
647 } /* }}} int mb_read_slave */
649 static int mb_read(user_data_t *user_data) /* {{{ */
655 if ((user_data == NULL) || (user_data->data == NULL))
658 host = user_data->data;
661 for (size_t i = 0; i < host->slaves_num; i++) {
662 status = mb_read_slave(host, host->slaves + i);
671 } /* }}} int mb_read */
675 static void data_free_one(mb_data_t *data) /* {{{ */
682 } /* }}} void data_free_one */
684 static void data_free_all(mb_data_t *data) /* {{{ */
695 } /* }}} void data_free_all */
697 static void slaves_free_all(mb_slave_t *slaves, size_t slaves_num) /* {{{ */
702 for (size_t i = 0; i < slaves_num; i++)
703 data_free_all(slaves[i].collect);
705 } /* }}} void slaves_free_all */
707 static void host_free(void *void_host) /* {{{ */
709 mb_host_t *host = void_host;
714 slaves_free_all(host->slaves, host->slaves_num);
716 } /* }}} void host_free */
718 /* Config functions */
720 static int mb_config_add_data(oconfig_item_t *ci) /* {{{ */
722 mb_data_t data = {0};
726 data.register_type = REG_TYPE_UINT16;
731 status = cf_util_get_string(ci, &data.name);
735 for (int i = 0; i < ci->children_num; i++) {
736 oconfig_item_t *child = ci->children + i;
738 if (strcasecmp("Type", child->key) == 0)
739 status = cf_util_get_string_buffer(child, data.type, sizeof(data.type));
740 else if (strcasecmp("Instance", child->key) == 0)
741 status = cf_util_get_string_buffer(child, data.instance,
742 sizeof(data.instance));
743 else if (strcasecmp("Scale", child->key) == 0)
744 status = cf_util_get_double(child, &data.scale);
745 else if (strcasecmp("Shift", child->key) == 0)
746 status = cf_util_get_double(child, &data.shift);
747 else if (strcasecmp("RegisterBase", child->key) == 0)
748 status = cf_util_get_int(child, &data.register_base);
749 else if (strcasecmp("RegisterType", child->key) == 0) {
751 status = cf_util_get_string_buffer(child, tmp, sizeof(tmp));
754 else if (strcasecmp("Int16", tmp) == 0)
755 data.register_type = REG_TYPE_INT16;
756 else if (strcasecmp("Int32", tmp) == 0)
757 data.register_type = REG_TYPE_INT32;
758 else if (strcasecmp("Int32LE", tmp) == 0)
759 data.register_type = REG_TYPE_INT32_CDAB;
760 else if (strcasecmp("Uint16", tmp) == 0)
761 data.register_type = REG_TYPE_UINT16;
762 else if (strcasecmp("Uint32", tmp) == 0)
763 data.register_type = REG_TYPE_UINT32;
764 else if (strcasecmp("Uint32LE", tmp) == 0)
765 data.register_type = REG_TYPE_UINT32_CDAB;
766 else if (strcasecmp("Float", tmp) == 0)
767 data.register_type = REG_TYPE_FLOAT;
768 else if (strcasecmp("FloatLE", tmp) == 0)
769 data.register_type = REG_TYPE_FLOAT_CDAB;
771 ERROR("Modbus plugin: The register type \"%s\" is unknown.", tmp);
774 } else if (strcasecmp("RegisterCmd", child->key) == 0) {
776 ERROR("Modbus plugin: RegisterCmd parameter can not be used "
777 "with your libmodbus version");
780 status = cf_util_get_string_buffer(child, tmp, sizeof(tmp));
783 else if (strcasecmp("ReadHolding", tmp) == 0)
784 data.modbus_register_type = MREG_HOLDING;
785 else if (strcasecmp("ReadInput", tmp) == 0)
786 data.modbus_register_type = MREG_INPUT;
788 ERROR("Modbus plugin: The modbus_register_type \"%s\" is unknown.",
794 ERROR("Modbus plugin: Unknown configuration option: %s", child->key);
800 } /* for (i = 0; i < ci->children_num; i++) */
802 assert(data.name != NULL);
803 if (data.type[0] == 0) {
804 ERROR("Modbus plugin: Data block \"%s\": No type has been specified.",
810 data_copy(&data_definitions, &data);
815 } /* }}} int mb_config_add_data */
817 static int mb_config_set_host_address(mb_host_t *host, /* {{{ */
818 const char *address) {
819 struct addrinfo *ai_list;
822 if ((host == NULL) || (address == NULL))
825 struct addrinfo ai_hints = {
826 /* XXX: libmodbus can only handle IPv4 addresses. */
827 .ai_family = AF_INET,
828 .ai_flags = AI_ADDRCONFIG};
830 status = getaddrinfo(address, /* service = */ NULL, &ai_hints, &ai_list);
832 ERROR("Modbus plugin: getaddrinfo failed: %s",
833 (status == EAI_SYSTEM) ? STRERRNO : gai_strerror(status));
837 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL;
838 ai_ptr = ai_ptr->ai_next) {
839 status = getnameinfo(ai_ptr->ai_addr, ai_ptr->ai_addrlen, host->node,
841 /* service = */ NULL, /* length = */ 0,
842 /* flags = */ NI_NUMERICHOST);
847 freeaddrinfo(ai_list);
850 ERROR("Modbus plugin: Unable to translate node name: \"%s\"", address);
851 else /* if (status == 0) */
853 DEBUG("Modbus plugin: mb_config_set_host_address: %s -> %s", address,
858 } /* }}} int mb_config_set_host_address */
860 static int mb_config_add_slave(mb_host_t *host, oconfig_item_t *ci) /* {{{ */
865 if ((host == NULL) || (ci == NULL))
868 slave = realloc(host->slaves, sizeof(*slave) * (host->slaves_num + 1));
871 host->slaves = slave;
872 slave = host->slaves + host->slaves_num;
873 memset(slave, 0, sizeof(*slave));
874 slave->collect = NULL;
876 status = cf_util_get_int(ci, &slave->id);
880 for (int i = 0; i < ci->children_num; i++) {
881 oconfig_item_t *child = ci->children + i;
883 if (strcasecmp("Instance", child->key) == 0)
884 status = cf_util_get_string_buffer(child, slave->instance,
885 sizeof(slave->instance));
886 else if (strcasecmp("Collect", child->key) == 0) {
888 status = cf_util_get_string_buffer(child, buffer, sizeof(buffer));
890 data_copy_by_name(&slave->collect, data_definitions, buffer);
891 status = 0; /* continue after failure. */
893 ERROR("Modbus plugin: Unknown configuration option: %s", child->key);
901 if ((status == 0) && (slave->collect == NULL))
909 else /* if (status != 0) */
910 data_free_all(slave->collect);
913 } /* }}} int mb_config_add_slave */
915 static int mb_config_add_host(oconfig_item_t *ci) /* {{{ */
920 host = calloc(1, sizeof(*host));
925 status = cf_util_get_string_buffer(ci, host->host, sizeof(host->host));
930 if (host->host[0] == 0) {
935 for (int i = 0; i < ci->children_num; i++) {
936 oconfig_item_t *child = ci->children + i;
939 if (strcasecmp("Address", child->key) == 0) {
940 char buffer[NI_MAXHOST];
941 status = cf_util_get_string_buffer(child, buffer, sizeof(buffer));
943 status = mb_config_set_host_address(host, buffer);
945 host->conntype = MBCONN_TCP;
946 } else if (strcasecmp("Port", child->key) == 0) {
947 host->port = cf_util_get_port_number(child);
950 } else if (strcasecmp("Device", child->key) == 0) {
951 status = cf_util_get_string_buffer(child, host->node, sizeof(host->node));
953 host->conntype = MBCONN_RTU;
954 } else if (strcasecmp("Baudrate", child->key) == 0)
955 status = cf_util_get_int(child, &host->baudrate);
956 else if (strcasecmp("Interval", child->key) == 0)
957 status = cf_util_get_cdtime(child, &host->interval);
958 else if (strcasecmp("Slave", child->key) == 0)
959 /* Don't set status: Gracefully continue if a slave fails. */
960 mb_config_add_slave(host, child);
962 ERROR("Modbus plugin: Unknown configuration option: %s", child->key);
968 } /* for (i = 0; i < ci->children_num; i++) */
970 assert(host->host[0] != 0);
971 if (host->node[0] == 0) {
972 ERROR("Modbus plugin: Data block \"%s\": No address or device has been "
977 if (host->conntype == MBCONN_RTU && !host->baudrate) {
978 ERROR("Modbus plugin: Data block \"%s\": No serial baudrate has been "
983 if ((host->conntype == MBCONN_TCP && host->baudrate) ||
984 (host->conntype == MBCONN_RTU && host->port)) {
985 ERROR("Modbus plugin: Data block \"%s\": You've mixed up RTU and TCP "
994 snprintf(name, sizeof(name), "modbus-%s", host->host);
996 plugin_register_complex_read(/* group = */ NULL, name,
997 /* callback = */ mb_read,
998 /* interval = */ host->interval,
1000 .data = host, .free_func = host_free,
1007 } /* }}} int mb_config_add_host */
1009 static int mb_config(oconfig_item_t *ci) /* {{{ */
1014 for (int i = 0; i < ci->children_num; i++) {
1015 oconfig_item_t *child = ci->children + i;
1017 if (strcasecmp("Data", child->key) == 0)
1018 mb_config_add_data(child);
1019 else if (strcasecmp("Host", child->key) == 0)
1020 mb_config_add_host(child);
1022 ERROR("Modbus plugin: Unknown configuration option: %s", child->key);
1026 } /* }}} int mb_config */
1030 static int mb_shutdown(void) /* {{{ */
1032 data_free_all(data_definitions);
1033 data_definitions = NULL;
1036 } /* }}} int mb_shutdown */
1038 void module_register(void) {
1039 plugin_register_complex_config("modbus", mb_config);
1040 plugin_register_shutdown("modbus", mb_shutdown);
1041 } /* void module_register */