change tud_descriptor_string_cb() to be consistent with other descriptor callback

This commit is contained in:
hathach 2019-05-12 15:38:15 +07:00
parent ba2136486c
commit 1174949308
No known key found for this signature in database
GPG Key ID: 2FA891220FBFD581
8 changed files with 90 additions and 74 deletions

View File

@ -174,30 +174,36 @@ char const* string_desc_arr [] =
"TinyUSB HID" // 6: HID
};
// Invoked when received GET_STRING_DESC request
// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64
// Return number of characters. Note usb string is in 16-bits unicode format
uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char)
static uint16_t _desc_str[32];
// Invoked when received GET STRING DESCRIPTOR request
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
uint16_t const* tud_descriptor_string_cb(uint8_t index)
{
uint8_t chr_count;
if ( index == 0)
{
memcpy(desc, string_desc_arr[0], 2);
return 1;
memcpy(&_desc_str[1], string_desc_arr[0], 2);
chr_count = 1;
}else
{
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return 0;
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
const char* str = string_desc_arr[index];
// Cap at max char
uint8_t count = strlen(str);
if ( count > max_char ) count = max_char;
chr_count = strlen(str);
if ( chr_count > 31 ) chr_count = 31;
for(uint8_t i=0; i<count; i++)
for(uint8_t i=0; i<chr_count; i++)
{
*desc++ = str[i];
_desc_str[1+i] = str[i];
}
return count;
}
// first byte is len, second byte is string type
_desc_str[0] = TUD_DESC_STR_HEADER(chr_count);
return _desc_str;
}

View File

@ -174,30 +174,36 @@ char const* string_desc_arr [] =
"TinyUSB HID" // 6: HID
};
// Invoked when received GET_STRING_DESC request
// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64
// Return number of characters. Note usb string is in 16-bits unicode format
uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char)
static uint16_t _desc_str[32];
// Invoked when received GET STRING DESCRIPTOR request
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
uint16_t const* tud_descriptor_string_cb(uint8_t index)
{
uint8_t chr_count;
if ( index == 0)
{
memcpy(desc, string_desc_arr[0], 2);
return 1;
memcpy(&_desc_str[1], string_desc_arr[0], 2);
chr_count = 1;
}else
{
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return 0;
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
const char* str = string_desc_arr[index];
// Cap at max char
uint8_t count = strlen(str);
if ( count > max_char ) count = max_char;
chr_count = strlen(str);
if ( chr_count > 31 ) chr_count = 31;
for(uint8_t i=0; i<count; i++)
for(uint8_t i=0; i<chr_count; i++)
{
*desc++ = str[i];
_desc_str[1+i] = str[i];
}
return count;
}
// first byte is len, second byte is string type
_desc_str[0] = TUD_DESC_STR_HEADER(chr_count);
return _desc_str;
}

View File

@ -123,30 +123,36 @@ char const* string_desc_arr [] =
"123456", // 3: Serials, should use chip ID
};
// Invoked when received GET_STRING_DESC request
// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64
// Return number of characters. Note usb string is in 16-bits unicode format
uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char)
static uint16_t _desc_str[32];
// Invoked when received GET STRING DESCRIPTOR request
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
uint16_t const* tud_descriptor_string_cb(uint8_t index)
{
uint8_t chr_count;
if ( index == 0)
{
memcpy(desc, string_desc_arr[0], 2);
return 1;
memcpy(&_desc_str[1], string_desc_arr[0], 2);
chr_count = 1;
}else
{
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return 0;
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
const char* str = string_desc_arr[index];
// Cap at max char
uint8_t count = strlen(str);
if ( count > max_char ) count = max_char;
chr_count = strlen(str);
if ( chr_count > 31 ) chr_count = 31;
for(uint8_t i=0; i<count; i++)
for(uint8_t i=0; i<chr_count; i++)
{
*desc++ = str[i];
_desc_str[1+i] = str[i];
}
return count;
}
// first byte is len, second byte is string type
_desc_str[0] = TUD_DESC_STR_HEADER(chr_count);
return _desc_str;
}

View File

