esp32-ota-with-ble-setup icon indicating copy to clipboard operation
esp32-ota-with-ble-setup copied to clipboard

BLE library stack size

Open vtomanov opened this issue 4 years ago • 10 comments

Any suggestion how I can increase the stack size in the BLE library (callbacks) ?

vtomanov avatar Apr 07 '20 18:04 vtomanov

Usually its not needed, but you can start new task and start BLE from it.

chegewara avatar Apr 07 '20 23:04 chegewara

Where I can find a example how to start my own task and start the BLE from it ? (so far I have seen only examples that the BLE LIB is doing everything it self)

vtomanov avatar Apr 08 '20 09:04 vtomanov

Its common use. Just create normal task, let say somewhere on start your app, then move to it all BLE related functions, like init, start advertising or start scan etc.

chegewara avatar Apr 08 '20 10:04 chegewara

you mean the BLE lib checks of it runs on the main thread/task and if YES create its own but in NOT then continue running on the current one ?

e.g. if it is started inside a RTOS task/thread the callback will be the same task/thread ? (as currently the callbacks are on another task/thread)

this is the current scenario - all called on the setup() function 👍


BLEDevice::init(String(BLE_NAME_PREFIX + String(GetSetupDeviceName()) + ")").c_str()); BLEDevice::setMTU(BLE_DEFAULT_MTU); // Create the BLE Server bleServer = BLEDevice::createServer(); bleServer->setCallbacks(new bleServiceCallbacks());

// Create the BLE Service BLEService *pService = bleServer->createService(BLE_SERVICE_UUID);

// Create input BLE Characteristic bleCharacteristicInTransport = pService->createCharacteristic( BLE_CHARACTERISTIC_TRANSPORT_IN_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE );

bleCharacteristicInTransport->setCallbacks(new bleCharacteristicInTransportCallback());

// Create a BLE Descriptor bleCharacteristicInTransport->addDescriptor(new BLE2902());

// Create output BLE Characteristic bleCharacteristicOutTransport = pService->createCharacteristic( BLE_CHARACTERISTIC_TRANSPORT_OUT_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE );

// Create a BLE Descriptor bleCharacteristicOutTransport->addDescriptor(new BLE2902());

// Start the service pService->start();

LOG64_SET(F("BLE: CONNECTED : START NOTIFY")); LOG64_NEW_LINE; // Start advertising

bleServer->getAdvertising()->setScanResponse(true); bleServer->getAdvertising()->setMinPreferred(0x06); bleServer->getAdvertising()->setMaxPreferred(0x12); bleServer->getAdvertising()->start();

vtomanov avatar Apr 08 '20 10:04 vtomanov

This looks like usual use case, in which default arduino stack should be enough. If you have problem with stack i would suggest to check stack water mark.

You can create new task in setup, and like i said earlier, move all BLE init stuff to new task. Callbacks will be called from new task, but global data can be accessible from other tasks too, also in loop.

chegewara avatar Apr 08 '20 10:04 chegewara

The issue is inside the callbacks - e.g. I want to change the callbacks task stack size..

vtomanov avatar Apr 08 '20 16:04 vtomanov

Do you have logs? Do you track stack watermark?

chegewara avatar Apr 08 '20 17:04 chegewara

also I have some issue with the BLE lib - if I leave a mobile phone connected to the BLEServer for about 24h with constant small data exchange e.g. log messages - sometime after closing the app on the mobile phone the BLE server do not issue the void onDisconnect(BLEServer* pServer) callback and I need to restart the device to get the BLE working again as the bleServer->startAdvertising(); // restart advertising is called only after disconnect

vtomanov avatar Apr 23 '20 19:04 vtomanov

and if I enable the verbose from time to time the ESP32 restarts with the watchdog triggered from BLE

