1212#include "shared-bindings/_bleio/Characteristic.h"
1313#include "shared-bindings/_bleio/Service.h"
1414#include "shared-bindings/_bleio/UUID.h"
15+ #include "shared-bindings/util.h"
16+
1517
1618//| class Characteristic:
1719//| """Stores information about a BLE service characteristic and allows reading
@@ -137,14 +139,29 @@ static mp_obj_t bleio_characteristic_add_to_service(size_t n_args, const mp_obj_
137139static MP_DEFINE_CONST_FUN_OBJ_KW (bleio_characteristic_add_to_service_fun_obj , 1 , bleio_characteristic_add_to_service ) ;
138140static MP_DEFINE_CONST_CLASSMETHOD_OBJ (bleio_characteristic_add_to_service_obj , MP_ROM_PTR (& bleio_characteristic_add_to_service_fun_obj )) ;
139141
142+ //| def deinit(self) -> None:
143+ //| """Deinitialises the Characteristic and releases any hardware resources for reuse."""
144+ //| ...
145+ static mp_obj_t bleio_characteristic_deinit (mp_obj_t self_in ) {
146+ bleio_characteristic_obj_t * self = MP_OBJ_TO_PTR (self_in );
147+ common_hal_bleio_characteristic_deinit (self );
148+ return mp_const_none ;
149+ }
150+ static MP_DEFINE_CONST_FUN_OBJ_1 (bleio_characteristic_deinit_obj , bleio_characteristic_deinit ) ;
140151
152+ static void check_for_deinit (bleio_characteristic_obj_t * self ) {
153+ if (common_hal_bleio_characteristic_deinited (self )) {
154+ raise_deinited_error ();
155+ }
156+ }
141157
142158//| properties: int
143159//| """An int bitmask representing which properties are set, specified as bitwise or'ing of
144160//| of these possible values.
145161//| `BROADCAST`, `INDICATE`, `NOTIFY`, `READ`, `WRITE`, `WRITE_NO_RESPONSE`."""
146162static mp_obj_t bleio_characteristic_get_properties (mp_obj_t self_in ) {
147163 bleio_characteristic_obj_t * self = MP_OBJ_TO_PTR (self_in );
164+ check_for_deinit (self );
148165
149166 return MP_OBJ_NEW_SMALL_INT (common_hal_bleio_characteristic_get_properties (self ));
150167}
@@ -159,6 +176,7 @@ MP_PROPERTY_GETTER(bleio_characteristic_properties_obj,
159176//| Will be ``None`` if the 128-bit UUID for this characteristic is not known."""
160177static mp_obj_t bleio_characteristic_get_uuid (mp_obj_t self_in ) {
161178 bleio_characteristic_obj_t * self = MP_OBJ_TO_PTR (self_in );
179+ check_for_deinit (self );
162180
163181 bleio_uuid_obj_t * uuid = common_hal_bleio_characteristic_get_uuid (self );
164182 return uuid ? MP_OBJ_FROM_PTR (uuid ) : mp_const_none ;
@@ -172,6 +190,7 @@ MP_PROPERTY_GETTER(bleio_characteristic_uuid_obj,
172190//| """The value of this characteristic."""
173191static mp_obj_t bleio_characteristic_get_value (mp_obj_t self_in ) {
174192 bleio_characteristic_obj_t * self = MP_OBJ_TO_PTR (self_in );
193+ check_for_deinit (self );
175194
176195 uint8_t temp [512 ];
177196 size_t actual_len = common_hal_bleio_characteristic_get_value (self , temp , sizeof (temp ));
@@ -181,6 +200,7 @@ static MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_value_obj, bleio_chara
181200
182201static mp_obj_t bleio_characteristic_set_value (mp_obj_t self_in , mp_obj_t value_in ) {
183202 bleio_characteristic_obj_t * self = MP_OBJ_TO_PTR (self_in );
203+ check_for_deinit (self );
184204
185205 mp_buffer_info_t bufinfo ;
186206 mp_get_buffer_raise (value_in , & bufinfo , MP_BUFFER_READ );
@@ -199,6 +219,7 @@ MP_PROPERTY_GETSET(bleio_characteristic_value_obj,
199219//| """The max length of this characteristic."""
200220static mp_obj_t bleio_characteristic_get_max_length (mp_obj_t self_in ) {
201221 bleio_characteristic_obj_t * self = MP_OBJ_TO_PTR (self_in );
222+ check_for_deinit (self );
202223
203224 return MP_OBJ_NEW_SMALL_INT (common_hal_bleio_characteristic_get_max_length (self ));
204225}
@@ -211,6 +232,8 @@ MP_PROPERTY_GETTER(bleio_characteristic_max_length_obj,
211232//| """A tuple of :py:class:`Descriptor` objects related to this characteristic. (read-only)"""
212233static mp_obj_t bleio_characteristic_get_descriptors (mp_obj_t self_in ) {
213234 bleio_characteristic_obj_t * self = MP_OBJ_TO_PTR (self_in );
235+ check_for_deinit (self );
236+
214237 // Return list as a tuple so user won't be able to change it.
215238 return MP_OBJ_FROM_PTR (common_hal_bleio_characteristic_get_descriptors (self ));
216239}
@@ -224,6 +247,7 @@ MP_PROPERTY_GETTER(bleio_characteristic_descriptors_obj,
224247//| """The Service this Characteristic is a part of."""
225248static mp_obj_t bleio_characteristic_get_service (mp_obj_t self_in ) {
226249 bleio_characteristic_obj_t * self = MP_OBJ_TO_PTR (self_in );
250+ check_for_deinit (self );
227251
228252 return common_hal_bleio_characteristic_get_service (self );
229253}
@@ -241,6 +265,7 @@ MP_PROPERTY_GETTER(bleio_characteristic_service_obj,
241265//| ...
242266static mp_obj_t bleio_characteristic_set_cccd (mp_uint_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
243267 bleio_characteristic_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
268+ check_for_deinit (self );
244269
245270 enum { ARG_notify , ARG_indicate };
246271 static const mp_arg_t allowed_args [] = {
@@ -258,6 +283,9 @@ static mp_obj_t bleio_characteristic_set_cccd(mp_uint_t n_args, const mp_obj_t *
258283static MP_DEFINE_CONST_FUN_OBJ_KW (bleio_characteristic_set_cccd_obj , 1 , bleio_characteristic_set_cccd ) ;
259284
260285static const mp_rom_map_elem_t bleio_characteristic_locals_dict_table [] = {
286+ { MP_ROM_QSTR (MP_QSTR_deinit ), MP_ROM_PTR (& bleio_characteristic_deinit_obj ) },
287+ { MP_ROM_QSTR (MP_QSTR___del__ ), MP_ROM_PTR (& bleio_characteristic_deinit_obj ) },
288+
261289 { MP_ROM_QSTR (MP_QSTR_add_to_service ), MP_ROM_PTR (& bleio_characteristic_add_to_service_obj ) },
262290 { MP_ROM_QSTR (MP_QSTR_descriptors ), MP_ROM_PTR (& bleio_characteristic_descriptors_obj ) },
263291 { MP_ROM_QSTR (MP_QSTR_properties ), MP_ROM_PTR (& bleio_characteristic_properties_obj ) },
0 commit comments