Skip to content

Commit 77eeecb

Browse files
committed
nrf: BLE driver cleanup
1 parent a126897 commit 77eeecb

File tree

5 files changed

+53
-98
lines changed

5 files changed

+53
-98
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,22 @@
3131
#include "ble_drv.h"
3232
#include "nrfx.h"
3333
#include "nrf_error.h"
34+
#include "nrf_sdm.h"
35+
#include "py/nlr.h"
3436
#include "shared-module/bleio/Address.h"
3537

3638
void common_hal_bleio_adapter_set_enabled(bool enabled) {
37-
if (enabled) {
38-
const uint32_t err = ble_drv_stack_enable();
39-
if (err != NRF_SUCCESS) {
40-
NRFX_ASSERT(err);
41-
}
39+
uint32_t err_code;
4240

43-
printf("SoftDevice enabled\n");
41+
if (enabled) {
42+
err_code = ble_drv_stack_enable();
4443
} else {
45-
ble_drv_stack_disable();
44+
err_code = sd_softdevice_disable();
45+
}
46+
47+
if (err_code != NRF_SUCCESS) {
48+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
49+
"Failed to change softdevice status, error: 0x%08lX", err_code));
4650
}
4751
}
4852

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,8 @@
2828
#include "shared-module/bleio/Characteristic.h"
2929
#include "shared-module/bleio/Device.h"
3030

31-
void data_callback(bleio_characteristic_obj_t *self, uint16_t length, uint8_t *data) {
32-
self->value_data = mp_obj_new_bytearray(length, data);
33-
}
34-
3531
void common_hal_bleio_characteristic_read_value(bleio_characteristic_obj_t *self) {
36-
ble_drv_attr_c_read(self, data_callback);
32+
ble_drv_attr_c_read(self);
3733
}
3834

