2019-01-07 22:46:20 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2018 Dan Halbert 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.
|
|
|
|
*/
|
|
|
|
|
2019-01-19 19:45:35 -05:00
|
|
|
#include "py/mperrno.h"
|
2022-10-14 13:14:06 -04:00
|
|
|
#include "py/stream.h"
|
2019-01-07 22:46:20 -05:00
|
|
|
#include "py/objproperty.h"
|
|
|
|
#include "py/runtime.h"
|
2019-01-19 19:45:35 -05:00
|
|
|
#include "py/stream.h"
|
|
|
|
|
2019-11-20 16:53:50 -05:00
|
|
|
#include "shared-bindings/_bleio/__init__.h"
|
2019-08-29 18:44:27 -04:00
|
|
|
#include "shared-bindings/_bleio/CharacteristicBuffer.h"
|
|
|
|
#include "shared-bindings/_bleio/UUID.h"
|
2019-01-19 19:45:35 -05:00
|
|
|
#include "shared-bindings/util.h"
|
|
|
|
|
|
|
|
STATIC void raise_error_if_not_connected(bleio_characteristic_buffer_obj_t *self) {
|
|
|
|
if (!common_hal_bleio_characteristic_buffer_connected(self)) {
|
2023-10-30 04:49:06 -04:00
|
|
|
mp_raise_ConnectionError(MP_ERROR_TEXT("Not connected"));
|
2019-01-19 19:45:35 -05:00
|
|
|
}
|
|
|
|
}
|
2019-01-07 22:46:20 -05:00
|
|
|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| class CharacteristicBuffer:
|
2020-05-12 20:15:28 -04:00
|
|
|
//| """Accumulates a Characteristic's incoming values in a FIFO buffer."""
|
2019-01-07 22:46:20 -05:00
|
|
|
//|
|
2022-09-27 16:21:42 -04:00
|
|
|
//| def __init__(
|
|
|
|
//| self, characteristic: Characteristic, *, timeout: int = 1, buffer_size: int = 64
|
|
|
|
//| ) -> None:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Monitor the given Characteristic. Each time a new value is written to the Characteristic
|
|
|
|
//| add the newly-written bytes to a FIFO buffer.
|
2019-01-07 22:46:20 -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 timeout: the timeout in seconds to wait for the first character and between subsequent characters.
|
|
|
|
//| :param int buffer_size: Size of ring buffer that stores incoming data coming from client.
|
|
|
|
//| Must be >= 1."""
|
|
|
|
//| ...
|
2021-10-15 14:43:12 -04:00
|
|
|
STATIC mp_obj_t bleio_characteristic_buffer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
2019-01-19 19:45:35 -05:00
|
|
|
enum { ARG_characteristic, ARG_timeout, ARG_buffer_size, };
|
2019-01-07 22:46:20 -05:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_characteristic, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
2019-01-19 19:45:35 -05:00
|
|
|
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(1)} },
|
|
|
|
{ MP_QSTR_buffer_size, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 64} },
|
2019-01-07 22:46:20 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
2021-10-15 14:43:12 -04:00
|
|
|
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2019-01-07 22:46:20 -05:00
|
|
|
|
2021-10-10 02:46:14 -04:00
|
|
|
bleio_characteristic_obj_t *characteristic = mp_arg_validate_type(args[ARG_characteristic].u_obj, &bleio_characteristic_type, MP_QSTR_characteristic);
|
2019-01-07 22:46:20 -05:00
|
|
|
|
2022-05-13 15:33:43 -04:00
|
|
|
mp_float_t timeout = mp_arg_validate_obj_float_non_negative(args[ARG_timeout].u_obj, 1.0f, MP_QSTR_timeout);
|
2019-01-19 19:45:35 -05:00
|
|
|
|
2022-05-13 15:33:43 -04:00
|
|
|
const mp_int_t buffer_size = mp_arg_validate_int_min(args[ARG_buffer_size].u_int, 1, MP_QSTR_buffer_size);
|
2019-01-07 22:46:20 -05:00
|
|
|
|
2023-08-07 20:45:57 -04:00
|
|
|
bleio_characteristic_buffer_obj_t *self =
|
|
|
|
mp_obj_malloc(bleio_characteristic_buffer_obj_t, &bleio_characteristic_buffer_type);
|
2019-01-07 22:46:20 -05:00
|
|
|
|
2021-10-10 02:46:14 -04:00
|
|
|
common_hal_bleio_characteristic_buffer_construct(self, characteristic, timeout, buffer_size);
|
2019-01-07 22:46:20 -05:00
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
2019-06-12 04:00:59 -04:00
|
|
|
STATIC void check_for_deinit(bleio_characteristic_buffer_obj_t *self) {
|
|
|
|
if (common_hal_bleio_characteristic_buffer_deinited(self)) {
|
|
|
|
raise_deinited_error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 19:45:35 -05:00
|
|
|
// These are standard stream methods. Code is in py/stream.c.
|
|
|
|
//
|
2020-07-25 04:58:37 -04:00
|
|
|
//| def read(self, nbytes: Optional[int] = None) -> Optional[bytes]:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Read characters. If ``nbytes`` is specified then read at most that many
|
|
|
|
//| bytes. Otherwise, read everything that arrives until the connection
|
|
|
|
//| times out. Providing the number of bytes expected is highly recommended
|
|
|
|
//| because it will be faster.
|
2019-01-19 19:45:35 -05:00
|
|
|
//|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| :return: Data read
|
|
|
|
//| :rtype: bytes or None"""
|
|
|
|
//| ...
|
2020-07-02 17:47:52 -04:00
|
|
|
//| def readinto(self, buf: WriteableBuffer) -> Optional[int]:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Read bytes into the ``buf``. Read at most ``len(buf)`` bytes.
|
2019-01-19 19:45:35 -05:00
|
|
|
//|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| :return: number of bytes read and stored into ``buf``
|
|
|
|
//| :rtype: int or None (on a non-blocking error)"""
|
|
|
|
//| ...
|
2020-07-02 14:11:25 -04:00
|
|
|
//| def readline(self) -> bytes:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Read a line, ending in a newline character.
|
2019-01-19 19:45:35 -05:00
|
|
|
//|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| :return: the line read
|
|
|
|
//| :rtype: int or None"""
|
|
|
|
//| ...
|
2019-01-19 19:45:35 -05:00
|
|
|
|
|
|
|
// These three methods are used by the shared stream methods.
|
|
|
|
STATIC mp_uint_t bleio_characteristic_buffer_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
|
2019-01-07 22:46:20 -05:00
|
|
|
bleio_characteristic_buffer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2019-01-19 19:45:35 -05:00
|
|
|
raise_error_if_not_connected(self);
|
|
|
|
byte *buf = buf_in;
|
2019-01-07 22:46:20 -05:00
|
|
|
|
2019-01-19 19:45:35 -05:00
|
|
|
// make sure we want at least 1 char
|
|
|
|
if (size == 0) {
|
|
|
|
return 0;
|
2019-01-07 22:46:20 -05:00
|
|
|
}
|
|
|
|
|
2019-01-19 19:45:35 -05:00
|
|
|
return common_hal_bleio_characteristic_buffer_read(self, buf, size, errcode);
|
2019-01-07 22:46:20 -05:00
|
|
|
}
|
2019-01-19 19:45:35 -05:00
|
|
|
|
|
|
|
STATIC mp_uint_t bleio_characteristic_buffer_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) {
|
2023-10-30 04:49:06 -04:00
|
|
|
mp_raise_NotImplementedError(MP_ERROR_TEXT("CharacteristicBuffer writing not provided"));
|
2019-01-19 19:45:35 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_uint_t bleio_characteristic_buffer_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
|
|
|
|
bleio_characteristic_buffer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2019-01-19 19:45:35 -05:00
|
|
|
raise_error_if_not_connected(self);
|
|
|
|
mp_uint_t ret;
|
2022-10-14 13:14:06 -04:00
|
|
|
if (request == MP_STREAM_POLL) {
|
2019-01-19 19:45:35 -05:00
|
|
|
mp_uint_t flags = arg;
|
|
|
|
ret = 0;
|
2022-10-14 13:14:06 -04:00
|
|
|
if ((flags & MP_STREAM_POLL_RD) && common_hal_bleio_characteristic_buffer_rx_characters_available(self) > 0) {
|
|
|
|
ret |= MP_STREAM_POLL_RD;
|
2019-01-19 19:45:35 -05:00
|
|
|
}
|
|
|
|
// No writing provided.
|
2022-10-14 13:14:06 -04:00
|
|
|
// if ((flags & MP_STREAM_POLL_WR) && common_hal_busio_uart_ready_to_tx(self)) {
|
|
|
|
// ret |= MP_STREAM_POLL_WR;
|
2019-01-19 19:45:35 -05:00
|
|
|
// }
|
|
|
|
} else {
|
|
|
|
*errcode = MP_EINVAL;
|
|
|
|
ret = MP_STREAM_ERROR;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| in_waiting: int
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """The number of bytes in the input buffer, available to be read"""
|
2019-01-19 19:45:35 -05:00
|
|
|
STATIC mp_obj_t bleio_characteristic_buffer_obj_get_in_waiting(mp_obj_t self_in) {
|
|
|
|
bleio_characteristic_buffer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2022-07-30 01:09:49 -04:00
|
|
|
uint32_t available = common_hal_bleio_characteristic_buffer_rx_characters_available(self);
|
|
|
|
if (available == 0) {
|
|
|
|
// Only check if connected when none available, otherwise, allow code to continue.
|
|
|
|
raise_error_if_not_connected(self);
|
|
|
|
}
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(available);
|
2019-01-19 19:45:35 -05:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_buffer_get_in_waiting_obj, bleio_characteristic_buffer_obj_get_in_waiting);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETTER(bleio_characteristic_buffer_in_waiting_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&bleio_characteristic_buffer_get_in_waiting_obj);
|
2019-01-19 19:45:35 -05:00
|
|
|
|
2020-07-02 14:11:25 -04:00
|
|
|
//| def reset_input_buffer(self) -> None:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Discard any unread characters in the input buffer."""
|
|
|
|
//| ...
|
2019-01-19 19:45:35 -05:00
|
|
|
STATIC mp_obj_t bleio_characteristic_buffer_obj_reset_input_buffer(mp_obj_t self_in) {
|
|
|
|
bleio_characteristic_buffer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2019-01-19 19:45:35 -05:00
|
|
|
common_hal_bleio_characteristic_buffer_clear_rx_buffer(self);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_buffer_reset_input_buffer_obj, bleio_characteristic_buffer_obj_reset_input_buffer);
|
2019-01-07 22:46:20 -05:00
|
|
|
|
2020-07-02 14:11:25 -04:00
|
|
|
//| def deinit(self) -> None:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Disable permanently."""
|
|
|
|
//| ...
|
2022-09-29 20:22:32 -04:00
|
|
|
//|
|
2019-01-07 22:46:20 -05:00
|
|
|
STATIC mp_obj_t bleio_characteristic_buffer_deinit(mp_obj_t self_in) {
|
|
|
|
bleio_characteristic_buffer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
common_hal_bleio_characteristic_buffer_deinit(self);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_buffer_deinit_obj, bleio_characteristic_buffer_deinit);
|
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t bleio_characteristic_buffer_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&bleio_characteristic_buffer_deinit_obj) },
|
2019-01-19 19:45:35 -05:00
|
|
|
|
|
|
|
// Standard stream methods.
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj)},
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
|
|
|
|
// CharacteristicBuffer is currently read-only.
|
|
|
|
// { MP_OBJ_NEW_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
|
|
|
|
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_reset_input_buffer), MP_ROM_PTR(&bleio_characteristic_buffer_reset_input_buffer_obj) },
|
|
|
|
// Properties
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_in_waiting), MP_ROM_PTR(&bleio_characteristic_buffer_in_waiting_obj) },
|
|
|
|
|
2019-01-07 22:46:20 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(bleio_characteristic_buffer_locals_dict, bleio_characteristic_buffer_locals_dict_table);
|
|
|
|
|
2019-01-19 19:45:35 -05:00
|
|
|
STATIC const mp_stream_p_t characteristic_buffer_stream_p = {
|
protocols: Allow them to be (optionally) type-safe
Protocols are nice, but there is no way for C code to verify whether
a type's "protocol" structure actually implements some particular
protocol. As a result, you can pass an object that implements the
"vfs" protocol to one that expects the "stream" protocol, and the
opposite of awesomeness ensues.
This patch adds an OPTIONAL (but enabled by default) protocol identifier
as the first member of any protocol structure. This identifier is
simply a unique QSTR chosen by the protocol designer and used by each
protocol implementer. When checking for protocol support, instead of
just checking whether the object's type has a non-NULL protocol field,
use `mp_proto_get` which implements the protocol check when possible.
The existing protocols are now named:
protocol_framebuf
protocol_i2c
protocol_pin
protocol_stream
protocol_spi
protocol_vfs
(most of these are unused in CP and are just inherited from MP; vfs and
stream are definitely used though)
I did not find any crashing examples, but here's one to give a flavor of what
is improved, using `micropython_coverage`. Before the change,
the vfs "ioctl" protocol is invoked, and the result is not intelligible
as json (but it could have resulted in a hard fault, potentially):
>>> import uos, ujson
>>> u = uos.VfsPosix('/tmp')
>>> ujson.load(u)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: syntax error in JSON
After the change, the vfs object is correctly detected as not supporting
the stream protocol:
>>> ujson.load(p)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: stream operation not supported
2019-12-03 15:50:37 -05:00
|
|
|
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_stream)
|
2019-01-19 19:45:35 -05:00
|
|
|
.read = bleio_characteristic_buffer_read,
|
|
|
|
.write = bleio_characteristic_buffer_write,
|
|
|
|
.ioctl = bleio_characteristic_buffer_ioctl,
|
|
|
|
.is_text = false,
|
2021-02-17 23:24:11 -05:00
|
|
|
// Disallow readinto() size parameter.
|
|
|
|
.pyserial_readinto_compatibility = true,
|
2019-01-19 19:45:35 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-09-19 21:09:29 -04:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
bleio_characteristic_buffer_type,
|
|
|
|
MP_QSTR_CharacteristicBuffer,
|
2023-10-19 17:27:20 -04:00
|
|
|
MP_TYPE_FLAG_ITER_IS_ITERNEXT | MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS,
|
2023-09-19 21:09:29 -04:00
|
|
|
make_new, bleio_characteristic_buffer_make_new,
|
|
|
|
locals_dict, &bleio_characteristic_buffer_locals_dict,
|
2023-10-03 15:03:59 -04:00
|
|
|
iter, mp_stream_unbuffered_iter,
|
2023-09-19 21:09:29 -04:00
|
|
|
protocol, &characteristic_buffer_stream_p
|
|
|
|
);
|