Skip to content

Commit 31cc71f

Browse files
committed
WIP: bleio revisions
1 parent 0ea31ec commit 31cc71f

11 files changed

Lines changed: 149 additions & 284 deletions

File tree

ports/nrf/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,7 @@ SRC_BINDINGS_ENUMS += \
193193
bleio/Address.c \
194194
bleio/AddressType.c \
195195
bleio/AdvertisementData.c \
196-
bleio/ScanEntry.c \
197-
bleio/UUIDType.c
196+
bleio/ScanEntry.c
198197
endif
199198

200199
SRC_SHARED_MODULE = \

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void common_hal_bleio_service_add_characteristic(bleio_service_obj_t *self, blei
5353

5454
ble_uuid_t uuid = {
5555
.type = BLE_UUID_TYPE_BLE,
56-
.uuid = characteristic->uuid->value[0] | (characteristic->uuid->value[1] << 8),
56+
.uuid = characteristic->uuid->uuid16;
5757
};
5858

5959
if (characteristic->uuid->type == UUID_TYPE_128BIT)
@@ -79,8 +79,7 @@ void common_hal_bleio_service_add_characteristic(bleio_service_obj_t *self, blei
7979
uint32_t err_code;
8080
err_code = sd_ble_gatts_characteristic_add(self->handle, &char_md, &attr_char_value, &handles);
8181
if (err_code != NRF_SUCCESS) {
82-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
83-
translate("Failed to add characteristic, status: 0x%08lX"), err_code));
82+
mp_raise_OSError(translate("Could not add characteristic"));
8483
}
8584

8685
characteristic->user_desc_handle = handles.user_desc_handle;

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

Lines changed: 31 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -25,100 +25,47 @@
2525
* THE SOFTWARE.
2626
*/
2727

28-
#include "ble.h"
29-
#include "ble_drv.h"
30-
#include "common-hal/bleio/UUID.h"
31-
#include "nrf_error.h"
32-
#include "py/objstr.h"
28+
#include <string.h>
29+
3330
#include "py/runtime.h"
31+
#include "common-hal/bleio/UUID.h"
3432
#include "shared-bindings/bleio/Adapter.h"
35-
#include "shared-bindings/bleio/UUID.h"
36-
37-
#define UUID_STR_16BIT_LEN 6
38-
#define UUID_STR_128BIT_LEN 36
39-
40-
static uint8_t xdigit_8b_value(byte nibble1, byte nibble2) {
41-
return unichar_xdigit_value(nibble1) |
42-
(unichar_xdigit_value(nibble2) << 4);
43-
}
44-
45-
void common_hal_bleio_uuid_construct(bleio_uuid_obj_t *self, const mp_obj_t *uuid) {
46-
if (MP_OBJ_IS_INT(*uuid)) {
47-
self->type = UUID_TYPE_16BIT;
48-
49-
self->value[1] = (mp_obj_get_int(*uuid) >> 8) & 0xFF;
50-
self->value[0] = (mp_obj_get_int(*uuid) >> 0) & 0xFF;
51-
return;
52-
}
53-
54-
if (MP_OBJ_IS_STR(*uuid)) {
55-
GET_STR_DATA_LEN(*uuid, str_data, str_len);
56-
57-
if (str_len == UUID_STR_16BIT_LEN) {
58-
self->type = UUID_TYPE_16BIT;
59-
60-
self->value[0] = xdigit_8b_value(str_data[5], str_data[4]);
61-
self->value[1] = xdigit_8b_value(str_data[3], str_data[2]);
62-
} else if (str_len == UUID_STR_128BIT_LEN) {
63-
self->type = UUID_TYPE_128BIT;
6433

65-
ble_uuid128_t vs_uuid;
66-
vs_uuid.uuid128[0] = xdigit_8b_value(str_data[35], str_data[34]);
67-
vs_uuid.uuid128[1] = xdigit_8b_value(str_data[33], str_data[32]);
68-
vs_uuid.uuid128[2] = xdigit_8b_value(str_data[31], str_data[30]);
69-
vs_uuid.uuid128[3] = xdigit_8b_value(str_data[29], str_data[28]);
70-
vs_uuid.uuid128[4] = xdigit_8b_value(str_data[27], str_data[26]);
71-
vs_uuid.uuid128[5] = xdigit_8b_value(str_data[25], str_data[24]);
72-
73-
// 23 '-'
74-
vs_uuid.uuid128[6] = xdigit_8b_value(str_data[22], str_data[21]);
75-
vs_uuid.uuid128[7] = xdigit_8b_value(str_data[20], str_data[19]);
76-
77-
// 18 '-'
78-
vs_uuid.uuid128[8] = xdigit_8b_value(str_data[17], str_data[16]);
79-
vs_uuid.uuid128[9] = xdigit_8b_value(str_data[15], str_data[14]);
80-
81-
// 13 '-'
82-
vs_uuid.uuid128[10] = xdigit_8b_value(str_data[12], str_data[11]);
83-
vs_uuid.uuid128[11] = xdigit_8b_value(str_data[10], str_data[9]);
84-
85-
// 8 '-'
86-
self->value[0] = xdigit_8b_value(str_data[7], str_data[6]);
87-
self->value[1] = xdigit_8b_value(str_data[5], str_data[4]);
88-
89-
vs_uuid.uuid128[14] = xdigit_8b_value(str_data[3], str_data[2]);
90-
vs_uuid.uuid128[15] = xdigit_8b_value(str_data[1], str_data[0]);
91-
92-
common_hal_bleio_adapter_set_enabled(true);
93-
94-
const uint32_t err_code = sd_ble_uuid_vs_add(&vs_uuid, &self->uuid_vs_idx);
95-
if (err_code != NRF_SUCCESS) {
96-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
97-
translate("Failed to add Vendor Specific UUID, status: 0x%08lX"), err_code));
98-
}
99-
100-
} else {
101-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
102-
translate("Invalid UUID string length")));
103-
}
34+
#include "ble.h"
35+
#include "ble_drv.h"
36+
#include "nrf_error.h"
10437

