summaryrefslogtreecommitdiff
path: root/shared-bindings
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
parente947e4eb580e080d37445240ec88610fc1787975 (diff)
parent62b835ad7637c5336f210ab688289dc607bec661 (diff)
merged master
Diffstat (limited to 'shared-bindings')
-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
-rw-r--r--shared-bindings/_pixelbuf/PixelBuf.c10
-rw-r--r--shared-bindings/_pixelbuf/__init__.c17
-rw-r--r--shared-bindings/aesio/__init__.c79
-rw-r--r--shared-bindings/aesio/__init__.h53
-rw-r--r--shared-bindings/aesio/aes.c271
-rw-r--r--shared-bindings/busio/UART.c4
-rw-r--r--shared-bindings/rtc/RTC.c2
12 files changed, 530 insertions, 42 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);
diff --git a/shared-bindings/_pixelbuf/PixelBuf.c b/shared-bindings/_pixelbuf/PixelBuf.c
index 61b4c9ae0..626dde680 100644
--- a/shared-bindings/_pixelbuf/PixelBuf.c
+++ b/shared-bindings/_pixelbuf/PixelBuf.c
@@ -44,7 +44,7 @@ extern const int32_t colorwheel(float pos);
static void parse_byteorder(mp_obj_t byteorder_obj, pixelbuf_byteorder_details_t* parsed);
-//| .. currentmodule:: pixelbuf
+//| .. currentmodule:: _pixelbuf
//|
//| :class:`PixelBuf` -- A fast RGB[W] pixel buffer for LED and similar devices
//| ===========================================================================
@@ -58,13 +58,13 @@ static void parse_byteorder(mp_obj_t byteorder_obj, pixelbuf_byteorder_details_t
//| When brightness is less than 1.0, a second buffer will be used to store the color values
//| before they are adjusted for brightness.
//|
-//| When ``P`` (pwm duration) is present as the 4th character of the byteorder
+//| When ``P`` (pwm duration) is present as the first character of the byteorder
//| string, the 4th value in the tuple/list for a pixel is the individual pixel
//| brightness (0.0-1.0) and will enable a Dotstar compatible 1st byte in the
//| output buffer (``buf``).
//|
-//| :param ~int size: Number of pixelsx
-//| :param ~str byteorder: Byte order string (such as "BGR" or "PBGR")
+//| :param ~int size: Number of pixels
+//| :param ~str byteorder: Byte order string (such as "BGR" or "BGRP")
//| :param ~float brightness: Brightness (0 to 1.0, default 1.0)
//| :param ~bool auto_write: Whether to automatically write pixels (Default False)
//| :param bytes header: Sequence of bytes to always send before pixel values.
@@ -265,7 +265,7 @@ STATIC mp_obj_t pixelbuf_pixelbuf_show(mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pixelbuf_pixelbuf_show_obj, pixelbuf_pixelbuf_show);
-//| .. function:: fill(color)
+//| .. method:: fill(color)
//|
//| Fills the given pixelbuf with the given color.
//|
diff --git a/shared-bindings/_pixelbuf/__init__.c b/shared-bindings/_pixelbuf/__init__.c
index 424ed23e4..744a8a2b8 100644
--- a/shared-bindings/_pixelbuf/__init__.c
+++ b/shared-bindings/_pixelbuf/__init__.c
@@ -53,36 +53,39 @@
//| PixelBuf
-//| .. function:: wheel(n)
+//| .. function:: colorwheel(n)
//|
//| C implementation of the common wheel() function found in many examples.
//| Returns the colorwheel RGB value as an integer value for n (usable in :py:class:`PixelBuf`, neopixel, and dotstar).
//|
+//| .. function:: wheel(n)
+//| Use of wheel() is deprecated. Please use colorwheel().
-STATIC mp_obj_t pixelbuf_wheel(mp_obj_t n) {
+STATIC mp_obj_t pixelbuf_colorwheel(mp_obj_t n) {
return MP_OBJ_NEW_SMALL_INT(colorwheel(MP_OBJ_IS_SMALL_INT(n) ? MP_OBJ_SMALL_INT_VALUE(n) : mp_obj_float_get(n)));
}
-STATIC MP_DEFINE_CONST_FUN_OBJ_1(pixelbuf_wheel_obj, pixelbuf_wheel);
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(pixelbuf_colorwheel_obj, pixelbuf_colorwheel);
const int32_t colorwheel(float pos) {
if (pos > 255) {
pos = pos - ((uint32_t)(pos / 256) * 256);
}
if (pos < 85)
- return (uint8_t)(pos * 3) << 16 | (uint8_t)(255 - (pos * 3)) << 8;
+ return (uint8_t)(255 - (pos * 3)) << 16 | (uint8_t)(pos * 3) << 8;
else if (pos < 170) {
pos -= 85;
- return (uint8_t)(255 - (pos * 3)) << 16 | (uint8_t)(pos * 3);
+ return (uint8_t)(255 - (pos * 3)) << 8 | (uint8_t)(pos * 3);
} else {
pos -= 170;
- return (uint8_t)(pos * 3) << 8 | (uint8_t)(255 - pos * 3);
+ return (uint8_t)(pos * 3) << 16 | (uint8_t)(255 - (pos * 3));
}
}
STATIC const mp_rom_map_elem_t pixelbuf_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__pixelbuf) },
{ MP_ROM_QSTR(MP_QSTR_PixelBuf), MP_ROM_PTR(&pixelbuf_pixelbuf_type) },
- { MP_ROM_QSTR(MP_QSTR_wheel), MP_ROM_PTR(&pixelbuf_wheel_obj) },
+ { MP_ROM_QSTR(MP_QSTR_wheel), MP_ROM_PTR(&pixelbuf_colorwheel_obj) },
+ { MP_ROM_QSTR(MP_QSTR_colorwheel), MP_ROM_PTR(&pixelbuf_colorwheel_obj) },
};
STATIC MP_DEFINE_CONST_DICT(pixelbuf_module_globals, pixelbuf_module_globals_table);
diff --git a/shared-bindings/aesio/__init__.c b/shared-bindings/aesio/__init__.c
new file mode 100644
index 000000000..43dc73e8b
--- /dev/null
+++ b/shared-bindings/aesio/__init__.c
@@ -0,0 +1,79 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 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 <stdint.h>
+
+#include "py/obj.h"
+#include "py/runtime.h"
+
+#include "__init__.h"
+
+//| :mod:`aesio` --- AES encryption routines
+//| ========================================
+//|
+//| .. module:: aesio
+//| :synopsis: Embedded implementation of AES
+//|
+//| The `AES` module contains classes used to implement encryption
+//| and decryption. It aims to be low overhead in terms of memory.
+//|
+//|
+
+//| Libraries
+//|
+//| .. toctree::
+//| :maxdepth: 3
+//|
+//| aes
+
+
+STATIC const mp_obj_tuple_t mp_aes_key_size_obj = {
+ {&mp_type_tuple},
+ 3,
+ {
+ MP_OBJ_NEW_SMALL_INT(16),
+ MP_OBJ_NEW_SMALL_INT(24),
+ MP_OBJ_NEW_SMALL_INT(32),
+ }
+};
+
+STATIC const mp_rom_map_elem_t aesio_module_globals_table[] = {
+ {MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_aesio)},
+ {MP_ROM_QSTR(MP_QSTR_AES), MP_ROM_PTR(&aesio_aes_type) },
+ {MP_ROM_QSTR(MP_QSTR_MODE_ECB), MP_ROM_INT(AES_MODE_ECB)},
+ {MP_ROM_QSTR(MP_QSTR_MODE_CBC), MP_ROM_INT(AES_MODE_CBC)},
+ {MP_ROM_QSTR(MP_QSTR_MODE_CTR), MP_ROM_INT(AES_MODE_CTR)},
+ {MP_ROM_QSTR(MP_QSTR_block_size), MP_ROM_INT(AES_BLOCKLEN)},
+ {MP_ROM_QSTR(MP_QSTR_key_size), (mp_obj_t)&mp_aes_key_size_obj},
+};
+
+STATIC MP_DEFINE_CONST_DICT(aesio_module_globals, aesio_module_globals_table);
+
+const mp_obj_module_t aesio_module = {
+ .base = {&mp_type_module},
+ .globals = (mp_obj_dict_t *)&aesio_module_globals,
+};
+
diff --git a/shared-bindings/aesio/__init__.h b/shared-bindings/aesio/__init__.h
new file mode 100644
index 000000000..634fed2e5
--- /dev/null
+++ b/shared-bindings/aesio/__init__.h
@@ -0,0 +1,53 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 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.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_AESIO_H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_AESIO_H
+
+#include "shared-module/aesio/__init__.h"
+
+extern const mp_obj_type_t aesio_aes_type;
+
+void common_hal_aesio_aes_construct(aesio_aes_obj_t* self,
+ const uint8_t* key,
+ uint32_t key_length,
+ const uint8_t* iv,
+ int mode,
+ int counter);
+void common_hal_aesio_aes_rekey(aesio_aes_obj_t* self,
+ const uint8_t* key,
+ uint32_t key_length,
+ const uint8_t* iv);
+void common_hal_aesio_aes_set_mode(aesio_aes_obj_t* self,
+ int mode);
+void common_hal_aesio_aes_encrypt(aesio_aes_obj_t* self,
+ uint8_t* buffer,
+ size_t len);
+void common_hal_aesio_aes_decrypt(aesio_aes_obj_t* self,
+ uint8_t* buffer,
+ size_t len);
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_AESIO_H
diff --git a/shared-bindings/aesio/aes.c b/shared-bindings/aesio/aes.c
new file mode 100644
index 000000000..4ddcfa898
--- /dev/null
+++ b/shared-bindings/aesio/aes.c
@@ -0,0 +1,271 @@
+#include <stdint.h>
+#include <string.h>
+
+#include "py/obj.h"
+#include "py/objproperty.h"
+#include "py/runtime.h"
+
+#include "shared-bindings/aesio/__init__.h"
+
+// Defined at the end of this file
+
+//| .. currentmodule:: aesio
+//|
+//| :class:`aesio` -- Encrypt and decrypt AES streams
+//| =====================================================
+//|
+//| An object that represents an AES stream, including the current state.
+//|
+//| .. class:: AES(key, mode=0, iv=None, segment_size=8)
+//|
+//| Create a new AES state with the given key.
+//|
+//| :param bytearray key: A 16-, 24-, or 32-byte key
+//| :param int mode: AES mode to use. One of: AES.MODE_ECB, AES.MODE_CBC, or
+//| AES.MODE_CTR
+//| :param bytearray iv: Initialization vector to use for CBC or CTR mode
+//|
+//| Additional arguments are supported for legacy reasons.
+//|
+//| Encrypting a string::
+//|
+//| import aesio
+//| from binascii import hexlify
+//|
+//| key = b'Sixteen byte key'
+//| inp = b'Circuit Python!!' # Note: 16-bytes long
+//| outp = bytearray(len(inp))
+//| cipher = aesio.AES(key, aesio.mode.MODE_ECB)
+//| cipher.encrypt_into(inp, outp)
+//| hexlify(outp)
+//|
+
+STATIC mp_obj_t aesio_aes_make_new(const mp_obj_type_t *type, size_t n_args,
+ const mp_obj_t *pos_args,
+ mp_map_t *kw_args) {
+ (void)type;
+ enum { ARG_key, ARG_mode, ARG_IV, ARG_counter, ARG_segment_size };
+ static const mp_arg_t allowed_args[] = {
+ {MP_QSTR_key, MP_ARG_OBJ | MP_ARG_REQUIRED},
+ {MP_QSTR_mode, MP_ARG_INT, {.u_int = AES_MODE_ECB}},
+ {MP_QSTR_IV, MP_ARG_OBJ},
+ {MP_QSTR_counter, MP_ARG_OBJ},
+ {MP_QSTR_segment_size, MP_ARG_INT, {.u_int = 8}},
+ };
+ 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);
+
+ aesio_aes_obj_t *self = m_new_obj(aesio_aes_obj_t);
+ self->base.type = &aesio_aes_type;
+
+ mp_buffer_info_t bufinfo;
+
+ const uint8_t *key = NULL;
+ uint32_t key_length = 0;
+ if (mp_get_buffer(args[ARG_key].u_obj, &bufinfo, MP_BUFFER_READ)) {
+ if ((bufinfo.len != 16) && (bufinfo.len != 24) && (bufinfo.len != 32)) {
+ mp_raise_TypeError(translate("Key must be 16, 24, or 32 bytes long"));
+ }
+ key = bufinfo.buf;
+ key_length = bufinfo.len;
+ } else {
+ mp_raise_TypeError(translate("No key was specified"));
+ }
+
+ int mode = args[ARG_mode].u_int;
+ switch (args[ARG_mode].u_int) {
+ case AES_MODE_CBC:
+ case AES_MODE_ECB:
+ case AES_MODE_CTR:
+ break;
+ default:
+ mp_raise_TypeError(translate("Requested AES mode is unsupported"));
+ }
+
+ // IV is required for CBC mode and is ignored for other modes.
+ const uint8_t *iv = NULL;
+ if (args[ARG_IV].u_obj != NULL &&
+ mp_get_buffer(args[ARG_IV].u_obj, &bufinfo, MP_BUFFER_READ)) {
+ if (bufinfo.len != AES_BLOCKLEN) {
+ mp_raise_TypeError_varg(translate("IV must be %d bytes long"),
+ AES_BLOCKLEN);
+ }
+ iv = bufinfo.buf;
+ }
+
+ common_hal_aesio_aes_construct(self, key, key_length, iv, mode,
+ args[ARG_counter].u_int);
+ return MP_OBJ_FROM_PTR(self);
+}
+
+STATIC mp_obj_t aesio_aes_rekey(size_t n_args, const mp_obj_t *pos_args) {
+ aesio_aes_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
+
+ size_t key_length = 0;
+ const uint8_t *key =
+ (const uint8_t *)mp_obj_str_get_data(pos_args[1], &key_length);
+ if (key == NULL) {
+ mp_raise_ValueError(translate("No key was specified"));
+ }
+ if ((key_length != 16) && (key_length != 24) && (key_length != 32)) {
+ mp_raise_TypeError(translate("Key must be 16, 24, or 32 bytes long"));
+ }
+
+ const uint8_t *iv = NULL;
+ if (n_args > 2) {
+ size_t iv_length = 0;
+ iv = (const uint8_t *)mp_obj_str_get_data(pos_args[2], &iv_length);
+ if (iv_length != AES_BLOCKLEN) {
+ mp_raise_TypeError_varg(translate("IV must be %d bytes long"),
+ AES_BLOCKLEN);
+ }
+ }
+
+ common_hal_aesio_aes_rekey(self, key, key_length, iv);
+ return mp_const_none;
+}
+
+MP_DEFINE_CONST_FUN_OBJ_VAR(aesio_aes_rekey_obj, 2, aesio_aes_rekey);
+
+STATIC void validate_length(aesio_aes_obj_t *self, size_t src_length,
+ size_t dest_length) {
+ if (src_length != dest_length) {
+ mp_raise_ValueError(
+ translate("Source and destination buffers must be the same length"));
+ }
+
+ switch (self->mode) {
+ case AES_MODE_ECB:
+ if (src_length != 16) {
+ mp_raise_msg(&mp_type_ValueError,
+ translate("ECB only operates on 16 bytes at a time"));
+ }
+ break;
+ case AES_MODE_CBC:
+ if ((src_length & 15) != 0) {
+ mp_raise_msg(&mp_type_ValueError,
+ translate("CBC blocks must be multiples of 16 bytes"));
+ }
+ break;
+ case AES_MODE_CTR:
+ break;
+ }
+}
+
+//| .. method:: encrypt_into(src, dest)
+//|
+//| Encrypt the buffer from ``src`` into ``dest``.
+//| For ECB mode, the buffers must be 16 bytes long. For CBC mode, the
+//| buffers must be a multiple of 16 bytes, and must be equal length. For
+//| CTX mode, there are no restrictions.
+//|
+STATIC mp_obj_t aesio_aes_encrypt_into(mp_obj_t aesio_obj, mp_obj_t src,
+ mp_obj_t dest) {
+ if (!MP_OBJ_IS_TYPE(aesio_obj, &aesio_aes_type)) {
+ mp_raise_TypeError_varg(translate("Expected a %q"), aesio_aes_type.name);
+ }
+ // Convert parameters into expected types.
+ aesio_aes_obj_t *aes = MP_OBJ_TO_PTR(aesio_obj);
+
+ mp_buffer_info_t srcbufinfo, destbufinfo;
+ mp_get_buffer_raise(src, &srcbufinfo, MP_BUFFER_READ);
+ mp_get_buffer_raise(dest, &destbufinfo, MP_BUFFER_READ);
+ validate_length(aes, srcbufinfo.len, destbufinfo.len);
+
+ memcpy(destbufinfo.buf, srcbufinfo.buf, srcbufinfo.len);
+
+ common_hal_aesio_aes_encrypt(aes, (uint8_t *)destbufinfo.buf,
+ destbufinfo.len);
+ return mp_const_none;
+}
+
+STATIC MP_DEFINE_CONST_FUN_OBJ_3(aesio_aes_encrypt_into_obj,
+ aesio_aes_encrypt_into);
+
+//| .. method:: decrypt_into(src, dest)
+//|
+//| Decrypt the buffer from ``src`` into ``dest``.
+//| For ECB mode, the buffers must be 16 bytes long. For CBC mode, the
+//| buffers must be a multiple of 16 bytes, and must be equal length. For
+//| CTX mode, there are no restrictions.
+//|
+STATIC mp_obj_t aesio_aes_decrypt_into(mp_obj_t aesio_obj, mp_obj_t src,
+ mp_obj_t dest) {
+ if (!MP_OBJ_IS_TYPE(aesio_obj, &aesio_aes_type)) {
+ mp_raise_TypeError_varg(translate("Expected a %q"), aesio_aes_type.name);
+ }
+ // Convert parameters into expected types.
+ aesio_aes_obj_t *aes = MP_OBJ_TO_PTR(aesio_obj);
+
+ mp_buffer_info_t srcbufinfo, destbufinfo;
+ mp_get_buffer_raise(src, &srcbufinfo, MP_BUFFER_READ);
+ mp_get_buffer_raise(dest, &destbufinfo, MP_BUFFER_READ);
+ validate_length(aes, srcbufinfo.len, destbufinfo.len);
+
+ memcpy(destbufinfo.buf, srcbufinfo.buf, srcbufinfo.len);
+
+ common_hal_aesio_aes_decrypt(aes, (uint8_t *)destbufinfo.buf,
+ destbufinfo.len);
+ return mp_const_none;
+}
+
+STATIC MP_DEFINE_CONST_FUN_OBJ_3(aesio_aes_decrypt_into_obj,
+ aesio_aes_decrypt_into);
+
+STATIC mp_obj_t aesio_aes_get_mode(mp_obj_t aesio_obj) {
+ if (!MP_OBJ_IS_TYPE(aesio_obj, &aesio_aes_type)) {
+ mp_raise_TypeError_varg(translate("Expected a %q"), aesio_aes_type.name);
+ }
+ aesio_aes_obj_t *self = MP_OBJ_TO_PTR(aesio_obj);
+ return MP_OBJ_NEW_SMALL_INT(self->mode);
+}
+MP_DEFINE_CONST_FUN_OBJ_1(aesio_aes_get_mode_obj, aesio_aes_get_mode);
+
+STATIC mp_obj_t aesio_aes_set_mode(mp_obj_t aesio_obj, mp_obj_t mode_obj) {
+ if (!MP_OBJ_IS_TYPE(aesio_obj, &aesio_aes_type)) {
+ mp_raise_TypeError_varg(translate("Expected a %q"), aesio_aes_type.name);
+ }
+ aesio_aes_obj_t *self = MP_OBJ_TO_PTR(aesio_obj);
+
+ int mode = mp_obj_get_int(mode_obj);
+ switch (mode) {
+ case AES_MODE_CBC:
+ case AES_MODE_ECB:
+ case AES_MODE_CTR:
+ break;
+ default:
+ mp_raise_TypeError(translate("Requested AES mode is unsupported"));
+ }
+
+ common_hal_aesio_aes_set_mode(self, mode);
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_2(aesio_aes_set_mode_obj, aesio_aes_set_mode);
+
+const mp_obj_property_t aesio_aes_mode_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {
+ (mp_obj_t)&aesio_aes_get_mode_obj,
+ (mp_obj_t)&aesio_aes_set_mode_obj,
+ (mp_obj_t)&mp_const_none_obj
+ },
+};
+
+STATIC const mp_rom_map_elem_t aesio_locals_dict_table[] = {
+ // Methods
+ {MP_ROM_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_AES)},
+ {MP_ROM_QSTR(MP_QSTR_encrypt_into), (mp_obj_t)&aesio_aes_encrypt_into_obj},
+ {MP_ROM_QSTR(MP_QSTR_decrypt_into), (mp_obj_t)&aesio_aes_decrypt_into_obj},
+ {MP_ROM_QSTR(MP_QSTR_rekey), (mp_obj_t)&aesio_aes_rekey_obj},
+ {MP_ROM_QSTR(MP_QSTR_mode), (mp_obj_t)&aesio_aes_mode_obj},
+};
+STATIC MP_DEFINE_CONST_DICT(aesio_locals_dict, aesio_locals_dict_table);
+
+const mp_obj_type_t aesio_aes_type = {
+ {&mp_type_type},
+ .name = MP_QSTR_AES,
+ .make_new = aesio_aes_make_new,
+ .locals_dict = (mp_obj_dict_t *)&aesio_locals_dict,
+};
diff --git a/shared-bindings/busio/UART.c b/shared-bindings/busio/UART.c
index f231924d5..883b4630f 100644
--- a/shared-bindings/busio/UART.c
+++ b/shared-bindings/busio/UART.c
@@ -39,6 +39,9 @@
#include "py/stream.h"
#include "supervisor/shared/translate.h"
+#define STREAM_DEBUG(...) (void)0
+// #define STREAM_DEBUG(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__)
+
//| .. currentmodule:: busio
//|
@@ -219,6 +222,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_uart___exit___obj, 4, 4, busio_
// These three methods are used by the shared stream methods.
STATIC mp_uint_t busio_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
+ STREAM_DEBUG("busio_uart_read stream %d\n", size);
busio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
byte *buf = buf_in;
diff --git a/shared-bindings/rtc/RTC.c b/shared-bindings/rtc/RTC.c
index 3ff09a4ec..07d170ee6 100644
--- a/shared-bindings/rtc/RTC.c
+++ b/shared-bindings/rtc/RTC.c
@@ -65,7 +65,7 @@ STATIC mp_obj_t rtc_rtc_make_new(const mp_obj_type_t *type, size_t n_args, const
//| import time
//|
//| r = rtc.RTC()
-//| r.datetime = rtctime.struct_time((2019, 5, 29, 15, 14, 15, 0, -1, -1))
+//| r.datetime = time.struct_time((2019, 5, 29, 15, 14, 15, 0, -1, -1))
//|
//|
//| Once set, the RTC will automatically update this value as time passes. You can read this