arduino-libraries / ArduinoBLE

ArduinoBLE library for Arduino
GNU Lesser General Public License v2.1
310 stars 205 forks source link

esp32 resets to bootloader when calling BLE.end() before BLE.begin() #273

Open mvandeg-git opened 1 year ago

mvandeg-git commented 1 year ago

Thanks for adding ESP32 support! There seems to be one quirk: When calling BLE.end() before BLE.begin(), the whole device resets and starts the bootloader. Tested on a Beetle-ESP2-C3 from DF-Robot.

On an Arduino this does not seem to cause any issues and BLE gets reset (when BLE.begin() had been called before)/or nothing noticeable happens (when BLE.begin()).

I do not know if BLE.end() also resets the device when BLE.end() is called after BLE.begin(), since I loose the USB Serial connection after BLE has been initialized.

Thanks for looking into this.

dominsch commented 1 year ago

Can you provide a use case where you would need to call end() before begin()? If used correctly it doesn't reset the device if that is your concern.

mvandeg-git commented 1 year ago

Hi Dominsch,

Thanks for your reply. Perhaps I am interpreting some functionality incorrectly.

I envisioned a scenario like this:

BLE setup with BLE.begin() Regular activity Something goes wrong or user (soft) resets device BLE needs to be reset: BLE.end()

I had BLE.end() in an init state function to start with a clean slate regardless of previous initialization. Since there is (as far as I could find) no way to determine the BLE status after BLE.begin(), this was a bit of an ugly hack. I've solved it now by maintaining a status variable.

I mostly filed this, because it was surprising behavior to me that the whole device was reset (in the ESP32 case).

I hope this helps a bit.

Best,

Martijn

On Thu, Nov 17, 2022 at 12:03 PM dominsch @.***> wrote:

Can you provide a use case where you would need to call end() before begin()? If used correctly it doesn't reset the device if that is your concern.

— Reply to this email directly, view it on GitHub https://github.com/arduino-libraries/ArduinoBLE/issues/273#issuecomment-1318464199, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUI4NLGZBHGVRBDTDG7AH7DWIYGH3ANCNFSM6AAAAAARVBLQLE . You are receiving this because you authored the thread.Message ID: @.***>

dominsch commented 1 year ago

This is the end() function:

void HCIVirtualTransportClass::end()
{
  vStreamBufferDelete(rec_buffer);
  vStreamBufferDelete(send_buffer);
  esp_bt_controller_disable();
  esp_bt_controller_deinit();
  vTaskDelete(bleHandle);
}

If you call it before begin it is trying to delete things that haven't been created yet and your program will crash. All memory is freed after a reset so no need to clean up manually.