2019-12-06 15:27:46 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019 Scott Shawcroft for Adafruit Industries
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/mperrno.h"
|
|
|
|
#include "py/ioctl.h"
|
|
|
|
#include "py/objproperty.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/stream.h"
|
|
|
|
|
|
|
|
#include "shared-bindings/_bleio/__init__.h"
|
|
|
|
#include "shared-bindings/_bleio/PacketBuffer.h"
|
|
|
|
#include "shared-bindings/_bleio/UUID.h"
|
|
|
|
#include "shared-bindings/util.h"
|
|
|
|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| class PacketBuffer:
|
2020-05-12 20:15:28 -04:00
|
|
|
//| """Accumulates a Characteristic's incoming packets in a FIFO buffer and facilitates packet aware
|
2020-05-01 18:23:27 -04:00
|
|
|
//| outgoing writes. A packet's size is either the characteristic length or the maximum transmission
|
2020-05-12 12:26:02 -04:00
|
|
|
//| unit (MTU) minus overhead, whichever is smaller. The MTU can change so check `incoming_packet_length`
|
|
|
|
//| and `outgoing_packet_length` before creating a buffer to store data.
|
2019-12-06 15:27:46 -05:00
|
|
|
//|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| When we're the server, we ignore all connections besides the first to subscribe to
|
|
|
|
//| notifications."""
|
2019-12-06 15:27:46 -05:00
|
|
|
//|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| def __init__(self, characteristic: Characteristic, *, buffer_size: int):
|
|
|
|
//| """Monitor the given Characteristic. Each time a new value is written to the Characteristic
|
|
|
|
//| add the newly-written bytes to a FIFO buffer.
|
2019-12-06 15:27:46 -05:00
|
|
|
//|
|
2020-05-12 12:26:02 -04:00
|
|
|
//| Monitor the given Characteristic. Each time a new value is written to the Characteristic
|
|
|
|
//| add the newly-written packet of bytes to a FIFO buffer.
|
2019-12-06 15:27:46 -05:00
|
|
|
//|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| :param Characteristic characteristic: The Characteristic to monitor.
|
|
|
|
//| It may be a local Characteristic provided by a Peripheral Service, or a remote Characteristic
|
|
|
|
//| in a remote Service that a Central has connected to.
|
|
|
|
//| :param int buffer_size: Size of ring buffer (in packets of the Characteristic's maximum
|
2020-05-12 20:15:28 -04:00
|
|
|
//| length) that stores incoming packets coming from the peer."""
|
2020-05-01 18:23:27 -04:00
|
|
|
//| ...
|
2019-12-06 15:27:46 -05:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t bleio_packet_buffer_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_characteristic, ARG_buffer_size };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_characteristic, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_buffer_size, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
|
|
|
|
};
|
|
|
|
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
|
|
|
const mp_obj_t characteristic = args[ARG_characteristic].u_obj;
|
|
|
|
|
2020-04-29 17:44:10 -04:00
|
|
|
const mp_int_t buffer_size = args[ARG_buffer_size].u_int;
|
2019-12-06 15:27:46 -05:00
|
|
|
if (buffer_size < 1) {
|
|
|
|
mp_raise_ValueError_varg(translate("%q must be >= 1"), MP_QSTR_buffer_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!MP_OBJ_IS_TYPE(characteristic, &bleio_characteristic_type)) {
|
|
|
|
mp_raise_TypeError(translate("Expected a Characteristic"));
|
|
|
|
}
|
|
|
|
|
|
|
|
bleio_packet_buffer_obj_t *self = m_new_obj(bleio_packet_buffer_obj_t);
|
|
|
|
self->base.type = &bleio_packet_buffer_type;
|
|
|
|
|
|
|
|
common_hal_bleio_packet_buffer_construct(self, MP_OBJ_TO_PTR(characteristic), buffer_size);
|
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void check_for_deinit(bleio_packet_buffer_obj_t *self) {
|
|
|
|
if (common_hal_bleio_packet_buffer_deinited(self)) {
|
|
|
|
raise_deinited_error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 17:47:52 -04:00
|
|
|
//| def readinto(self, buf: WriteableBuffer) -> int:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Reads a single BLE packet into the ``buf``. Raises an exception if the next packet is longer
|
|
|
|
//| than the given buffer. Use `packet_size` to read the maximum length of a single packet.
|
2019-12-06 15:27:46 -05:00
|
|
|
//|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| :return: number of bytes read and stored into ``buf``
|
|
|
|
//| :rtype: int"""
|
|
|
|
//| ...
|
2019-12-06 15:27:46 -05:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t bleio_packet_buffer_readinto(mp_obj_t self_in, mp_obj_t buffer_obj) {
|
|
|
|
bleio_packet_buffer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
check_for_deinit(self);
|
|
|
|
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(buffer_obj, &bufinfo, MP_BUFFER_WRITE);
|
|
|
|
|
2020-04-29 17:44:10 -04:00
|
|
|
mp_int_t size = common_hal_bleio_packet_buffer_readinto(self, bufinfo.buf, bufinfo.len);
|
2020-03-25 20:41:47 -04:00
|
|
|
if (size < 0) {
|
|
|
|
mp_raise_ValueError_varg(translate("Buffer too short by %d bytes"), size * -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(size);
|
2019-12-06 15:27:46 -05:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(bleio_packet_buffer_readinto_obj, bleio_packet_buffer_readinto);
|
|
|
|
|
2020-07-02 14:11:25 -04:00
|
|
|
//| def write(self, data: bytes, *, header: bytes = None) -> int:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Writes all bytes from data into the same outgoing packet. The bytes from header are included
|
|
|
|
//| before data when the pending packet is currently empty.
|
2019-12-06 15:27:46 -05:00
|
|
|
//|
|
2020-05-12 20:15:28 -04:00
|
|
|
//| This does not block until the data is sent. It only blocks until the data is pending.
|
2019-12-06 15:27:46 -05:00
|
|
|
//|
|
2020-05-12 20:15:28 -04:00
|
|
|
//| :return: number of bytes written. May include header bytes when packet is empty.
|
|
|
|
//| :rtype: int"""
|
|
|
|
//| ...
|
2020-05-04 14:29:37 -04:00
|
|
|
//|
|
2019-12-06 15:27:46 -05:00
|
|
|
// TODO: Add a kwarg `merge=False` to dictate whether subsequent writes are merged into a pending
|
|
|
|
// one.
|
|
|
|
STATIC mp_obj_t bleio_packet_buffer_write(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_data, ARG_header };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_header, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
|
|
|
|
};
|
|
|
|
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
|
|
|
bleio_packet_buffer_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
|
|
|
check_for_deinit(self);
|
|
|
|
|
|
|
|
mp_buffer_info_t data_bufinfo;
|
|
|
|
mp_get_buffer_raise(args[ARG_data].u_obj, &data_bufinfo, MP_BUFFER_READ);
|
|
|
|
|
|
|
|
mp_buffer_info_t header_bufinfo;
|
|
|
|
header_bufinfo.len = 0;
|
|
|
|
if (args[ARG_header].u_obj != MP_OBJ_NULL) {
|
|
|
|
mp_get_buffer_raise(args[ARG_header].u_obj, &header_bufinfo, MP_BUFFER_READ);
|
|
|
|
}
|
|
|
|
|
2020-05-04 14:29:37 -04:00
|
|
|
mp_int_t num_bytes_written = common_hal_bleio_packet_buffer_write(
|
|
|
|
self, data_bufinfo.buf, data_bufinfo.len, header_bufinfo.buf, header_bufinfo.len);
|
|
|
|
if (num_bytes_written < 0) {
|
|
|
|
// TODO: Raise an error if not connected. Right now the not-connected error
|
|
|
|
// is unreliable, because common_hal_bleio_packet_buffer_write()
|
|
|
|
// checks for conn_handle being set, but setting that
|
|
|
|
// can be delayed because conn_handle is discovered by spying on
|
|
|
|
// gatts write events, which may not have been sent yet.
|
|
|
|
//
|
|
|
|
// IDEAL:
|
|
|
|
// mp_raise_bleio_ConnectionError(translate("Not connected"));
|
|
|
|
// TEMPORARY:
|
|
|
|
num_bytes_written = 0;
|
|
|
|
}
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(num_bytes_written);
|
2019-12-06 15:27:46 -05:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_packet_buffer_write_obj, 1, bleio_packet_buffer_write);
|
|
|
|
|
2020-07-02 14:11:25 -04:00
|
|
|
//| def deinit(self) -> None:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Disable permanently."""
|
|
|
|
//| ...
|
2019-12-06 15:27:46 -05:00
|
|
|
STATIC mp_obj_t bleio_packet_buffer_deinit(mp_obj_t self_in) {
|
|
|
|
bleio_packet_buffer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
common_hal_bleio_packet_buffer_deinit(self);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_packet_buffer_deinit_obj, bleio_packet_buffer_deinit);
|
|
|
|
|
2020-05-12 20:15:28 -04:00
|
|
|
//| packet_size: int = ...
|
2020-05-12 12:26:02 -04:00
|
|
|
//| """`packet_size` is the same as `incoming_packet_length`.
|
2020-05-04 14:29:37 -04:00
|
|
|
//| The name `packet_size` is deprecated and
|
2020-05-12 12:26:02 -04:00
|
|
|
//| will be removed in CircuitPython 6.0.0."""
|
2020-05-04 14:29:37 -04:00
|
|
|
//|
|
2020-07-02 14:11:25 -04:00
|
|
|
//| incoming_packet_length: int = ...
|
2020-05-12 12:26:02 -04:00
|
|
|
//| """Maximum length in bytes of a packet we are reading."""
|
2020-05-04 14:29:37 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t bleio_packet_buffer_get_incoming_packet_length(mp_obj_t self_in) {
|
|
|
|
bleio_packet_buffer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
|
|
|
mp_int_t size = common_hal_bleio_packet_buffer_get_incoming_packet_length(self);
|
|
|
|
if (size < 0) {
|
2020-05-04 19:51:08 -04:00
|
|
|
mp_raise_ValueError(translate("No connection: length cannot be determined"));
|
2020-05-04 14:29:37 -04:00
|
|
|
}
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(size);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_packet_buffer_get_incoming_packet_length_obj, bleio_packet_buffer_get_incoming_packet_length);
|
|
|
|
|
|
|
|
const mp_obj_property_t bleio_packet_buffer_incoming_packet_length_obj = {
|
|
|
|
.base.type = &mp_type_property,
|
|
|
|
.proxy = { (mp_obj_t)&bleio_packet_buffer_get_incoming_packet_length_obj,
|
|
|
|
(mp_obj_t)&mp_const_none_obj,
|
|
|
|
(mp_obj_t)&mp_const_none_obj },
|
|
|
|
};
|
|
|
|
|
2020-05-12 20:15:28 -04:00
|
|
|
//| outgoing_packet_length: int = ...
|
|
|
|
//| """Maximum length in bytes of a packet we are writing."""
|
2019-12-06 15:27:46 -05:00
|
|
|
//|
|
2020-05-04 14:29:37 -04:00
|
|
|
STATIC mp_obj_t bleio_packet_buffer_get_outgoing_packet_length(mp_obj_t self_in) {
|
2019-12-06 15:27:46 -05:00
|
|
|
bleio_packet_buffer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
2020-05-04 14:29:37 -04:00
|
|
|
mp_int_t size = common_hal_bleio_packet_buffer_get_outgoing_packet_length(self);
|
2020-04-29 17:44:10 -04:00
|
|
|
if (size < 0) {
|
2020-05-04 19:51:08 -04:00
|
|
|
mp_raise_ValueError(translate("No connection: length cannot be determined"));
|
2020-04-29 17:44:10 -04:00
|
|
|
}
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(size);
|
2019-12-06 15:27:46 -05:00
|
|
|
}
|
2020-05-04 14:29:37 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_packet_buffer_get_outgoing_packet_length_obj, bleio_packet_buffer_get_outgoing_packet_length);
|
2019-12-06 15:27:46 -05:00
|
|
|
|
2020-05-04 14:29:37 -04:00
|
|
|
const mp_obj_property_t bleio_packet_buffer_outgoing_packet_length_obj = {
|
2019-12-06 15:27:46 -05:00
|
|
|
.base.type = &mp_type_property,
|
2020-05-04 14:29:37 -04:00
|
|
|
.proxy = { (mp_obj_t)&bleio_packet_buffer_get_outgoing_packet_length_obj,
|
2019-12-06 15:27:46 -05:00
|
|
|
(mp_obj_t)&mp_const_none_obj,
|
|
|
|
(mp_obj_t)&mp_const_none_obj },
|
|
|
|
};
|
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t bleio_packet_buffer_locals_dict_table[] = {
|
2020-05-04 14:29:37 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&bleio_packet_buffer_deinit_obj) },
|
2019-12-06 15:27:46 -05:00
|
|
|
|
|
|
|
// Standard stream methods.
|
2020-05-04 14:29:37 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&bleio_packet_buffer_readinto_obj) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), MP_ROM_PTR(&bleio_packet_buffer_write_obj) },
|
2019-12-06 15:27:46 -05:00
|
|
|
|
2020-05-04 14:29:37 -04:00
|
|
|
// .packet_size is now an alias for .incoming_packet_length
|
2020-05-04 23:05:47 -04:00
|
|
|
// TODO: Remove in 6.0.0.
|
2020-05-04 14:29:37 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_packet_size), MP_ROM_PTR(&bleio_packet_buffer_incoming_packet_length_obj) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_incoming_packet_length), MP_ROM_PTR(&bleio_packet_buffer_incoming_packet_length_obj) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_outgoing_packet_length), MP_ROM_PTR(&bleio_packet_buffer_outgoing_packet_length_obj) },
|
2019-12-06 15:27:46 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(bleio_packet_buffer_locals_dict, bleio_packet_buffer_locals_dict_table);
|
|
|
|
|
|
|
|
|
|
|
|
const mp_obj_type_t bleio_packet_buffer_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_PacketBuffer,
|
|
|
|
.make_new = bleio_packet_buffer_make_new,
|
|
|
|
.locals_dict = (mp_obj_dict_t*)&bleio_packet_buffer_locals_dict
|
|
|
|
};
|