Skip to content

Commit 140904e

Browse files
committed
getting Scanner to work
1 parent 4881e1f commit 140904e

13 files changed

Lines changed: 90 additions & 37 deletions

File tree

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,15 @@ STATIC void on_ble_evt(ble_evt_t *ble_evt, void *scanner_in) {
5757
entry->base.type = &bleio_scanentry_type;
5858
entry->rssi = report->rssi;
5959

60-
memcpy(entry->address.bytes, report->data.p_data, NUM_BLEIO_ADDRESS_BYTES);
61-
entry->address.type = report->peer_addr.addr_type;
60+
bleio_address_obj_t *address = m_new_obj(bleio_address_obj_t);
61+
address->base.type = &bleio_address_type;
62+
common_hal_bleio_address_construct(MP_OBJ_TO_PTR(address),
63+
report->peer_addr.addr, report->peer_addr.addr_type);
64+
entry->address = address;
6265

6366
entry->data = mp_obj_new_bytes(report->data.p_data, report->data.len);
6467

65-
mp_obj_list_append(scanner->adv_reports, entry);
68+
mp_obj_list_append(scanner->scan_entries, MP_OBJ_FROM_PTR(entry));
6669

6770
const uint32_t err_code = sd_ble_gap_scan_start(NULL, &m_scan_buffer);
6871
if (err_code != NRF_SUCCESS) {
@@ -71,7 +74,7 @@ STATIC void on_ble_evt(ble_evt_t *ble_evt, void *scanner_in) {
7174
}
7275

7376
void common_hal_bleio_scanner_construct(bleio_scanner_obj_t *self) {
74-
self->adv_reports = mp_obj_new_list(0, NULL);
77+
self->scan_entries = mp_obj_new_list(0, NULL);
7578
}
7679

7780
void common_hal_bleio_scanner_scan(bleio_scanner_obj_t *self, mp_float_t timeout, mp_float_t interval, mp_float_t window) {
@@ -85,7 +88,7 @@ void common_hal_bleio_scanner_scan(bleio_scanner_obj_t *self, mp_float_t timeout
8588
};
8689

8790
// Empty the advertising reports list.
88-
mp_obj_list_clear(self->adv_reports);
91+
mp_obj_list_clear(self->scan_entries);
8992

9093
uint32_t err_code;
9194
err_code = sd_ble_gap_scan_start(&scan_params, &m_scan_buffer);
@@ -98,6 +101,6 @@ void common_hal_bleio_scanner_scan(bleio_scanner_obj_t *self, mp_float_t timeout
98101
sd_ble_gap_scan_stop();
99102
}
100103

101-
mp_obj_t common_hal_bleio_scanner_get_adv_reports(bleio_scanner_obj_t *self) {
102-
return self->adv_reports;
104+
mp_obj_t common_hal_bleio_scanner_get_scan_entries(bleio_scanner_obj_t *self) {
105+
return self->scan_entries;
103106
}

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

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

3333
typedef struct {
3434
mp_obj_base_t base;
35-
mp_obj_t adv_reports; // List of reports.
35+
mp_obj_t scan_entries;
3636
uint16_t interval;
3737
uint16_t window;
3838
} bleio_scanner_obj_t;

shared-bindings/bleio/Address.c

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ STATIC mp_obj_t bleio_address_make_new(const mp_obj_type_t *type, size_t n_args,
8080
mp_raise_ValueError(translate("Address type out of range"));
8181
}
8282

83-
common_hal_bleio_address_construct(self, buf_info.buf, buf_info.len, address_type);
83+
common_hal_bleio_address_construct(self, buf_info.buf, address_type);
8484

8585
return MP_OBJ_FROM_PTR(self);
8686
}
@@ -101,6 +101,13 @@ STATIC mp_obj_t bleio_address_get_address_bytes(mp_obj_t self_in) {
101101
}
102102
MP_DEFINE_CONST_FUN_OBJ_1(bleio_address_get_address_bytes_obj, bleio_address_get_address_bytes);
103103

104+
const mp_obj_property_t bleio_address_address_bytes_obj = {
105+
.base.type = &mp_type_property,
106+
.proxy = {(mp_obj_t)&bleio_address_get_address_bytes_obj,
107+
(mp_obj_t)&mp_const_none_obj,
108+
(mp_obj_t)&mp_const_none_obj},
109+
};
110+
104111
//| .. attribute:: type
105112
//|
106113
//| The address type (read-only). One of these integers:
@@ -124,9 +131,47 @@ const mp_obj_property_t bleio_address_type_obj = {
124131
(mp_obj_t)&mp_const_none_obj},
125132
};
126133

134+
//| .. method:: __eq__(other)
135+
//|
136+
//| Two Address objects are equal if their addresses and address types are equal.
137+
//|
138+
STATIC mp_obj_t bleio_address_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
139+
switch (op) {
140+
// Two Addresses are equal if their address bytes and address_type are equal
141+
case MP_BINARY_OP_EQUAL:
142+
if (MP_OBJ_IS_TYPE(rhs_in, &bleio_address_type)) {
143+
bleio_address_obj_t *lhs = MP_OBJ_TO_PTR(lhs_in);
144+
bleio_address_obj_t *rhs = MP_OBJ_TO_PTR(rhs_in);
145+
return mp_obj_new_bool(
146+
mp_obj_equal(common_hal_bleio_address_get_address_bytes(lhs),
147+
common_hal_bleio_address_get_address_bytes(rhs)) &&
148+
common_hal_bleio_address_get_type(lhs) ==
149+
common_hal_bleio_address_get_type(rhs));
150+
151+
} else {
152+
return mp_const_false;
153+
}
154+
155+
default:
156+
return MP_OBJ_NULL; // op not supported
157+
}
158+
}
159+
160+
STATIC void bleio_address_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
161+
bleio_address_obj_t *self = MP_OBJ_TO_PTR(self_in);
162+
mp_obj_t address_bytes = common_hal_bleio_address_get_address_bytes(self);
163+
164+
mp_buffer_info_t buf_info;
165+
mp_get_buffer_raise(address_bytes, &buf_info, MP_BUFFER_READ);
166+
const uint8_t *buf = (uint8_t *) buf_info.buf;
167+
mp_printf(print,
168+
"%02x:%02x:%02x:%02x:%02x:%02x",
169+
buf[5], buf[4], buf[3], buf[2], buf[1], buf[0]);
170+
}
171+
127172
STATIC const mp_rom_map_elem_t bleio_address_locals_dict_table[] = {
128-
{ MP_ROM_QSTR(MP_QSTR_address_bytes), MP_ROM_PTR(&bleio_address_get_address_bytes_obj) },
129-
{ MP_ROM_QSTR(MP_QSTR_type), MP_ROM_PTR(&bleio_address_get_type_obj) },
173+
{ MP_ROM_QSTR(MP_QSTR_address_bytes), MP_ROM_PTR(&bleio_address_address_bytes_obj) },
174+
{ MP_ROM_QSTR(MP_QSTR_type), MP_ROM_PTR(&bleio_address_type_obj) },
130175
// These match the BLE_GAP_ADDR_TYPES values used by the nRF library.
131176
{ MP_ROM_QSTR(MP_QSTR_PUBLIC), MP_OBJ_NEW_SMALL_INT(0) },
132177
{ MP_ROM_QSTR(MP_QSTR_RANDOM_STATIC), MP_OBJ_NEW_SMALL_INT(1) },
@@ -141,5 +186,7 @@ const mp_obj_type_t bleio_address_type = {
141186
{ &mp_type_type },
142187
.name = MP_QSTR_Address,
143188
.make_new = bleio_address_make_new,
189+
.print = bleio_address_print,
190+
.binary_op = bleio_address_binary_op,
144191
.locals_dict = (mp_obj_dict_t*)&bleio_address_locals_dict
145192
};

shared-bindings/bleio/Address.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
extern const mp_obj_type_t bleio_address_type;
4343

44-
extern void common_hal_bleio_address_construct(bleio_address_obj_t *self, uint8_t *bytes, size_t bytes_length, uint8_t address_type);
44+
extern void common_hal_bleio_address_construct(bleio_address_obj_t *self, uint8_t *bytes, uint8_t address_type);
4545
extern mp_obj_t common_hal_bleio_address_get_address_bytes(bleio_address_obj_t *self);
4646
extern uint8_t common_hal_bleio_address_get_type(bleio_address_obj_t *self);
4747

shared-bindings/bleio/ScanEntry.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,19 @@ const mp_obj_property_t bleio_scanentry_address_obj = {
6262
(mp_obj_t)&mp_const_none_obj },
6363
};
6464

