summaryrefslogtreecommitdiff
path: root/ports/nrf
diff options
context:
space:
mode:
authorarturo182 <arturo182@tlen.pl>2018-07-16 14:44:20 +0200
committerarturo182 <arturo182@tlen.pl>2018-10-21 15:43:08 +0200
commitf4940c9aec2ac73086efe18fbc57151f55c85981 (patch)
tree633bbd5f1c054760b409e6638a9c8c0296ab9a49 /ports/nrf
parentb436666e852b0d75a970052997ea019af7411b45 (diff)
nrf: Move the UUID class from ubluepy to the shared bleio module
Also added a UUIDType enum-like class for determining UUID type.
Diffstat (limited to 'ports/nrf')
-rwxr-xr-xports/nrf/Makefile9
-rw-r--r--ports/nrf/common-hal/bleio/UUID.c125
-rw-r--r--ports/nrf/common-hal/bleio/UUID.h41
-rw-r--r--ports/nrf/drivers/bluetooth/ble_drv.c4
-rw-r--r--ports/nrf/drivers/bluetooth/ble_uart.c18
-rw-r--r--ports/nrf/modules/ubluepy/modubluepy.c2
-rw-r--r--ports/nrf/modules/ubluepy/modubluepy.h20
-rw-r--r--ports/nrf/modules/ubluepy/ubluepy_characteristic.c3
-rw-r--r--ports/nrf/modules/ubluepy/ubluepy_peripheral.c10
-rw-r--r--ports/nrf/modules/ubluepy/ubluepy_service.c8
-rw-r--r--ports/nrf/modules/ubluepy/ubluepy_uuid.c174
11 files changed, 201 insertions, 213 deletions
diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile
index 8b84f43df..3cb7b2035 100755
--- a/ports/nrf/Makefile
+++ b/ports/nrf/Makefile
@@ -132,7 +132,6 @@ DRIVERS_SRC_C += $(addprefix modules/,\
ubluepy/ubluepy_peripheral.c \
ubluepy/ubluepy_service.c \
ubluepy/ubluepy_characteristic.c \
- ubluepy/ubluepy_uuid.c \
ubluepy/ubluepy_delegate.c \
ubluepy/ubluepy_constants.c \
ubluepy/ubluepy_descriptor.c \
@@ -168,7 +167,8 @@ SRC_COMMON_HAL += \
ifneq ($(SD), )
SRC_COMMON_HAL += \
bleio/__init__.c \
- bleio/Adapter.c
+ bleio/Adapter.c \
+ bleio/UUID.c
endif
# These don't have corresponding files in each port but are still located in
@@ -183,6 +183,11 @@ SRC_BINDINGS_ENUMS = \
math/__init__.c \
util.c
+ifneq ($(SD), )
+SRC_BINDINGS_ENUMS += \
+ bleio/UUIDType.c
+endif
+
SRC_COMMON_HAL_EXPANDED = $(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \
$(addprefix shared-bindings/, $(SRC_BINDINGS_ENUMS)) \
$(addprefix common-hal/, $(SRC_COMMON_HAL))
diff --git a/ports/nrf/common-hal/bleio/UUID.c b/ports/nrf/common-hal/bleio/UUID.c
new file mode 100644
index 000000000..e655589a6
--- /dev/null
+++ b/ports/nrf/common-hal/bleio/UUID.c
@@ -0,0 +1,125 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Glenn Ruben Bakke
+ * 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 "ble_drv.h"
+#include "common-hal/bleio/UUID.h"
+#include "py/objstr.h"
+#include "py/runtime.h"
+#include "shared-bindings/bleio/UUID.h"
+
+#define UUID_STR_16BIT_LEN 6
+#define UUID_STR_128BIT_LEN 36
+
+static uint8_t xdigit_8b_value(byte nibble1, byte nibble2) {
+ return unichar_xdigit_value(nibble1) |
+ (unichar_xdigit_value(nibble2) << 4);
+}
+
+void common_hal_bleio_uuid_construct(bleio_uuid_obj_t *self, const mp_obj_t *uuid) {
+ if (MP_OBJ_IS_INT(*uuid)) {
+ self->type = UUID_TYPE_16BIT;
+
+ self->value[1] = (mp_obj_get_int(*uuid) >> 8) & 0xFF;
+ self->value[0] = (mp_obj_get_int(*uuid) >> 0) & 0xFF;
+ return;
+ }
+
+ if (MP_OBJ_IS_STR(*uuid)) {
+ GET_STR_DATA_LEN(*uuid, str_data, str_len);
+
+ if (str_len == UUID_STR_16BIT_LEN) {
+ self->type = UUID_TYPE_16BIT;
+
+ self->value[0] = xdigit_8b_value(str_data[5], str_data[4]);
+ self->value[1] = xdigit_8b_value(str_data[3], str_data[2]);
+ } else if (str_len == UUID_STR_128BIT_LEN) {
+ self->type = UUID_TYPE_128BIT;
+
+ uint8_t buffer[16];
+ buffer[0] = xdigit_8b_value(str_data[35], str_data[34]);
+ buffer[1] = xdigit_8b_value(str_data[33], str_data[32]);
+ buffer[2] = xdigit_8b_value(str_data[31], str_data[30]);
+ buffer[3] = xdigit_8b_value(str_data[29], str_data[28]);
+ buffer[4] = xdigit_8b_value(str_data[27], str_data[26]);
+ buffer[5] = xdigit_8b_value(str_data[25], str_data[24]);
+
+ // 23 '-'
+ buffer[6] = xdigit_8b_value(str_data[22], str_data[21]);
+ buffer[7] = xdigit_8b_value(str_data[20], str_data[19]);
+
+ // 18 '-'
+ buffer[8] = xdigit_8b_value(str_data[17], str_data[16]);
+ buffer[9] = xdigit_8b_value(str_data[15], str_data[14]);
+
+ // 13 '-'
+ buffer[10] = xdigit_8b_value(str_data[12], str_data[11]);
+ buffer[11] = xdigit_8b_value(str_data[10], str_data[9]);
+
+ // 8 '-'
+ self->value[0] = xdigit_8b_value(str_data[7], str_data[6]);
+ self->value[1] = xdigit_8b_value(str_data[5], str_data[4]);
+
+ buffer[14] = xdigit_8b_value(str_data[3], str_data[2]);
+ buffer[15] = xdigit_8b_value(str_data[1], str_data[0]);
+
+ ble_drv_uuid_add_vs(buffer, &self->uuid_vs_idx);
+ } else {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
+ "Invalid UUID string length"));
+ }
+
+ return;
+ }
+
+ // deep copy
+ if (MP_OBJ_IS_TYPE(*uuid, &bleio_uuid_type)) {
+ bleio_uuid_obj_t *other = MP_OBJ_TO_PTR(*uuid);
+ self->type = other->type;
+ self->uuid_vs_idx = other->uuid_vs_idx;
+ self->value[0] = other->value[0];
+ self->value[1] = other->value[1];
+
+ return;
+ }
+
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
+ "Invalid UUID parameter"));
+}
+
+void common_hal_bleio_uuid_print(bleio_uuid_obj_t *self, const mp_print_t *print) {
+ if (self->type == UUID_TYPE_16BIT) {
+ mp_printf(print, "UUID(uuid: 0x" HEX2_FMT HEX2_FMT ")",
+ self->value[1], self->value[0]);
+ } else {
+ mp_printf(print, "UUID(uuid: 0x" HEX2_FMT HEX2_FMT ", VS idx: " HEX2_FMT ")",
+ self->value[1], self->value[0], self->uuid_vs_idx);
+ }
+}
+
+bleio_uuid_type_t common_hal_bleio_uuid_get_type(bleio_uuid_obj_t *self) {
+ return self->type;
+}
diff --git a/ports/nrf/common-hal/bleio/UUID.h b/ports/nrf/common-hal/bleio/UUID.h
new file mode 100644
index 000000000..b919fec28
--- /dev/null
+++ b/ports/nrf/common-hal/bleio/UUID.h
@@ -0,0 +1,41 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Glenn Ruben Bakke
+ * 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_NRF_COMMON_HAL_BLEIO_UUID_H
+#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_UUID_H
+
+#include "py/obj.h"
+#include "shared-bindings/bleio/UUIDType.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ bleio_uuid_type_t type;
+ uint8_t uuid_vs_idx;
+ uint8_t value[2];
+} bleio_uuid_obj_t;
+
+#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_UUID_H
diff --git a/ports/nrf/drivers/bluetooth/ble_drv.c b/ports/nrf/drivers/bluetooth/ble_drv.c
index 3f21f8c49..81d2a8ece 100644
--- a/ports/nrf/drivers/bluetooth/ble_drv.c
+++ b/ports/nrf/drivers/bluetooth/ble_drv.c
@@ -430,11 +430,11 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) {
for (uint8_t i = 0; i < p_adv_params->num_of_services; i++) {
ubluepy_service_obj_t * p_service = (ubluepy_service_obj_t *)p_adv_params->p_services[i];
- if (p_service->p_uuid->type == UBLUEPY_UUID_16_BIT) {
+ if (p_service->p_uuid->type == UUID_TYPE_16BIT) {
type_16bit_present = true;
}
- if (p_service->p_uuid->type == UBLUEPY_UUID_128_BIT) {
+ if (p_service->p_uuid->type == UUID_TYPE_128BIT) {
type_128bit_present = true;
}
}
diff --git a/ports/nrf/drivers/bluetooth/ble_uart.c b/ports/nrf/drivers/bluetooth/ble_uart.c
index 52c042f7b..b1d54eefa 100644
--- a/ports/nrf/drivers/bluetooth/ble_uart.c
+++ b/ports/nrf/drivers/bluetooth/ble_uart.c
@@ -34,21 +34,21 @@
#if MICROPY_PY_BLE_NUS
-static ubluepy_uuid_obj_t uuid_obj_service = {
- .base.type = &ubluepy_uuid_type,
- .type = UBLUEPY_UUID_128_BIT,
+static bleio_uuid_obj_t uuid_obj_service = {
+ .base.type = &bleio_uuid_type,
+ .type = UUID_128_BIT,
.value = {0x01, 0x00}
};
-static ubluepy_uuid_obj_t uuid_obj_char_tx = {
- .base.type = &ubluepy_uuid_type,
- .type = UBLUEPY_UUID_128_BIT,
+static bleio_uuid_obj_t uuid_obj_char_tx = {
+ .base.type = &bleio_uuid_type,
+ .type = UUID_128_BIT,
.value = {0x03, 0x00}
};
-static ubluepy_uuid_obj_t uuid_obj_char_rx = {
- .base.type = &ubluepy_uuid_type,
- .type = UBLUEPY_UUID_128_BIT,
+static bleio_uuid_obj_t uuid_obj_char_rx = {
+ .base.type = &bleio_uuid_type,
+ .type = UUID_128_BIT,
.value = {0x02, 0x00}
};
diff --git a/ports/nrf/modules/ubluepy/modubluepy.c b/ports/nrf/modules/ubluepy/modubluepy.c
index b306c065b..b4831e4f9 100644
--- a/ports/nrf/modules/ubluepy/modubluepy.c
+++ b/ports/nrf/modules/ubluepy/modubluepy.c
@@ -30,7 +30,6 @@
extern const mp_obj_type_t ubluepy_peripheral_type;
extern const mp_obj_type_t ubluepy_service_type;
-extern const mp_obj_type_t ubluepy_uuid_type;
extern const mp_obj_type_t ubluepy_characteristic_type;
extern const mp_obj_type_t ubluepy_delegate_type;
extern const mp_obj_type_t ubluepy_constants_type;
@@ -50,7 +49,6 @@ STATIC const mp_rom_map_elem_t mp_module_ubluepy_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_ScanEntry), MP_ROM_PTR(&ubluepy_scan_entry_type) },
#endif
{ MP_ROM_QSTR(MP_QSTR_DefaultDelegate), MP_ROM_PTR(&ubluepy_delegate_type) },
- { MP_ROM_QSTR(MP_QSTR_UUID), MP_ROM_PTR(&ubluepy_uuid_type) },
{ MP_ROM_QSTR(MP_QSTR_Service), MP_ROM_PTR(&ubluepy_service_type) },
{ MP_ROM_QSTR(MP_QSTR_Characteristic), MP_ROM_PTR(&ubluepy_characteristic_type) },
{ MP_ROM_QSTR(MP_QSTR_constants), MP_ROM_PTR(&ubluepy_constants_type) },
diff --git a/ports/nrf/modules/ubluepy/modubluepy.h b/ports/nrf/modules/ubluepy/modubluepy.h
index 83d86c5df..6c207b8c9 100644
--- a/ports/nrf/modules/ubluepy/modubluepy.h
+++ b/ports/nrf/modules/ubluepy/modubluepy.h
@@ -71,9 +71,9 @@ p.advertise(device_name="micr", services=[s])
*/
+#include "common-hal/bleio/UUID.h"
#include "py/obj.h"
-extern const mp_obj_type_t ubluepy_uuid_type;
extern const mp_obj_type_t ubluepy_service_type;
extern const mp_obj_type_t ubluepy_characteristic_type;
extern const mp_obj_type_t ubluepy_peripheral_type;
@@ -83,11 +83,6 @@ extern const mp_obj_type_t ubluepy_constants_type;
extern const mp_obj_type_t ubluepy_constants_ad_types_type;
typedef enum {
- UBLUEPY_UUID_16_BIT = 1,
- UBLUEPY_UUID_128_BIT
-} ubluepy_uuid_type_t;
-
-typedef enum {
UBLUEPY_SERVICE_PRIMARY = 1,
UBLUEPY_SERVICE_SECONDARY = 2
} ubluepy_service_type_t;
@@ -106,13 +101,6 @@ typedef enum {
UBLUEPY_ROLE_CENTRAL
} ubluepy_role_type_t;
-typedef struct _ubluepy_uuid_obj_t {
- mp_obj_base_t base;
- ubluepy_uuid_type_t type;
- uint8_t value[2];
- uint8_t uuid_vs_idx;
-} ubluepy_uuid_obj_t;
-
typedef struct _ubluepy_peripheral_obj_t {
mp_obj_base_t base;
ubluepy_role_type_t role;
@@ -127,7 +115,7 @@ typedef struct _ubluepy_service_obj_t {
mp_obj_base_t base;
uint16_t handle;
uint8_t type;
- ubluepy_uuid_obj_t * p_uuid;
+ bleio_uuid_obj_t * p_uuid;
ubluepy_peripheral_obj_t * p_periph;
mp_obj_t char_list;
uint16_t start_handle;
@@ -137,7 +125,7 @@ typedef struct _ubluepy_service_obj_t {
typedef struct _ubluepy_characteristic_obj_t {
mp_obj_base_t base;
uint16_t handle;
- ubluepy_uuid_obj_t * p_uuid;
+ bleio_uuid_obj_t * p_uuid;
uint16_t service_handle;
uint16_t user_desc_handle;
uint16_t cccd_handle;
@@ -151,7 +139,7 @@ typedef struct _ubluepy_characteristic_obj_t {
typedef struct _ubluepy_descriptor_obj_t {
mp_obj_base_t base;
uint16_t handle;
- ubluepy_uuid_obj_t * p_uuid;
+ bleio_uuid_obj_t * p_uuid;
} ubluepy_descriptor_obj_t;
typedef struct _ubluepy_delegate_obj_t {
diff --git a/ports/nrf/modules/ubluepy/ubluepy_characteristic.c b/ports/nrf/modules/ubluepy/ubluepy_characteristic.c
index 0935615a5..e0259fb06 100644
--- a/ports/nrf/modules/ubluepy/ubluepy_characteristic.c
+++ b/ports/nrf/modules/ubluepy/ubluepy_characteristic.c
@@ -32,6 +32,7 @@
#include "modubluepy.h"
#include "ble_drv.h"
+#include "shared-bindings/bleio/UUID.h"
STATIC void ubluepy_characteristic_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
ubluepy_characteristic_obj_t * self = (ubluepy_characteristic_obj_t *)o;
@@ -60,7 +61,7 @@ STATIC mp_obj_t ubluepy_characteristic_make_new(const mp_obj_type_t *type, size_
return MP_OBJ_FROM_PTR(s);
}
- if (MP_OBJ_IS_TYPE(uuid_obj, &ubluepy_uuid_type)) {
+ if (MP_OBJ_IS_TYPE(uuid_obj, &bleio_uuid_type)) {
s->p_uuid = MP_OBJ_TO_PTR(uuid_obj);
// (void)sd_characterstic_add(s);
} else {
diff --git a/ports/nrf/modules/ubluepy/ubluepy_peripheral.c b/ports/nrf/modules/ubluepy/ubluepy_peripheral.c
index 7b6b315a9..6fced1695 100644
--- a/ports/nrf/modules/ubluepy/ubluepy_peripheral.c
+++ b/ports/nrf/modules/ubluepy/ubluepy_peripheral.c
@@ -33,6 +33,8 @@
#if MICROPY_PY_UBLUEPY
#include "ble_drv.h"
+#include "common-hal/bleio/UUID.h"
+#include "shared-bindings/bleio/UUID.h"
STATIC void ubluepy_peripheral_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
ubluepy_peripheral_obj_t * self = (ubluepy_peripheral_obj_t *)o;
@@ -294,8 +296,8 @@ void static disc_add_service(mp_obj_t self, ble_drv_service_data_t * p_service_d
ubluepy_service_obj_t * p_service = m_new_obj(ubluepy_service_obj_t);
p_service->base.type = &ubluepy_service_type;
- ubluepy_uuid_obj_t * p_uuid = m_new_obj(ubluepy_uuid_obj_t);
- p_uuid->base.type = &ubluepy_uuid_type;
+ bleio_uuid_obj_t * p_uuid = m_new_obj(bleio_uuid_obj_t);
+ p_uuid->base.type = &bleio_uuid_type;
p_service->p_uuid = p_uuid;
@@ -317,8 +319,8 @@ void static disc_add_char(mp_obj_t service_in, ble_drv_char_data_t * p_desc_data
ubluepy_characteristic_obj_t * p_char = m_new_obj(ubluepy_characteristic_obj_t);
p_char->base.type = &ubluepy_characteristic_type;
- ubluepy_uuid_obj_t * p_uuid = m_new_obj(ubluepy_uuid_obj_t);
- p_uuid->base.type = &ubluepy_uuid_type;
+ bleio_uuid_obj_t * p_uuid = m_new_obj(bleio_uuid_obj_t);
+ p_uuid->base.type = &bleio_uuid_type;
p_char->p_uuid = p_uuid;
diff --git a/ports/nrf/modules/ubluepy/ubluepy_service.c b/ports/nrf/modules/ubluepy/ubluepy_service.c
index 59b81234f..69d98bb7e 100644
--- a/ports/nrf/modules/ubluepy/ubluepy_service.c
+++ b/ports/nrf/modules/ubluepy/ubluepy_service.c
@@ -33,6 +33,8 @@
#include "modubluepy.h"
#include "ble_drv.h"
+#include "common-hal/bleio/UUID.h"
+#include "shared-bindings/bleio/UUID.h"
STATIC void ubluepy_service_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
ubluepy_service_obj_t * self = (ubluepy_service_obj_t *)o;
@@ -62,7 +64,7 @@ STATIC mp_obj_t ubluepy_service_make_new(const mp_obj_type_t *type, size_t n_arg
return MP_OBJ_FROM_PTR(s);
}
- if (MP_OBJ_IS_TYPE(uuid_obj, &ubluepy_uuid_type)) {
+ if (MP_OBJ_IS_TYPE(uuid_obj, &bleio_uuid_type)) {
s->p_uuid = MP_OBJ_TO_PTR(uuid_obj);
uint8_t type = args[ARG_NEW_TYPE].u_int;
@@ -124,10 +126,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(ubluepy_service_get_chars_obj, service_get_char
///
STATIC mp_obj_t service_get_characteristic(mp_obj_t self_in, mp_obj_t uuid) {
ubluepy_service_obj_t * self = MP_OBJ_TO_PTR(self_in);
- ubluepy_uuid_obj_t * p_uuid = MP_OBJ_TO_PTR(uuid);
+ bleio_uuid_obj_t * p_uuid = MP_OBJ_TO_PTR(uuid);
// validate that there is an UUID object passed in as parameter
- if (!(MP_OBJ_IS_TYPE(uuid, &ubluepy_uuid_type))) {
+ if (!(MP_OBJ_IS_TYPE(uuid, &bleio_uuid_type))) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
translate("Invalid UUID parameter")));
}
diff --git a/ports/nrf/modules/ubluepy/ubluepy_uuid.c b/ports/nrf/modules/ubluepy/ubluepy_uuid.c
deleted file mode 100644
index b45494e41..000000000
--- a/ports/nrf/modules/ubluepy/ubluepy_uuid.c
+++ /dev/null
@@ -1,174 +0,0 @@
-/*
- * This file is part of the MicroPython project, http://micropython.org/
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2017 Glenn Ruben Bakke
- *
- * 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/obj.h"
-#include "py/runtime.h"
-#include "py/objstr.h"
-#include "py/misc.h"
-#include "supervisor/shared/translate.h"
-
-#if MICROPY_PY_UBLUEPY
-
-#include "modubluepy.h"
-#include "ble_drv.h"
-
-STATIC void ubluepy_uuid_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
- ubluepy_uuid_obj_t * self = (ubluepy_uuid_obj_t *)o;
- if (self->type == UBLUEPY_UUID_16_BIT) {
- mp_printf(print, "UUID(uuid: 0x" HEX2_FMT HEX2_FMT ")",
- self->value[1], self->value[0]);
- } else {
- mp_printf(print, "UUID(uuid: 0x" HEX2_FMT HEX2_FMT ", VS idx: " HEX2_FMT ")",
- self->value[1], self->value[0], self->uuid_vs_idx);
- }
-}
-
-STATIC mp_obj_t ubluepy_uuid_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
-
- enum { ARG_NEW_UUID };
-
- static const mp_arg_t allowed_args[] = {
- { ARG_NEW_UUID, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
- };
-
- // parse args
- mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
- mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
-
- ubluepy_uuid_obj_t *s = m_new_obj(ubluepy_uuid_obj_t);
- s->base.type = type;
-
- mp_obj_t uuid_obj = args[ARG_NEW_UUID].u_obj;
-
- if (uuid_obj == MP_OBJ_NULL) {
- return MP_OBJ_FROM_PTR(s);
- }
-
- if (MP_OBJ_IS_INT(uuid_obj)) {
- s->type = UBLUEPY_UUID_16_BIT;
- s->value[1] = (((uint16_t)mp_obj_get_int(uuid_obj)) >> 8) & 0xFF;
- s->value[0] = ((uint8_t)mp_obj_get_int(uuid_obj)) & 0xFF;
- } else if (MP_OBJ_IS_STR(uuid_obj)) {
- GET_STR_DATA_LEN(uuid_obj, str_data, str_len);
- if (str_len == 6) { // Assume hex digit prefixed with 0x
- s->type = UBLUEPY_UUID_16_BIT;
- s->value[0] = unichar_xdigit_value(str_data[5]);
- s->value[0] += unichar_xdigit_value(str_data[4]) << 4;
- s->value[1] = unichar_xdigit_value(str_data[3]);
- s->value[1] += unichar_xdigit_value(str_data[2]) << 4;
- } else if (str_len == 36) {
- s->type = UBLUEPY_UUID_128_BIT;
- uint8_t buffer[16];
- buffer[0] = unichar_xdigit_value(str_data[35]);
- buffer[0] += unichar_xdigit_value(str_data[34]) << 4;
- buffer[1] = unichar_xdigit_value(str_data[33]);
- buffer[1] += unichar_xdigit_value(str_data[32]) << 4;
- buffer[2] = unichar_xdigit_value(str_data[31]);
- buffer[2] += unichar_xdigit_value(str_data[30]) << 4;
- buffer[3] = unichar_xdigit_value(str_data[29]);
- buffer[3] += unichar_xdigit_value(str_data[28]) << 4;
- buffer[4] = unichar_xdigit_value(str_data[27]);
- buffer[4] += unichar_xdigit_value(str_data[26]) << 4;
- buffer[5] = unichar_xdigit_value(str_data[25]);
- buffer[5] += unichar_xdigit_value(str_data[24]) << 4;
- // 23 '-'
- buffer[6] = unichar_xdigit_value(str_data[22]);
- buffer[6] += unichar_xdigit_value(str_data[21]) << 4;
- buffer[7] = unichar_xdigit_value(str_data[20]);
- buffer[7] += unichar_xdigit_value(str_data[19]) << 4;
- // 18 '-'
- buffer[8] = unichar_xdigit_value(str_data[17]);
- buffer[8] += unichar_xdigit_value(str_data[16]) << 4;
- buffer[9] = unichar_xdigit_value(str_data[15]);
- buffer[9] += unichar_xdigit_value(str_data[14]) << 4;
- // 13 '-'
- buffer[10] = unichar_xdigit_value(str_data[12]);
- buffer[10] += unichar_xdigit_value(str_data[11]) << 4;
- buffer[11] = unichar_xdigit_value(str_data[10]);
- buffer[11] += unichar_xdigit_value(str_data[9]) << 4;
- // 8 '-'
- // 16-bit field
- s->value[0] = unichar_xdigit_value(str_data[7]);
- s->value[0] += unichar_xdigit_value(str_data[6]) << 4;
- s->value[1] = unichar_xdigit_value(str_data[5]);
- s->value[1] += unichar_xdigit_value(str_data[4]) << 4;
-
- buffer[14] = unichar_xdigit_value(str_data[3]);
- buffer[14] += unichar_xdigit_value(str_data[2]) << 4;
- buffer[15] = unichar_xdigit_value(str_data[1]);
- buffer[15] += unichar_xdigit_value(str_data[0]) << 4;
-
- ble_drv_uuid_add_vs(buffer, &s->uuid_vs_idx);
- } else {
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
- translate("Invalid UUID string length")));
- }
- } else if (MP_OBJ_IS_TYPE(uuid_obj, &ubluepy_uuid_type)) {
- // deep copy instance
- ubluepy_uuid_obj_t * p_old = MP_OBJ_TO_PTR(uuid_obj);
- s->type = p_old->type;
- s->value[0] = p_old->value[0];
- s->value[1] = p_old->value[1];
- } else {
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
- translate("Invalid UUID parameter")));
- }
-
- return MP_OBJ_FROM_PTR(s);
-}
-
-/// \method binVal()
-/// Get binary value of the 16 or 128 bit UUID. Returned as bytearray type.
-///
-STATIC mp_obj_t uuid_bin_val(mp_obj_t self_in) {
- ubluepy_uuid_obj_t * self = MP_OBJ_TO_PTR(self_in);
-
- // TODO: Extend the uint16 byte value to 16 byte if 128-bit,
- // also encapsulate it in a bytearray. For now, return
- // the uint16_t field of the UUID.
- return MP_OBJ_NEW_SMALL_INT(self->value[0] | self->value[1] << 8);
-}
-STATIC MP_DEFINE_CONST_FUN_OBJ_1(ubluepy_uuid_bin_val_obj, uuid_bin_val);
-
-STATIC const mp_rom_map_elem_t ubluepy_uuid_locals_dict_table[] = {
-#if 0
- { MP_ROM_QSTR(MP_QSTR_getCommonName), MP_ROM_PTR(&ubluepy_uuid_get_common_name_obj) },
-#endif
- // Properties
- { MP_ROM_QSTR(MP_QSTR_binVal), MP_ROM_PTR(&ubluepy_uuid_bin_val_obj) },
-};
-
-STATIC MP_DEFINE_CONST_DICT(ubluepy_uuid_locals_dict, ubluepy_uuid_locals_dict_table);
-
-const mp_obj_type_t ubluepy_uuid_type = {
- { &mp_type_type },
- .name = MP_QSTR_UUID,
- .print = ubluepy_uuid_print,
- .make_new = ubluepy_uuid_make_new,
- .locals_dict = (mp_obj_dict_t*)&ubluepy_uuid_locals_dict
-};
-
-#endif // MICROPY_PY_UBLUEPY