2 * collectd - src/serial.c
3 * Copyright (C) 2005,2006 David Bacher
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; either version 2 of the License, or (at your
8 * option) any later version.
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 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * David Bacher <drbacher at gmail.com>
21 * Florian octo Forster <octo at collectd.org>
29 # error "No applicable input method."
32 static void serial_submit (const char *type_instance,
33 derive_t rx, derive_t tx)
36 value_list_t vl = VALUE_LIST_INIT;
38 values[0].derive = rx;
39 values[1].derive = tx;
43 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
44 sstrncpy (vl.plugin, "serial", sizeof (vl.plugin));
45 sstrncpy (vl.type, "serial_octets", sizeof (vl.type));
46 sstrncpy (vl.type_instance, type_instance,
47 sizeof (vl.type_instance));
49 plugin_dispatch_values (&vl);
52 static int serial_read (void)
57 /* there are a variety of names for the serial device */
58 if ((fh = fopen ("/proc/tty/driver/serial", "r")) == NULL &&
59 (fh = fopen ("/proc/tty/driver/ttyS", "r")) == NULL)
62 WARNING ("serial: fopen: %s",
63 sstrerror (errno, errbuf, sizeof (errbuf)));
67 while (fgets (buffer, sizeof (buffer), fh) != NULL)
71 _Bool have_rx = 0, have_tx = 0;
78 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
83 * 0: uart:16550A port:000003F8 irq:4 tx:0 rx:0
84 * 1: uart:16550A port:000002F8 irq:3 tx:0 rx:0
86 len = strlen (fields[0]);
89 if (fields[0][len - 1] != ':')
91 fields[0][len - 1] = 0;
93 for (i = 1; i < numfields; i++)
95 len = strlen (fields[i]);
99 if (strncmp (fields[i], "tx:", 3) == 0)
101 if (strtoderive (fields[i] + 3, &tx) == 0)
104 else if (strncmp (fields[i], "rx:", 3) == 0)
106 if (strtoderive (fields[i] + 3, &rx) == 0)
111 if (have_rx && have_tx)
112 serial_submit (fields[0], rx, tx);
117 } /* int serial_read */
119 void module_register (void)
121 plugin_register_read ("serial", serial_read);
122 } /* void module_register */