4242//|
4343//| Accumulates a Characteristic's incoming packets in a FIFO buffer and facilitates packet aware
4444//| outgoing writes. A packet's size is either the characteristic length or the maximum transmission
45- //| unit (MTU), whichever is smaller. The MTU can change so check `packet_size` before creating a
46- //| buffer to store data.
45+ //| unit (MTU) minus overhead , whichever is smaller. The MTU can change so check `incoming_packet_length`
46+ //| and `outgoing_packet_length` before creating a buffer to store data.
4747//|
4848//| When we're the server, we ignore all connections besides the first to subscribe to
4949//| notifications.
5050//|
5151//| .. class:: PacketBuffer(characteristic, *, buffer_size)
5252//|
5353//| Monitor the given Characteristic. Each time a new value is written to the Characteristic
54- //| add the newly-written bytes to a FIFO buffer.
54+ //| add the newly-written packet of bytes to a FIFO buffer.
5555//|
5656//| :param Characteristic characteristic: The Characteristic to monitor.
5757//| It may be a local Characteristic provided by a Peripheral Service, or a remote Characteristic
@@ -125,6 +125,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(bleio_packet_buffer_readinto_obj, bleio_packet_
125125//|
126126//| This does not block until the data is sent. It only blocks until the data is pending.
127127//|
128+ //| :return: number of bytes written. May include header bytes when packet is empty.
129+ //| :rtype: int
130+ //|
128131// TODO: Add a kwarg `merge=False` to dictate whether subsequent writes are merged into a pending
129132// one.
130133STATIC mp_obj_t bleio_packet_buffer_write (mp_uint_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
@@ -149,9 +152,21 @@ STATIC mp_obj_t bleio_packet_buffer_write(mp_uint_t n_args, const mp_obj_t *pos_
149152 mp_get_buffer_raise (args [ARG_header ].u_obj , & header_bufinfo , MP_BUFFER_READ );
150153 }
151154
152- common_hal_bleio_packet_buffer_write (self , data_bufinfo .buf , data_bufinfo .len ,
153- header_bufinfo .buf , header_bufinfo .len );
154- return mp_const_none ;
155+ mp_int_t num_bytes_written = common_hal_bleio_packet_buffer_write (
156+ self , data_bufinfo .buf , data_bufinfo .len , header_bufinfo .buf , header_bufinfo .len );
157+ if (num_bytes_written < 0 ) {
158+ // TODO: Raise an error if not connected. Right now the not-connected error
159+ // is unreliable, because common_hal_bleio_packet_buffer_write()
160+ // checks for conn_handle being set, but setting that
161+ // can be delayed because conn_handle is discovered by spying on
162+ // gatts write events, which may not have been sent yet.
163+ //
164+ // IDEAL:
165+ // mp_raise_bleio_ConnectionError(translate("Not connected"));
166+ // TEMPORARY:
167+ num_bytes_written = 0 ;
168+ }
169+ return MP_OBJ_NEW_SMALL_INT (num_bytes_written );
155170}
156171STATIC MP_DEFINE_CONST_FUN_OBJ_KW (bleio_packet_buffer_write_obj , 1 , bleio_packet_buffer_write );
157172
@@ -168,37 +183,72 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_packet_buffer_deinit_obj, bleio_packet_bu
168183
169184//| .. attribute:: packet_size
170185//|
171- //| Maximum size of a packet.
186+ //| `packet_size` is the same as `incoming_packet_length`.
187+ //| The name `packet_size` is deprecated and
188+ //| will be removed in CircuitPython 6.0.0.
189+ //|
190+ //| .. attribute:: incoming_packet_length
191+ //|
192+ //| Maximum length in bytes of a packet we are reading.
172193//| If the packet is arriving from a remote service via notify or indicate,
173- //| the maximum size is `Connection.max_packet_length`.
194+ //| the maximum length is `Connection.max_packet_length`.
195+ //| Otherwise it is the ``max_length`` of the :py:class:`~_bleio.Characteristic`.
196+ //|
197+ STATIC mp_obj_t bleio_packet_buffer_get_incoming_packet_length (mp_obj_t self_in ) {
198+ bleio_packet_buffer_obj_t * self = MP_OBJ_TO_PTR (self_in );
199+
200+ mp_int_t size = common_hal_bleio_packet_buffer_get_incoming_packet_length (self );
201+ if (size < 0 ) {
202+ mp_raise_ValueError (translate ("No connection: size cannot be determined" ));
203+ }
204+ return MP_OBJ_NEW_SMALL_INT (size );
205+ }
206+ STATIC MP_DEFINE_CONST_FUN_OBJ_1 (bleio_packet_buffer_get_incoming_packet_length_obj , bleio_packet_buffer_get_incoming_packet_length );
207+
208+ const mp_obj_property_t bleio_packet_buffer_incoming_packet_length_obj = {
209+ .base .type = & mp_type_property ,
210+ .proxy = { (mp_obj_t )& bleio_packet_buffer_get_incoming_packet_length_obj ,
211+ (mp_obj_t )& mp_const_none_obj ,
212+ (mp_obj_t )& mp_const_none_obj },
213+ };
214+
215+ //| .. attribute:: outgoing_packet_length
216+ //|
217+ //| Maximum length in bytes of a packet we are writing.
218+ //| If the packet is being sent via notify or indicate,
219+ //| the maximum length is `Connection.max_packet_length`.
174220//| Otherwise it is the ``max_length`` of the :py:class:`~_bleio.Characteristic`.
175221//|
176- STATIC mp_obj_t bleio_packet_buffer_get_packet_size (mp_obj_t self_in ) {
222+ STATIC mp_obj_t bleio_packet_buffer_get_outgoing_packet_length (mp_obj_t self_in ) {
177223 bleio_packet_buffer_obj_t * self = MP_OBJ_TO_PTR (self_in );
178224
179- mp_int_t size = common_hal_bleio_packet_buffer_get_packet_size (self );
225+ mp_int_t size = common_hal_bleio_packet_buffer_get_outgoing_packet_length (self );
180226 if (size < 0 ) {
181227 mp_raise_ValueError (translate ("No connection: size cannot be determined" ));
182228 }
183229 return MP_OBJ_NEW_SMALL_INT (size );
184230}
185- STATIC MP_DEFINE_CONST_FUN_OBJ_1 (bleio_packet_buffer_get_packet_size_obj , bleio_packet_buffer_get_packet_size );
231+ STATIC MP_DEFINE_CONST_FUN_OBJ_1 (bleio_packet_buffer_get_outgoing_packet_length_obj , bleio_packet_buffer_get_outgoing_packet_length );
186232
187- const mp_obj_property_t bleio_packet_buffer_packet_size_obj = {
233+ const mp_obj_property_t bleio_packet_buffer_outgoing_packet_length_obj = {
188234 .base .type = & mp_type_property ,
189- .proxy = { (mp_obj_t )& bleio_packet_buffer_get_packet_size_obj ,
235+ .proxy = { (mp_obj_t )& bleio_packet_buffer_get_outgoing_packet_length_obj ,
190236 (mp_obj_t )& mp_const_none_obj ,
191237 (mp_obj_t )& mp_const_none_obj },
192238};
193239
194240STATIC const mp_rom_map_elem_t bleio_packet_buffer_locals_dict_table [] = {
195- { MP_ROM_QSTR (MP_QSTR_deinit ), MP_ROM_PTR (& bleio_packet_buffer_deinit_obj ) },
241+ { MP_ROM_QSTR (MP_QSTR_deinit ), MP_ROM_PTR (& bleio_packet_buffer_deinit_obj ) },
196242
197243 // Standard stream methods.
198- { MP_OBJ_NEW_QSTR (MP_QSTR_readinto ), MP_ROM_PTR (& bleio_packet_buffer_readinto_obj ) },
199- { MP_OBJ_NEW_QSTR (MP_QSTR_write ), MP_ROM_PTR (& bleio_packet_buffer_write_obj ) },
244+ { MP_OBJ_NEW_QSTR (MP_QSTR_readinto ), MP_ROM_PTR (& bleio_packet_buffer_readinto_obj ) },
245+ { MP_OBJ_NEW_QSTR (MP_QSTR_write ), MP_ROM_PTR (& bleio_packet_buffer_write_obj ) },
200246
201- { MP_OBJ_NEW_QSTR (MP_QSTR_packet_size ), MP_ROM_PTR (& bleio_packet_buffer_packet_size_obj ) },
247+ // .packet_size is now an alias for .incoming_packet_length
248+ // TODO: It will be removed in 6.0.0.
249+ { MP_OBJ_NEW_QSTR (MP_QSTR_packet_size ), MP_ROM_PTR (& bleio_packet_buffer_incoming_packet_length_obj ) },
250+ { MP_OBJ_NEW_QSTR (MP_QSTR_incoming_packet_length ), MP_ROM_PTR (& bleio_packet_buffer_incoming_packet_length_obj ) },
251+ { MP_OBJ_NEW_QSTR (MP_QSTR_outgoing_packet_length ), MP_ROM_PTR (& bleio_packet_buffer_outgoing_packet_length_obj ) },
202252};
203253
204254STATIC MP_DEFINE_CONST_DICT (bleio_packet_buffer_locals_dict , bleio_packet_buffer_locals_dict_table );
0 commit comments