Keep ESP32 Bluetooth handles stable and refresh paired client caches
This commit is contained in:
1 parent
669107c3db
commit
1087643ae8
9 files changed
+682
-14
No files matched your search
+79
-7
@@ -2,10 +2,23 @@
|
||||
#include <Arduino.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <cstring>
|
||||
#include <cassert>
|
||||
using esp_err_t = int;
|
||||
constexpr esp_err_t ESP_OK = 0;
|
||||
using esp_gatt_if_t = uint8_t;
|
||||
constexpr esp_gatt_if_t ESP_GATT_IF_NONE = 0xff;
|
||||
constexpr int ESP_GATT_OK=0, ESP_UUID_LEN_16=2, ESP_UUID_LEN_128=16;
|
||||
constexpr int ESP_GATT_UUID_PRI_SERVICE=0x2800, ESP_GATT_AUTO_RSP=1, ESP_GATT_PERM_READ=1;
|
||||
constexpr int ESP_BLE_SEC_ENCRYPT_MITM=3;
|
||||
enum esp_gatts_cb_event_t { ESP_GATTS_CONNECT_EVT, ESP_GATTS_DISCONNECT_EVT,
|
||||
ESP_GATTS_CREAT_ATTR_TAB_EVT, ESP_GATTS_START_EVT, ESP_GATTS_DELETE_EVT };
|
||||
struct esp_bt_uuid_t { uint16_t len=16; union { uint16_t uuid16; uint8_t uuid128[16]; } uuid{}; };
|
||||
struct esp_gatts_attr_db_t {
|
||||
struct { uint8_t auto_rsp; } attr_control;
|
||||
struct { uint16_t uuid_length; uint8_t* uuid_p; uint16_t perm,max_length,length; uint8_t* value; } att_desc;
|
||||
};
|
||||
enum esp_bt_controller_status_t { ESP_BT_CONTROLLER_STATUS_IDLE, ESP_BT_CONTROLLER_STATUS_INITED, ESP_BT_CONTROLLER_STATUS_ENABLED };
|
||||
enum esp_bluedroid_status_t { ESP_BLUEDROID_STATUS_UNINITIALIZED, ESP_BLUEDROID_STATUS_INITIALIZED, ESP_BLUEDROID_STATUS_ENABLED };
|
||||
using esp_bd_addr_t = uint8_t[6];
|
||||
@@ -16,7 +29,12 @@ constexpr int ESP_LE_KEY_PID=1, ESP_BLE_ADV_FLAG_BREDR_NOT_SPT=4;
|
||||
constexpr int ESP_GATT_PERM_READ_ENC_MITM=1, ESP_GATT_PERM_WRITE_ENC_MITM=2;
|
||||
constexpr int ESP_IO_CAP_OUT=0, ESP_LE_AUTH_REQ_SC_MITM_BOND=15;
|
||||
struct esp_ble_auth_cmpl_t { bool success=true; int addr_type=0, fail_reason=0; uint8_t bd_addr[6]={1,2,3,4,5,6}; };
|
||||
struct esp_ble_gatts_cb_param_t { struct { uint16_t conn_id=7; } connect, mtu; };
|
||||
struct esp_ble_gatts_cb_param_t {
|
||||
struct { uint16_t conn_id=7; uint8_t remote_bda[6]={1,2,3,4,5,6}; } connect, mtu;
|
||||
struct { int status=0; esp_bt_uuid_t svc_uuid; uint8_t svc_inst_id=0; uint16_t num_handle=1; uint16_t* handles=nullptr; } add_attr_tab;
|
||||
struct { int status=0; uint16_t service_handle=0; } start, del;
|
||||
};
|
||||
using gatts_event_handler=void(*)(esp_gatts_cb_event_t,esp_gatt_if_t,esp_ble_gatts_cb_param_t*);
|
||||
struct esp_ble_bond_dev_t {
|
||||
uint8_t bd_addr[6]={1,2,3,4,5,6};
|
||||
struct { unsigned key_mask=0; struct { uint8_t static_addr[6]={}; int addr_type=0; } pid_key; } bond_key;
|
||||
@@ -32,6 +50,12 @@ inline unsigned servers=0, services=0, characteristics=0, descriptors=0;
|
||||
inline unsigned bond_deletions=0, disconnects=0, stops=0, uuid_adds=0;
|
||||
inline uint8_t address[6]={};
|
||||
inline uint32_t pin=0;
|
||||
inline unsigned encryptions=0, cache_creates=0, cache_starts=0, cache_deletes=0;
|
||||
inline bool in_callback=false;
|
||||
inline const esp_gatts_attr_db_t* marker_attributes=nullptr;
|
||||
inline esp_gatt_if_t marker_if=0;
|
||||
inline uint16_t marker_handle=0x40;
|
||||
inline int esp_ble_set_encryption_result=0;
|
||||
inline BLEServer* server=nullptr;
|
||||
}
|
||||
inline esp_bt_controller_status_t esp_bt_controller_get_status() { return fake_bt::controller; }
|
||||
@@ -58,6 +82,30 @@ inline int esp_ble_get_bond_device_list(int*, esp_ble_bond_dev_t* dest) { *dest=
|
||||
inline int esp_ble_remove_bond_device(uint8_t*) { ++fake_bt::bond_deletions; return ESP_OK; }
|
||||
inline int esp_ble_gap_clear_whitelist() { return ESP_OK; }
|
||||
inline int esp_ble_gap_update_whitelist(bool, esp_bd_addr_t, esp_ble_wl_addr_type_t) { return ESP_OK; }
|
||||
inline int esp_ble_set_encryption(uint8_t*, int mode) {
|
||||
assert(!fake_bt::in_callback && mode==ESP_BLE_SEC_ENCRYPT_MITM);
|
||||
++fake_bt::encryptions;
|
||||
return fake_bt::failure=="encrypt" ? 1 : ESP_OK;
|
||||
}
|
||||
inline int esp_ble_gatts_create_attr_tab(const esp_gatts_attr_db_t* db, esp_gatt_if_t id,
|
||||
uint16_t count, uint8_t instance) {
|
||||
assert(!fake_bt::in_callback && count==1 && instance==0);
|
||||
assert(db->attr_control.auto_rsp==ESP_GATT_AUTO_RSP);
|
||||
assert(*reinterpret_cast<uint16_t*>(db->att_desc.uuid_p)==ESP_GATT_UUID_PRI_SERVICE);
|
||||
assert(db->att_desc.length==16);
|
||||
++fake_bt::cache_creates; fake_bt::marker_attributes=db; fake_bt::marker_if=id;
|
||||
return fake_bt::failure=="cache-create" ? 1 : ESP_OK;
|
||||
}
|
||||
inline int esp_ble_gatts_start_service(uint16_t handle) {
|
||||
assert(!fake_bt::in_callback && handle==fake_bt::marker_handle);
|
||||
++fake_bt::cache_starts;
|
||||
return fake_bt::failure=="cache-start" ? 1 : ESP_OK;
|
||||
}
|
||||
inline int esp_ble_gatts_delete_service(uint16_t handle) {
|
||||
assert(!fake_bt::in_callback && handle==fake_bt::marker_handle);
|
||||
++fake_bt::cache_deletes;
|
||||
return fake_bt::failure=="cache-delete" ? 1 : ESP_OK;
|
||||
}
|
||||
class BLESecurityCallbacks {
|
||||
public:
|
||||
virtual ~BLESecurityCallbacks()=default;
|
||||
@@ -114,16 +162,37 @@ public:
|
||||
static constexpr uint32_t PROPERTY_READ=1, PROPERTY_NOTIFY=2, PROPERTY_WRITE=4;
|
||||
BLECharacteristicCallbacks* callbacks=nullptr;
|
||||
std::vector<uint8_t> value;
|
||||
BLECharacteristic() { ++fake_bt::characteristics; }
|
||||
std::string uuid;
|
||||
uint32_t properties;
|
||||
uint16_t value_handle = 0, cccd_handle = 0;
|
||||
BLE2902* descriptor=nullptr;
|
||||
BLECharacteristic(const char* id = "", uint32_t props = 0)
|
||||
: uuid(id), properties(props) { ++fake_bt::characteristics; }
|
||||
~BLECharacteristic() { --fake_bt::characteristics; }
|
||||
void setCallbacks(BLECharacteristicCallbacks* cb) { callbacks=cb; }
|
||||
void setAccessPermissions(int) {}
|
||||
void addDescriptor(BLE2902*) {}
|
||||
void addDescriptor(BLE2902* value) { descriptor=value; }
|
||||
void setValue(uint8_t* data,size_t len) { value.assign(data,data+len); }
|
||||
uint8_t* getData() { return value.data(); }
|
||||
size_t getLength() { return value.size(); }
|
||||
void notify() { callbacks->onStatus(this,BLECharacteristicCallbacks::SUCCESS_NOTIFY,0); }
|
||||
};
|
||||
// Exact legacy SDK behavior: registration follows a std::map keyed by
|
||||
// object address, rather than UUID or characteristic creation order.
|
||||
inline void registerPointerOrderedCharacteristics(
|
||||
const std::vector<BLECharacteristic*>& characteristics) {
|
||||
std::map<BLECharacteristic*, bool> pointer_map;
|
||||
for (auto* characteristic : characteristics) pointer_map[characteristic] = true;
|
||||
uint16_t next = 0x0029;
|
||||
for (const auto& entry : pointer_map) {
|
||||
auto* characteristic = entry.first;
|
||||
characteristic->value_handle = next + 1;
|
||||
next += 2;
|
||||
characteristic->cccd_handle = 0;
|
||||
if (characteristic->properties & BLECharacteristic::PROPERTY_NOTIFY)
|
||||
characteristic->cccd_handle = next++;
|
||||
}
|
||||
}
|
||||
class BLEService {
|
||||
public:
|
||||
BLEService() { ++fake_bt::services; }
|
||||
@@ -137,14 +206,15 @@ public:
|
||||
#endif
|
||||
std::vector<BLECharacteristic*> children;
|
||||
unsigned made=0;
|
||||
BLECharacteristic* createCharacteristic(const char*,uint32_t) {
|
||||
void addCharacteristic(BLECharacteristic* child) { children.push_back(child); }
|
||||
BLECharacteristic* createCharacteristic(const char* id,uint32_t properties) {
|
||||
const char* phase = made++ ? "rx" : "tx";
|
||||
if (fake_bt::failure==phase) return nullptr;
|
||||
auto* child = new BLECharacteristic;
|
||||
children.push_back(child);
|
||||
auto* child = new BLECharacteristic(id, properties);
|
||||
addCharacteristic(child);
|
||||
return child;
|
||||
}
|
||||
void start() {}
|
||||
void start() { registerPointerOrderedCharacteristics(children); }
|
||||
};
|
||||
inline BLEAdvertising* fake_advertising=nullptr;
|
||||
class BLEServer {
|
||||
@@ -170,6 +240,8 @@ public:
|
||||
class BLEDevice {
|
||||
public:
|
||||
inline static BLESecurityCallbacks* security=nullptr;
|
||||
inline static gatts_event_handler m_customGattsHandler=nullptr;
|
||||
static void setCustomGattsHandler(gatts_event_handler handler) { m_customGattsHandler=handler; }
|
||||
static void init(const std::string& name) {
|
||||
fake_bt::name=name;
|
||||
if(fake_bt::failure=="init") return;
|
||||
|
||||
+172
-1
@@ -1,8 +1,26 @@
|
||||
#include <helpers/esp32/SerialBLEInterface.h>
|
||||
#include <cassert>
|
||||
#include <new>
|
||||
using namespace fake_bt;
|
||||
static const uint8_t custom[]={0xc2,1,2,3,4,5};
|
||||
static unsigned suites;
|
||||
void* operator new(std::size_t size, const std::nothrow_t&) noexcept {
|
||||
return failure == "characteristics" ? nullptr : ::operator new(size);
|
||||
}
|
||||
void operator delete(void* ptr, const std::nothrow_t&) noexcept { ::operator delete(ptr); }
|
||||
static void pointer_map_reproduces_cached_cccd_failure() {
|
||||
// Reversed heap addresses register RX before TX even when TX was added
|
||||
// first, exactly reproducing the physical2B->2D cached-handle failure.
|
||||
struct Reversed {
|
||||
BLECharacteristic rx{"6E400002", BLECharacteristic::PROPERTY_WRITE};
|
||||
BLECharacteristic tx{"6E400003", BLECharacteristic::PROPERTY_NOTIFY};
|
||||
} reversed;
|
||||
registerPointerOrderedCharacteristics({&reversed.tx, &reversed.rx});
|
||||
assert(reversed.rx.value_handle == 0x002a);
|
||||
assert(reversed.tx.cccd_handle == 0x002d);
|
||||
assert(reversed.tx.cccd_handle != 0x002b); // Old cached write is invalid.
|
||||
++suites;
|
||||
}
|
||||
static void retire_global() {
|
||||
delete server; server=nullptr;
|
||||
delete fake_advertising; fake_advertising=nullptr;
|
||||
@@ -25,6 +43,13 @@ static void good(SerialBLEInterface& a) {
|
||||
assert(servers==1 && services==1 && characteristics==2 && descriptors==1);
|
||||
assert(name=="MeshCore-legacy" && pin==246810);
|
||||
assert(memcmp(address,custom,6)==0 && fake_advertising->active);
|
||||
auto* tx = server->service->children[0];
|
||||
auto* rx = server->service->children[1];
|
||||
assert(tx->uuid == "6E400003-B5A3-F393-E0A9-E50E24DCCA9E");
|
||||
assert(rx->uuid == "6E400002-B5A3-F393-E0A9-E50E24DCCA9E");
|
||||
assert(reinterpret_cast<uintptr_t>(tx) < reinterpret_cast<uintptr_t>(rx));
|
||||
assert(tx->value_handle == 0x002a && tx->cccd_handle == 0x002b);
|
||||
assert(rx->value_handle == 0x002d); // Saved clients never chase heap order.
|
||||
}
|
||||
static void repeated_cycles() {
|
||||
SerialBLEInterface a; assert(a.begin("MeshCore-","legacy",246810,custom));
|
||||
@@ -60,7 +85,7 @@ static void partial_stop_failures_and_late_callbacks() {
|
||||
++suites;
|
||||
}
|
||||
static void startup_failures() {
|
||||
for(const char* fault : {"init","server","service","tx","rx"}) {
|
||||
for(const char* fault : {"init","server","service","characteristics"}) {
|
||||
SerialBLEInterface a; assert(a.begin("MeshCore-","legacy",246810,custom)); a.enable(); stopped(a);
|
||||
failure=fault; a.enable();
|
||||
assert(!a.isEnabled() && !a.isConnected()); a.servicePowerState();
|
||||
@@ -70,7 +95,153 @@ static void startup_failures() {
|
||||
}
|
||||
++suites;
|
||||
}
|
||||
#if MESH_BLE_AUTO_CACHE_REFRESH
|
||||
static unsigned chained_events;
|
||||
static void prior_handler(esp_gatts_cb_event_t, esp_gatt_if_t, esp_ble_gatts_cb_param_t*) {
|
||||
++chained_events;
|
||||
}
|
||||
static void tick(SerialBLEInterface& a) { test_millis += 1000; a.servicePowerState(); }
|
||||
static void emit(esp_gatts_cb_event_t event, esp_ble_gatts_cb_param_t& param, esp_gatt_if_t id=3) {
|
||||
assert(BLEDevice::m_customGattsHandler);
|
||||
in_callback=true;
|
||||
BLEDevice::m_customGattsHandler(event,id,¶m);
|
||||
in_callback=false;
|
||||
}
|
||||
static void connect_cache(SerialBLEInterface& a, uint8_t peer=6) {
|
||||
server->connected=true;
|
||||
esp_ble_gatts_cb_param_t param; param.connect.remote_bda[5]=peer;
|
||||
server->callbacks->onConnect(server,¶m);
|
||||
emit(ESP_GATTS_CONNECT_EVT,param);
|
||||
tick(a); // Requests MITM without relying on a possibly stale NUS handle.
|
||||
}
|
||||
static void auth_cache(uint8_t peer=6) {
|
||||
esp_ble_auth_cmpl_t auth; auth.bd_addr[5]=peer;
|
||||
BLEDevice::security->onAuthenticationComplete(auth);
|
||||
}
|
||||
static void disconnect_cache() {
|
||||
server->connected=false;
|
||||
server->callbacks->onDisconnect(server);
|
||||
esp_ble_gatts_cb_param_t param; emit(ESP_GATTS_DISCONNECT_EVT,param);
|
||||
}
|
||||
static void created_cache(int status=ESP_GATT_OK, esp_gatt_if_t id=3, bool correct=true, uint8_t instance=0) {
|
||||
esp_ble_gatts_cb_param_t param; uint16_t handle=marker_handle;
|
||||
param.add_attr_tab.status=status;
|
||||
memcpy(param.add_attr_tab.svc_uuid.uuid.uuid128,marker_attributes->att_desc.value,16);
|
||||
if(!correct) param.add_attr_tab.svc_uuid.uuid.uuid128[0]^=1;
|
||||
param.add_attr_tab.svc_inst_id=instance;
|
||||
param.add_attr_tab.handles=&handle;
|
||||
emit(ESP_GATTS_CREAT_ATTR_TAB_EVT,param,id);
|
||||
handle=0; // The backend must have copied the ephemeral callback array.
|
||||
}
|
||||
static void changed_cache(bool starting, int status=ESP_GATT_OK, uint16_t handle=0) {
|
||||
esp_ble_gatts_cb_param_t param;
|
||||
auto& change=starting ? param.start : param.del;
|
||||
change.status=status; change.service_handle=handle ? handle : marker_handle;
|
||||
emit(starting ? ESP_GATTS_START_EVT : ESP_GATTS_DELETE_EVT,param);
|
||||
}
|
||||
static void reset_cache_stats() {
|
||||
encryptions=cache_creates=cache_starts=cache_deletes=chained_events=0;
|
||||
failure.clear(); marker_attributes=nullptr;
|
||||
BLEDevice::setCustomGattsHandler(prior_handler);
|
||||
}
|
||||
static void verify_nus(SerialBLEInterface& a) {
|
||||
auto* tx=server->service->children[0]; auto* rx=server->service->children[1];
|
||||
uint8_t frame[]={22}; rx->setValue(frame,sizeof(frame));
|
||||
// A cached CCCD hitting RX is insufficient while actual TX is unsubscribed.
|
||||
esp_ble_gatts_cb_param_t param;
|
||||
rx->callbacks->onWrite(rx,¶m); tick(a);
|
||||
tx->descriptor->setNotifications(true);
|
||||
rx->callbacks->onWrite(rx,¶m); tick(a);
|
||||
}
|
||||
static void cache_async_migration_and_real_nus_proof() {
|
||||
reset_cache_stats();
|
||||
SerialBLEInterface a; assert(a.begin("MeshCore-","legacy",246810,custom)); a.enable();
|
||||
connect_cache(a); assert(encryptions==1 && cache_creates==0);
|
||||
auth_cache(7); tick(a); assert(cache_creates==0); // Stale auth from another peer.
|
||||
auth_cache(); tick(a); assert(cache_creates==1 && cache_starts==0);
|
||||
assert(marker_attributes->att_desc.value[0]==0x91); // Static queued data remains alive.
|
||||
created_cache(0,4); created_cache(0,3,false); created_cache(0,3,true,1);
|
||||
tick(a); assert(cache_starts==0);
|
||||
created_cache(); created_cache(); tick(a); assert(cache_starts==1);
|
||||
changed_cache(true,0,marker_handle+1); tick(a); assert(cache_deletes==0);
|
||||
changed_cache(true); changed_cache(true); tick(a);
|
||||
uint8_t stale_cccd[]={1,0};
|
||||
auto* rx=server->service->children[1]; rx->setValue(stale_cccd,sizeof(stale_cccd));
|
||||
esp_ble_gatts_cb_param_t stale_write;
|
||||
rx->callbacks->onWrite(rx,&stale_write); tick(a); // Old CCCD2D is now RX, actual TX remains off.
|
||||
disconnect_cache(); connect_cache(a); auth_cache(); tick(a);
|
||||
assert(cache_deletes==1); // Local START was not proof that this peer repaired its cache.
|
||||
changed_cache(false,1); tick(a); assert(cache_deletes==2); // Known failure retry.
|
||||
changed_cache(false); tick(a);
|
||||
verify_nus(a);
|
||||
disconnect_cache(); connect_cache(a); auth_cache(); tick(a);
|
||||
assert(cache_creates==1 && cache_deletes==2); // Real handle proof permits fast reconnect.
|
||||
disconnect_cache(); connect_cache(a,7); auth_cache(7); tick(a);
|
||||
assert(cache_creates==2); // A different peer still needs migration.
|
||||
created_cache(); tick(a); changed_cache(true); tick(a);
|
||||
good(a); disconnect_cache(); stopped(a);
|
||||
assert(BLEDevice::m_customGattsHandler==prior_handler && bond_deletions==0);
|
||||
assert(chained_events>0); retire_global(); BLEDevice::setCustomGattsHandler(nullptr); ++suites;
|
||||
}
|
||||
static void cache_disconnect_pending_timeout_and_teardown() {
|
||||
for(const char* stage : {"create","start","delete"}) {
|
||||
reset_cache_stats();
|
||||
SerialBLEInterface a; assert(a.begin("MeshCore-","legacy",246810,custom)); a.enable();
|
||||
connect_cache(a); auth_cache(); tick(a); assert(cache_creates==1);
|
||||
if(std::string(stage)!="create") {
|
||||
created_cache(); tick(a); assert(cache_starts==1);
|
||||
if(std::string(stage)=="delete") {
|
||||
// Completion while disconnected must not mark its peer cache repaired.
|
||||
disconnect_cache(); changed_cache(true); tick(a);
|
||||
connect_cache(a); auth_cache(); tick(a); assert(cache_deletes==1);
|
||||
}
|
||||
}
|
||||
test_millis+=6000; a.servicePowerState();
|
||||
const unsigned before=cache_creates+cache_starts+cache_deletes;
|
||||
tick(a); assert(cache_creates+cache_starts+cache_deletes==before); // No orphan retry.
|
||||
disconnect_cache(); connect_cache(a,7); auth_cache(7); tick(a);
|
||||
assert(cache_creates+cache_starts+cache_deletes==before);
|
||||
disconnect_cache(); stopped(a);
|
||||
assert(BLEDevice::m_customGattsHandler==prior_handler);
|
||||
esp_ble_gatts_cb_param_t late; emit(ESP_GATTS_START_EVT,late); // Restored hook only.
|
||||
a.enable(); good(a); stopped(a); retire_global();
|
||||
BLEDevice::setCustomGattsHandler(nullptr);
|
||||
}
|
||||
++suites;
|
||||
}
|
||||
static void cache_api_and_async_failures_are_bounded() {
|
||||
for(const char* fault : {"encrypt","cache-create","async-create","malformed-create","cache-start"}) {
|
||||
reset_cache_stats();
|
||||
SerialBLEInterface a; assert(a.begin("MeshCore-","legacy",246810,custom)); a.enable();
|
||||
if(std::string(fault)=="encrypt") failure=fault;
|
||||
connect_cache(a); auth_cache();
|
||||
if(std::string(fault)=="cache-create") failure=fault;
|
||||
tick(a);
|
||||
if(std::string(fault)=="async-create") { created_cache(1); tick(a); created_cache(1); tick(a); created_cache(1); }
|
||||
if(std::string(fault)=="malformed-create") {
|
||||
esp_ble_gatts_cb_param_t param;
|
||||
memcpy(param.add_attr_tab.svc_uuid.uuid.uuid128,marker_attributes->att_desc.value,16);
|
||||
param.add_attr_tab.num_handle=2; // An unknown successful allocation cannot be retried safely.
|
||||
emit(ESP_GATTS_CREAT_ATTR_TAB_EVT,param);
|
||||
}
|
||||
if(std::string(fault)=="cache-start") { created_cache(); failure=fault; }
|
||||
tick(a); tick(a); tick(a); tick(a);
|
||||
const unsigned attempts=encryptions+cache_creates+cache_starts;
|
||||
tick(a); assert(encryptions+cache_creates+cache_starts==attempts);
|
||||
assert(encryptions<=3 && cache_creates<=3 && cache_starts<=3);
|
||||
failure.clear(); disconnect_cache(); stopped(a); retire_global();
|
||||
BLEDevice::setCustomGattsHandler(nullptr);
|
||||
}
|
||||
++suites;
|
||||
}
|
||||
#endif
|
||||
int main() {
|
||||
#if MESH_BLE_AUTO_CACHE_REFRESH
|
||||
cache_async_migration_and_real_nus_proof();
|
||||
cache_disconnect_pending_timeout_and_teardown();
|
||||
cache_api_and_async_failures_are_bounded();
|
||||
#endif
|
||||
pointer_map_reproduces_cached_cccd_failure();
|
||||
repeated_cycles(); partial_stop_failures_and_late_callbacks(); startup_failures();
|
||||
// The legacy library deliberately retains its static server/advertiser
|
||||
// until replacement. Release the final objects after the host has stopped.
|
||||
|
||||
@@ -14,12 +14,16 @@ class Esp32BlePowerTests(unittest.TestCase):
|
||||
ROOT / "test/fixtures/nimble_companion/mocks")
|
||||
|
||||
def test_bluedroid_legacy_static_ownership_and_partial_stop_failures(self):
|
||||
self.run_adapter("test_bluedroid_power.cpp", ["-DCONFIG_BLUEDROID_ENABLED=1"],
|
||||
self.run_adapter("test_bluedroid_power.cpp", ["-DCONFIG_BLUEDROID_ENABLED=1", "-DCONFIG_BT_GATTS_SEND_SERVICE_CHANGE_AUTO=1"],
|
||||
ROOT / "test/fixtures/esp32_ble_power/mocks")
|
||||
|
||||
def test_bluedroid_new_library_ownership_and_partial_stop_failures(self):
|
||||
self.run_adapter("test_bluedroid_power.cpp", ["-DCONFIG_BLUEDROID_ENABLED=1",
|
||||
"-DTEST_BLE_OWNED_SERVER=1"],
|
||||
"-DTEST_BLE_OWNED_SERVER=1", "-DCONFIG_BT_GATTS_SEND_SERVICE_CHANGE_AUTO=1"],
|
||||
ROOT / "test/fixtures/esp32_ble_power/mocks")
|
||||
|
||||
def test_bluedroid_manual_mode_keeps_marker_dependency_disabled(self):
|
||||
self.run_adapter("test_bluedroid_power.cpp", ["-DCONFIG_BLUEDROID_ENABLED=1"],
|
||||
ROOT / "test/fixtures/esp32_ble_power/mocks")
|
||||
|
||||
def run_adapter(self, fixture, flags, mocks):
|
||||
|
||||
@@ -182,7 +182,7 @@ class NativeNimbleStopTests(unittest.TestCase):
|
||||
cpp.write_text(harness)
|
||||
built = subprocess.run([
|
||||
os.environ.get("CXX", "c++"), "-std=c++17", "-Wall", "-Wextra", "-Werror",
|
||||
"-fsanitize=address,undefined", "-DCONFIG_NIMBLE_ENABLED=1",
|
||||
"-fsanitize=address,undefined", "-DCONFIG_NIMBLE_ENABLED=1", "-DMESH_BLE_USES_NIMBLE=1",
|
||||
str(cpp), "-o", str(exe)], text=True, capture_output=True, timeout=30)
|
||||
self.assertEqual(built.returncode, 0, built.stderr)
|
||||
result = subprocess.run([str(exe)], text=True, capture_output=True, timeout=10)
|
||||
@@ -193,7 +193,7 @@ class NativeNimbleStopTests(unittest.TestCase):
|
||||
"!_nativeHostStopped")
|
||||
cpp.write_text(broken)
|
||||
built = subprocess.run([
|
||||
os.environ.get("CXX", "c++"), "-std=c++17", "-DCONFIG_NIMBLE_ENABLED=1",
|
||||
os.environ.get("CXX", "c++"), "-std=c++17", "-DCONFIG_NIMBLE_ENABLED=1", "-DMESH_BLE_USES_NIMBLE=1",
|
||||
str(cpp), "-o", str(exe)], text=True, capture_output=True, timeout=30)
|
||||
self.assertEqual(built.returncode, 0, built.stderr)
|
||||
result = subprocess.run([str(exe)], text=True, capture_output=True, timeout=10)
|
||||
|
||||
@@ -130,6 +130,10 @@ TIMER = r'''
|
||||
#include <cassert>
|
||||
#include <helpers/PowerSavingDiagnostics.h>
|
||||
#include <helpers/PowerManagementUtils.h>
|
||||
#if defined(ESP32_PLATFORM)
|
||||
int64_t esp_timer_get_time(){return static_cast<int64_t>(millis())*1000;}
|
||||
#include <helpers/esp32/ProfileIdleSleep.h>
|
||||
#endif
|
||||
using TimerHandle_t=void*;
|
||||
using TickType_t=uint32_t;
|
||||
bool allocate=true,queue_ok=true,active=false;
|
||||
@@ -159,16 +163,21 @@ public:
|
||||
static SoftwareTimer tracker_wake_timer;
|
||||
static void trackerWakeCallback(TimerHandle_t){}
|
||||
@ARM@
|
||||
struct Mesh {bool tracker=true;uint32_t sleep_secs=30;
|
||||
struct Mesh {bool tracker=true,dual=false,profile_ready=false;uint32_t sleep_secs=30;
|
||||
uint32_t profile_ms=3;unsigned profile_queries=0;
|
||||
bool isTrackerModeEnabled()const{return tracker;}
|
||||
bool isDualRadioActive()const{return dual;}
|
||||
bool getDualRadioIdleDelay(uint32_t& out){++profile_queries;out=profile_ms;return profile_ready;}
|
||||
bool canIdleDualRadio()const{return true;}
|
||||
uint32_t getPowerSaveSleepMilliseconds(uint32_t max)const{assert(max==30000);return sleep_secs*1000;}
|
||||
bool getNextNoiseFloorWakeDelay(uint32_t&)const{return false;}
|
||||
bool millisHasNowPassed(uint32_t deadline)const{return (int32_t)(millis()-deadline)>=0;}
|
||||
} the_mesh;
|
||||
struct Board {unsigned sleeps=0,last=0;
|
||||
unsigned absolute_calls=0;uint32_t deadline=0;
|
||||
void sleep(unsigned value){++sleeps;last=value;}
|
||||
void sleepMilliseconds(unsigned value){++sleeps;last=value;}
|
||||
void sleepUntilMicroseconds(uint32_t value){++sleeps;++absolute_calls;deadline=value;}
|
||||
} board;
|
||||
static const unsigned long POWERSAVING_FIRST_SLEEP_SECS=120;
|
||||
void sleepGate(bool can_power_save){
|
||||
@@ -192,6 +201,21 @@ int main(){
|
||||
now_ms=119999;sleepGate(true);assert(board.sleeps==0);
|
||||
now_ms=120000;sleepGate(true);assert(board.sleeps==1&&board.last==30000);
|
||||
the_mesh.sleep_secs=0;sleepGate(true);assert(board.sleeps==1);
|
||||
#if defined(ESP32_PLATFORM)
|
||||
// Execute the real ESP32 profile helper through the extracted Sensor gate.
|
||||
// A refused scanner slice must retain polling, without a legacy long sleep.
|
||||
the_mesh.sleep_secs=30;the_mesh.dual=true;sleepGate(true);
|
||||
assert(the_mesh.profile_queries==1&&board.sleeps==1&&board.absolute_calls==0);
|
||||
the_mesh.profile_ready=true;the_mesh.profile_ms=1;sleepGate(true);
|
||||
assert(board.sleeps==1&&board.absolute_calls==0);
|
||||
the_mesh.profile_ms=3;sleepGate(true);
|
||||
assert(board.sleeps==2&&board.absolute_calls==1);
|
||||
assert(board.deadline==120003000);
|
||||
the_mesh.profile_ms=11;sleepGate(true);
|
||||
assert(board.sleeps==2&&board.absolute_calls==1);
|
||||
unsigned queries=the_mesh.profile_queries;sleepGate(false);
|
||||
assert(the_mesh.profile_queries==queries&&board.sleeps==2);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
'''
|
||||
@@ -269,6 +293,9 @@ class SensorTrackerSleepTest(unittest.TestCase):
|
||||
with tempfile.TemporaryDirectory(prefix="meshcore-tracker-sleep-") as directory:
|
||||
path = Path(directory)
|
||||
(path / "Arduino.h").write_text(ARDUINO, encoding="ascii")
|
||||
(path / "esp_timer.h").write_text(
|
||||
"#pragma once\n#include <stdint.h>\nint64_t esp_timer_get_time();\n",
|
||||
encoding="ascii")
|
||||
cpp, binary = path / "test.cpp", path / "test"
|
||||
cpp.write_text(source, encoding="ascii")
|
||||
result = subprocess.run([
|
||||
|
||||
Reference in new issue
Block a user