altosui: Report error message back from libaltos
authorKeith Packard <keithp@keithp.com>
Sun, 28 Aug 2011 22:50:30 +0000 (15:50 -0700)
committerKeith Packard <keithp@keithp.com>
Sun, 28 Aug 2011 22:50:30 +0000 (15:50 -0700)
This includes changing all of the error dialogs to show the error
message rather than just the file name.

Signed-off-by: Keith Packard <keithp@keithp.com>
17 files changed:
altosui/AltosBTDevice.java
altosui/AltosCSVUI.java
altosui/AltosConfig.java
altosui/AltosDataChooser.java
altosui/AltosDevice.java
altosui/AltosEepromDownload.java
altosui/AltosEepromManage.java
altosui/AltosFlashUI.java
altosui/AltosIgniteUI.java
altosui/AltosLanded.java
altosui/AltosLaunchUI.java
altosui/AltosScanUI.java
altosui/AltosSerial.java
altosui/AltosUI.java
altosui/AltosUSBDevice.java
altosui/libaltos/libaltos.c
altosui/libaltos/libaltos.h

index 7a876c25feae21ee3a56d2a4ccae372ddfd4e50b..55b8f8fc8bf5f28c2b7f848bbe56bf9b2434ff17 100644 (file)
@@ -42,6 +42,13 @@ public class AltosBTDevice extends altos_bt_device implements AltosDevice {
                return getAddr();
        }
 
+       public String getErrorString() {
+               altos_error     error = new altos_error();
+
+               libaltos.altos_get_last_error(error);
+               return String.format("%s (%d)", error.getString(), error.getCode());
+       }
+
        public int getSerial() {
                String name = getName();
                if (name == null)
index e1b6002dfa23235d08a1fb5ae4a92a537be5e15e..a212409ee83e3fc4f60aaa6d361a39cd87e15fc3 100644 (file)
@@ -99,7 +99,7 @@ public class AltosCSVUI
                                writer.close();
                        } catch (FileNotFoundException ee) {
                                JOptionPane.showMessageDialog(frame,
-                                                             file.getName(),
+                                                             ee.getMessage(),
                                                              "Cannot open file",
                                                              JOptionPane.ERROR_MESSAGE);
                        }
index 122ebeccf88d449800286f569a84026e962a5cfb..93def70daaad2e527616da648c4384187d152c95 100644 (file)
@@ -480,8 +480,7 @@ public class AltosConfig implements ActionListener {
                                }
                        } catch (FileNotFoundException ee) {
                                JOptionPane.showMessageDialog(owner,
-                                                             String.format("Cannot open device \"%s\"",
-                                                                           device.toShortString()),
+                                                             ee.getMessage(),
                                                              "Cannot open target device",
                                                              JOptionPane.ERROR_MESSAGE);
                        } catch (AltosSerialInUseException si) {
index 15de05c2bb510723dabbe22704cbc41d55e57dad..d81ca6d1a128aa009c7420e0f9a67f96901ae4f4 100644 (file)
@@ -61,7 +61,7 @@ public class AltosDataChooser extends JFileChooser {
                                }
                        } catch (FileNotFoundException fe) {
                                JOptionPane.showMessageDialog(frame,
-                                                             filename,
+                                                             fe.getMessage(),
                                                              "Cannot open file",
                                                              JOptionPane.ERROR_MESSAGE);
                        }
index 3357c550fa632ab66c8f73773ae15849b3605c77..1b5c1a91bf76ec83b52d13bab659855b0eb7a310 100644 (file)
@@ -26,5 +26,6 @@ public interface AltosDevice {
        public abstract int getSerial();
        public abstract String getPath();
        public abstract boolean matchProduct(int product);
+       public abstract String getErrorString();
        public SWIGTYPE_p_altos_file open();
 }
index 358ad337900e204435726f005cd529a299fc20ab..e7e52466b71b24cd577036e18b51dde4bcf8a3e7 100644 (file)
@@ -248,6 +248,10 @@ public class AltosEepromDownload implements Runnable {
                        done = true;
        }
 
