Skip to content

Commit 1c6bf9a

Browse files
committed
bleio: Move the Scanner class to a shared module
1 parent 7390dc7 commit 1c6bf9a

File tree

12 files changed

+317
-144
lines changed

12 files changed

+317
-144
lines changed

ports/nrf/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ DRIVERS_SRC_C += $(addprefix modules/,\
134134
ubluepy/ubluepy_characteristic.c \
135135
ubluepy/ubluepy_delegate.c \
136136
ubluepy/ubluepy_constants.c \
137-
ubluepy/ubluepy_scanner.c \
138137
)
139138

140139
SRC_COMMON_HAL += \
@@ -167,6 +166,7 @@ SRC_COMMON_HAL += \
167166
bleio/__init__.c \
168167
bleio/Adapter.c \
169168
bleio/Descriptor.c \
169+
bleio/Scanner.c \
170170
bleio/UUID.c
171171
endif
172172

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Glenn Ruben Bakke
7+
* Copyright (c) 2018 Artur Pacholec
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include <string.h>
29+
30+
#include "ble_drv.h"
31+
#include "py/mphal.h"
32+
#include "shared-bindings/bleio/Scanner.h"
33+
#include "shared-bindings/bleio/ScanEntry.h"
34+
35+
STATIC void adv_event_handler(bleio_scanner_obj_t *self, ble_drv_adv_data_t *data) {
36+
// TODO: Don't add new entry for each item, group by address and update
37+
bleio_scanentry_obj_t *item = m_new_obj(bleio_scanentry_obj_t);
38+
item->base.type = &bleio_scanentry_type;
39+
40+
item->rssi = data->rssi;
41+
item->data = mp_obj_new_bytearray(data->data_len, data->p_data);
42+
43+
item->address.type = data->addr_type;
44+
memcpy(item->address.value, data->p_peer_addr, BLEIO_ADDRESS_BYTES);
45+
46+
mp_obj_list_append(self->adv_reports, item);
47+
48+
ble_drv_scan_continue();
49+
}
50+
51+
void common_hal_bleio_scanner_scan(bleio_scanner_obj_t *self, mp_int_t timeout) {
52+
ble_drv_adv_report_handler_set(self, adv_event_handler);
53+
54+
ble_drv_scan_start(self->interval, self->window);
55+
56+
mp_hal_delay_ms(timeout);
57+
58+
ble_drv_scan_stop();
59+
}

ports/nrf/drivers/bluetooth/ble_drv.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static volatile ble_drv_disc_add_service_callback_t disc_add_service_handler;
9494
static volatile ble_drv_disc_add_char_callback_t disc_add_char_handler;
9595
static volatile ble_drv_gattc_char_data_callback_t gattc_char_data_handle;
9696

97-
static mp_obj_t mp_adv_observer;
97+
static bleio_scanner_obj_t *mp_adv_observer;
9898
static mp_obj_t mp_gattc_observer;
9999
static mp_obj_t mp_gattc_disc_service_observer;
100100
static mp_obj_t mp_gattc_disc_char_observer;
@@ -707,8 +707,8 @@ void ble_drv_gattc_event_handler_set(mp_obj_t obj, ble_drv_gattc_evt_callback_t
707707
gattc_event_handler = evt_handler;
708708
}
709709

