From 25c01719f17be8da73a859867c14df0fc29b5441 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 11 Apr 2013 22:16:03 -0700 Subject: libaltos: Retry Windows serial port open five times Maybe this helps? Signed-off-by: Keith Packard --- libaltos/libaltos.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'libaltos') diff --git a/libaltos/libaltos.c b/libaltos/libaltos.c index 2a41ef80..ad03e638 100644 --- a/libaltos/libaltos.c +++ b/libaltos/libaltos.c @@ -1319,6 +1319,7 @@ altos_open(struct altos_device *device) struct altos_file *file = calloc (1, sizeof (struct altos_file)); char full_name[64]; COMMTIMEOUTS timeouts; + int i; if (!file) return NULL; @@ -1326,7 +1327,15 @@ altos_open(struct altos_device *device) strcpy(full_name, "\\\\.\\"); strcat(full_name, device->path); - file->handle = open_serial(full_name); + file->handle = INVALID_HANDLE_VALUE; + + for (i = 0; i < 5; i++) { + file->handle = open_serial(full_name); + if (file->handle != INVALID_HANDLE_VALUE) + break; + Sleep(100); + } + if (file->handle == INVALID_HANDLE_VALUE) { free(file); return NULL; -- cgit v1.2.3 From 02a564bbc3a23b4f90685e8b29083ddb3e4b3563 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 20 Apr 2013 22:05:01 -0500 Subject: libaltos: Try Bluetooth open 5 times on EBUSY 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 --- libaltos/libaltos.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'libaltos') diff --git a/libaltos/libaltos.c b/libaltos/libaltos.c index ad03e638..69a6735f 100644 --- a/libaltos/libaltos.c +++ b/libaltos/libaltos.c @@ -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: -- cgit v1.2.3 From 2e28d3541b8da31ebef5a199baf8f544d238298e Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 20 Apr 2013 22:16:28 -0500 Subject: libaltos: Delay after opening bluetooth device on linux Writes immediately after the open disappear sometimes. Signed-off-by: Keith Packard --- libaltos/libaltos.c | 1 + 1 file changed, 1 insertion(+) (limited to 'libaltos') diff --git a/libaltos/libaltos.c b/libaltos/libaltos.c index 69a6735f..fc949c70 100644 --- a/libaltos/libaltos.c +++ b/libaltos/libaltos.c @@ -765,6 +765,7 @@ altos_bt_open(struct altos_bt_device *device) altos_set_last_posix_error(); goto no_link; } + usleep(100 * 1000); #ifdef USE_POLL pipe(file->pipe); -- cgit v1.2.3