altosui: Handle Monitor Idle errors better
authorKeith Packard <keithp@keithp.com>
Mon, 30 Jul 2012 02:34:00 +0000 (19:34 -0700)
committerKeith Packard <keithp@keithp.com>
Mon, 30 Jul 2012 02:34:00 +0000 (19:34 -0700)
Deal with missing data by checking for MISSING in more places.
Handle serial communication failures during send by reporting back
from libaltos.

Signed-off-by: Keith Packard <keithp@keithp.com>
altoslib/AltosIdleMonitor.java
altoslib/AltosLink.java
altoslib/AltosRecord.java
altoslib/AltosRecordTM.java
altoslib/AltosState.java
altosui/AltosSerial.java
altosui/libaltos/libaltos.c

index 57c4da71f3ffb78465c78cd467e87e4faafbe033..27ea3a2bcfa21ad94b6913f1246004a2aeaabd3c 100644 (file)
@@ -23,16 +23,10 @@ import java.text.*;
 import java.util.prefs.*;
 import java.util.concurrent.*;
 
-class AltosSensorTM {
-       int     tick;
-       int     accel;
-       int     pres;
-       int     temp;
-       int     batt;
-       int     drogue;
-       int     main;
+class AltosSensorTM extends AltosRecordTM {
 
-       public AltosSensorTM(AltosLink link) throws InterruptedException, TimeoutException {
+       public AltosSensorTM(AltosLink link, AltosConfigData config_data) throws InterruptedException, TimeoutException {
+               super();
                link.printf("a\n");
                for (;;) {
                        String line = link.get_reply_no_dialog(5000);
@@ -82,6 +76,10 @@ class AltosSensorTM {
                        }
                        break;
                }
+               ground_accel = config_data.accel_cal_plus;
+               ground_pres = pres;
+               accel_plus_g = config_data.accel_cal_plus;
+               accel_minus_g = config_data.accel_cal_minus;
        }
 }
 
@@ -253,7 +251,7 @@ class AltosGPSQuery extends AltosGPS {
                        if (line.startsWith("Date:")) {
                                if (bits.length < 2)
                                        continue;
-                               String[] d = bits[1].split(":");
+                               String[] d = bits[1].split("/");
                                if (d.length < 3)
                                        continue;
                                year = Integer.parseInt(d[0]) + 2000;
@@ -264,7 +262,7 @@ class AltosGPSQuery extends AltosGPS {
                        if (line.startsWith("Time:")) {
                                if (bits.length < 2)
                                        continue;
-                               String[] d = bits[1].split("/");
+                               String[] d = bits[1].split(":");
                                if (d.length < 3)
                                        continue;
                                hour = Integer.parseInt(d[0]);
@@ -339,8 +337,7 @@ public class AltosIdleMonitor extends Thread {
        }
 
        void update_state() throws InterruptedException, TimeoutException {
-               AltosRecord     record;
-               int             rssi;
+               AltosRecord     record = null;
 
                try {
                        if (remote) {
@@ -350,20 +347,7 @@ public class AltosIdleMonitor extends Thread {
                                link.flush_input();
                        config_data = new AltosConfigData(link);
                        if (config_data.product.startsWith("TeleMetrum")) {
-                               AltosRecordTM record_tm = new AltosRecordTM();
-                               AltosSensorTM sensor = new AltosSensorTM(link);
-                               record_tm.accel = sensor.accel;
-                               record_tm.pres = sensor.pres;
-                               record_tm.batt = sensor.batt;
-                               record_tm.temp = sensor.temp;
-                               record_tm.drogue = sensor.drogue;
-                               record_tm.main = sensor.main;
-                               record_tm.ground_accel = record_tm.accel;
-                               record_tm.ground_pres = record_tm.pres;
-                               record_tm.accel_plus_g = config_data.accel_cal_plus;
-                               record_tm.accel_minus_g = config_data.accel_cal_minus;
-                               record_tm.tick = sensor.tick;
-                               record = record_tm;
+                               record = new AltosSensorTM(link, config_data);
                        } else if (config_data.product.startsWith("MegaMetrum")) {
                                AltosRecordMM record_mm = new AltosRecordMM();
                                AltosSensorMM sensor = new AltosSensorMM(link);
@@ -390,24 +374,27 @@ public class AltosIdleMonitor extends Thread {
                                record = new AltosRecord();
 
                        gps = new AltosGPSQuery(link, config_data);
+
+                       record.version = 0;
+                       record.callsign = config_data.callsign;
+                       record.serial = config_data.serial;
+                       record.flight = config_data.log_available() > 0 ? 255 : 0;
+                       record.status = 0;
+                       record.state = AltosLib.ao_flight_idle;
+                       record.gps = gps;
+                       record.new_gps = true;
+                       state = new AltosState (record, state);
                } finally {
                        if (remote) {
                                link.stop_remote();
-                               rssi = AltosRSSI();
-                       } else
-                               rssi = 0;
+                               if (record != null)
+                                       record.rssi = AltosRSSI();
+                       } else {
+                               if (record != null)
+                                       record.rssi = 0;
+                       }
                }
 
-               record.version = 0;
-               record.callsign = config_data.callsign;
-               record.serial = config_data.serial;
-               record.flight = config_data.log_available() > 0 ? 255 : 0;
-               record.rssi = rssi;
-               record.status = 0;
-               record.state = AltosLib.ao_flight_idle;
-
-               record.gps = gps;
-               state = new AltosState (record, state);
        }
 
        public void set_frequency(double in_frequency) {
index d59e73ba6eca2a690da98f0d46e9db7e6c870919..fd5db7e977fd7092ee7a79077a7343abb3905029 100644 (file)
@@ -101,15 +101,23 @@ public abstract class AltosLink {
                try {
                        for (;;) {
                                c = getchar();
-                               if (Thread.interrupted())
+                               if (Thread.interrupted()) {
+                                       if (debug)
+                                               System.out.printf("INTERRUPTED\n");
                                        break;
+                               }
                                if (c == ERROR) {
+                                       if (debug)
+                                               System.out.printf("ERROR\n");
                                        add_telem (new AltosLine());
                                        add_reply (new AltosLine());
                                        break;
                                }
-                               if (c == TIMEOUT)
+                               if (c == TIMEOUT) {
+                                       if (debug)
+                                               System.out.printf("TIMEOUT\n");
                                        continue;
+                               }
                                if (c == '\r')
                                        continue;
                                synchronized(this) {
@@ -180,6 +188,14 @@ public abstract class AltosLink {
                reply_queue.put (line);
        }
 
+       public void abort_reply() {
+               try {
+                       add_telem (new AltosLine());
+                       add_reply (new AltosLine());
+               } catch (InterruptedException e) {
+               }
+       }
+
        public void add_string(String line) throws InterruptedException {
                if (line.startsWith("TELEM") || line.startsWith("VERSION") || line.startsWith("CRC")) {
                        add_telem(new AltosLine(line));
index e468f84b425b705a1b6a498d309679c11449b18d..8722bc050bac011f183248301012d27b1d46e5c0 100644 (file)
@@ -127,7 +127,7 @@ public class AltosRecord implements Comparable <AltosRecord>, Cloneable {
 
                double  p = filtered_pressure();
                if (p == MISSING)
-                       return MISSING;
+                       return raw_altitude();
                return AltosConvert.pressure_to_altitude(p);
        }
 
index afb7079097fd05359fe8d998f4ca8fdf46515ee4..37accef60289cd8d2518d4217c5d9b22403cae26 100644 (file)
@@ -177,14 +177,14 @@ public class AltosRecordTM extends AltosRecord {
                drogue = MISSING;
                main = MISSING;
 
-               flight_accel = 0;
-               flight_vel = 0;
-               flight_pres = 0;
-
-               ground_accel = 0;
-               ground_pres = 0;
-               accel_plus_g = 0;
-               accel_minus_g = 0;
+               flight_accel = MISSING;
+               flight_vel = MISSING;
+               flight_pres = MISSING;
+
+               ground_accel = MISSING;
+               ground_pres = MISSING;
+               accel_plus_g = MISSING;
+               accel_minus_g = MISSING;
        }
 
        public AltosRecordTM(AltosRecord old) {
index e20ec9a7109dbcd493deac0b544f692f2e78cd38..3b37a3d4b498fc90a8ba4e47cd1d6a03fd5681d8 100644 (file)
@@ -38,6 +38,7 @@ public class AltosState {
        public boolean boost;   /* under power */
 
        public double   ground_altitude;
+       public double   altitude;
        public double   height;
        public double   speed;
        public double   acceleration;
@@ -82,6 +83,7 @@ public class AltosState {
                data = cur;
 
                ground_altitude = data.ground_altitude();
+               altitude = data.raw_altitude();
                height = data.filtered_height();
 
                report_time = System.currentTimeMillis();
index c4e9c69720339e0d0a4b474f87319986fc4dd33b..8b692fa909cc36e3c06ed82be0ee8241e9df29f3 100644 (file)
@@ -54,13 +54,19 @@ public class AltosSerial extends AltosLink implements Runnable {
        Frame frame;
 
        public int getchar() {
+               if (altos == null)
+                       return ERROR;
                return libaltos.altos_getchar(altos, 0);
        }
 
        public void flush_output() {
                super.flush_output();
                if (altos != null) {
-                       libaltos.altos_flush(altos);
+                       if (libaltos.altos_flush(altos) != 0) {
+                               libaltos.altos_close(altos);
+                               altos = null;
+                               abort_reply();
+                       }
                }
        }
 
@@ -155,7 +161,11 @@ public class AltosSerial extends AltosLink implements Runnable {
 
        private void putc(char c) {
                if (altos != null)
-                       libaltos.altos_putchar(altos, c);
+                       if (libaltos.altos_putchar(altos, c) != 0) {
+                               libaltos.altos_close(altos);
+                               altos = null;
+                               abort_reply();
+                       }
        }
 
        public void print(String data) {
index 1cc27cbee6e42040a287f85851cc179559edc6ae..515432f9ac21f8ed4e351ae89a881ae59cc398fe 100644 (file)
@@ -221,7 +221,7 @@ altos_flush(struct altos_file *file)
 #endif
                if (ret < 0) {
                        altos_set_last_posix_error();
-                       return -errno;
+                       return -last_error.code;
                }
                if (ret) {
                        memmove(file->out_data, file->out_data + ret,
@@ -247,7 +247,7 @@ altos_putchar(struct altos_file *file, char c)
        ret = 0;
        if (file->out_used == USB_BUF_SIZE)
                ret = altos_flush(file);
-       return 0;
+       return ret;
 }
 
 #ifdef USE_POLL