X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fserial.c;h=0878d3c93b1dac2b4d4bc331e4d13e56a358ede9;hb=939f4098d43c2934a9a97e076d64a7504613b872;hp=1c5d5a5594389a140d3de387e5d4e83237f5b5d2;hpb=5f9ec13b946733ff4e1edf2d8e3b7a22311dd894;p=collectd.git diff --git a/src/serial.c b/src/serial.c index 1c5d5a55..0878d3c9 100644 --- a/src/serial.c +++ b/src/serial.c @@ -18,7 +18,7 @@ * * Authors: * David Bacher - * Florian octo Forster + * Florian octo Forster **/ #include "collectd.h" @@ -30,20 +30,19 @@ #endif static void serial_submit (const char *type_instance, - counter_t rx, counter_t tx) + derive_t rx, derive_t tx) { value_t values[2]; value_list_t vl = VALUE_LIST_INIT; - values[0].counter = rx; - values[1].counter = tx; + values[0].derive = rx; + values[1].derive = tx; vl.values = values; vl.values_len = 2; - vl.time = time (NULL); - strcpy (vl.host, hostname_g); - strcpy (vl.plugin, "serial"); - strcpy (vl.type, "serial_octets"); + sstrncpy (vl.host, hostname_g, sizeof (vl.host)); + sstrncpy (vl.plugin, "serial", sizeof (vl.plugin)); + sstrncpy (vl.type, "serial_octets", sizeof (vl.type)); sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); @@ -55,13 +54,6 @@ static int serial_read (void) FILE *fh; char buffer[1024]; - counter_t rx = 0; - counter_t tx = 0; - - char *fields[16]; - int i, numfields; - int len; - /* there are a variety of names for the serial device */ if ((fh = fopen ("/proc/tty/driver/serial", "r")) == NULL && (fh = fopen ("/proc/tty/driver/ttyS", "r")) == NULL) @@ -74,10 +66,16 @@ static int serial_read (void) while (fgets (buffer, sizeof (buffer), fh) != NULL) { - int have_rx = 0, have_tx = 0; + derive_t rx = 0; + derive_t tx = 0; + _Bool have_rx = 0, have_tx = 0; + size_t len; - numfields = strsplit (buffer, fields, 16); + char *fields[16]; + int numfields; + int i; + numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields)); if (numfields < 6) continue; @@ -85,12 +83,12 @@ static int serial_read (void) * 0: uart:16550A port:000003F8 irq:4 tx:0 rx:0 * 1: uart:16550A port:000002F8 irq:3 tx:0 rx:0 */ - len = strlen (fields[0]) - 1; - if (len < 1) + len = strlen (fields[0]); + if (len < 2) continue; - if (fields[0][len] != ':') + if (fields[0][len - 1] != ':') continue; - fields[0][len] = '\0'; + fields[0][len - 1] = 0; for (i = 1; i < numfields; i++) { @@ -100,20 +98,18 @@ static int serial_read (void) if (strncmp (fields[i], "tx:", 3) == 0) { - tx = atoll (fields[i] + 3); - have_tx++; + if (strtoderive (fields[i] + 3, &tx) == 0) + have_tx = 1; } else if (strncmp (fields[i], "rx:", 3) == 0) { - rx = atoll (fields[i] + 3); - have_rx++; + if (strtoderive (fields[i] + 3, &rx) == 0) + have_rx = 1; } } - if ((have_rx == 0) || (have_tx == 0)) - continue; - - serial_submit (fields[0], rx, tx); + if (have_rx && have_tx) + serial_submit (fields[0], rx, tx); } fclose (fh);