Skip to content

Commit 364ee62

Browse files
committed
Address review comments.
1 parent cb99546 commit 364ee62

12 files changed

Lines changed: 66 additions & 131 deletions

File tree

conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
# List of patterns, relative to source directory, that match files and
8585
# directories to ignore when looking for source files.
8686
exclude_patterns = ["**/build*",
87+
".git",
8788
".venv",
8889
".direnv",
8990
"docs/README.md",

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,9 @@ STATIC void scanner_on_ble_evt(ble_evt_t *ble_evt, void *scanner_in) {
7474
}
7575

7676
void common_hal_bleio_scanner_construct(bleio_scanner_obj_t *self) {
77-
self->scan_entries = mp_obj_new_list(0, NULL);
7877
}
7978

80-
void common_hal_bleio_scanner_scan(bleio_scanner_obj_t *self, mp_float_t timeout, mp_float_t interval, mp_float_t window) {
79+
mp_obj_t common_hal_bleio_scanner_scan(bleio_scanner_obj_t *self, mp_float_t timeout, mp_float_t interval, mp_float_t window) {
8180
common_hal_bleio_adapter_set_enabled(true);
8281
ble_drv_add_event_handler(scanner_on_ble_evt, self);
8382

@@ -87,8 +86,7 @@ void common_hal_bleio_scanner_scan(bleio_scanner_obj_t *self, mp_float_t timeout
8786
.scan_phys = BLE_GAP_PHY_1MBPS,
8887
};
8988

90-
// Empty the advertising reports list.
91-
mp_obj_list_clear(self->scan_entries);
89+
self->scan_entries = mp_obj_new_list(0, NULL);
9290

9391
uint32_t err_code;
9492
err_code = sd_ble_gap_scan_start(&scan_params, &m_scan_buffer);
@@ -99,8 +97,9 @@ void common_hal_bleio_scanner_scan(bleio_scanner_obj_t *self, mp_float_t timeout
9997

10098
mp_hal_delay_ms(timeout * 1000);
10199
sd_ble_gap_scan_stop();
102-
}
103100

104-
mp_obj_t common_hal_bleio_scanner_get_scan_entries(bleio_scanner_obj_t *self) {
105-
return self->scan_entries;
101+
// Return list, and don't hang on to it, so it can be GC'd.
102+
mp_obj_t entries = self->scan_entries;
103+
self->scan_entries = MP_OBJ_NULL;
104+
return entries;
106105
}

shared-bindings/bleio/Address.c

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,9 @@
4848
//| The value itself can be one of:
4949
//|
5050
//| :param buf address: The address value to encapsulate. A buffer object (bytearray, bytes) of 6 bytes.
51-
//| :param int address_type: one of these integers:
52-
//| - ``bleio.Address.PUBLIC`` = 0
53-
//| - ``bleio.Address.RANDOM_STATIC`` = 1
54-
//| - ``bleio.Address.RANDOM_PRIVATE_RESOLVABLE`` = 2
55-
//| - ``bleio.Address.RANDOM_PRIVATE_NON_RESOLVABLE`` = 3
51+
//| :param int address_type: one of the integer values: `PUBLIC`, `RANDOM_STATIC`,
52+
//| `RANDOM_PRIVATE_RESOLVABLE`, or `RANDOM_PRIVATE_NON_RESOLVABLE`.
5653
//|
57-
5854
STATIC mp_obj_t bleio_address_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
5955
enum { ARG_address, ARG_address_type };
6056
static const mp_arg_t allowed_args[] = {
@@ -105,12 +101,9 @@ const mp_obj_property_t bleio_address_address_bytes_obj = {
105101

106102
//| .. attribute:: type
107103
//|
108-
//| The address type (read-only). One of these integers:
109-
//|
110-
//| - ``bleio.Address.PUBLIC`` = 0
111-
//| - ``bleio.Address.RANDOM_STATIC`` = 1
112-
//| - ``bleio.Address.RANDOM_PRIVATE_RESOLVABLE`` = 2
113-
//| - ``bleio.Address.RANDOM_PRIVATE_NON_RESOLVABLE`` = 3
104+
//| The address type (read-only).
105+
//| One of the integer values: `PUBLIC`, `RANDOM_STATIC`,
106+
//| `RANDOM_PRIVATE_RESOLVABLE`, or `RANDOM_PRIVATE_NON_RESOLVABLE`.
114107
//|
115108
STATIC mp_obj_t bleio_address_get_type(mp_obj_t self_in) {
116109
bleio_address_obj_t *self = MP_OBJ_TO_PTR(self_in);
@@ -154,16 +147,37 @@ STATIC mp_obj_t bleio_address_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_o
154147

155148
STATIC void bleio_address_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
156149
bleio_address_obj_t *self = MP_OBJ_TO_PTR(self_in);
157-
mp_obj_t address_bytes = common_hal_bleio_address_get_address_bytes(self);
158-
159-
mp_buffer_info_t buf_info;
160-
mp_get_buffer_raise(address_bytes, &buf_info, MP_BUFFER_READ);
161-
const uint8_t *buf = (uint8_t *) buf_info.buf;
162-
mp_printf(print,
163-
"%02x:%02x:%02x:%02x:%02x:%02x",
164-
buf[5], buf[4], buf[3], buf[2], buf[1], buf[0]);
150+
if (kind == PRINT_STR) {
151+
mp_buffer_info_t buf_info;
152+
mp_obj_t address_bytes = common_hal_bleio_address_get_address_bytes(self);
153+
mp_get_buffer_raise(address_bytes, &buf_info, MP_BUFFER_READ);
154+
155+
const uint8_t *buf = (uint8_t *) buf_info.buf;
156+
mp_printf(print,
157+
"%02x:%02x:%02x:%02x:%02x:%02x",
158+
buf[5], buf[4], buf[3], buf[2], buf[1], buf[0]);
159+
} else {
160+
mp_printf(print, "<Address>");
161+
}
165162
}
166163

164+
//| .. data:: PUBLIC
165+
//|
166+
//| A publicly known address, with a company ID (high 24 bits)and company-assigned part (low 24 bits).
167+
//|
168+
//| .. data:: RANDOM_STATIC
169+
//|
170+
//| A randomly generated address that does not change often. It may never change or may change after
171+
//| a power cycle.
172+
//|
173+
//| .. data:: RANDOM_PRIVATE_RESOLVABLE
174+
//|
175+
//| An address that is usable when the peer knows the other device's secret Identity Resolving Key (IRK).
176+
//|
177+
//| .. data:: RANDOM_PRIVATE_NON_RESOLVABLE
178+
//|
179+
//| A randomly generated address that changes on every connection.
180+
//|
167181
STATIC const mp_rom_map_elem_t bleio_address_locals_dict_table[] = {
168182
{ MP_ROM_QSTR(MP_QSTR_address_bytes), MP_ROM_PTR(&bleio_address_address_bytes_obj) },
169183
{ MP_ROM_QSTR(MP_QSTR_type), MP_ROM_PTR(&bleio_address_type_obj) },

shared-bindings/bleio/Central.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
//|
5757
//| my_entry = None
5858
//| for entry in entries:
59-
//| if entry.name is not None and entry.name == 'MyCentral':
59+
//| if entry.name is not None and entry.name == 'MyPeripheral':
6060
//| my_entry = entry
6161
//| break
6262
//|
@@ -86,13 +86,13 @@ STATIC mp_obj_t bleio_central_make_new(const mp_obj_type_t *type, size_t n_args,
8686
//|
8787
//| :param bleio.Address address: The address of the peripheral to connect to
8888
//| :param float/int timeout: Try to connect for timeout seconds.
89-
//| :param iterable service_uuids: a collection of :py:class:~`UUID` objects for the services
89+
//| :param iterable service_uuids_whitelist: an iterable of :py:class:~`UUID` objects for the services
9090
//| provided by the peripheral that you want to use.
9191
//| The peripheral may provide more services, but services not listed are ignored.
9292
//| If a service in service_uuids is not found during discovery, it will not
9393
//| appear in `remote_services`.
9494
//|
95-
//| If service_uuids is None, then all services will undergo discovery, which can be slow.
95+
//| If service_uuids_whitelist is None, then all services will undergo discovery, which can be slow.
9696
//|
9797
//| If the service UUID is 128-bit, or its characteristic UUID's are 128-bit, you
9898
//| you must have already created a :py:class:~`UUID` object for that UUID in order for the
@@ -101,11 +101,11 @@ STATIC mp_obj_t bleio_central_make_new(const mp_obj_type_t *type, size_t n_args,
101101
STATIC mp_obj_t bleio_central_connect(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
102102
bleio_central_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
103103

104-
enum { ARG_address, ARG_timeout, ARG_service_uuids };
104+
enum { ARG_address, ARG_timeout, ARG_service_uuids_whitelist };
105105
static const mp_arg_t allowed_args[] = {
106106
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_OBJ },
107107
{ MP_QSTR_timeout, MP_ARG_REQUIRED | MP_ARG_OBJ },
108-
{ MP_QSTR_service_uuids, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
108+
{ MP_QSTR_service_uuids_whitelist, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
109109
};
110110

111111
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
@@ -119,7 +119,7 @@ STATIC mp_obj_t bleio_central_connect(mp_uint_t n_args, const mp_obj_t *pos_args
119119
mp_float_t timeout = mp_obj_get_float(args[ARG_timeout].u_obj);
120120

121121
// common_hal_bleio_central_connect() will validate that services is an iterable or None.
122-
common_hal_bleio_central_connect(self, address, timeout, args[ARG_service_uuids].u_obj);
122+
common_hal_bleio_central_connect(self, address, timeout, args[ARG_service_uuids_whitelist].u_obj);
123123

124124
return mp_const_none;
125125
}
@@ -160,12 +160,15 @@ const mp_obj_property_t bleio_central_connected_obj = {
160160

161161
//| .. attribute:: remote_services (read-only)
162162
//|
163-
//| Empty until connected, then a list of services provided by the remote peripheral.
163+
//| A tuple of services provided by the remote peripheral.
164+
//| If the Central is not connected, an empty tuple will be returned.
164165
//|
165166
STATIC mp_obj_t bleio_central_get_remote_services(mp_obj_t self_in) {
166167
bleio_central_obj_t *self = MP_OBJ_TO_PTR(self_in);
167168

168-
return MP_OBJ_FROM_PTR(common_hal_bleio_central_get_remote_services(self));
169+
// Return list as a tuple so user won't be able to change it.
170+
mp_obj_list_t *service_list = common_hal_bleio_central_get_remote_services(self);
171+
return mp_obj_new_tuple(service_list->len, service_list->items);
169172
}
170173
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_central_get_remote_services_obj, bleio_central_get_remote_services);
171174

shared-bindings/bleio/Characteristic.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,13 @@ STATIC MP_DEFINE_CONST_DICT(bleio_characteristic_locals_dict, bleio_characterist
316316

317317
STATIC void bleio_characteristic_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
318318
bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
319-
mp_printf(print, "Characteristic(");
320319
if (self->uuid) {
320+
mp_printf(print, "Characteristic(");
321321
bleio_uuid_print(print, MP_OBJ_FROM_PTR(self->uuid), kind);
322+
mp_printf(print, ")");
322323
} else {
323-
mp_printf(print, "Unregistered uUID");
324+
mp_printf(print, "<Characteristic with Unregistered UUID>");
324325
}
325-
mp_printf(print, ")");
326326
}
327327

328328
const mp_obj_type_t bleio_characteristic_type = {

shared-bindings/bleio/Descriptor.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,13 @@ STATIC MP_DEFINE_CONST_DICT(bleio_descriptor_locals_dict, bleio_descriptor_local
162162

163163
STATIC void bleio_descriptor_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
164164
bleio_descriptor_obj_t *self = MP_OBJ_TO_PTR(self_in);
165-
mp_printf(print, "Descriptor(");
166165
if (self->uuid) {
166+
mp_printf(print, "Descriptor(");
167167
bleio_uuid_print(print, MP_OBJ_FROM_PTR(self->uuid), kind);
168+
mp_printf(print, ")");
168169
} else {
169-
mp_printf(print, "Unregistered uUID");
170+
mp_printf(print, "<Descriptor with Unregistered UUID>");
170171
}
171-
mp_printf(print, ")");
172172
}
173173

174174
const mp_obj_type_t bleio_descriptor_type = {

shared-bindings/bleio/Peripheral.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ static const char default_name[] = "CIRCUITPY";
6363
//| Usage::
6464
//|
6565
//| import bleio
66+
//| from adafruit_ble.advertising import ServerAdvertisement
6667
//|
6768
//| # Create a Characteristic.
6869
//| chara = bleio.Characteristic(bleio.UUID(0x2919), read=True, notify=True)

shared-bindings/bleio/ScanEntry.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include "shared-bindings/bleio/Address.h"
3333
#include "shared-bindings/bleio/ScanEntry.h"
3434
#include "shared-bindings/bleio/UUID.h"
35-
#include "shared-module/bleio/AdvertisementData.h"
3635
#include "shared-module/bleio/ScanEntry.h"
3736

3837
//| .. currentmodule:: bleio

shared-bindings/bleio/Scanner.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ STATIC mp_obj_t bleio_scanner_make_new(const mp_obj_type_t *type, size_t n_args,
7575
//| Must be in the range 0.0025 - 40.959375 seconds.
7676
//| :param float window: the duration (in seconds) to scan a single BLE channel.
7777
//| window must be <= interval.
78-
//| :returns: advertising packets found
79-
//| :rtype: list of :py:class:`bleio.ScanEntry`
78+
//| :returns: an iterable of `bleio.ScanEntry` objects
79+
//| :rtype: iterable
8080
//|
8181
STATIC mp_obj_t bleio_scanner_scan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
8282
enum { ARG_timeout, ARG_interval, ARG_window };
@@ -110,9 +110,7 @@ STATIC mp_obj_t bleio_scanner_scan(size_t n_args, const mp_obj_t *pos_args, mp_m
110110
mp_raise_ValueError(translate("window must be <= interval"));
111111
}
112112

113-
common_hal_bleio_scanner_scan(self, timeout, interval, window);
114-
115-
return common_hal_bleio_scanner_get_scan_entries(self);
113+
return common_hal_bleio_scanner_scan(self, timeout, interval, window);
116114
}
117115
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_scanner_scan_obj, 2, bleio_scanner_scan);
118116

shared-bindings/bleio/Scanner.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434
extern const mp_obj_type_t bleio_scanner_type;
3535

3636
extern void common_hal_bleio_scanner_construct(bleio_scanner_obj_t *self);
37-
extern void common_hal_bleio_scanner_scan(bleio_scanner_obj_t *self, mp_float_t timeout, mp_float_t interval, mp_float_t window);
37+
extern mp_obj_t common_hal_bleio_scanner_scan(bleio_scanner_obj_t *self, mp_float_t timeout, mp_float_t interval, mp_float_t window);
3838
extern void common_hal_bleio_scanner_stop(bleio_scanner_obj_t *self);
39-
extern mp_obj_t common_hal_bleio_scanner_get_scan_entries(bleio_scanner_obj_t *self);
4039

4140
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_SCANNER_H

0 commit comments

Comments
 (0)