summaryrefslogtreecommitdiff
path: root/shared-bindings
diff options
context:
space:
mode:
Diffstat (limited to 'shared-bindings')
-rw-r--r--shared-bindings/bleio/Characteristic.c12
-rw-r--r--shared-bindings/bleio/Service.c8
-rw-r--r--shared-bindings/bleio/UUID.c151
-rw-r--r--shared-bindings/bleio/UUID.h6
-rw-r--r--shared-bindings/bleio/UUIDType.c75
-rw-r--r--shared-bindings/bleio/UUIDType.h46
-rw-r--r--shared-bindings/bleio/__init__.c3
7 files changed, 109 insertions, 192 deletions
diff --git a/shared-bindings/bleio/Characteristic.c b/shared-bindings/bleio/Characteristic.c
index 369f3f991..2dd1d4270 100644
--- a/shared-bindings/bleio/Characteristic.c
+++ b/shared-bindings/bleio/Characteristic.c
@@ -80,18 +80,19 @@
//| .. attribute:: write
//|
-//| A `bool` specifying if the characteristic allows writting to its value.
+//| A `bool` specifying if the characteristic allows writing to its value.
//|
//| .. attribute:: write_no_resp
//|
-//| A `bool` specifying if the characteristic allows writting to its value without response.
+//| A `bool` specifying if the characteristic allows writing to its value without response.
//|
STATIC void bleio_characteristic_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
- mp_printf(print, "Characteristic(uuid: 0x"HEX2_FMT""HEX2_FMT" handle: 0x" HEX2_FMT ")",
- self->uuid->value[1], self->uuid->value[0], self->handle);
+ mp_printf(print, "Characteristic(");
+ common_hal_bleio_uuid_print(print, self->uuid);
+ mp_printf(print, ")");
}
STATIC mp_obj_t bleio_characteristic_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) {
@@ -121,8 +122,7 @@ STATIC mp_obj_t bleio_characteristic_make_new(const mp_obj_type_t *type, size_t
if (MP_OBJ_IS_TYPE(uuid, &bleio_uuid_type)) {
self->uuid = MP_OBJ_TO_PTR(uuid);
} else {
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
- translate("Invalid UUID parameter")));
+ mp_raise_ValueError(translate("Expected a UUID"));
}
common_hal_bleio_characteristic_construct(self);
diff --git a/shared-bindings/bleio/Service.c b/shared-bindings/bleio/Service.c
index 2d09cbc3c..727350215 100644
--- a/shared-bindings/bleio/Service.c
+++ b/shared-bindings/bleio/Service.c
@@ -66,8 +66,9 @@
STATIC void bleio_service_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
bleio_service_obj_t *self = MP_OBJ_TO_PTR(self_in);
- mp_printf(print, "Service(uuid: 0x"HEX2_FMT""HEX2_FMT")",
- self->uuid->value[1], self->uuid->value[0]);
+ mp_printf(print, "Service(");
+ common_hal_bleio_uuid_print(print, self->uuid);
+ mp_print(print, ")");
}
STATIC mp_obj_t bleio_service_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) {
@@ -101,8 +102,7 @@ STATIC mp_obj_t bleio_service_make_new(const mp_obj_type_t *type, size_t n_args,
if (MP_OBJ_IS_TYPE(uuid, &bleio_uuid_type)) {
self->uuid = MP_OBJ_TO_PTR(uuid);
} else {
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
- translate("Invalid UUID parameter")));
+ mp_raise_ValueError(translate("Expected a UUID or None"));
}
return MP_OBJ_FROM_PTR(self);
diff --git a/shared-bindings/bleio/UUID.c b/shared-bindings/bleio/UUID.c
index 7cef9a26f..33d280304 100644
--- a/shared-bindings/bleio/UUID.c
+++ b/shared-bindings/bleio/UUID.c
@@ -5,6 +5,7 @@
*
* Copyright (c) 2017 Glenn Ruben Bakke
* Copyright (c) 2018 Artur Pacholec
+ * 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
@@ -29,47 +30,38 @@
#include "py/runtime.h"
#include "shared-bindings/bleio/UUID.h"
+// Including hyphens.
+#define UUID128_STR_LEN 36
+// Number of bytes
+#define UUID128_BYTE_LEN 32
+
+STATIC uint8_t xdigit_8b_value(byte nibble1, byte nibble2) {
+ return unichar_xdigit_value(nibble1) | (unichar_xdigit_value(nibble2) << 4);
+}
+
+
//| .. currentmodule:: bleio
//|
-//| :class:`UUID` -- BLE UUID
+//| :class:`UUID16` -- BLE UUID16
//| =========================================================
//|
-//| Encapsulates both 16-bit and 128-bit UUIDs. Can be used for services,
-//| characteristics, descriptors and more.
+//| A 16-bit or 128-bit UUID. Can be used for services, characteristics, descriptors and more.
//|
//| .. class:: UUID(uuid)
//|
-//| Create a new UUID object encapsulating the uuid value.
-//| The value itself can be one of:
+//| Create a new UUID or UUID object encapsulating the uuid value.
+//| The value can be one of:
//|
-//| - a `int` value in range of 0 to 0xFFFF
-//| - a `str` value in the format of '0xXXXX' for 16-bit or 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX' for 128-bit
+//| - an `int` value in range 0 to 0xFFFF (Bluetooth SIG 16-bit UUID)
+//| - a `str` value in the format 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', where the X's are hex digits.
+//| (128 bit UUID)
//|
//| :param int/str uuid: The uuid to encapsulate
//|
-//| .. method:: __len__()
-//|
-//| Returns the uuid length in bits
-//|
-//| This allows you to:
-//|
-//| uuid = bleio.UUID(0x1801)
-//| print(len(uuid))
-//|
-
-//| .. attribute:: type
-//|
-//| The UUID type. One of:
-//|
-//| - `bleio.UUIDType.TYPE_16BIT`
-//| - `bleio.UUIDType.TYPE_128BIT`
-//|
STATIC mp_obj_t bleio_uuid_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) {
mp_arg_check_num(n_args, n_kw, 1, 1, true);
- bleio_uuid_obj_t *self = m_new_obj(bleio_uuid_obj_t);
- self->base.type = &bleio_uuid_type;
mp_map_t kw_args;
mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args);
@@ -84,20 +76,55 @@ STATIC mp_obj_t bleio_uuid_make_new(const mp_obj_type_t *type, size_t n_args, si
const mp_obj_t uuid = args[ARG_uuid].u_obj;
- common_hal_bleio_uuid_construct(self, &uuid);
-
- return MP_OBJ_FROM_PTR(self);
-}
-
-STATIC mp_obj_t bleio_uuid_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
- bleio_uuid_obj_t *self = MP_OBJ_TO_PTR(self_in);
-
- const bleio_uuid_type_t type = common_hal_bleio_uuid_get_type(self);
- const uint8_t len = (type == UUID_TYPE_16BIT) ? 16 : 128;
- 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
+ if (MP_OBJ_IS_INT(uuid)) {
+ mp_int_t uuid16 = mp_obj_get_int(uuid);
+ if (uuid16 < 0 || uuid16 > 0xffff) {
+ mp_raise_ValueError(translate("Integer UUID not in range 0 to 0xffff"));
+ }
+
+ // This is a 16-bit Bluetooth SIG UUID. NULL means no 128-bit value.
+ common_hal_bleio_uuid_construct(self, uuid16, NULL);
+
+ } else if (MP_OBJ_IS_STR(uuid)) {
+ uint8_t uuid128[UUID128_BYTE_LEN];
+ GET_STR_DATA_LEN(uuid, str, str_len);
+ if (str_len == UUID128_STR_LEN &&
+ str[8] == '-' && str[13] == '-' && str[18] == '-' && str[23] == '-') {
+ size_t str_index = UUID128_STR_LEN - 1;
+ size_t uuid128_index = 0;
+ bool error = false;
+
+ // Loop until fewer than two characters left.
+ while (str_index >= 1 && uuid128_index < UUID128_BYTE_LEN) {
+ if (str[str_index] == '-') {
+ // Skip hyphen separators.
+ str--;
+ continue;
+ }
+
+ if (!unichar_isxdigit(str[str_index]) ||
+ !unichar_isxdigit(str[str_index-1])) {
+ error = true;
+ break;
+ }
+
+ uuid128[uuid128_index] = xdigit_8b_value(str[str_index],
+ str[str_index-1]);
+ uuid128_index += 1;
+ str_index -= 2;
+ }
+ // Check for correct number of hex digits and no parsing errors.
+ if (!error && uuid128_index == UUID128_BYTE_LEN && str_index == -1) {
+ uint32_t uuid16 = (uuid128[13] << 8) | uuid128[12];
+ uuid128[12] = 0;
+ uuid128[13] = 0;
+ common_hal_bleio_uuid_construct(self, uuid16, uuid128);
+ return MP_OBJ_FROM_PTR(self);
+ }
+ }
+ mp_raise_ValueError(translate("UUID string must be of the form xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"));
+ } else {
+ mp_raise_ValueError(translate("UUID value is not int or string"));
}
}
@@ -107,31 +134,46 @@ STATIC void bleio_uuid_print(const mp_print_t *print, mp_obj_t self_in, mp_print
common_hal_bleio_uuid_print(self, print);
}
-STATIC mp_obj_t bleio_uuid_get_type(mp_obj_t self_in) {
+//| .. attribute:: uuid16
+//|
+//| The 16-bit part of the UUID. (read-only)
+//|
+STATIC void bleio_uuid_get_uuid16(mp_obj_t self_in) {
bleio_uuid_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ return MP_OBJ_NEW_SMALL_INT(common_hal_bleio_uuid_get_uuid16(self));
+}
- const bleio_uuid_type_t type = common_hal_bleio_uuid_get_type(self);
- if (type == UUID_TYPE_16BIT) {
- return (mp_obj_t)&bleio_uuidtype_16bit_obj;
- }
+MP_DEFINE_CONST_FUN_OBJ_1(bleio_uuid_get_uuid16_obj, bleio_uuid_get_uuid16);
- if (type == UUID_TYPE_128BIT) {
- return (mp_obj_t)&bleio_uuidtype_128bit_obj;
- }
+const mp_obj_property_t bleio_uuid16_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&bleio_uuid_get_uuid16_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_obj},
+};
- return (mp_obj_t)&mp_const_none_obj;
+//| .. attribute:: vendor_specific
+//|
+//| True if this UUID represents a 128-bit vendor-specific UUID.
+//| False if this UUID represents a 16-bit Bluetooth SIG assigned UUID. (read-only)
+//|
+STATIC mp_obj_t bleio_uuid_get_vendor_specific(mp_obj_t self_in) {
+ bleio_uuid_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ return mp_obj_new_bool(common_hal_bleio_uuid_get_vendor_specific(self));
}
-MP_DEFINE_CONST_FUN_OBJ_1(bleio_uuid_get_type_obj, bleio_uuid_get_type);
-const mp_obj_property_t bleio_uuid_type_obj = {
+MP_DEFINE_CONST_FUN_OBJ_1(bleio_uuid_get_vendor_specific_obj, bleio_uuid_get_vendor_specific);
+
+const mp_obj_property_t bleio_uuid_vendor_specific_obj = {
.base.type = &mp_type_property,
- .proxy = {(mp_obj_t)&bleio_uuid_get_type_obj,
+ .proxy = {(mp_obj_t)&bleio_uuid_get_vendor_specific_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj},
};
STATIC const mp_rom_map_elem_t bleio_uuid_locals_dict_table[] = {
- { MP_ROM_QSTR(MP_QSTR_type), MP_ROM_PTR(&bleio_uuid_type_obj) },
+ { MP_ROM_QSTR(MP_QSTR_uuid16), MP_ROM_PTR(&bleio_uuid16_obj) },
+ { MP_ROM_QSTR(MP_QSTR_vendor_specific), MP_ROM_PTR(&bleio_uuid_vendor_specific_obj) },
};
STATIC MP_DEFINE_CONST_DICT(bleio_uuid_locals_dict, bleio_uuid_locals_dict_table);
@@ -141,6 +183,5 @@ const mp_obj_type_t bleio_uuid_type = {
.name = MP_QSTR_UUID,
.print = bleio_uuid_print,
.make_new = bleio_uuid_make_new,
- .unary_op = bleio_uuid_unary_op,
- .locals_dict = (mp_obj_dict_t*)&bleio_uuid_locals_dict
+ .locals_dict = (mp_obj_dict_t*)&bleio_uuid_locals_dict,
};
diff --git a/shared-bindings/bleio/UUID.h b/shared-bindings/bleio/UUID.h
index d6fcbac1b..e27058516 100644
--- a/shared-bindings/bleio/UUID.h
+++ b/shared-bindings/bleio/UUID.h
@@ -28,12 +28,12 @@
#define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_UUID_H
#include "common-hal/bleio/UUID.h"
-#include "shared-bindings/bleio/UUIDType.h"
extern const mp_obj_type_t bleio_uuid_type;
-extern void common_hal_bleio_uuid_construct(bleio_uuid_obj_t *self, const mp_obj_t *uuid);
+extern void common_hal_bleio_uuid_construct(bleio_uuid_obj_t *self, mp_int_t uuid16, uint8_t uuid128[]);
extern void common_hal_bleio_uuid_print(bleio_uuid_obj_t *self, const mp_print_t *print);
-extern bleio_uuid_type_t common_hal_bleio_uuid_get_type(bleio_uuid_obj_t *self);
+extern bool common_hal_bleio_uuid_get_vendor_specific(bleio_uuid_obj_t *self);
+extern uint32_t common_hal_bleio_uuid_get_uuid16(bleio_uuid_obj_t *self);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_UUID_H
diff --git a/shared-bindings/bleio/UUIDType.c b/shared-bindings/bleio/UUIDType.c
deleted file mode 100644
index e36d1f06d..000000000
--- a/shared-bindings/bleio/UUIDType.c
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * This file is part of the Micro Python project, http://micropython.org/
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2018 Artur Pacholec
- *
- * 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 "shared-bindings/bleio/UUIDType.h"
-
-//| .. currentmodule:: bleio
-//|
-//| :class:`UUIDType` -- defines the type of a BLE UUID
-//| =============================================================
-//|
-//| .. class:: bleio.UUIDType
-//|
-//| Enum-like class to define the type of a BLE UUID.
-//|
-//| .. data:: TYPE_16BIT
-//|
-//| The UUID is 16-bit
-//|
-//| .. data:: TYPE_128BIT
-//|
-//| The UUID is 128-bit
-//|
-const mp_obj_type_t bleio_uuidtype_type;
-
-const bleio_uuidtype_obj_t bleio_uuidtype_16bit_obj = {
- { &bleio_uuidtype_type },
-};
-
-const bleio_uuidtype_obj_t bleio_uuidtype_128bit_obj = {
- { &bleio_uuidtype_type },
-};
-
-STATIC const mp_rom_map_elem_t bleio_uuidtype_locals_dict_table[] = {
- { MP_ROM_QSTR(MP_QSTR_TYPE_16BIT), MP_ROM_PTR(&bleio_uuidtype_16bit_obj) },
- { MP_ROM_QSTR(MP_QSTR_TYPE_128BIT), MP_ROM_PTR(&bleio_uuidtype_128bit_obj) },
-};
-STATIC MP_DEFINE_CONST_DICT(bleio_uuidtype_locals_dict, bleio_uuidtype_locals_dict_table);
-
-STATIC void bleio_uuidtype_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
- qstr type = MP_QSTR_TYPE_128BIT;
- if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&bleio_uuidtype_16bit_obj)) {
- type = MP_QSTR_TYPE_16BIT;
- }
- mp_printf(print, "%q.%q.%q", MP_QSTR_bleio, MP_QSTR_UUIDType, type);
-}
-
-const mp_obj_type_t bleio_uuidtype_type = {
- { &mp_type_type },
- .name = MP_QSTR_UUIDType,
- .print = bleio_uuidtype_print,
- .locals_dict = (mp_obj_t)&bleio_uuidtype_locals_dict,
-};
diff --git a/shared-bindings/bleio/UUIDType.h b/shared-bindings/bleio/UUIDType.h
deleted file mode 100644
index 87bbf9b53..000000000
--- a/shared-bindings/bleio/UUIDType.h
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * This file is part of the Micro Python project, http://micropython.org/
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2018 Artur Pacholec
- *
- * 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_BLEIO_UUIDTYPE_H
-#define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_UUIDTYPE_H
-
-#include "py/obj.h"
-
-typedef enum {
- UUID_TYPE_16BIT,
- UUID_TYPE_128BIT
-} bleio_uuid_type_t;
-
-extern const mp_obj_type_t bleio_uuidtype_type;
-
-typedef struct {
- mp_obj_base_t base;
-} bleio_uuidtype_obj_t;
-
-extern const bleio_uuidtype_obj_t bleio_uuidtype_16bit_obj;
-extern const bleio_uuidtype_obj_t bleio_uuidtype_128bit_obj;
-
-#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_UUIDTYPE_H
diff --git a/shared-bindings/bleio/__init__.c b/shared-bindings/bleio/__init__.c
index 98099422c..46822603f 100644
--- a/shared-bindings/bleio/__init__.c
+++ b/shared-bindings/bleio/__init__.c
@@ -36,7 +36,6 @@
#include "shared-bindings/bleio/Scanner.h"
#include "shared-bindings/bleio/Service.h"
#include "shared-bindings/bleio/UUID.h"
-#include "shared-bindings/bleio/UUIDType.h"
//| :mod:`bleio` --- Bluetooth Low Energy functionality
//| ================================================================
@@ -63,7 +62,6 @@
//| Scanner
//| Service
//| UUID
-//| UUIDType
//|
//| .. attribute:: adapter
//|
@@ -89,7 +87,6 @@ STATIC const mp_rom_map_elem_t bleio_module_globals_table[] = {
// Enum-like Classes.
{ MP_ROM_QSTR(MP_QSTR_AddressType), MP_ROM_PTR(&bleio_addresstype_type) },
- { MP_ROM_QSTR(MP_QSTR_UUIDType), MP_ROM_PTR(&bleio_uuidtype_type) },
};
STATIC MP_DEFINE_CONST_DICT(bleio_module_globals, bleio_module_globals_table);