3838#include "supervisor/shared/traceback.h"
3939#include "supervisor/shared/translate.h"
4040#include "supervisor/shared/workflow.h"
41+ #include "supervisor/usb.h"
4142
4243#include "shared-bindings/microcontroller/__init__.h"
4344#include "shared-bindings/supervisor/__init__.h"
@@ -311,6 +312,66 @@ STATIC mp_obj_t supervisor_reset_terminal(mp_obj_t x_pixels, mp_obj_t y_pixels)
311312}
312313MP_DEFINE_CONST_FUN_OBJ_2 (supervisor_reset_terminal_obj , supervisor_reset_terminal );
313314
315+ #if CIRCUITPY_USB
316+ //| def set_usb_identification(manufacturer: Optional[str] = None, product: Optional[str] = None, vid: int = -1, pid: int = -1) -> None:
317+ //| """Override identification constants in the USB Device Descriptor.
318+ //|
319+ //| If passed, `manufacturer` and `product` must be ASCII strings (or buffers) of at most 126
320+ //| characters. Any omitted arguments will be left at their default values.
321+ //|
322+ //| This method must be called in boot.py to have any effect."""
323+ //| ...
324+ //|
325+ STATIC mp_obj_t supervisor_set_usb_identification (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
326+ static const mp_arg_t allowed_args [] = {
327+ { MP_QSTR_manufacturer , MP_ARG_OBJ , {.u_rom_obj = mp_const_none } },
328+ { MP_QSTR_product , MP_ARG_OBJ , {.u_rom_obj = mp_const_none } },
329+ { MP_QSTR_vid , MP_ARG_INT , {.u_int = -1 } },
330+ { MP_QSTR_pid , MP_ARG_INT , {.u_int = -1 } },
331+ };
332+ struct {
333+ mp_arg_val_t manufacturer ;
334+ mp_arg_val_t product ;
335+ mp_arg_val_t vid ;
336+ mp_arg_val_t pid ;
337+ } args ;
338+ mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , (mp_arg_val_t * )& args );
339+
340+ if (!usb_identification_allocation ) {
341+ usb_identification_allocation = allocate_memory (sizeof (usb_identification_t ), false, true);
342+ }
343+ usb_identification_t * identification = (usb_identification_t * )usb_identification_allocation -> ptr ;
344+
345+ mp_arg_validate_int_range (args .vid .u_int , -1 , (1 << 16 ) - 1 , MP_QSTR_vid );
346+ mp_arg_validate_int_range (args .pid .u_int , -1 , (1 << 16 ) - 1 , MP_QSTR_pid );
347+
348+ identification -> vid = args .vid .u_int > -1 ? args .vid .u_int : USB_VID ;
349+ identification -> pid = args .pid .u_int > -1 ? args .pid .u_int : USB_PID ;
350+
351+ mp_buffer_info_t info ;
352+ if (args .manufacturer .u_obj != mp_const_none ) {
353+ mp_get_buffer_raise (args .manufacturer .u_obj , & info , MP_BUFFER_READ );
354+ mp_arg_validate_length_range (info .len , 0 , 126 , MP_QSTR_manufacturer );
355+ memcpy (identification -> manufacturer_name , info .buf , info .len );
356+ identification -> manufacturer_name [info .len ] = 0 ;
357+ } else {
358+ memcpy (identification -> manufacturer_name , USB_MANUFACTURER , sizeof (USB_MANUFACTURER ));
359+ }
360+
361+ if (args .product .u_obj != mp_const_none ) {
362+ mp_get_buffer_raise (args .product .u_obj , & info , MP_BUFFER_READ );
363+ mp_arg_validate_length_range (info .len , 0 , 126 , MP_QSTR_product );
364+ memcpy (identification -> product_name , info .buf , info .len );
365+ identification -> product_name [info .len ] = 0 ;
366+ } else {
367+ memcpy (identification -> product_name , USB_MANUFACTURER , sizeof (USB_MANUFACTURER ));
368+ }
369+
370+ return mp_const_none ;
371+ }
372+ MP_DEFINE_CONST_FUN_OBJ_KW (supervisor_set_usb_identification_obj , 0 , supervisor_set_usb_identification );
373+ #endif
374+
314375STATIC const mp_rom_map_elem_t supervisor_module_globals_table [] = {
315376 { MP_ROM_QSTR (MP_QSTR___name__ ), MP_ROM_QSTR (MP_QSTR_supervisor ) },
316377 { MP_ROM_QSTR (MP_QSTR_enable_autoreload ), MP_ROM_PTR (& supervisor_enable_autoreload_obj ) },
@@ -325,6 +386,9 @@ STATIC const mp_rom_map_elem_t supervisor_module_globals_table[] = {
325386 { MP_ROM_QSTR (MP_QSTR_get_previous_traceback ), MP_ROM_PTR (& supervisor_get_previous_traceback_obj ) },
326387 { MP_ROM_QSTR (MP_QSTR_disable_ble_workflow ), MP_ROM_PTR (& supervisor_disable_ble_workflow_obj ) },
327388 { MP_ROM_QSTR (MP_QSTR_reset_terminal ), MP_ROM_PTR (& supervisor_reset_terminal_obj ) },
389+ #if CIRCUITPY_USB
390+ { MP_ROM_QSTR (MP_QSTR_set_usb_identification ), MP_ROM_PTR (& supervisor_set_usb_identification_obj ) },
391+ #endif
328392};
329393
330394STATIC MP_DEFINE_CONST_DICT (supervisor_module_globals , supervisor_module_globals_table );
0 commit comments