Skip to content

Commit f4940c9

Browse files
committed
nrf: Move the UUID class from ubluepy to the shared bleio module
Also added a UUIDType enum-like class for determining UUID type.
1 parent b436666 commit f4940c9

File tree

16 files changed

+581
-215
lines changed

16 files changed

+581
-215
lines changed

ports/nrf/Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ DRIVERS_SRC_C += $(addprefix modules/,\
132132
ubluepy/ubluepy_peripheral.c \
133133
ubluepy/ubluepy_service.c \
134134
ubluepy/ubluepy_characteristic.c \
135-
ubluepy/ubluepy_uuid.c \
136135
ubluepy/ubluepy_delegate.c \
137136
ubluepy/ubluepy_constants.c \
138137
ubluepy/ubluepy_descriptor.c \
@@ -168,7 +167,8 @@ SRC_COMMON_HAL += \
168167
ifneq ($(SD), )
169168
SRC_COMMON_HAL += \
170169
bleio/__init__.c \
171-
bleio/Adapter.c
170+
bleio/Adapter.c \
171+
bleio/UUID.c
172172
endif
173173

174174
# These don't have corresponding files in each port but are still located in
@@ -183,6 +183,11 @@ SRC_BINDINGS_ENUMS = \
183183
math/__init__.c \
184184
util.c
185185

186+
ifneq ($(SD), )
187+
SRC_BINDINGS_ENUMS += \
188+
bleio/UUIDType.c
189+
endif
190+
186191
SRC_COMMON_HAL_EXPANDED = $(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \
187192
$(addprefix shared-bindings/, $(SRC_BINDINGS_ENUMS)) \
188193
$(addprefix common-hal/, $(SRC_COMMON_HAL))

ports/nrf/common-hal/bleio/UUID.c

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Glenn Ruben Bakke
7+
* Copyright (c) 2018 Artur Pacholec
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include "ble_drv.h"
29+
#include "common-hal/bleio/UUID.h"
30+
#include "py/objstr.h"
31+
#include "py/runtime.h"
32+
#include "shared-bindings/bleio/UUID.h"
33+
34+
#define UUID_STR_16BIT_LEN 6
35+
#define UUID_STR_128BIT_LEN 36
36+
37+
static uint8_t xdigit_8b_value(byte nibble1, byte nibble2) {
38+
return unichar_xdigit_value(nibble1) |
39+
(unichar_xdigit_value(nibble2) << 4);
40+
}
41+
42+
void common_hal_bleio_uuid_construct(bleio_uuid_obj_t *self, const mp_obj_t *uuid) {
43+
if (MP_OBJ_IS_INT(*uuid)) {
44+
self->type = UUID_TYPE_16BIT;
45+
46+
self->value[1] = (mp_obj_get_int(*uuid) >> 8) & 0xFF;
47+
self->value[0] = (mp_obj_get_int(*uuid) >> 0) & 0xFF;
48+
return;
49+
}
50+
51+
if (MP_OBJ_IS_STR(*uuid)) {
52+
GET_STR_DATA_LEN(*uuid, str_data, str_len);
53+
54+
if (str_len == UUID_STR_16BIT_LEN) {
55+
self->type = UUID_TYPE_16BIT;
56+
57+
self->value[0] = xdigit_8b_value(str_data[5], str_data[4]);
58+
self->value[1] = xdigit_8b_value(str_data[3], str_data[2]);
59+
} else if (str_len == UUID_STR_128BIT_LEN) {
60+
self->type = UUID_TYPE_128BIT;
61+
62+
uint8_t buffer[16];
63+
buffer[0] = xdigit_8b_value(str_data[35], str_data[34]);
64+
buffer[1] = xdigit_8b_value(str_data[33], str_data[32]);
65+
buffer[2] = xdigit_8b_value(str_data[31], str_data[30]);
66+
buffer[3] = xdigit_8b_value(str_data[29], str_data[28]);
67+
buffer[4] = xdigit_8b_value(str_data[27], str_data[26]);
68+
buffer[5] = xdigit_8b_value(str_data[25], str_data[24]);
69+
70+
// 23 '-'
71+
buffer[6] = xdigit_8b_value(str_data[22], str_data[21]);
72+
buffer[7] = xdigit_8b_value(str_data[20], str_data[19]);
73+
74+
// 18 '-'
75+
buffer[8] = xdigit_8b_value(str_data[17], str_data[16]);
76+
buffer[9] = xdigit_8b_value(str_data[15], str_data[14]);
77+
78+
// 13 '-'
79+
buffer[10] = xdigit_8b_value(str_data[12], str_data[11]);
80+
buffer[11] = xdigit_8b_value(str_data[10], str_data[9]);
81+
82+
// 8 '-'
83+
self->value[0] = xdigit_8b_value(str_data[7], str_data[6]);
84+
self->value[1] = xdigit_8b_value(str_data[5], str_data[4]);
85+
86+
buffer[14] = xdigit_8b_value(str_data[3], str_data[2]);
87+
buffer[15] = xdigit_8b_value(str_data[1], str_data[0]);
88+
89+
ble_drv_uuid_add_vs(buffer, &self->uuid_vs_idx);
90+
} else {
91+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
92+
"Invalid UUID string length"));
93+
}
94+
95+
return;
96+
}
97+
98+
// deep copy
99+
if (MP_OBJ_IS_TYPE(*uuid, &bleio_uuid_type)) {
100+
bleio_uuid_obj_t *other = MP_OBJ_TO_PTR(*uuid);
101+
self->type = other->type;
102+
self->uuid_vs_idx = other->uuid_vs_idx;
103+
self->value[0] = other->value[0];
104+
self->value[1] = other->value[1];
105+
106+
return;
107+
}
108+
109+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
110+
"Invalid UUID parameter"));
111+
}
112+
113+
void common_hal_bleio_uuid_print(bleio_uuid_obj_t *self, const mp_print_t *print) {
114+
if (self->type == UUID_TYPE_16BIT) {
115+
mp_printf(print, "UUID(uuid: 0x" HEX2_FMT HEX2_FMT ")",
116+
self->value[1], self->value[0]);
117+
} else {
118+
mp_printf(print, "UUID(uuid: 0x" HEX2_FMT HEX2_FMT ", VS idx: " HEX2_FMT ")",
119+
self->value[1], self->value[0], self->uuid_vs_idx);
120+
}
121+
}
122+
123+
bleio_uuid_type_t common_hal_bleio_uuid_get_type(bleio_uuid_obj_t *self) {
124+
return self->type;
125+
}

