+/* Read functions */
+
+static int mb_submit (mb_host_t *host, mb_slave_t *slave, /* {{{ */
+ mb_data_t *data, value_t value)
+{
+ value_list_t vl = VALUE_LIST_INIT;
+
+ if ((host == NULL) || (slave == NULL) || (data == NULL))
+ return (EINVAL);
+
+ if (host->interval <= 0)
+ host->interval = interval_g;
+
+ if (slave->instance[0] == 0)
+ ssnprintf (slave->instance, sizeof (slave->instance), "slave_%i",
+ slave->id);
+
+ vl.values = &value;
+ vl.values_len = 1;
+ vl.interval = host->interval;
+ sstrncpy (vl.host, host->host, sizeof (vl.host));
+ sstrncpy (vl.plugin, "modbus", sizeof (vl.plugin));
+ sstrncpy (vl.plugin_instance, slave->instance, sizeof (vl.plugin_instance));
+ sstrncpy (vl.type, data->type, sizeof (vl.type));
+ sstrncpy (vl.type_instance, data->instance, sizeof (vl.type_instance));
+
+ return (plugin_dispatch_values (&vl));
+} /* }}} int mb_submit */
+
+static float mb_register_to_float (uint16_t hi, uint16_t lo) /* {{{ */
+{
+ union
+ {
+ uint8_t b[4];
+ float f;
+ } conv;
+
+#if BYTE_ORDER == LITTLE_ENDIAN
+ /* little endian */
+ conv.b[0] = lo & 0x00ff;
+ conv.b[1] = (lo >> 8) & 0x00ff;
+ conv.b[2] = hi & 0x00ff;
+ conv.b[3] = (hi >> 8) & 0x00ff;
+#else
+ conv.b[3] = lo & 0x00ff;
+ conv.b[2] = (lo >> 8) & 0x00ff;
+ conv.b[1] = hi & 0x00ff;
+ conv.b[0] = (hi >> 8) & 0x00ff;
+#endif
+
+ return (conv.f);
+} /* }}} float mb_register_to_float */
+
+#if LEGACY_LIBMODBUS
+/* Version 2.0.3 */
+static int mb_init_connection (mb_host_t *host) /* {{{ */
+{
+ int status;
+
+ if (host == NULL)
+ return (EINVAL);
+
+ if (host->is_connected)
+ return (0);
+
+ /* Only reconnect once per interval. */
+ if (host->have_reconnected)
+ return (-1);
+
+ modbus_set_debug (&host->connection, 1);
+
+ /* We'll do the error handling ourselves. */
+ modbus_set_error_handling (&host->connection, NOP_ON_ERROR);
+
+ if ((host->port < 1) || (host->port > 65535))
+ host->port = MODBUS_TCP_DEFAULT_PORT;
+
+ DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
+ host->node, host->port);
+
+ modbus_init_tcp (&host->connection,
+ /* host = */ host->node,
+ /* port = */ host->port);
+
+ status = modbus_connect (&host->connection);
+ if (status != 0)
+ {
+ ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
+ host->node, host->port, status);
+ return (status);
+ }
+
+ host->is_connected = 1;
+ host->have_reconnected = 1;
+ return (0);
+} /* }}} int mb_init_connection */
+/* #endif LEGACY_LIBMODBUS */
+
+#else /* if !LEGACY_LIBMODBUS */
+/* Version 2.9.2 */
+static int mb_init_connection (mb_host_t *host) /* {{{ */
+{
+ int status;
+
+ if (host == NULL)
+ return (EINVAL);
+
+ if (host->connection != NULL)
+ return (0);
+
+ /* Only reconnect once per interval. */
+ if (host->have_reconnected)
+ return (-1);
+
+ if ((host->port < 1) || (host->port > 65535))
+ host->port = MODBUS_TCP_DEFAULT_PORT;
+
+ DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
+ host->node, host->port);
+
+ host->connection = modbus_new_tcp (host->node, host->port);
+ if (host->connection == NULL)
+ {
+ host->have_reconnected = 1;
+ ERROR ("Modbus plugin: Creating new Modbus/TCP object failed.");
+ return (-1);
+ }
+
+ modbus_set_debug (host->connection, 1);
+
+ /* We'll do the error handling ourselves. */
+ modbus_set_error_recovery (host->connection, 0);
+
+ status = modbus_connect (host->connection);
+ if (status != 0)
+ {
+ ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
+ host->node, host->port, status);
+ modbus_free (host->connection);
+ host->connection = NULL;
+ return (status);
+ }
+
+ host->have_reconnected = 1;
+ return (0);
+} /* }}} int mb_init_connection */
+#endif /* !LEGACY_LIBMODBUS */
+
+#define CAST_TO_VALUE_T(ds,vt,raw) do { \
+ if ((ds)->ds[0].type == DS_TYPE_COUNTER) \
+ (vt).counter = (counter_t) (raw); \
+ else if ((ds)->ds[0].type == DS_TYPE_GAUGE) \
+ (vt).gauge = (gauge_t) (raw); \
+ else if ((ds)->ds[0].type == DS_TYPE_DERIVE) \
+ (vt).derive = (derive_t) (raw); \
+ else /* if (ds->ds[0].type == DS_TYPE_ABSOLUTE) */ \
+ (vt).absolute = (absolute_t) (raw); \
+} while (0)
+
+static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */
+ mb_data_t *data)
+{
+ uint16_t values[2];
+ int values_num;
+ const data_set_t *ds;
+ int status;
+ int i;
+
+ if ((host == NULL) || (slave == NULL) || (data == NULL))
+ return (EINVAL);
+
+ ds = plugin_get_ds (data->type);
+ if (ds == NULL)
+ {
+ ERROR ("Modbus plugin: Type \"%s\" is not defined.", data->type);
+ return (-1);
+ }
+
+ if (ds->ds_num != 1)
+ {
+ ERROR ("Modbus plugin: The type \"%s\" has %i data sources. "
+ "I can only handle data sets with only one data source.",
+ data->type, ds->ds_num);
+ return (-1);
+ }
+
+ if ((ds->ds[0].type != DS_TYPE_GAUGE)
+ && (data->register_type != REG_TYPE_UINT32))
+ {
+ NOTICE ("Modbus plugin: The data source of type \"%s\" is %s, not gauge. "
+ "This will most likely result in problems, because the register type "
+ "is not UINT32.", data->type, DS_TYPE_TO_STRING (ds->ds[0].type));
+ }
+
+ memset (values, 0, sizeof (values));
+ if ((data->register_type == REG_TYPE_UINT32)
+ || (data->register_type == REG_TYPE_FLOAT))
+ values_num = 2;
+ else
+ values_num = 1;
+
+#if LEGACY_LIBMODBUS
+ /* Version 2.0.3: Pass the connection struct as a pointer and pass the slave
+ * id to each call of "read_holding_registers". */
+# define modbus_read_registers(ctx, addr, nb, dest) \
+ read_holding_registers (&(ctx), slave->id, (addr), (nb), (dest))
+#else /* if !LEGACY_LIBMODBUS */
+ /* Version 2.9.2: Set the slave id once before querying the registers. */
+ status = modbus_set_slave (host->connection, slave->id);
+ if (status != 0)
+ {
+ ERROR ("Modbus plugin: modbus_set_slave (%i) failed with status %i.",
+ slave->id, status);
+ return (-1);
+ }
+#endif
+
+ for (i = 0; i < 2; i++)
+ {
+ status = modbus_read_registers (host->connection,
+ /* start_addr = */ data->register_base,
+ /* num_registers = */ values_num, /* buffer = */ values);
+ if (status > 0)
+ break;
+
+ if (host->is_connected)
+ {
+#if LEGACY_LIBMODBUS
+ modbus_close (&host->connection);
+ host->is_connected = 0;
+#else
+ modbus_close (host->connection);
+ modbus_free (host->connection);
+ host->connection = NULL;
+#endif
+ }
+
+ /* If we already tried reconnecting this round, give up. */
+ if (host->have_reconnected)
+ {
+ ERROR ("Modbus plugin: modbus_read_registers (%s) failed. "
+ "Reconnecting has already been tried. Giving up.", host->host);
+ return (-1);
+ }
+
+ /* Maybe the device closed the connection during the waiting interval.
+ * Try re-establishing the connection. */
+ status = mb_init_connection (host);
+ if (status != 0)
+ {
+ ERROR ("Modbus plugin: modbus_read_registers (%s) failed. "
+ "While trying to reconnect, connecting to \"%s\" failed. "
+ "Giving up.",
+ host->host, host->node);
+ return (-1);
+ }
+
+ DEBUG ("Modbus plugin: Re-established connection to %s", host->host);
+
+ /* try again */
+ continue;
+ } /* for (i = 0, 1) */
+
+ DEBUG ("Modbus plugin: mb_read_data: Success! "
+ "modbus_read_registers returned with status %i.", status);
+
+ if (data->register_type == REG_TYPE_FLOAT)
+ {
+ float float_value;
+ value_t vt;
+
+ float_value = mb_register_to_float (values[0], values[1]);
+ DEBUG ("Modbus plugin: mb_read_data: "
+ "Returned float value is %g", (double) float_value);
+
+ CAST_TO_VALUE_T (ds, vt, float_value);
+ mb_submit (host, slave, data, vt);
+ }
+ else if (data->register_type == REG_TYPE_UINT32)
+ {
+ uint32_t v32;
+ value_t vt;
+
+ v32 = (values[0] << 16) | values[1];
+ DEBUG ("Modbus plugin: mb_read_data: "
+ "Returned uint32 value is %"PRIu32, v32);
+
+ CAST_TO_VALUE_T (ds, vt, v32);
+ mb_submit (host, slave, data, vt);
+ }
+ else /* if (data->register_type == REG_TYPE_UINT16) */
+ {
+ value_t vt;
+
+ DEBUG ("Modbus plugin: mb_read_data: "
+ "Returned uint16 value is %"PRIu16, values[0]);
+
+ CAST_TO_VALUE_T (ds, vt, values[0]);
+ mb_submit (host, slave, data, vt);
+ }
+
+ return (0);
+} /* }}} int mb_read_data */
+
+static int mb_read_slave (mb_host_t *host, mb_slave_t *slave) /* {{{ */
+{
+ mb_data_t *data;
+ int success;
+ int status;
+
+ if ((host == NULL) || (slave == NULL))
+ return (EINVAL);
+
+ success = 0;
+ for (data = slave->collect; data != NULL; data = data->next)
+ {
+ status = mb_read_data (host, slave, data);
+ if (status == 0)
+ success++;
+ }
+
+ if (success == 0)
+ return (-1);
+ else
+ return (0);
+} /* }}} int mb_read_slave */
+
+static int mb_read (user_data_t *user_data) /* {{{ */
+{
+ mb_host_t *host;
+ size_t i;
+ int success;
+ int status;
+
+ if ((user_data == NULL) || (user_data->data == NULL))
+ return (EINVAL);
+
+ host = user_data->data;
+
+ /* Clear the reconnect flag. */
+ host->have_reconnected = 0;
+
+ success = 0;
+ for (i = 0; i < host->slaves_num; i++)
+ {
+ status = mb_read_slave (host, host->slaves + i);
+ if (status == 0)
+ success++;
+ }
+
+ if (success == 0)
+ return (-1);
+ else
+ return (0);
+} /* }}} int mb_read */
+
+/* Free functions */
+
+static void data_free_one (mb_data_t *data) /* {{{ */
+{
+ if (data == NULL)
+ return;
+
+ sfree (data->name);
+ sfree (data);
+} /* }}} void data_free_one */
+
+static void data_free_all (mb_data_t *data) /* {{{ */
+{
+ mb_data_t *next;
+
+ if (data == NULL)
+ return;
+
+ next = data->next;
+ data_free_one (data);
+
+ data_free_all (next);
+} /* }}} void data_free_all */
+
+static void slaves_free_all (mb_slave_t *slaves, size_t slaves_num) /* {{{ */
+{
+ size_t i;
+
+ if (slaves == NULL)
+ return;
+
+ for (i = 0; i < slaves_num; i++)
+ data_free_all (slaves[i].collect);
+ sfree (slaves);
+} /* }}} void slaves_free_all */
+
+static void host_free (void *void_host) /* {{{ */
+{
+ mb_host_t *host = void_host;
+
+ if (host == NULL)
+ return;
+
+ slaves_free_all (host->slaves, host->slaves_num);
+ sfree (host);
+} /* }}} void host_free */
+
+/* Config functions */
+