getting Scanner to work

This commit is contained in:
Dan Halbert 2019-06-22 22:10:15 -04:00
parent 4881e1ff55
commit 140904ec84
13 changed files with 90 additions and 37 deletions

View File

@ -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;
}

View File

@ -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;

View File

@ -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
};

View File

@ -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);

View File

@ -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);

View File

@ -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

View File

@ -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);

View File

@ -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

View File

@ -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.

View File

@ -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) {

View File

@ -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

View File

@ -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;
}

View File

@ -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;