+       void CaptureTelemetry(AltosEepromChunk eechunk) throws IOException {
+               
+       }
+
        void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException {
                int                     block, state_block = 0;
                int                     log_format = flights.config_data.log_format;
@@ -300,10 +304,10 @@ public class AltosEepromDownload implements Runnable {
                                extension = "eeprom";
                                CaptureTiny(eechunk);
                                break;
-//                     case Altos.AO_LOG_FORMAT_TELEMETRY:
-//                             extension = "telem";
-//                             CaptureTelemetry(eechunk);
-//                             break;
+                       case Altos.AO_LOG_FORMAT_TELEMETRY:
+                               extension = "telem";
+                               CaptureTelemetry(eechunk);
+                               break;
                        case Altos.AO_LOG_FORMAT_TELESCIENCE:
                                extension = "science";
                                CaptureTeleScience(eechunk);
index 2e5206280b1e308e5aef46d5358faefc4b769835..083c73725d44e4f2fa928ce8532b2fd8f7bb363d 100644 (file)
@@ -219,8 +219,7 @@ public class AltosEepromManage implements ActionListener {
                                t.start();
                        } catch (FileNotFoundException ee) {
                                JOptionPane.showMessageDialog(frame,
-                                                             String.format("Cannot open device \"%s\"",
-                                                                           device.toShortString()),
+                                                             ee.getMessage(),
                                                              "Cannot open target device",
                                                              JOptionPane.ERROR_MESSAGE);
                        } catch (AltosSerialInUseException si) {
index 3874b5004995cb0269a612005b2949783f2747de..3956ff20a94789d99148df23fdda1455e9aa0beb 100644 (file)
@@ -200,8 +200,8 @@ public class AltosFlashUI
        void exception (Exception e) {
                if (e instanceof FileNotFoundException) {
                        JOptionPane.showMessageDialog(frame,
-                                                     "Cannot open image",
-                                                     file.toString(),
+                                                     ((FileNotFoundException) e).getMessage(),
+                                                     "Cannot open file",
                                                      JOptionPane.ERROR_MESSAGE);
                } else if (e instanceof AltosSerialInUseException) {
                        JOptionPane.showMessageDialog(frame,
index c11a86145ce1a68b0d8768cfa75a920469f25272..b215c22865604fd0631109e5b03b6ecd8000f6df 100644 (file)
@@ -122,8 +122,7 @@ public class AltosIgniteUI
        void ignite_exception(Exception e) {
                if (e instanceof FileNotFoundException) {
                        JOptionPane.showMessageDialog(owner,
-                                                     String.format("Cannot open device \"%s\"",
-                                                                   device.toShortString()),
+                                                     ((FileNotFoundException) e).getMessage(),
                                                      "Cannot open target device",
                                                      JOptionPane.ERROR_MESSAGE);
                } else if (e instanceof AltosSerialInUseException) {
index 50e6b542f0501093938614b2b4bc4794f0558a8e..4dd9a2dddd7dfe19295db71d8d68c17f0b654dd5 100644 (file)
@@ -250,7 +250,7 @@ public class AltosLanded extends JComponent implements AltosFlightDisplay, Actio
                                                FileInputStream in = new FileInputStream(file);
                                                records = new AltosTelemetryIterable(in);
                                        } else {
-                                               throw new FileNotFoundException();
+                                               throw new FileNotFoundException(filename);
                                        }
                                        try {
                                                new AltosGraphUI(records, filename);
@@ -259,7 +259,7 @@ public class AltosLanded extends JComponent implements AltosFlightDisplay, Actio
                                        }
                                } catch (FileNotFoundException fe) {
                                        JOptionPane.showMessageDialog(null,
-                                                                     filename,
+                                                                     fe.getMessage(),
                                                                      "Cannot open file",
                                                                      JOptionPane.ERROR_MESSAGE);
                                }
index 4e630afb11045477fff135c42d32f19bc16f51c0..47365e03bd46a19bc933553d55beac501e3effd9 100644 (file)
@@ -164,8 +164,7 @@ public class AltosLaunchUI
        void launch_exception(Exception e) {
                if (e instanceof FileNotFoundException) {
                        JOptionPane.showMessageDialog(owner,
-                                                     String.format("Cannot open device \"%s\"",
-                                                                   device.toShortString()),
+                                                     ((FileNotFoundException) e).getMessage(),
                                                      "Cannot open target device",
                                                      JOptionPane.ERROR_MESSAGE);
                } else if (e instanceof AltosSerialInUseException) {
index bce4b32c9ef02e19db030e33eb77a5e5a2558005..df5c51d4a2e3ab74d98dd9ad7b0a7bd7fe9bd1f8 100644 (file)
@@ -130,8 +130,7 @@ public class AltosScanUI
        void scan_exception(Exception e) {
                if (e instanceof FileNotFoundException) {
                        JOptionPane.showMessageDialog(owner,
-                                                     String.format("Cannot open device \"%s\"",
-                                                                   device.toShortString()),
+                                                     ((FileNotFoundException) e).getMessage(),
                                                      "Cannot open target device",
                                                      JOptionPane.ERROR_MESSAGE);
                } else if (e instanceof AltosSerialInUseException) {
@@ -326,8 +325,7 @@ public class AltosScanUI
                        return true;
                } catch (FileNotFoundException ee) {
                        JOptionPane.showMessageDialog(owner,
-                                                     String.format("Cannot open device \"%s\"",
-                                                                   device.toShortString()),
+                                                     ee.getMessage(),
                                                      "Cannot open target device",
                                                      JOptionPane.ERROR_MESSAGE);
                } catch (AltosSerialInUseException si) {
index 0a531aa96faa8b2b0b27fc0f412b78c920648231..4cf306d051e478706294010c90c35c6ce7aae104 100644 (file)
@@ -323,8 +323,10 @@ public class AltosSerial implements Runnable {
                }
                altos = device.open();
                if (altos == null) {
+                       final String    message = device.getErrorString();
                        close();
-                       throw new FileNotFoundException(device.toShortString());
+                       throw new FileNotFoundException(String.format("%s (%s)",
+                                                                     device.toShortString(), message));
                }
                if (debug)
                        System.out.printf("Open %s\n", device.getPath());
index 60adfc7c823a7d480f690ae983da296f73d26af1..3e5bcf434fc5b8b8276fe2074873e528d8032d74 100644 (file)
@@ -52,8 +52,7 @@ public class AltosUI extends JFrame {
                                new AltosFlightUI(voice, reader, device.getSerial());
                } catch (FileNotFoundException ee) {
                        JOptionPane.showMessageDialog(AltosUI.this,
-                                                     String.format("Cannot open device \"%s\"",
-                                                                   device.toShortString()),
+                                                     ee.getMessage(),
                                                      "Cannot open target device",
                                                      JOptionPane.ERROR_MESSAGE);
                } catch (AltosSerialInUseException si) {
@@ -356,7 +355,7 @@ public class AltosUI extends JFrame {
                        else
                                return new AltosTelemetryIterable(in);
                } catch (FileNotFoundException fe) {
-                       System.out.printf("Cannot open '%s'\n", filename);
+                       System.out.printf("%s\n", fe.getMessage());
                        return null;
                }
        }
@@ -366,7 +365,7 @@ public class AltosUI extends JFrame {
                try {
                        return new AltosCSV(file);
                } catch (FileNotFoundException fe) {
-                       System.out.printf("Cannot open '%s'\n", filename);
+                       System.out.printf("%s\n", fe.getMessage());
                        return null;
                }
        }
@@ -376,7 +375,7 @@ public class AltosUI extends JFrame {
                try {
                        return new AltosKML(file);
                } catch (FileNotFoundException fe) {
-                       System.out.printf("Cannot open '%s'\n", filename);
+                       System.out.printf("%s\n", fe.getMessage());
                        return null;
                }
        }
index dc746a64fb4aa7de038872bbf18f5fde13d7d523..b11a3934b1376906974eab03cdf02b8c698f3817 100644 (file)
@@ -39,6 +39,13 @@ public class AltosUSBDevice  extends altos_device implements AltosDevice {
 
        }
 
+       public String getErrorString() {
+               altos_error     error = new altos_error();
+
+               libaltos.altos_get_last_error(error);
+               return String.format("%s (%d)", error.getString(), error.getCode());
+       }
+
        public SWIGTYPE_p_altos_file open() {
                return libaltos.altos_open(this);
        }
index a3796ee380e9d0b2a3516719b946cfe3c5d0e276..48e00a4415d4bd034f33bd45710035a561d69fe9 100644 (file)
@@ -49,6 +49,22 @@ altos_fini(void)
 {
 }
 
+static struct altos_error last_error;
+
+static void
+altos_set_last_error(int code, char *string)
+{
+       last_error.code = code;
+       strncpy(last_error.string, string, sizeof (last_error.string) -1);
+       last_error.string[sizeof(last_error.string)-1] = '\0';
+}
+
+PUBLIC void
+altos_get_last_error(struct altos_error *error)
+{
+       *error = last_error;
+}
+
 #ifdef DARWIN
 
 #undef USE_POLL
@@ -96,6 +112,12 @@ struct altos_file {
        int                             in_read;
 };
 
+static void
+altos_set_last_posix_error(void)
+{
+       altos_set_last_error(errno, strerror(errno));
+}
+
 PUBLIC struct altos_file *
 altos_open(struct altos_device *device)
 {
@@ -103,12 +125,18 @@ altos_open(struct altos_device *device)
        int                     ret;
        struct termios          term;
 
-       if (!file)
+       if (!file) {
+               altos_set_last_posix_error();
                return NULL;
+       }
+
+//     altos_set_last_error(12, "yeah yeah, failed again");
+//     free(file);
+//     return NULL;
 
        file->fd = open(device->path, O_RDWR | O_NOCTTY);
        if (file->fd < 0) {
-               perror(device->path);
+               altos_set_last_posix_error();
                free(file);
                return NULL;
        }
@@ -117,7 +145,7 @@ altos_open(struct altos_device *device)
 #else
        file->out_fd = open(device->path, O_RDWR | O_NOCTTY);
        if (file->out_fd < 0) {
-               perror(device->path);
+               altos_set_last_posix_error();
                close(file->fd);
                free(file);
                return NULL;
@@ -125,7 +153,7 @@ altos_open(struct altos_device *device)
 #endif
        ret = tcgetattr(file->fd, &term);
        if (ret < 0) {
-               perror("tcgetattr");
+               altos_set_last_posix_error();
                close(file->fd);
 #ifndef USE_POLL
                close(file->out_fd);
@@ -143,7 +171,7 @@ altos_open(struct altos_device *device)
 #endif
        ret = tcsetattr(file->fd, TCSAFLUSH, &term);
        if (ret < 0) {
-               perror("tcsetattr");
+               altos_set_last_posix_error();
                close(file->fd);
 #ifndef USE_POLL
                close(file->out_fd);
@@ -195,8 +223,10 @@ altos_flush(struct altos_file *file)
 #else
                ret = write (file->out_fd, file->out_data, file->out_used);
 #endif
-               if (ret < 0)
+               if (ret < 0) {
+                       altos_set_last_posix_error();
                        return -errno;
+               }
                if (ret) {
                        memmove(file->out_data, file->out_data + ret,
                                file->out_used - ret);
@@ -248,7 +278,7 @@ altos_fill(struct altos_file *file, int timeout)
                fd[1].events = POLLIN;
                ret = poll(fd, 2, timeout);
                if (ret < 0) {
-                       perror("altos_getchar");
+                       altos_set_last_posix_error();
                        return LIBALTOS_ERROR;
                }
                if (ret == 0)
@@ -261,7 +291,7 @@ altos_fill(struct altos_file *file, int timeout)
                {
                        ret = read(file->fd, file->in_data, USB_BUF_SIZE);
                        if (ret < 0) {
-                               perror("altos_getchar");
+                               altos_set_last_posix_error();
                                return LIBALTOS_ERROR;
                        }
                        file->in_read = 0;
@@ -700,8 +730,10 @@ altos_bt_open(struct altos_bt_device *device)
        if (!file)
                goto no_file;
        file->fd = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
-       if (file->fd < 0)
+       if (file->fd < 0) {
+               altos_set_last_posix_error();
                goto no_sock;
+       }
 
        addr.rc_family = AF_BLUETOOTH;
        addr.rc_channel = 1;
@@ -711,7 +743,7 @@ altos_bt_open(struct altos_bt_device *device)
                         (struct sockaddr *)&addr,
                         sizeof(addr));
        if (status < 0) {
-               perror("connect");
+               altos_set_last_posix_error();
                goto no_link;
        }
        sleep(1);
@@ -912,6 +944,21 @@ struct altos_file {
        OVERLAPPED                      ov_write;
 };
 
+static void
+altos_set_last_windows_error(void)
+{
+       DWORD   error = GetLastError();
+       TCHAR   message[1024];
+       FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
+                     0,
+                     error,
+                     0,
+                     message,
+                     sizeof (message) / sizeof (TCHAR),
+                     NULL);
+       altos_set_last_error(error, message);
+}
+
 PUBLIC struct altos_list *
 altos_list_start(void)
 {
@@ -922,7 +969,7 @@ altos_list_start(void)
        list->dev_info = SetupDiGetClassDevs(NULL, "USB", NULL,
                                             DIGCF_ALLCLASSES|DIGCF_PRESENT);
        if (list->dev_info == INVALID_HANDLE_VALUE) {
-               printf("SetupDiGetClassDevs failed %ld\n", GetLastError());
+               altos_set_last_windows_error();
                free(list);
                return NULL;
        }
@@ -956,6 +1003,7 @@ altos_list_next(struct altos_list *list, struct altos_device *device)
                                               DICS_FLAG_GLOBAL, 0, DIREG_DEV,
                                               KEY_READ);
                if (dev_key == INVALID_HANDLE_VALUE) {
+                       altos_set_last_windows_error();
                        printf("cannot open device registry key\n");
                        continue;
                }
@@ -966,6 +1014,7 @@ altos_list_next(struct altos_list *list, struct altos_device *device)
                result = RegQueryValueEx(dev_key, "SymbolicName", NULL, NULL,
                                         symbolic, &symbolic_len);
                if (result != 0) {
+                       altos_set_last_windows_error();
                        printf("cannot find SymbolicName value\n");
                        RegCloseKey(dev_key);
                        continue;
@@ -988,6 +1037,7 @@ altos_list_next(struct altos_list *list, struct altos_device *device)
                                         port, &port_len);
                RegCloseKey(dev_key);
                if (result != 0) {
+                       altos_set_last_windows_error();
                        printf("failed to get PortName\n");
                        continue;
                }
@@ -1003,6 +1053,7 @@ altos_list_next(struct altos_list *list, struct altos_device *device)
                                                     sizeof(friendlyname),
                                                     &friendlyname_len))
                {
+                       altos_set_last_windows_error();
                        printf("Failed to get friendlyname\n");
                        continue;
                }
@@ -1015,8 +1066,10 @@ altos_list_next(struct altos_list *list, struct altos_device *device)
                return 1;
        }
        result = GetLastError();
-       if (result != ERROR_NO_MORE_ITEMS)
+       if (result != ERROR_NO_MORE_ITEMS) {
+               altos_set_last_windows_error();
                printf ("SetupDiEnumDeviceInfo failed error %d\n", (int) result);
+       }
        return 0;
 }
 
@@ -1035,8 +1088,10 @@ altos_queue_read(struct altos_file *file)
                return LIBALTOS_SUCCESS;
 
        if (!ReadFile(file->handle, file->in_data, USB_BUF_SIZE, &got, &file->ov_read)) {
-               if (GetLastError() != ERROR_IO_PENDING)
+               if (GetLastError() != ERROR_IO_PENDING) {
+                       altos_set_last_windows_error();
                        return LIBALTOS_ERROR;
+               }
                file->pend_read = TRUE;
        } else {
                file->pend_read = FALSE;
@@ -1061,8 +1116,10 @@ altos_wait_read(struct altos_file *file, int timeout)
        ret = WaitForSingleObject(file->ov_read.hEvent, timeout);
        switch (ret) {
        case WAIT_OBJECT_0:
-               if (!GetOverlappedResult(file->handle, &file->ov_read, &got, FALSE))
+               if (!GetOverlappedResult(file->handle, &file->ov_read, &got, FALSE)) {
+                       altos_set_last_windows_error();
                        return LIBALTOS_ERROR;
+               }
                file->pend_read = FALSE;
                file->in_read = 0;
                file->in_used = got;
@@ -1106,15 +1163,20 @@ altos_flush(struct altos_file *file)
 
        while (used) {
                if (!WriteFile(file->handle, data, used, &put, &file->ov_write)) {
-                       if (GetLastError() != ERROR_IO_PENDING)
+                       if (GetLastError() != ERROR_IO_PENDING) {
+                               altos_set_last_windows_error();
                                return LIBALTOS_ERROR;
+                       }
                        ret = WaitForSingleObject(file->ov_write.hEvent, INFINITE);
                        switch (ret) {
                        case WAIT_OBJECT_0:
-                               if (!GetOverlappedResult(file->handle, &file->ov_write, &put, FALSE))
+                               if (!GetOverlappedResult(file->handle, &file->ov_write, &put, FALSE)) {
+                                       altos_set_last_windows_error();
                                        return LIBALTOS_ERROR;
+                               }
                                break;
                        default:
+                               altos_set_last_windows_error();
                                return LIBALTOS_ERROR;
                        }
                }
@@ -1142,6 +1204,7 @@ altos_open(struct altos_device *device)
                                  0, NULL, OPEN_EXISTING,
                                  FILE_FLAG_OVERLAPPED, NULL);
        if (file->handle == INVALID_HANDLE_VALUE) {
+               altos_set_last_windows_error();
                free(file);
                return NULL;
        }
@@ -1157,6 +1220,7 @@ altos_open(struct altos_device *device)
 
        dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
        if (!GetCommState(file->handle, &dcbSerialParams)) {
+               altos_set_last_windows_error();
                CloseHandle(file->handle);
                free(file);
                return NULL;
@@ -1166,6 +1230,7 @@ altos_open(struct altos_device *device)
        dcbSerialParams.StopBits = ONESTOPBIT;
        dcbSerialParams.Parity = NOPARITY;
        if (!SetCommState(file->handle, &dcbSerialParams)) {
+               altos_set_last_windows_error();
                CloseHandle(file->handle);
                free(file);
                return NULL;
index a05bed4c2e6c9038873461e4c9f5c630f1770506..f90fbb879d3934a2cf1ba7251aa50dd3553cf878 100644 (file)
@@ -51,6 +51,11 @@ struct altos_bt_device {
        //%mutable;
 };
 
+struct altos_error {
+       int                             code;
+       char                            string[1024];
+};
+
 #define LIBALTOS_SUCCESS       0
 #define LIBALTOS_ERROR         -1
 #define LIBALTOS_TIMEOUT       -2
@@ -62,6 +67,9 @@ altos_init(void);
 PUBLIC void
 altos_fini(void);
 
+PUBLIC void
+altos_get_last_error(struct altos_error *error);
+
 PUBLIC struct altos_list *
 altos_list_start(void);