38
38
#include "supervisor/shared/traceback.h"
39
39
#include "supervisor/shared/translate.h"
40
40
#include "supervisor/shared/workflow.h"
41
+ #include "supervisor/shared/usb/usb_desc.h"
41
42
42
43
#include "shared-bindings/microcontroller/__init__.h"
43
44
#include "shared-bindings/supervisor/__init__.h"
@@ -311,6 +312,63 @@ STATIC mp_obj_t supervisor_reset_terminal(mp_obj_t x_pixels, mp_obj_t y_pixels)
311
312
}
312
313
MP_DEFINE_CONST_FUN_OBJ_2 (supervisor_reset_terminal_obj , supervisor_reset_terminal );
313
314
315
+ //| def set_usb_descriptor_overrides(manufacturer: Optional[str] = None, product: Optional[str] = None, vid: Optional[int] = None, pid: Optional[int] = None) -> None:
316
+ //| """Override constants in the USB Device Descriptor.
317
+ //|
318
+ //| Any omitted arguments will be left at their current or default values.
319
+ //| This method must be called in boot.py to have any effect."""
320
+ //| ...
321
+ //|
322
+ STATIC mp_obj_t supervisor_set_usb_descriptor_overrides (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
323
+ static const mp_arg_t allowed_args [] = {
324
+ { MP_QSTR_manufacturer , MP_ARG_OBJ , {.u_rom_obj = mp_const_none } },
325
+ { MP_QSTR_product , MP_ARG_OBJ , {.u_rom_obj = mp_const_none } },
326
+ { MP_QSTR_vid , MP_ARG_INT , {.u_int = -1 } },
327
+ { MP_QSTR_pid , MP_ARG_INT , {.u_int = -1 } },
328
+ };
329
+ struct {
330
+ mp_arg_val_t manufacturer ;
331
+ mp_arg_val_t product ;
332
+ mp_arg_val_t vid ;
333
+ mp_arg_val_t pid ;
334
+ } args ;
335
+ mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , (mp_arg_val_t * )& args );
336
+
337
+ if (!mp_obj_is_str_or_bytes (args .manufacturer .u_obj ) && args .manufacturer .u_obj != mp_const_none ) {
338
+ mp_raise_TypeError (translate ("argument has wrong type" ));
339
+ }
340
+ if (!mp_obj_is_str_or_bytes (args .product .u_obj ) && args .product .u_obj != mp_const_none ) {
341
+ mp_raise_TypeError (translate ("argument has wrong type" ));
342
+ }
343
+
344
+ const char * manufacturer = NULL ;
345
+ const char * product = NULL ;
346
+
347
+ size_t len ;
348
+ if (args .manufacturer .u_obj != mp_const_none ) {
349
+ manufacturer = mp_obj_str_get_data (args .manufacturer .u_obj , & len );
350
+ if (len > 126 ) {
351
+ mp_raise_ValueError (translate ("string too long" ));
352
+ }
353
+ }
354
+ if (args .product .u_obj != mp_const_none ) {
355
+ product = mp_obj_str_get_data (args .product .u_obj , & len );
356
+ if (len > 126 ) {
357
+ mp_raise_ValueError (translate ("string too long" ));
358
+ }
359
+ }
360
+ if (args .vid .u_int < -1 || args .vid .u_int >= (1 << 16 )) {
361
+ mp_raise_ValueError (translate ("vid or pid out of range" ));
362
+ }
363
+ if (args .pid .u_int < -1 || args .pid .u_int >= (1 << 16 )) {
364
+ mp_raise_ValueError (translate ("vid or pid out of range" ));
365
+ }
366
+
367
+ usb_set_descriptor_overrides (manufacturer , product , args .vid .u_int , args .pid .u_int );
368
+ return mp_const_none ;
369
+ }
370
+ MP_DEFINE_CONST_FUN_OBJ_KW (supervisor_set_usb_descriptor_overrides_obj , 0 , supervisor_set_usb_descriptor_overrides );
371
+
314
372
STATIC const mp_rom_map_elem_t supervisor_module_globals_table [] = {
315
373
{ MP_ROM_QSTR (MP_QSTR___name__ ), MP_ROM_QSTR (MP_QSTR_supervisor ) },
316
374
{ MP_ROM_QSTR (MP_QSTR_enable_autoreload ), MP_ROM_PTR (& supervisor_enable_autoreload_obj ) },
@@ -325,6 +383,7 @@ STATIC const mp_rom_map_elem_t supervisor_module_globals_table[] = {
325
383
{ MP_ROM_QSTR (MP_QSTR_get_previous_traceback ), MP_ROM_PTR (& supervisor_get_previous_traceback_obj ) },
326
384
{ MP_ROM_QSTR (MP_QSTR_disable_ble_workflow ), MP_ROM_PTR (& supervisor_disable_ble_workflow_obj ) },
327
385
{ MP_ROM_QSTR (MP_QSTR_reset_terminal ), MP_ROM_PTR (& supervisor_reset_terminal_obj ) },
386
+ { MP_ROM_QSTR (MP_QSTR_set_usb_descriptor_overrides ), MP_ROM_PTR (& supervisor_set_usb_descriptor_overrides_obj ) },
328
387
};
329
388
330
389
STATIC MP_DEFINE_CONST_DICT (supervisor_module_globals , supervisor_module_globals_table );
0 commit comments