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;
101 typedef struct mb_data_s mb_data_t;
102 struct mb_data_s /* {{{ */
106 mb_register_type_t register_type;
107 mb_mreg_type_t modbus_register_type;
108 char type[DATA_MAX_NAME_LEN];
109 char instance[DATA_MAX_NAME_LEN];
116 struct mb_slave_s /* {{{ */
119 char instance[DATA_MAX_NAME_LEN];
122 typedef struct mb_slave_s mb_slave_t;
124 struct mb_host_s /* {{{ */
126 char host[DATA_MAX_NAME_LEN];
127 char node[NI_MAXHOST]; /* TCP hostname or RTU serial device */
128 /* char service[NI_MAXSERV]; */
129 int port; /* for Modbus/TCP */
130 int baudrate; /* for Modbus/RTU */
131 mb_conntype_t conntype;
137 modbus_param_t connection;
139 modbus_t *connection;
143 typedef struct mb_host_s mb_host_t;
145 struct mb_data_group_s;
146 typedef struct mb_data_group_s mb_data_group_t;
147 struct mb_data_group_s /* {{{ */
149 mb_data_t *registers;
150 size_t registers_num;
152 mb_data_group_t *next;
158 static mb_data_t *data_definitions;
163 static mb_data_t *data_get_by_name(mb_data_t *src, /* {{{ */
168 for (mb_data_t *ptr = src; ptr != NULL; ptr = ptr->next)
169 if (strcasecmp(ptr->name, name) == 0)
173 } /* }}} mb_data_t *data_get_by_name */
175 static int data_append(mb_data_t **dst, mb_data_t *src) /* {{{ */
179 if ((dst == NULL) || (src == NULL))
189 while (ptr->next != NULL)
195 } /* }}} int data_append */
197 /* Copy a single mb_data_t and append it to another list. */
198 static int data_copy(mb_data_t **dst, const mb_data_t *src) /* {{{ */
203 if ((dst == NULL) || (src == NULL))
206 tmp = malloc(sizeof(*tmp));
209 memcpy(tmp, src, sizeof(*tmp));
213 tmp->name = strdup(src->name);
214 if (tmp->name == NULL) {
219 status = data_append(dst, tmp);
227 } /* }}} int data_copy */
229 /* Lookup a single mb_data_t instance, copy it and append the copy to another
231 static int data_copy_by_name(mb_data_t **dst, mb_data_t *src, /* {{{ */
235 if ((dst == NULL) || (src == NULL) || (name == NULL))
238 ptr = data_get_by_name(src, name);
242 return data_copy(dst, ptr);
243 } /* }}} int data_copy_by_name */
247 static int mb_submit(mb_host_t *host, mb_slave_t *slave, /* {{{ */
248 mb_data_t *data, value_t value) {
249 value_list_t vl = VALUE_LIST_INIT;
251 if ((host == NULL) || (slave == NULL) || (data == NULL))
254 if (slave->instance[0] == 0)
255 snprintf(slave->instance, sizeof(slave->instance), "slave_%i", slave->id);
259 sstrncpy(vl.host, host->host, sizeof(vl.host));
260 sstrncpy(vl.plugin, "modbus", sizeof(vl.plugin));
261 sstrncpy(vl.plugin_instance, slave->instance, sizeof(vl.plugin_instance));
262 sstrncpy(vl.type, data->type, sizeof(vl.type));
263 sstrncpy(vl.type_instance, data->instance, sizeof(vl.type_instance));
265 return plugin_dispatch_values(&vl);
266 } /* }}} int mb_submit */
268 static float mb_register_to_float(uint16_t hi, uint16_t lo) /* {{{ */
276 #if BYTE_ORDER == LITTLE_ENDIAN
278 conv.b[0] = lo & 0x00ff;
279 conv.b[1] = (lo >> 8) & 0x00ff;
280 conv.b[2] = hi & 0x00ff;
281 conv.b[3] = (hi >> 8) & 0x00ff;
283 conv.b[3] = lo & 0x00ff;
284 conv.b[2] = (lo >> 8) & 0x00ff;
285 conv.b[1] = hi & 0x00ff;
286 conv.b[0] = (hi >> 8) & 0x00ff;
290 } /* }}} float mb_register_to_float */
294 static int mb_init_connection(mb_host_t *host) /* {{{ */
302 modbus_set_debug(&host->connection, 1);
305 /* We'll do the error handling ourselves. */
306 modbus_set_error_handling(&host->connection, NOP_ON_ERROR);
308 if (host->conntype == MBCONN_TCP) {
309 if ((host->port < 1) || (host->port > 65535))
310 host->port = MODBUS_TCP_DEFAULT_PORT;
312 DEBUG("Modbus plugin: Trying to connect to \"%s\", port %i.", host->node,
315 modbus_init_tcp(&host->connection,
316 /* host = */ host->node,
317 /* port = */ host->port);
318 } else /* MBCONN_RTU */
320 DEBUG("Modbus plugin: Trying to connect to \"%s\".", host->node);
322 modbus_init_rtu(&host->connection,
323 /* device = */ host->node,
324 /* baudrate = */ host->baudrate, 'N', 8, 1, 0);
327 status = modbus_connect(&host->connection);
329 ERROR("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
330 host->node, host->port ? host->port : host->baudrate, status);
334 host->is_connected = true;
336 } /* }}} int mb_init_connection */
337 /* #endif LEGACY_LIBMODBUS */
339 #else /* if !LEGACY_LIBMODBUS */
341 static int mb_init_connection(mb_host_t *host) /* {{{ */
348 if (host->connection != NULL)
351 if (host->conntype == MBCONN_TCP) {
352 if ((host->port < 1) || (host->port > 65535))
353 host->port = MODBUS_TCP_DEFAULT_PORT;
355 DEBUG("Modbus plugin: Trying to connect to \"%s\", port %i.", host->node,
358 host->connection = modbus_new_tcp(host->node, host->port);
359 if (host->connection == NULL) {
360 ERROR("Modbus plugin: Creating new Modbus/TCP object failed.");
364 DEBUG("Modbus plugin: Trying to connect to \"%s\", baudrate %i.",
365 host->node, host->baudrate);
367 host->connection = modbus_new_rtu(host->node, host->baudrate, 'N', 8, 1);
368 if (host->connection == NULL) {
369 ERROR("Modbus plugin: Creating new Modbus/RTU object failed.");
375 modbus_set_debug(host->connection, 1);
378 /* We'll do the error handling ourselves. */
379 modbus_set_error_recovery(host->connection, 0);
381 status = modbus_connect(host->connection);
383 ERROR("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
384 host->node, host->port ? host->port : host->baudrate, status);
385 modbus_free(host->connection);
386 host->connection = NULL;
391 } /* }}} int mb_init_connection */
392 #endif /* !LEGACY_LIBMODBUS */
394 #define CAST_TO_VALUE_T(ds, vt, raw, scale, shift) \
396 if ((ds)->ds[0].type == DS_TYPE_COUNTER) \
397 (vt).counter = (((counter_t)(raw)*scale) + shift); \
398 else if ((ds)->ds[0].type == DS_TYPE_GAUGE) \
399 (vt).gauge = (((gauge_t)(raw)*scale) + shift); \
400 else if ((ds)->ds[0].type == DS_TYPE_DERIVE) \
401 (vt).derive = (((derive_t)(raw)*scale) + shift); \
402 else /* if (ds->ds[0].type == DS_TYPE_ABSOLUTE) */ \
403 (vt).absolute = (((absolute_t)(raw)*scale) + shift); \
406 static int mb_read_data(mb_host_t *host, mb_slave_t *slave, /* {{{ */
408 uint16_t values[4] = {0};
410 const data_set_t *ds;
413 if ((host == NULL) || (slave == NULL) || (data == NULL))
416 ds = plugin_get_ds(data->type);
418 ERROR("Modbus plugin: Type \"%s\" is not defined.", data->type);
422 if (ds->ds_num != 1) {
423 ERROR("Modbus plugin: The type \"%s\" has %" PRIsz " data sources. "
424 "I can only handle data sets with only one data source.",
425 data->type, ds->ds_num);
429 if ((ds->ds[0].type != DS_TYPE_GAUGE) &&
430 (data->register_type != REG_TYPE_INT32) &&
431 (data->register_type != REG_TYPE_INT32_CDAB) &&
432 (data->register_type != REG_TYPE_UINT32) &&
433 (data->register_type != REG_TYPE_UINT32_CDAB) &&
434 (data->register_type != REG_TYPE_INT64) &&
435 (data->register_type != REG_TYPE_UINT64)) {
437 "Modbus plugin: The data source of type \"%s\" is %s, not gauge. "
438 "This will most likely result in problems, because the register type "
439 "is not UINT32 or UINT64.",
440 data->type, DS_TYPE_TO_STRING(ds->ds[0].type));
443 if ((data->register_type == REG_TYPE_INT32) ||
444 (data->register_type == REG_TYPE_INT32_CDAB) ||
445 (data->register_type == REG_TYPE_UINT32) ||
446 (data->register_type == REG_TYPE_UINT32_CDAB) ||
447 (data->register_type == REG_TYPE_FLOAT) ||
448 (data->register_type == REG_TYPE_FLOAT_CDAB))
450 else if ((data->register_type == REG_TYPE_INT64) ||
451 (data->register_type == REG_TYPE_UINT64))
456 if (host->connection == NULL) {
458 } else if (host->conntype == MBCONN_TCP) {
459 /* getpeername() is used only to determine if the socket is connected, not
460 * because we're really interested in the peer's IP address. */
461 if (getpeername(modbus_get_socket(host->connection),
462 (void *)&(struct sockaddr_storage){0},
463 &(socklen_t){sizeof(struct sockaddr_storage)}) != 0)
467 if ((status == EBADF) || (status == ENOTSOCK) || (status == ENOTCONN)) {
468 status = mb_init_connection(host);
470 ERROR("Modbus plugin: mb_init_connection (%s/%s) failed. ", host->host,
472 host->is_connected = false;
473 host->connection = NULL;
476 } else if (status != 0) {
478 modbus_close(&host->connection);
480 modbus_close(host->connection);
481 modbus_free(host->connection);
486 /* Version 2.0.3: Pass the connection struct as a pointer and pass the slave
487 * id to each call of "read_holding_registers". */
488 #define modbus_read_registers(ctx, addr, nb, dest) \
489 read_holding_registers(&(ctx), slave->id, (addr), (nb), (dest))
490 #else /* if !LEGACY_LIBMODBUS */
491 /* Version 2.9.2: Set the slave id once before querying the registers. */
492 status = modbus_set_slave(host->connection, slave->id);
494 ERROR("Modbus plugin: modbus_set_slave (%i) failed with status %i.",
499 if (data->modbus_register_type == MREG_INPUT) {
500 status = modbus_read_input_registers(host->connection,
501 /* start_addr = */ data->register_base,
502 /* num_registers = */ values_num,
503 /* buffer = */ values);
505 status = modbus_read_registers(host->connection,
506 /* start_addr = */ data->register_base,
507 /* num_registers = */ values_num,
508 /* buffer = */ values);
510 if (status != values_num) {
511 ERROR("Modbus plugin: modbus read function (%s/%s) failed. "
512 " status = %i, start_addr = %i, values_num = %i. Giving up.",
513 host->host, host->node, status, data->register_base, values_num);
515 modbus_close(&host->connection);
517 modbus_close(host->connection);
518 modbus_free(host->connection);
520 host->connection = NULL;
524 DEBUG("Modbus plugin: mb_read_data: Success! "
525 "modbus_read_registers returned with status %i.",
528 if (data->register_type == REG_TYPE_FLOAT) {
532 float_value = mb_register_to_float(values[0], values[1]);
533 DEBUG("Modbus plugin: mb_read_data: "
534 "Returned float value is %g",
535 (double)float_value);
537 CAST_TO_VALUE_T(ds, vt, float_value, data->scale, data->shift);
538 mb_submit(host, slave, data, vt);
539 } else if (data->register_type == REG_TYPE_FLOAT_CDAB) {
543 float_value = mb_register_to_float(values[1], values[0]);
544 DEBUG("Modbus plugin: mb_read_data: "
545 "Returned float value is %g",
546 (double)float_value);
548 CAST_TO_VALUE_T(ds, vt, float_value, data->scale, data->shift);
549 mb_submit(host, slave, data, vt);
550 } else if (data->register_type == REG_TYPE_INT32) {
557 v.u32 = (((uint32_t)values[0]) << 16) | ((uint32_t)values[1]);
558 DEBUG("Modbus plugin: mb_read_data: "
559 "Returned int32 value is %" PRIi32,
562 CAST_TO_VALUE_T(ds, vt, v.i32, data->scale, data->shift);
563 mb_submit(host, slave, data, vt);
564 } else if (data->register_type == REG_TYPE_INT32_CDAB) {
571 v.u32 = (((uint32_t)values[1]) << 16) | ((uint32_t)values[0]);
572 DEBUG("Modbus plugin: mb_read_data: "
573 "Returned int32 value is %" PRIi32,
576 CAST_TO_VALUE_T(ds, vt, v.i32, data->scale, data->shift);
577 mb_submit(host, slave, data, vt);
578 } else if (data->register_type == REG_TYPE_INT16) {
587 DEBUG("Modbus plugin: mb_read_data: "
588 "Returned int16 value is %" PRIi16,
591 CAST_TO_VALUE_T(ds, vt, v.i16, data->scale, data->shift);
592 mb_submit(host, slave, data, vt);
593 } else if (data->register_type == REG_TYPE_UINT32) {
597 v32 = (((uint32_t)values[0]) << 16) | ((uint32_t)values[1]);
598 DEBUG("Modbus plugin: mb_read_data: "
599 "Returned uint32 value is %" PRIu32,
602 CAST_TO_VALUE_T(ds, vt, v32, data->scale, data->shift);
603 mb_submit(host, slave, data, vt);
604 } else if (data->register_type == REG_TYPE_UINT32_CDAB) {
608 v32 = (((uint32_t)values[1]) << 16) | ((uint32_t)values[0]);
609 DEBUG("Modbus plugin: mb_read_data: "
610 "Returned uint32 value is %" PRIu32,
613 CAST_TO_VALUE_T(ds, vt, v32, data->scale, data->shift);
614 mb_submit(host, slave, data, vt);
615 } else if (data->register_type == REG_TYPE_UINT64) {
619 v64 = (((uint64_t)values[0]) << 48) | (((uint64_t)values[1]) << 32) |
620 (((uint64_t)values[2]) << 16) | (((uint64_t)values[3]));
621 DEBUG("Modbus plugin: mb_read_data: "
622 "Returned uint64 value is %" PRIu64,
625 CAST_TO_VALUE_T(ds, vt, v64, data->scale, data->shift);
626 mb_submit(host, slave, data, vt);
627 } else if (data->register_type == REG_TYPE_INT64) {
634 v.u64 = (((uint64_t)values[0]) << 48) | (((uint64_t)values[1]) << 32) |
635 (((uint64_t)values[2]) << 16) | ((uint64_t)values[3]);
636 DEBUG("Modbus plugin: mb_read_data: "
637 "Returned uint64 value is %" PRIi64,
640 CAST_TO_VALUE_T(ds, vt, v.i64, data->scale, data->shift);
641 mb_submit(host, slave, data, vt);
642 } else /* if (data->register_type == REG_TYPE_UINT16) */
646 DEBUG("Modbus plugin: mb_read_data: "
647 "Returned uint16 value is %" PRIu16,
650 CAST_TO_VALUE_T(ds, vt, values[0], data->scale, data->shift);
651 mb_submit(host, slave, data, vt);
655 } /* }}} int mb_read_data */
657 static int mb_read_slave(mb_host_t *host, mb_slave_t *slave) /* {{{ */
662 if ((host == NULL) || (slave == NULL))
666 for (mb_data_t *data = slave->collect; data != NULL; data = data->next) {
667 status = mb_read_data(host, slave, data);
676 } /* }}} int mb_read_slave */
678 static int mb_read(user_data_t *user_data) /* {{{ */
684 if ((user_data == NULL) || (user_data->data == NULL))
687 host = user_data->data;
690 for (size_t i = 0; i < host->slaves_num; i++) {
691 status = mb_read_slave(host, host->slaves + i);
700 } /* }}} int mb_read */
704 static void data_free_one(mb_data_t *data) /* {{{ */
711 } /* }}} void data_free_one */
713 static void data_free_all(mb_data_t *data) /* {{{ */
724 } /* }}} void data_free_all */
726 static void slaves_free_all(mb_slave_t *slaves, size_t slaves_num) /* {{{ */
731 for (size_t i = 0; i < slaves_num; i++)
732 data_free_all(slaves[i].collect);
734 } /* }}} void slaves_free_all */
736 static void host_free(void *void_host) /* {{{ */
738 mb_host_t *host = void_host;
743 slaves_free_all(host->slaves, host->slaves_num);
745 } /* }}} void host_free */
747 /* Config functions */
749 static int mb_config_add_data(oconfig_item_t *ci) /* {{{ */
751 mb_data_t data = {0};
755 data.register_type = REG_TYPE_UINT16;
760 status = cf_util_get_string(ci, &data.name);
764 for (int i = 0; i < ci->children_num; i++) {
765 oconfig_item_t *child = ci->children + i;
767 if (strcasecmp("Type", child->key) == 0)
768 status = cf_util_get_string_buffer(child, data.type, sizeof(data.type));
769 else if (strcasecmp("Instance", child->key) == 0)
770 status = cf_util_get_string_buffer(child, data.instance,
771 sizeof(data.instance));
772 else if (strcasecmp("Scale", child->key) == 0)
773 status = cf_util_get_double(child, &data.scale);
774 else if (strcasecmp("Shift", child->key) == 0)
775 status = cf_util_get_double(child, &data.shift);
776 else if (strcasecmp("RegisterBase", child->key) == 0)
777 status = cf_util_get_int(child, &data.register_base);
778 else if (strcasecmp("RegisterType", child->key) == 0) {
780 status = cf_util_get_string_buffer(child, tmp, sizeof(tmp));
783 else if (strcasecmp("Int16", tmp) == 0)
784 data.register_type = REG_TYPE_INT16;
785 else if (strcasecmp("Int32", tmp) == 0)
786 data.register_type = REG_TYPE_INT32;
787 else if (strcasecmp("Int32LE", tmp) == 0)
788 data.register_type = REG_TYPE_INT32_CDAB;
789 else if (strcasecmp("Uint16", tmp) == 0)
790 data.register_type = REG_TYPE_UINT16;
791 else if (strcasecmp("Uint32", tmp) == 0)
792 data.register_type = REG_TYPE_UINT32;
793 else if (strcasecmp("Uint32LE", tmp) == 0)
794 data.register_type = REG_TYPE_UINT32_CDAB;
795 else if (strcasecmp("Float", tmp) == 0)
796 data.register_type = REG_TYPE_FLOAT;
797 else if (strcasecmp("FloatLE", tmp) == 0)
798 data.register_type = REG_TYPE_FLOAT_CDAB;
799 else if (strcasecmp("Uint64", tmp) == 0)
800 data.register_type = REG_TYPE_UINT64;
801 else if (strcasecmp("Int64", tmp) == 0)
802 data.register_type = REG_TYPE_INT64;
804 ERROR("Modbus plugin: The register type \"%s\" is unknown.", tmp);
807 } else if (strcasecmp("RegisterCmd", child->key) == 0) {
809 ERROR("Modbus plugin: RegisterCmd parameter can not be used "
810 "with your libmodbus version");
813 status = cf_util_get_string_buffer(child, tmp, sizeof(tmp));
816 else if (strcasecmp("ReadHolding", tmp) == 0)
817 data.modbus_register_type = MREG_HOLDING;
818 else if (strcasecmp("ReadInput", tmp) == 0)
819 data.modbus_register_type = MREG_INPUT;
821 ERROR("Modbus plugin: The modbus_register_type \"%s\" is unknown.",
827 ERROR("Modbus plugin: Unknown configuration option: %s", child->key);
833 } /* for (i = 0; i < ci->children_num; i++) */
835 assert(data.name != NULL);
836 if (data.type[0] == 0) {
837 ERROR("Modbus plugin: Data block \"%s\": No type has been specified.",
843 data_copy(&data_definitions, &data);
848 } /* }}} int mb_config_add_data */
850 static int mb_config_set_host_address(mb_host_t *host, /* {{{ */
851 const char *address) {
852 struct addrinfo *ai_list;
855 if ((host == NULL) || (address == NULL))
858 struct addrinfo ai_hints = {
859 /* XXX: libmodbus can only handle IPv4 addresses. */
860 .ai_family = AF_INET,
861 .ai_flags = AI_ADDRCONFIG};
863 status = getaddrinfo(address, /* service = */ NULL, &ai_hints, &ai_list);
865 ERROR("Modbus plugin: getaddrinfo failed: %s",
866 (status == EAI_SYSTEM) ? STRERRNO : gai_strerror(status));
870 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL;
871 ai_ptr = ai_ptr->ai_next) {
872 status = getnameinfo(ai_ptr->ai_addr, ai_ptr->ai_addrlen, host->node,
874 /* service = */ NULL, /* length = */ 0,
875 /* flags = */ NI_NUMERICHOST);
880 freeaddrinfo(ai_list);
883 ERROR("Modbus plugin: Unable to translate node name: \"%s\"", address);
884 else /* if (status == 0) */
886 DEBUG("Modbus plugin: mb_config_set_host_address: %s -> %s", address,
891 } /* }}} int mb_config_set_host_address */
893 static int mb_config_add_slave(mb_host_t *host, oconfig_item_t *ci) /* {{{ */
898 if ((host == NULL) || (ci == NULL))
901 slave = realloc(host->slaves, sizeof(*slave) * (host->slaves_num + 1));
904 host->slaves = slave;
905 slave = host->slaves + host->slaves_num;
906 memset(slave, 0, sizeof(*slave));
907 slave->collect = NULL;
909 status = cf_util_get_int(ci, &slave->id);
913 for (int i = 0; i < ci->children_num; i++) {
914 oconfig_item_t *child = ci->children + i;
916 if (strcasecmp("Instance", child->key) == 0)
917 status = cf_util_get_string_buffer(child, slave->instance,
918 sizeof(slave->instance));
919 else if (strcasecmp("Collect", child->key) == 0) {
921 status = cf_util_get_string_buffer(child, buffer, sizeof(buffer));
923 data_copy_by_name(&slave->collect, data_definitions, buffer);
924 status = 0; /* continue after failure. */
926 ERROR("Modbus plugin: Unknown configuration option: %s", child->key);
934 if ((status == 0) && (slave->collect == NULL))
942 else /* if (status != 0) */
943 data_free_all(slave->collect);
946 } /* }}} int mb_config_add_slave */
948 static int mb_config_add_host(oconfig_item_t *ci) /* {{{ */
950 cdtime_t interval = 0;
954 host = calloc(1, sizeof(*host));
959 status = cf_util_get_string_buffer(ci, host->host, sizeof(host->host));
964 if (host->host[0] == 0) {
969 for (int i = 0; i < ci->children_num; i++) {
970 oconfig_item_t *child = ci->children + i;
973 if (strcasecmp("Address", child->key) == 0) {
974 char buffer[NI_MAXHOST];
975 status = cf_util_get_string_buffer(child, buffer, sizeof(buffer));
977 status = mb_config_set_host_address(host, buffer);
979 host->conntype = MBCONN_TCP;
980 } else if (strcasecmp("Port", child->key) == 0) {
981 host->port = cf_util_get_port_number(child);
984 } else if (strcasecmp("Device", child->key) == 0) {
985 status = cf_util_get_string_buffer(child, host->node, sizeof(host->node));
987 host->conntype = MBCONN_RTU;
988 } else if (strcasecmp("Baudrate", child->key) == 0)
989 status = cf_util_get_int(child, &host->baudrate);
990 else if (strcasecmp("Interval", child->key) == 0)
991 status = cf_util_get_cdtime(child, &interval);
992 else if (strcasecmp("Slave", child->key) == 0)
993 /* Don't set status: Gracefully continue if a slave fails. */
994 mb_config_add_slave(host, child);
996 ERROR("Modbus plugin: Unknown configuration option: %s", child->key);
1002 } /* for (i = 0; i < ci->children_num; i++) */
1004 assert(host->host[0] != 0);
1005 if (host->node[0] == 0) {
1006 ERROR("Modbus plugin: Data block \"%s\": No address or device has been "
1011 if (host->conntype == MBCONN_RTU && !host->baudrate) {
1012 ERROR("Modbus plugin: Data block \"%s\": No serial baudrate has been "
1017 if ((host->conntype == MBCONN_TCP && host->baudrate) ||
1018 (host->conntype == MBCONN_RTU && host->port)) {
1019 ERROR("Modbus plugin: Data block \"%s\": You've mixed up RTU and TCP "
1028 snprintf(name, sizeof(name), "modbus-%s", host->host);
1030 plugin_register_complex_read(/* group = */ NULL, name,
1031 /* callback = */ mb_read,
1032 /* interval = */ interval,
1034 .data = host, .free_func = host_free,
1041 } /* }}} int mb_config_add_host */
1043 static int mb_config(oconfig_item_t *ci) /* {{{ */
1048 for (int i = 0; i < ci->children_num; i++) {
1049 oconfig_item_t *child = ci->children + i;
1051 if (strcasecmp("Data", child->key) == 0)
1052 mb_config_add_data(child);
1053 else if (strcasecmp("Host", child->key) == 0)
1054 mb_config_add_host(child);
1056 ERROR("Modbus plugin: Unknown configuration option: %s", child->key);
1060 } /* }}} int mb_config */
1064 static int mb_shutdown(void) /* {{{ */
1066 data_free_all(data_definitions);
1067 data_definitions = NULL;
1070 } /* }}} int mb_shutdown */
1072 void module_register(void) {
1073 plugin_register_complex_config("modbus", mb_config);
1074 plugin_register_shutdown("modbus", mb_shutdown);
1075 } /* void module_register */