diff options
| author | Dan Halbert <halbert@halwitz.org> | 2019-06-22 22:10:15 -0400 |
|---|---|---|
| committer | Dan Halbert <halbert@halwitz.org> | 2019-06-22 22:10:15 -0400 |
| commit | 140904ec84ad1900f280c1febaa89a499c42b8c5 (patch) | |
| tree | d6d686b2056dc99975f66399ad82428f75ee8dcb | |
| parent | 4881e1ff55768c027404cc6a1375c43d4c3acf2a (diff) | |
getting Scanner to work
| -rw-r--r-- | ports/nrf/common-hal/bleio/Scanner.c | 17 | ||||
| -rw-r--r-- | ports/nrf/common-hal/bleio/Scanner.h | 2 | ||||
| -rw-r--r-- | shared-bindings/bleio/Address.c | 53 | ||||
| -rw-r--r-- | shared-bindings/bleio/Address.h | 2 | ||||
| -rw-r--r-- | shared-bindings/bleio/ScanEntry.c | 19 | ||||
| -rw-r--r-- | shared-bindings/bleio/ScanEntry.h | 2 | ||||
| -rw-r--r-- | shared-bindings/bleio/Scanner.c | 4 | ||||
| -rw-r--r-- | shared-bindings/bleio/Scanner.h | 2 | ||||
| -rw-r--r-- | shared-bindings/bleio/UUID.c | 6 | ||||
| -rw-r--r-- | shared-module/bleio/Address.c | 8 | ||||
| -rw-r--r-- | shared-module/bleio/Address.h | 2 | ||||
| -rw-r--r-- | shared-module/bleio/ScanEntry.c | 8 | ||||
| -rw-r--r-- | shared-module/bleio/ScanEntry.h | 2 |
13 files changed, 90 insertions, 37 deletions
diff --git a/ports/nrf/common-hal/bleio/Scanner.c b/ports/nrf/common-hal/bleio/Scanner.c index 6715bd7a2..04dc62f95 100644 --- a/ports/nrf/common-hal/bleio/Scanner.c +++ b/ports/nrf/common-hal/bleio/Scanner.c @@ -57,12 +57,15 @@ STATIC void on_ble_evt(ble_evt_t *ble_evt, void *scanner_in) { entry->base.type = &bleio_scanentry_type; entry->rssi = report->rssi; - memcpy(entry->address.bytes, report->data.p_data, NUM_BLEIO_ADDRESS_BYTES); - entry->address.type = report->peer_addr.addr_type; + bleio_address_obj_t *address = m_new_obj(bleio_address_obj_t); + address->base.type = &bleio_address_type; + common_hal_bleio_address_construct(MP_OBJ_TO_PTR(address), + report->peer_addr.addr, report->peer_addr.addr_type); + entry->address = address; entry->data = mp_obj_new_bytes(report->data.p_data, report->data.len); - mp_obj_list_append(scanner->adv_reports, entry); + mp_obj_list_append(scanner->scan_entries, MP_OBJ_FROM_PTR(entry)); const uint32_t err_code = sd_ble_gap_scan_start(NULL, &m_scan_buffer); if (err_code != NRF_SUCCESS) { @@ -71,7 +74,7 @@ STATIC void on_ble_evt(ble_evt_t *ble_evt, void *scanner_in) { } void common_hal_bleio_scanner_construct(bleio_scanner_obj_t *self) { - self->adv_reports = mp_obj_new_list(0, NULL); + self->scan_entries = mp_obj_new_list(0, NULL); } 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 }; // Empty the advertising reports list. - mp_obj_list_clear(self->adv_reports); + mp_obj_list_clear(self->scan_entries); uint32_t err_code; 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 sd_ble_gap_scan_stop(); } -mp_obj_t common_hal_bleio_scanner_get_adv_reports(bleio_scanner_obj_t *self) { - return self->adv_reports; +mp_obj_t common_hal_bleio_scanner_get_scan_entries(bleio_scanner_obj_t *self) { + return self->scan_entries; } diff --git a/ports/nrf/common-hal/bleio/Scanner.h b/ports/nrf/common-hal/bleio/Scanner.h index a71066487..3768a52cb 100644 --- a/ports/nrf/common-hal/bleio/Scanner.h +++ b/ports/nrf/common-hal/bleio/Scanner.h @@ -32,7 +32,7 @@ typedef struct { mp_obj_base_t base; - mp_obj_t adv_reports; // List of reports. + mp_obj_t scan_entries; uint16_t interval; uint16_t window; } bleio_scanner_obj_t; diff --git a/shared-bindings/bleio/Address.c b/shared-bindings/bleio/Address.c index 5875cf9bd..0e094c2e3 100644 --- a/shared-bindings/bleio/Address.c +++ b/shared-bindings/bleio/Address.c @@ -80,7 +80,7 @@ STATIC mp_obj_t bleio_address_make_new(const mp_obj_type_t *type, size_t n_args, mp_raise_ValueError(translate("Address type out of range")); } - common_hal_bleio_address_construct(self, buf_info.buf, buf_info.len, address_type); + common_hal_bleio_address_construct(self, buf_info.buf, address_type); return MP_OBJ_FROM_PTR(self); } @@ -101,6 +101,13 @@ STATIC mp_obj_t bleio_address_get_address_bytes(mp_obj_t self_in) { } MP_DEFINE_CONST_FUN_OBJ_1(bleio_address_get_address_bytes_obj, bleio_address_get_address_bytes); +const mp_obj_property_t bleio_address_address_bytes_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&bleio_address_get_address_bytes_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + //| .. attribute:: type //| //| The address type (read-only). One of these integers: @@ -124,9 +131,47 @@ const mp_obj_property_t bleio_address_type_obj = { (mp_obj_t)&mp_const_none_obj}, }; +//| .. method:: __eq__(other) +//| +//| Two Address objects are equal if their addresses and address types are equal. +//| +STATIC mp_obj_t bleio_address_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { + switch (op) { + // Two Addresses are equal if their address bytes and address_type are equal + case MP_BINARY_OP_EQUAL: + if (MP_OBJ_IS_TYPE(rhs_in, &bleio_address_type)) { + bleio_address_obj_t *lhs = MP_OBJ_TO_PTR(lhs_in); + bleio_address_obj_t *rhs = MP_OBJ_TO_PTR(rhs_in); + return mp_obj_new_bool( + mp_obj_equal(common_hal_bleio_address_get_address_bytes(lhs), + common_hal_bleio_address_get_address_bytes(rhs)) && + common_hal_bleio_address_get_type(lhs) == + common_hal_bleio_address_get_type(rhs)); + + } else { + return mp_const_false; + } + + default: + return MP_OBJ_NULL; // op not supported + } +} + +STATIC void bleio_address_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + bleio_address_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_obj_t address_bytes = common_hal_bleio_address_get_address_bytes(self); + + mp_buffer_info_t buf_info; + mp_get_buffer_raise(address_bytes, &buf_info, MP_BUFFER_READ); + const uint8_t *buf = (uint8_t *) buf_info.buf; + mp_printf(print, + "%02x:%02x:%02x:%02x:%02x:%02x", + buf[5], buf[4], buf[3], buf[2], buf[1], buf[0]); +} + STATIC const mp_rom_map_elem_t bleio_address_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_address_bytes), MP_ROM_PTR(&bleio_address_get_address_bytes_obj) }, - { MP_ROM_QSTR(MP_QSTR_type), MP_ROM_PTR(&bleio_address_get_type_obj) }, + { MP_ROM_QSTR(MP_QSTR_address_bytes), MP_ROM_PTR(&bleio_address_address_bytes_obj) }, + { MP_ROM_QSTR(MP_QSTR_type), MP_ROM_PTR(&bleio_address_type_obj) }, // These match the BLE_GAP_ADDR_TYPES values used by the nRF library. { MP_ROM_QSTR(MP_QSTR_PUBLIC), MP_OBJ_NEW_SMALL_INT(0) }, { 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 = { { &mp_type_type }, .name = MP_QSTR_Address, .make_new = bleio_address_make_new, + .print = bleio_address_print, + .binary_op = bleio_address_binary_op, .locals_dict = (mp_obj_dict_t*)&bleio_address_locals_dict }; diff --git a/shared-bindings/bleio/Address.h b/shared-bindings/bleio/Address.h index bff1542cf..5e02ee210 100644 --- a/shared-bindings/bleio/Address.h +++ b/shared-bindings/bleio/Address.h @@ -41,7 +41,7 @@ extern const mp_obj_type_t bleio_address_type; -extern void common_hal_bleio_address_construct(bleio_address_obj_t *self, uint8_t *bytes, size_t bytes_length, uint8_t address_type); +extern void common_hal_bleio_address_construct(bleio_address_obj_t *self, uint8_t *bytes, uint8_t address_type); extern mp_obj_t common_hal_bleio_address_get_address_bytes(bleio_address_obj_t *self); extern uint8_t common_hal_bleio_address_get_type(bleio_address_obj_t *self); diff --git a/shared-bindings/bleio/ScanEntry.c b/shared-bindings/bleio/ScanEntry.c index 145d2b023..b7798175c 100644 --- a/shared-bindings/bleio/ScanEntry.c +++ b/shared-bindings/bleio/ScanEntry.c @@ -62,19 +62,19 @@ const mp_obj_property_t bleio_scanentry_address_obj = { (mp_obj_t)&mp_const_none_obj }, }; -//| .. attribute:: raw_data +//| .. attribute:: advertisement_bytes //| //| All the advertisement data present in the packet, returned as a ``bytes`` object. (read-only) //| -STATIC mp_obj_t scanentry_get_raw_data(mp_obj_t self_in) { +STATIC mp_obj_t scanentry_get_advertisement_bytes(mp_obj_t self_in) { bleio_scanentry_obj_t *self = MP_OBJ_TO_PTR(self_in); - return common_hal_bleio_scanentry_get_raw_data(self); + return common_hal_bleio_scanentry_get_advertisement_bytes(self); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_scanentry_get_raw_data_obj, scanentry_get_raw_data); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_scanentry_get_advertisement_bytes_obj, scanentry_get_advertisement_bytes); -const mp_obj_property_t bleio_scanentry_raw_data_obj = { +const mp_obj_property_t bleio_scanentry_advertisement_bytes_obj = { .base.type = &mp_type_property, - .proxy = { (mp_obj_t)&bleio_scanentry_get_raw_data_obj, + .proxy = { (mp_obj_t)&bleio_scanentry_get_advertisement_bytes_obj, (mp_obj_t)&mp_const_none_obj, (mp_obj_t)&mp_const_none_obj }, }; @@ -96,10 +96,11 @@ const mp_obj_property_t bleio_scanentry_rssi_obj = { (mp_obj_t)&mp_const_none_obj }, }; + STATIC const mp_rom_map_elem_t bleio_scanentry_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_address), MP_ROM_PTR(&bleio_scanentry_address_obj) }, - { MP_ROM_QSTR(MP_QSTR_raw_data), MP_ROM_PTR(&bleio_scanentry_raw_data_obj) }, - { MP_ROM_QSTR(MP_QSTR_rssi), MP_ROM_PTR(&bleio_scanentry_rssi_obj) }, + { MP_ROM_QSTR(MP_QSTR_address), MP_ROM_PTR(&bleio_scanentry_address_obj) }, + { MP_ROM_QSTR(MP_QSTR_advertisement_bytes), MP_ROM_PTR(&bleio_scanentry_advertisement_bytes_obj) }, + { MP_ROM_QSTR(MP_QSTR_rssi), MP_ROM_PTR(&bleio_scanentry_rssi_obj) }, }; STATIC MP_DEFINE_CONST_DICT(bleio_scanentry_locals_dict, bleio_scanentry_locals_dict_table); diff --git a/shared-bindings/bleio/ScanEntry.h b/shared-bindings/bleio/ScanEntry.h index 3a02f2fac..77e93175f 100644 --- a/shared-bindings/bleio/ScanEntry.h +++ b/shared-bindings/bleio/ScanEntry.h @@ -35,7 +35,7 @@ extern const mp_obj_type_t bleio_scanentry_type; mp_obj_t common_hal_bleio_scanentry_get_address(bleio_scanentry_obj_t *self); -mp_obj_t common_hal_bleio_scanentry_get_raw_data(bleio_scanentry_obj_t *self); +mp_obj_t common_hal_bleio_scanentry_get_advertisement_bytes(bleio_scanentry_obj_t *self); mp_int_t common_hal_bleio_scanentry_get_rssi(bleio_scanentry_obj_t *self); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_SCANENTRY_H diff --git a/shared-bindings/bleio/Scanner.c b/shared-bindings/bleio/Scanner.c index 92f6a2a18..d50f69fc5 100644 --- a/shared-bindings/bleio/Scanner.c +++ b/shared-bindings/bleio/Scanner.c @@ -42,7 +42,7 @@ //| :class:`Scanner` -- scan for nearby BLE devices //| ========================================================= //| -//| Allows scanning for nearby BLE devices. +//| Scan for nearby BLE devices. //| //| Usage:: //| @@ -112,7 +112,7 @@ STATIC mp_obj_t bleio_scanner_scan(size_t n_args, const mp_obj_t *pos_args, mp_m common_hal_bleio_scanner_scan(self, timeout, interval, window); - return common_hal_bleio_scanner_get_adv_reports(self); + return common_hal_bleio_scanner_get_scan_entries(self); } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_scanner_scan_obj, 2, bleio_scanner_scan); diff --git a/shared-bindings/bleio/Scanner.h b/shared-bindings/bleio/Scanner.h index b9e3ecdee..1bbab78f4 100644 --- a/shared-bindings/bleio/Scanner.h +++ b/shared-bindings/bleio/Scanner.h @@ -36,6 +36,6 @@ extern const mp_obj_type_t bleio_scanner_type; extern void common_hal_bleio_scanner_construct(bleio_scanner_obj_t *self); extern void common_hal_bleio_scanner_scan(bleio_scanner_obj_t *self, mp_float_t timeout, mp_float_t interval, mp_float_t window); extern void common_hal_bleio_scanner_stop(bleio_scanner_obj_t *self); -extern mp_obj_t common_hal_bleio_scanner_get_adv_reports(bleio_scanner_obj_t *self); +extern mp_obj_t common_hal_bleio_scanner_get_scan_entries(bleio_scanner_obj_t *self); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_SCANNER_H diff --git a/shared-bindings/bleio/UUID.c b/shared-bindings/bleio/UUID.c index 972505058..0db7435f4 100644 --- a/shared-bindings/bleio/UUID.c +++ b/shared-bindings/bleio/UUID.c @@ -220,6 +220,12 @@ STATIC mp_obj_t bleio_uuid_unary_op(mp_unary_op_t op, mp_obj_t self_in) { } } +//| + +//| .. method:: __eq__(other) +//| +//| Two UUID objects are equal if their values match and they are both 128-bit or both 16-bit. +//| STATIC mp_obj_t bleio_uuid_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { switch (op) { // Two UUID's are equal if their uuid16 values and uuid128 references match. diff --git a/shared-module/bleio/Address.c b/shared-module/bleio/Address.c index e26bbe2ac..6bc458b7c 100644 --- a/shared-module/bleio/Address.c +++ b/shared-module/bleio/Address.c @@ -27,17 +27,17 @@ #include <string.h> -#include "py/objproperty.h" +#include "py/objstr.h" #include "shared-bindings/bleio/Address.h" #include "shared-module/bleio/Address.h" -void common_hal_bleio_address_construct(bleio_address_obj_t *self, uint8_t *bytes, size_t bytes_length, uint8_t address_type) { - memcpy(self->bytes, bytes, bytes_length); +void common_hal_bleio_address_construct(bleio_address_obj_t *self, uint8_t *bytes, uint8_t address_type) { + self->bytes = mp_obj_new_bytes(bytes, NUM_BLEIO_ADDRESS_BYTES); self->type = address_type; } mp_obj_t common_hal_bleio_address_get_address_bytes(bleio_address_obj_t *self) { - return mp_obj_new_bytes(self->bytes, NUM_BLEIO_ADDRESS_BYTES); + return self->bytes; } uint8_t common_hal_bleio_address_get_type(bleio_address_obj_t *self) { diff --git a/shared-module/bleio/Address.h b/shared-module/bleio/Address.h index eb1aab258..39789842f 100644 --- a/shared-module/bleio/Address.h +++ b/shared-module/bleio/Address.h @@ -35,7 +35,7 @@ typedef struct { mp_obj_base_t base; uint8_t type; - uint8_t bytes[NUM_BLEIO_ADDRESS_BYTES]; + mp_obj_t bytes; // a bytes() object } bleio_address_obj_t; #endif // MICROPY_INCLUDED_SHARED_MODULE_BLEIO_ADDRESS_H diff --git a/shared-module/bleio/ScanEntry.c b/shared-module/bleio/ScanEntry.c index 44b50a4ce..306ac33c5 100644 --- a/shared-module/bleio/ScanEntry.c +++ b/shared-module/bleio/ScanEntry.c @@ -33,14 +33,10 @@ #include "shared-module/bleio/ScanEntry.h" mp_obj_t common_hal_bleio_scanentry_get_address(bleio_scanentry_obj_t *self) { - bleio_address_obj_t *address = m_new_obj(bleio_address_obj_t); - address->base.type = &bleio_address_type; - memcpy(address->bytes, self->address.bytes, NUM_BLEIO_ADDRESS_BYTES); - address->type = self->address.type; - return MP_OBJ_TO_PTR(address); + return MP_OBJ_FROM_PTR(self->address); } -mp_obj_t common_hal_bleio_scanentry_get_raw_data(bleio_scanentry_obj_t *self) { +mp_obj_t common_hal_bleio_scanentry_get_advertisement_bytes(bleio_scanentry_obj_t *self) { return self->data; } diff --git a/shared-module/bleio/ScanEntry.h b/shared-module/bleio/ScanEntry.h index 4e7028ba2..b302460e6 100644 --- a/shared-module/bleio/ScanEntry.h +++ b/shared-module/bleio/ScanEntry.h @@ -35,7 +35,7 @@ typedef struct { mp_obj_base_t base; bool connectable; int8_t rssi; - bleio_address_obj_t address; + bleio_address_obj_t *address; mp_obj_t data; } bleio_scanentry_obj_t; |
