Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watch for ctrl-c over BLE workflow serial #7465

Merged
merged 2 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 17 additions & 3 deletions ports/espressif/common-hal/_bleio/CharacteristicBuffer.c
Expand Up @@ -49,7 +49,19 @@ STATIC int characteristic_buffer_on_ble_evt(struct ble_gap_event *event, void *p
event->notify_rx.attr_handle == self->characteristic->handle) {
const struct os_mbuf *m = event->notify_rx.om;
while (m != NULL) {
ringbuf_put_n(&self->ringbuf, m->om_data, m->om_len);
const uint8_t *data = m->om_data;
uint16_t len = m->om_len;
if (self->watch_for_interrupt_char) {
for (uint16_t i = 0; i < len; i++) {
if (data[i] == mp_interrupt_char) {
mp_sched_keyboard_interrupt();
} else {
ringbuf_put(&self->ringbuf, data[i]);
}
}
} else {
ringbuf_put_n(&self->ringbuf, data, len);
}
m = SLIST_NEXT(m, om_next);
}
}
Expand All @@ -69,9 +81,11 @@ void _common_hal_bleio_characteristic_buffer_construct(bleio_characteristic_buff
bleio_characteristic_obj_t *characteristic,
mp_float_t timeout,
uint8_t *buffer, size_t buffer_size,
void *static_handler_entry) {
void *static_handler_entry,
bool watch_for_interrupt_char) {
tannewt marked this conversation as resolved.
Show resolved Hide resolved
self->characteristic = characteristic;
self->timeout_ms = timeout * 1000;
self->watch_for_interrupt_char = watch_for_interrupt_char;
ringbuf_init(&self->ringbuf, buffer, buffer_size);

if (static_handler_entry != NULL) {
Expand All @@ -87,7 +101,7 @@ void common_hal_bleio_characteristic_buffer_construct(bleio_characteristic_buffe
mp_float_t timeout,
size_t buffer_size) {
uint8_t *buffer = m_malloc(buffer_size, true);
_common_hal_bleio_characteristic_buffer_construct(self, characteristic, timeout, buffer, buffer_size, NULL);
_common_hal_bleio_characteristic_buffer_construct(self, characteristic, timeout, buffer, buffer_size, NULL, false);
}

uint32_t common_hal_bleio_characteristic_buffer_read(bleio_characteristic_buffer_obj_t *self, uint8_t *data, size_t len, int *errcode) {
Expand Down
3 changes: 3 additions & 0 deletions ports/espressif/common-hal/_bleio/CharacteristicBuffer.h
Expand Up @@ -27,6 +27,8 @@
#ifndef MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H
#define MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H

#include <stdbool.h>

#include "py/ringbuf.h"
#include "shared-bindings/_bleio/Characteristic.h"

Expand All @@ -36,6 +38,7 @@ typedef struct {
uint32_t timeout_ms;
// Ring buffer storing consecutive incoming values.
ringbuf_t ringbuf;
bool watch_for_interrupt_char;
} bleio_characteristic_buffer_obj_t;

#endif // MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H
18 changes: 15 additions & 3 deletions ports/nrf/common-hal/_bleio/CharacteristicBuffer.c
Expand Up @@ -45,7 +45,17 @@
STATIC void write_to_ringbuf(bleio_characteristic_buffer_obj_t *self, uint8_t *data, uint16_t len) {
uint8_t is_nested_critical_region;
sd_nvic_critical_region_enter(&is_nested_critical_region);
ringbuf_put_n(&self->ringbuf, data, len);
if (self->watch_for_interrupt_char) {
for (uint16_t i = 0; i < len; i++) {
if (data[i] == mp_interrupt_char) {
mp_sched_keyboard_interrupt();
} else {
ringbuf_put(&self->ringbuf, data[i]);
}
}
} else {
ringbuf_put_n(&self->ringbuf, data, len);
}
sd_nvic_critical_region_exit(is_nested_critical_region);
}

Expand Down Expand Up @@ -85,10 +95,12 @@ void _common_hal_bleio_characteristic_buffer_construct(bleio_characteristic_buff
bleio_characteristic_obj_t *characteristic,
mp_float_t timeout,
uint8_t *buffer, size_t buffer_size,
void *static_handler_entry) {
void *static_handler_entry,
bool watch_for_interrupt_char) {

self->characteristic = characteristic;
self->timeout_ms = timeout * 1000;
self->watch_for_interrupt_char = watch_for_interrupt_char;

ringbuf_init(&self->ringbuf, buffer, buffer_size);

Expand All @@ -105,7 +117,7 @@ void common_hal_bleio_characteristic_buffer_construct(bleio_characteristic_buffe
mp_float_t timeout,
size_t buffer_size) {
uint8_t *buffer = m_malloc(buffer_size, true);
_common_hal_bleio_characteristic_buffer_construct(self, characteristic, timeout, buffer, buffer_size, NULL);
_common_hal_bleio_characteristic_buffer_construct(self, characteristic, timeout, buffer, buffer_size, NULL, false);
}

uint32_t common_hal_bleio_characteristic_buffer_read(bleio_characteristic_buffer_obj_t *self, uint8_t *data, size_t len, int *errcode) {
Expand Down
3 changes: 3 additions & 0 deletions ports/nrf/common-hal/_bleio/CharacteristicBuffer.h
Expand Up @@ -27,6 +27,8 @@
#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H
#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H

#include <stdbool.h>

#include "nrf_soc.h"

#include "py/ringbuf.h"
Expand All @@ -38,6 +40,7 @@ typedef struct {
uint32_t timeout_ms;
// Ring buffer storing consecutive incoming values.
ringbuf_t ringbuf;
bool watch_for_interrupt_char;
} bleio_characteristic_buffer_obj_t;

#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H
5 changes: 4 additions & 1 deletion shared-bindings/_bleio/CharacteristicBuffer.h
Expand Up @@ -27,6 +27,8 @@
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_CHARACTERISTICBUFFER_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_CHARACTERISTICBUFFER_H

#include <stdbool.h>

#include "common-hal/_bleio/CharacteristicBuffer.h"

extern const mp_obj_type_t bleio_characteristic_buffer_type;
Expand All @@ -35,7 +37,8 @@ void _common_hal_bleio_characteristic_buffer_construct(bleio_characteristic_buff
bleio_characteristic_obj_t *characteristic,
mp_float_t timeout,
uint8_t *buffer, size_t buffer_size,
void *static_handler_entry);
void *static_handler_entry,
bool watch_for_interrupt_char);
void common_hal_bleio_characteristic_buffer_construct(bleio_characteristic_buffer_obj_t *self,
bleio_characteristic_obj_t *characteristic,
mp_float_t timeout,
Expand Down
3 changes: 2 additions & 1 deletion supervisor/shared/bluetooth/serial.c
Expand Up @@ -146,7 +146,8 @@ void supervisor_start_bluetooth_serial(void) {
&supervisor_ble_circuitpython_rx_characteristic,
0.1f,
(uint8_t *)_incoming, sizeof(_incoming) * sizeof(uint32_t),
&rx_static_handler_entry);
&rx_static_handler_entry,
true /* watch for interrupt character */);

_enabled = true;
}
Expand Down