Skip to content

Commit 9697c4d

Browse files
committed
update all examples to use unique ID as serial if avaialble
1 parent 67ff3f7 commit 9697c4d

File tree

23 files changed

+947
-684
lines changed

23 files changed

+947
-684
lines changed

examples/device/audio_4_channel_mic/src/usb_descriptors.c

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*
2424
*/
2525

26+
#include "bsp/board_api.h"
2627
#include "tusb.h"
2728

2829
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
@@ -116,50 +117,63 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
116117
// String Descriptors
117118
//--------------------------------------------------------------------+
118119

120+
// String Descriptor Index
121+
enum {
122+
STRID_LANGID = 0,
123+
STRID_MANUFACTURER,
124+
STRID_PRODUCT,
125+
STRID_SERIAL,
126+
};
127+
119128
// array of pointer to string descriptors
120-
char const* string_desc_arr [] =
121-
{
122-
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
123-
"PaniRCorp", // 1: Manufacturer
124-
"MicNode_4_Ch", // 2: Product
125-
"123458", // 3: Serials, should use chip ID
126-
"UAC2", // 4: Audio Interface
129+
char const* string_desc_arr [] = {
130+
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
131+
"PaniRCorp", // 1: Manufacturer
132+
"MicNode_4_Ch", // 2: Product
133+
NULL, // 3: Serials will use unique ID if possible
134+
"UAC2", // 4: Audio Interface
127135
};
128136

129-
static uint16_t _desc_str[32];
137+
static uint16_t _desc_str[32 + 1];
130138

131139
// Invoked when received GET STRING DESCRIPTOR request
132140
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
133-
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
134-
{
141+
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
135142
(void) langid;
143+
size_t chr_count;
144+
145+
switch ( index ) {
146+
case STRID_LANGID:
147+
memcpy(&_desc_str[1], string_desc_arr[0], 2);
148+
chr_count = 1;
149+
break;
136150

137-
uint8_t chr_count;
151+
case STRID_SERIAL:
152+
chr_count = board_usb_get_serial(_desc_str + 1, 32);
153+
break;
138154

139-
if ( index == 0)
140-
{
141-
memcpy(&_desc_str[1], string_desc_arr[0], 2);
142-
chr_count = 1;
143-
}else
144-
{
145-
// Convert ASCII string into UTF-16
155+
default:
156+
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
157+
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
146158

147-
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
159+
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
148160

149-
const char* str = string_desc_arr[index];
161+
const char *str = string_desc_arr[index];
150162

151-
// Cap at max char
152-
chr_count = (uint8_t) strlen(str);
153-
if ( chr_count > 31 ) chr_count = 31;
163+
// Cap at max char
164+
chr_count = strlen(str);
165+
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
166+
if ( chr_count > max_count ) chr_count = max_count;
154167

155-
for(uint8_t i=0; i<chr_count; i++)
156-
{
157-
_desc_str[1+i] = str[i];
158-
}
168+
// Convert ASCII string into UTF-16
169+
for ( size_t i = 0; i < chr_count; i++ ) {
170+
_desc_str[1 + i] = str[i];
171+
}
172+
break;
159173
}
160174

161175
// first byte is length (including header), second byte is string type
162-
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
176+
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
163177

164178
return _desc_str;
165179
}

examples/device/audio_test/src/usb_descriptors.c

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*
2424
*/
2525

26+
#include "bsp/board_api.h"
2627
#include "tusb.h"
2728

2829
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
@@ -116,50 +117,65 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
116117
// String Descriptors
117118
//--------------------------------------------------------------------+
118119

120+
// String Descriptor Index
121+
enum {
122+
STRID_LANGID = 0,
123+
STRID_MANUFACTURER,
124+
STRID_PRODUCT,
125+
STRID_SERIAL,
126+
};
127+
119128
// array of pointer to string descriptors
120129
char const* string_desc_arr [] =
121130
{
122-
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
123-
"PaniRCorp", // 1: Manufacturer
124-
"MicNode", // 2: Product
125-
"123456", // 3: Serials, should use chip ID
126-
"UAC2", // 4: Audio Interface
131+
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
132+
"PaniRCorp", // 1: Manufacturer
133+
"MicNode", // 2: Product
134+
NULL, // 3: Serials will use unique ID if possible
135+
"UAC2", // 4: Audio Interface
136+
127137
};
128138

129-
static uint16_t _desc_str[32];
139+
static uint16_t _desc_str[32 + 1];
130140

131141
// Invoked when received GET STRING DESCRIPTOR request
132142
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
133-
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
134-
{
143+
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
135144
(void) langid;
145+
size_t chr_count;
146+
147+
switch ( index ) {
148+
case STRID_LANGID:
149+
memcpy(&_desc_str[1], string_desc_arr[0], 2);
150+
chr_count = 1;
151+
break;
136152

137-
uint8_t chr_count;
153+
case STRID_SERIAL:
154+
chr_count = board_usb_get_serial(_desc_str + 1, 32);
155+
break;
138156

139-
if ( index == 0)
140-
{
141-
memcpy(&_desc_str[1], string_desc_arr[0], 2);
142-
chr_count = 1;
143-
}else
144-
{
145-
// Convert ASCII string into UTF-16
157+
default:
158+
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
159+
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
146160

147-
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
161+
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
148162

149-
const char* str = string_desc_arr[index];
163+
const char *str = string_desc_arr[index];
150164

151-
// Cap at max char
152-
chr_count = (uint8_t) strlen(str);
153-
if ( chr_count > 31 ) chr_count = 31;
165+
// Cap at max char
166+
chr_count = strlen(str);
167+
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
168+
if ( chr_count > max_count ) chr_count = max_count;
154169

155-
for(uint8_t i=0; i<chr_count; i++)
156-
{
157-
_desc_str[1+i] = str[i];
158-
}
170+
// Convert ASCII string into UTF-16
171+
for ( size_t i = 0; i < chr_count; i++ ) {
172+
_desc_str[1 + i] = str[i];
173+
}
174+
break;
159175
}
160176

161177
// first byte is length (including header), second byte is string type
162-
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
178+
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
163179

164180
return _desc_str;
165181
}

examples/device/audio_test_multi_rate/src/usb_descriptors.c

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*
2525
*/
2626

27+
#include "bsp/board_api.h"
2728
#include "tusb.h"
2829
#include "usb_descriptors.h"
2930

@@ -120,50 +121,65 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
120121
// String Descriptors
121122
//--------------------------------------------------------------------+
122123

124+
// String Descriptor Index
125+
enum {
126+
STRID_LANGID = 0,
127+
STRID_MANUFACTURER,
128+
STRID_PRODUCT,
129+
STRID_SERIAL,
130+
};
131+
123132
// array of pointer to string descriptors
124133
char const* string_desc_arr [] =
125134
{
126-
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
127-
"PaniRCorp", // 1: Manufacturer
128-
"MicNode", // 2: Product
129-
"123456", // 3: Serials, should use chip ID
130-
"UAC2", // 4: Audio Interface
135+
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
136+
"PaniRCorp", // 1: Manufacturer
137+
"MicNode", // 2: Product
138+
NULL, // 3: Serials will use unique ID if possible
139+
"UAC2", // 4: Audio Interface
140+
131141
};
132142

133-
static uint16_t _desc_str[32];
143+
static uint16_t _desc_str[32 + 1];
134144

135145
// Invoked when received GET STRING DESCRIPTOR request
136146
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
137-
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
138-
{
147+
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
139148
(void) langid;
149+
size_t chr_count;
150+
151+
switch ( index ) {
152+
case STRID_LANGID:
153+
memcpy(&_desc_str[1], string_desc_arr[0], 2);
154+
chr_count = 1;
155+
break;
140156

141-
uint8_t chr_count;
157+
case STRID_SERIAL:
158+
chr_count = board_usb_get_serial(_desc_str + 1, 32);
159+
break;
142160

143-
if ( index == 0)
144-
{
145-
memcpy(&_desc_str[1], string_desc_arr[0], 2);
146-
chr_count = 1;
147-
}else
148-
{
149-
// Convert ASCII string into UTF-16
161+
default:
162+
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
163+
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
150164

151-
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
165+
if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
152166

153-
const char* str = string_desc_arr[index];
167+
const char *str = string_desc_arr[index];
154168

155-
// Cap at max char
156-
chr_count = (uint8_t) strlen(str);
157-
if ( chr_count > 31 ) chr_count = 31;
169+
// Cap at max char
170+
chr_count = strlen(str);
171+
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
172+
if ( chr_count > max_count ) chr_count = max_count;
158173

159-
for(uint8_t i=0; i<chr_count; i++)
160-
{
161-
_desc_str[1+i] = str[i];
162-
}
174+
// Convert ASCII string into UTF-16
175+
for ( size_t i = 0; i < chr_count; i++ ) {
176+
_desc_str[1 + i] = str[i];
177+
}
178+
break;
163179
}
164180

165181
// first byte is length (including header), second byte is string type
166-
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
182+
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
167183

168184
return _desc_str;
169185
}

0 commit comments

Comments
 (0)