1 /**
2 * collectd - src/modbus.c
3 * Copyright (C) 2010,2011 noris network AG
4 *
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
8 * applicable.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Florian Forster <octo at noris.net>
21 **/
22
23 #include "collectd.h"
24
25 #include "configfile.h"
26 #include "plugin.h"
27 #include "utils/common/common.h"
28
29 #include <modbus.h>
30 #include <netdb.h>
31 #include <sys/socket.h>
32
33 #ifndef LIBMODBUS_VERSION_CHECK
34 /* Assume version 2.0.3 */
35 #define LEGACY_LIBMODBUS 1
36 #else
37 /* Assume version 2.9.2 */
38 #endif
39
40 #ifndef MODBUS_TCP_DEFAULT_PORT
41 #ifdef MODBUS_TCP_PORT
42 #define MODBUS_TCP_DEFAULT_PORT MODBUS_TCP_PORT
43 #else
44 #define MODBUS_TCP_DEFAULT_PORT 502
45 #endif
46 #endif
47
48 /*
49 * <Data "data_name">
50 * RegisterBase 1234
51 * RegisterCmd ReadHolding
52 * RegisterType float
53 * Type gauge
54 * Instance "..."
55 * </Data>
56 *
57 * <Host "name">
58 * Address "addr"
59 * Port "1234"
60 * # Or:
61 * # Device "/dev/ttyUSB0"
62 * # Baudrate 38400
63 * # (Assumes 8N1)
64 * Interval 60
65 *
66 * <Slave 1>
67 * Instance "foobar" # optional
68 * Collect "data_name"
69 * </Slave>
70 * </Host>
71 */
72
73 /*
74 * Data structures
75 */
76 enum mb_register_type_e /* {{{ */
77 { REG_TYPE_INT16,
78 REG_TYPE_INT32,
79 REG_TYPE_INT32_CDAB,
80 REG_TYPE_UINT16,
81 REG_TYPE_UINT32,
82 REG_TYPE_UINT32_CDAB,
83 REG_TYPE_INT64,
84 REG_TYPE_UINT64,
85 REG_TYPE_FLOAT,
86 REG_TYPE_FLOAT_CDAB }; /* }}} */
87
88 enum mb_mreg_type_e /* {{{ */
89 { MREG_HOLDING,
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;
93
94 /* TCP or RTU depending on what is specified in host config block */
95 enum mb_conntype_e /* {{{ */
96 { MBCONN_TCP,
97 MBCONN_RTU }; /* }}} */
98 typedef enum mb_conntype_e mb_conntype_t;
99
100 enum mb_uarttype_e /* {{{ */
101 { UARTTYPE_RS232,
102 UARTTYPE_RS422,
103 UARTTYPE_RS485 }; /* }}} */
104 typedef enum mb_uarttype_e mb_uarttype_t;
105
106 struct mb_data_s;
107 typedef struct mb_data_s mb_data_t;
108 struct mb_data_s /* {{{ */
109 {
110 char *name;
111 int register_base;
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];
116 double scale;
117 double shift;
118
119 mb_data_t *next;
120 }; /* }}} */
121
122 struct mb_slave_s /* {{{ */
123 {
124 int id;
125 char instance[DATA_MAX_NAME_LEN];
126 mb_data_t *collect;
127 }; /* }}} */
128 typedef struct mb_slave_s mb_slave_t;
129
130 struct mb_host_s /* {{{ */
131 {
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;
139
140 mb_slave_t *slaves;
141 size_t slaves_num;
142
143 #if LEGACY_LIBMODBUS
144 modbus_param_t connection;
145 #else
146 modbus_t *connection;
147 #endif
148 bool is_connected;
149 }; /* }}} */
150 typedef struct mb_host_s mb_host_t;
151
152 struct mb_data_group_s;
153 typedef struct mb_data_group_s mb_data_group_t;
154 struct mb_data_group_s /* {{{ */
155 {
156 mb_data_t *registers;
157 size_t registers_num;
158
159 mb_data_group_t *next;
160 }; /* }}} */
161
162 /*
163 * Global variables
164 */
165 static mb_data_t *data_definitions;
166
167 /*
168 * Functions
169 */
data_get_by_name(mb_data_t * src,const char * name)170 static mb_data_t *data_get_by_name(mb_data_t *src, /* {{{ */
171 const char *name) {
172 if (name == NULL)
173 return NULL;
174
175 for (mb_data_t *ptr = src; ptr != NULL; ptr = ptr->next)
176 if (strcasecmp(ptr->name, name) == 0)
177 return ptr;
178
179 return NULL;
180 } /* }}} mb_data_t *data_get_by_name */
181
data_append(mb_data_t ** dst,mb_data_t * src)182 static int data_append(mb_data_t **dst, mb_data_t *src) /* {{{ */
183 {
184 mb_data_t *ptr;
185
186 if ((dst == NULL) || (src == NULL))
187 return EINVAL;
188
189 ptr = *dst;
190
191 if (ptr == NULL) {
192 *dst = src;
193 return 0;
194 }
195
196 while (ptr->next != NULL)
197 ptr = ptr->next;
198
199 ptr->next = src;
200
201 return 0;
202 } /* }}} int data_append */
203
204 /* Copy a single mb_data_t and append it to another list. */
data_copy(mb_data_t ** dst,const mb_data_t * src)205 static int data_copy(mb_data_t **dst, const mb_data_t *src) /* {{{ */
206 {
207 mb_data_t *tmp;
208 int status;
209
210 if ((dst == NULL) || (src == NULL))
211 return EINVAL;
212
213 tmp = malloc(sizeof(*tmp));
214 if (tmp == NULL)
215 return ENOMEM;
216 memcpy(tmp, src, sizeof(*tmp));
217 tmp->name = NULL;
218 tmp->next = NULL;
219
220 tmp->name = strdup(src->name);
221 if (tmp->name == NULL) {
222 sfree(tmp);
223 return ENOMEM;
224 }
225
226 status = data_append(dst, tmp);
227 if (status != 0) {
228 sfree(tmp->name);
229 sfree(tmp);
230 return status;
231 }
232
233 return 0;
234 } /* }}} int data_copy */
235
236 /* Lookup a single mb_data_t instance, copy it and append the copy to another
237 * list. */
data_copy_by_name(mb_data_t ** dst,mb_data_t * src,const char * name)238 static int data_copy_by_name(mb_data_t **dst, mb_data_t *src, /* {{{ */
239 const char *name) {
240 mb_data_t *ptr;
241
242 if ((dst == NULL) || (src == NULL) || (name == NULL))
243 return EINVAL;
244
245 ptr = data_get_by_name(src, name);
246 if (ptr == NULL)
247 return ENOENT;
248
249 return data_copy(dst, ptr);
250 } /* }}} int data_copy_by_name */
251
252 /* Read functions */
253
mb_submit(mb_host_t * host,mb_slave_t * slave,mb_data_t * data,value_t value)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;
257
258 if ((host == NULL) || (slave == NULL) || (data == NULL))
259 return EINVAL;
260
261 if (slave->instance[0] == 0)
262 ssnprintf(slave->instance, sizeof(slave->instance), "slave_%i", slave->id);
263
264 vl.values = &value;
265 vl.values_len = 1;
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));
271
272 return plugin_dispatch_values(&vl);
273 } /* }}} int mb_submit */
274
mb_register_to_float(uint16_t hi,uint16_t lo)275 static float mb_register_to_float(uint16_t hi, uint16_t lo) /* {{{ */
276 {
277 union {
278 uint8_t b[4];
279 uint16_t s[2];
280 float f;
281 } conv;
282
283 #if BYTE_ORDER == LITTLE_ENDIAN
284 /* 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;
289 #else
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;
294 #endif
295
296 return conv.f;
297 } /* }}} float mb_register_to_float */
298
299 #if LEGACY_LIBMODBUS
300 /* Version 2.0.3 */
mb_init_connection(mb_host_t * host)301 static int mb_init_connection(mb_host_t *host) /* {{{ */
302 {
303 int status;
304
305 if (host == NULL)
306 return EINVAL;
307
308 #if COLLECT_DEBUG
309 modbus_set_debug(&host->connection, 1);
310 #endif
311
312 /* We'll do the error handling ourselves. */
313 modbus_set_error_handling(&host->connection, NOP_ON_ERROR);
314
315 if (host->conntype == MBCONN_TCP) {
316 if ((host->port < 1) || (host->port > 65535))
317 host->port = MODBUS_TCP_DEFAULT_PORT;
318
319 DEBUG("Modbus plugin: Trying to connect to \"%s\", port %i.", host->node,
320 host->port);
321
322 modbus_init_tcp(&host->connection,
323 /* host = */ host->node,
324 /* port = */ host->port);
325 } else /* MBCONN_RTU */
326 {
327 DEBUG("Modbus plugin: Trying to connect to \"%s\".", host->node);
328
329 modbus_init_rtu(&host->connection,
330 /* device = */ host->node,
331 /* baudrate = */ host->baudrate, 'N', 8, 1, 0);
332 }
333
334 status = modbus_connect(&host->connection);
335 if (status != 0) {
336 ERROR("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
337 host->node, host->port ? host->port : host->baudrate, status);
338 return status;
339 }
340
341 host->is_connected = true;
342 return 0;
343 } /* }}} int mb_init_connection */
344 /* #endif LEGACY_LIBMODBUS */
345
346 #else /* if !LEGACY_LIBMODBUS */
347 /* Version 2.9.2 */
mb_init_connection(mb_host_t * host)348 static int mb_init_connection(mb_host_t *host) /* {{{ */
349 {
350 int status;
351
352 if (host == NULL)
353 return EINVAL;
354
355 if (host->connection != NULL)
356 return 0;
357
358 if (host->conntype == MBCONN_TCP) {
359 if ((host->port < 1) || (host->port > 65535))
360 host->port = MODBUS_TCP_DEFAULT_PORT;
361
362 DEBUG("Modbus plugin: Trying to connect to \"%s\", port %i.", host->node,
363 host->port);
364
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.");
368 return -1;
369 }
370 } else {
371 DEBUG("Modbus plugin: Trying to connect to \"%s\", baudrate %i.",
372 host->node, host->baudrate);
373
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.");
377 return -1;
378 }
379 }
380
381 #if COLLECT_DEBUG
382 modbus_set_debug(host->connection, 1);
383 #endif
384
385 /* We'll do the error handling ourselves. */
386 modbus_set_error_recovery(host->connection, 0);
387
388 status = modbus_connect(host->connection);
389 if (status != 0) {
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;
394 return status;
395 }
396
397 #if defined(linux) && LIBMODBUS_VERSION_CHECK(2, 9, 4)
398 switch (host->uarttype) {
399 case UARTTYPE_RS485:
400 if (modbus_rtu_set_serial_mode(host->connection, MODBUS_RTU_RS485))
401 DEBUG("Modbus plugin: Setting RS485 mode failed.");
402 break;
403 case UARTTYPE_RS422:
404 /* libmodbus doesn't say anything about full-duplex symmetric RS422 UART */
405 break;
406 case UARTTYPE_RS232:
407 break;
408 default:
409 DEBUG("Modbus plugin: Invalid UART type!.");
410 }
411 #endif /* defined(linux) && LIBMODBUS_VERSION_CHECK(2, 9, 4) */
412
413 return 0;
414 } /* }}} int mb_init_connection */
415 #endif /* !LEGACY_LIBMODBUS */
416
417 #define CAST_TO_VALUE_T(ds, vt, raw, scale, shift) \
418 do { \
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); \
427 } while (0)
428
mb_read_data(mb_host_t * host,mb_slave_t * slave,mb_data_t * data)429 static int mb_read_data(mb_host_t *host, mb_slave_t *slave, /* {{{ */
430 mb_data_t *data) {
431 uint16_t values[4] = {0};
432 int values_num;
433 const data_set_t *ds;
434 int status = 0;
435
436 if ((host == NULL) || (slave == NULL) || (data == NULL))
437 return EINVAL;
438
439 ds = plugin_get_ds(data->type);
440 if (ds == NULL) {
441 ERROR("Modbus plugin: Type \"%s\" is not defined.", data->type);
442 return -1;
443 }
444
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);
449 return -1;
450 }
451
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)) {
459 NOTICE(
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));
464 }
465
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))
472 values_num = 2;
473 else if ((data->register_type == REG_TYPE_INT64) ||
474 (data->register_type == REG_TYPE_UINT64))
475 values_num = 4;
476 else
477 values_num = 1;
478
479 if (host->connection == NULL) {
480 status = EBADF;
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)
487 status = errno;
488 }
489
490 if ((status == EBADF) || (status == ENOTSOCK) || (status == ENOTCONN)) {
491 status = mb_init_connection(host);
492 if (status != 0) {
493 ERROR("Modbus plugin: mb_init_connection (%s/%s) failed. ", host->host,
494 host->node);
495 host->is_connected = false;
496 host->connection = NULL;
497 return -1;
498 }
499 } else if (status != 0) {
500 #if LEGACY_LIBMODBUS
501 modbus_close(&host->connection);
502 #else
503 modbus_close(host->connection);
504 modbus_free(host->connection);
505 #endif
506 }
507
508 #if LEGACY_LIBMODBUS
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);
516 if (status != 0) {
517 ERROR("Modbus plugin: modbus_set_slave (%i) failed with status %i.",
518 slave->id, status);
519 return -1;
520 }
521 #endif
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);
527 } else {
528 status = modbus_read_registers(host->connection,
529 /* start_addr = */ data->register_base,
530 /* num_registers = */ values_num,
531 /* buffer = */ values);
532 }
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);
537 #if LEGACY_LIBMODBUS
538 modbus_close(&host->connection);
539 #else
540 modbus_close(host->connection);
541 modbus_free(host->connection);
542 #endif
543 host->connection = NULL;
544 return -1;
545 }
546
547 DEBUG("Modbus plugin: mb_read_data: Success! "
548 "modbus_read_registers returned with status %i.",
549 status);
550
551 if (data->register_type == REG_TYPE_FLOAT) {
552 float float_value;
553 value_t vt;
554
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);
559
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) {
563 float float_value;
564 value_t vt;
565
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);
570
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) {
574 union {
575 uint32_t u32;
576 int32_t i32;
577 } v;
578 value_t vt;
579
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,
583 v.i32);
584
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) {
588 union {
589 uint32_t u32;
590 int32_t i32;
591 } v;
592 value_t vt;
593
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,
597 v.i32);
598
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) {
602 union {
603 uint16_t u16;
604 int16_t i16;
605 } v;
606 value_t vt;
607
608 v.u16 = values[0];
609
610 DEBUG("Modbus plugin: mb_read_data: "
611 "Returned int16 value is %" PRIi16,
612 v.i16);
613
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) {
617 uint32_t v32;
618 value_t vt;
619
620 v32 = (((uint32_t)values[0]) << 16) | ((uint32_t)values[1]);
621 DEBUG("Modbus plugin: mb_read_data: "
622 "Returned uint32 value is %" PRIu32,
623 v32);
624
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) {
628 uint32_t v32;
629 value_t vt;
630
631 v32 = (((uint32_t)values[1]) << 16) | ((uint32_t)values[0]);
632 DEBUG("Modbus plugin: mb_read_data: "
633 "Returned uint32 value is %" PRIu32,
634 v32);
635
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) {
639 uint64_t v64;
640 value_t vt;
641
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,
646 v64);
647
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) {
651 union {
652 uint64_t u64;
653 int64_t i64;
654 } v;
655 value_t vt;
656
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,
661 v.i64);
662
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) */
666 {
667 value_t vt;
668
669 DEBUG("Modbus plugin: mb_read_data: "
670 "Returned uint16 value is %" PRIu16,
671 values[0]);
672
673 CAST_TO_VALUE_T(ds, vt, values[0], data->scale, data->shift);
674 mb_submit(host, slave, data, vt);
675 }
676
677 return 0;
678 } /* }}} int mb_read_data */
679
mb_read_slave(mb_host_t * host,mb_slave_t * slave)680 static int mb_read_slave(mb_host_t *host, mb_slave_t *slave) /* {{{ */
681 {
682 int success;
683 int status;
684
685 if ((host == NULL) || (slave == NULL))
686 return EINVAL;
687
688 success = 0;
689 for (mb_data_t *data = slave->collect; data != NULL; data = data->next) {
690 status = mb_read_data(host, slave, data);
691 if (status == 0)
692 success++;
693 }
694
695 if (success == 0)
696 return -1;
697 else
698 return 0;
699 } /* }}} int mb_read_slave */
700
mb_read(user_data_t * user_data)701 static int mb_read(user_data_t *user_data) /* {{{ */
702 {
703 mb_host_t *host;
704 int success;
705 int status;
706
707 if ((user_data == NULL) || (user_data->data == NULL))
708 return EINVAL;
709
710 host = user_data->data;
711
712 success = 0;
713 for (size_t i = 0; i < host->slaves_num; i++) {
714 status = mb_read_slave(host, host->slaves + i);
715 if (status == 0)
716 success++;
717 }
718
719 if (success == 0)
720 return -1;
721 else
722 return 0;
723 } /* }}} int mb_read */
724
725 /* Free functions */
726
data_free_one(mb_data_t * data)727 static void data_free_one(mb_data_t *data) /* {{{ */
728 {
729 if (data == NULL)
730 return;
731
732 sfree(data->name);
733 sfree(data);
734 } /* }}} void data_free_one */
735
data_free_all(mb_data_t * data)736 static void data_free_all(mb_data_t *data) /* {{{ */
737 {
738 mb_data_t *next;
739
740 if (data == NULL)
741 return;
742
743 next = data->next;
744 data_free_one(data);
745
746 data_free_all(next);
747 } /* }}} void data_free_all */
748
slaves_free_all(mb_slave_t * slaves,size_t slaves_num)749 static void slaves_free_all(mb_slave_t *slaves, size_t slaves_num) /* {{{ */
750 {
751 if (slaves == NULL)
752 return;
753
754 for (size_t i = 0; i < slaves_num; i++)
755 data_free_all(slaves[i].collect);
756 sfree(slaves);
757 } /* }}} void slaves_free_all */
758
host_free(void * void_host)759 static void host_free(void *void_host) /* {{{ */
760 {
761 mb_host_t *host = void_host;
762
763 if (host == NULL)
764 return;
765
766 slaves_free_all(host->slaves, host->slaves_num);
767 sfree(host);
768 } /* }}} void host_free */
769
770 /* Config functions */
771
mb_config_add_data(oconfig_item_t * ci)772 static int mb_config_add_data(oconfig_item_t *ci) /* {{{ */
773 {
774 mb_data_t data = {0};
775 int status;
776
777 data.name = NULL;
778 data.register_type = REG_TYPE_UINT16;
779 data.next = NULL;
780 data.scale = 1;
781 data.shift = 0;
782
783 status = cf_util_get_string(ci, &data.name);
784 if (status != 0)
785 return status;
786
787 for (int i = 0; i < ci->children_num; i++) {
788 oconfig_item_t *child = ci->children + i;
789
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) {
802 char tmp[16];
803 status = cf_util_get_string_buffer(child, tmp, sizeof(tmp));
804 if (status != 0)
805 /* do nothing */;
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;
826 else {
827 ERROR("Modbus plugin: The register type \"%s\" is unknown.", tmp);
828 status = -1;
829 }
830 } else if (strcasecmp("RegisterCmd", child->key) == 0) {
831 #if LEGACY_LIBMODBUS
832 ERROR("Modbus plugin: RegisterCmd parameter can not be used "
833 "with your libmodbus version");
834 #else
835 char tmp[16];
836 status = cf_util_get_string_buffer(child, tmp, sizeof(tmp));
837 if (status != 0)
838 /* do nothing */;
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;
843 else {
844 ERROR("Modbus plugin: The modbus_register_type \"%s\" is unknown.",
845 tmp);
846 status = -1;
847 }
848 #endif
849 } else {
850 ERROR("Modbus plugin: Unknown configuration option: %s", child->key);
851 status = -1;
852 }
853
854 if (status != 0)
855 break;
856 } /* for (i = 0; i < ci->children_num; i++) */
857
858 assert(data.name != NULL);
859 if (data.type[0] == 0) {
860 ERROR("Modbus plugin: Data block \"%s\": No type has been specified.",
861 data.name);
862 status = -1;
863 }
864
865 if (status == 0)
866 data_copy(&data_definitions, &data);
867
868 sfree(data.name);
869
870 return status;
871 } /* }}} int mb_config_add_data */
872
mb_config_set_host_address(mb_host_t * host,const char * address)873 static int mb_config_set_host_address(mb_host_t *host, /* {{{ */
874 const char *address) {
875 struct addrinfo *ai_list;
876 int status;
877
878 if ((host == NULL) || (address == NULL))
879 return EINVAL;
880
881 struct addrinfo ai_hints = {
882 /* XXX: libmodbus can only handle IPv4 addresses. */
883 .ai_family = AF_INET,
884 .ai_flags = AI_ADDRCONFIG};
885
886 status = getaddrinfo(address, /* service = */ NULL, &ai_hints, &ai_list);
887 if (status != 0) {
888 ERROR("Modbus plugin: getaddrinfo failed: %s",
889 (status == EAI_SYSTEM) ? STRERRNO : gai_strerror(status));
890 return status;
891 }
892
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,
896 sizeof(host->node),
897 /* service = */ NULL, /* length = */ 0,
898 /* flags = */ NI_NUMERICHOST);
899 if (status == 0)
900 break;
901 } /* for (ai_ptr) */
902
903 freeaddrinfo(ai_list);
904
905 if (status != 0)
906 ERROR("Modbus plugin: Unable to translate node name: \"%s\"", address);
907 else /* if (status == 0) */
908 {
909 DEBUG("Modbus plugin: mb_config_set_host_address: %s -> %s", address,
910 host->node);
911 }
912
913 return status;
914 } /* }}} int mb_config_set_host_address */
915
mb_config_add_slave(mb_host_t * host,oconfig_item_t * ci)916 static int mb_config_add_slave(mb_host_t *host, oconfig_item_t *ci) /* {{{ */
917 {
918 mb_slave_t *slave;
919 int status;
920
921 if ((host == NULL) || (ci == NULL))
922 return EINVAL;
923
924 slave = realloc(host->slaves, sizeof(*slave) * (host->slaves_num + 1));
925 if (slave == NULL)
926 return ENOMEM;
927 host->slaves = slave;
928 slave = host->slaves + host->slaves_num;
929 memset(slave, 0, sizeof(*slave));
930 slave->collect = NULL;
931
932 status = cf_util_get_int(ci, &slave->id);
933 if (status != 0)
934 return status;
935
936 for (int i = 0; i < ci->children_num; i++) {
937 oconfig_item_t *child = ci->children + i;
938
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) {
943 char buffer[1024];
944 status = cf_util_get_string_buffer(child, buffer, sizeof(buffer));
945 if (status == 0)
946 data_copy_by_name(&slave->collect, data_definitions, buffer);
947 status = 0; /* continue after failure. */
948 } else {
949 ERROR("Modbus plugin: Unknown configuration option: %s", child->key);
950 status = -1;
951 }
952
953 if (status != 0)
954 break;
955 }
956
957 if ((status == 0) && (slave->collect == NULL))
958 status = EINVAL;
959
960 if (slave->id < 0)
961 status = EINVAL;
962
963 if (status == 0)
964 host->slaves_num++;
965 else /* if (status != 0) */
966 data_free_all(slave->collect);
967
968 return status;
969 } /* }}} int mb_config_add_slave */
970
mb_config_add_host(oconfig_item_t * ci)971 static int mb_config_add_host(oconfig_item_t *ci) /* {{{ */
972 {
973 cdtime_t interval = 0;
974 mb_host_t *host;
975 int status;
976
977 host = calloc(1, sizeof(*host));
978 if (host == NULL)
979 return ENOMEM;
980 host->slaves = NULL;
981
982 status = cf_util_get_string_buffer(ci, host->host, sizeof(host->host));
983 if (status != 0) {
984 sfree(host);
985 return status;
986 }
987 if (host->host[0] == 0) {
988 sfree(host);
989 return EINVAL;
990 }
991
992 for (int i = 0; i < ci->children_num; i++) {
993 oconfig_item_t *child = ci->children + i;
994 status = 0;
995
996 if (strcasecmp("Address", child->key) == 0) {
997 char buffer[NI_MAXHOST];
998 status = cf_util_get_string_buffer(child, buffer, sizeof(buffer));
999 if (status == 0)
1000 status = mb_config_set_host_address(host, buffer);
1001 if (status == 0)
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)
1006 status = -1;
1007 } else if (strcasecmp("Device", child->key) == 0) {
1008 status = cf_util_get_string_buffer(child, host->node, sizeof(host->node));
1009 if (status == 0) {
1010 host->conntype = MBCONN_RTU;
1011 host->uarttype = UARTTYPE_RS232;
1012 }
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));
1019 if (status != 0)
1020 break;
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;
1027 else {
1028 ERROR("Modbus plugin: The UARTType \"%s\" is unknown.", buffer);
1029 status = -1;
1030 break;
1031 }
1032 #else
1033 ERROR("Modbus plugin: Option `UARTType' not supported. Please "
1034 "upgrade libmodbus to at least 2.9.4");
1035 return -1;
1036 #endif
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);
1042 else {
1043 ERROR("Modbus plugin: Unknown configuration option: %s", child->key);
1044 status = -1;
1045 }
1046
1047 if (status != 0)
1048 break;
1049 } /* for (i = 0; i < ci->children_num; i++) */
1050
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 "
1054 "specified.",
1055 host->host);
1056 status = -1;
1057 }
1058 if (host->conntype == MBCONN_RTU && !host->baudrate) {
1059 ERROR("Modbus plugin: Data block \"%s\": No serial baudrate has been "
1060 "specified.",
1061 host->host);
1062 status = -1;
1063 }
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 "
1067 "options.",
1068 host->host);
1069 status = -1;
1070 }
1071
1072 if (status == 0) {
1073 char name[1024];
1074
1075 ssnprintf(name, sizeof(name), "modbus-%s", host->host);
1076
1077 plugin_register_complex_read(/* group = */ NULL, name,
1078 /* callback = */ mb_read,
1079 /* interval = */ interval,
1080 &(user_data_t){
1081 .data = host,
1082 .free_func = host_free,
1083 });
1084 } else {
1085 host_free(host);
1086 }
1087
1088 return status;
1089 } /* }}} int mb_config_add_host */
1090
mb_config(oconfig_item_t * ci)1091 static int mb_config(oconfig_item_t *ci) /* {{{ */
1092 {
1093 if (ci == NULL)
1094 return EINVAL;
1095
1096 for (int i = 0; i < ci->children_num; i++) {
1097 oconfig_item_t *child = ci->children + i;
1098
1099 if (strcasecmp("Data", child->key) == 0)
1100 mb_config_add_data(child);
1101 else if (strcasecmp("Host", child->key) == 0)
1102 mb_config_add_host(child);
1103 else
1104 ERROR("Modbus plugin: Unknown configuration option: %s", child->key);
1105 }
1106
1107 return 0;
1108 } /* }}} int mb_config */
1109
1110 /* ========= */
1111
mb_shutdown(void)1112 static int mb_shutdown(void) /* {{{ */
1113 {
1114 data_free_all(data_definitions);
1115 data_definitions = NULL;
1116
1117 return 0;
1118 } /* }}} int mb_shutdown */
1119
module_register(void)1120 void module_register(void) {
1121 plugin_register_complex_config("modbus", mb_config);
1122 plugin_register_shutdown("modbus", mb_shutdown);
1123 } /* void module_register */
1124