libaltos: Try Bluetooth open 5 times on EBUSY
authorKeith Packard <keithp@keithp.com>
Sun, 21 Apr 2013 03:05:01 +0000 (22:05 -0500)
committerKeith Packard <keithp@keithp.com>
Sun, 21 Apr 2013 03:05:01 +0000 (22:05 -0500)
After closing Bluetooth, it can take a second before the device is up
for another connection. Hang around retrying a few times.

Signed-off-by: Keith Packard <keithp@keithp.com>
libaltos/libaltos.c
src/stm-bringup/Makefile
src/stm/Makefile.defs

index ad03e638e2a4937e0c7bd913afef88e8fcb862f9..69a6735f50e72febc28db62726d9f80760d2a8f7 100644 (file)
@@ -736,30 +736,35 @@ struct altos_file *
 altos_bt_open(struct altos_bt_device *device)
 {
        struct sockaddr_rc addr = { 0 };
-       int     s, status;
+       int     status, i;
        struct altos_file *file;
 
        file = calloc(1, sizeof (struct altos_file));
        if (!file)
                goto no_file;
-       file->fd = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
-       if (file->fd < 0) {
-               altos_set_last_posix_error();
-               goto no_sock;
-       }
-
        addr.rc_family = AF_BLUETOOTH;
        addr.rc_channel = 1;
        str2ba(device->addr, &addr.rc_bdaddr);
 
-       status = connect(file->fd,
-                        (struct sockaddr *)&addr,
-                        sizeof(addr));
+       for (i = 0; i < 5; i++) {
+               file->fd = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
+               if (file->fd < 0) {
+                       altos_set_last_posix_error();
+                       goto no_sock;
+               }
+
+               status = connect(file->fd,
+                                (struct sockaddr *)&addr,
+                                sizeof(addr));
+               if (status >= 0 || errno != EBUSY)
+                       break;
+               close(file->fd);
+               usleep(100 * 1000);
+       }
        if (status < 0) {
                altos_set_last_posix_error();
                goto no_link;
        }
-       sleep(1);
 
 #ifdef USE_POLL
        pipe(file->pipe);
@@ -768,7 +773,7 @@ altos_bt_open(struct altos_bt_device *device)
 #endif
        return file;
 no_link:
-       close(s);
+       close(file->fd);
 no_sock:
        free(file);
 no_file:
index 5cc94bd969f64a3a147461bbe7489ef2416115a3..f4e77046b200d6784a28373a41de6995c898dce5 100644 (file)
@@ -8,8 +8,10 @@ endif
 CC=arm-none-eabi-gcc
 OBJCOPY=arm-none-eabi-objcopy
 
-PDCLIB=/home/keithp/sat
-C_LIB=$(PDCLIB)/lib/pdclib.a
+#PDCLIB=/home/keithp/sat
+#C_LIB=$(PDCLIB)/lib/pdclib.a
+PDCLIB=/opt/sat
+C_LIB=$(PDCLIB)/lib/pdclib-cortex-m3.a
 C_INC=-I$(PDCLIB)/include
 
 DEF_CFLAGS=-g -std=gnu99 -Os -mlittle-endian -mthumb -ffreestanding -nostdlib -I. -I../../src/stm $(C_INC)
index 04404cdcda11b3a86c400becabfcfdee7721cd57..e4a154c539a9e7b80abb998dceddb5658a0cfc97 100644 (file)
@@ -8,8 +8,10 @@ vpath matrix.5c ../kalman
 vpath ao-make-product.5c ../util
 
 CC=arm-none-eabi-gcc
-SAT=/home/keithp/sat
-SAT_CLIB=$(SAT)/lib/pdclib.a
+#SAT=/home/keithp/sat
+#SAT_CLIB=$(SAT)/lib/pdclib.a
+SAT=/opt/sat
+SAT_CLIB=$(SAT)/lib/pdclib-cortex-m3.a
 SAT_CFLAGS=-I$(SAT)/include
 
 ifndef VERSION