105-
return;
38+
// If uuid128 is NULL, this is a Bluetooth SIG 16-bit UUID.
39+
// If uuid128 is not NULL, it's a 128-bit (16-byte) UUID, with bytes 12 and 13 zero'd out, where
40+
// the 16-bit part goes. Those 16 bits are passed in uuid16.
41+
void common_hal_bleio_uuid_construct(bleio_uuid_obj_t *self, uint32_t uuid16, uint8_t uuid128[]) {
42+
self->uuid16 = uuid16;
43+
self->uuid_vs_idx = 0;
44+
if (uuid128 != NULL) {
45+
ble_uuid128_t vs_uuid;
46+
memcpy(vs_uuid.uuid128, uuid128, sizeof(vs_uuid.uuid128));
47+
48+
// Register this vendor-specific UUID. Bytes 12 and 13 will be zero.
49+
common_hal_bleio_adapter_set_enabled(true);
50+
const uint32_t err_code = sd_ble_uuid_vs_add(&vs_uuid, &self->uuid_vs_idx);
51+
if (err_code != NRF_SUCCESS) {
52+
mp_raise_OSError(&mp_type_OSError, translate("Could not register Vendor-Specific UUID"));
10653
}
107-
108-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
109-
translate("Invalid UUID parameter")));
11054
}
11155

