40
40
41
41
const char *_spp_server_name = " ESP32SPP" ;
42
42
43
- // #define RX_QUEUE_SIZE 512
44
- // #define TX_QUEUE_SIZE 32
45
- #define RX_QUEUE_SIZE 512 * 4
46
- #define TX_QUEUE_SIZE 512 * 4
43
+ // #define RX_QUEUE_SIZE 512
44
+ // #define TX_QUEUE_SIZE 32
47
45
#define SPP_TX_QUEUE_TIMEOUT 1000
48
46
#define SPP_TX_DONE_TIMEOUT 1000
49
47
#define SPP_CONGESTED_TIMEOUT 1000
@@ -114,8 +112,7 @@ typedef struct
114
112
uint8_t data[];
115
113
} spp_packet_t ;
116
114
117
- #if (ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO)
118
- static char *bda2str (esp_bd_addr_t bda, char *str, size_t size)
115
+ char *bda2str (esp_bd_addr_t bda, char *str, size_t size)
119
116
{
120
117
if (bda == NULL || str == NULL || size < 18 )
121
118
{
@@ -126,7 +123,6 @@ static char *bda2str(esp_bd_addr_t bda, char *str, size_t size)
126
123
snprintf (str, size, " %02x:%02x:%02x:%02x:%02x:%02x" , p[0 ], p[1 ], p[2 ], p[3 ], p[4 ], p[5 ]);
127
124
return str;
128
125
}
129
- #endif
130
126
131
127
static bool get_name_from_eir (uint8_t *eir, char *bdname, uint8_t *bdname_len)
132
128
{
@@ -271,7 +267,7 @@ static void _spp_tx_task(void *arg)
271
267
}
272
268
else
273
269
{
274
- log_e (" Something went horribly wrong" );
270
+ log_e (" BluetoothSerial _spp_tx_task: something went horribly wrong" );
275
271
}
276
272
}
277
273
vTaskDelete (NULL );
@@ -382,6 +378,7 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
382
378
{
383
379
log_e (" ESP_SPP_CLOSE_EVT failed!, status:%d" , param->close .status );
384
380
}
381
+
385
382
break ;
386
383
387
384
case ESP_SPP_START_EVT: // Enum 28 - When SPP server started
@@ -414,7 +411,7 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
414
411
{
415
412
if (xQueueSend (_spp_rx_queue, param->data_ind .data + i, (TickType_t)0 ) != pdTRUE)
416
413
{
417
- log_e (" RX Full! Discarding %u bytes" , param->data_ind .len - i);
414
+ log_e (" BluetoothSerial RX Full! Discarding %u bytes\r\n " , param->data_ind .len - i);
418
415
break ;
419
416
}
420
417
}
@@ -563,9 +560,7 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
563
560
case ESP_BT_GAP_DEV_PROP_EIR: // Enum 4 - Extended Inquiry Response, value type is uint8_t []
564
561
if (get_name_from_eir ((uint8_t *)param->disc_res .prop [i].val , peer_bdname, &peer_bdname_len))
565
562
{
566
-
567
563
log_i (" ESP_BT_GAP_DISC_RES_EVT : EIR : %s : %d" , peer_bdname, peer_bdname_len);
568
-
569
564
if (strlen (_remote_name) == peer_bdname_len &&
570
565
strncmp (peer_bdname, _remote_name, peer_bdname_len) == 0 )
571
566
{
@@ -628,15 +623,15 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
628
623
case ESP_BT_GAP_AUTH_CMPL_EVT: // Enum 4 - Authentication complete event
629
624
if (param->auth_cmpl .stat == ESP_BT_STATUS_SUCCESS)
630
625
{
631
- log_v (" authentication success: %s" , param->auth_cmpl .device_name );
626
+ log_v (" ESP_BT_GAP_AUTH_CMPL_EVT authentication success: %s" , param->auth_cmpl .device_name );
632
627
if (auth_complete_callback)
633
628
{
634
629
auth_complete_callback (true );
635
630
}
636
631
}
637
632
else
638
633
{
639
- log_e (" authentication failed, status:%d" , param->auth_cmpl .stat );
634
+ log_e (" ESP_BT_GAP_AUTH_CMPL_EVT authentication failed, status:%d" , param->auth_cmpl .stat );
640
635
if (auth_complete_callback)
641
636
{
642
637
auth_complete_callback (false );
@@ -657,8 +652,12 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
657
652
}
658
653
break ;
659
654
#ifdef CONFIG_BT_SSP_ENABLED
660
- case ESP_BT_GAP_CFM_REQ_EVT: // Enum 6 - Security Simple Pairing User Confirmation request.
661
- log_i (" ESP_BT_GAP_CFM_REQ_EVT Please compare the numeric value: %d" , param->cfm_req .num_val );
655
+ case ESP_BT_GAP_CFM_REQ_EVT: // Enum 6 - Security Simple Pairing User Confirmation request.
656
+ #if (ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO)
657
+ char bda_str[18 ];
658
+ log_i (" ESP_BT_GAP_CFM_REQ_EVT from %s. Please compare the numeric value: %d" ,
659
+ bda2str (param->cfm_req .bda , bda_str, 18 ), param->cfm_req .num_val );
660
+ #endif
662
661
if (confirm_request_callback)
663
662
{
664
663
memcpy (current_bd_addr, param->cfm_req .bda , sizeof (esp_bd_addr_t ));
@@ -667,15 +666,17 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
667
666
else
668
667
{
669
668
// log_w("ESP_BT_GAP_CFM_REQ_EVT: confirm_request_callback does not exist - refusing pairing");
670
- // esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, false);
671
- log_w (" ESP_BT_GAP_CFM_REQ_EVT: confirming replay " );
669
+ // esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, false);
670
+ log_w (" ESP_BT_GAP_CFM_REQ_EVT: confirming reply " );
672
671
esp_bt_gap_ssp_confirm_reply (param->cfm_req .bda , true );
673
672
}
674
673
break ;
675
674
#endif
675
+
676
676
case ESP_BT_GAP_KEY_NOTIF_EVT: // Enum 7 - Security Simple Pairing Passkey Notification
677
677
log_i (" ESP_BT_GAP_KEY_NOTIF_EVT passkey:%d" , param->key_notif .passkey );
678
678
break ;
679
+
679
680
#ifdef CONFIG_BT_SSP_ENABLED
680
681
case ESP_BT_GAP_KEY_REQ_EVT: // Enum 8 - Security Simple Pairing Passkey request
681
682
log_i (" ESP_BT_GAP_KEY_REQ_EVT Please enter passkey!" );
@@ -691,10 +692,10 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
691
692
}
692
693
break ;
693
694
#endif
695
+
694
696
case ESP_BT_GAP_READ_RSSI_DELTA_EVT: // Enum 9 - Read rssi event
695
697
log_i (" ESP_BT_GAP_READ_RSSI_DELTA_EVT Read rssi event" );
696
698
break ;
697
-
698
699
case ESP_BT_GAP_CONFIG_EIR_DATA_EVT: // Enum 10 - Config EIR data event
699
700
log_i (" ESP_BT_GAP_CONFIG_EIR_DATA_EVT: stat:%d num:%d" , param->config_eir_data .stat ,
700
701
param->config_eir_data .eir_type_num );
@@ -731,8 +732,11 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
731
732
732
733
case ESP_BT_GAP_ACL_CONN_CMPL_STAT_EVT: // Enum 16 - ACL connection complete status event
733
734
{
735
+ #if (ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO)
734
736
char bda_str[18 ];
735
- log_i (" ESP_BT_GAP_ACL_CONN_CMPL_STAT_EVT ACL connection from: %s" , bda2str (param->acl_conn_cmpl_stat .bda , bda_str, 18 ));
737
+ log_i (" ESP_BT_GAP_ACL_CONN_CMPL_STAT_EVT ACL connection from: %s" ,
738
+ bda2str (param->acl_conn_cmpl_stat .bda , bda_str, 18 ));
739
+ #endif
736
740
737
741
memcpy (_aclAddress, param->acl_conn_cmpl_stat .bda , ESP_BD_ADDR_LEN);
738
742
@@ -757,7 +761,7 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
757
761
}
758
762
}
759
763
760
- static bool _init_bt (const char *deviceName, bt_mode mode)
764
+ static bool _init_bt (const char *deviceName, bt_mode mode, uint16_t rxQueueSize, uint16_t txQueueSize )
761
765
{
762
766
if (!_bt_event_group)
763
767
{
@@ -784,7 +788,8 @@ static bool _init_bt(const char *deviceName, bt_mode mode)
784
788
}
785
789
if (_spp_rx_queue == NULL )
786
790
{
787
- _spp_rx_queue = xQueueCreate (RX_QUEUE_SIZE, sizeof (uint8_t )); // initialize the queue
791
+ // _spp_rx_queue = xQueueCreate(RX_QUEUE_SIZE, sizeof(uint8_t)); //initialize the queue
792
+ _spp_rx_queue = xQueueCreate (rxQueueSize, sizeof (uint8_t )); // initialize the queue
788
793
if (_spp_rx_queue == NULL )
789
794
{
790
795
log_e (" RX Queue Create Failed" );
@@ -793,7 +798,8 @@ static bool _init_bt(const char *deviceName, bt_mode mode)
793
798
}
794
799
if (_spp_tx_queue == NULL )
795
800
{
796
- _spp_tx_queue = xQueueCreate (TX_QUEUE_SIZE, sizeof (spp_packet_t *)); // initialize the queue
801
+ // _spp_tx_queue = xQueueCreate(TX_QUEUE_SIZE, sizeof(spp_packet_t *)); //initialize the queue
802
+ _spp_tx_queue = xQueueCreate (txQueueSize, sizeof (spp_packet_t *)); // initialize the queue
797
803
if (_spp_tx_queue == NULL )
798
804
{
799
805
log_e (" TX Queue Create Failed" );
@@ -830,14 +836,14 @@ static bool _init_bt(const char *deviceName, bt_mode mode)
830
836
esp_bluedroid_status_t bt_state = esp_bluedroid_get_status ();
831
837
if (bt_state == ESP_BLUEDROID_STATUS_UNINITIALIZED)
832
838
{
833
- // #ifdef CONFIG_BT_SSP_ENABLED
839
+ // #ifdef CONFIG_BT_SSP_ENABLED
834
840
#if false
835
841
// Start with SSP
836
842
esp_bluedroid_config_t bluedroid_cfg = BT_BLUEDROID_INIT_CONFIG_DEFAULT ();
837
843
if (_enableSSP == false )
838
844
bluedroid_cfg.ssp_en = false ;
839
845
840
- if (esp_bluedroid_init_with_cfg (&bluedroid_cfg))
846
+ if (esp_bluedroid_init_with_cfg (&bluedroid_cfg)) // *** Not supported by IDF5.1 ***
841
847
{
842
848
log_e (" initialize bluedroid failed" );
843
849
return false ;
@@ -882,7 +888,8 @@ static bool _init_bt(const char *deviceName, bt_mode mode)
882
888
883
889
log_i (" device name set" );
884
890
esp_bt_dev_set_device_name (deviceName);
885
- // esp_bt_gap_set_device_name(deviceName);
891
+ // Note: in later cores, this changes to:
892
+ // esp_bt_gap_set_device_name(deviceName);
886
893
887
894
#ifdef CONFIG_BT_SSP_ENABLED
888
895
if (_enableSSP)
@@ -989,10 +996,12 @@ static bool waitForConnect(int timeout)
989
996
}
990
997
else if ((rc & SPP_CLOSED) != 0 )
991
998
{
992
- log_d (" connection closed!" );
999
+ if (timeout > 0 )
1000
+ log_d (" connection closed!" );
993
1001
return false ;
994
1002
}
995
- log_d (" timeout" );
1003
+ if (timeout > 0 )
1004
+ log_d (" timeout" );
996
1005
return false ;
997
1006
}
998
1007
@@ -1029,14 +1038,16 @@ BluetoothSerial::~BluetoothSerial(void)
1029
1038
* @param isMaster set to true if you want to connect to an other device
1030
1039
* @param disableBLE if BLE is not used, its ram can be freed to get +10kB free ram
1031
1040
*/
1032
- bool BluetoothSerial::begin (String localName, bool isMaster, bool disableBLE)
1041
+ // bool BluetoothSerial::begin(String localName, bool isMaster, bool disableBLE) {
1042
+ bool BluetoothSerial::begin (String localName, bool isMaster, bool disableBLE, uint16_t rxQueueSize,
1043
+ uint16_t txQueueSize)
1033
1044
{
1034
1045
_isMaster = isMaster;
1035
1046
if (localName.length ())
1036
1047
{
1037
1048
local_name = localName;
1038
1049
}
1039
- return _init_bt (local_name.c_str (), disableBLE ? BT_MODE_CLASSIC_BT : BT_MODE_BTDM);
1050
+ return _init_bt (local_name.c_str (), disableBLE ? BT_MODE_CLASSIC_BT : BT_MODE_BTDM, rxQueueSize, txQueueSize );
1040
1051
}
1041
1052
1042
1053
int BluetoothSerial::available (void )
@@ -1103,7 +1114,7 @@ void BluetoothSerial::flush()
1103
1114
{
1104
1115
while (uxQueueMessagesWaiting (_spp_tx_queue) > 0 )
1105
1116
{
1106
- delay (2 );
1117
+ delay (2 ); // https://github.com/espressif/arduino-esp32/pull/9905
1107
1118
}
1108
1119
}
1109
1120
}
@@ -1145,6 +1156,11 @@ void BluetoothSerial::onAuthComplete(AuthCompleteCb cb)
1145
1156
1146
1157
void BluetoothSerial::confirmReply (boolean confirm)
1147
1158
{
1159
+ #if (ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO)
1160
+ char bda_str[18 ];
1161
+ log_i (" BluetoothSerial::confirmReply : %s : %s" ,
1162
+ bda2str (current_bd_addr, bda_str, 18 ), confirm ? " true" : " false" );
1163
+ #endif
1148
1164
esp_bt_gap_ssp_confirm_reply (current_bd_addr, confirm);
1149
1165
}
1150
1166
@@ -1703,19 +1719,21 @@ void BluetoothSerial::deleteAllBondedDevices()
1703
1719
}
1704
1720
}
1705
1721
1722
+ // aclConnected() is a one-shot. __aclConnected is cleared when returning true
1706
1723
bool BluetoothSerial::aclConnected ()
1707
1724
{
1708
- if (_aclConnected == true )
1725
+ if (_aclConnected)
1709
1726
{
1710
1727
_aclConnected = false ;
1711
- return ( true ) ;
1728
+ return true ;
1712
1729
}
1713
- return (false );
1730
+
1731
+ return false ;
1714
1732
}
1715
1733
1716
- uint8_t * BluetoothSerial::aclGetAddress ()
1734
+ uint8_t * BluetoothSerial::aclGetAddress ()
1717
1735
{
1718
- return (_aclAddress);
1736
+ return (& _aclAddress[ 0 ] );
1719
1737
}
1720
1738
1721
1739
#endif // defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED)
0 commit comments