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+
12281247typedef 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
12611281static 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
12841304static 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
13091331static 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
13321356static 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
13421366static 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
13491373static 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
19731997void 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
19832007void 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
0 commit comments