@ -108,30 +108,36 @@ char const* string_desc_arr [] =
"123456", // 3: Serials, should use chip ID
};
// Invoked when received GET_STRING_DESC request
// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64
// Return number of characters. Note usb string is in 16-bits unicode format
uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char)
static uint16_t _desc_str[32];
// Invoked when received GET STRING DESCRIPTOR request
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
uint16_t const* tud_descriptor_string_cb(uint8_t index)
{
uint8_t chr_count;
if ( index == 0)
{
memcpy(desc, string_desc_arr[0], 2);
return 1;
memcpy(&_desc_str[1], string_desc_arr[0], 2);
chr_count = 1;
}else
{
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return 0;
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
const char* str = string_desc_arr[index];
// Cap at max char
uint8_t count = strlen(str);
if ( count > max_char ) count = max_char;
chr_count = strlen(str);
if ( chr_count > 31 ) chr_count = 31;
for(uint8_t i=0; i<count; i++)
for(uint8_t i=0; i<chr_count; i++)
{
*desc++ = str[i];
_desc_str[1+i] = str[i];
}
return count;
}
// first byte is len, second byte is string type
_desc_str[0] = TUD_DESC_STR_HEADER(chr_count);
return _desc_str;
}

View File

@ -68,9 +68,8 @@ bool tud_hid_mouse_report(uint8_t report_id, uint8_t buttons, int8_t x, int8_t y
// Callbacks (Weak is optional)
//--------------------------------------------------------------------+
// Invoked when received GET HID REPORT DESCRIPTOR
// Application return pointer to descriptor
// Descriptor contents must exist long enough for transfer to complete
// Invoked when received GET HID REPORT DESCRIPTOR request
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
uint8_t const * tud_hid_descriptor_report_cb(void);
// Invoked when received GET_REPORT control request

View File

@ -403,10 +403,10 @@ static inline uint8_t tu_desc_len(void const* desc)
}
// Length of the string descriptors in bytes with slen characters
#define TUD_DESC_STRLEN(_slen) (2*(_slen) + 2)
#define TUD_DESC_STRLEN(_chr_count) (2*(_chr_count) + 2)
// Header of string descriptors with len + string type
#define TUD_DESC_STR_HEADER(_slen) ( (uint16_t) ( (TUSB_DESC_STRING << 8 ) | TUD_DESC_STRLEN(_slen)) )
#define TUD_DESC_STR_HEADER(_chr_count) ( (uint16_t) ( (TUSB_DESC_STRING << 8 ) | TUD_DESC_STRLEN(_chr_count)) )
// Convert comma-separated string to descriptor unicode format
#define TUD_DESC_STRCONV( ... ) (const uint16_t[]) { TUD_DESC_STR_HEADER(VA_ARGS_NUM_(__VA_ARGS__)), __VA_ARGS__ }

View File

@ -577,16 +577,11 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
return false;
}else
{
uint16_t desc_str[CFG_TUD_ENDOINT0_SIZE/2]; // up to endpoint0 size only
uint8_t len = 2*tud_descriptor_string_cb(desc_index, desc_str+1, CFG_TUD_ENDOINT0_SIZE/2-1);
uint8_t const* desc_str = (uint8_t const*) tud_descriptor_string_cb(desc_index);
TU_ASSERT(desc_str);
TU_ASSERT(len > 0);
// first byte of descriptor is size, second byte is string type
len += 2; // header len
desc_str[0] = tu_u16_from_u8(TUSB_DESC_STRING, len);
return usbd_control_xfer(rhport, p_request, desc_str, len);
// first byte of descriptor is its size
return usbd_control_xfer(rhport, p_request, (void*) desc_str, desc_str[0]);
}
break;

View File

@ -63,19 +63,17 @@ bool tud_remote_wakeup(void);
// Application Callbacks (WEAK is optional)
//--------------------------------------------------------------------+
// Invoked when received GET DEVICE DESCRIPTOR
// Invoked when received GET DEVICE DESCRIPTOR request
// Application return pointer to descriptor
uint8_t const * tud_descriptor_device_cb(void);
// Invoked when received GET CONFIGURATION DESCRIPTOR
// Application return pointer to descriptor
// Descriptor contents must exist long enough for transfer to complete
// Invoked when received GET CONFIGURATION DESCRIPTOR request
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
uint8_t const * tud_descriptor_configuration_cb(void);
// Invoked when received GET STRING DESC request
// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64
// Return number of characters. Note usb string is in UTF-16 format
uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char);
// Invoked when received GET STRING DESCRIPTOR request
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
uint16_t const* tud_descriptor_string_cb(uint8_t index);
// Invoked when device is mounted (configured)
ATTR_WEAK void tud_mount_cb(void);