X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=libaltos%2Flibaltos.c;h=4e3bc2c51b2d5b1f7fcd2eb9c8d090118657a13a;hp=2a41ef80159e1813f7573672fe049a69d1c78a65;hb=5872bd10df14b47de0e541bff16d9220af0558aa;hpb=760b1f02c178c600226f39b5e66d8cbadbf4a29b diff --git a/libaltos/libaltos.c b/libaltos/libaltos.c index 2a41ef80..4e3bc2c5 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 */ @@ -641,6 +643,49 @@ altos_list_finish(struct altos_list *usbdevs) free(usbdevs); } +#include + +static void *libbt; +static int bt_initialized; + +static int init_bt(void) { + if (!bt_initialized) { + bt_initialized = 1; + libbt = dlopen("libbluetooth.so.3", RTLD_LAZY); + if (!libbt) + printf("failed to find bluetooth library\n"); + } + return libbt != NULL; +} + +#define join(a,b) a ## b +#define bt_func(name, ret, fail, formals, actuals) \ + static ret join(altos_, name) formals { \ + static ret (*name) formals; \ + if (!init_bt()) return fail; \ + name = dlsym(libbt, #name); \ + if (!name) return fail; \ + return name actuals; \ + } + +bt_func(ba2str, int, -1, (const bdaddr_t *ba, char *str), (ba, str)) +#define ba2str altos_ba2str + +bt_func(str2ba, int, -1, (const char *str, bdaddr_t *ba), (str, ba)) +#define str2ba altos_str2ba + +bt_func(hci_read_remote_name, int, -1, (int sock, const bdaddr_t *ba, int len, char *name, int timeout), (sock, ba, len, name, timeout)) +#define hci_read_remote_name altos_hci_read_remote_name + +bt_func(hci_open_dev, int, -1, (int dev_id), (dev_id)) +#define hci_open_dev altos_hci_open_dev + +bt_func(hci_get_route, int, -1, (bdaddr_t *bdaddr), (bdaddr)) +#define hci_get_route altos_hci_get_route + +bt_func(hci_inquiry, int, -1, (int adapter_id, int len, int max_rsp, const uint8_t *lap, inquiry_info **devs, long flags), (adapter_id, len, max_rsp, lap, devs, flags)) +#define hci_inquiry altos_hci_inquiry + struct altos_bt_list { inquiry_info *ii; int sock; @@ -704,7 +749,8 @@ altos_bt_list_next(struct altos_bt_list *bt_list, return 0; ii = &bt_list->ii[bt_list->rsp]; - ba2str(&ii->bdaddr, device->addr); + if (ba2str(&ii->bdaddr, device->addr) < 0) + return 0; memset(&device->name, '\0', sizeof (device->name)); if (hci_read_remote_name(bt_list->sock, &ii->bdaddr, sizeof (device->name), @@ -736,30 +782,42 @@ 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) + if (!file) { + errno = ENOMEM; + altos_set_last_posix_error(); goto no_file; - file->fd = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); - if (file->fd < 0) { + } + addr.rc_family = AF_BLUETOOTH; + addr.rc_channel = 1; + if (str2ba(device->addr, &addr.rc_bdaddr) < 0) { altos_set_last_posix_error(); goto no_sock; } - addr.rc_family = AF_BLUETOOTH; - addr.rc_channel = 1; - str2ba(device->addr, &addr.rc_bdaddr); + 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)); + 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 +826,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: @@ -809,7 +867,7 @@ get_string(io_object_t object, CFStringRef entry, char *result, int result_len) got_string = CFStringGetCString(entry_as_string, result, result_len, kCFStringEncodingASCII); - + CFRelease(entry_as_string); if (got_string) return 1; @@ -822,7 +880,7 @@ get_number(io_object_t object, CFStringRef entry, int *result) { CFTypeRef entry_as_number; Boolean got_number; - + entry_as_number = IORegistryEntrySearchCFProperty (object, kIOServicePlane, entry, @@ -877,19 +935,10 @@ altos_list_next(struct altos_list *list, struct altos_device *device) object = IOIteratorNext(list->iterator); if (!object) return 0; - + 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 +1037,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 +1373,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 +1381,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 +1422,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); } }