refactor(core/usbd_core): process string internal, you can use like \"cherryusb\" now

This commit is contained in:
sakumisu
2024-05-16 11:19:51 +08:00
parent 0985a7e5fa
commit af92236cf4
4 changed files with 48 additions and 75 deletions

View File

@@ -33,6 +33,10 @@
/* ================= USB Device Stack Configuration ================ */
#ifndef CONFIG_USBDEV_STRING_LANGID
#define CONFIG_USBDEV_STRING_LANGID 0x0409
#endif
/* Ep0 in and out transfer buffer */
#ifndef CONFIG_USBDEV_REQUEST_BUFFER_LEN
#define CONFIG_USBDEV_REQUEST_BUFFER_LEN 512

View File

@@ -18,6 +18,10 @@
#define INTF_DESC_bInterfaceNumber 2 /** Interface number offset */
#define INTF_DESC_bAlternateSetting 3 /** Alternate setting offset */
#ifndef CONFIG_USBDEV_STRING_LANGID
#define CONFIG_USBDEV_STRING_LANGID 0x0409
#endif
struct usbd_tx_rx_msg {
uint8_t ep;
uint8_t ep_mult;
@@ -151,6 +155,7 @@ static bool usbd_get_descriptor(uint8_t busid, uint16_t type_index, uint8_t **da
uint8_t index = 0U;
bool found = true;
uint32_t desc_len = 0;
const char *string = NULL;
const uint8_t *desc = NULL;
type = HI_BYTE(type_index);
@@ -186,12 +191,40 @@ static bool usbd_get_descriptor(uint8_t busid, uint16_t type_index, uint8_t **da
desc = (uint8_t *)g_usbd_core[busid].descriptors->msosv1_descriptor->string;
desc_len = g_usbd_core[busid].descriptors->msosv1_descriptor->string[0];
} else {
desc = g_usbd_core[busid].descriptors->string_descriptor_callback(g_usbd_core[busid].speed, index);
if (desc == NULL) {
if (index == USB_STRING_LANGID_INDEX) {
uint16_t STRING_LANGID = CONFIG_USBDEV_STRING_LANGID;
(*data)[0] = 0x04;
(*data)[1] = USB_DESCRIPTOR_TYPE_STRING;
(*data)[2] = (uint8_t)(STRING_LANGID & 0xff);
(*data)[3] = (uint8_t)((STRING_LANGID & 0xff00) >> 8);
*len = 4;
return true;
}
string = g_usbd_core[busid].descriptors->string_descriptor_callback(g_usbd_core[busid].speed, index);
if (string == NULL) {
found = false;
break;
}
desc_len = desc[0];
uint16_t str_size = strlen(string);
uint16_t total_size = 2 * str_size + 2;
if (total_size > CONFIG_USBDEV_REQUEST_BUFFER_LEN) {
USB_LOG_ERR("string size overflow\r\n");
return false;
}
(*data)[0] = total_size;
(*data)[1] = USB_DESCRIPTOR_TYPE_STRING;
for (uint16_t i = 0; i < str_size; i++) {
(*data)[2 * i + 2] = string[i];
(*data)[2 * i + 3] = 0x00;
}
*len = total_size;
return true;
}
break;
case USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER:
@@ -236,7 +269,7 @@ static bool usbd_get_descriptor(uint8_t busid, uint16_t type_index, uint8_t **da
/* nothing found */
USB_LOG_ERR("descriptor <type:%x,index:%x> not found!\r\n", type, index);
} else {
*data = desc;
*data = (uint8_t *)desc;
//memcpy(*data, desc, desc_len);
*len = desc_len;
}

View File

@@ -67,7 +67,7 @@ struct usb_descriptor {
const uint8_t *(*config_descriptor_callback)(uint8_t speed);
const uint8_t *(*device_quality_descriptor_callback)(uint8_t speed);
const uint8_t *(*other_speed_descriptor_callback)(uint8_t speed);
const uint8_t *(*string_descriptor_callback)(uint8_t speed, uint8_t index);
const char *(*string_descriptor_callback)(uint8_t speed, uint8_t index);
const struct usb_msosv1_descriptor *msosv1_descriptor;
const struct usb_msosv2_descriptor *msosv2_descriptor;
const struct usb_webusb_url_ex_descriptor *webusb_url_descriptor;

View File

@@ -62,76 +62,12 @@ static const uint8_t device_quality_descriptor[] = {
0x00,
};
static const uint8_t string0_descriptor[] = {
///////////////////////////////////////
/// string0 descriptor
///////////////////////////////////////
USB_LANGID_INIT(USBD_LANGID_STRING),
static const char *string_descriptors[] = {
"CherryUSB", /* Manufacturer */
"CherryUSB CDC DEMO", /* Product */
"2022123456", /* Serial Number */
};
static const uint8_t string1_descriptor[] = {
///////////////////////////////////////
/// string1 descriptor
///////////////////////////////////////
0x14, /* bLength */
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
'C', 0x00, /* wcChar0 */
'h', 0x00, /* wcChar1 */
'e', 0x00, /* wcChar2 */
'r', 0x00, /* wcChar3 */
'r', 0x00, /* wcChar4 */
'y', 0x00, /* wcChar5 */
'U', 0x00, /* wcChar6 */
'S', 0x00, /* wcChar7 */
'B', 0x00, /* wcChar8 */
};
static const uint8_t string2_descriptor[] = {
///////////////////////////////////////
/// string2 descriptor
///////////////////////////////////////
0x26, /* bLength */
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
'C', 0x00, /* wcChar0 */
'h', 0x00, /* wcChar1 */
'e', 0x00, /* wcChar2 */
'r', 0x00, /* wcChar3 */
'r', 0x00, /* wcChar4 */
'y', 0x00, /* wcChar5 */
'U', 0x00, /* wcChar6 */
'S', 0x00, /* wcChar7 */
'B', 0x00, /* wcChar8 */
' ', 0x00, /* wcChar9 */
'C', 0x00, /* wcChar10 */
'-', 0x00, /* wcChar11 */
'M', 0x00, /* wcChar12 */
' ', 0x00, /* wcChar13 */
'D', 0x00, /* wcChar14 */
'E', 0x00, /* wcChar15 */
'M', 0x00, /* wcChar16 */
'O', 0x00, /* wcChar17 */
};
static const uint8_t string3_descriptor[] = {
///////////////////////////////////////
/// string3 descriptor
///////////////////////////////////////
0x16, /* bLength */
USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
'2', 0x00, /* wcChar0 */
'0', 0x00, /* wcChar1 */
'2', 0x00, /* wcChar2 */
'2', 0x00, /* wcChar3 */
'1', 0x00, /* wcChar4 */
'2', 0x00, /* wcChar5 */
'3', 0x00, /* wcChar6 */
'4', 0x00, /* wcChar7 */
'5', 0x00, /* wcChar8 */
'6', 0x00, /* wcChar9 */
};
static const uint8_t *string_descriptors[4] = { string0_descriptor, string1_descriptor, string2_descriptor, string3_descriptor };
static const uint8_t *device_descriptor_callback(uint8_t speed)
{
return device_descriptor;
@@ -147,12 +83,12 @@ static const uint8_t *device_quality_descriptor_callback(uint8_t speed)
return device_quality_descriptor;
}
static const uint8_t *string_descriptor_callback(uint8_t speed, uint8_t index)
static const char *string_descriptor_callback(uint8_t speed, uint8_t index)
{
if (index > 3) {
return NULL;
}
return string_descriptors[index];
return string_descriptors[index - 1];
}
const struct usb_descriptor cdc_msc_descriptor = {