Skip to content

Commit 6434cc0

Browse files
committed
!wip pbdrv/bluetooth: RFCOMM socket API.
This implements the complete RFCOMM socket API, including listen, connect, send and recv. Tested via manual ping-pong testing for both listen and connect, against a Windows desktop.
1 parent efa2712 commit 6434cc0

30 files changed

Lines changed: 2414 additions & 39 deletions

.vscode/c_cpp_properties.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"gcc",
8080
"MICROPY_MODULE_FROZEN_MPY",
8181
"MICROPY_ROM_TEXT_COMPRESSION",
82+
"HAVE_UMM_MALLOC=1"
8283
],
8384
"compilerArgs": [
8485
"-Wall",
@@ -286,7 +287,8 @@
286287
],
287288
"defines": [
288289
"MICROPY_MODULE_FROZEN_MPY",
289-
"MICROPY_ROM_TEXT_COMPRESSION"
290+
"MICROPY_ROM_TEXT_COMPRESSION",
291+
"HAVE_UMM_MALLOC=1"
290292
],
291293
"compilerArgs": [
292294
"-mthumb",
@@ -404,7 +406,8 @@
404406
],
405407
"defines": [
406408
"MICROPY_MODULE_FROZEN_MPY",
407-
"MICROPY_USE_READLINE=1"
409+
"MICROPY_USE_READLINE=1",
410+
"HAVE_UMM_MALLOC=1",
408411
],
409412
"compilerPath": "/usr/bin/gcc",
410413
"cStandard": "c11",

bricks/_common/common.mk

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,16 @@ BTSTACK_BLE_SRC_C += $(addprefix lib/btstack/src/ble/,\
416416
sm.c \
417417
)
418418

