X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=libaltos%2Flibaltos.c;h=fc949c7020a2e8143f5c5f83238ebda3cb4b1003;hp=2a41ef80159e1813f7573672fe049a69d1c78a65;hb=bd8d061d0f63158b5b03814d77cb76fdf5a0abad;hpb=84d35e4cbd7ea2f681c43496b9b9db84f9dd923f diff --git a/libaltos/libaltos.c b/libaltos/libaltos.c index 2a41ef80..fc949c70 100644 --- a/libaltos/libaltos.c +++ b/libaltos/libaltos.c @@ -736,30 +736,36 @@ 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); + usleep(100 * 1000); #ifdef USE_POLL pipe(file->pipe); @@ -768,7 +774,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: @@ -1319,6 +1325,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 +1333,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;