710-
void ble_drv_adv_report_handler_set(mp_obj_t obj, ble_drv_adv_evt_callback_t evt_handler) {
711-
mp_adv_observer = obj;
710+
void ble_drv_adv_report_handler_set(bleio_scanner_obj_t *self, ble_drv_adv_evt_callback_t evt_handler) {
711+
mp_adv_observer = self;
712712
adv_event_handler = evt_handler;
713713
}
714714

@@ -760,15 +760,15 @@ void ble_drv_attr_c_write(uint16_t conn_handle, uint16_t handle, uint16_t len, u
760760
;
761761
}
762762
}
763-
void ble_drv_scan_start(void) {
763+
void ble_drv_scan_start(uint16_t interval, uint16_t window) {
764764
SD_TEST_OR_ENABLE();
765765

766766
ble_gap_scan_params_t scan_params;
767767
memset(&scan_params, 0, sizeof(ble_gap_scan_params_t));
768768

769769
scan_params.active = 1;
770-
scan_params.interval = MSEC_TO_UNITS(100, UNIT_0_625_MS);
771-
scan_params.window = MSEC_TO_UNITS(100, UNIT_0_625_MS);
770+
scan_params.interval = MSEC_TO_UNITS(interval, UNIT_0_625_MS);
771+
scan_params.window = MSEC_TO_UNITS(window, UNIT_0_625_MS);
772772
#if (BLUETOOTH_SD == 140)
773773
scan_params.scan_phys = BLE_GAP_PHY_1MBPS;
774774
#endif
@@ -1003,10 +1003,9 @@ static void ble_evt_handler(ble_evt_t * p_ble_evt) {
10031003
#endif
10041004
};
10051005

1006-
// TODO: Fix unsafe callback to possible undefined callback...
1007-
adv_event_handler(mp_adv_observer,
1008-
p_ble_evt->header.evt_id,
1009-
&adv_data);
1006+
if (adv_event_handler != NULL) {
1007+
adv_event_handler(mp_adv_observer, &adv_data);
1008+
}
10101009
break;
10111010

10121011
case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST:

ports/nrf/drivers/bluetooth/ble_drv.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include <stdint.h>
3333
#include <stdbool.h>
3434

35+
#include "shared-module/bleio/Scanner.h"
36+
3537
#include "modubluepy.h"
3638

3739
typedef struct {
@@ -67,7 +69,7 @@ typedef struct {
6769
typedef void (*ble_drv_gap_evt_callback_t)(mp_obj_t self, uint16_t event_id, uint16_t conn_handle, uint16_t length, uint8_t * data);
6870
typedef void (*ble_drv_gatts_evt_callback_t)(mp_obj_t self, uint16_t event_id, uint16_t attr_handle, uint16_t length, uint8_t * data);
6971
typedef void (*ble_drv_gattc_evt_callback_t)(mp_obj_t self, uint16_t event_id, uint16_t attr_handle, uint16_t length, uint8_t * data);
70-
typedef void (*ble_drv_adv_evt_callback_t)(mp_obj_t self, uint16_t event_id, ble_drv_adv_data_t * data);
72+
typedef void (*ble_drv_adv_evt_callback_t)(bleio_scanner_obj_t *self, ble_drv_adv_data_t *data);
7173
typedef void (*ble_drv_disc_add_service_callback_t)(mp_obj_t self, ble_drv_service_data_t * p_service_data);
7274
typedef void (*ble_drv_disc_add_char_callback_t)(mp_obj_t self, ble_drv_char_data_t * p_desc_data);
7375
typedef void (*ble_drv_gattc_char_data_callback_t)(mp_obj_t self, uint16_t length, uint8_t * p_data);
@@ -106,13 +108,13 @@ void ble_drv_attr_s_notify(uint16_t conn_handle, uint16_t handle, uint16_t len,
106108

107109
void ble_drv_attr_c_write(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data, bool w_response);
108110

109-
void ble_drv_scan_start(void);
111+
void ble_drv_scan_start(uint16_t interval, uint16_t window);
110112

111113
void ble_drv_scan_continue(void);
112114

113115
void ble_drv_scan_stop(void);
114116

115-
void ble_drv_adv_report_handler_set(mp_obj_t obj, ble_drv_adv_evt_callback_t evt_handler);
117+
void ble_drv_adv_report_handler_set(bleio_scanner_obj_t *self, ble_drv_adv_evt_callback_t evt_handler);
116118

117119
void ble_drv_connect(uint8_t * p_addr, uint8_t addr_type);
118120

ports/nrf/modules/ubluepy/modubluepy.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ extern const mp_obj_type_t ubluepy_service_type;
3333
extern const mp_obj_type_t ubluepy_characteristic_type;
3434
extern const mp_obj_type_t ubluepy_delegate_type;
3535
extern const mp_obj_type_t ubluepy_constants_type;
36-
extern const mp_obj_type_t ubluepy_scanner_type;
3736

3837
STATIC const mp_rom_map_elem_t mp_module_ubluepy_globals_table[] = {
3938
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ubluepy) },
@@ -42,9 +41,6 @@ STATIC const mp_rom_map_elem_t mp_module_ubluepy_globals_table[] = {
4241
#endif
4342
#if 0 // MICROPY_PY_UBLUEPY_CENTRAL
4443
{ MP_ROM_QSTR(MP_QSTR_Central), MP_ROM_PTR(&ubluepy_central_type) },
45-
#endif
46-
#if MICROPY_PY_UBLUEPY_CENTRAL
47-
{ MP_ROM_QSTR(MP_QSTR_Scanner), MP_ROM_PTR(&ubluepy_scanner_type) },
4844
#endif
4945
{ MP_ROM_QSTR(MP_QSTR_DefaultDelegate), MP_ROM_PTR(&ubluepy_delegate_type) },
5046
{ MP_ROM_QSTR(MP_QSTR_Service), MP_ROM_PTR(&ubluepy_service_type) },

ports/nrf/modules/ubluepy/modubluepy.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ p.advertise(device_name="micr", services=[s])
7777
extern const mp_obj_type_t ubluepy_service_type;
7878
extern const mp_obj_type_t ubluepy_characteristic_type;
7979
extern const mp_obj_type_t ubluepy_peripheral_type;
80-
extern const mp_obj_type_t ubluepy_scanner_type;
8180
extern const mp_obj_type_t ubluepy_constants_type;
8281
extern const mp_obj_type_t ubluepy_constants_ad_types_type;
8382

@@ -147,11 +146,6 @@ typedef struct _ubluepy_advertise_data_t {
147146
bool connectable;
148147
} ubluepy_advertise_data_t;
149148

150-
typedef struct _ubluepy_scanner_obj_t {
151-
mp_obj_base_t base;
152-
mp_obj_t adv_reports;
153-
} ubluepy_scanner_obj_t;
154-
155149
typedef enum _ubluepy_prop_t {
156150
UBLUEPY_PROP_BROADCAST = 0x01,
157151
UBLUEPY_PROP_READ = 0x02,

ports/nrf/modules/ubluepy/ubluepy_scanner.c

Lines changed: 0 additions & 118 deletions
This file was deleted.

shared-bindings/bleio/ScanEntry.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
//| .. attribute:: address
4949
//|
5050
//| The address of the device. (read-only)
51-
//| This attribute is of type `bleio:Address`.
51+
//| This attribute is of type `bleio.Address`.
5252
//|
5353

5454
//| .. attribute:: manufacturer_specific_data
@@ -75,7 +75,7 @@
7575
//| .. attribute:: service_uuids
7676
//|
7777
//| The address of the device. (read-only)
78-
//| This attribute is a list of `bleio:UUID`.
78+
//| This attribute is a list of `bleio.UUID`.
7979
//| This attribute might be empty or incomplete, depending on the advertisement packet.
8080
//| Currently only 16-bit UUIDS are listed.
8181
//|

0 commit comments

Comments
 (0)