[5e07e2b5] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[160b75e] | 29 | /** @addtogroup libusbdev
|
---|
[5e07e2b5] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * USB device polling functions.
|
---|
| 34 | */
|
---|
[7d521e24] | 35 | #ifndef LIBUSBDEV_POLL_H_
|
---|
| 36 | #define LIBUSBDEV_POLL_H_
|
---|
[5e07e2b5] | 37 |
|
---|
[7d521e24] | 38 | #include <usb/dev/driver.h>
|
---|
[8989f48f] | 39 | #include <time.h>
|
---|
| 40 |
|
---|
[6e3c005] | 41 | /** Parameters and callbacks for automated polling. */
|
---|
[8989f48f] | 42 | typedef struct {
|
---|
[5e168be1] | 43 | /** Level of debugging messages from auto polling.
|
---|
| 44 | * 0 - nothing
|
---|
| 45 | * 1 - inform about errors and polling start/end
|
---|
| 46 | * 2 - also dump every retrieved buffer
|
---|
| 47 | */
|
---|
| 48 | int debug;
|
---|
[8989f48f] | 49 | /** Maximum number of consecutive errors before polling termination. */
|
---|
| 50 | size_t max_failures;
|
---|
| 51 | /** Delay between poll requests in milliseconds.
|
---|
| 52 | * Set to negative value to use value from endpoint descriptor.
|
---|
| 53 | */
|
---|
| 54 | int delay;
|
---|
| 55 | /** Whether to automatically try to clear the HALT feature after
|
---|
| 56 | * the endpoint stalls.
|
---|
| 57 | */
|
---|
| 58 | bool auto_clear_halt;
|
---|
| 59 | /** Callback when data arrives.
|
---|
| 60 | *
|
---|
| 61 | * @param dev Device that was polled.
|
---|
| 62 | * @param data Data buffer (in USB endianness).
|
---|
| 63 | * @param data_size Size of the @p data buffer in bytes.
|
---|
| 64 | * @param arg Custom argument.
|
---|
| 65 | * @return Whether to continue in polling.
|
---|
| 66 | */
|
---|
| 67 | bool (*on_data)(usb_device_t *dev, uint8_t *data, size_t data_size,
|
---|
| 68 | void *arg);
|
---|
| 69 | /** Callback when polling is terminated.
|
---|
| 70 | *
|
---|
| 71 | * @param dev Device where the polling was terminated.
|
---|
| 72 | * @param due_to_errors Whether polling stopped due to several failures.
|
---|
| 73 | * @param arg Custom argument.
|
---|
| 74 | */
|
---|
| 75 | void (*on_polling_end)(usb_device_t *dev, bool due_to_errors,
|
---|
| 76 | void *arg);
|
---|
| 77 | /** Callback when error occurs.
|
---|
| 78 | *
|
---|
| 79 | * @param dev Device where error occurred.
|
---|
| 80 | * @param err_code Error code (as returned from usb_pipe_read).
|
---|
| 81 | * @param arg Custom argument.
|
---|
| 82 | * @return Whether to continue in polling.
|
---|
| 83 | */
|
---|
| 84 | bool (*on_error)(usb_device_t *dev, int err_code, void *arg);
|
---|
[a0487a2] | 85 | /** Argument to pass to callbacks. */
|
---|
| 86 | void *arg;
|
---|
[8989f48f] | 87 | } usb_device_auto_polling_t;
|
---|
| 88 |
|
---|
[99a1a56] | 89 | int usb_device_auto_polling(usb_device_t *, size_t,
|
---|
[a0487a2] | 90 | const usb_device_auto_polling_t *, size_t);
|
---|
[5e07e2b5] | 91 |
|
---|
| 92 | typedef bool (*usb_polling_callback_t)(usb_device_t *,
|
---|
| 93 | uint8_t *, size_t, void *);
|
---|
| 94 | typedef void (*usb_polling_terminted_callback_t)(usb_device_t *, bool, void *);
|
---|
| 95 |
|
---|
| 96 | int usb_device_auto_poll(usb_device_t *, size_t,
|
---|
[ea30cc1] | 97 | usb_polling_callback_t, size_t, int, usb_polling_terminted_callback_t, void *);
|
---|
[5e07e2b5] | 98 |
|
---|
| 99 | #endif
|
---|
| 100 | /**
|
---|
| 101 | * @}
|
---|
| 102 | */
|
---|