NimBLE-Arduino icon indicating copy to clipboard operation
NimBLE-Arduino copied to clipboard

when creating service in loop, get crash

Open sdetweil opened this issue 2 years ago • 0 comments

unable to get remove/add to work, per #547 , back to adding on the fly, get crash

OTA Start requested
scanning for app task <---- see code below
removing advertised UUID=21010001-27b9-42f0-82aa-2e951747bbf9
NOT nimble creating OTA service
assert failed: ble_svc_gap_init ble_svc_gap.c:302 (rc == 0)


Backtrace: 0x40083d39:0x3ffce8d0 0x40092ea5:0x3ffce8f0 0x40098b39:0x3ffce910 0x400daf23:0x3ffcea40 0x400d9766:0x3ffcea60 0x400d9e10:0x3ffcea80 0x400dd0e5:0x3ffceaf0 0x400de3c2:0x3ffceb10 0x400de3e9:0x3ffceb70 0x400e2aca:0x3ffcebd0 0x400e2e68:0x3ffcebf0 0x400e1585:0x3ffcec10 0x400813ce:0x3ffcec30 0x400d89b7:0x3ffcec50

  #0  0x40083d39:0x3ffce8d0 in panic_abort at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/panic.c:408
  #1  0x40092ea5:0x3ffce8f0 in esp_system_abort at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/esp_system.c:137
  #2  0x40098b39:0x3ffce910 in __assert_func at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/newlib/assert.c:85
  #3  0x400daf23:0x3ffcea40 in ble_svc_gap_init at .pio/libdeps/esp32doit-devkit-v1-ota-nimble/NimBLE-Arduino/src/nimble/nimble/host/services/gap/src/ble_svc_gap.c:302 (discriminator 1)
  #4  0x400d9766:0x3ffcea60 in NimBLEServer::resetGATT() at .pio/libdeps/esp32doit-devkit-v1-ota-nimble/NimBLE-Arduino/src/NimBLEServer.cpp:720
  #5  0x400d9e10:0x3ffcea80 in NimBLEServer::handleGapEvent(ble_gap_event*, void*) at .pio/libdeps/esp32doit-devkit-v1-ota-nimble/NimBLE-Arduino/src/NimBLEServer.cpp:395
  #6  0x400dd0e5:0x3ffceaf0 in ble_gap_call_event_cb at .pio/libdeps/esp32doit-devkit-v1-ota-nimble/NimBLE-Arduino/src/nimble/nimble/host/src/ble_gap.c:756
  #7  0x400de3c2:0x3ffceb10 in ble_gap_conn_broken at .pio/libdeps/esp32doit-devkit-v1-ota-nimble/NimBLE-Arduino/src/nimble/nimble/host/src/ble_gap.c:1314
  #8  0x400de3e9:0x3ffceb70 in ble_gap_rx_disconn_complete at .pio/libdeps/esp32doit-devkit-v1-ota-nimble/NimBLE-Arduino/src/nimble/nimble/host/src/ble_gap.c:1342 (discriminator 4)
  #9  0x400e2aca:0x3ffcebd0 in ble_hs_hci_evt_disconn_complete at .pio/libdeps/esp32doit-devkit-v1-ota-nimble/NimBLE-Arduino/src/nimble/nimble/host/src/ble_hs_hci_evt.c:268
  #10 0x400e2e68:0x3ffcebf0 in ble_hs_hci_evt_process at .pio/libdeps/esp32doit-devkit-v1-ota-nimble/NimBLE-Arduino/src/nimble/nimble/host/src/ble_hs_hci_evt.c:985
  #11 0x400e1585:0x3ffcec10 in ble_hs_event_rx_hci_ev at .pio/libdeps/esp32doit-devkit-v1-ota-nimble/NimBLE-Arduino/src/nimble/nimble/host/src/ble_hs.c:548
  #12 0x400813ce:0x3ffcec30 in ble_npl_event_run at .pio/libdeps/esp32doit-devkit-v1-ota-nimble/NimBLE-Arduino/src/nimble/porting/npl/freertos/include/nimble/nimble_npl_os.h:526
      (inlined by) nimble_port_run at .pio/libdeps/esp32doit-devkit-v1-ota-nimble/NimBLE-Arduino/src/nimble/porting/nimble/src/nimble_port.c:319
  #13 0x400d89b7:0x3ffcec50 in NimBLEDevice::host_task(void*) at .pio/libdeps/esp32doit-devkit-v1-ota-nimble/NimBLE-Arduino/src/NimBLEDevice.cpp:836

code does

  pAdvertising->stop();
  Sprintf("removing advertised UUID=");
  Sprintln(activeService[status]->getUUID().toString().c_str());
  #ifdef USE_NIMBLE
  pAdvertising->removeServiceUUID(activeService[status]->getUUID());
  #endif
  delay(50);
#ifdef USE_NIMBLE1
    Sprintln("nimble adding OTA service");
    pServer->addService(OTAService);
#else
    // start the Over The Air update service
    Sprintln("NOT nimble creating OTA service");
    OTAService = ArduinoBleOTA.begin(pServer, InternalStorage, HW_NAME_INFO.c_str(), HW_VER, SW_NAME.c_str(), SW_VER);
#endif


one key change is to move the scanner (for our app) to a task 

xTaskCreate(scantask, "scantask", 5000, null, 1, &xScanTaskHandle);

void scantask(void * parameter){ for (;;){ Sprintln("scanning for app task"); <----- this message is in between the messages above---------------- BLEScanResults foundDevices = pBLEScanner->start(2 /scanTimeout/1000/, true);

// scan lasts for 2 seconds, SO, this is active when fiddling with the advertise state <---------

// Sprint("scan Devices found: ");
// Sprintln(foundDevices.getCount());
// Sprintln("Scan done!");
for (int i = 0; i < foundDevices.getCount(); i++)
{
  BLEAdvertisedDevice peripheral = foundDevices.getDevice(i);
  // Sprintf(("Advertised Device: %s \n", peripheral.toString().c_str()));
  for (int s = 0; s < peripheral.getServiceUUIDCount(); s++)
  {
    // const char* dev_uuid = peripheral.getServiceUUID(s).toString().c_str();
    // Sprintf(("\tAdvertised service: %s \n", peripheral.getServiceUUID(s).toString().c_str()));
    // Sprintf("App UUID= %s=%s \n",AppUUID.c_str(),peripheral.getServiceUUID(s).toString().c_str());
    if (AppUUID.equalsIgnoreCase(String(peripheral.getServiceUUID(s).toString().c_str())))
    {
      if (!advertising)
      {
        Serial.printf("found our app= %s \n", peripheral.getServiceUUID(s).toString().c_str());
        startAdvertising();
      }
      else
      {
        Sprintln("already advertising to our app");
        ;
      }
      // indicate we heard app
      lastHeard = millis();
      break;
    }
  }
}
pBLEScanner->clearResults();
vTaskDelay(3000/portTICK_PERIOD_MS);

} vTaskDelete(null); }

sdetweil avatar Jun 16 '23 04:06 sdetweil