Skip to content

Commit

Permalink
Merge 027d780 into 6e0f9b9
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgeorge committed Apr 20, 2021
2 parents 6e0f9b9 + 027d780 commit 7f7b6c3
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
61 changes: 60 additions & 1 deletion extmod/modbluetooth.c
Expand Up @@ -34,6 +34,7 @@
#include "py/objarray.h"
#include "py/qstr.h"
#include "py/runtime.h"
#include "py/stackctrl.h"
#include "extmod/modbluetooth.h"
#include <string.h>

Expand Down Expand Up @@ -1111,7 +1112,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(bluetooth_ble_invoke_irq_obj, bluetooth_ble_inv

#if MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS

STATIC mp_obj_t invoke_irq_handler(uint16_t event,
STATIC mp_obj_t invoke_irq_handler_run(uint16_t event,
const mp_int_t *numeric, size_t n_unsigned, size_t n_signed,
const uint8_t *addr,
const mp_obj_bluetooth_uuid_t *uuid,
Expand Down Expand Up @@ -1161,6 +1162,64 @@ STATIC mp_obj_t invoke_irq_handler(uint16_t event,
return result;
}

#if MICROPY_PY_THREAD

#if MICROPY_ENABLE_PYSTACK
#error not supported
#endif

STATIC mp_obj_t invoke_irq_handler(uint16_t event,
const mp_int_t *numeric, size_t n_unsigned, size_t n_signed,
const uint8_t *addr,
const mp_obj_bluetooth_uuid_t *uuid,
const uint8_t **data, size_t *data_len, size_t n_data) {

// This code may run on an existing MicroPython thread, or a non-MicroPython thread
// that's not using the mp_thread_get_state() value. In the former case the state
// must be restored once this callback finishes.
mp_state_thread_t *ts_orig = mp_thread_get_state();

mp_state_thread_t ts;
if (ts_orig == NULL) {
mp_thread_set_state(&ts);
mp_stack_set_top(&ts + 1); // need to include ts in root-pointer scan
mp_stack_set_limit(MICROPY_PY_BLUETOOTH_IRQ_STACK_SIZE - 1024);
mp_locals_set(mp_state_ctx.thread.dict_locals); // set from the outer context
mp_globals_set(mp_state_ctx.thread.dict_globals); // set from the outer context
MP_THREAD_GIL_ENTER();
}

mp_obj_t result = mp_const_none;
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
result = invoke_irq_handler_run(event, numeric, n_unsigned, n_signed, addr, uuid, data, data_len, n_data);
nlr_pop();
} else {
// Uncaught exception, print it out.
mp_printf(MICROPY_ERROR_PRINTER, "Unhandled exception in IRQ callback handler\n");
mp_obj_print_exception(MICROPY_ERROR_PRINTER, MP_OBJ_FROM_PTR(nlr.ret_val));
}

if (ts_orig == NULL) {
MP_THREAD_GIL_EXIT();
mp_thread_set_state(ts_orig);
}

return result;
}

#else

STATIC mp_obj_t invoke_irq_handler(uint16_t event,
const mp_int_t *numeric, size_t n_unsigned, size_t n_signed,
const uint8_t *addr,
const mp_obj_bluetooth_uuid_t *uuid,
const uint8_t **data, size_t *data_len, size_t n_data) {
return invoke_irq_handler_run(event, numeric, n_unsigned, n_signed, addr, uuid, data, data_len, n_data);
}

#endif

#define NULL_NUMERIC NULL
#define NULL_ADDR NULL
#define NULL_UUID NULL
Expand Down
13 changes: 8 additions & 5 deletions extmod/nimble/modbluetooth_nimble.c
Expand Up @@ -1019,7 +1019,8 @@ int mp_bluetooth_gatts_notify(uint16_t conn_handle, uint16_t value_handle) {
}
// Confusingly, notify/notify_custom/indicate are "gattc" function (even though they're used by peripherals (i.e. gatt servers)).
// See https://www.mail-archive.com/dev@mynewt.apache.org/msg01293.html
return ble_hs_err_to_errno(ble_gattc_notify(conn_handle, value_handle));
int ret = ble_hs_err_to_errno(ble_gattc_notify(conn_handle, value_handle));
return ret;
}

int mp_bluetooth_gatts_notify_send(uint16_t conn_handle, uint16_t value_handle, const uint8_t *value, size_t value_len) {
Expand All @@ -1040,7 +1041,8 @@ int mp_bluetooth_gatts_indicate(uint16_t conn_handle, uint16_t value_handle) {
}
// This will raise BLE_GAP_EVENT_NOTIFY_TX with a status when it is
// acknowledged (or timeout/error).
return ble_hs_err_to_errno(ble_gattc_indicate(conn_handle, value_handle));
int ret = ble_hs_err_to_errno(ble_gattc_indicate(conn_handle, value_handle));
return ret;
}

int mp_bluetooth_gatts_set_buffer(uint16_t value_handle, size_t len, bool append) {
Expand Down Expand Up @@ -1070,7 +1072,8 @@ int mp_bluetooth_set_preferred_mtu(uint16_t mtu) {
#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
int mp_bluetooth_gap_pair(uint16_t conn_handle) {
DEBUG_printf("mp_bluetooth_gap_pair: conn_handle=%d\n", conn_handle);
return ble_hs_err_to_errno(ble_gap_security_initiate(conn_handle));
int ret = ble_hs_err_to_errno(ble_gap_security_initiate(conn_handle));
return ret;
}

int mp_bluetooth_gap_passkey(uint16_t conn_handle, uint8_t action, mp_int_t passkey) {
Expand Down Expand Up @@ -1791,9 +1794,9 @@ STATIC int ble_store_ram_read(int obj_type, const union ble_store_key *key, unio
case BLE_STORE_OBJ_TYPE_OUR_SEC: {
// <type=our,addr,ediv_rand>
// Find our secret for this remote device, matching this ediv/rand key.
assert(ble_addr_cmp(&key->sec.peer_addr, BLE_ADDR_ANY)); // Must have address.
// TODO: Support cases where key->sec.peer_addr is BLE_ADDR_ANY, and
// key->sec.ediv_rand_present is false.
assert(key->sec.idx == 0);
assert(key->sec.ediv_rand_present);
key_data = (const uint8_t *)&key->sec.peer_addr;
key_data_len = sizeof(ble_addr_t);
break;
Expand Down
6 changes: 5 additions & 1 deletion ports/esp32/main/CMakeLists.txt
Expand Up @@ -172,11 +172,15 @@ target_compile_options(${MICROPY_TARGET} PUBLIC
-Wno-missing-field-initializers
)

# Additional include directories needed for private NimBLE headers.
target_include_directories(${MICROPY_TARGET} PUBLIC
${IDF_PATH}/components/bt/host/nimble/nimble
)

# Add additional extmod and usermod components.
target_link_libraries(${MICROPY_TARGET} micropy_extmod_btree)
target_link_libraries(${MICROPY_TARGET} usermod)


# Collect all of the include directories and compile definitions for the IDF components.
foreach(comp ${IDF_COMPONENTS})
micropy_gather_target_properties(__idf_${comp})
Expand Down
3 changes: 3 additions & 0 deletions ports/esp32/mpconfigport.h
Expand Up @@ -124,7 +124,10 @@
// extended modules
#ifndef MICROPY_PY_BLUETOOTH
#define MICROPY_PY_BLUETOOTH (1)
#define MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS (1)
#define MICROPY_PY_BLUETOOTH_IRQ_STACK_SIZE (CONFIG_BT_NIMBLE_TASK_STACK_SIZE)
#define MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE (1)
#define MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING (1)
#define MICROPY_BLUETOOTH_NIMBLE (1)
#define MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY (1)
#endif
Expand Down

0 comments on commit 7f7b6c3

Please sign in to comment.