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