ESP-IDF Firmware
Firmware architecture and call graph
Loading...
Searching...
No Matches
descriptors_control.c
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#include <string.h>
8#include "esp_log.h"
10
11static const char *TAG = "tusb_desc";
12static tusb_desc_device_t s_device_descriptor;
13static const uint8_t *s_configuration_descriptor;
15#define MAX_DESC_BUF_SIZE 32
16
17// =============================================================================
18// CALLBACKS
19// =============================================================================
20
27uint8_t const *tud_descriptor_device_cb(void)
28{
29 return (uint8_t const *)&s_device_descriptor;
30}
31
39uint8_t const *tud_descriptor_configuration_cb(uint8_t index)
40{
41 (void)index; // for multiple configurations
43}
44
46
47// Invoked when received GET STRING DESCRIPTOR request
48// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
49uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid)
50{
51 (void) langid;
52
53 uint8_t chr_count;
54
55 if ( index == 0) {
56 memcpy(&_desc_str[1], s_str_descriptor[0], 2);
57 chr_count = 1;
58 } else {
59 // Convert ASCII string into UTF-16
60
61 if ( index >= sizeof(s_str_descriptor) / sizeof(s_str_descriptor[0]) ) {
62 ESP_LOGE(TAG, "String index (%u) is out of bounds, check your string descriptor", index);
63 return NULL;
64 }
65
66 if (s_str_descriptor[index] == NULL) {
67 ESP_LOGE(TAG, "String index (%u) points to NULL, check your string descriptor", index);
68 return NULL;
69 }
70
71 const char *str = s_str_descriptor[index];
72
73 // Cap at max char
74 chr_count = strlen(str);
75 if ( chr_count > MAX_DESC_BUF_SIZE - 1 ) {
76 chr_count = MAX_DESC_BUF_SIZE - 1;
77 }
78
79 for (uint8_t i = 0; i < chr_count; i++) {
80 _desc_str[1 + i] = str[i];
81 }
82 }
83
84 // first byte is length (including header), second byte is string type
85 _desc_str[0] = (TUSB_DESC_STRING << 8 ) | (2 * chr_count + 2);
86
87 return _desc_str;
88}
89
90// =============================================================================
91// Driver functions
92// =============================================================================
93
94void tusb_set_descriptor(const tusb_desc_device_t *dev_desc, const char **str_desc, const uint8_t *cfg_desc)
95{
96 ESP_LOGI(TAG, "\n"
97 "┌─────────────────────────────────┐\n"
98 "│ USB Device Descriptor Summary │\n"
99 "├───────────────────┬─────────────┤\n"
100 "│bDeviceClass │ %-4u │\n"
101 "├───────────────────┼─────────────┤\n"
102 "│bDeviceSubClass │ %-4u │\n"
103 "├───────────────────┼─────────────┤\n"
104 "│bDeviceProtocol │ %-4u │\n"
105 "├───────────────────┼─────────────┤\n"
106 "│bMaxPacketSize0 │ %-4u │\n"
107 "├───────────────────┼─────────────┤\n"
108 "│idVendor │ %-#10x │\n"
109 "├───────────────────┼─────────────┤\n"
110 "│idProduct │ %-#10x │\n"
111 "├───────────────────┼─────────────┤\n"
112 "│bcdDevice │ %-#10x │\n"
113 "├───────────────────┼─────────────┤\n"
114 "│iManufacturer │ %-#10x │\n"
115 "├───────────────────┼─────────────┤\n"
116 "│iProduct │ %-#10x │\n"
117 "├───────────────────┼─────────────┤\n"
118 "│iSerialNumber │ %-#10x │\n"
119 "├───────────────────┼─────────────┤\n"
120 "│bNumConfigurations │ %-#10x │\n"
121 "└───────────────────┴─────────────┘",
122 dev_desc->bDeviceClass, dev_desc->bDeviceSubClass,
123 dev_desc->bDeviceProtocol, dev_desc->bMaxPacketSize0,
124 dev_desc->idVendor, dev_desc->idProduct, dev_desc->bcdDevice,
125 dev_desc->iManufacturer, dev_desc->iProduct, dev_desc->iSerialNumber,
126 dev_desc->bNumConfigurations);
127 s_device_descriptor = *dev_desc;
129
130 if (str_desc != NULL) {
131 memcpy(s_str_descriptor, str_desc,
133 }
134}
135
136tusb_desc_device_t *tusb_get_active_desc(void)
137{
138 return &s_device_descriptor;
139}
140
142{
143 return s_str_descriptor;
144}
145
147{
148 memset(&s_device_descriptor, 0, sizeof(s_device_descriptor));
149 memset(&s_str_descriptor, 0, sizeof(s_str_descriptor));
150}
char ** tusb_get_active_str_desc(void)
uint16_t const * tud_descriptor_string_cb(uint8_t index, uint16_t langid)
uint8_t const * tud_descriptor_device_cb(void)
Invoked when received GET DEVICE DESCRIPTOR. Application returns pointer to descriptor.
tusb_desc_device_t * tusb_get_active_desc(void)
static const uint8_t * s_configuration_descriptor
#define MAX_DESC_BUF_SIZE
static tusb_desc_device_t s_device_descriptor
static char * s_str_descriptor[USB_STRING_DESCRIPTOR_ARRAY_SIZE]
void tusb_set_descriptor(const tusb_desc_device_t *dev_desc, const char **str_desc, const uint8_t *cfg_desc)
uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
Invoked when received GET CONFIGURATION DESCRIPTOR. Descriptor contents must exist long enough for tr...
static uint16_t _desc_str[32]
void tusb_clear_descriptor(void)
static const char * TAG
Definition main/main.c:31
#define USB_STRING_DESCRIPTOR_ARRAY_SIZE