libaltos: Delay freeing serial device until not busy
[fw/altos] / libaltos / libaltos_common.c
index 713a775c6f70c9acee6011705b5aa6256cd06bc2..6f0cbe6137d898ce2998de3628eb46213cd78575 100644 (file)
@@ -49,12 +49,17 @@ PUBLIC int
 altos_getchar(struct altos_file *file, int timeout)
 {
        int     ret;
+
+       file->busy = 1;
        while (file->in_read == file->in_used) {
                ret = altos_fill(file, timeout);
                if (ret)
-                       return ret;
+                       goto done;
        }
-       return file->in_data[file->in_read++];
+       ret = file->in_data[file->in_read++];
+done:
+       file->busy = 0;
+       return ret;
 }
 
 PUBLIC int
@@ -112,9 +117,16 @@ int altos_bt_port(struct altos_bt_device *device) {
        return BT_PORT_DEFAULT;
 }
 
+#include <time.h>
+
 PUBLIC void
 altos_free(struct altos_file *file)
 {
+       int i;
        altos_close(file);
+       for (i = 0; i < 10 && file->busy; i++) {
+               struct timespec delay = { .tv_sec = 1, .tv_nsec = 0 };
+               nanosleep(&delay, NULL);
+       }
        free(file);
 }