modbus plugin: Set the interval when dispatching values.
[collectd.git] / src / modbus.c
1 /**
2  * collectd - src/modbus.c
3  * Copyright (C) 2010  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 General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian Forster <octo at noris.net>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26
27 #include <modbus/modbus.h>
28
29 /*
30  * <Data "data_name">
31  *   RegisterBase 1234
32  *   RegisterType float
33  *   Type gauge
34  *   Instance "..."
35  * </Data>
36  *
37  * <Host "name">
38  *   Address "addr"
39  *   Port "1234"
40  *   Interval 60
41  *
42  *   <Slave 1>
43  *     Instance "foobar" # optional
44  *     Collect "data_name"
45  *   </Slave>
46  * </Host>
47  */
48
49 /*
50  * Data structures
51  */
52 enum mb_register_type_e /* {{{ */
53 {
54   REG_TYPE_UINT16,
55   REG_TYPE_UINT32,
56   REG_TYPE_FLOAT
57 }; /* }}} */
58 typedef enum mb_register_type_e mb_register_type_t;
59
60 struct mb_data_s;
61 typedef struct mb_data_s mb_data_t;
62 struct mb_data_s /* {{{ */
63 {
64   char *name;
65   int register_base;
66   mb_register_type_t register_type;
67   char type[DATA_MAX_NAME_LEN];
68   char instance[DATA_MAX_NAME_LEN];
69
70   mb_data_t *next;
71 }; /* }}} */
72
73 struct mb_slave_s /* {{{ */
74 {
75   int id;
76   char instance[DATA_MAX_NAME_LEN];
77   mb_data_t *collect;
78 }; /* }}} */
79 typedef struct mb_slave_s mb_slave_t;
80
81 struct mb_host_s /* {{{ */
82 {
83   char host[DATA_MAX_NAME_LEN];
84   char node[NI_MAXHOST];
85   /* char service[NI_MAXSERV]; */
86   int port;
87   int interval;
88
89   mb_slave_t *slaves;
90   size_t slaves_num;
91
92   modbus_param_t connection;
93   _Bool is_connected;
94   _Bool have_reconnected;
95 }; /* }}} */
96 typedef struct mb_host_s mb_host_t;
97
98 struct mb_data_group_s;
99 typedef struct mb_data_group_s mb_data_group_t;
100 struct mb_data_group_s /* {{{ */
101 {
102   mb_data_t *registers;
103   size_t registers_num;
104
105   mb_data_group_t *next;
106 }; /* }}} */
107
108 /*
109  * Global variables
110  */
111 static mb_data_t *data_definitions = NULL;
112
113 /*
114  * Functions
115  */
116 static mb_data_t *data_get_by_name (mb_data_t *src, /* {{{ */
117     const char *name)
118 {
119   mb_data_t *ptr;
120
121   if (name == NULL)
122     return (NULL);
123
124   for (ptr = src; ptr != NULL; ptr = ptr->next)
125     if (strcasecmp (ptr->name, name) == 0)
126       return (ptr);
127
128   return (NULL);
129 } /* }}} mb_data_t *data_get_by_name */
130
131 static int data_append (mb_data_t **dst, mb_data_t *src) /* {{{ */
132 {
133   mb_data_t *ptr;
134
135   if ((dst == NULL) || (src == NULL))
136     return (EINVAL);
137
138   ptr = *dst;
139
140   if (ptr == NULL)
141   {
142     *dst = src;
143     return (0);
144   }
145
146   while (ptr->next != NULL)
147     ptr = ptr->next;
148
149   ptr->next = src;
150
151   return (0);
152 } /* }}} int data_append */
153
154 /* Copy a single mb_data_t and append it to another list. */
155 static int data_copy (mb_data_t **dst, const mb_data_t *src) /* {{{ */
156 {
157   mb_data_t *tmp;
158   int status;
159
160   if ((dst == NULL) || (src == NULL))
161     return (EINVAL);
162
163   tmp = malloc (sizeof (*tmp));
164   if (tmp == NULL)
165     return (ENOMEM);
166   memcpy (tmp, src, sizeof (*tmp));
167   tmp->name = NULL;
168   tmp->next = NULL;
169
170   tmp->name = strdup (src->name);
171   if (tmp->name == NULL)
172   {
173     sfree (tmp);
174     return (ENOMEM);
175   }
176
177   status = data_append (dst, tmp);
178   if (status != 0)
179   {
180     sfree (tmp->name);
181     sfree (tmp);
182     return (status);
183   }
184
185   return (0);
186 } /* }}} int data_copy */
187
188 /* Lookup a single mb_data_t instance, copy it and append the copy to another
189  * list. */
190 static int data_copy_by_name (mb_data_t **dst, mb_data_t *src, /* {{{ */
191     const char *name)
192 {
193   mb_data_t *ptr;
194
195   if ((dst == NULL) || (src == NULL) || (name == NULL))
196     return (EINVAL);
197
198   ptr = data_get_by_name (src, name);
199   if (ptr == NULL)
200     return (ENOENT);
201
202   return (data_copy (dst, ptr));
203 } /* }}} int data_copy_by_name */
204
205 /* Read functions */
206
207 static int mb_submit (mb_host_t *host, mb_slave_t *slave, /* {{{ */
208     mb_data_t *data, value_t value)
209 {
210   value_list_t vl = VALUE_LIST_INIT;
211
212   if ((host == NULL) || (slave == NULL) || (data == NULL))
213     return (EINVAL);
214
215   if (host->interval <= 0)
216     host->interval = interval_g;
217
218   if (slave->instance[0] == 0)
219     ssnprintf (slave->instance, sizeof (slave->instance), "slave_%i",
220         slave->id);
221
222   vl.values = &value;
223   vl.values_len = 1;
224   vl.interval = host->interval;
225   sstrncpy (vl.host, host->host, sizeof (vl.host));
226   sstrncpy (vl.plugin, "modbus", sizeof (vl.plugin));
227   sstrncpy (vl.plugin_instance, slave->instance, sizeof (vl.plugin_instance));
228   sstrncpy (vl.type, data->type, sizeof (vl.type));
229   sstrncpy (vl.type_instance, data->instance, sizeof (vl.type_instance));
230
231   return (plugin_dispatch_values (&vl));
232 } /* }}} int mb_submit */
233
234 static float mb_register_to_float (uint16_t hi, uint16_t lo) /* {{{ */
235 {
236   union
237   {
238     uint8_t b[4];
239     float f;
240   } conv;
241
242 #if BYTE_ORDER == LITTLE_ENDIAN
243   /* little endian */
244   conv.b[0] = lo & 0x00ff;
245   conv.b[1] = (lo >> 8) & 0x00ff;
246   conv.b[2] = hi & 0x00ff;
247   conv.b[3] = (hi >> 8) & 0x00ff;
248 #else
249   conv.b[3] = lo & 0x00ff;
250   conv.b[2] = (lo >> 8) & 0x00ff;
251   conv.b[1] = hi & 0x00ff;
252   conv.b[0] = (hi >> 8) & 0x00ff;
253 #endif
254
255   return (conv.f);
256 } /* }}} float mb_register_to_float */
257
258 static int mb_init_connection (mb_host_t *host) /* {{{ */
259 {
260   int status;
261
262   if (host == NULL)
263     return (EINVAL);
264
265   if (host->is_connected)
266     return (0);
267
268   /* Only reconnect once per interval. */
269   if (host->have_reconnected)
270     return (-1);
271
272   modbus_set_debug (&host->connection, 1);
273
274   /* We'll do the error handling ourselves. */
275   modbus_set_error_handling (&host->connection, NOP_ON_ERROR);
276
277   if ((host->port < 1) || (host->port > 65535))
278     host->port = MODBUS_TCP_DEFAULT_PORT;
279
280   DEBUG ("Modbus plugin: Trying to connect to \"%s\", port %i.",
281       host->node, host->port);
282
283   modbus_init_tcp (&host->connection,
284       /* host = */ host->node,
285       /* port = */ host->port);
286
287   status = modbus_connect (&host->connection);
288   if (status != 0)
289   {
290     ERROR ("Modbus plugin: modbus_connect (%s, %i) failed with status %i.",
291         host->node, host->port, status);
292     return (status);
293   }
294
295   host->is_connected = 1;
296   host->have_reconnected = 1;
297   return (0);
298 } /* }}} int mb_init_connection */
299
300 #define CAST_TO_VALUE_T(ds,vt,raw) do { \
301   if ((ds)->ds[0].type == DS_TYPE_COUNTER) \
302     (vt).counter = (counter_t) (raw); \
303   else if ((ds)->ds[0].type == DS_TYPE_GAUGE) \
304     (vt).gauge = (gauge_t) (raw); \
305   else if ((ds)->ds[0].type == DS_TYPE_DERIVE) \
306     (vt).derive = (derive_t) (raw); \
307   else /* if (ds->ds[0].type == DS_TYPE_ABSOLUTE) */ \
308     (vt).absolute = (absolute_t) (raw); \
309 } while (0)
310
311 static int mb_read_data (mb_host_t *host, mb_slave_t *slave, /* {{{ */
312     mb_data_t *data)
313 {
314   uint16_t values[2];
315   int values_num;
316   const data_set_t *ds;
317   int status;
318   int i;
319
320   if ((host == NULL) || (slave == NULL) || (data == NULL))
321     return (EINVAL);
322
323   ds = plugin_get_ds (data->type);
324   if (ds == NULL)
325   {
326     ERROR ("Modbus plugin: Type \"%s\" is not defined.", data->type);
327     return (-1);
328   }
329
330   if (ds->ds_num != 1)
331   {
332     ERROR ("Modbus plugin: The type \"%s\" has %i data sources. "
333         "I can only handle data sets with only one data source.",
334         data->type, ds->ds_num);
335     return (-1);
336   }
337
338   if ((ds->ds[0].type != DS_TYPE_GAUGE)
339       && (data->register_type != REG_TYPE_UINT32))
340   {
341     NOTICE ("Modbus plugin: The data source of type \"%s\" is %s, not gauge. "
342         "This will most likely result in problems, because the register type "
343         "is not UINT32.", data->type, DS_TYPE_TO_STRING (ds->ds[0].type));
344   }
345
346   memset (values, 0, sizeof (values));
347   if ((data->register_type == REG_TYPE_UINT32)
348       || (data->register_type == REG_TYPE_FLOAT))
349     values_num = 2;
350   else
351     values_num = 1;
352
353   for (i = 0; i < 2; i++)
354   {
355     status = read_holding_registers (&host->connection,
356         /* slave = */ slave->id, /* start_addr = */ data->register_base,
357         /* num_registers = */ values_num, /* buffer = */ values);
358     if (status > 0)
359       break;
360
361     if (host->is_connected)
362       modbus_close (&host->connection);
363     host->is_connected = 0;
364
365     /* If we already tried reconnecting this round, give up. */
366     if (host->have_reconnected)
367     {
368       ERROR ("Modbus plugin: read_holding_registers (%s) failed. "
369           "Reconnecting has already been tried. Giving up.", host->host);
370       return (-1);
371     }
372
373     /* Maybe the device closed the connection during the waiting interval.
374      * Try re-establishing the connection. */
375     status = mb_init_connection (host);
376     if (status != 0)
377     {
378       ERROR ("Modbus plugin: read_holding_registers (%s) failed. "
379           "While trying to reconnect, connecting to \"%s\" failed. "
380           "Giving up.",
381           host->host, host->node);
382       return (-1);
383     }
384
385     DEBUG ("Modbus plugin: Re-established connection to %s", host->host);
386
387     /* try again */
388     continue;
389   } /* for (i = 0, 1) */
390
391   DEBUG ("Modbus plugin: mb_read_data: Success! "
392       "read_holding_registers returned with status %i.", status);
393
394   if (data->register_type == REG_TYPE_FLOAT)
395   {
396     float float_value;
397     value_t vt;
398
399     float_value = mb_register_to_float (values[0], values[1]);
400     DEBUG ("Modbus plugin: mb_read_data: "
401         "Returned float value is %g", (double) float_value);
402
403     CAST_TO_VALUE_T (ds, vt, float_value);
404     mb_submit (host, slave, data, vt);
405   }
406   else if (data->register_type == REG_TYPE_UINT32)
407   {
408     uint32_t v32;
409     value_t vt;
410
411     v32 = (values[0] << 16) | values[1];
412     DEBUG ("Modbus plugin: mb_read_data: "
413         "Returned uint32 value is %"PRIu32, v32);
414
415     CAST_TO_VALUE_T (ds, vt, v32);
416     mb_submit (host, slave, data, vt);
417   }
418   else /* if (data->register_type == REG_TYPE_UINT16) */
419   {
420     value_t vt;
421
422     DEBUG ("Modbus plugin: mb_read_data: "
423         "Returned uint16 value is %"PRIu16, values[0]);
424
425     CAST_TO_VALUE_T (ds, vt, values[0]);
426     mb_submit (host, slave, data, vt);
427   }
428
429   return (0);
430 } /* }}} int mb_read_data */
431
432 static int mb_read_slave (mb_host_t *host, mb_slave_t *slave) /* {{{ */
433 {
434   mb_data_t *data;
435   int success;
436   int status;
437
438   if ((host == NULL) || (slave == NULL))
439     return (EINVAL);
440
441   success = 0;
442   for (data = slave->collect; data != NULL; data = data->next)
443   {
444     status = mb_read_data (host, slave, data);
445     if (status == 0)
446       success++;
447   }
448
449   if (success == 0)
450     return (-1);
451   else
452     return (0);
453 } /* }}} int mb_read_slave */
454
455 static int mb_read (user_data_t *user_data) /* {{{ */
456 {
457   mb_host_t *host;
458   size_t i;
459   int success;
460   int status;
461
462   if ((user_data == NULL) || (user_data->data == NULL))
463     return (EINVAL);
464
465   host = user_data->data;
466
467   /* Clear the reconnect flag. */
468   host->have_reconnected = 0;
469
470   success = 0;
471   for (i = 0; i < host->slaves_num; i++)
472   {
473     status = mb_read_slave (host, host->slaves + i);
474     if (status == 0)
475       success++;
476   }
477
478   if (success == 0)
479     return (-1);
480   else
481     return (0);
482 } /* }}} int mb_read */
483
484 /* Free functions */
485
486 static void data_free_one (mb_data_t *data) /* {{{ */
487 {
488   if (data == NULL)
489     return;
490
491   sfree (data->name);
492   sfree (data);
493 } /* }}} void data_free_one */
494
495 static void data_free_all (mb_data_t *data) /* {{{ */
496 {
497   mb_data_t *next;
498
499   if (data == NULL)
500     return;
501
502   next = data->next;
503   data_free_one (data);
504
505   data_free_all (next);
506 } /* }}} void data_free_all */
507
508 static void slaves_free_all (mb_slave_t *slaves, size_t slaves_num) /* {{{ */
509 {
510   size_t i;
511
512   if (slaves == NULL)
513     return;
514
515   for (i = 0; i < slaves_num; i++)
516     data_free_all (slaves[i].collect);
517   sfree (slaves);
518 } /* }}} void slaves_free_all */
519
520 static void host_free (void *void_host) /* {{{ */
521 {
522   mb_host_t *host = void_host;
523
524   if (host == NULL)
525     return;
526
527   slaves_free_all (host->slaves, host->slaves_num);
528   sfree (host);
529 } /* }}} void host_free */
530
531 /* Config functions */
532
533 static int mb_config_add_data (oconfig_item_t *ci) /* {{{ */
534 {
535   mb_data_t data;
536   int status;
537   int i;
538
539   memset (&data, 0, sizeof (data));
540   data.name = NULL;
541   data.register_type = REG_TYPE_UINT16;
542   data.next = NULL;
543
544   status = cf_util_get_string (ci, &data.name);
545   if (status != 0)
546     return (status);
547
548   for (i = 0; i < ci->children_num; i++)
549   {
550     oconfig_item_t *child = ci->children + i;
551     status = 0;
552
553     if (strcasecmp ("Type", child->key) == 0)
554       status = cf_util_get_string_buffer (child,
555           data.type, sizeof (data.type));
556     else if (strcasecmp ("Instance", child->key) == 0)
557       status = cf_util_get_string_buffer (child,
558           data.instance, sizeof (data.instance));
559     else if (strcasecmp ("RegisterBase", child->key) == 0)
560       status = cf_util_get_int (child, &data.register_base);
561     else if (strcasecmp ("RegisterType", child->key) == 0)
562     {
563       char tmp[16];
564       status = cf_util_get_string_buffer (child, tmp, sizeof (tmp));
565       if (status != 0)
566         /* do nothing */;
567       else if (strcasecmp ("Uint16", tmp) == 0)
568         data.register_type = REG_TYPE_UINT16;
569       else if (strcasecmp ("Uint32", tmp) == 0)
570         data.register_type = REG_TYPE_UINT32;
571       else if (strcasecmp ("Float", tmp) == 0)
572         data.register_type = REG_TYPE_FLOAT;
573       else
574       {
575         ERROR ("Modbus plugin: The register type \"%s\" is unknown.", tmp);
576         status = -1;
577       }
578     }
579     else
580     {
581       ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
582       status = -1;
583     }
584
585     if (status != 0)
586       break;
587   } /* for (i = 0; i < ci->children_num; i++) */
588
589   assert (data.name != NULL);
590   if (data.type[0] == 0)
591   {
592     ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
593         data.name);
594     status = -1;
595   }
596
597   if (status == 0)
598     data_copy (&data_definitions, &data);
599
600   sfree (data.name);
601
602   return (status);
603 } /* }}} int mb_config_add_data */
604
605 static int mb_config_set_host_address (mb_host_t *host, /* {{{ */
606     const char *address)
607 {
608   struct addrinfo *ai_list;
609   struct addrinfo *ai_ptr;
610   struct addrinfo  ai_hints;
611   int status;
612
613   if ((host == NULL) || (address == NULL))
614     return (EINVAL);
615
616   memset (&ai_hints, 0, sizeof (ai_hints));
617 #if AI_ADDRCONFIG
618   ai_hints.ai_flags |= AI_ADDRCONFIG;
619 #endif
620   /* XXX: libmodbus can only handle IPv4 addresses. */
621   ai_hints.ai_family = AF_INET;
622   ai_hints.ai_addr = NULL;
623   ai_hints.ai_canonname = NULL;
624   ai_hints.ai_next = NULL;
625
626   ai_list = NULL;
627   status = getaddrinfo (address, /* service = */ NULL,
628       &ai_hints, &ai_list);
629   if (status != 0)
630   {
631     char errbuf[1024];
632     ERROR ("Modbus plugin: getaddrinfo failed: %s",
633         (status == EAI_SYSTEM)
634         ? sstrerror (errno, errbuf, sizeof (errbuf))
635         : gai_strerror (status));
636     return (status);
637   }
638
639   for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
640   {
641     status = getnameinfo (ai_ptr->ai_addr, ai_ptr->ai_addrlen,
642         host->node, sizeof (host->node),
643         /* service = */ NULL, /* length = */ 0,
644         /* flags = */ NI_NUMERICHOST);
645     if (status == 0)
646       break;
647   } /* for (ai_ptr) */
648
649   freeaddrinfo (ai_list);
650
651   if (status != 0)
652     ERROR ("Modbus plugin: Unable to translate node name: \"%s\"", address);
653   else /* if (status == 0) */
654   {
655     DEBUG ("Modbus plugin: mb_config_set_host_address: %s -> %s",
656         address, host->node);
657   }
658
659   return (status);
660 } /* }}} int mb_config_set_host_address */
661
662 static int mb_config_add_slave (mb_host_t *host, oconfig_item_t *ci) /* {{{ */
663 {
664   mb_slave_t *slave;
665   int status;
666   int i;
667
668   if ((host == NULL) || (ci == NULL))
669     return (EINVAL);
670
671   slave = realloc (host->slaves, sizeof (*slave) * (host->slaves_num + 1));
672   if (slave == NULL)
673     return (ENOMEM);
674   host->slaves = slave;
675   slave = host->slaves + host->slaves_num;
676   memset (slave, 0, sizeof (*slave));
677   slave->collect = NULL;
678
679   status = cf_util_get_int (ci, &slave->id);
680   if (status != 0)
681     return (status);
682
683   for (i = 0; i < ci->children_num; i++)
684   {
685     oconfig_item_t *child = ci->children + i;
686     status = 0;
687
688     if (strcasecmp ("Instance", child->key) == 0)
689       status = cf_util_get_string_buffer (child,
690           slave->instance, sizeof (slave->instance));
691     else if (strcasecmp ("Collect", child->key) == 0)
692     {
693       char buffer[1024];
694       status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
695       if (status == 0)
696         data_copy_by_name (&slave->collect, data_definitions, buffer);
697       status = 0; /* continue after failure. */
698     }
699     else
700     {
701       ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
702       status = -1;
703     }
704
705     if (status != 0)
706       break;
707   }
708
709   if ((status == 0) && (slave->collect == NULL))
710     status = EINVAL;
711
712   if (slave->id < 0)
713     status = EINVAL;
714
715   if (status == 0)
716     host->slaves_num++;
717   else /* if (status != 0) */
718     data_free_all (slave->collect);
719
720   return (status);
721 } /* }}} int mb_config_add_slave */
722
723 static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */
724 {
725   mb_host_t *host;
726   int status;
727   int i;
728
729   host = malloc (sizeof (*host));
730   if (host == NULL)
731     return (ENOMEM);
732   memset (host, 0, sizeof (*host));
733   host->slaves = NULL;
734
735   status = cf_util_get_string_buffer (ci, host->host, sizeof (host->host));
736   if (status != 0)
737     return (status);
738   if (host->host[0] == 0)
739     return (EINVAL);
740
741   for (i = 0; i < ci->children_num; i++)
742   {
743     oconfig_item_t *child = ci->children + i;
744     status = 0;
745
746     if (strcasecmp ("Address", child->key) == 0)
747     {
748       char buffer[NI_MAXHOST];
749       status = cf_util_get_string_buffer (child, buffer, sizeof (buffer));
750       if (status == 0)
751         status = mb_config_set_host_address (host, buffer);
752     }
753     else if (strcasecmp ("Port", child->key) == 0)
754     {
755       host->port = cf_util_get_port_number (child);
756       if (host->port <= 0)
757         status = -1;
758     }
759     else if (strcasecmp ("Interval", child->key) == 0)
760       status = cf_util_get_int (child, &host->interval);
761     else if (strcasecmp ("Slave", child->key) == 0)
762       /* Don't set status: Gracefully continue if a slave fails. */
763       mb_config_add_slave (host, child);
764     else
765     {
766       ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
767       status = -1;
768     }
769
770     if (status != 0)
771       break;
772   } /* for (i = 0; i < ci->children_num; i++) */
773
774   assert (host->host[0] != 0);
775   if (host->host[0] == 0)
776   {
777     ERROR ("Modbus plugin: Data block \"%s\": No type has been specified.",
778         host->host);
779     status = -1;
780   }
781
782   if (status == 0)
783   {
784     user_data_t ud;
785     char name[1024];
786     struct timespec interval;
787
788     ud.data = host;
789     ud.free_func = host_free;
790
791     ssnprintf (name, sizeof (name), "modbus-%s", host->host);
792
793     interval.tv_nsec = 0;
794     if (host->interval > 0)
795       interval.tv_sec = host->interval;
796     else
797       interval.tv_sec = 0;
798
799     plugin_register_complex_read (name, mb_read,
800         (interval.tv_sec > 0) ? &interval : NULL,
801         &ud);
802   }
803   else
804   {
805     host_free (host);
806   }
807
808   return (status);
809 } /* }}} int mb_config_add_host */
810
811 static int mb_config (oconfig_item_t *ci) /* {{{ */
812 {
813   int i;
814
815   if (ci == NULL)
816     return (EINVAL);
817
818   for (i = 0; i < ci->children_num; i++)
819   {
820     oconfig_item_t *child = ci->children + i;
821
822     if (strcasecmp ("Data", child->key) == 0)
823       mb_config_add_data (child);
824     else if (strcasecmp ("Host", child->key) == 0)
825       mb_config_add_host (child);
826     else
827       ERROR ("Modbus plugin: Unknown configuration option: %s", child->key);
828   }
829
830   return (0);
831 } /* }}} int mb_config */
832
833 /* ========= */
834
835 #if 0
836 static int foo (void) /* {{{ */
837 {
838   int status;
839   uint16_t values[2];
840   int values_num;
841
842   if (dev == NULL)
843     return (EINVAL);
844
845   printf ("mb_read (addr = %i, float = %s);\n", register_addr,
846       is_float ? "true" : "false");
847
848   memset (values, 0, sizeof (values));
849   if (is_float)
850     values_num = 2;
851   else
852     values_num = 1;
853
854   status = read_holding_registers (dev->connection,
855       /* slave = */ 1, /* start_addr = */ register_addr,
856       /* num_registers = */ values_num, /* buffer = */ values);
857   printf ("read_coil_status returned with status %i\n", status);
858   if (status <= 0)
859     return (EAGAIN);
860
861   if (is_float)
862   {
863     float value = mb_register_to_float (values[0], values[1]);
864     printf ("read_coil_status returned value %g (hi %#"PRIx16", lo %#"PRIx16")\n",
865         value, values[0], values[1]);
866   }
867   else
868   {
869     printf ("read_coil_status returned value %"PRIu16"\n", values[0]);
870   }
871
872   return (0);
873 } /* }}} int foo */
874 #endif
875
876 static int mb_shutdown (void) /* {{{ */
877 {
878   data_free_all (data_definitions);
879   data_definitions = NULL;
880
881   return (0);
882 } /* }}} int mb_shutdown */
883
884 void module_register (void)
885 {
886   plugin_register_complex_config ("modbus", mb_config);
887   plugin_register_shutdown ("modbus", mb_shutdown);
888 } /* void module_register */
889
890 /* vim: set sw=2 sts=2 et fdm=marker : */