#include #include #include #include #include #include #include "util.h" #include "sia.h" extern int new_block (int fd, SIA_Block *b, int log, const char *email, const char *state_file, const char *mqtt_host); extern void dump_state (const char *); //extern void periodic_task(void); static int usage (const char *name) { fprintf (stderr, "Usage:\n"); fprintf (stderr, "%s [ -l ] [ -b baud ] -p serial_device [ -e email address ] [ -s state_file ] [ -m mqtt_host ]\n\n", name); fprintf (stderr, "baud defaults to 9600\n"); return 1; } int main (int argc, char *argv[]) { const char *port; int baud = 9600; unsigned opt; int fd; SIA_Block b; ssize_t red; int log = 0; unsigned char buf[SIA_MAX_BLOCK_LENGTH]; unsigned ptr = 0; const char *email = NULL; const char *state_file = NULL; const char *mqtt_host = NULL; while ((opt = getopt (argc, argv, "p:b:le:s:m:")) != -1) { switch (opt) { case 'p': port = optarg; break; case 'b': baud = atoi (optarg); break; case 'e': email = optarg; break; case 's': state_file = optarg; break; case 'm': mqtt_host = optarg; break; case 'l': log++; break; default: /* '?' */ return usage (argv[0]); } } if (!port) return usage (argv[0]); if (log) openlog ("serial_rx", 0, LOG_USER); fd = open_tty (port, baud); set_blocking (fd); if (state_file) dump_state (state_file); for (;;) { red = read (fd, &buf[ptr], 1); if ((red < 0) || (red > 1)) { perror ("read"); return 1; } if (!red) continue; ptr++; switch (sia_parse (buf, ptr, &b)) { case -1: /*Parse errror, flush and carry on */ tcflush (fd, TCIFLUSH); ptr = 0; break; case 0: /*More data needed*/ break; default: /*Valid block */ new_block (fd, &b, log, email, state_file, mqtt_host); ptr = 0; break; } if (ptr == SIA_MAX_BLOCK_LENGTH) { tcflush (fd, TCIFLUSH); ptr = 0; } } }