3939
4040#include "py/objproperty.h"
4141#include "shared-bindings/usb/core/Device.h"
42+ #include "shared-bindings/util.h"
4243#include "py/runtime.h"
4344
4445//| class Device:
4950//| ...
5051//|
5152
53+ static void check_for_deinit (usb_core_device_obj_t * self ) {
54+ if (common_hal_usb_core_device_deinited (self )) {
55+ raise_deinited_error ();
56+ }
57+ }
58+
5259//| def __del__(self) -> None:
5360//| """Closes any resources used for this device."""
5461//| ...
@@ -64,6 +71,7 @@ static MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_deinit_obj, usb_core_device_dei
6471//| """The USB vendor ID of the device"""
6572static mp_obj_t usb_core_device_obj_get_idVendor (mp_obj_t self_in ) {
6673 usb_core_device_obj_t * self = MP_OBJ_TO_PTR (self_in );
74+ check_for_deinit (self );
6775 return MP_OBJ_NEW_SMALL_INT (common_hal_usb_core_device_get_idVendor (self ));
6876}
6977MP_DEFINE_CONST_FUN_OBJ_1 (usb_core_device_get_idVendor_obj , usb_core_device_obj_get_idVendor );
@@ -75,6 +83,7 @@ MP_PROPERTY_GETTER(usb_core_device_idVendor_obj,
7583//| """The USB product ID of the device"""
7684static mp_obj_t usb_core_device_obj_get_idProduct (mp_obj_t self_in ) {
7785 usb_core_device_obj_t * self = MP_OBJ_TO_PTR (self_in );
86+ check_for_deinit (self );
7887 return MP_OBJ_NEW_SMALL_INT (common_hal_usb_core_device_get_idProduct (self ));
7988}
8089MP_DEFINE_CONST_FUN_OBJ_1 (usb_core_device_get_idProduct_obj , usb_core_device_obj_get_idProduct );
@@ -86,6 +95,7 @@ MP_PROPERTY_GETTER(usb_core_device_idProduct_obj,
8695//| """The USB device's serial number string."""
8796static mp_obj_t usb_core_device_obj_get_serial_number (mp_obj_t self_in ) {
8897 usb_core_device_obj_t * self = MP_OBJ_TO_PTR (self_in );
98+ check_for_deinit (self );
8999 return common_hal_usb_core_device_get_serial_number (self );
90100}
91101MP_DEFINE_CONST_FUN_OBJ_1 (usb_core_device_get_serial_number_obj , usb_core_device_obj_get_serial_number );
@@ -97,6 +107,7 @@ MP_PROPERTY_GETTER(usb_core_device_serial_number_obj,
97107//| """The USB device's product string."""
98108static mp_obj_t usb_core_device_obj_get_product (mp_obj_t self_in ) {
99109 usb_core_device_obj_t * self = MP_OBJ_TO_PTR (self_in );
110+ check_for_deinit (self );
100111 return common_hal_usb_core_device_get_product (self );
101112}
102113MP_DEFINE_CONST_FUN_OBJ_1 (usb_core_device_get_product_obj , usb_core_device_obj_get_product );
@@ -109,6 +120,7 @@ MP_PROPERTY_GETTER(usb_core_device_product_obj,
109120//|
110121static mp_obj_t usb_core_device_obj_get_manufacturer (mp_obj_t self_in ) {
111122 usb_core_device_obj_t * self = MP_OBJ_TO_PTR (self_in );
123+ check_for_deinit (self );
112124 return common_hal_usb_core_device_get_manufacturer (self );
113125}
114126MP_DEFINE_CONST_FUN_OBJ_1 (usb_core_device_get_manufacturer_obj , usb_core_device_obj_get_manufacturer );
@@ -121,6 +133,7 @@ MP_PROPERTY_GETTER(usb_core_device_manufacturer_obj,
121133//|
122134static mp_obj_t usb_core_device_obj_get_bus (mp_obj_t self_in ) {
123135 usb_core_device_obj_t * self = MP_OBJ_TO_PTR (self_in );
136+ check_for_deinit (self );
124137 return MP_OBJ_NEW_SMALL_INT (common_hal_usb_core_device_get_bus (self ));
125138}
126139MP_DEFINE_CONST_FUN_OBJ_1 (usb_core_device_get_bus_obj , usb_core_device_obj_get_bus );
@@ -134,6 +147,7 @@ MP_PROPERTY_GETTER(usb_core_device_bus_obj,
134147//|
135148static mp_obj_t usb_core_device_obj_get_port_numbers (mp_obj_t self_in ) {
136149 usb_core_device_obj_t * self = MP_OBJ_TO_PTR (self_in );
150+ check_for_deinit (self );
137151 return common_hal_usb_core_device_get_port_numbers (self );
138152}
139153MP_DEFINE_CONST_FUN_OBJ_1 (usb_core_device_get_port_numbers_obj , usb_core_device_obj_get_port_numbers );
@@ -147,6 +161,7 @@ MP_PROPERTY_GETTER(usb_core_device_port_numbers_obj,
147161//|
148162static mp_obj_t usb_core_device_obj_get_speed (mp_obj_t self_in ) {
149163 usb_core_device_obj_t * self = MP_OBJ_TO_PTR (self_in );
164+ check_for_deinit (self );
150165 return MP_OBJ_NEW_SMALL_INT (common_hal_usb_core_device_get_speed (self ));
151166}
152167MP_DEFINE_CONST_FUN_OBJ_1 (usb_core_device_get_speed_obj , usb_core_device_obj_get_speed );
@@ -171,6 +186,7 @@ static mp_obj_t usb_core_device_set_configuration(size_t n_args, const mp_obj_t
171186 { MP_QSTR_configuration , MP_ARG_INT , {.u_int = 1 } },
172187 };
173188 usb_core_device_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
189+ check_for_deinit (self );
174190 mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
175191 mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
176192
@@ -197,6 +213,7 @@ static mp_obj_t usb_core_device_write(size_t n_args, const mp_obj_t *pos_args, m
197213 { MP_QSTR_timeout , MP_ARG_INT , {.u_int = 0 } },
198214 };
199215 usb_core_device_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
216+ check_for_deinit (self );
200217 mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
201218 mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
202219
@@ -228,6 +245,7 @@ static mp_obj_t usb_core_device_read(size_t n_args, const mp_obj_t *pos_args, mp
228245 { MP_QSTR_timeout , MP_ARG_INT , {.u_int = 0 } },
229246 };
230247 usb_core_device_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
248+ check_for_deinit (self );
231249 mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
232250 mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
233251
@@ -277,6 +295,7 @@ static mp_obj_t usb_core_device_ctrl_transfer(size_t n_args, const mp_obj_t *pos
277295 { MP_QSTR_timeout , MP_ARG_INT , {.u_int = 0 } },
278296 };
279297 usb_core_device_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
298+ check_for_deinit (self );
280299 mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
281300 mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
282301
@@ -310,6 +329,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(usb_core_device_ctrl_transfer_obj, 2, usb_core_device
310329//|
311330static mp_obj_t usb_core_device_is_kernel_driver_active (mp_obj_t self_in , mp_obj_t interface_in ) {
312331 usb_core_device_obj_t * self = MP_OBJ_TO_PTR (self_in );
332+ check_for_deinit (self );
313333 mp_int_t interface = mp_obj_get_int (interface_in );
314334 bool active = common_hal_usb_core_device_is_kernel_driver_active (self , interface );
315335 return mp_obj_new_bool (active );
@@ -327,6 +347,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(usb_core_device_is_kernel_driver_active_obj, usb_core_
327347//|
328348static mp_obj_t usb_core_device_detach_kernel_driver (mp_obj_t self_in , mp_obj_t interface_in ) {
329349 usb_core_device_obj_t * self = MP_OBJ_TO_PTR (self_in );
350+ check_for_deinit (self );
330351 mp_int_t interface = mp_obj_get_int (interface_in );
331352 common_hal_usb_core_device_detach_kernel_driver (self , interface );
332353 return mp_const_none ;
@@ -343,6 +364,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(usb_core_device_detach_kernel_driver_obj, usb_core_dev
343364//|
344365static mp_obj_t usb_core_device_attach_kernel_driver (mp_obj_t self_in , mp_obj_t interface_in ) {
345366 usb_core_device_obj_t * self = MP_OBJ_TO_PTR (self_in );
367+ check_for_deinit (self );
346368 mp_int_t interface = mp_obj_get_int (interface_in );
347369 common_hal_usb_core_device_attach_kernel_driver (self , interface );
348370 return mp_const_none ;
0 commit comments