|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * @file |
| 9 | + * @brief Descriptor matching and searching utilities |
| 10 | + * |
| 11 | + * This file contains utilities to browse USB descriptors returned by a device |
| 12 | + * according to the USB Specification 2.0 Chapter 9. |
| 13 | + */ |
| 14 | + |
| 15 | +#ifndef ZEPHYR_INCLUDE_USBH_DESC_H |
| 16 | +#define ZEPHYR_INCLUDE_USBH_DESC_H |
| 17 | + |
| 18 | +#include <stdint.h> |
| 19 | + |
| 20 | +#include <zephyr/usb/usbh.h> |
| 21 | + |
| 22 | +/** |
| 23 | + * @brief Get the next descriptor in an array of descriptors. |
| 24 | + * |
| 25 | + * This |
| 26 | + * to search either an interface or interface association descriptor: |
| 27 | + * |
| 28 | + * @code{.c} |
| 29 | + * BIT(USB_DESC_INTERFACE) | BIT(USB_DESC_INTERFACE_ASSOC) |
| 30 | + * @endcode |
| 31 | + * |
| 32 | + * @param[in] desc_beg Pointer to the beginning of the descriptor array; to search. May be NULL. |
| 33 | + * @param[in] desc_end Pointer after the end of the descriptor array to search |
| 34 | + * @param[in] type_mask Bitmask of bDescriptorType values to match |
| 35 | + * @return A pointer to the first descriptor matching |
| 36 | + */ |
| 37 | +const struct usb_desc_header *usbh_desc_get_next(const struct usb_desc_header *const desc, |
| 38 | + const void *const desc_end); |
| 39 | + |
| 40 | +/** |
| 41 | + * @brief Search the first descriptor matching the selected type(s). |
| 42 | + * |
| 43 | + * As an example, the @p type_mask argument can be constructed this way |
| 44 | + * to search either an interface or interface association descriptor: |
| 45 | + * |
| 46 | + * @code{.c} |
| 47 | + * BIT(USB_DESC_INTERFACE) | BIT(USB_DESC_INTERFACE_ASSOC) |
| 48 | + * @endcode |
| 49 | + * |
| 50 | + * @param[in] desc_beg Pointer to the beginning of the descriptor array; to search. May be NULL. |
| 51 | + * @param[in] desc_end Pointer after the end of the descriptor array to search |
| 52 | + * @param[in] type_mask Bitmask of bDescriptorType values to match |
| 53 | + * @return A pointer to the first descriptor matching |
| 54 | + */ |
| 55 | +const struct usb_desc_header *usbh_desc_get_by_type(const void *const desc_beg, |
| 56 | + const void *const desc_end, |
| 57 | + uint32_t type_mask); |
| 58 | + |
| 59 | +/** |
| 60 | + * @brief Search the first descriptor with the interace number wanted. |
| 61 | + * |
| 62 | + * The descriptors are going to be scanned until either an interface association |
| 63 | + * @c bFirstInterface field or an interface @c bInterfaceNumber field match. |
| 64 | + * |
| 65 | + * The descriptors following it can be browsed using @ref usbh_desc_get_next |
| 66 | + * |
| 67 | + * @param[in] desc_beg Pointer to the beginning of the descriptor array; to search. May be NULL. |
| 68 | + * @param[in] desc_end Pointer after the end of the descriptor array to search |
| 69 | + * @param[in] ifnum The interface number to search |
| 70 | + * @return A pointer to the first descriptor matching |
| 71 | + */ |
| 72 | +const struct usb_desc_header *usbh_desc_get_by_ifnum(const void *const desc_beg, |
| 73 | + const void *const desc_end, |
| 74 | + const uint8_t ifnum); |
| 75 | + |
| 76 | +#endif /* ZEPHYR_INCLUDE_USBH_DESC_H */ |
0 commit comments