1 /* 2 * Windows compat: POSIX compatibility wrapper 3 * Copyright 2012-2013 RealVNC Ltd. 4 * Copyright 2009-2010 Pete Batard <pete (at) akeo.ie> 5 * With contributions from Michael Plante, Orin Eman et al. 6 * Parts of poll implementation from libusb-win32, by Stephan Meyer et al. 7 * 8 * This library is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public 10 * License as published by the Free Software Foundation; either 11 * version 2.1 of the License, or (at your option) any later version. 12 * 13 * This library is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with this library; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 21 * 22 */ 23 #pragma once 24 25 #if defined(_MSC_VER) 26 // disable /W4 MSVC warnings that are benign 27 #pragma warning(disable:4127) // conditional expression is constant 28 #endif 29 30 // Handle synchronous completion through the overlapped structure 31 #if !defined(STATUS_REPARSE) // reuse the REPARSE status code 32 #define STATUS_REPARSE ((LONG)0x00000104L) 33 #endif 34 #define STATUS_COMPLETED_SYNCHRONOUSLY STATUS_REPARSE 35 #if defined(_WIN32_WCE) 36 // WinCE doesn't have a HasOverlappedIoCompleted() macro, so attempt to emulate it 37 #define HasOverlappedIoCompleted(lpOverlapped) (((DWORD)(lpOverlapped)->Internal) != STATUS_PENDING) 38 #endif 39 #define HasOverlappedIoCompletedSync(lpOverlapped) (((DWORD)(lpOverlapped)->Internal) == STATUS_COMPLETED_SYNCHRONOUSLY) 40 41 #define DUMMY_HANDLE ((HANDLE)(LONG_PTR)-2) 42 43 /* Windows versions */ 44 enum windows_version { 45 WINDOWS_CE = -2, 46 WINDOWS_UNDEFINED = -1, 47 WINDOWS_UNSUPPORTED = 0, 48 WINDOWS_XP = 0x51, 49 WINDOWS_2003 = 0x52, // Also XP x64 50 WINDOWS_VISTA = 0x60, 51 WINDOWS_7 = 0x61, 52 WINDOWS_8 = 0x62, 53 WINDOWS_8_1_OR_LATER = 0x63, 54 WINDOWS_MAX 55 }; 56 extern int windows_version; 57 58 #define MAX_FDS 256 59 60 #define POLLIN 0x0001 /* There is data to read */ 61 #define POLLPRI 0x0002 /* There is urgent data to read */ 62 #define POLLOUT 0x0004 /* Writing now will not block */ 63 #define POLLERR 0x0008 /* Error condition */ 64 #define POLLHUP 0x0010 /* Hung up */ 65 #define POLLNVAL 0x0020 /* Invalid request: fd not open */ 66 67 struct pollfd { 68 int fd; /* file descriptor */ 69 short events; /* requested events */ 70 short revents; /* returned events */ 71 }; 72 73 // access modes 74 enum rw_type { 75 RW_NONE, 76 RW_READ, 77 RW_WRITE, 78 }; 79 80 // fd struct that can be used for polling on Windows 81 typedef int cancel_transfer(struct usbi_transfer *itransfer); 82 83 struct winfd { 84 int fd; // what's exposed to libusb core 85 HANDLE handle; // what we need to attach overlapped to the I/O op, so we can poll it 86 OVERLAPPED* overlapped; // what will report our I/O status 87 struct usbi_transfer *itransfer; // Associated transfer, or NULL if completed 88 cancel_transfer *cancel_fn; // Function pointer to cancel transfer API 89 enum rw_type rw; // I/O transfer direction: read *XOR* write (NOT BOTH) 90 }; 91 extern const struct winfd INVALID_WINFD; 92 93 int usbi_pipe(int pipefd[2]); 94 int usbi_poll(struct pollfd *fds, unsigned int nfds, int timeout); 95 ssize_t usbi_write(int fd, const void *buf, size_t count); 96 ssize_t usbi_read(int fd, void *buf, size_t count); 97 int usbi_close(int fd); 98 99 void init_polling(void); 100 void exit_polling(void); 101 struct winfd usbi_create_fd(HANDLE handle, int access_mode, 102 struct usbi_transfer *transfer, cancel_transfer *cancel_fn); 103 void usbi_free_fd(struct winfd* winfd); 104 struct winfd fd_to_winfd(int fd); 105 struct winfd handle_to_winfd(HANDLE handle); 106 struct winfd overlapped_to_winfd(OVERLAPPED* overlapped); 107 108 /* 109 * Timeval operations 110 */ 111 #if defined(DDKBUILD) 112 #include <winsock.h> // defines timeval functions on DDK 113 #endif 114 115 #if !defined(TIMESPEC_TO_TIMEVAL) 116 #define TIMESPEC_TO_TIMEVAL(tv, ts) { \ 117 (tv)->tv_sec = (long)(ts)->tv_sec; \ 118 (tv)->tv_usec = (long)(ts)->tv_nsec / 1000; \ 119 } 120 #endif 121 #if !defined(timersub) 122 #define timersub(a, b, result) \ 123 do { \ 124 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ 125 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \ 126 if ((result)->tv_usec < 0) { \ 127 --(result)->tv_sec; \ 128 (result)->tv_usec += 1000000; \ 129 } \ 130 } while (0) 131 #endif 132