[V][BLEServer.cpp:271] handleGATTServerEvent(): << handleGATTServerEvent 23:18:53: [D][BLEDevice.cpp:102] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... Unknown 23:18:53: [V][BLEUtils.cpp:1530] dumpGattServerEvent(): GATT ServerEvent: Unknown 23:18:53: [V][BLEUtils.cpp:1720] dumpGattServerEvent(): dumpGattServerEvent: *** NOT CODED *** 23:18:53: [V][BLEServer.cpp:144] handleGATTServerEvent(): >> handleGATTServerEvent: Unknown 23:18:53: [V][BLECharacteristic.cpp:198] handleGATTServerEvent(): >> handleGATTServerEvent: Unknown 23:18:53: [V][BLECharacteristic.cpp:452] handleGATTServerEvent(): << handleGATTServerEvent 23:18:53: [V][BLECharacteristic.cpp:198] handleGATTServerEvent(): >> handleGATTServerEvent: Unknown 23:18:53: [V][BLECharacteristic.cpp:452] handleGATTServerEvent(): << handleGATTServerEvent 23:18:53: [V][BLEServer.cpp:271] handleGATTServerEvent(): << handleGATTServerEvent 23:18:53: [D][BLEDevice.cpp:102] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... Unknown 23:18:53: [V][BLEUtils.cpp:1530] dumpGattServerEvent(): GATT ServerEvent: Unknown 23:18:53: [V][BLEUtils.cpp:1720] dumpGattServerEvent(): dumpGattServerEvent: *** NOT CODED *** 23:18:53: [V][BLEServer.cpp:144] handleGATTServerEvent(): >> handleGATTServerEvent: Unknown 23:18:53: [V][BLECharacteristic.cpp:198] handleGATTServerEvent(): >> handleGATTServerEvent: Unknown 23:18:53: [V][FreeRTOS.cpp:143] give(): Semaphore giving: name: ConfEvt (0x3fff6394), owner: <N/A> 23:18:53: [V][BLECharacteristic.cpp:452] handleGATTServerEvent(): << handleGATTServerEvent 23:18:53: [V][BLECharacteristic.cpp:198] handleGATTServerEvent(): >> handleGATTServerEvent: Unknown 23:18:53: [V][FreeRTOS.cpp:143] give(): Semaphore giving: name: ConfEvt (0x3fff6df4), owner: <N/A> 23:18:53: [V][BLECharacteristic.cpp:452] handleGATTServerEvent(): << handleGATTServerEvent 23:18:53: [V][BLEServer.cpp:271] handleGATTServerEvent(): << handleGATTServerEvent 23:18:53: [D][BLEDevice.cpp:102] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... Unknown 23:18:53: [V][BLEUtils.cpp:1530] dumpGattServerEvent(): GATT ServerEvent: Unknown 23:18:53: [V][BLEUtils.cpp:1720] dumpGattServerEvent(): dumpGattServerEvent: *** NOT CODED *** 23:18:54: [V][BLESerE (27871) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time: 23:18:54: E (27871) task_wdt: - IDLE0 (CPU 0) 23:18:54: E (27871) task_wdt: Tasks currently running: 23:18:54: E (27871) task_wdt: CPU 0: Btc_task 23:18:54: E (27871) task_wdt: CPU 1: loopTask 23:18:54: E (27871) task_wdt: Aborting. 23:18:54: abort() was called at PC 0x400f37b3 on core 0 23:18:54: 3ffe6f40 0x400ec60a:0x3ffe6fa0 0x400ea2c9:0x3ffe6ff0 0x4015d069:0x3ffe7030 0x401562e6:0x3ffe7070 0x4008f75d:0x3ffe70a0 23:18:54: Rebooting... 23:18:54: Initialized: SERIAL_LOG64
23:18:56: Ver.#3.20.1238

vtomanov avatar Apr 23 '20 22:04 vtomanov

Its either in your code or esp-idf bluetooth stack:

23:18:54: E (27871) task_wdt: CPU 0: Btc_task
23:18:54: E (27871) task_wdt: CPU 1: loopTask

EDIT try to decrease log level to info or debug

chegewara avatar Apr 24 '20 17:04 chegewara