summaryrefslogtreecommitdiff
path: root/shared-bindings/_bleio
diff options
context:
space:
mode:
authorDaniel Pollard <daniel@learnweaver.com>2020-05-12 14:41:28 +1000
committerDaniel Pollard <daniel@learnweaver.com>2020-05-12 14:41:28 +1000
commitee2cb703c83ff1c27675e98ce28f4cc49a33ebb9 (patch)
tree989b08a0d3016ac63e254ae44fcb7385bf9fc020 /shared-bindings/_bleio
parente947e4eb580e080d37445240ec88610fc1787975 (diff)
parent62b835ad7637c5336f210ab688289dc607bec661 (diff)
merged master
Diffstat (limited to 'shared-bindings/_bleio')
-rw-r--r--shared-bindings/_bleio/CharacteristicBuffer.h2
-rw-r--r--shared-bindings/_bleio/Connection.c28
-rw-r--r--shared-bindings/_bleio/Connection.h11
-rw-r--r--shared-bindings/_bleio/PacketBuffer.c88
-rw-r--r--shared-bindings/_bleio/PacketBuffer.h7
5 files changed, 107 insertions, 29 deletions
diff --git a/shared-bindings/_bleio/CharacteristicBuffer.h b/shared-bindings/_bleio/CharacteristicBuffer.h
index 83e6fef02..e82e96ca9 100644
--- a/shared-bindings/_bleio/CharacteristicBuffer.h
+++ b/shared-bindings/_bleio/CharacteristicBuffer.h
@@ -32,7 +32,7 @@
extern const mp_obj_type_t bleio_characteristic_buffer_type;
extern void common_hal_bleio_characteristic_buffer_construct(bleio_characteristic_buffer_obj_t *self, bleio_characteristic_obj_t *characteristic, mp_float_t timeout, size_t buffer_size);
-int common_hal_bleio_characteristic_buffer_read(bleio_characteristic_buffer_obj_t *self, uint8_t *data, size_t len, int *errcode);
+uint32_t common_hal_bleio_characteristic_buffer_read(bleio_characteristic_buffer_obj_t *self, uint8_t *data, size_t len, int *errcode);
uint32_t common_hal_bleio_characteristic_buffer_rx_characters_available(bleio_characteristic_buffer_obj_t *self);
void common_hal_bleio_characteristic_buffer_clear_rx_buffer(bleio_characteristic_buffer_obj_t *self);
bool common_hal_bleio_characteristic_buffer_deinited(bleio_characteristic_buffer_obj_t *self);
diff --git a/shared-bindings/_bleio/Connection.c b/shared-bindings/_bleio/Connection.c
index c157af365..f612517bb 100644
--- a/shared-bindings/_bleio/Connection.c
+++ b/shared-bindings/_bleio/Connection.c
@@ -214,6 +214,25 @@ STATIC mp_obj_t bleio_connection_get_connection_interval(mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_connection_get_connection_interval_obj, bleio_connection_get_connection_interval);
+//| .. attribute:: max_packet_length
+//|
+//| The maximum number of data bytes that can be sent in a single transmission,
+//| not including overhead bytes.
+//|
+//| This is the maximum number of bytes that can be sent in a notification,
+//| which must be sent in a single packet.
+//| But for a regular characteristic read or write, may be sent in multiple packets,
+//| so this limit does not apply.
+//|
+STATIC mp_obj_t bleio_connection_get_max_packet_length(mp_obj_t self_in) {
+ bleio_connection_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+ bleio_connection_ensure_connected(self);
+ return mp_obj_new_int(common_hal_bleio_connection_get_max_packet_length(self->connection));
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_connection_get_max_packet_length_obj, bleio_connection_get_max_packet_length);
+
+
STATIC mp_obj_t bleio_connection_set_connection_interval(mp_obj_t self_in, mp_obj_t interval_in) {
bleio_connection_obj_t *self = MP_OBJ_TO_PTR(self_in);
@@ -233,6 +252,13 @@ const mp_obj_property_t bleio_connection_connection_interval_obj = {
(mp_obj_t)&mp_const_none_obj },
};
+const mp_obj_property_t bleio_connection_max_packet_length_obj = {
+ .base.type = &mp_type_property,
+ .proxy = { (mp_obj_t)&bleio_connection_get_max_packet_length_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_obj },
+};
+
STATIC const mp_rom_map_elem_t bleio_connection_locals_dict_table[] = {
// Methods
{ MP_ROM_QSTR(MP_QSTR_pair), MP_ROM_PTR(&bleio_connection_pair_obj) },
@@ -243,7 +269,7 @@ STATIC const mp_rom_map_elem_t bleio_connection_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_connected), MP_ROM_PTR(&bleio_connection_connected_obj) },
{ MP_ROM_QSTR(MP_QSTR_paired), MP_ROM_PTR(&bleio_connection_paired_obj) },
{ MP_ROM_QSTR(MP_QSTR_connection_interval), MP_ROM_PTR(&bleio_connection_connection_interval_obj) },
-
+ { MP_ROM_QSTR(MP_QSTR_max_packet_length), MP_ROM_PTR(&bleio_connection_max_packet_length_obj) },
};
STATIC MP_DEFINE_CONST_DICT(bleio_connection_locals_dict, bleio_connection_locals_dict_table);
diff --git a/shared-bindings/_bleio/Connection.h b/shared-bindings/_bleio/Connection.h
index c6f260160..a5313a937 100644
--- a/shared-bindings/_bleio/Connection.h
+++ b/shared-bindings/_bleio/Connection.h
@@ -34,11 +34,12 @@
extern const mp_obj_type_t bleio_connection_type;
-extern void common_hal_bleio_connection_pair(bleio_connection_internal_t *self, bool bond);
-extern void common_hal_bleio_connection_disconnect(bleio_connection_internal_t *self);
-extern bool common_hal_bleio_connection_get_connected(bleio_connection_obj_t *self);
-extern bool common_hal_bleio_connection_get_paired(bleio_connection_obj_t *self);
-extern mp_obj_tuple_t *common_hal_bleio_connection_discover_remote_services(bleio_connection_obj_t *self, mp_obj_t service_uuids_whitelist);
+void common_hal_bleio_connection_pair(bleio_connection_internal_t *self, bool bond);
+void common_hal_bleio_connection_disconnect(bleio_connection_internal_t *self);
+bool common_hal_bleio_connection_get_connected(bleio_connection_obj_t *self);
+mp_int_t common_hal_bleio_connection_get_max_packet_length(bleio_connection_internal_t *self);
+bool common_hal_bleio_connection_get_paired(bleio_connection_obj_t *self);
+mp_obj_tuple_t *common_hal_bleio_connection_discover_remote_services(bleio_connection_obj_t *self, mp_obj_t service_uuids_whitelist);
mp_float_t common_hal_bleio_connection_get_connection_interval(bleio_connection_internal_t *self);
void common_hal_bleio_connection_set_connection_interval(bleio_connection_internal_t *self, mp_float_t new_interval);
diff --git a/shared-bindings/_bleio/PacketBuffer.c b/shared-bindings/_bleio/PacketBuffer.c
index 9e3666044..6dbf29072 100644
--- a/shared-bindings/_bleio/PacketBuffer.c
+++ b/shared-bindings/_bleio/PacketBuffer.c
@@ -42,8 +42,8 @@
//|
//| Accumulates a Characteristic's incoming packets in a FIFO buffer and facilitates packet aware
//| outgoing writes. A packet's size is either the characteristic length or the maximum transmission
-//| unit (MTU), whichever is smaller. The MTU can change so check `packet_size` before creating a
-//| buffer to store data.
+//| 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.
//|
//| When we're the server, we ignore all connections besides the first to subscribe to
//| notifications.
@@ -51,7 +51,7 @@
//| .. class:: PacketBuffer(characteristic, *, buffer_size)
//|
//| Monitor the given Characteristic. Each time a new value is written to the Characteristic
-//| add the newly-written bytes to a FIFO buffer.
+//| add the newly-written packet of bytes to a FIFO buffer.
//|
//| :param Characteristic characteristic: The Characteristic to monitor.
//| It may be a local Characteristic provided by a Peripheral Service, or a remote Characteristic
@@ -71,7 +71,7 @@ STATIC mp_obj_t bleio_packet_buffer_make_new(const mp_obj_type_t *type, size_t n
const mp_obj_t characteristic = args[ARG_characteristic].u_obj;
- const int buffer_size = args[ARG_buffer_size].u_int;
+ const mp_int_t buffer_size = args[ARG_buffer_size].u_int;
if (buffer_size < 1) {
mp_raise_ValueError_varg(translate("%q must be >= 1"), MP_QSTR_buffer_size);
}
@@ -109,7 +109,7 @@ STATIC mp_obj_t bleio_packet_buffer_readinto(mp_obj_t self_in, mp_obj_t buffer_o
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buffer_obj, &bufinfo, MP_BUFFER_WRITE);
- int size = common_hal_bleio_packet_buffer_readinto(self, bufinfo.buf, bufinfo.len);
+ mp_int_t size = common_hal_bleio_packet_buffer_readinto(self, bufinfo.buf, bufinfo.len);
if (size < 0) {
mp_raise_ValueError_varg(translate("Buffer too short by %d bytes"), size * -1);
}
@@ -125,6 +125,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(bleio_packet_buffer_readinto_obj, bleio_packet_
//|
//| This does not block until the data is sent. It only blocks until the data is pending.
//|
+//| :return: number of bytes written. May include header bytes when packet is empty.
+//| :rtype: int
+//|
// 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) {
@@ -149,9 +152,21 @@ STATIC mp_obj_t bleio_packet_buffer_write(mp_uint_t n_args, const mp_obj_t *pos_
mp_get_buffer_raise(args[ARG_header].u_obj, &header_bufinfo, MP_BUFFER_READ);
}
- common_hal_bleio_packet_buffer_write(self, data_bufinfo.buf, data_bufinfo.len,
- header_bufinfo.buf, header_bufinfo.len);
- return mp_const_none;
+ 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);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_packet_buffer_write_obj, 1, bleio_packet_buffer_write);
@@ -168,31 +183,66 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_packet_buffer_deinit_obj, bleio_packet_bu
//| .. attribute:: packet_size
//|
-//| Maximum size of each packet in bytes. This is the minimum of the Characteristic length and
-//| the negotiated Maximum Transfer Unit (MTU).
+//| `packet_size` is the same as `incoming_packet_length`.
+//| The name `packet_size` is deprecated and
+//| will be removed in CircuitPython 6.0.0.
+//|
+//| .. attribute:: incoming_packet_length
+//|
+//| Maximum length in bytes of a packet we are reading.
//|
-STATIC mp_obj_t bleio_packet_buffer_get_packet_size(mp_obj_t self_in) {
+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);
- return MP_OBJ_NEW_SMALL_INT(common_hal_bleio_packet_buffer_get_packet_size(self));
+ mp_int_t size = common_hal_bleio_packet_buffer_get_incoming_packet_length(self);
+ if (size < 0) {
+ mp_raise_ValueError(translate("No connection: length cannot be determined"));
+ }
+ 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 },
+};
+
+//| .. attribute:: outgoing_packet_length
+//|
+//| Maximum length in bytes of a packet we are writing.
+//|
+STATIC mp_obj_t bleio_packet_buffer_get_outgoing_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_outgoing_packet_length(self);
+ if (size < 0) {
+ mp_raise_ValueError(translate("No connection: length cannot be determined"));
+ }
+ return MP_OBJ_NEW_SMALL_INT(size);
}
-STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_packet_buffer_get_packet_size_obj, bleio_packet_buffer_get_packet_size);
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_packet_buffer_get_outgoing_packet_length_obj, bleio_packet_buffer_get_outgoing_packet_length);
-const mp_obj_property_t bleio_packet_buffer_packet_size_obj = {
+const mp_obj_property_t bleio_packet_buffer_outgoing_packet_length_obj = {
.base.type = &mp_type_property,
- .proxy = { (mp_obj_t)&bleio_packet_buffer_get_packet_size_obj,
+ .proxy = { (mp_obj_t)&bleio_packet_buffer_get_outgoing_packet_length_obj,
(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[] = {
- { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&bleio_packet_buffer_deinit_obj) },
+ { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&bleio_packet_buffer_deinit_obj) },
// Standard stream methods.
- { 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) },
+ { 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) },
- { MP_OBJ_NEW_QSTR(MP_QSTR_packet_size), MP_ROM_PTR(&bleio_packet_buffer_packet_size_obj) },
+ // .packet_size is now an alias for .incoming_packet_length
+ // TODO: Remove in 6.0.0.
+ { 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) },
};
STATIC MP_DEFINE_CONST_DICT(bleio_packet_buffer_locals_dict, bleio_packet_buffer_locals_dict_table);
diff --git a/shared-bindings/_bleio/PacketBuffer.h b/shared-bindings/_bleio/PacketBuffer.h
index 990a2f8bb..769e0a0c7 100644
--- a/shared-bindings/_bleio/PacketBuffer.h
+++ b/shared-bindings/_bleio/PacketBuffer.h
@@ -34,9 +34,10 @@ extern const mp_obj_type_t bleio_packet_buffer_type;
extern void common_hal_bleio_packet_buffer_construct(
bleio_packet_buffer_obj_t *self, bleio_characteristic_obj_t *characteristic,
size_t buffer_size);
-void common_hal_bleio_packet_buffer_write(bleio_packet_buffer_obj_t *self, uint8_t *data, size_t len, uint8_t* header, size_t header_len);
-int common_hal_bleio_packet_buffer_readinto(bleio_packet_buffer_obj_t *self, uint8_t *data, size_t len);
-uint16_t common_hal_bleio_packet_buffer_get_packet_size(bleio_packet_buffer_obj_t *self);
+mp_int_t common_hal_bleio_packet_buffer_write(bleio_packet_buffer_obj_t *self, uint8_t *data, size_t len, uint8_t* header, size_t header_len);
+mp_int_t common_hal_bleio_packet_buffer_readinto(bleio_packet_buffer_obj_t *self, uint8_t *data, size_t len);
+mp_int_t common_hal_bleio_packet_buffer_get_incoming_packet_length(bleio_packet_buffer_obj_t *self);
+mp_int_t common_hal_bleio_packet_buffer_get_outgoing_packet_length(bleio_packet_buffer_obj_t *self);
bool common_hal_bleio_packet_buffer_deinited(bleio_packet_buffer_obj_t *self);
void common_hal_bleio_packet_buffer_deinit(bleio_packet_buffer_obj_t *self);