@@ -192,6 +192,47 @@ bool Adafruit_USBD_Device::attach(void)
192
192
return tud_connect ();
193
193
}
194
194
195
+ static int strcpy_utf16 (const char *s, uint16_t *buf, int bufsize);
196
+ uint16_t const * Adafruit_USBD_Device::descriptor_string_cb (uint8_t index, uint16_t langid)
197
+ {
198
+ (void ) langid;
199
+
200
+ // up to 32 unicode characters (header make it 33)
201
+ static uint16_t _desc_str[33 ];
202
+ uint8_t chr_count;
203
+
204
+ switch (index)
205
+ {
206
+ case 0 :
207
+ _desc_str[1 ] = _language_id;
208
+ chr_count = 1 ;
209
+ break ;
210
+
211
+ case 1 :
212
+ chr_count = strcpy_utf16 (_manufacturer, _desc_str + 1 , 32 );
213
+ break ;
214
+
215
+ case 2 :
216
+ chr_count = strcpy_utf16 (_product, _desc_str + 1 , 32 );
217
+ break ;
218
+
219
+ case 3 :
220
+ // serial Number
221
+ chr_count = this ->getSerialDescriptor (_desc_str+1 );
222
+ break ;
223
+
224
+ default : return NULL ;
225
+ }
226
+
227
+ // first byte is length (including header), second byte is string type
228
+ _desc_str[0 ] = (TUSB_DESC_STRING << 8 ) | (2 *chr_count + 2 );
229
+
230
+ return _desc_str;
231
+ }
232
+
233
+ // --------------------------------------------------------------------+
234
+ // TinyUSB stack callbacks
235
+ // --------------------------------------------------------------------+
195
236
extern " C"
196
237
{
197
238
@@ -210,6 +251,39 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
210
251
return USBDevice._desc_cfg ;
211
252
}
212
253
254
+ // Invoked when received GET STRING DESCRIPTOR request
255
+ // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
256
+ // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
257
+ // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
258
+ uint16_t const * tud_descriptor_string_cb (uint8_t index, uint16_t langid)
259
+ {
260
+ return USBDevice.descriptor_string_cb (index, langid);
261
+ }
262
+
263
+ // --------------------------------------------------------------------+
264
+ // Some of TinyUSB class driver requires strong callbacks, which cause
265
+ // link error if 'Adafruit_TinyUSB_Arduino' is not included, provide
266
+ // weak callback here to prevent linking error
267
+ // --------------------------------------------------------------------+
268
+
269
+ // HID
270
+ TU_ATTR_WEAK uint8_t const * tud_hid_descriptor_report_cb (void ) { return NULL ; }
271
+ TU_ATTR_WEAK uint16_t tud_hid_get_report_cb (uint8_t report_id, hid_report_type_t report_type, uint8_t * buffer, uint16_t reqlen) { return 0 ; }
272
+ TU_ATTR_WEAK void tud_hid_set_report_cb (uint8_t report_id, hid_report_type_t report_type, uint8_t const * buffer, uint16_t bufsize) { }
273
+
274
+ // MSC
275
+ TU_ATTR_WEAK int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void * buffer, uint32_t bufsize) { return -1 ; }
276
+ TU_ATTR_WEAK int32_t tud_msc_write10_cb (uint8_t lun, uint32_t lba, uint32_t offset, uint8_t * buffer, uint32_t bufsize) { return -1 ; }
277
+ TU_ATTR_WEAK void tud_msc_inquiry_cb (uint8_t lun, uint8_t vendor_id[8 ], uint8_t product_id[16 ], uint8_t product_rev[4 ]) {}
278
+ TU_ATTR_WEAK bool tud_msc_test_unit_ready_cb (uint8_t lun) { return false ; }
279
+ TU_ATTR_WEAK void tud_msc_capacity_cb (uint8_t lun, uint32_t * block_count, uint16_t * block_size) { }
280
+ TU_ATTR_WEAK int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16 ], void * buffer, uint16_t bufsize) { return -1 ; }
281
+
282
+ } // extern C
283
+
284
+ // --------------------------------------------------------------------+
285
+ // Helper
286
+ // --------------------------------------------------------------------+
213
287
constexpr static inline bool isInvalidUtf8Octet (uint8_t t) {
214
288
// see bullets in https://tools.ietf.org/html/rfc3629#section-1
215
289
return (t == 0xc0 ) || (t == 0xC1 ) || (t >= 0xF5 );
@@ -240,7 +314,7 @@ constexpr static inline bool isInvalidUtf8Octet(uint8_t t) {
240
314
// NOTE: evilUTF8 could just contain a single byte 0xF9 ....
241
315
//
242
316
// Attempting to decode evilUTF8 will progress to whatever is next to it on the stack.
243
- // The above should work when optimizations are turned
317
+ // The above should work when optimizations are turned
244
318
//
245
319
static int8_t utf8Codepoint (const uint8_t *utf8, uint32_t *codepointp)
246
320
{
@@ -351,67 +425,4 @@ static int strcpy_utf16(const char *s, uint16_t *buf, int bufsize)
351
425
return buflen;
352
426
}
353
427
354
- // up to 32 unicode characters (header make it 33)
355
- static uint16_t _desc_str[33 ];
356
-
357
- // Invoked when received GET STRING DESCRIPTOR request
358
- // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
359
- // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
360
- // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
361
- uint16_t const * tud_descriptor_string_cb (uint8_t index, uint16_t langid)
362
- {
363
- (void ) langid;
364
-
365
- uint8_t chr_count;
366
-
367
- switch (index)
368
- {
369
- case 0 :
370
- _desc_str[1 ] = USBDevice.getLanguageDescriptor ();
371
- chr_count = 1 ;
372
- break ;
373
-
374
- case 1 :
375
- chr_count = strcpy_utf16 (USBDevice.getManufacturerDescriptor (), _desc_str + 1 , 32 );
376
- break ;
377
-
378
- case 2 :
379
- chr_count = strcpy_utf16 (USBDevice.getProductDescriptor (), _desc_str + 1 , 32 );
380
- break ;
381
-
382
- case 3 :
383
- // serial Number
384
- chr_count = USBDevice.getSerialDescriptor (_desc_str+1 );
385
- break ;
386
-
387
- default : return NULL ;
388
- }
389
-
390
- // first byte is length (including header), second byte is string type
391
- _desc_str[0 ] = (TUSB_DESC_STRING << 8 ) | (2 *chr_count + 2 );
392
-
393
- return _desc_str;
394
- }
395
-
396
- // --------------------------------------------------------------------+
397
- // Some of TinyUSB class driver requires strong callbacks, which cause
398
- // link error if 'Adafruit_TinyUSB_Arduino' is not included, provide
399
- // weak callback here to prevent linking error
400
- // --------------------------------------------------------------------+
401
-
402
- // HID
403
- TU_ATTR_WEAK uint8_t const * tud_hid_descriptor_report_cb (void ) { return NULL ; }
404
- TU_ATTR_WEAK uint16_t tud_hid_get_report_cb (uint8_t report_id, hid_report_type_t report_type, uint8_t * buffer, uint16_t reqlen) { return 0 ; }
405
- TU_ATTR_WEAK void tud_hid_set_report_cb (uint8_t report_id, hid_report_type_t report_type, uint8_t const * buffer, uint16_t bufsize) { }
406
-
407
- // MSC
408
- TU_ATTR_WEAK int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void * buffer, uint32_t bufsize) { return -1 ; }
409
- TU_ATTR_WEAK int32_t tud_msc_write10_cb (uint8_t lun, uint32_t lba, uint32_t offset, uint8_t * buffer, uint32_t bufsize) { return -1 ; }
410
- TU_ATTR_WEAK void tud_msc_inquiry_cb (uint8_t lun, uint8_t vendor_id[8 ], uint8_t product_id[16 ], uint8_t product_rev[4 ]) {}
411
- TU_ATTR_WEAK bool tud_msc_test_unit_ready_cb (uint8_t lun) { return false ; }
412
- TU_ATTR_WEAK void tud_msc_capacity_cb (uint8_t lun, uint32_t * block_count, uint16_t * block_size) { }
413
- TU_ATTR_WEAK int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16 ], void * buffer, uint16_t bufsize) { return -1 ; }
414
-
415
- } // extern C
416
-
417
428
#endif // USE_TINYUSB
0 commit comments