Cleanup receive buffer. Report last pump on error
authorvolpol <volpol@packet-gain.de>
Thu, 23 Aug 2018 18:48:25 +0000 (20:48 +0200)
committervolpol <volpol@packet-gain.de>
Thu, 23 Aug 2018 18:48:25 +0000 (20:48 +0200)
main.c

diff --git a/main.c b/main.c
index 52924d5552e99fe78177ffccd9aa81d45a90bd70..59cdc434a540b750a90d70b9b50bb55ea8202b95 100644 (file)
--- a/main.c
+++ b/main.c
@@ -35,6 +35,8 @@ static void handle_signal(int sig){
 //0 - ready, >0 - busy, -1 - err/unknown
 static volatile int mix_state;
 
+static volatile int last_pump;
+
 static void mix(const char *recipe){
     int idx;
     int vol;
@@ -43,7 +45,7 @@ static void mix(const char *recipe){
     int sr;
 
     s = recipe;
-
+    last_pump = 0;
     while (!force_quit && s && *s){
         fprintf (stderr, "#%s#\n", s);
         idx = strtoul(s, &n, 10);
@@ -56,6 +58,7 @@ static void mix(const char *recipe){
         fprintf (stderr, "START PUMP: %d VOL %d\n", idx, vol);
 
         if (0 != pump_add(idx)) break; //or continue?
+        last_pump = idx;
         mgmt_start(vol);
         pump_on(idx);
         while (MGMT_CONTINUE == (sr = mgmt_stop()) && !force_quit) usleep(100);
@@ -100,7 +103,7 @@ do {
         else {
             total += bread;
             if ('\n' == buf[total -  1]){
-                buf[total -  1] = 0;
+                memset (buf + total - 1, 0, sizeof buf - total);
                 fprintf (stderr, "%s\n", buf);
                 if (0 == strncmp((char *)buf, "MIX:", 4)) mix((char *)buf + 4);
                 if (0 == strncmp((char *)buf, "QUIT", 4)) res = -1;
@@ -163,8 +166,12 @@ int bbd(void){
                         DPRINT ("PID: %lX\n", dealer);
                     break;
                 case -1:
-
-                    write (as, "ERROR\n", 6);
+                    if (!last_pump)
+                        write (as, "ERROR\n", 6);
+                    else {
+                        char buf[256];
+                        write (as, buf, sprintf (buf, "ERROR, LAST PUMP:%u\n", last_pump));
+                    }
                     net_close (as);
                     break;
                 default: