summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorhathach <[email protected]>2019-05-12 15:38:15 +0700
committerhathach <[email protected]>2019-05-12 15:38:15 +0700
commit1174949308bb7595e99fd5789ebf3466cf2ab123 (patch)
tree2a04ff594ea42234c532fc4ab5f8e6f0b4603623 /examples
parentba2136486c153edd372eadaa325dab27fe1297b2 (diff)
change tud_descriptor_string_cb() to be consistent with other descriptor callback
Diffstat (limited to 'examples')
-rw-r--r--examples/device/cdc_msc_hid/src/usb_descriptors.c32
-rw-r--r--examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c32
-rw-r--r--examples/device/hid_generic_inout/src/usb_descriptors.c32
-rw-r--r--examples/device/msc_dual_lun/src/usb_descriptors.c32
4 files changed, 76 insertions, 52 deletions
diff --git a/examples/device/cdc_msc_hid/src/usb_descriptors.c b/examples/device/cdc_msc_hid/src/usb_descriptors.c
index bb830ca27..73c8e7ce1 100644
--- a/examples/device/cdc_msc_hid/src/usb_descriptors.c
+++ b/examples/device/cdc_msc_hid/src/usb_descriptors.c
@@ -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;
}
diff --git a/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c b/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c
index fad6794a7..92ce8c6ec 100644
--- a/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c
+++ b/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c
@@ -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;
}
diff --git a/examples/device/hid_generic_inout/src/usb_descriptors.c b/examples/device/hid_generic_inout/src/usb_descriptors.c
index 2b0e40d9c..fb4dd378c 100644
--- a/examples/device/hid_generic_inout/src/usb_descriptors.c
+++ b/examples/device/hid_generic_inout/src/usb_descriptors.c
@@ -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;
}
diff --git a/examples/device/msc_dual_lun/src/usb_descriptors.c b/examples/device/msc_dual_lun/src/usb_descriptors.c
index 5c01d9e6b..1ae000fd9 100644
--- a/examples/device/msc_dual_lun/src/usb_descriptors.c
+++ b/examples/device/msc_dual_lun/src/usb_descriptors.c
@@ -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;
}