Closed sdetweil closed 1 year ago
I need to change the service and UUID being served, so I have decided to try a different approach, as BLEServer has add and remove service, I will create the services statically (well pointers to objects)
assign the 1st at startup when the conditions change, I have an array of 2 services, with a variable that controls which I use
int trigger = !status;
pServer->removeService(activeService[trigger], false);
pServer->addService(activeService[status]);
pAdvertising->addServiceUUID(activeService[status]->getUUID());
pServer->startAdvertising();
this works, but shortly later there is a crash
13:19:52.712 -> reporting change of conditions
13:19:52.747 -> need to restart the service with updated UUID
13:19:52.747 -> trigger=0
13:19:52.747 -> status=1
13:19:52.747 -> after restart advertising
13:19:52.747 -> updated service
13:19:52.747 -> end of changed check
13:19:55.266 -> already advertising to our app
13:19:57.758 -> already advertising to our app
13:20:00.290 -> already advertising to our app
13:20:02.787 -> already advertising to our app
13:20:04.781 -> some 12 seconds after the service changed ---- I did bring up an iphone BLE Scanner app to check the UUID (which as correct)
13:20:04.781 -> assert failed: ble_svc_gap_init ble_svc_gap.c:302 (rc == 0)
the stack trace looks like this
Stack trace:
0x40083d39: panic_abort at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/panic.c: line 408'
0x40092e01: esp_system_abort at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/esp_system.c: line 137'
0x40098a95: __assert_func at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/newlib/assert.c: line 85'
0x400d7ca7: ble_svc_gap_init at /Users/sam/Documents/Arduino/libraries/NimBLE-Arduino/src/nimble/nimble/host/services/gap/src/ble_svc_gap.c: line 302 (discriminator 1)'
0x400d57c1: NimBLEServer::resetGATT() at /Users/sam/Documents/Arduino/libraries/NimBLE-Arduino/src/NimBLEServer.cpp:720\n (inlined by) NimBLEServer::resetGATT() at /Users/sam/Documents/Arduino/libraries/NimBLE-Arduino/src/NimBLEServer.cpp: line 713'
0x400d5af6: NimBLEServer::handleGapEvent(ble_gap_event*, void*) at /Users/sam/Documents/Arduino/libraries/NimBLE-Arduino/src/NimBLEServer.cpp: line 395'
0x400d9a31: ble_gap_call_event_cb at /Users/sam/Documents/Arduino/libraries/NimBLE-Arduino/src/nimble/nimble/host/src/ble_gap.c: line 748'
0x400da625: ble_gap_conn_broken at /Users/sam/Documents/Arduino/libraries/NimBLE-Arduino/src/nimble/nimble/host/src/ble_gap.c: line 1306'
0x400da64c: ble_gap_rx_disconn_complete at /Users/sam/Documents/Arduino/libraries/NimBLE-Arduino/src/nimble/nimble/host/src/ble_gap.c: line 1334 (discriminator 4)'
0x400de6ee: ble_hs_hci_evt_disconn_complete at /Users/sam/Documents/Arduino/libraries/NimBLE-Arduino/src/nimble/nimble/host/src/ble_hs_hci_evt.c:209\n (inlined by) ble_hs_hci_evt_disconn_complete at /Users/sam/Documents/Arduino/libraries/NimBLE-Arduino/src/nimble/nimble/host/src/ble_hs_hci_evt.c: line 166'
0x400de901: ble_hs_hci_evt_process at /Users/sam/Documents/Arduino/libraries/NimBLE-Arduino/src/nimble/nimble/host/src/ble_hs_hci_evt.c: line 879'
0x400dd275: ble_hs_event_rx_hci_ev at /Users/sam/Documents/Arduino/libraries/NimBLE-Arduino/src/nimble/nimble/host/src/ble_hs.c: line 548'
0x400813ce: ble_npl_event_run at /Users/sam/Documents/Arduino/libraries/NimBLE-Arduino/src/nimble/porting/npl/freertos/include/nimble/nimble_npl_os.h:526\n (inlined by) nimble_port_run at /Users/sam/Documents/Arduino/libraries/NimBLE-Arduino/src/nimble/porting/nimble/src/nimble_port.c: line 269'
0x400d49fb: NimBLEDevice::host_task(void*) at /Users/sam/Documents/Arduino/libraries/NimBLE-Arduino/src/NimBLEDevice.cpp: line 83
I am using library 1.4.1
void
ble_svc_gap_init(void)
{
#if NIMBLE_BLE_CONNECT
int rc;
#endif
/* Ensure this function only gets called by sysinit. */
SYSINIT_ASSERT_ACTIVE();
#if NIMBLE_BLE_CONNECT
rc = ble_gatts_count_cfg(ble_svc_gap_defs);
SYSINIT_PANIC_ASSERT(rc == 0);
rc = ble_gatts_add_svcs(ble_svc_gap_defs);
SYSINIT_PANIC_ASSERT(rc == 0); //<----- this line did panic, rc = 1
#endif
}
called from
void NimBLEServer::resetGATT() {
if(getConnectedCount() > 0) {
return;
}
NimBLEDevice::stopAdvertising();
ble_gatts_reset();
ble_svc_gap_init(); // <--- here
ble_svc_gatt_init();
for(auto it = m_svcVec.begin(); it != m_svcVec.end(); ) {
if ((*it)->m_removed > 0) {
if ((*it)->m_removed == NIMBLE_ATT_REMOVE_DELETE) {
delete *it;
it = m_svcVec.erase(it);
} else {
++it;
}
continue;
}
(*it)->start();
++it;
}
no matter what I do, the client still sees both services advertised
in setup
// create and save a service instance
activeService[index]= pServer->createService(ServiceUUID->c_str()); // <--- creating new service, on new server() instance
// but remove it from the server
pServer->removeService( activeService[index],true);
and then
if(index==0){ // thru the loop on 1st or last time, 0-->2, or 2--->0
#ifdef ESP32
if (pAdvertising == null) {
pAdvertising = BLEDevice::getAdvertising(); // init the advertising pointer
pAdvertising->setScanResponse(true); // set scan response (one time)
}
pAdvertising->addServiceUUID(activeService[index]->getUUID()); add this uuid to the adverting data... (but its supposedly not added to the server
then at runtime, on the switch
int trigger - = !status; // get the inverse of curent status (0 or 1 toggle)
pAdvertising->removeServiceUUID(activeService[trigger]->getUUID()); // remove it from advertising
pServer->removeService(activeService[trigger],true); // remove it from server, delete, not just hidden
pServer->addService(activeService[status]); // add the other service
pAdvertising->addServiceUUID(activeService[status]->getUUID()); // add it to advertising
pAdvertising->start(); // and start advertising...
client ble characteristic discover
{
"status": "discovered",
"services": [
{
"characteristics": [
{
"descriptors": [],
"properties": {
"write": true
},
"uuid": "9A61"
},
{
"descriptors": [],
"properties": {
"write": true
},
"uuid": "9A62"
},
{
"descriptors": [],
"properties": {
"write": true,
"read": true
},
"uuid": "9AFF"
},
{
"descriptors": [
{
"uuid": "2902"
}
],
"properties": {
"notify": true,
"read": true
},
"uuid": "2A05"
},
{
"descriptors": [],
"properties": {
"read": true
},
"uuid": "9A63"
}
],
"uuid": "11050011-27B9-42F0-82AA-2E951747BBF9"
},
{
"characteristics": [
{
"descriptors": [],
"properties": {
"write": true
},
"uuid": "9A61"
},
{
"descriptors": [],
"properties": {
"write": true
},
"uuid": "9A62"
},
{
"descriptors": [],
"properties": {
"write": true,
"read": true
},
"uuid": "9AFF"
},
{
"descriptors": [],
"properties": {
"notify": true,
"read": true
},
"uuid": "2A05"
},
{
"descriptors": [],
"properties": {
"read": true
},
"uuid": "9A63"
}
],
"uuid": "11050001-27B9-42F0-82AA-2E951747BBF9"
}
],
"name": "c8:f0:9e:75:1a:26",
"address": "97CAB758-A70B-9867-D2D5-4C678EF5BD1D"
}
I think #544 may be of help here.
no need to delete/recreate
in my business solution
on ArduinoBLE and ESP32 BLE I have to basically destroy all the setup undo the service , and characteristics, and server and rebuild them.
issues
the code at time to change UUID
from the setupservice function
include setup