ports/nrf/common-hal/bleio/UUID.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Glenn Ruben Bakke
7+
* Copyright (c) 2018 Artur Pacholec
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_UUID_H
29+
#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_UUID_H
30+
31+
#include "py/obj.h"
32+
#include "shared-bindings/bleio/UUIDType.h"
33+
34+
typedef struct {
35+
mp_obj_base_t base;
36+
bleio_uuid_type_t type;
37+
uint8_t uuid_vs_idx;
38+
uint8_t value[2];
39+
} bleio_uuid_obj_t;
40+
41+
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_UUID_H

ports/nrf/drivers/bluetooth/ble_drv.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,11 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) {
430430

431431
for (uint8_t i = 0; i < p_adv_params->num_of_services; i++) {
432432
ubluepy_service_obj_t * p_service = (ubluepy_service_obj_t *)p_adv_params->p_services[i];
433-
if (p_service->p_uuid->type == UBLUEPY_UUID_16_BIT) {
433+
if (p_service->p_uuid->type == UUID_TYPE_16BIT) {
434434
type_16bit_present = true;
435435
}
436436

437-
if (p_service->p_uuid->type == UBLUEPY_UUID_128_BIT) {
437+
if (p_service->p_uuid->type == UUID_TYPE_128BIT) {
438438
type_128bit_present = true;
439439
}
440440
}

ports/nrf/drivers/bluetooth/ble_uart.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@
3434

3535
#if MICROPY_PY_BLE_NUS
3636

37-
static ubluepy_uuid_obj_t uuid_obj_service = {
38-
.base.type = &ubluepy_uuid_type,
39-
.type = UBLUEPY_UUID_128_BIT,
37+
static bleio_uuid_obj_t uuid_obj_service = {
38+
.base.type = &bleio_uuid_type,
39+
.type = UUID_128_BIT,
4040
.value = {0x01, 0x00}
4141
};
4242

43-
static ubluepy_uuid_obj_t uuid_obj_char_tx = {
44-
.base.type = &ubluepy_uuid_type,
45-
.type = UBLUEPY_UUID_128_BIT,
43+
static bleio_uuid_obj_t uuid_obj_char_tx = {
44+
.base.type = &bleio_uuid_type,
45+
.type = UUID_128_BIT,
4646
.value = {0x03, 0x00}
4747
};
4848

49-
static ubluepy_uuid_obj_t uuid_obj_char_rx = {
50-
.base.type = &ubluepy_uuid_type,
51-
.type = UBLUEPY_UUID_128_BIT,
49+
static bleio_uuid_obj_t uuid_obj_char_rx = {
50+
.base.type = &bleio_uuid_type,
51+
.type = UUID_128_BIT,
5252
.value = {0x02, 0x00}
5353
};
5454

ports/nrf/modules/ubluepy/modubluepy.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
extern const mp_obj_type_t ubluepy_peripheral_type;
3232
extern const mp_obj_type_t ubluepy_service_type;
33-
extern const mp_obj_type_t ubluepy_uuid_type;
3433
extern const mp_obj_type_t ubluepy_characteristic_type;
3534
extern const mp_obj_type_t ubluepy_delegate_type;
3635
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[] = {
5049
{ MP_ROM_QSTR(MP_QSTR_ScanEntry), MP_ROM_PTR(&ubluepy_scan_entry_type) },
5150
#endif
5251
{ MP_ROM_QSTR(MP_QSTR_DefaultDelegate), MP_ROM_PTR(&ubluepy_delegate_type) },
53-
{ MP_ROM_QSTR(MP_QSTR_UUID), MP_ROM_PTR(&ubluepy_uuid_type) },
5452
{ MP_ROM_QSTR(MP_QSTR_Service), MP_ROM_PTR(&ubluepy_service_type) },
5553
{ MP_ROM_QSTR(MP_QSTR_Characteristic), MP_ROM_PTR(&ubluepy_characteristic_type) },
5654
{ MP_ROM_QSTR(MP_QSTR_constants), MP_ROM_PTR(&ubluepy_constants_type) },

ports/nrf/modules/ubluepy/modubluepy.h

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ p.advertise(device_name="micr", services=[s])
7171
7272
*/
7373

74+
#include "common-hal/bleio/UUID.h"
7475
#include "py/obj.h"
7576

76-
extern const mp_obj_type_t ubluepy_uuid_type;
7777
extern const mp_obj_type_t ubluepy_service_type;
7878
extern const mp_obj_type_t ubluepy_characteristic_type;
7979
extern const mp_obj_type_t ubluepy_peripheral_type;
@@ -82,11 +82,6 @@ extern const mp_obj_type_t ubluepy_scan_entry_type;
8282
extern const mp_obj_type_t ubluepy_constants_type;
8383
extern const mp_obj_type_t ubluepy_constants_ad_types_type;
8484

85-
typedef enum {
86-
UBLUEPY_UUID_16_BIT = 1,
87-
UBLUEPY_UUID_128_BIT
88-
} ubluepy_uuid_type_t;
89-
9085
typedef enum {
9186
UBLUEPY_SERVICE_PRIMARY = 1,
9287
UBLUEPY_SERVICE_SECONDARY = 2
@@ -106,13 +101,6 @@ typedef enum {
106101
UBLUEPY_ROLE_CENTRAL
107102
} ubluepy_role_type_t;
108103

109-
typedef struct _ubluepy_uuid_obj_t {
110-
mp_obj_base_t base;
111-
ubluepy_uuid_type_t type;
112-
uint8_t value[2];
113-
uint8_t uuid_vs_idx;
114-
} ubluepy_uuid_obj_t;
115-
116104
typedef struct _ubluepy_peripheral_obj_t {
117105
mp_obj_base_t base;
118106
ubluepy_role_type_t role;
@@ -127,7 +115,7 @@ typedef struct _ubluepy_service_obj_t {
127115
mp_obj_base_t base;
128116
uint16_t handle;
129117
uint8_t type;
130-
ubluepy_uuid_obj_t * p_uuid;
118+
bleio_uuid_obj_t * p_uuid;
131119
ubluepy_peripheral_obj_t * p_periph;
132120
mp_obj_t char_list;
133121
uint16_t start_handle;
@@ -137,7 +125,7 @@ typedef struct _ubluepy_service_obj_t {
137125
typedef struct _ubluepy_characteristic_obj_t {
138126
mp_obj_base_t base;
139127
uint16_t handle;
140-
ubluepy_uuid_obj_t * p_uuid;
128+
bleio_uuid_obj_t * p_uuid;
141129
uint16_t service_handle;
142130
uint16_t user_desc_handle;
143131
uint16_t cccd_handle;
@@ -151,7 +139,7 @@ typedef struct _ubluepy_characteristic_obj_t {
151139
typedef struct _ubluepy_descriptor_obj_t {
152140
mp_obj_base_t base;
153141
uint16_t handle;
154-
ubluepy_uuid_obj_t * p_uuid;
142+
bleio_uuid_obj_t * p_uuid;
155143
} ubluepy_descriptor_obj_t;
156144

157145
typedef struct _ubluepy_delegate_obj_t {

ports/nrf/modules/ubluepy/ubluepy_characteristic.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "modubluepy.h"
3434
#include "ble_drv.h"
35+
#include "shared-bindings/bleio/UUID.h"
3536

3637
STATIC void ubluepy_characteristic_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
3738
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_
6061
return MP_OBJ_FROM_PTR(s);
6162
}
6263

63-
if (MP_OBJ_IS_TYPE(uuid_obj, &ubluepy_uuid_type)) {
64+
if (MP_OBJ_IS_TYPE(uuid_obj, &bleio_uuid_type)) {
6465
s->p_uuid = MP_OBJ_TO_PTR(uuid_obj);
6566
// (void)sd_characterstic_add(s);
6667
} else {

ports/nrf/modules/ubluepy/ubluepy_peripheral.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#if MICROPY_PY_UBLUEPY
3434

3535
#include "ble_drv.h"
36+
#include "common-hal/bleio/UUID.h"
37+
#include "shared-bindings/bleio/UUID.h"
3638

3739
STATIC void ubluepy_peripheral_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
3840
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
294296
ubluepy_service_obj_t * p_service = m_new_obj(ubluepy_service_obj_t);
295297
p_service->base.type = &ubluepy_service_type;
296298

297-
ubluepy_uuid_obj_t * p_uuid = m_new_obj(ubluepy_uuid_obj_t);
298-
p_uuid->base.type = &ubluepy_uuid_type;
299+
bleio_uuid_obj_t * p_uuid = m_new_obj(bleio_uuid_obj_t);
300+
p_uuid->base.type = &bleio_uuid_type;
299301

300302
p_service->p_uuid = p_uuid;
301303

@@ -317,8 +319,8 @@ void static disc_add_char(mp_obj_t service_in, ble_drv_char_data_t * p_desc_data
317319
ubluepy_characteristic_obj_t * p_char = m_new_obj(ubluepy_characteristic_obj_t);
318320
p_char->base.type = &ubluepy_characteristic_type;
319321

320-
ubluepy_uuid_obj_t * p_uuid = m_new_obj(ubluepy_uuid_obj_t);
321-
p_uuid->base.type = &ubluepy_uuid_type;
322+
bleio_uuid_obj_t * p_uuid = m_new_obj(bleio_uuid_obj_t);
323+
p_uuid->base.type = &bleio_uuid_type;
322324

323325
p_char->p_uuid = p_uuid;
324326

0 commit comments

Comments
 (0)