X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=libaltos%2Flibaltos.c;h=a623d5ae0b5c187f9b0c5a7bec962b1c73e2451d;hp=2a41ef80159e1813f7573672fe049a69d1c78a65;hb=c0966cd40f05f3a65b0c977b4b92586a58192f4b;hpb=784edcda52d681bbc9302fbc7efb80cb214f71b8 diff --git a/libaltos/libaltos.c b/libaltos/libaltos.c index 2a41ef80..a623d5ae 100644 --- a/libaltos/libaltos.c +++ b/libaltos/libaltos.c @@ -53,6 +53,8 @@ altos_get_last_error(struct altos_error *error) #ifdef DARWIN +#include + #undef USE_POLL /* Mac OS X don't have strndup even if _GNU_SOURCE is defined */ @@ -736,30 +738,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 +776,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: @@ -881,15 +889,6 @@ altos_list_next(struct altos_list *list, struct altos_device *device) if (!get_number (object, CFSTR(kUSBVendorID), &device->vendor) || !get_number (object, CFSTR(kUSBProductID), &device->product)) continue; - if (list->ftdi) { - if (device->vendor != 0x0403) - continue; - } else { - if (device->vendor != 0xfffe) - continue; - if (device->product < 0x000a || 0x0013 < device->product) - continue; - } if (get_string (object, CFSTR("IOCalloutDevice"), device->path, sizeof (device->path)) && get_string (object, CFSTR("USB Product Name"), device->name, sizeof (device->name)) && get_string (object, CFSTR("USB Serial Number"), serial_string, sizeof (serial_string))) { @@ -988,6 +987,11 @@ log_message(char *fmt, ...) if (!log) log = fopen("\\temp\\altos.txt", "w"); if (log) { + SYSTEMTIME time; + GetLocalTime(&time); + fprintf (log, "%4d-%02d-%02d %2d:%02d:%02d. ", + time.wYear, time.wMonth, time.wDay, + time.wHour, time.wMinute, time.wSecond); va_start(a, fmt); vfprintf(log, fmt, a); va_end(a); @@ -1319,6 +1323,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 +1331,16 @@ 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; + altos_set_last_windows_error(); + Sleep(100); + } + if (file->handle == INVALID_HANDLE_VALUE) { free(file); return NULL; @@ -1358,13 +1372,19 @@ altos_open(struct altos_device *device) PUBLIC void altos_close(struct altos_file *file) { - if (file->handle != INVALID_HANDLE_VALUE) { - CloseHandle(file->handle); + HANDLE handle = file->handle; + if (handle != INVALID_HANDLE_VALUE) { + HANDLE ov_read = file->ov_read.hEvent; + HANDLE ov_write = file->ov_write.hEvent; + file->handle = INVALID_HANDLE_VALUE; + file->ov_read.hEvent = INVALID_HANDLE_VALUE; + file->ov_write.hEvent = INVALID_HANDLE_VALUE; + PurgeComm(handle, PURGE_RXABORT|PURGE_RXCLEAR|PURGE_TXABORT|PURGE_TXCLEAR); + Sleep(100); + CloseHandle(handle); file->handle = INVALID_HANDLE_VALUE; - SetEvent(file->ov_read.hEvent); - SetEvent(file->ov_write.hEvent); - CloseHandle(file->ov_read.hEvent); - CloseHandle(file->ov_write.hEvent); + CloseHandle(ov_read); + CloseHandle(ov_write); } }