Merge branch 'collectd-5.6'
[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,
34                 derive_t rx, derive_t tx)
35 {
36         value_list_t vl = VALUE_LIST_INIT;
37         value_t values[] = {
38                 { .derive = rx },
39                 { .derive = tx },
40         };
41
42         vl.values = values;
43         vl.values_len = STATIC_ARRAY_SIZE (values);
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));
48
49         plugin_dispatch_values (&vl);
50 }
51
52 static int serial_read (void)
53 {
54         FILE *fh;
55         char buffer[1024];
56
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)
60         {
61                 char errbuf[1024];
62                 WARNING ("serial: fopen: %s",
63                                 sstrerror (errno, errbuf, sizeof (errbuf)));
64                 return (-1);
65         }
66
67         while (fgets (buffer, sizeof (buffer), fh) != NULL)
68         {
69                 derive_t rx = 0;
70                 derive_t tx = 0;
71                 _Bool have_rx = 0, have_tx = 0;
72                 size_t len;
73
74                 char *fields[16];
75                 int numfields;
76
77                 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
78                 if (numfields < 6)
79                         continue;
80
81                 /*
82                  * 0: uart:16550A port:000003F8 irq:4 tx:0 rx:0
83                  * 1: uart:16550A port:000002F8 irq:3 tx:0 rx:0
84                  */
85                 len = strlen (fields[0]);
86                 if (len < 2)
87                         continue;
88                 if (fields[0][len - 1] != ':')
89                         continue;
90                 fields[0][len - 1] = 0;
91
92                 for (int i = 1; i < numfields; i++)
93                 {
94                         len = strlen (fields[i]);
95                         if (len < 4)
96                                 continue;
97
98                         if (strncmp (fields[i], "tx:", 3) == 0)
99                         {
100                                 if (strtoderive (fields[i] + 3, &tx) == 0)
101                                         have_tx = 1;
102                         }
103                         else if (strncmp (fields[i], "rx:", 3) == 0)
104                         {
105                                 if (strtoderive (fields[i] + 3, &rx) == 0)
106                                         have_rx = 1;
107                         }
108                 }
109
110                 if (have_rx && have_tx)
111                         serial_submit (fields[0], rx, tx);
112         }
113
114         fclose (fh);
115         return (0);
116 } /* int serial_read */
117
118 void module_register (void)
119 {
120         plugin_register_read ("serial", serial_read);
121 } /* void module_register */