altosui: Close serial, join reader thread, free altos_file
[fw/altos] / ao-tools / libaltos / libaltos.c
index df0d5b2eba6b21f99ea35e9196bed7771565cd6a..00fb2125443bc452b9807f8f106d81b726dd30b9 100644 (file)
@@ -567,6 +567,14 @@ void
 altos_close(struct altos_file *file)
 {
        close(file->fd);
+       file->fd = -1;
+}
+
+void
+altos_free(struct altos_file *file)
+{
+       if (file->fd != -1)
+               close(file->fd);
        free(file);
 }
 
@@ -592,6 +600,8 @@ altos_flush(struct altos_file *file)
        while (file->out_used) {
                int     ret;
 
+               if (file->fd < 0)
+                       return -EBADF;
                ret = write (file->fd, file->out_data, file->out_used);
                if (ret < 0)
                        return -errno;
@@ -610,6 +620,8 @@ altos_getchar(struct altos_file *file, int timeout)
                int     ret;
 
                altos_flush(file);
+               if (file->fd < 0)
+                       return -EBADF;
                ret = read(file->fd, file->in_data, USB_BUF_SIZE);
                if (ret < 0)
                        return -errno;