Tree wide: Reformat with clang-format.
[collectd.git] / src / serial.c
1 /**
2  * collectd - src/serial.c
3  * Copyright (C) 2005,2006  David Bacher
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; either version 2 of the License, or (at your
8  * option) any later version.
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  * General Public License for more details.
14  *
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
18  *
19  * Authors:
20  *   David Bacher <drbacher at gmail.com>
21  *   Florian octo Forster <octo at collectd.org>
22  **/
23
24 #include "collectd.h"
25
26 #include "common.h"
27 #include "plugin.h"
28
29 #if !KERNEL_LINUX
30 #error "No applicable input method."
31 #endif
32
33 static void serial_submit(const char *type_instance, derive_t rx, derive_t tx) {
34   value_t values[2];
35   value_list_t vl = VALUE_LIST_INIT;
36
37   values[0].derive = rx;
38   values[1].derive = tx;
39
40   vl.values = values;
41   vl.values_len = 2;
42   sstrncpy(vl.host, hostname_g, sizeof(vl.host));
43   sstrncpy(vl.plugin, "serial", sizeof(vl.plugin));
44   sstrncpy(vl.type, "serial_octets", sizeof(vl.type));
45   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
46
47   plugin_dispatch_values(&vl);
48 }
49
50 static int serial_read(void) {
51   FILE *fh;
52   char buffer[1024];
53
54   /* there are a variety of names for the serial device */
55   if ((fh = fopen("/proc/tty/driver/serial", "r")) == NULL &&
56       (fh = fopen("/proc/tty/driver/ttyS", "r")) == NULL) {
57     char errbuf[1024];
58     WARNING("serial: fopen: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
59     return (-1);
60   }
61
62   while (fgets(buffer, sizeof(buffer), fh) != NULL) {
63     derive_t rx = 0;
64     derive_t tx = 0;
65     _Bool have_rx = 0, have_tx = 0;
66     size_t len;
67
68     char *fields[16];
69     int numfields;
70
71     numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
72     if (numfields < 6)
73       continue;
74
75     /*
76      * 0: uart:16550A port:000003F8 irq:4 tx:0 rx:0
77      * 1: uart:16550A port:000002F8 irq:3 tx:0 rx:0
78      */
79     len = strlen(fields[0]);
80     if (len < 2)
81       continue;
82     if (fields[0][len - 1] != ':')
83       continue;
84     fields[0][len - 1] = 0;
85
86     for (int i = 1; i < numfields; i++) {
87       len = strlen(fields[i]);
88       if (len < 4)
89         continue;
90
91       if (strncmp(fields[i], "tx:", 3) == 0) {
92         if (strtoderive(fields[i] + 3, &tx) == 0)
93           have_tx = 1;
94       } else if (strncmp(fields[i], "rx:", 3) == 0) {
95         if (strtoderive(fields[i] + 3, &rx) == 0)
96           have_rx = 1;
97       }
98     }
99
100     if (have_rx && have_tx)
101       serial_submit(fields[0], rx, tx);
102   }
103
104   fclose(fh);
105   return (0);
106 } /* int serial_read */
107
108 void module_register(void) {
109   plugin_register_read("serial", serial_read);
110 } /* void module_register */