From 1639354e5fc886ee4b2064eaceedfd26ebbe1cd3 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 23 May 2019 16:07:54 -0400 Subject: [PATCH] Scanner working, but not very first time --- ports/nrf/bluetooth/ble_drv.h | 5 +- ports/nrf/common-hal/bleio/Scanner.c | 26 ++---- shared-bindings/bleio/Scanner.c | 120 ++++++++++----------------- shared-bindings/bleio/Scanner.h | 2 +- shared-bindings/bleio/__init__.c | 8 +- 5 files changed, 56 insertions(+), 105 deletions(-) diff --git a/ports/nrf/bluetooth/ble_drv.h b/ports/nrf/bluetooth/ble_drv.h index cdb4c9b033..133d77b48d 100644 --- a/ports/nrf/bluetooth/ble_drv.h +++ b/ports/nrf/bluetooth/ble_drv.h @@ -30,10 +30,6 @@ #include "ble.h" -#if (BLUETOOTH_SD == 132) && (BLE_API_VERSION == 2) -#define NRF52 -#endif - #define MAX_TX_IN_PROGRESS 10 #ifndef BLE_GATT_ATT_MTU_DEFAULT @@ -43,6 +39,7 @@ #define BLE_CONN_CFG_TAG_CUSTOM 1 #define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION)) +#define SEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000000) / (RESOLUTION)) // 0.625 msecs (625 usecs) #define ADV_INTERVAL_UNIT_FLOAT_SECS (0.000625) #define UNIT_0_625_MS (625) diff --git a/ports/nrf/common-hal/bleio/Scanner.c b/ports/nrf/common-hal/bleio/Scanner.c index d2e19b5f9f..d4479515e7 100644 --- a/ports/nrf/common-hal/bleio/Scanner.c +++ b/ports/nrf/common-hal/bleio/Scanner.c @@ -36,14 +36,12 @@ #include "shared-bindings/bleio/Scanner.h" #include "shared-module/bleio/ScanEntry.h" -#if (BLUETOOTH_SD == 140) static uint8_t m_scan_buffer_data[BLE_GAP_SCAN_BUFFER_MIN]; static ble_data_t m_scan_buffer = { m_scan_buffer_data, BLE_GAP_SCAN_BUFFER_MIN }; -#endif STATIC void on_ble_evt(ble_evt_t *ble_evt, void *scanner_in) { bleio_scanner_obj_t *scanner = (bleio_scanner_obj_t*)scanner_in; @@ -61,48 +59,34 @@ STATIC void on_ble_evt(ble_evt_t *ble_evt, void *scanner_in) { entry->address.type = report->peer_addr.addr_type; memcpy(entry->address.value, report->peer_addr.addr, BLEIO_ADDRESS_BYTES); -#if (BLUETOOTH_SD == 140) entry->data = mp_obj_new_bytearray(report->data.len, report->data.p_data); -#else - entry->data = mp_obj_new_bytearray(report->dlen, report->data); -#endif mp_obj_list_append(scanner->adv_reports, entry); -#if (BLUETOOTH_SD == 140) const uint32_t err_code = sd_ble_gap_scan_start(NULL, &m_scan_buffer); if (err_code != NRF_SUCCESS) { mp_raise_OSError_msg_varg(translate("Failed to continue scanning, err 0x%04x"), err_code); } -#endif } -void common_hal_bleio_scanner_scan(bleio_scanner_obj_t *self, mp_int_t timeout) { +void common_hal_bleio_scanner_scan(bleio_scanner_obj_t *self, mp_float_t timeout, mp_float_t interval, mp_float_t window) { ble_drv_add_event_handler(on_ble_evt, self); ble_gap_scan_params_t scan_params = { - .interval = MSEC_TO_UNITS(self->interval, UNIT_0_625_MS), - .window = MSEC_TO_UNITS(self->window, UNIT_0_625_MS), -#if (BLUETOOTH_SD == 140) + .interval = SEC_TO_UNITS(interval, UNIT_0_625_MS), + .window = SEC_TO_UNITS(window, UNIT_0_625_MS), .scan_phys = BLE_GAP_PHY_1MBPS, -#endif }; common_hal_bleio_adapter_set_enabled(true); uint32_t err_code; -#if (BLUETOOTH_SD == 140) err_code = sd_ble_gap_scan_start(&scan_params, &m_scan_buffer); -#else - err_code = sd_ble_gap_scan_start(&scan_params); -#endif if (err_code != NRF_SUCCESS) { mp_raise_OSError_msg_varg(translate("Failed to start scanning, err 0x%04x"), err_code); } - if (timeout > 0) { - mp_hal_delay_ms(timeout); - sd_ble_gap_scan_stop(); - } + mp_hal_delay_ms(timeout * 1000); + sd_ble_gap_scan_stop(); } diff --git a/shared-bindings/bleio/Scanner.c b/shared-bindings/bleio/Scanner.c index 73b6e6a0b2..5615ce1030 100644 --- a/shared-bindings/bleio/Scanner.c +++ b/shared-bindings/bleio/Scanner.c @@ -30,8 +30,12 @@ #include "shared-bindings/bleio/ScanEntry.h" #include "shared-bindings/bleio/Scanner.h" -#define DEFAULT_INTERVAL 100 -#define DEFAULT_WINDOW 100 +#define INTERVAL_DEFAULT (0.1f) +#define INTERVAL_MIN (0.0025f) +#define INTERVAL_MIN_STRING "0.0025" +#define INTERVAL_MAX (40.959375f) +#define INTERVAL_MAX_STRING "40.959375" +#define WINDOW_DEFAULT (0.1f) //| .. currentmodule:: bleio //| @@ -45,109 +49,76 @@ //| import bleio //| scanner = bleio.Scanner() //| entries = scanner.scan(2.5) # Scan for 2.5 seconds -//| print(entries) //| //| .. class:: Scanner() //| //| Create a new Scanner object. //| - -//| .. attribute:: interval -//| -//| The interval (in seconds) between the start of two consecutive scan windows. -//| Allowed values are between 0.010 and 10.24 sec. -//| - -//| .. attribute:: window -//| -//| The duration (in seconds) in which a single BLE channel is scanned. -//| Allowed values are between 0.010 and 10.24 sec. -//| - -//| .. method:: scan(timeout) -//| -//| Performs a BLE scan. -//| -//| :param float timeout: the scan timeout in seconds -//| :returns: advertising packets found -//| :rtype: list of :py:class:`bleio.ScanEntry` -//| STATIC mp_obj_t bleio_scanner_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *all_args, mp_map_t *kw_args) { mp_arg_check_num(n_args, kw_args, 0, 0, false); - bleio_scanner_obj_t *self = m_new_obj(bleio_scanner_obj_t); self->base.type = type; - self->interval = DEFAULT_INTERVAL; - self->window = DEFAULT_WINDOW; - return MP_OBJ_FROM_PTR(self); } -STATIC mp_obj_t bleio_scanner_get_interval(mp_obj_t self_in) { - bleio_scanner_obj_t *self = MP_OBJ_TO_PTR(self_in); +//| .. method:: scan(timeout, \*, interval=0.1, window=0.1) +//| +//| Performs a BLE scan. +//| +//| :param float timeout: the scan timeout in seconds +//| :param float interval: the interval (in seconds) between the start of two consecutive scan windows +//| Must be in the range 0.0025 - 40.959375 seconds. +//| :param float window: the duration (in seconds) to scan a single BLE channel +//| `window` must be <= `interval`. +//| :returns: advertising packets found +//| :rtype: list of :py:class:`bleio.ScanEntry` +//| +STATIC mp_obj_t bleio_scanner_scan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_timeout, ARG_interval, ARG_window }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_timeout, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_interval, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_window, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_obj = MP_OBJ_NULL} }, + }; - return mp_obj_new_int(self->interval); -} -MP_DEFINE_CONST_FUN_OBJ_1(bleio_scanner_get_interval_obj, bleio_scanner_get_interval); + bleio_scanner_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); -static mp_obj_t bleio_scanner_set_interval(mp_obj_t self_in, mp_obj_t value) { - bleio_scanner_obj_t *self = MP_OBJ_TO_PTR(self_in); + const mp_float_t timeout = mp_obj_get_float(args[ARG_timeout].u_obj); - self->interval = mp_obj_get_int(value); + if (args[ARG_interval].u_obj == MP_OBJ_NULL) { + args[ARG_interval].u_obj = mp_obj_new_float(INTERVAL_DEFAULT); + } - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_2(bleio_scanner_set_interval_obj, bleio_scanner_set_interval); + if (args[ARG_window].u_obj == MP_OBJ_NULL) { + args[ARG_window].u_obj = mp_obj_new_float(WINDOW_DEFAULT); + } -const mp_obj_property_t bleio_scanner_interval_obj = { - .base.type = &mp_type_property, - .proxy = { (mp_obj_t)&bleio_scanner_get_interval_obj, - (mp_obj_t)&bleio_scanner_set_interval_obj, - (mp_obj_t)&mp_const_none_obj }, -}; + const mp_float_t interval = mp_obj_float_get(args[ARG_interval].u_obj); + if (interval < INTERVAL_MIN || interval > INTERVAL_MAX) { + mp_raise_ValueError_varg(translate("interval must be in range %s-%s"), INTERVAL_MIN_STRING, INTERVAL_MAX_STRING); + } -STATIC mp_obj_t scanner_scan(mp_obj_t self_in, mp_obj_t timeout_in) { - bleio_scanner_obj_t *self = MP_OBJ_TO_PTR(self_in); - const mp_int_t timeout = mp_obj_get_int(timeout_in); + const mp_float_t window = mp_obj_float_get(args[ARG_window].u_obj); + if (window > interval) { + mp_raise_ValueError(translate("window must be <= interval")); + } self->adv_reports = mp_obj_new_list(0, NULL); - common_hal_bleio_scanner_scan(self, timeout); + common_hal_bleio_scanner_scan(self, timeout, interval, window); return self->adv_reports; } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(bleio_scanner_scan_obj, scanner_scan); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_scanner_scan_obj, 2, bleio_scanner_scan); -STATIC mp_obj_t bleio_scanner_get_window(mp_obj_t self_in) { - bleio_scanner_obj_t *self = MP_OBJ_TO_PTR(self_in); - - return mp_obj_new_int(self->window); -} -MP_DEFINE_CONST_FUN_OBJ_1(bleio_scanner_get_window_obj, bleio_scanner_get_window); - -static mp_obj_t bleio_scanner_set_window(mp_obj_t self_in, mp_obj_t value) { - bleio_scanner_obj_t *self = MP_OBJ_TO_PTR(self_in); - - self->window = mp_obj_get_int(value); - - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_2(bleio_scanner_set_window_obj, bleio_scanner_set_window); - -const mp_obj_property_t bleio_scanner_window_obj = { - .base.type = &mp_type_property, - .proxy = { (mp_obj_t)&bleio_scanner_get_window_obj, - (mp_obj_t)&bleio_scanner_set_window_obj, - (mp_obj_t)&mp_const_none_obj }, -}; STATIC const mp_rom_map_elem_t bleio_scanner_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_interval), MP_ROM_PTR(&bleio_scanner_interval_obj) }, { MP_ROM_QSTR(MP_QSTR_scan), MP_ROM_PTR(&bleio_scanner_scan_obj) }, - { MP_ROM_QSTR(MP_QSTR_window), MP_ROM_PTR(&bleio_scanner_window_obj) }, }; STATIC MP_DEFINE_CONST_DICT(bleio_scanner_locals_dict, bleio_scanner_locals_dict_table); @@ -155,7 +126,6 @@ STATIC MP_DEFINE_CONST_DICT(bleio_scanner_locals_dict, bleio_scanner_locals_dict const mp_obj_type_t bleio_scanner_type = { { &mp_type_type }, .name = MP_QSTR_Scanner, - .print = bleio_scanner_print, .make_new = bleio_scanner_make_new, .locals_dict = (mp_obj_dict_t*)&bleio_scanner_locals_dict }; diff --git a/shared-bindings/bleio/Scanner.h b/shared-bindings/bleio/Scanner.h index c72136389b..54c09675ba 100644 --- a/shared-bindings/bleio/Scanner.h +++ b/shared-bindings/bleio/Scanner.h @@ -33,7 +33,7 @@ extern const mp_obj_type_t bleio_scanner_type; -extern void common_hal_bleio_scanner_scan(bleio_scanner_obj_t *self, mp_int_t timeout); +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); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_SCANNER_H diff --git a/shared-bindings/bleio/__init__.c b/shared-bindings/bleio/__init__.c index 7348655acc..907d211016 100644 --- a/shared-bindings/bleio/__init__.c +++ b/shared-bindings/bleio/__init__.c @@ -64,8 +64,8 @@ // Descriptor // Device //| Peripheral -// ScanEntry -// Scanner +//| ScanEntry +//| Scanner //| Service //| UUID //| @@ -86,8 +86,8 @@ STATIC const mp_rom_map_elem_t bleio_module_globals_table[] = { // { MP_ROM_QSTR(MP_QSTR_Descriptor), MP_ROM_PTR(&bleio_descriptor_type) }, { MP_ROM_QSTR(MP_QSTR_Peripheral), MP_ROM_PTR(&bleio_peripheral_type) }, // Hide work-in-progress. -// { MP_ROM_QSTR(MP_QSTR_ScanEntry), MP_ROM_PTR(&bleio_scanentry_type) }, -// { MP_ROM_QSTR(MP_QSTR_Scanner), MP_ROM_PTR(&bleio_scanner_type) }, + { MP_ROM_QSTR(MP_QSTR_ScanEntry), MP_ROM_PTR(&bleio_scanentry_type) }, + { MP_ROM_QSTR(MP_QSTR_Scanner), MP_ROM_PTR(&bleio_scanner_type) }, { MP_ROM_QSTR(MP_QSTR_Service), MP_ROM_PTR(&bleio_service_type) }, { MP_ROM_QSTR(MP_QSTR_UUID), MP_ROM_PTR(&bleio_uuid_type) },