11256
void common_hal_bleio_uuid_print(bleio_uuid_obj_t *self, const mp_print_t *print) {
113-
if (self->type == UUID_TYPE_16BIT) {
114-
mp_printf(print, "UUID(uuid: 0x" HEX2_FMT HEX2_FMT ")",
115-
self->value[1], self->value[0]);
57+
if (self->uuid_vs_idx != 0) {
58+
mp_printf(print, "UUID(uuid16: 0x%04x, Vendor-Specific index: " HEX2_FMT ")",
59+
self->uuid16, self->uuid_vs_idx);
11660
} else {
117-
mp_printf(print, "UUID(uuid: 0x" HEX2_FMT HEX2_FMT ", VS idx: " HEX2_FMT ")",
118-
self->value[1], self->value[0], self->uuid_vs_idx);
61+
mp_printf(print, "UUID16(0x%04x)", self->uuid16);
11962
}
12063
}
12164

122-
bleio_uuid_type_t common_hal_bleio_uuid_get_type(bleio_uuid_obj_t *self) {
123-
return self->type;
65+
bool common_hal_bleio_uuid_get_vendor_specific(bleio_uuid_obj_t *self) {
66+
return self->uuid_vs_idx != 0;
67+
}
68+
69+
uint32_t common_hal_bleio_uuid_get_uuid16(bleio_uuid_obj_t *self) {
70+
return self->uuid16;
12471
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@
2828
#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_UUID_H
2929
#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_UUID_H
3030

31-
#include "shared-bindings/bleio/UUIDType.h"
31+
#include "py/obj.h"
3232

3333
typedef struct {
3434
mp_obj_base_t base;
35-
bleio_uuid_type_t type;
35+
// If non-zero, `uuid_vs_idx` is an index into the SoftDevice's table of registered vendor-specific UUID's.
36+
// If zero, `value` is a 16-bit Bluetooth SIG UUID, which would convert to this 128-bit UUID.
37+
// 0000xxxx-0000-1000-8000-00805F9B34FB
3638
uint8_t uuid_vs_idx;
37-
uint8_t value[2];
39+
// The 16-bit part of the UUID. This replaces bytes 12 and 13 in the registered 128-bit UUID.
40+
uint16_t uuid16;
3841
} bleio_uuid_obj_t;
3942

4043
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_UUID_H

shared-bindings/bleio/Characteristic.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,19 @@
8080

8181
//| .. attribute:: write
8282
//|
83-
//| A `bool` specifying if the characteristic allows writting to its value.
83+
//| A `bool` specifying if the characteristic allows writing to its value.
8484
//|
8585

8686
//| .. attribute:: write_no_resp
8787
//|
88-
//| A `bool` specifying if the characteristic allows writting to its value without response.
88+
//| A `bool` specifying if the characteristic allows writing to its value without response.
8989
//|
9090
STATIC void bleio_characteristic_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
9191
bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
9292

93-
mp_printf(print, "Characteristic(uuid: 0x"HEX2_FMT""HEX2_FMT" handle: 0x" HEX2_FMT ")",
94-
self->uuid->value[1], self->uuid->value[0], self->handle);
93+
mp_printf(print, "Characteristic(");
94+
common_hal_bleio_uuid_print(print, self->uuid);
95+
mp_printf(print, ")");
9596
}
9697

9798
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
121122
if (MP_OBJ_IS_TYPE(uuid, &bleio_uuid_type)) {
122123
self->uuid = MP_OBJ_TO_PTR(uuid);
123124
} else {
124-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
125-
translate("Invalid UUID parameter")));
125+
mp_raise_ValueError(translate("Expected a UUID"));
126126
}
127127

128128
common_hal_bleio_characteristic_construct(self);

shared-bindings/bleio/Service.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@
6666
STATIC void bleio_service_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
6767
bleio_service_obj_t *self = MP_OBJ_TO_PTR(self_in);
6868

69-
mp_printf(print, "Service(uuid: 0x"HEX2_FMT""HEX2_FMT")",
70-
self->uuid->value[1], self->uuid->value[0]);
69+
mp_printf(print, "Service(");
70+
common_hal_bleio_uuid_print(print, self->uuid);
71+
mp_print(print, ")");
7172
}
7273

7374
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,
101102
if (MP_OBJ_IS_TYPE(uuid, &bleio_uuid_type)) {
102103
self->uuid = MP_OBJ_TO_PTR(uuid);
103104
} else {
104-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
105-
translate("Invalid UUID parameter")));
105+
mp_raise_ValueError(translate("Expected a UUID or None"));
106106
}
107107

108108
return MP_OBJ_FROM_PTR(self);

0 commit comments

Comments
 (0)