| 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 |
|
|---|
| 29 | /** @addtogroup libusbdev
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * USB device polling functions.
|
|---|
| 34 | */
|
|---|
| 35 | #ifndef LIBUSBDEV_POLL_H_
|
|---|
| 36 | #define LIBUSBDEV_POLL_H_
|
|---|
| 37 |
|
|---|
| 38 | #include <usb/usb.h>
|
|---|
| 39 | #include <usb/dev/device.h>
|
|---|
| 40 | #include <usb/dev/pipes.h>
|
|---|
| 41 |
|
|---|
| 42 | #include <stdbool.h>
|
|---|
| 43 | #include <stddef.h>
|
|---|
| 44 | #include <stdint.h>
|
|---|
| 45 |
|
|---|
| 46 | /** Parameters and callbacks for automated polling. */
|
|---|
| 47 | typedef struct {
|
|---|
| 48 | /** Level of debugging messages from auto polling.
|
|---|
| 49 | * 0 - nothing
|
|---|
| 50 | * 1 - inform about errors and polling start/end
|
|---|
| 51 | * 2 - also dump every retrieved buffer
|
|---|
| 52 | */
|
|---|
| 53 | int debug;
|
|---|
| 54 | /** Maximum number of consecutive errors before polling termination. */
|
|---|
| 55 | size_t max_failures;
|
|---|
| 56 | /** Delay between poll requests in milliseconds.
|
|---|
| 57 | * Set to negative value to use value from endpoint descriptor.
|
|---|
| 58 | */
|
|---|
| 59 | int delay;
|
|---|
| 60 | /** Whether to automatically try to clear the HALT feature after
|
|---|
| 61 | * the endpoint stalls.
|
|---|
| 62 | */
|
|---|
| 63 | bool auto_clear_halt;
|
|---|
| 64 | /** Callback when data arrives.
|
|---|
| 65 | *
|
|---|
| 66 | * @param dev Device that was polled.
|
|---|
| 67 | * @param data Data buffer (in USB endianness).
|
|---|
| 68 | * @param data_size Size of the @p data buffer in bytes.
|
|---|
| 69 | * @param arg Custom argument.
|
|---|
| 70 | * @return Whether to continue in polling.
|
|---|
| 71 | */
|
|---|
| 72 | bool (*on_data)(usb_device_t *dev, uint8_t *data, size_t data_size,
|
|---|
| 73 | void *arg);
|
|---|
| 74 | /** Callback when polling is terminated.
|
|---|
| 75 | *
|
|---|
| 76 | * @param dev Device where the polling was terminated.
|
|---|
| 77 | * @param due_to_errors Whether polling stopped due to several failures.
|
|---|
| 78 | * @param arg Custom argument.
|
|---|
| 79 | */
|
|---|
| 80 | void (*on_polling_end)(usb_device_t *dev, bool due_to_errors,
|
|---|
| 81 | void *arg);
|
|---|
| 82 | /** Callback when error occurs.
|
|---|
| 83 | *
|
|---|
| 84 | * @param dev Device where error occurred.
|
|---|
| 85 | * @param err_code Error code (as returned from usb_pipe_read).
|
|---|
| 86 | * @param arg Custom argument.
|
|---|
| 87 | * @return Whether to continue in polling.
|
|---|
| 88 | */
|
|---|
| 89 | bool (*on_error)(usb_device_t *dev, int err_code, void *arg);
|
|---|
| 90 | /** Argument to pass to callbacks. */
|
|---|
| 91 | void *arg;
|
|---|
| 92 | } usb_device_auto_polling_t;
|
|---|
| 93 |
|
|---|
| 94 | typedef bool (*usb_polling_callback_t)(usb_device_t *, uint8_t *, size_t, void *);
|
|---|
| 95 | typedef void (*usb_polling_terminted_callback_t)(usb_device_t *, bool, void *);
|
|---|
| 96 |
|
|---|
| 97 | extern int usb_device_auto_polling(usb_device_t *, usb_endpoint_t,
|
|---|
| 98 | const usb_device_auto_polling_t *, size_t);
|
|---|
| 99 |
|
|---|
| 100 | extern int usb_device_auto_poll(usb_device_t *, usb_endpoint_t,
|
|---|
| 101 | usb_polling_callback_t, size_t, int, usb_polling_terminted_callback_t, void *);
|
|---|
| 102 |
|
|---|
| 103 | extern int usb_device_auto_polling_desc(usb_device_t *,
|
|---|
| 104 | const usb_endpoint_description_t *, const usb_device_auto_polling_t *,
|
|---|
| 105 | size_t);
|
|---|
| 106 |
|
|---|
| 107 | extern int usb_device_auto_poll_desc(usb_device_t *,
|
|---|
| 108 | const usb_endpoint_description_t *, usb_polling_callback_t, size_t, int,
|
|---|
| 109 | usb_polling_terminted_callback_t, void *);
|
|---|
| 110 |
|
|---|
| 111 | #endif
|
|---|
| 112 | /**
|
|---|
| 113 | * @}
|
|---|
| 114 | */
|
|---|