Skip to content

Commit e3fa6e2

Browse files
authored
Merge pull request #5089 from tannewt/disable_ble_workflow
Add ability to disable BLE workflow
2 parents 4bcf0d4 + 713c8e7 commit e3fa6e2

File tree

10 files changed

+270
-21
lines changed

10 files changed

+270
-21
lines changed

main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,7 @@ int __attribute__((used)) main(void) {
832832
serial_init();
833833

834834
#if CIRCUITPY_BLEIO
835+
supervisor_bluetooth_enable_workflow();
835836
supervisor_start_bluetooth();
836837
#endif
837838

ports/nrf/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ CFLAGS += \
120120
# TODO: check this
121121
CFLAGS += -D__START=main
122122

123-
LDFLAGS = $(CFLAGS) -nostartfiles -Wl,-nostdlib -Wl,-T,$(GENERATED_LD_FILE) -Wl,-Map=$@.map -Wl,-cref -Wl,-gc-sections -specs=nano.specs
123+
LDFLAGS = $(CFLAGS) -nostartfiles -Wl,-nostdlib -Wl,-z,max-page-size=0x1000 -Wl,-T,$(GENERATED_LD_FILE) -Wl,-Map=$@.map -Wl,-cref -Wl,-gc-sections -specs=nano.specs
124124
LIBS := -lgcc -lc
125125

126126
LDFLAGS += -mthumb -mcpu=cortex-m4

shared-bindings/supervisor/__init__.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "lib/utils/interrupt_char.h"
3434
#include "supervisor/shared/autoreload.h"
35+
#include "supervisor/shared/bluetooth/bluetooth.h"
3536
#include "supervisor/shared/status_leds.h"
3637
#include "supervisor/shared/stack.h"
3738
#include "supervisor/shared/traceback.h"
@@ -283,6 +284,21 @@ STATIC mp_obj_t supervisor_get_previous_traceback(void) {
283284
}
284285
MP_DEFINE_CONST_FUN_OBJ_0(supervisor_get_previous_traceback_obj, supervisor_get_previous_traceback);
285286

287+
//| def disable_ble_workflow() -> None:
288+
//| """Disable ble workflow until a reset. This prevents BLE advertising outside of the VM and
289+
//| the services used for it."""
290+
//| ...
291+
//|
292+
STATIC mp_obj_t supervisor_disable_ble_workflow(void) {
293+
#if !CIRCUITPY_BLE_FILE_SERVICE && !CIRCUITPY_SERIAL_BLE
294+
mp_raise_NotImplementedError(NULL);
295+
#else
296+
supervisor_bluetooth_disable_workflow();
297+
#endif
298+
return mp_const_none;
299+
}
300+
MP_DEFINE_CONST_FUN_OBJ_0(supervisor_disable_ble_workflow_obj, supervisor_disable_ble_workflow);
301+
286302
STATIC const mp_rom_map_elem_t supervisor_module_globals_table[] = {
287303
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_supervisor) },
288304
{ MP_ROM_QSTR(MP_QSTR_enable_autoreload), MP_ROM_PTR(&supervisor_enable_autoreload_obj) },
@@ -295,6 +311,7 @@ STATIC const mp_rom_map_elem_t supervisor_module_globals_table[] = {
295311
{ MP_ROM_QSTR(MP_QSTR_set_next_code_file), MP_ROM_PTR(&supervisor_set_next_code_file_obj) },
296312
{ MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&supervisor_ticks_ms_obj) },
297313
{ MP_ROM_QSTR(MP_QSTR_get_previous_traceback), MP_ROM_PTR(&supervisor_get_previous_traceback_obj) },
314+
{ MP_ROM_QSTR(MP_QSTR_disable_ble_workflow), MP_ROM_PTR(&supervisor_disable_ble_workflow_obj) },
298315
};
299316

300317
STATIC MP_DEFINE_CONST_DICT(supervisor_module_globals, supervisor_module_globals_table);

