|
Packit Service |
b0a153 |
/*
|
|
Packit Service |
b0a153 |
* Windows compat: POSIX compatibility wrapper
|
|
Packit Service |
b0a153 |
* Copyright © 2012-2013 RealVNC Ltd.
|
|
Packit Service |
b0a153 |
* Copyright © 2009-2010 Pete Batard <pete@akeo.ie>
|
|
Packit Service |
b0a153 |
* Copyright © 2016-2018 Chris Dickens <christopher.a.dickens@gmail.com>
|
|
Packit Service |
b0a153 |
* With contributions from Michael Plante, Orin Eman et al.
|
|
Packit Service |
b0a153 |
* Parts of poll implementation from libusb-win32, by Stephan Meyer et al.
|
|
Packit Service |
b0a153 |
*
|
|
Packit Service |
b0a153 |
* This library is free software; you can redistribute it and/or
|
|
Packit Service |
b0a153 |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
b0a153 |
* License as published by the Free Software Foundation; either
|
|
Packit Service |
b0a153 |
* version 2.1 of the License, or (at your option) any later version.
|
|
Packit Service |
b0a153 |
*
|
|
Packit Service |
b0a153 |
* This library is distributed in the hope that it will be useful,
|
|
Packit Service |
b0a153 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
b0a153 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
b0a153 |
* Lesser General Public License for more details.
|
|
Packit Service |
b0a153 |
*
|
|
Packit Service |
b0a153 |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
b0a153 |
* License along with this library; if not, write to the Free Software
|
|
Packit Service |
b0a153 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
Packit Service |
b0a153 |
*
|
|
Packit Service |
b0a153 |
*/
|
|
Packit Service |
b0a153 |
#pragma once
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
#if defined(_MSC_VER)
|
|
Packit Service |
b0a153 |
// disable /W4 MSVC warnings that are benign
|
|
Packit Service |
b0a153 |
#pragma warning(disable:4127) // conditional expression is constant
|
|
Packit Service |
b0a153 |
#endif
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
// Handle synchronous completion through the overlapped structure
|
|
Packit Service |
b0a153 |
#if !defined(STATUS_REPARSE) // reuse the REPARSE status code
|
|
Packit Service |
b0a153 |
#define STATUS_REPARSE ((LONG)0x00000104L)
|
|
Packit Service |
b0a153 |
#endif
|
|
Packit Service |
b0a153 |
#define STATUS_COMPLETED_SYNCHRONOUSLY STATUS_REPARSE
|
|
Packit Service |
b0a153 |
#if defined(_WIN32_WCE)
|
|
Packit Service |
b0a153 |
// WinCE doesn't have a HasOverlappedIoCompleted() macro, so attempt to emulate it
|
|
Packit Service |
b0a153 |
#define HasOverlappedIoCompleted(lpOverlapped) (((DWORD)(lpOverlapped)->Internal) != STATUS_PENDING)
|
|
Packit Service |
b0a153 |
#endif
|
|
Packit Service |
b0a153 |
#define HasOverlappedIoCompletedSync(lpOverlapped) (((DWORD)(lpOverlapped)->Internal) == STATUS_COMPLETED_SYNCHRONOUSLY)
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
#define DUMMY_HANDLE ((HANDLE)(LONG_PTR)-2)
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
#define POLLIN 0x0001 /* There is data to read */
|
|
Packit Service |
b0a153 |
#define POLLPRI 0x0002 /* There is urgent data to read */
|
|
Packit Service |
b0a153 |
#define POLLOUT 0x0004 /* Writing now will not block */
|
|
Packit Service |
b0a153 |
#define POLLERR 0x0008 /* Error condition */
|
|
Packit Service |
b0a153 |
#define POLLHUP 0x0010 /* Hung up */
|
|
Packit Service |
b0a153 |
#define POLLNVAL 0x0020 /* Invalid request: fd not open */
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
struct pollfd {
|
|
Packit Service |
b0a153 |
int fd; /* file descriptor */
|
|
Packit Service |
b0a153 |
short events; /* requested events */
|
|
Packit Service |
b0a153 |
short revents; /* returned events */
|
|
Packit Service |
b0a153 |
};
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
struct winfd {
|
|
Packit Service |
b0a153 |
int fd; // what's exposed to libusb core
|
|
Packit Service |
b0a153 |
OVERLAPPED *overlapped; // what will report our I/O status
|
|
Packit Service |
b0a153 |
};
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
extern const struct winfd INVALID_WINFD;
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
struct winfd usbi_create_fd(void);
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
int usbi_pipe(int pipefd[2]);
|
|
Packit Service |
b0a153 |
int usbi_poll(struct pollfd *fds, unsigned int nfds, int timeout);
|
|
Packit Service |
b0a153 |
ssize_t usbi_write(int fd, const void *buf, size_t count);
|
|
Packit Service |
b0a153 |
ssize_t usbi_read(int fd, void *buf, size_t count);
|
|
Packit Service |
b0a153 |
int usbi_close(int fd);
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
void usbi_inc_fds_ref(struct pollfd *fds, unsigned int nfds);
|
|
Packit Service |
b0a153 |
void usbi_dec_fds_ref(struct pollfd *fds, unsigned int nfds);
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
/*
|
|
Packit Service |
b0a153 |
* Timeval operations
|
|
Packit Service |
b0a153 |
*/
|
|
Packit Service |
b0a153 |
#if defined(DDKBUILD)
|
|
Packit Service |
b0a153 |
#include <winsock.h> // defines timeval functions on DDK
|
|
Packit Service |
b0a153 |
#endif
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
#if !defined(TIMESPEC_TO_TIMEVAL)
|
|
Packit Service |
b0a153 |
#define TIMESPEC_TO_TIMEVAL(tv, ts) { \
|
|
Packit Service |
b0a153 |
(tv)->tv_sec = (long)(ts)->tv_sec; \
|
|
Packit Service |
b0a153 |
(tv)->tv_usec = (long)(ts)->tv_nsec / 1000; \
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
#endif
|
|
Packit Service |
b0a153 |
#if !defined(timersub)
|
|
Packit Service |
b0a153 |
#define timersub(a, b, result) \
|
|
Packit Service |
b0a153 |
do { \
|
|
Packit Service |
b0a153 |
(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
|
|
Packit Service |
b0a153 |
(result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
|
|
Packit Service |
b0a153 |
if ((result)->tv_usec < 0) { \
|
|
Packit Service |
b0a153 |
--(result)->tv_sec; \
|
|
Packit Service |
b0a153 |
(result)->tv_usec += 1000000; \
|
|
Packit Service |
b0a153 |
} \
|
|
Packit Service |
b0a153 |
} while (0)
|
|
Packit Service |
b0a153 |
#endif
|