summaryrefslogtreecommitdiff
path: root/shared-bindings
diff options
context:
space:
mode:
authorBernhard Boser <boser@berkeley.edu>2021-01-18 11:26:39 -0800
committerBernhard Boser <boser@berkeley.edu>2021-01-18 11:26:39 -0800
commite27dd26dc72b3603c0f50796d7747fb4858af113 (patch)
tree4d139685ae771e0854e2d9187d496253106a4572 /shared-bindings
parent1e88b9411d54e5c15a27d4a9c2748c264cb9522f (diff)
parent6a76b60027aa163756aace389a5c9203594759ce (diff)
make translate, BEWARE adds the 'world'
Diffstat (limited to 'shared-bindings')
-rw-r--r--shared-bindings/_bleio/Adapter.c14
-rw-r--r--shared-bindings/_pixelbuf/__init__.c2
-rw-r--r--shared-bindings/alarm/SleepMemory.c20
-rw-r--r--shared-bindings/audiobusio/PDMIn.c8
-rw-r--r--shared-bindings/audiobusio/PDMIn.h2
-rw-r--r--shared-bindings/msgpack/ExtType.c126
-rw-r--r--shared-bindings/msgpack/ExtType.h40
-rw-r--r--shared-bindings/msgpack/__init__.c162
-rw-r--r--shared-bindings/msgpack/__init__.h34
-rw-r--r--shared-bindings/wifi/Network.c16
-rw-r--r--shared-bindings/wifi/Network.h1
-rw-r--r--shared-bindings/wifi/Radio.c6
-rw-r--r--shared-bindings/wifi/Radio.h37
13 files changed, 453 insertions, 15 deletions
diff --git a/shared-bindings/_bleio/Adapter.c b/shared-bindings/_bleio/Adapter.c
index 7d7076aab..81277fd70 100644
--- a/shared-bindings/_bleio/Adapter.c
+++ b/shared-bindings/_bleio/Adapter.c
@@ -33,8 +33,8 @@
#include "shared-bindings/_bleio/Address.h"
#include "shared-bindings/_bleio/Adapter.h"
-#define ADV_INTERVAL_MIN (0.02001f)
-#define ADV_INTERVAL_MIN_STRING "0.02001"
+#define ADV_INTERVAL_MIN (0.02f)
+#define ADV_INTERVAL_MIN_STRING "0.02"
#define ADV_INTERVAL_MAX (10.24f)
#define ADV_INTERVAL_MAX_STRING "10.24"
// 20ms is recommended by Apple
@@ -204,7 +204,7 @@ const mp_obj_property_t bleio_adapter_name_obj = {
//| :param ~_typing.ReadableBuffer scan_response: scan response data packet bytes. ``None`` if no scan response is needed.
//| :param bool connectable: If `True` then other devices are allowed to connect to this peripheral.
//| :param bool anonymous: If `True` then this device's MAC address is randomized before advertising.
-//| :param int timeout: If set, we will only advertise for this many seconds.
+//| :param int timeout: If set, we will only advertise for this many seconds. Zero means no timeout.
//| :param float interval: advertising interval, in seconds"""
//| ...
//|
@@ -237,7 +237,7 @@ STATIC mp_obj_t bleio_adapter_start_advertising(mp_uint_t n_args, const mp_obj_t
args[ARG_interval].u_obj = mp_obj_new_float(ADV_INTERVAL_DEFAULT);
}
- const mp_float_t interval = mp_obj_float_get(args[ARG_interval].u_obj);
+ const mp_float_t interval = mp_obj_get_float(args[ARG_interval].u_obj);
if (interval < ADV_INTERVAL_MIN || interval > ADV_INTERVAL_MAX) {
mp_raise_ValueError_varg(translate("interval must be in range %s-%s"),
ADV_INTERVAL_MIN_STRING, ADV_INTERVAL_MAX_STRING);
@@ -279,7 +279,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_stop_advertising_obj, bleio_adapt
//| ignored. Format is one byte for length (n) and n bytes of prefix and can be repeated.
//| :param int buffer_size: the maximum number of advertising bytes to buffer.
//| :param bool extended: When True, support extended advertising packets. Increasing buffer_size is recommended when this is set.
-//| :param float timeout: the scan timeout in seconds. If None, will scan until `stop_scan` is called.
+//| :param float timeout: the scan timeout in seconds. If None or zero, will scan until `stop_scan` is called.
//| :param float interval: the interval (in seconds) between the start of two consecutive scan windows
//| Must be in the range 0.0025 - 40.959375 seconds.
//| :param float window: the duration (in seconds) to scan a single BLE channel.
@@ -320,7 +320,7 @@ STATIC mp_obj_t bleio_adapter_start_scan(size_t n_args, const mp_obj_t *pos_args
args[ARG_window].u_obj = mp_obj_new_float(WINDOW_DEFAULT);
}
- const mp_float_t interval = mp_obj_float_get(args[ARG_interval].u_obj);
+ const mp_float_t interval = mp_obj_get_float(args[ARG_interval].u_obj);
if (interval < INTERVAL_MIN || interval > INTERVAL_MAX) {
mp_raise_ValueError_varg(translate("interval must be in range %s-%s"), INTERVAL_MIN_STRING, INTERVAL_MAX_STRING);
}
@@ -332,7 +332,7 @@ STATIC mp_obj_t bleio_adapter_start_scan(size_t n_args, const mp_obj_t *pos_args
}
#pragma GCC diagnostic pop
- const mp_float_t window = mp_obj_float_get(args[ARG_window].u_obj);
+ const mp_float_t window = mp_obj_get_float(args[ARG_window].u_obj);
if (window > interval) {
mp_raise_ValueError(translate("window must be <= interval"));
}
diff --git a/shared-bindings/_pixelbuf/__init__.c b/shared-bindings/_pixelbuf/__init__.c
index c61acc939..fdd02509c 100644
--- a/shared-bindings/_pixelbuf/__init__.c
+++ b/shared-bindings/_pixelbuf/__init__.c
@@ -51,7 +51,7 @@
//|
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)));
+ return MP_OBJ_NEW_SMALL_INT(colorwheel(MP_OBJ_IS_SMALL_INT(n) ? MP_OBJ_SMALL_INT_VALUE(n) : mp_obj_get_float(n)));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pixelbuf_colorwheel_obj, pixelbuf_colorwheel);
diff --git a/shared-bindings/alarm/SleepMemory.c b/shared-bindings/alarm/SleepMemory.c
index bec0b7665..aed24827a 100644
--- a/shared-bindings/alarm/SleepMemory.c
+++ b/shared-bindings/alarm/SleepMemory.c
@@ -53,6 +53,25 @@
//| """Not used. Access the sole instance through `alarm.sleep_memory`."""
//| ...
//|
+//| def __bool__(self) -> bool:
+//| """``sleep_memory`` is ``True`` if its length is greater than zero.
+//| This is an easy way to check for its existence.
+//| """
+//| ...
+//|
+//| def __len__(self) -> int:
+//| """Return the length. This is used by (`len`)"""
+//| ...
+//|
+STATIC mp_obj_t alarm_sleep_memory_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
+ alarm_sleep_memory_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ uint16_t len = common_hal_alarm_sleep_memory_get_length(self);
+ switch (op) {
+ case MP_UNARY_OP_BOOL: return mp_obj_new_bool(len != 0);
+ case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(len);
+ default: return MP_OBJ_NULL; // op not supported
+ }
+}
STATIC const mp_rom_map_elem_t alarm_sleep_memory_locals_dict_table[] = {
};
@@ -154,6 +173,7 @@ const mp_obj_type_t alarm_sleep_memory_type = {
{ &mp_type_type },
.name = MP_QSTR_SleepMemory,
.subscr = alarm_sleep_memory_subscr,
+ .unary_op = alarm_sleep_memory_unary_op,
.print = NULL,
.locals_dict = (mp_obj_t)&alarm_sleep_memory_locals_dict,
};
diff --git a/shared-bindings/audiobusio/PDMIn.c b/shared-bindings/audiobusio/PDMIn.c
index 6c5fa7939..a3cc8b07e 100644
--- a/shared-bindings/audiobusio/PDMIn.c
+++ b/shared-bindings/audiobusio/PDMIn.c
@@ -84,6 +84,9 @@
//| ...
//|
STATIC mp_obj_t audiobusio_pdmin_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+#if !CIRCUITPY_AUDIOBUSIO_PDMIN
+ mp_raise_NotImplementedError(translate("PDMIn not available"));
+#else
enum { ARG_clock_pin, ARG_data_pin, ARG_sample_rate, ARG_bit_depth, ARG_mono, ARG_oversample, ARG_startup_delay };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_clock_pin, MP_ARG_REQUIRED | MP_ARG_OBJ },
@@ -132,8 +135,10 @@ STATIC mp_obj_t audiobusio_pdmin_make_new(const mp_obj_type_t *type, size_t n_ar
mp_hal_delay_ms(startup_delay * 1000);
return MP_OBJ_FROM_PTR(self);
+#endif
}
+#if CIRCUITPY_AUDIOBUSIO_PDMIN
//| def deinit(self) -> None:
//| """Deinitialises the PDMIn and releases any hardware resources for reuse."""
//| ...
@@ -237,10 +242,13 @@ STATIC const mp_rom_map_elem_t audiobusio_pdmin_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_sample_rate), MP_ROM_PTR(&audiobusio_pdmin_sample_rate_obj) }
};
STATIC MP_DEFINE_CONST_DICT(audiobusio_pdmin_locals_dict, audiobusio_pdmin_locals_dict_table);
+#endif
const mp_obj_type_t audiobusio_pdmin_type = {
{ &mp_type_type },
.name = MP_QSTR_PDMIn,
.make_new = audiobusio_pdmin_make_new,
+#if CIRCUITPY_AUDIOBUSIO_PDMIN
.locals_dict = (mp_obj_dict_t*)&audiobusio_pdmin_locals_dict,
+#endif
};
diff --git a/shared-bindings/audiobusio/PDMIn.h b/shared-bindings/audiobusio/PDMIn.h
index c2a8bab2f..e89fc7e23 100644
--- a/shared-bindings/audiobusio/PDMIn.h
+++ b/shared-bindings/audiobusio/PDMIn.h
@@ -33,6 +33,7 @@
extern const mp_obj_type_t audiobusio_pdmin_type;
+#if CIRCUITPY_AUDIOBUSIO_PDMIN
void common_hal_audiobusio_pdmin_construct(audiobusio_pdmin_obj_t* self,
const mcu_pin_obj_t* clock_pin, const mcu_pin_obj_t* data_pin,
uint32_t sample_rate, uint8_t bit_depth, bool mono, uint8_t oversample);
@@ -43,5 +44,6 @@ uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* se
uint8_t common_hal_audiobusio_pdmin_get_bit_depth(audiobusio_pdmin_obj_t* self);
uint32_t common_hal_audiobusio_pdmin_get_sample_rate(audiobusio_pdmin_obj_t* self);
// TODO(tannewt): Add record to file
+#endif
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_AUDIOBUSIO_AUDIOOUT_H
diff --git a/shared-bindings/msgpack/ExtType.c b/shared-bindings/msgpack/ExtType.c
new file mode 100644
index 000000000..e9ddd32bc
--- /dev/null
+++ b/shared-bindings/msgpack/ExtType.c
@@ -0,0 +1,126 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Bernhard Boser
+ *
+ * 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/runtime.h"
+#include "py/smallint.h"
+#include "py/objproperty.h"
+#include "shared-bindings/msgpack/ExtType.h"
+
+//| class ExtType:
+//| """ExtType represents ext type in msgpack."""
+//| def __init__(self, code: int, data: bytes) -> None:
+//| """Constructor
+//| :param int code: type code in range 0~127.
+//| :param bytes data: representation."""
+//|
+STATIC mp_obj_t mod_msgpack_exttype_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+ mod_msgpack_extype_obj_t *self = m_new_obj(mod_msgpack_extype_obj_t);
+ self->base.type = &mod_msgpack_exttype_type;
+ enum { ARG_code, ARG_data };
+ static const mp_arg_t allowed_args[] = {
+ { MP_QSTR_code, MP_ARG_INT | MP_ARG_REQUIRED },
+ { MP_QSTR_data, MP_ARG_OBJ | MP_ARG_REQUIRED },
+ };
+ 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);
+
+ int code = args[ARG_code].u_int;
+ if (code < 0 || code > 127) {
+ mp_raise_AttributeError(translate("code outside range 0~127"));
+ }
+ self->code = code;
+
+ mp_obj_t data = args[ARG_data].u_obj;
+ self->data = data;
+ return MP_OBJ_FROM_PTR(self);
+}
+
+
+//| code: int
+//| """The type code, in range 0~127."""
+//| ...
+//|
+STATIC mp_obj_t mod_msgpack_exttype_get_code(mp_obj_t self_in) {
+ mod_msgpack_extype_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ return MP_OBJ_NEW_SMALL_INT(self->code);
+}
+MP_DEFINE_CONST_FUN_OBJ_1(mod_msgpack_exttype_get_code_obj, mod_msgpack_exttype_get_code);
+
+STATIC mp_obj_t mod_msgpack_exttype_set_code(mp_obj_t self_in, mp_obj_t code_in) {
+ mod_msgpack_extype_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ int code = mp_obj_get_int(code_in);
+ if (code < 0 || code > 127) {
+ mp_raise_AttributeError(translate("code outside range 0~127"));
+ }
+ self->code = code;
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_2(mod_msgpack_exttype_set_code_obj, mod_msgpack_exttype_set_code);
+
+const mp_obj_property_t mod_msgpack_exttype_code_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&mod_msgpack_exttype_get_code_obj,
+ (mp_obj_t)&mod_msgpack_exttype_set_code_obj,
+ (mp_obj_t)&mp_const_none_obj},
+};
+
+//| data: bytes
+//| """Data."""
+//| ...
+//|
+STATIC mp_obj_t mod_msgpack_exttype_get_data(mp_obj_t self_in) {
+ mod_msgpack_extype_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ return self->data;
+}
+MP_DEFINE_CONST_FUN_OBJ_1(mod_msgpack_exttype_get_data_obj, mod_msgpack_exttype_get_data);
+
+STATIC mp_obj_t mod_msgpack_exttype_set_data(mp_obj_t self_in, mp_obj_t data_in) {
+ mod_msgpack_extype_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ self->data = data_in;
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_2(mod_msgpack_exttype_set_data_obj, mod_msgpack_exttype_set_data);
+
+const mp_obj_property_t mod_msgpack_exttype_data_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&mod_msgpack_exttype_get_data_obj,
+ (mp_obj_t)&mod_msgpack_exttype_set_data_obj,
+ (mp_obj_t)&mp_const_none_obj},
+};
+
+STATIC mp_rom_map_elem_t mod_msgpack_exttype_locals_dict_table[] = {
+ // Properties
+ { MP_ROM_QSTR(MP_QSTR_code), MP_ROM_PTR(&mod_msgpack_exttype_code_obj) },
+ { MP_ROM_QSTR(MP_QSTR_data), MP_ROM_PTR(&mod_msgpack_exttype_data_obj) },
+};
+STATIC MP_DEFINE_CONST_DICT(mod_msgpack_exttype_locals_dict, mod_msgpack_exttype_locals_dict_table);
+
+const mp_obj_type_t mod_msgpack_exttype_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_ExtType,
+ .make_new = mod_msgpack_exttype_make_new,
+ .locals_dict = (mp_obj_dict_t*)&mod_msgpack_exttype_locals_dict,
+};
diff --git a/shared-bindings/msgpack/ExtType.h b/shared-bindings/msgpack/ExtType.h
new file mode 100644
index 000000000..64173b221
--- /dev/null
+++ b/shared-bindings/msgpack/ExtType.h
@@ -0,0 +1,40 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Bernhard Boser
+ *
+ * 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_MSGPACK_EXTTYPE___INIT___H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_MSGPACK_EXTTYPE___INIT___H
+
+#include "py/obj.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ int32_t code;
+ mp_obj_t data;
+} mod_msgpack_extype_obj_t;
+
+extern const mp_obj_type_t mod_msgpack_exttype_type;
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_MSGPACK_EXTTYPE___INIT___H
diff --git a/shared-bindings/msgpack/__init__.c b/shared-bindings/msgpack/__init__.c
new file mode 100644
index 000000000..f5c47bb21
--- /dev/null
+++ b/shared-bindings/msgpack/__init__.c
@@ -0,0 +1,162 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Bernhard Boser
+ *
+ * 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 <stdio.h>
+#include "py/obj.h"
+#include "py/runtime.h"
+#include "shared-bindings/msgpack/__init__.h"
+#include "shared-module/msgpack/__init__.h"
+#include "shared-bindings/msgpack/ExtType.h"
+
+#define MP_OBJ_IS_METH(o) (MP_OBJ_IS_OBJ(o) && (((mp_obj_base_t*)MP_OBJ_TO_PTR(o))->type->name == MP_QSTR_bound_method))
+
+//| """Pack object in msgpack format
+//|
+//| The msgpack format is similar to json, except that the encoded data is binary.
+//| See https://msgpack.org for details. The module implements a subset of the cpython
+//| module msgpack-python.
+//|
+//| Not implemented: 64-bit int, uint, float.
+//|
+//| Example 1::
+//|
+//| import msgpack
+//| from io import BytesIO
+//|
+//| b = BytesIO()
+//| msgpack.pack({'list': [True, False, None, 1, 3.14], 'str': 'blah'}, b)
+//| b.seek(0)
+//| print(msgpack.unpack(b))
+//|
+//| Example 2: handling objects::
+//|
+//| from msgpack import pack, unpack, ExtType
+//| from io import BytesIO
+//|
+//| class MyClass:
+//| def __init__(self, val):
+//| self.value = val
+//| def __str__(self):
+//| return str(self.value)
+//|
+//| data = MyClass(b'my_value')
+//|
+//| def encoder(obj):
+//| if isinstance(obj, MyClass):
+//| return ExtType(1, obj.value)
+//| return f"no encoder for {obj}"
+//|
+//| def decoder(code, data):
+//| if code == 1:
+//| return MyClass(data)
+//| return f"no decoder for type {code}"
+//|
+//| buffer = BytesIO()
+//| pack(data, buffer, default=encoder)
+//| buffer.seek(0)
+//| decoded = unpack(buffer, ext_hook=decoder)
+//| print(f"{data} -> {buffer.getvalue()} -> {decoded}")
+//|
+//| """
+//|
+
+//| def pack(obj: object, buffer: WriteableBuffer, *, default: Union[Callable[[object], None], None] = None) -> None:
+//| """Ouput object to buffer in msgpack format.
+//|
+//| :param object obj: Object to convert to msgpack format.
+//| :param ~_typing.WriteableBuffer buffer: buffer to write into
+//| :param Optional[~_typing.Callable[[object], None]] default:
+//| function called for python objects that do not have
+//| a representation in msgpack format.
+//| """
+//| ...
+//|
+STATIC mp_obj_t mod_msgpack_pack(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+ enum { ARG_obj, ARG_buffer, ARG_default };
+ STATIC const mp_arg_t allowed_args[] = {
+ { MP_QSTR_obj, MP_ARG_REQUIRED | MP_ARG_OBJ },
+ { MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ },
+ { MP_QSTR_default, MP_ARG_KW_ONLY | MP_ARG_OBJ, { .u_obj = mp_const_none } },
+ };
+ 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);
+
+ mp_obj_t handler = args[ARG_default].u_obj;
+ if (handler != mp_const_none && !MP_OBJ_IS_FUN(handler) && !MP_OBJ_IS_METH(handler)) {
+ mp_raise_ValueError(translate("default is not a function"));
+ }
+
+ common_hal_msgpack_pack(args[ARG_obj].u_obj, args[ARG_buffer].u_obj, handler);
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_KW(mod_msgpack_pack_obj, 1, mod_msgpack_pack);
+
+
+//| def unpack(buffer: ReadableBuffer, *, ext_hook: Union[Callable[[int, bytes], object], None] = None, use_list: bool=True) -> object:
+//| """Unpack and return one object from buffer.
+//|
+//| :param ~_typing.ReadableBuffer buffer: buffer to read from
+//| :param Optional[~_typing.Callable[[int, bytes], object]] ext_hook: function called for objects in
+//| msgpack ext format.
+//| :param Optional[bool] use_list: return array as list or tuple (use_list=False).
+//|
+//| :return object: object read from buffer.
+//| """
+//| ...
+//|
+STATIC mp_obj_t mod_msgpack_unpack(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+ enum { ARG_buffer, ARG_ext_hook, ARG_use_list };
+ STATIC const mp_arg_t allowed_args[] = {
+ { MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, },
+ { MP_QSTR_ext_hook, MP_ARG_KW_ONLY | MP_ARG_OBJ, { .u_obj = mp_const_none } },
+ { MP_QSTR_use_list, MP_ARG_KW_ONLY | MP_ARG_BOOL, { .u_bool = true } },
+ };
+ 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);
+
+ mp_obj_t hook = args[ARG_ext_hook].u_obj;
+ if (hook != mp_const_none && !MP_OBJ_IS_FUN(hook) && !MP_OBJ_IS_METH(hook)) {
+ mp_raise_ValueError(translate("ext_hook is not a function"));
+ }
+
+ return common_hal_msgpack_unpack(args[ARG_buffer].u_obj, hook, args[ARG_use_list].u_bool);
+}
+MP_DEFINE_CONST_FUN_OBJ_KW(mod_msgpack_unpack_obj, 1, mod_msgpack_unpack);
+
+
+STATIC const mp_rom_map_elem_t msgpack_module_globals_table[] = {
+ { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_msgpack) },
+ { MP_ROM_QSTR(MP_QSTR_ExtType), MP_ROM_PTR(&mod_msgpack_exttype_type) },
+ { MP_ROM_QSTR(MP_QSTR_pack), MP_ROM_PTR(&mod_msgpack_pack_obj) },
+ { MP_ROM_QSTR(MP_QSTR_unpack), MP_ROM_PTR(&mod_msgpack_unpack_obj) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(msgpack_module_globals, msgpack_module_globals_table);
+
+const mp_obj_module_t msgpack_module = {
+ .base = { &mp_type_module },
+ .globals = (mp_obj_dict_t*)&msgpack_module_globals,
+};
diff --git a/shared-bindings/msgpack/__init__.h b/shared-bindings/msgpack/__init__.h
new file mode 100644
index 000000000..a02ead0bd
--- /dev/null
+++ b/shared-bindings/msgpack/__init__.h
@@ -0,0 +1,34 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 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_MSGPACK___INIT___H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_MSGPACK___INIT___H
+
+#include "py/obj.h"
+
+// nothing for now
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_MSGPACK___INIT___H
diff --git a/shared-bindings/wifi/Network.c b/shared-bindings/wifi/Network.c
index 009712ad1..0f3006f2e 100644
--- a/shared-bindings/wifi/Network.c
+++ b/shared-bindings/wifi/Network.c
@@ -124,6 +124,21 @@ const mp_obj_property_t wifi_network_country_obj = {
(mp_obj_t)&mp_const_none_obj },
};
+//| authmode: str
+//| """String id of the authmode"""
+//|
+STATIC mp_obj_t wifi_network_get_authmode(mp_obj_t self) {
+ return common_hal_wifi_network_get_authmode(self);
+
+}
+MP_DEFINE_CONST_FUN_OBJ_1(wifi_network_get_authmode_obj, wifi_network_get_authmode);
+
+const mp_obj_property_t wifi_network_authmode_obj = {
+ .base.type = &mp_type_property,
+ .proxy = { (mp_obj_t)&wifi_network_get_authmode_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_obj },
+};
STATIC const mp_rom_map_elem_t wifi_network_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_ssid), MP_ROM_PTR(&wifi_network_ssid_obj) },
@@ -131,6 +146,7 @@ STATIC const mp_rom_map_elem_t wifi_network_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_rssi), MP_ROM_PTR(&wifi_network_rssi_obj) },
{ MP_ROM_QSTR(MP_QSTR_channel), MP_ROM_PTR(&wifi_network_channel_obj) },
{ MP_ROM_QSTR(MP_QSTR_country), MP_ROM_PTR(&wifi_network_country_obj) },
+ { MP_ROM_QSTR(MP_QSTR_authmode), MP_ROM_PTR(&wifi_network_authmode_obj) },
};
STATIC MP_DEFINE_CONST_DICT(wifi_network_locals_dict, wifi_network_locals_dict_table);
diff --git a/shared-bindings/wifi/Network.h b/shared-bindings/wifi/Network.h
index e672e3108..0f07e7b55 100644
--- a/shared-bindings/wifi/Network.h
+++ b/shared-bindings/wifi/Network.h
@@ -40,5 +40,6 @@ extern mp_obj_t common_hal_wifi_network_get_bssid(wifi_network_obj_t *self);
extern mp_obj_t common_hal_wifi_network_get_rssi(wifi_network_obj_t *self);
extern mp_obj_t common_hal_wifi_network_get_channel(wifi_network_obj_t *self);
extern mp_obj_t common_hal_wifi_network_get_country(wifi_network_obj_t *self);
+extern mp_obj_t common_hal_wifi_network_get_authmode(wifi_network_obj_t *self);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_WIFI_NETWORK_H
diff --git a/shared-bindings/wifi/Radio.c b/shared-bindings/wifi/Radio.c
index 723572a32..8181fc4c9 100644
--- a/shared-bindings/wifi/Radio.c
+++ b/shared-bindings/wifi/Radio.c
@@ -218,12 +218,12 @@ STATIC mp_obj_t wifi_radio_connect(size_t n_args, const mp_obj_t *pos_args, mp_m
}
wifi_radio_error_t error = common_hal_wifi_radio_connect(self, ssid.buf, ssid.len, password.buf, password.len, args[ARG_channel].u_int, timeout, bssid.buf, bssid.len);
- if (error == WIFI_RADIO_ERROR_AUTH) {
+ if (error == WIFI_RADIO_ERROR_AUTH_FAIL) {
mp_raise_ConnectionError(translate("Authentication failure"));
} else if (error == WIFI_RADIO_ERROR_NO_AP_FOUND) {
mp_raise_ConnectionError(translate("No network with that ssid"));
} else if (error != WIFI_RADIO_ERROR_NONE) {
- mp_raise_ConnectionError(translate("Unknown failure"));
+ mp_raise_msg_varg(&mp_type_ConnectionError, translate("Unknown failure %d"), error);
}
return mp_const_none;
@@ -295,7 +295,7 @@ const mp_obj_property_t wifi_radio_ipv4_dns_obj = {
};
//| ap_info: Optional[Network]
-//| """Network object containing BSSID, SSID, channel, country and RSSI when connected to an access point. None otherwise."""
+//| """Network object containing BSSID, SSID, authmode, channel, country and RSSI when connected to an access point. None otherwise."""
//|
STATIC mp_obj_t wifi_radio_get_ap_info(mp_obj_t self) {
return common_hal_wifi_radio_get_ap_info(self);
diff --git a/shared-bindings/wifi/Radio.h b/shared-bindings/wifi/Radio.h
index 1e8f362e3..64c1052f7 100644
--- a/shared-bindings/wifi/Radio.h
+++ b/shared-bindings/wifi/Radio.h
@@ -36,10 +36,39 @@
const mp_obj_type_t wifi_radio_type;
typedef enum {
- WIFI_RADIO_ERROR_NONE,
- WIFI_RADIO_ERROR_UNKNOWN,
- WIFI_RADIO_ERROR_AUTH,
- WIFI_RADIO_ERROR_NO_AP_FOUND
+ // 0 is circuitpython-specific; 1-53 are IEEE; 200+ are Espressif
+ WIFI_RADIO_ERROR_NONE = 0,
+ WIFI_RADIO_ERROR_UNSPECIFIED = 1,
+ WIFI_RADIO_ERROR_AUTH_EXPIRE = 2,
+ WIFI_RADIO_ERROR_AUTH_LEAVE = 3,
+ WIFI_RADIO_ERROR_ASSOC_EXPIRE = 4,
+ WIFI_RADIO_ERROR_ASSOC_TOOMANY = 5,
+ WIFI_RADIO_ERROR_NOT_AUTHED = 6,
+ WIFI_RADIO_ERROR_NOT_ASSOCED = 7,
+ WIFI_RADIO_ERROR_ASSOC_LEAVE = 8,
+ WIFI_RADIO_ERROR_ASSOC_NOT_AUTHED = 9,
+ WIFI_RADIO_ERROR_DISASSOC_PWRCAP_BAD = 10,
+ WIFI_RADIO_ERROR_DISASSOC_SUPCHAN_BAD = 11,
+ WIFI_RADIO_ERROR_IE_INVALID = 13,
+ WIFI_RADIO_ERROR_MIC_FAILURE = 14,
+ WIFI_RADIO_ERROR_4WAY_HANDSHAKE_TIMEOUT = 15,
+ WIFI_RADIO_ERROR_GROUP_KEY_UPDATE_TIMEOUT = 16,
+ WIFI_RADIO_ERROR_IE_IN_4WAY_DIFFERS = 17,
+ WIFI_RADIO_ERROR_GROUP_CIPHER_INVALID = 18,
+ WIFI_RADIO_ERROR_PAIRWISE_CIPHER_INVALID = 19,
+ WIFI_RADIO_ERROR_AKMP_INVALID = 20,
+ WIFI_RADIO_ERROR_UNSUPP_RSN_IE_VERSION = 21,
+ WIFI_RADIO_ERROR_INVALID_RSN_IE_CAP = 22,
+ WIFI_RADIO_ERROR_802_1X_AUTH_FAILED = 23,
+ WIFI_RADIO_ERROR_CIPHER_SUITE_REJECTED = 24,
+ WIFI_RADIO_ERROR_INVALID_PMKID = 53,
+ WIFI_RADIO_ERROR_BEACON_TIMEOUT = 200,
+ WIFI_RADIO_ERROR_NO_AP_FOUND = 201,
+ WIFI_RADIO_ERROR_AUTH_FAIL = 202,
+ WIFI_RADIO_ERROR_ASSOC_FAIL = 203,
+ WIFI_RADIO_ERROR_HANDSHAKE_TIMEOUT = 204,
+ WIFI_RADIO_ERROR_CONNECTION_FAIL = 205,
+ WIFI_RADIO_ERROR_AP_TSF_RESET = 206,
} wifi_radio_error_t;
extern bool common_hal_wifi_radio_get_enabled(wifi_radio_obj_t *self);