65-
//| .. attribute:: raw_data
65+
//| .. attribute:: advertisement_bytes
6666
//|
6767
//| All the advertisement data present in the packet, returned as a ``bytes`` object. (read-only)
6868
//|
69-
STATIC mp_obj_t scanentry_get_raw_data(mp_obj_t self_in) {
69+
STATIC mp_obj_t scanentry_get_advertisement_bytes(mp_obj_t self_in) {
7070
bleio_scanentry_obj_t *self = MP_OBJ_TO_PTR(self_in);
71-
return common_hal_bleio_scanentry_get_raw_data(self);
71+
return common_hal_bleio_scanentry_get_advertisement_bytes(self);
7272
}
73-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_scanentry_get_raw_data_obj, scanentry_get_raw_data);
73+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_scanentry_get_advertisement_bytes_obj, scanentry_get_advertisement_bytes);
7474

75-
const mp_obj_property_t bleio_scanentry_raw_data_obj = {
75+
const mp_obj_property_t bleio_scanentry_advertisement_bytes_obj = {
7676
.base.type = &mp_type_property,
77-
.proxy = { (mp_obj_t)&bleio_scanentry_get_raw_data_obj,
77+
.proxy = { (mp_obj_t)&bleio_scanentry_get_advertisement_bytes_obj,
7878
(mp_obj_t)&mp_const_none_obj,
7979
(mp_obj_t)&mp_const_none_obj },
8080
};
@@ -96,10 +96,11 @@ const mp_obj_property_t bleio_scanentry_rssi_obj = {
9696
(mp_obj_t)&mp_const_none_obj },
9797
};
9898

99+
99100
STATIC const mp_rom_map_elem_t bleio_scanentry_locals_dict_table[] = {
100-
{ MP_ROM_QSTR(MP_QSTR_address), MP_ROM_PTR(&bleio_scanentry_address_obj) },
101-
{ MP_ROM_QSTR(MP_QSTR_raw_data), MP_ROM_PTR(&bleio_scanentry_raw_data_obj) },
102-
{ MP_ROM_QSTR(MP_QSTR_rssi), MP_ROM_PTR(&bleio_scanentry_rssi_obj) },
101+
{ MP_ROM_QSTR(MP_QSTR_address), MP_ROM_PTR(&bleio_scanentry_address_obj) },
102+
{ MP_ROM_QSTR(MP_QSTR_advertisement_bytes), MP_ROM_PTR(&bleio_scanentry_advertisement_bytes_obj) },
103+
{ MP_ROM_QSTR(MP_QSTR_rssi), MP_ROM_PTR(&bleio_scanentry_rssi_obj) },
103104
};
104105

105106
STATIC MP_DEFINE_CONST_DICT(bleio_scanentry_locals_dict, bleio_scanentry_locals_dict_table);

shared-bindings/bleio/ScanEntry.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
extern const mp_obj_type_t bleio_scanentry_type;
3636

3737
mp_obj_t common_hal_bleio_scanentry_get_address(bleio_scanentry_obj_t *self);
38-
mp_obj_t common_hal_bleio_scanentry_get_raw_data(bleio_scanentry_obj_t *self);
38+
mp_obj_t common_hal_bleio_scanentry_get_advertisement_bytes(bleio_scanentry_obj_t *self);
3939
mp_int_t common_hal_bleio_scanentry_get_rssi(bleio_scanentry_obj_t *self);
4040

4141
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_SCANENTRY_H

shared-bindings/bleio/Scanner.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
//| :class:`Scanner` -- scan for nearby BLE devices
4343
//| =========================================================
4444
//|
45-
//| Allows scanning for nearby BLE devices.
45+
//| Scan for nearby BLE devices.
4646
//|
4747
//| Usage::
4848
//|
@@ -112,7 +112,7 @@ STATIC mp_obj_t bleio_scanner_scan(size_t n_args, const mp_obj_t *pos_args, mp_m
112112

113113
common_hal_bleio_scanner_scan(self, timeout, interval, window);
114114

115-
return common_hal_bleio_scanner_get_adv_reports(self);
115+
return common_hal_bleio_scanner_get_scan_entries(self);
116116
}
117117
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_scanner_scan_obj, 2, bleio_scanner_scan);
118118