3935
void common_hal_bleio_characteristic_write_value(bleio_characteristic_obj_t *self, mp_buffer_info_t *bufinfo) {

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
#include "shared-bindings/bleio/Service.h"
3737
#include "shared-bindings/bleio/UUID.h"
3838

39-
static volatile bool m_disc_evt_received;
40-
4139
STATIC void gap_event_handler(bleio_device_obj_t *device, uint16_t event_id, uint16_t conn_handle, uint16_t length, uint8_t * data) {
4240
if (event_id == BLE_GAP_EVT_CONNECTED) {
4341
device->conn_handle = conn_handle;
@@ -50,10 +48,6 @@ STATIC void gatts_event_handler(bleio_device_obj_t *device, uint16_t event_id, u
5048

5149
}
5250

53-
STATIC void gattc_event_handler(bleio_device_obj_t *device, uint16_t event_id, uint16_t attr_handle, uint16_t length, uint8_t * data) {
54-
m_disc_evt_received = true;
55-
}
56-
5751
void common_hal_bleio_device_start_advertising(bleio_device_obj_t *device, bleio_advertisement_data_t *adv_data) {
5852
if (adv_data->connectable) {
5953
ble_drv_gap_event_handler_set(device, gap_event_handler);
@@ -80,8 +74,6 @@ void common_hal_bleio_device_connect(bleio_device_obj_t *device) {
8074
#endif
8175
}
8276

83-
ble_drv_gattc_event_handler_set(device, gattc_event_handler);
84-
8577
// TODO: read name
8678

8779
// find services

ports/nrf/drivers/bluetooth/ble_drv.c

Lines changed: 37 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@
4444
#include "py/runtime.h"
4545
#include "supervisor/shared/translate.h"
4646
#include "ble_drv.h"
47-
#include "mpconfigport.h"
47+
#include "nrf_nvic.h"
4848
#include "nrf_sdm.h"
4949
#include "nrfx_power.h"
50-
#include "ble_gap.h"
51-
#include "ble_hci.h"
52-
#include "ble.h" // sd_ble_uuid_encode
50+
#include "py/objstr.h"
51+
#include "py/runtime.h"
52+
#include "shared-bindings/bleio/Characteristic.h"
53+
#include "shared-bindings/bleio/ScanEntry.h"
54+
#include "shared-bindings/bleio/UUID.h"
5355

5456
#define BLE_DRIVER_LOG printf
5557

@@ -60,8 +62,6 @@
6062
#define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION))
6163
#define UNIT_0_625_MS (625)
6264
#define UNIT_10_MS (10000)
63-
#define APP_CFG_NON_CONN_ADV_TIMEOUT 0 // Disable timeout.
64-
#define NON_CONNECTABLE_ADV_INTERVAL MSEC_TO_UNITS(100, UNIT_0_625_MS)
6565

6666
#define BLE_MIN_CONN_INTERVAL MSEC_TO_UNITS(15, UNIT_0_625_MS)
6767
#define BLE_MAX_CONN_INTERVAL MSEC_TO_UNITS(300, UNIT_0_625_MS)
@@ -77,29 +77,24 @@ if (ble_drv_stack_enabled() == 0) { \
7777
(void)ble_drv_stack_enable(); \
7878
}
7979

80-
static volatile bool m_adv_in_progress;
81-
static volatile bool m_tx_in_progress;
82-
83-
static ble_drv_gap_evt_callback_t gap_event_handler;
84-
static ble_drv_gatts_evt_callback_t gatts_event_handler;
80+
static ble_drv_adv_evt_callback_t adv_event_handler;
81+
static ble_drv_gatts_evt_callback_t gatts_event_handler;
82+
static ble_drv_gap_evt_callback_t gap_event_handler;
8583

86-
static bleio_device_obj_t *mp_gap_observer;
84+
static bleio_characteristic_obj_t *mp_gattc_char_data_observer;
85+
static bleio_device_obj_t *mp_gattc_disc_service_observer;
86+
static bleio_service_obj_t *mp_gattc_disc_char_observer;
87+
static bleio_address_obj_t *mp_connect_address;
8788
static bleio_device_obj_t *mp_gatts_observer;
89+
static bleio_scanner_obj_t *mp_adv_observer;
90+
static bleio_device_obj_t *mp_gap_observer;
8891

8992
static volatile bool m_primary_service_found;
9093
static volatile bool m_characteristic_found;
94+
static volatile bool m_tx_in_progress;
9195
static volatile bool m_write_done;
9296

93-
static volatile ble_drv_adv_evt_callback_t adv_event_handler;
94-
static volatile ble_drv_gattc_evt_callback_t gattc_event_handler;
95-
static volatile ble_drv_gattc_char_data_callback_t gattc_char_data_handle;
96-
97-
static bleio_scanner_obj_t *mp_adv_observer;
98-
static bleio_device_obj_t *mp_gattc_observer;
99-
static bleio_device_obj_t *mp_gattc_disc_service_observer;
100-
static bleio_service_obj_t *mp_gattc_disc_char_observer;
101-
static bleio_characteristic_obj_t *mp_gattc_char_data_observer;
102-
static bleio_address_obj_t *mp_connect_address;
97+
nrf_nvic_state_t nrf_nvic_state = { 0 };
10398

10499
#if (BLUETOOTH_SD == 140)
105100
static uint8_t m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
@@ -112,18 +107,11 @@ static ble_data_t m_scan_buffer =
112107
};
113108
#endif
114109

115-
#include "nrf_nvic.h"
116-
117-
nrf_nvic_state_t nrf_nvic_state = {0};
118-
119110
void softdevice_assert_handler(uint32_t id, uint32_t pc, uint32_t info) {
120111
BLE_DRIVER_LOG("ERROR: SoftDevice assert!!!");
121112
}
122113

123114
uint32_t ble_drv_stack_enable(void) {
124-
m_adv_in_progress = false;
125-
m_tx_in_progress = false;
126-
127115
nrf_clock_lf_cfg_t clock_config = {
128116
.source = NRF_CLOCK_LF_SRC_XTAL,
129117
.rc_ctiv = 0,
@@ -173,10 +161,6 @@ uint32_t ble_drv_stack_enable(void) {
173161
return err_code;
174162
}
175163

176-
void ble_drv_stack_disable(void) {
177-
sd_softdevice_disable();
178-
}
179-
180164
uint8_t ble_drv_stack_enabled(void) {
181165
uint8_t is_enabled;
182166
uint32_t err_code = sd_softdevice_is_enabled(&is_enabled);
@@ -549,24 +533,25 @@ bool ble_drv_advertise_data(bleio_advertisement_data_t *adv_params) {
549533
translate("Can not start advertisement. status: 0x%02x"), (uint16_t)err_code));
550534
}
551535

552-
m_adv_in_progress = true;
553-
554536
return true;
555537
}
556538

557539
void ble_drv_advertise_stop(void) {
558-
if (m_adv_in_progress == true) {
559-
uint32_t err_code;
540+
uint32_t err_code;
541+
560542
#if (BLUETOOTH_SD == 140)
561-
if ((err_code = sd_ble_gap_adv_stop(m_adv_handle)) != 0) {
543+
if (m_adv_handle == BLE_GAP_ADV_SET_HANDLE_NOT_SET)
544+
return;
545+
546+
err_code = sd_ble_gap_adv_stop(m_adv_handle);
562547
#else
563-
if ((err_code = sd_ble_gap_adv_stop()) != 0) {
548+
err_code = sd_ble_gap_adv_stop();
564549
#endif
565-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
566-
translate("Can not stop advertisement. status: 0x%02x"), (uint16_t)err_code));
567-
}
550+
551+
if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_INVALID_STATE)) {
552+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
553+
translate("Can not stop advertisement. status: 0x%02x"), (uint16_t)err_code));
568554
}
569-
m_adv_in_progress = false;
570555
}
571556

