4040
4141const char *_spp_server_name = " ESP32SPP" ;
4242
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
4745#define SPP_TX_QUEUE_TIMEOUT 1000
4846#define SPP_TX_DONE_TIMEOUT 1000
4947#define SPP_CONGESTED_TIMEOUT 1000
@@ -114,8 +112,7 @@ typedef struct
114112 uint8_t data[];
115113} spp_packet_t ;
116114
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)
119116{
120117 if (bda == NULL || str == NULL || size < 18 )
121118 {
@@ -126,7 +123,6 @@ static char *bda2str(esp_bd_addr_t bda, char *str, size_t size)
126123 snprintf (str, size, " %02x:%02x:%02x:%02x:%02x:%02x" , p[0 ], p[1 ], p[2 ], p[3 ], p[4 ], p[5 ]);
127124 return str;
128125}
129- #endif
130126
131127static bool get_name_from_eir (uint8_t *eir, char *bdname, uint8_t *bdname_len)
132128{
@@ -271,7 +267,7 @@ static void _spp_tx_task(void *arg)
271267 }
272268 else
273269 {
274- log_e (" Something went horribly wrong" );
270+ log_e (" BluetoothSerial _spp_tx_task: something went horribly wrong" );
275271 }
276272 }
277273 vTaskDelete (NULL );
@@ -382,6 +378,7 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
382378 {
383379 log_e (" ESP_SPP_CLOSE_EVT failed!, status:%d" , param->close .status );
384380 }
381+
385382 break ;
386383
387384 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)
414411 {
415412 if (xQueueSend (_spp_rx_queue, param->data_ind .data + i, (TickType_t)0 ) != pdTRUE)
416413 {
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);
418415 break ;
419416 }
420417 }
@@ -563,9 +560,7 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
563560 case ESP_BT_GAP_DEV_PROP_EIR: // Enum 4 - Extended Inquiry Response, value type is uint8_t []
564561 if (get_name_from_eir ((uint8_t *)param->disc_res .prop [i].val , peer_bdname, &peer_bdname_len))
565562 {
566-
567563 log_i (" ESP_BT_GAP_DISC_RES_EVT : EIR : %s : %d" , peer_bdname, peer_bdname_len);
568-
569564 if (strlen (_remote_name) == peer_bdname_len &&
570565 strncmp (peer_bdname, _remote_name, peer_bdname_len) == 0 )
571566 {
@@ -628,15 +623,15 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
628623 case ESP_BT_GAP_AUTH_CMPL_EVT: // Enum 4 - Authentication complete event
629624 if (param->auth_cmpl .stat == ESP_BT_STATUS_SUCCESS)
630625 {
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 );
632627 if (auth_complete_callback)
633628 {
634629 auth_complete_callback (true );
635630 }
636631 }
637632 else
638633 {
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 );
640635 if (auth_complete_callback)
641636 {
642637 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
657652 }
658653 break ;
659654#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
662661 if (confirm_request_callback)
663662 {
664663 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
667666 else
668667 {
669668 // 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 " );
672671 esp_bt_gap_ssp_confirm_reply (param->cfm_req .bda , true );
673672 }
674673 break ;
675674#endif
675+
676676 case ESP_BT_GAP_KEY_NOTIF_EVT: // Enum 7 - Security Simple Pairing Passkey Notification
677677 log_i (" ESP_BT_GAP_KEY_NOTIF_EVT passkey:%d" , param->key_notif .passkey );
678678 break ;
679+
679680#ifdef CONFIG_BT_SSP_ENABLED
680681 case ESP_BT_GAP_KEY_REQ_EVT: // Enum 8 - Security Simple Pairing Passkey request
681682 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
691692 }
692693 break ;
693694#endif
695+
694696 case ESP_BT_GAP_READ_RSSI_DELTA_EVT: // Enum 9 - Read rssi event
695697 log_i (" ESP_BT_GAP_READ_RSSI_DELTA_EVT Read rssi event" );
696698 break ;
697-
698699 case ESP_BT_GAP_CONFIG_EIR_DATA_EVT: // Enum 10 - Config EIR data event
699700 log_i (" ESP_BT_GAP_CONFIG_EIR_DATA_EVT: stat:%d num:%d" , param->config_eir_data .stat ,
700701 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
731732
732733 case ESP_BT_GAP_ACL_CONN_CMPL_STAT_EVT: // Enum 16 - ACL connection complete status event
733734 {
735+ #if (ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO)
734736 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
736740
737741 memcpy (_aclAddress, param->acl_conn_cmpl_stat .bda , ESP_BD_ADDR_LEN);
738742
@@ -757,7 +761,7 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
757761 }
758762}
759763
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 )
761765{
762766 if (!_bt_event_group)
763767 {
@@ -784,7 +788,8 @@ static bool _init_bt(const char *deviceName, bt_mode mode)
784788 }
785789 if (_spp_rx_queue == NULL )
786790 {
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
788793 if (_spp_rx_queue == NULL )
789794 {
790795 log_e (" RX Queue Create Failed" );
@@ -793,7 +798,8 @@ static bool _init_bt(const char *deviceName, bt_mode mode)
793798 }
794799 if (_spp_tx_queue == NULL )
795800 {
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
797803 if (_spp_tx_queue == NULL )
798804 {
799805 log_e (" TX Queue Create Failed" );
@@ -830,14 +836,14 @@ static bool _init_bt(const char *deviceName, bt_mode mode)
830836 esp_bluedroid_status_t bt_state = esp_bluedroid_get_status ();
831837 if (bt_state == ESP_BLUEDROID_STATUS_UNINITIALIZED)
832838 {
833- // #ifdef CONFIG_BT_SSP_ENABLED
839+ // #ifdef CONFIG_BT_SSP_ENABLED
834840#if false
835841 // Start with SSP
836842 esp_bluedroid_config_t bluedroid_cfg = BT_BLUEDROID_INIT_CONFIG_DEFAULT ();
837843 if (_enableSSP == false )
838844 bluedroid_cfg.ssp_en = false ;
839845
840- if (esp_bluedroid_init_with_cfg (&bluedroid_cfg))
846+ if (esp_bluedroid_init_with_cfg (&bluedroid_cfg)) // *** Not supported by IDF5.1 ***
841847 {
842848 log_e (" initialize bluedroid failed" );
843849 return false ;
@@ -882,7 +888,8 @@ static bool _init_bt(const char *deviceName, bt_mode mode)
882888
883889 log_i (" device name set" );
884890 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);
886893
887894#ifdef CONFIG_BT_SSP_ENABLED
888895 if (_enableSSP)
@@ -989,10 +996,12 @@ static bool waitForConnect(int timeout)
989996 }
990997 else if ((rc & SPP_CLOSED) != 0 )
991998 {
992- log_d (" connection closed!" );
999+ if (timeout > 0 )
1000+ log_d (" connection closed!" );
9931001 return false ;
9941002 }
995- log_d (" timeout" );
1003+ if (timeout > 0 )
1004+ log_d (" timeout" );
9961005 return false ;
9971006}
9981007
@@ -1029,14 +1038,16 @@ BluetoothSerial::~BluetoothSerial(void)
10291038 * @param isMaster set to true if you want to connect to an other device
10301039 * @param disableBLE if BLE is not used, its ram can be freed to get +10kB free ram
10311040 */
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)
10331044{
10341045 _isMaster = isMaster;
10351046 if (localName.length ())
10361047 {
10371048 local_name = localName;
10381049 }
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 );
10401051}
10411052
10421053int BluetoothSerial::available (void )
@@ -1103,7 +1114,7 @@ void BluetoothSerial::flush()
11031114 {
11041115 while (uxQueueMessagesWaiting (_spp_tx_queue) > 0 )
11051116 {
1106- delay (2 );
1117+ delay (2 ); // https://github.com/espressif/arduino-esp32/pull/9905
11071118 }
11081119 }
11091120}
@@ -1145,6 +1156,11 @@ void BluetoothSerial::onAuthComplete(AuthCompleteCb cb)
11451156
11461157void BluetoothSerial::confirmReply (boolean confirm)
11471158{
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
11481164 esp_bt_gap_ssp_confirm_reply (current_bd_addr, confirm);
11491165}
11501166
@@ -1703,19 +1719,21 @@ void BluetoothSerial::deleteAllBondedDevices()
17031719 }
17041720}
17051721
1722+ // aclConnected() is a one-shot. __aclConnected is cleared when returning true
17061723bool BluetoothSerial::aclConnected ()
17071724{
1708- if (_aclConnected == true )
1725+ if (_aclConnected)
17091726 {
17101727 _aclConnected = false ;
1711- return ( true ) ;
1728+ return true ;
17121729 }
1713- return (false );
1730+
1731+ return false ;
17141732}
17151733
1716- uint8_t * BluetoothSerial::aclGetAddress ()
1734+ uint8_t * BluetoothSerial::aclGetAddress ()
17171735{
1718- return (_aclAddress);
1736+ return (& _aclAddress[ 0 ] );
17191737}
17201738
17211739#endif // defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED)
0 commit comments