shared-bindings/bleio/Scanner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ extern const mp_obj_type_t bleio_scanner_type;
3636
extern void common_hal_bleio_scanner_construct(bleio_scanner_obj_t *self);
3737
extern void 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_adv_reports(bleio_scanner_obj_t *self);
39+
extern mp_obj_t common_hal_bleio_scanner_get_scan_entries(bleio_scanner_obj_t *self);
4040

4141
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_SCANNER_H

shared-bindings/bleio/UUID.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,12 @@ STATIC mp_obj_t bleio_uuid_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
220220
}
221221
}
222222

223+
//|
224+
225+
//| .. method:: __eq__(other)
226+
//|
227+
//| Two UUID objects are equal if their values match and they are both 128-bit or both 16-bit.
228+
//|
223229
STATIC mp_obj_t bleio_uuid_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
224230
switch (op) {
225231
// Two UUID's are equal if their uuid16 values and uuid128 references match.

shared-module/bleio/Address.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@
2727

2828
#include <string.h>
2929

30-
#include "py/objproperty.h"
30+
#include "py/objstr.h"
3131
#include "shared-bindings/bleio/Address.h"
3232
#include "shared-module/bleio/Address.h"
3333

34-
void common_hal_bleio_address_construct(bleio_address_obj_t *self, uint8_t *bytes, size_t bytes_length, uint8_t address_type) {
35-
memcpy(self->bytes, bytes, bytes_length);
34+
void common_hal_bleio_address_construct(bleio_address_obj_t *self, uint8_t *bytes, uint8_t address_type) {
35+
self->bytes = mp_obj_new_bytes(bytes, NUM_BLEIO_ADDRESS_BYTES);
3636
self->type = address_type;
3737
}
3838

3939
mp_obj_t common_hal_bleio_address_get_address_bytes(bleio_address_obj_t *self) {
40-
return mp_obj_new_bytes(self->bytes, NUM_BLEIO_ADDRESS_BYTES);
40+
return self->bytes;
4141
}
4242

4343
uint8_t common_hal_bleio_address_get_type(bleio_address_obj_t *self) {

0 commit comments

Comments
 (0)