572557
void ble_drv_attr_s_read(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data) {
@@ -644,30 +629,24 @@ void ble_drv_gatts_event_handler_set(bleio_device_obj_t *device, ble_drv_gatts_e
644629
gatts_event_handler = evt_handler;
645630
}
646631

647-
void ble_drv_gattc_event_handler_set(bleio_device_obj_t *device, ble_drv_gattc_evt_callback_t evt_handler) {
648-
mp_gattc_observer = device;
649-
gattc_event_handler = evt_handler;
650-
}
651-
652632
void ble_drv_adv_report_handler_set(bleio_scanner_obj_t *device, ble_drv_adv_evt_callback_t evt_handler) {
653633
mp_adv_observer = device;
654634
adv_event_handler = evt_handler;
655635
}
656636

657-
void ble_drv_attr_c_read(bleio_characteristic_obj_t *characteristic, ble_drv_gattc_char_data_callback_t cb) {
637+
void ble_drv_attr_c_read(bleio_characteristic_obj_t *characteristic) {
658638
bleio_service_obj_t *service = characteristic->service;
659639
bleio_device_obj_t *device = MP_OBJ_TO_PTR(service->device);
660640

661641
mp_gattc_char_data_observer = characteristic;
662-
gattc_char_data_handle = cb;
663642

664643
const uint32_t err_code = sd_ble_gattc_read(device->conn_handle, characteristic->handle, 0);
665644
if (err_code != 0) {
666645
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
667646
translate("Can not read attribute value. status: 0x%02x"), (uint16_t)err_code));
668647
}
669648

670-
while (gattc_char_data_handle != NULL) {
649+
while (mp_gattc_char_data_observer != NULL) {
671650
#ifdef MICROPY_VM_HOOK_LOOP
672651
MICROPY_VM_HOOK_LOOP
673652
#endif
@@ -939,13 +918,18 @@ STATIC void on_characteristic_discovery_rsp(ble_gattc_evt_char_disc_rsp_t *respo
939918
mp_gattc_disc_char_observer = NULL;
940919
}
941920

921+
STATIC void on_read_rsp(ble_gattc_evt_read_rsp_t *response) {
922+
mp_gattc_char_data_observer->value_data = mp_obj_new_bytearray(response->len, response->data);
923+
924+
mp_gattc_char_data_observer = NULL;
925+
}
926+
942927
STATIC void ble_evt_handler(ble_evt_t *p_ble_evt) {
943928
printf("%s - 0x%02X\r\n", __func__, p_ble_evt->header.evt_id);
944929

945930
switch (p_ble_evt->header.evt_id) {
946931
case BLE_GAP_EVT_CONNECTED:
947932
BLE_DRIVER_LOG("GAP CONNECT\n");
948-
m_adv_in_progress = false;
949933
gap_event_handler(mp_gap_observer, p_ble_evt->header.evt_id, p_ble_evt->evt.gap_evt.conn_handle, p_ble_evt->header.evt_len - (2 * sizeof(uint16_t)), NULL);
950934

951935
ble_gap_conn_params_t conn_params;
@@ -1025,17 +1009,7 @@ STATIC void ble_evt_handler(ble_evt_t *p_ble_evt) {
10251009
break;
10261010

10271011
case BLE_GATTC_EVT_READ_RSP:
1028-
BLE_DRIVER_LOG("BLE EVT READ RESPONSE, offset: 0x"HEX2_FMT", length: 0x"HEX2_FMT"\n",
1029-
p_ble_evt->evt.gattc_evt.params.read_rsp.offset,
1030-
p_ble_evt->evt.gattc_evt.params.read_rsp.len);
1031-
1032-
gattc_char_data_handle(mp_gattc_char_data_observer,
1033-
p_ble_evt->evt.gattc_evt.params.read_rsp.len,
1034-
p_ble_evt->evt.gattc_evt.params.read_rsp.data);
1035-
1036-
// mark end of read
1037-
gattc_char_data_handle = NULL;
1038-
1012+
on_read_rsp(&p_ble_evt->evt.gattc_evt.params.read_rsp);
10391013
break;
10401014

10411015
case BLE_GATTC_EVT_WRITE_RSP:

ports/nrf/drivers/bluetooth/ble_drv.h

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,17 @@
2929

3030
#if BLUETOOTH_SD
3131

32-
#include <stdint.h>
33-
#include <stdbool.h>
34-
35-
#include "shared-module/bleio/AdvertisementData.h"
36-
#include "shared-module/bleio/Characteristic.h"
37-
#include "shared-module/bleio/Device.h"
32+
#include "shared-bindings/bleio/Device.h"
33+
#include "shared-bindings/bleio/Scanner.h"
34+
#include "shared-bindings/bleio/Service.h"
3835
#include "shared-module/bleio/ScanEntry.h"
39-
#include "shared-module/bleio/Scanner.h"
40-
#include "shared-module/bleio/Service.h"
4136

4237
typedef void (*ble_drv_gap_evt_callback_t)(bleio_device_obj_t *device, uint16_t event_id, uint16_t conn_handle, uint16_t length, uint8_t * data);
4338
typedef void (*ble_drv_gatts_evt_callback_t)(bleio_device_obj_t *device, uint16_t event_id, uint16_t attr_handle, uint16_t length, uint8_t * data);
44-
typedef void (*ble_drv_gattc_evt_callback_t)(bleio_device_obj_t *device, uint16_t event_id, uint16_t attr_handle, uint16_t length, uint8_t * data);
4539
typedef void (*ble_drv_adv_evt_callback_t)(bleio_scanner_obj_t *scanner, bleio_scanentry_obj_t *entry);
46-
typedef void (*ble_drv_gattc_char_data_callback_t)(bleio_characteristic_obj_t *self, uint16_t length, uint8_t * p_data);
4740

4841
uint32_t ble_drv_stack_enable(void);
4942

50-
void ble_drv_stack_disable(void);
51-
5243
uint8_t ble_drv_stack_enabled(void);
5344

5445
void ble_drv_address_get(bleio_address_obj_t *address);
@@ -67,11 +58,9 @@ void ble_drv_gap_event_handler_set(bleio_device_obj_t *device, ble_drv_gap_evt_c
6758

6859
void ble_drv_gatts_event_handler_set(bleio_device_obj_t *device, ble_drv_gatts_evt_callback_t evt_handler);
6960

70-
void ble_drv_gattc_event_handler_set(bleio_device_obj_t *device, ble_drv_gattc_evt_callback_t evt_handler);
71-
7261
void ble_drv_attr_s_read(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data);
7362

74-
void ble_drv_attr_c_read(bleio_characteristic_obj_t *characteristic, ble_drv_gattc_char_data_callback_t cb);
63+
void ble_drv_attr_c_read(bleio_characteristic_obj_t *characteristic);
7564

7665
void ble_drv_attr_s_write(bleio_characteristic_obj_t *characteristic, mp_buffer_info_t *bufinfo);
7766

0 commit comments

Comments
 (0)