Skip to content

Commit 7134d3e

Browse files
committed
fixup! pbdrv/bluetooth: RFCOMM socket API.
Fix assumption that umm_malloc is present.
1 parent 3869d60 commit 7134d3e

5 files changed

Lines changed: 59 additions & 26 deletions

File tree

.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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ SRC_STM32_USB_DEV += $(addprefix lib/pbio/drv/usb/stm32_usbd/,\
527527
SRC_UMM_MALLOC = lib/umm_malloc/src/umm_malloc.c
528528

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

533533
# NXT OS

lib/pbio/drv/bluetooth/bluetooth.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,9 @@ pbio_error_t pbdrv_bluetooth_close_user_tasks(pbio_os_state_t *state, pbio_os_ti
685685
// Requests peripheral operations to cancel, if they support it.
686686
pbdrv_bluetooth_cancel_operation_request();
687687

688+
#if PBDRV_CONFIG_BLUETOOTH_NUM_CLASSIC_CONNECTIONS
688689
pbdrv_bluetooth_rfcomm_disconnect_all();
690+
#endif // PBDRV_CONFIG_BLUETOOTH_NUM_CLASSIC_CONNECTIONS
689691

690692
for (peri_index = 0; peri_index < PBDRV_CONFIG_BLUETOOTH_NUM_PERIPHERALS; peri_index++) {
691693
peri = pbdrv_bluetooth_peripheral_get_by_index(peri_index);

lib/pbio/drv/bluetooth/bluetooth_btstack.c

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121
#include <classic/sdp_client.h>
2222
#include <classic/sdp_server.h>
2323
#include <classic/spp_server.h>
24-
#include <umm_malloc.h>
2524
#include <pbsys/storage.h>
2625
#include <pbsys/storage_settings.h>
2726
#include <lwrb/lwrb.h>
2827

28+
#if HAVE_UMM_MALLOC
29+
#include <umm_malloc.h>
30+
#endif
31+
2932
#include <pbdrv/bluetooth.h>
3033
#include <pbdrv/clock.h>
3134

@@ -1225,15 +1228,36 @@ bool is_already_paired(bd_addr_t addr) {
12251228
return gap_get_link_key_for_bd_addr(addr, key, &type);
12261229
}
12271230

1231+
#ifndef MAX_NR_RFCOMM_CHANNELS
1232+
#define MAX_NR_RFCOMM_CHANNELS 0
1233+
#endif
1234+
1235+
#define RFCOMM_SOCKET_COUNT (MAX_NR_RFCOMM_CHANNELS > 0 ? MAX_NR_RFCOMM_CHANNELS - 1 : 0)
1236+
#if HAVE_UMM_MALLOC
1237+
#define RFCOMM_RX_BUFFER_SIZE (4 * 1024)
1238+
#define RFCOMM_TX_BUFFER_SIZE (2 * 1024)
1239+
#else
1240+
// When we don't have umm_malloc, statically allocate small buffers.
1241+
// This limits throughput but doesn't constraint the logical size of
1242+
// messages we can send.
1243+
#define RFCOMM_RX_BUFFER_SIZE (256)
1244+
#define RFCOMM_TX_BUFFER_SIZE (256)
1245+
#endif
1246+
12281247
typedef struct {
1229-
uint8_t *tx_buffer_data; // tx_buffer from customer. We don't own this.
1248+
#if HAVE_UMM_MALLOC
1249+
uint8_t *tx_buffer_data; // tx_buffer from customer. We don't own this.
12301250
uint8_t *rx_buffer_data;
1231-
pbio_os_timer_t tx_timer; // Timer for tracking timeouts on the current send.
1232-
pbio_os_timer_t rx_timer; // Timer for tracking timeouts on the current receive.
1233-
lwrb_t tx_buffer; // Ring buffer to contain outgoing data.
1234-
lwrb_t rx_buffer; // Ring buffer to contain incoming data.
1251+
#else
1252+
uint8_t tx_buffer_data[RFCOMM_TX_BUFFER_SIZE];
1253+
uint8_t rx_buffer_data[RFCOMM_RX_BUFFER_SIZE];
1254+
#endif
1255+
pbio_os_timer_t tx_timer; // Timer for tracking timeouts on the current send.
1256+
pbio_os_timer_t rx_timer; // Timer for tracking timeouts on the current receive.
1257+
lwrb_t tx_buffer; // Ring buffer to contain outgoing data.
1258+
lwrb_t rx_buffer; // Ring buffer to contain incoming data.
12351259

1236-
int mtu; // MTU for this connection.
1260+
int mtu; // MTU for this connection.
12371261

12381262
// How many rfcomm credits are outstanding? When the connection is first started,
12391263
// this is the rx buffer size divided by the MTU (the frame size). Each time we receive
@@ -1247,16 +1271,12 @@ typedef struct {
12471271
uint16_t server_channel; // The remote rfcomm channel we're connected to.
12481272
bool is_used; // Is this socket descriptor in use?
12491273
bool is_connected; // Is this socket descriptor connected?
1250-
bool is_cancelled; // Has this socket been cancelled? Interrupts pending
1251-
// listen() or connect calls.
1274+
bool is_cancelled; // Has this socket been cancelled? Interrupts pending
1275+
// listen() or connect calls.
12521276
bool is_using_sdp_system; // Is this socket currently using the SDP system?
12531277
} pbdrv_bluetooth_classic_rfcomm_socket_t;
12541278

1255-
#define MAX_NUM_CONNECTIONS (7)
1256-
#define RFCOMM_RX_BUFFER_SIZE (4 * 1024)
1257-
#define RFCOMM_TX_BUFFER_SIZE (2 * 1024)
1258-
1259-
static pbdrv_bluetooth_classic_rfcomm_socket_t pbdrv_bluetooth_classic_rfcomm_sockets[MAX_NUM_CONNECTIONS];
1279+
static pbdrv_bluetooth_classic_rfcomm_socket_t pbdrv_bluetooth_classic_rfcomm_sockets[RFCOMM_SOCKET_COUNT];
12601280

12611281
static int pbdrv_bluetooth_classic_rfcomm_socket_max_credits(pbdrv_bluetooth_classic_rfcomm_socket_t *socket) {
12621282
return RFCOMM_RX_BUFFER_SIZE / socket->mtu;
@@ -1282,6 +1302,7 @@ static void pbdrv_bluetooth_classic_rfcomm_socket_grant_owed_credits(pbdrv_bluet
12821302
}
12831303

12841304
static void pbdrv_bluetooth_classic_rfcomm_socket_reset(pbdrv_bluetooth_classic_rfcomm_socket_t *socket) {
1305+
#if HAVE_UMM_MALLOC
12851306
if (socket->rx_buffer_data) {
12861307
umm_free(socket->rx_buffer_data);
12871308
socket->rx_buffer_data = NULL;
@@ -1290,6 +1311,7 @@ static void pbdrv_bluetooth_classic_rfcomm_socket_reset(pbdrv_bluetooth_classic_
12901311
umm_free(socket->tx_buffer_data);
12911312
socket->tx_buffer_data = NULL;
12921313
}
1314+
#endif
12931315
lwrb_free(&socket->rx_buffer);
12941316
lwrb_free(&socket->tx_buffer);
12951317
// A non-zero duration on these timers is used as a flag to indicate the
@@ -1307,18 +1329,20 @@ static void pbdrv_bluetooth_classic_rfcomm_socket_reset(pbdrv_bluetooth_classic_
13071329
}
13081330

13091331
static pbdrv_bluetooth_classic_rfcomm_socket_t *pbdrv_bluetooth_classic_rfcomm_socket_alloc() {
1310-
for (uint32_t i = 0; i < PBIO_ARRAY_SIZE(pbdrv_bluetooth_classic_rfcomm_sockets); i++) {
1332+
for (int i = 0; i < RFCOMM_SOCKET_COUNT; i++) {
13111333
if (!pbdrv_bluetooth_classic_rfcomm_sockets[i].is_used) {
13121334
pbdrv_bluetooth_classic_rfcomm_socket_t *sock = &pbdrv_bluetooth_classic_rfcomm_sockets[i];
13131335
pbdrv_bluetooth_classic_rfcomm_socket_reset(sock);
13141336
sock->is_used = true;
1337+
#if HAVE_UMM_MALLOC
13151338
sock->rx_buffer_data = umm_malloc(RFCOMM_RX_BUFFER_SIZE);
13161339
sock->tx_buffer_data = umm_malloc(RFCOMM_TX_BUFFER_SIZE);
1317-
if (!sock->rx_buffer_data) {
1318-
DEBUG_PRINT("Failed to allocate RFCOMM RX buffer.\n");
1340+
if (!sock->rx_buffer_data || !sock->tx_buffer_data) {
1341+
DEBUG_PRINT("Failed to allocate RFCOMM RX or TX buffer.\n");
13191342
sock->is_used = false;
13201343
return NULL;
13211344
}
1345+
#endif
13221346
lwrb_init(&sock->rx_buffer, sock->rx_buffer_data, RFCOMM_RX_BUFFER_SIZE);
13231347
lwrb_init(&sock->tx_buffer, sock->tx_buffer_data, RFCOMM_TX_BUFFER_SIZE);
13241348
return sock;
@@ -1330,7 +1354,7 @@ static pbdrv_bluetooth_classic_rfcomm_socket_t *pbdrv_bluetooth_classic_rfcomm_s
13301354

13311355

13321356
static pbdrv_bluetooth_classic_rfcomm_socket_t *pbdrv_bluetooth_classic_rfcomm_socket_find_by_cid(uint16_t cid) {
1333-
for (size_t i = 0; i < MAX_NUM_CONNECTIONS; i++) {
1357+
for (int i = 0; i < RFCOMM_SOCKET_COUNT; i++) {
13341358
if (pbdrv_bluetooth_classic_rfcomm_sockets[i].is_used &&
13351359
pbdrv_bluetooth_classic_rfcomm_sockets[i].cid == cid) {
13361360
return &pbdrv_bluetooth_classic_rfcomm_sockets[i];
@@ -1340,14 +1364,14 @@ static pbdrv_bluetooth_classic_rfcomm_socket_t *pbdrv_bluetooth_classic_rfcomm_s
13401364
}
13411365

13421366
static pbdrv_bluetooth_classic_rfcomm_socket_t *pbdrv_bluetooth_classic_rfcomm_socket_find_by_conn(const pbdrv_bluetooth_rfcomm_conn_t *c) {
1343-
if (c->conn_id < 0 || c->conn_id >= MAX_NUM_CONNECTIONS) {
1367+
if (c->conn_id < 0 || c->conn_id >= RFCOMM_SOCKET_COUNT) {
13441368
return NULL;
13451369
}
13461370
return &pbdrv_bluetooth_classic_rfcomm_sockets[c->conn_id];
13471371
}
13481372

13491373
static int pbdrv_bluetooth_classic_rfcomm_socket_id(pbdrv_bluetooth_classic_rfcomm_socket_t *socket) {
1350-
for (size_t i = 0; i < MAX_NUM_CONNECTIONS; i++) {
1374+
for (int i = 0; i < RFCOMM_SOCKET_COUNT; i++) {
13511375
if (&pbdrv_bluetooth_classic_rfcomm_sockets[i] == socket) {
13521376
return i;
13531377
}
@@ -1971,7 +1995,7 @@ bool pbdrv_bluetooth_rfcomm_is_connected(const pbdrv_bluetooth_rfcomm_conn_t *co
19711995
}
19721996

19731997
void pbdrv_bluetooth_rfcomm_cancel_connection() {
1974-
for (size_t i = 0; i < MAX_NUM_CONNECTIONS; i++) {
1998+
for (int i = 0; i < RFCOMM_SOCKET_COUNT; i++) {
19751999
pbdrv_bluetooth_classic_rfcomm_socket_t *sock =
19762000
&pbdrv_bluetooth_classic_rfcomm_sockets[i];
19772001
if (sock->is_used) {
@@ -1981,7 +2005,7 @@ void pbdrv_bluetooth_rfcomm_cancel_connection() {
19812005
}
19822006

19832007
void pbdrv_bluetooth_rfcomm_disconnect_all() {
1984-
for (size_t i = 0; i < MAX_NUM_CONNECTIONS; i++) {
2008+
for (int i = 0; i < RFCOMM_SOCKET_COUNT; i++) {
19852009
pbdrv_bluetooth_classic_rfcomm_socket_t *sock =
19862010
&pbdrv_bluetooth_classic_rfcomm_sockets[i];
19872011
if (sock->is_used && sock->is_connected) {
@@ -2204,7 +2228,7 @@ void pbdrv_bluetooth_init_hci(void) {
22042228
hci_dump_enable_log_level(HCI_DUMP_LOG_LEVEL_ERROR, true);
22052229
hci_dump_enable_log_level(HCI_DUMP_LOG_LEVEL_DEBUG, true);
22062230
hci_dump_enable_packet_log(false);
2207-
#endif
2231+
#endif
22082232

22092233
static btstack_packet_callback_registration_t hci_event_callback_registration;
22102234

lib/pbio/include/pbsys/config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
+ PBSYS_CONFIG_FEATURE_PROGRAM_FORMAT_MULTI_MPY_V6_3_NATIVE * PBIO_PYBRICKS_FEATURE_FLAG_USER_PROG_FORMAT_MULTI_MPY_V6_3_NATIVE \
1919
)
2020

21+
#ifndef PBSYS_CONFIG_BLUETOOTH_CLASSIC_LINK_KEY_DB_SIZE
22+
#define PBSYS_CONFIG_BLUETOOTH_CLASSIC_LINK_KEY_DB_SIZE 0
23+
#endif
24+
2125
// When set to (1) PBSYS_CONFIG_STATUS_LIGHT indicates that a hub has a hub status light
2226
#ifndef PBSYS_CONFIG_STATUS_LIGHT
2327
#error "Must define PBSYS_CONFIG_STATUS_LIGHT in pbsysconfig.h"

0 commit comments

Comments
 (0)