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

@@ -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;