shared-module/terminalio/Terminal.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ void common_hal_terminalio_terminal_construct(terminalio_terminal_obj_t *self, d
4646
}
4747

4848
size_t common_hal_terminalio_terminal_write(terminalio_terminal_obj_t *self, const byte *data, size_t len, int *errcode) {
49+
// Make sure the terminal is initialized before we do anything with it.
50+
if (self->tilegrid == NULL) {
51+
return len;
52+
}
4953
const byte *i = data;
5054
uint16_t start_y = self->cursor_y;
5155
while (i < data + len) {
@@ -169,5 +173,5 @@ size_t common_hal_terminalio_terminal_write(terminalio_terminal_obj_t *self, con
169173
}
170174

171175
bool common_hal_terminalio_terminal_ready_to_tx(terminalio_terminal_obj_t *self) {
172-
return true;
176+
return self->tilegrid != NULL;
173177
}

supervisor/shared/bluetooth/bluetooth.c

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,23 @@ uint8_t circuitpython_scan_response_data[] = {
8383
#endif
8484
};
8585

86-
bool boot_in_discovery_mode = false;
87-
bool advertising = false;
86+
87+
#if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
88+
STATIC bool boot_in_discovery_mode = false;
89+
STATIC bool advertising = false;
90+
STATIC bool ble_started = false;
91+
92+
#define WORKFLOW_UNSET 0
93+
#define WORKFLOW_ENABLED 1
94+
#define WORKFLOW_DISABLED 2
95+
96+
STATIC uint8_t workflow_state = WORKFLOW_UNSET;
97+
STATIC bool was_connected = false;
8898

8999
STATIC void supervisor_bluetooth_start_advertising(void) {
90-
#if !CIRCUITPY_BLE_FILE_SERVICE && !CIRCUITPY_SERIAL_BLE
91-
return;
92-
#else
100+
if (workflow_state != WORKFLOW_ENABLED) {
101+
return;
102+
}
93103
bool is_connected = common_hal_bleio_adapter_get_connected(&common_hal_bleio_adapter_obj);
94104
if (is_connected) {
95105
return;
@@ -130,16 +140,15 @@ STATIC void supervisor_bluetooth_start_advertising(void) {
130140
NULL);
131141
// This may fail if we are already advertising.
132142
advertising = status == NRF_SUCCESS;
133-
#endif
134143
}
135144

145+
#endif // CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
146+
136147
#define BLE_DISCOVERY_DATA_GUARD 0xbb0000bb
137148
#define BLE_DISCOVERY_DATA_GUARD_MASK 0xff0000ff
138149

139150
void supervisor_bluetooth_init(void) {
140-
#if !CIRCUITPY_BLE_FILE_SERVICE && !CIRCUITPY_SERIAL_BLE
141-
return;
142-
#endif
151+
#if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
143152
uint32_t reset_state = port_get_saved_word();
144153
uint32_t ble_mode = 0;
145154
if ((reset_state & BLE_DISCOVERY_DATA_GUARD_MASK) == BLE_DISCOVERY_DATA_GUARD) {
@@ -209,10 +218,14 @@ void supervisor_bluetooth_init(void) {
209218
status_led_deinit();
210219
#endif
211220
port_set_saved_word(reset_state);
221+
#endif
212222
}
213223

214-
STATIC bool was_connected;
215224
void supervisor_bluetooth_background(void) {
225+
#if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
226+
if (!ble_started) {
227+
return;
228+
}
216229
bool is_connected = common_hal_bleio_adapter_get_connected(&common_hal_bleio_adapter_obj);
217230
if (was_connected && !is_connected) {
218231
#if CIRCUITPY_BLE_FILE_SERVICE
@@ -228,12 +241,15 @@ void supervisor_bluetooth_background(void) {
228241
#if CIRCUITPY_BLE_FILE_SERVICE
229242
supervisor_bluetooth_file_transfer_background();
230243
#endif
244+
#endif
231245
}
232246

233247
void supervisor_start_bluetooth(void) {
234-
#if !CIRCUITPY_BLE_FILE_SERVICE && !CIRCUITPY_SERIAL_BLE
235-
return;
236-
#endif
248+
#if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
249+
250+
if (workflow_state != WORKFLOW_ENABLED) {
251+
return;
252+
}
237253

238254
common_hal_bleio_adapter_set_enabled(&common_hal_bleio_adapter_obj, true);
239255

@@ -245,16 +261,41 @@ void supervisor_start_bluetooth(void) {
245261
supervisor_start_bluetooth_serial();
246262
#endif
247263

248-
// Kick off advertisments
264+
// Mark as started so that the background call does something.
265+
ble_started = true;
266+
267+
// Kick off advertisements
249268
supervisor_bluetooth_background();
269+
270+
#endif
250271
}
251272

252273
void supervisor_stop_bluetooth(void) {
253-
#if !CIRCUITPY_BLE_FILE_SERVICE && !CIRCUITPY_SERIAL_BLE
254-
return;
255-
#endif
274+
#if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
275+
276+
if (!ble_started && workflow_state != WORKFLOW_ENABLED) {
277+
return;
278+
}
256279

257280
#if CIRCUITPY_SERIAL_BLE
258281
supervisor_stop_bluetooth_serial();
259282
#endif
283+
284+
#endif
285+
}
286+
287+
void supervisor_bluetooth_enable_workflow(void) {
288+
#if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
289+
if (workflow_state == WORKFLOW_DISABLED) {
290+
return;
291+
}
292+
293+
workflow_state = WORKFLOW_ENABLED;
294+
#endif
295+
}
296+
297+
void supervisor_bluetooth_disable_workflow(void) {
298+
#if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
299+
workflow_state = WORKFLOW_DISABLED;
300+
#endif
260301
}

supervisor/shared/bluetooth/bluetooth.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,8 @@ void supervisor_bluetooth_init(void);
3434
void supervisor_start_bluetooth(void);
3535
void supervisor_stop_bluetooth(void);
3636

37+
// Enable only works if it hasn't been set yet.
38+
void supervisor_bluetooth_enable_workflow(void);
39+
void supervisor_bluetooth_disable_workflow(void);
40+
3741
#endif // MICROPY_INCLUDED_SUPERVISOR_SHARED_BLUETOOTH_H

supervisor/shared/bluetooth/serial.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ void ble_serial_write(const char *text, size_t len) {
166166
}
167167
size_t sent = 0;
168168
while (sent < len) {
169-
uint16_t packet_size = MIN(len, (size_t)common_hal_bleio_packet_buffer_get_outgoing_packet_length(&_tx_packet_buffer));
169+
uint16_t packet_size = MIN(len - sent, (size_t)common_hal_bleio_packet_buffer_get_outgoing_packet_length(&_tx_packet_buffer));
170170
mp_int_t written = common_hal_bleio_packet_buffer_write(&_tx_packet_buffer, (const uint8_t *)text + sent, packet_size, NULL, 0);
171171
// Error, so we drop characters to transmit.
172172
if (written < 0) {

supervisor/shared/display.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ void supervisor_stop_terminal(void) {
123123
free_memory(tilegrid_tiles);
124124
tilegrid_tiles = NULL;
125125
supervisor_terminal_text_grid.tiles = NULL;
126+
supervisor_terminal.tilegrid = NULL;
126127
}
127128
#endif
128129
}

tools/gen_display_resources.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def _load_row(self, y, row):
228228
.font = &supervisor_terminal_font,
229229
.cursor_x = 0,
230230
.cursor_y = 0,
231-
.tilegrid = &supervisor_terminal_text_grid
231+
.tilegrid = NULL
232232
};
233233
"""
234234
)

0 commit comments

Comments
 (0)