419+
BTSTACK_CLASSIC_SRC_C = $(addprefix lib/btstack/src/classic/,\
420+
btstack_link_key_db_memory.c \
421+
rfcomm.c \
422+
sdp_client.c \
423+
sdp_client_rfcomm.c \
424+
sdp_server.c \
425+
sdp_util.c \
426+
spp_server.c \
427+
)
428+
419429
BTSTACK_SRC_C += $(addprefix lib/btstack/chipset/cc256x/,\
420430
btstack_chipset_cc256x.c \
421431
)
@@ -519,7 +529,7 @@ SRC_STM32_USB_DEV += $(addprefix lib/pbio/drv/usb/stm32_usbd/,\
519529
SRC_UMM_MALLOC = lib/umm_malloc/src/umm_malloc.c
520530

521531
ifeq ($(PB_LIB_UMM_MALLOC),1)
522-
CFLAGS += -I$(PBTOP)/lib/umm_malloc/src
532+
CFLAGS += -I$(PBTOP)/lib/umm_malloc/src -DHAVE_UMM_MALLOC=1
523533
endif
524534

525535
# NXT OS
@@ -579,6 +589,7 @@ ifeq ($(PB_LIB_BTSTACK),1)
579589
ifneq ($(CI_MODE),1)
580590
OBJ += $(addprefix $(BUILD)/, $(BTSTACK_SRC_C:.c=.o))
581591
OBJ += $(addprefix $(BUILD)/, $(BTSTACK_BLE_SRC_C:.c=.o))
592+
OBJ += $(addprefix $(BUILD)/, $(BTSTACK_CLASSIC_SRC_C:.c=.o))
582593
$(BUILD)/lib/btstack/%.o: CFLAGS += -Wno-error
583594
endif
584595
endif

bricks/_common/sources.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ PBIO_SRC_C = $(addprefix lib/pbio/,\
117117
drv/block_device/block_device_w25qxx_stm32.c \
118118
drv/bluetooth/bluetooth.c \
119119
drv/bluetooth/bluetooth_btstack.c \
120+
drv/bluetooth/rfcomm_btstack.c \
120121
drv/bluetooth/bluetooth_btstack_ev3.c \
121122
drv/bluetooth/bluetooth_btstack_posix.c \
122123
drv/bluetooth/bluetooth_btstack_stm32_hal.c \

bricks/primehub/mpconfigport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#define PYBRICKS_PY_IODEVICES_UART_DEVICE (1)
4242
#define PYBRICKS_PY_IODEVICES_XBOX_CONTROLLER (1)
4343
#define PYBRICKS_PY_MEDIA_IMAGE (0)
44-
#define PYBRICKS_PY_MESSAGING (1)
44+
#define PYBRICKS_PY_MESSAGING (0)
4545
#define PYBRICKS_PY_MESSAGING_RFCOMM (0)
4646
#define PYBRICKS_PY_NXTDEVICES (0)
4747
#define PYBRICKS_PY_PARAMETERS (1)

lib/pbio/drv/bluetooth/bluetooth.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,17 @@ pbio_error_t pbdrv_bluetooth_await_classic_task(pbio_os_state_t *state, void *co
579579

580580
return task->err;
581581
}
582+
#else
583+
584+
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) {
585+
return PBIO_ERROR_NOT_SUPPORTED;
586+
}
587+
588+
pbio_error_t pbdrv_bluetooth_await_classic_task(pbio_os_state_t *state, void *context) {
589+
return PBIO_ERROR_NOT_SUPPORTED;
590+
}
591+
592+
582593
#endif // PBDRV_CONFIG_BLUETOOTH_NUM_CLASSIC_CONNECTIONS
583594

584595
static bool pbdrv_bluetooth_shutting_down;
@@ -729,6 +740,10 @@ pbio_error_t pbdrv_bluetooth_close_user_tasks(pbio_os_state_t *state, pbio_os_ti
729740

730741
PBIO_OS_ASYNC_BEGIN(state);
731742

743+
#if PBDRV_CONFIG_BLUETOOTH_NUM_CLASSIC_CONNECTIONS
744+
pbdrv_bluetooth_rfcomm_disconnect_all();
745+
#endif // PBDRV_CONFIG_BLUETOOTH_NUM_CLASSIC_CONNECTIONS
746+
732747
for (peri_index = 0; peri_index < PBDRV_CONFIG_BLUETOOTH_NUM_PERIPHERALS; peri_index++) {
733748
peri = pbdrv_bluetooth_peripheral_get_by_index(peri_index);
734749
// Await ongoing peripheral user task, requesting cancellation to

lib/pbio/drv/bluetooth/bluetooth.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ typedef struct {
9999

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

102+
// The following functions are defined in each Bluetooth implementation that
103+
// supports RFCOMM sockets.
104+
105+
// Cancels all pending connection attempts. Called e.g. when the user interrupts
106+
// execution to drop into the debug REPL.
107+
void pbdrv_bluetooth_rfcomm_cancel_connection();
108+
109+
// Disconnects all active RFCOMM connections and cleans up all sockets. Called
110+
// during user program termination.
111+
void pbdrv_bluetooth_rfcomm_disconnect_all();
112+
102113
#endif // PBDRV_CONFIG_BLUETOOTH
103114

104115
#endif // _INTERNAL_PBDRV_BLUETOOTH_H_

lib/pbio/drv/bluetooth/bluetooth_btstack.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#if PBDRV_CONFIG_BLUETOOTH_BTSTACK
99

10+
#include <btstack_config.h>
11+
1012
#include <assert.h>
1113
#include <inttypes.h>
1214
#include <stdio.h>
@@ -16,9 +18,13 @@
1618
#include <btstack.h>
1719
#include <btstack_run_loop.h>
1820

21+
#include <gap.h>
22+
#include <lwrb/lwrb.h>
23+
1924
#include <pbdrv/bluetooth.h>
2025
#include <pbdrv/clock.h>
2126

27+
#include <pbio/int_math.h>
2228
#include <pbio/os.h>
2329
#include <pbio/protocol.h>
2430
#include <pbio/version.h>
@@ -338,12 +344,21 @@ static bool pbdrv_bluetooth_btstack_ble_supported(void) {
338344
return chipset_info && chipset_info->supports_ble;
339345
}
340346

347+
void pbdrv_bluetooth_classic_init(void);
348+
void pbdrv_bluetooth_btstack_handle_classic_security_packet(uint8_t *packet, uint16_t size);
349+
341350
// currently, this function just handles the Powered Up handset control.
342351
static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
343352

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

356+
if (packet_type == HCI_EVENT_PACKET) {
357+
// We have a separate handler for classic security packets,
358+
// which we don't mix in here for clarity's sake.
359+
pbdrv_bluetooth_btstack_handle_classic_security_packet(packet, size);
360+
}
361+
347362
switch (hci_event_packet_get_type(packet)) {
348363
case HCI_EVENT_COMMAND_COMPLETE: {
349364
const uint8_t *rp = hci_event_command_complete_get_return_parameters(packet);
@@ -1172,6 +1187,18 @@ pbio_error_t pbdrv_bluetooth_stop_observing_func(pbio_os_state_t *state, void *c
11721187
PBIO_OS_ASYNC_END(PBIO_SUCCESS);
11731188
}
11741189

1190+
void pbdrv_bluetooth_local_address(bdaddr_t addr) {
1191+
gap_local_bd_addr(addr);
1192+
}
1193+
1194+
const char *pbdrv_bluetooth_bdaddr_to_str(const bdaddr_t addr) {
1195+
return bd_addr_to_str(addr);
1196+
}
1197+
1198+
bool pbdrv_bluetooth_str_to_bdaddr(const char *str, bdaddr_t addr) {
1199+
return sscanf_bd_addr(str, addr) == 1;
1200+
}
1201+
11751202
static void pbdrv_bluetooth_inquiry_unpack_scan_event(uint8_t *event_packet, pbdrv_bluetooth_inquiry_result_t *result) {
11761203

11771204
gap_event_inquiry_result_get_bd_addr(event_packet, result->bdaddr);
@@ -1426,6 +1453,7 @@ void pbdrv_bluetooth_init_hci(void) {
14261453
hci_dump_enable_log_level(HCI_DUMP_LOG_LEVEL_INFO, true);
14271454
hci_dump_enable_log_level(HCI_DUMP_LOG_LEVEL_ERROR, true);
14281455
hci_dump_enable_log_level(HCI_DUMP_LOG_LEVEL_DEBUG, true);
1456+
hci_dump_enable_packet_log(false);
14291457
#endif
14301458

14311459
static btstack_packet_callback_registration_t hci_event_callback_registration;
@@ -1471,6 +1499,8 @@ void pbdrv_bluetooth_init_hci(void) {
14711499
// GATT Client setup
14721500
gatt_client_init();
14731501

1502+
pbdrv_bluetooth_classic_init();
1503+
14741504
#if PBDRV_CONFIG_BLUETOOTH_BTSTACK_NUM_LE_HOSTS
14751505
// setup ATT server
14761506
att_server_init(profile_data, att_read_callback, NULL);
@@ -1488,7 +1518,7 @@ void pbdrv_bluetooth_init_hci(void) {
14881518
(void)pybricks_configured;
14891519
(void)nordic_spp_packet_handler;
14901520
(void)sm_packet_handler;
1491-
#endif // PBDRV_CONFIG_BLUETOOTH_BTSTACK_LE
1521+
#endif // PBDRV_CONFIG_BLUETOOTH_BTSTACK_NUM_LE_HOSTS
14921522

14931523
bluetooth_thread_err = PBIO_ERROR_AGAIN;
14941524
bluetooth_thread_state = 0;

lib/pbio/drv/bluetooth/bluetooth_stm32_bluenrg.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,4 +1357,8 @@ void pbdrv_bluetooth_init_hci(void) {
13571357
pbio_os_process_start(&pbdrv_bluetooth_spi_process, pbdrv_bluetooth_spi_process_thread, NULL);
13581358
}
13591359

1360+
void pbdrv_bluetooth_rfcomm_disconnect_all(void) {
1361+
// RFCOMM not supported on our STM32 platforms.
1362+
}
1363+
13601364
#endif // PBDRV_CONFIG_BLUETOOTH_STM32_BLUENRG

lib/pbio/drv/bluetooth/bluetooth_stm32_cc2640.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,4 +2066,8 @@ void pbdrv_bluetooth_init_hci(void) {
20662066
pbio_os_process_start(&pbdrv_bluetooth_spi_process, pbdrv_bluetooth_spi_process_thread, NULL);
20672067
}
20682068

2069+
void pbdrv_bluetooth_rfcomm_disconnect_all(void) {
2070+
// RFCOMM not supported on STM32 platforms currently.
2071+
}
2072+
20692073
#endif // PBDRV_CONFIG_BLUETOOTH_STM32_CC2640

0 commit comments

Comments
 (0)