Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"gcc",
"MICROPY_MODULE_FROZEN_MPY",
"MICROPY_ROM_TEXT_COMPRESSION",
"HAVE_UMM_MALLOC=1"
],
"compilerArgs": [
"-Wall",
Expand Down Expand Up @@ -286,7 +287,8 @@
],
"defines": [
"MICROPY_MODULE_FROZEN_MPY",
"MICROPY_ROM_TEXT_COMPRESSION"
"MICROPY_ROM_TEXT_COMPRESSION",
"HAVE_UMM_MALLOC=1"
],
"compilerArgs": [
"-mthumb",
Expand Down Expand Up @@ -404,7 +406,8 @@
],
"defines": [
"MICROPY_MODULE_FROZEN_MPY",
"MICROPY_USE_READLINE=1"
"MICROPY_USE_READLINE=1",
"HAVE_UMM_MALLOC=1",
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
Expand Down
10 changes: 10 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@
"reveal": "always"
},
"problemMatcher": []
},
{
"label": "build ev3",
"type": "shell",
"command": "poetry run make ev3",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
13 changes: 12 additions & 1 deletion bricks/_common/common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,16 @@ BTSTACK_BLE_SRC_C += $(addprefix lib/btstack/src/ble/,\
sm.c \
)

BTSTACK_CLASSIC_SRC_C = $(addprefix lib/btstack/src/classic/,\
btstack_link_key_db_memory.c \
rfcomm.c \
sdp_client.c \
sdp_client_rfcomm.c \
sdp_server.c \
sdp_util.c \
spp_server.c \
)

BTSTACK_SRC_C += $(addprefix lib/btstack/chipset/cc256x/,\
btstack_chipset_cc256x.c \
)
Expand Down Expand Up @@ -519,7 +529,7 @@ SRC_STM32_USB_DEV += $(addprefix lib/pbio/drv/usb/stm32_usbd/,\
SRC_UMM_MALLOC = lib/umm_malloc/src/umm_malloc.c

ifeq ($(PB_LIB_UMM_MALLOC),1)
CFLAGS += -I$(PBTOP)/lib/umm_malloc/src
CFLAGS += -I$(PBTOP)/lib/umm_malloc/src -DHAVE_UMM_MALLOC=1
endif

# NXT OS
Expand Down Expand Up @@ -579,6 +589,7 @@ ifeq ($(PB_LIB_BTSTACK),1)
ifneq ($(CI_MODE),1)
OBJ += $(addprefix $(BUILD)/, $(BTSTACK_SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(BTSTACK_BLE_SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(BTSTACK_CLASSIC_SRC_C:.c=.o))
$(BUILD)/lib/btstack/%.o: CFLAGS += -Wno-error
endif
endif
Expand Down
1 change: 1 addition & 0 deletions bricks/_common/sources.mk
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ PBIO_SRC_C = $(addprefix lib/pbio/,\
drv/block_device/block_device_w25qxx_stm32.c \
drv/bluetooth/bluetooth.c \
drv/bluetooth/bluetooth_btstack.c \
drv/bluetooth/rfcomm_btstack.c \
drv/bluetooth/bluetooth_btstack_ev3.c \
drv/bluetooth/bluetooth_btstack_posix.c \
drv/bluetooth/bluetooth_btstack_stm32_hal.c \
Expand Down
2 changes: 1 addition & 1 deletion bricks/primehub/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#define PYBRICKS_PY_IODEVICES_UART_DEVICE (1)
#define PYBRICKS_PY_IODEVICES_XBOX_CONTROLLER (1)
#define PYBRICKS_PY_MEDIA_IMAGE (0)
#define PYBRICKS_PY_MESSAGING (1)
#define PYBRICKS_PY_MESSAGING (0)
#define PYBRICKS_PY_MESSAGING_RFCOMM (0)
#define PYBRICKS_PY_NXTDEVICES (0)
#define PYBRICKS_PY_PARAMETERS (1)
Expand Down
15 changes: 15 additions & 0 deletions lib/pbio/drv/bluetooth/bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,17 @@ pbio_error_t pbdrv_bluetooth_await_classic_task(pbio_os_state_t *state, void *co

return task->err;
}
#else

pbio_error_t pbdrv_bluetooth_start_inquiry_scan(pbdrv_bluetooth_inquiry_result_t *results, uint32_t *results_count, uint32_t *results_count_max, uint32_t duration_ms) {
return PBIO_ERROR_NOT_SUPPORTED;
}

pbio_error_t pbdrv_bluetooth_await_classic_task(pbio_os_state_t *state, void *context) {
return PBIO_ERROR_NOT_SUPPORTED;
}


#endif // PBDRV_CONFIG_BLUETOOTH_NUM_CLASSIC_CONNECTIONS

static bool pbdrv_bluetooth_shutting_down;
Expand Down Expand Up @@ -729,6 +740,10 @@ pbio_error_t pbdrv_bluetooth_close_user_tasks(pbio_os_state_t *state, pbio_os_ti

PBIO_OS_ASYNC_BEGIN(state);

#if PBDRV_CONFIG_BLUETOOTH_NUM_CLASSIC_CONNECTIONS
pbdrv_bluetooth_rfcomm_disconnect_all();
#endif // PBDRV_CONFIG_BLUETOOTH_NUM_CLASSIC_CONNECTIONS

for (peri_index = 0; peri_index < PBDRV_CONFIG_BLUETOOTH_NUM_PERIPHERALS; peri_index++) {
peri = pbdrv_bluetooth_peripheral_get_by_index(peri_index);
// Await ongoing peripheral user task, requesting cancellation to
Expand Down
11 changes: 11 additions & 0 deletions lib/pbio/drv/bluetooth/bluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ typedef struct {

pbio_error_t pbdrv_bluetooth_inquiry_scan_func(pbio_os_state_t *state, void *context);

// The following functions are defined in each Bluetooth implementation that
// supports RFCOMM sockets.

// Cancels all pending connection attempts. Called e.g. when the user interrupts
// execution to drop into the debug REPL.
void pbdrv_bluetooth_rfcomm_cancel_connection();

// Disconnects all active RFCOMM connections and cleans up all sockets. Called
// during user program termination.
void pbdrv_bluetooth_rfcomm_disconnect_all();

#endif // PBDRV_CONFIG_BLUETOOTH

#endif // _INTERNAL_PBDRV_BLUETOOTH_H_
34 changes: 32 additions & 2 deletions lib/pbio/drv/bluetooth/bluetooth_btstack.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#if PBDRV_CONFIG_BLUETOOTH_BTSTACK

#include <btstack_config.h>

#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
Expand All @@ -16,9 +18,13 @@
#include <btstack.h>
#include <btstack_run_loop.h>

#include <gap.h>
#include <lwrb/lwrb.h>

#include <pbdrv/bluetooth.h>
#include <pbdrv/clock.h>

#include <pbio/int_math.h>
#include <pbio/os.h>
#include <pbio/protocol.h>
#include <pbio/version.h>
Expand Down Expand Up @@ -46,7 +52,7 @@
#define PERIPHERAL_TIMEOUT_MS_CONNECT (5000)
#define PERIPHERAL_TIMEOUT_MS_PAIRING (5000)

#define DEBUG 0
#define DEBUG 2

#if DEBUG
#include <pbio/debug.h>
Expand Down Expand Up @@ -338,12 +344,21 @@ static bool pbdrv_bluetooth_btstack_ble_supported(void) {
return chipset_info && chipset_info->supports_ble;
}

void pbdrv_bluetooth_classic_init(void);
void pbdrv_bluetooth_btstack_handle_classic_security_packet(uint8_t *packet, uint16_t size);

// currently, this function just handles the Powered Up handset control.
static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {

// Platform-specific platform handler has priority.
pbdrv_bluetooth_btstack_platform_packet_handler(packet_type, channel, packet, size);

if (packet_type == HCI_EVENT_PACKET) {
// We have a separate handler for classic security packets,
// which we don't mix in here for clarity's sake.
pbdrv_bluetooth_btstack_handle_classic_security_packet(packet, size);
}

switch (hci_event_packet_get_type(packet)) {
case HCI_EVENT_COMMAND_COMPLETE: {
const uint8_t *rp = hci_event_command_complete_get_return_parameters(packet);
Expand Down Expand Up @@ -1172,6 +1187,18 @@ pbio_error_t pbdrv_bluetooth_stop_observing_func(pbio_os_state_t *state, void *c
PBIO_OS_ASYNC_END(PBIO_SUCCESS);
}

void pbdrv_bluetooth_local_address(bdaddr_t addr) {
gap_local_bd_addr(addr);
}

const char *pbdrv_bluetooth_bdaddr_to_str(const bdaddr_t addr) {
return bd_addr_to_str(addr);
}

bool pbdrv_bluetooth_str_to_bdaddr(const char *str, bdaddr_t addr) {
return sscanf_bd_addr(str, addr) == 1;
}

static void pbdrv_bluetooth_inquiry_unpack_scan_event(uint8_t *event_packet, pbdrv_bluetooth_inquiry_result_t *result) {

gap_event_inquiry_result_get_bd_addr(event_packet, result->bdaddr);
Expand Down Expand Up @@ -1426,6 +1453,7 @@ void pbdrv_bluetooth_init_hci(void) {
hci_dump_enable_log_level(HCI_DUMP_LOG_LEVEL_INFO, true);
hci_dump_enable_log_level(HCI_DUMP_LOG_LEVEL_ERROR, true);
hci_dump_enable_log_level(HCI_DUMP_LOG_LEVEL_DEBUG, true);
hci_dump_enable_packet_log(false);
#endif

static btstack_packet_callback_registration_t hci_event_callback_registration;
Expand Down Expand Up @@ -1471,6 +1499,8 @@ void pbdrv_bluetooth_init_hci(void) {
// GATT Client setup
gatt_client_init();

pbdrv_bluetooth_classic_init();

#if PBDRV_CONFIG_BLUETOOTH_BTSTACK_NUM_LE_HOSTS
// setup ATT server
att_server_init(profile_data, att_read_callback, NULL);
Expand All @@ -1488,7 +1518,7 @@ void pbdrv_bluetooth_init_hci(void) {
(void)pybricks_configured;
(void)nordic_spp_packet_handler;
(void)sm_packet_handler;
#endif // PBDRV_CONFIG_BLUETOOTH_BTSTACK_LE
#endif // PBDRV_CONFIG_BLUETOOTH_BTSTACK_NUM_LE_HOSTS

bluetooth_thread_err = PBIO_ERROR_AGAIN;
bluetooth_thread_state = 0;
Expand Down
4 changes: 4 additions & 0 deletions lib/pbio/drv/bluetooth/bluetooth_stm32_bluenrg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1357,4 +1357,8 @@ void pbdrv_bluetooth_init_hci(void) {
pbio_os_process_start(&pbdrv_bluetooth_spi_process, pbdrv_bluetooth_spi_process_thread, NULL);
}

void pbdrv_bluetooth_rfcomm_disconnect_all(void) {
// RFCOMM not supported on our STM32 platforms.
}

#endif // PBDRV_CONFIG_BLUETOOTH_STM32_BLUENRG
4 changes: 4 additions & 0 deletions lib/pbio/drv/bluetooth/bluetooth_stm32_cc2640.c
Original file line number Diff line number Diff line change
Expand Up @@ -2066,4 +2066,8 @@ void pbdrv_bluetooth_init_hci(void) {
pbio_os_process_start(&pbdrv_bluetooth_spi_process, pbdrv_bluetooth_spi_process_thread, NULL);
}

void pbdrv_bluetooth_rfcomm_disconnect_all(void) {
// RFCOMM not supported on STM32 platforms currently.
}

#endif // PBDRV_CONFIG_BLUETOOTH_STM32_CC2640
Loading
Loading