Enable reversible ESP32 BLE shutdown and bounded radio2 CPU sleep
This commit is contained in:
1 parent
a3d77a9a76
commit
61eecbd971
38 files changed
+1829
-117
No files matched your search
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "BLEDevice.h"
|
||||
+187
@@ -0,0 +1,187 @@
|
||||
#pragma once
|
||||
#include <Arduino.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#include <cassert>
|
||||
using esp_err_t = int;
|
||||
constexpr esp_err_t ESP_OK = 0;
|
||||
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];
|
||||
using esp_ble_wl_addr_type_t = int;
|
||||
using esp_gatt_perm_t = int;
|
||||
constexpr int BLE_ADDR_TYPE_PUBLIC=0, BLE_ADDR_TYPE_RANDOM=1, BLE_WL_ADDR_TYPE_PUBLIC=0, BLE_WL_ADDR_TYPE_RANDOM=1;
|
||||
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_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;
|
||||
};
|
||||
class BLEServer;
|
||||
class BLECharacteristic;
|
||||
namespace fake_bt {
|
||||
inline esp_bt_controller_status_t controller=ESP_BT_CONTROLLER_STATUS_IDLE;
|
||||
inline esp_bluedroid_status_t host=ESP_BLUEDROID_STATUS_UNINITIALIZED;
|
||||
inline std::string failure, name;
|
||||
inline bool initialized=false, persisted=true;
|
||||
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 BLEServer* server=nullptr;
|
||||
}
|
||||
inline esp_bt_controller_status_t esp_bt_controller_get_status() { return fake_bt::controller; }
|
||||
inline esp_bluedroid_status_t esp_bluedroid_get_status() { return fake_bt::host; }
|
||||
inline int esp_bluedroid_disable() {
|
||||
++fake_bt::stops;
|
||||
if(fake_bt::failure=="stop-host") return 1;
|
||||
fake_bt::host=ESP_BLUEDROID_STATUS_INITIALIZED; return ESP_OK;
|
||||
}
|
||||
inline int esp_bluedroid_deinit() {
|
||||
if(fake_bt::failure=="deinit-host") return 1;
|
||||
fake_bt::host=ESP_BLUEDROID_STATUS_UNINITIALIZED; return ESP_OK;
|
||||
}
|
||||
inline int esp_bt_controller_disable() {
|
||||
if(fake_bt::failure=="stop-controller") return 1;
|
||||
fake_bt::controller=ESP_BT_CONTROLLER_STATUS_INITED; return ESP_OK;
|
||||
}
|
||||
inline int esp_bt_controller_deinit() {
|
||||
if(fake_bt::failure=="deinit-controller") return 1;
|
||||
fake_bt::controller=ESP_BT_CONTROLLER_STATUS_IDLE; return ESP_OK;
|
||||
}
|
||||
inline int esp_ble_get_bond_device_num() { return fake_bt::persisted ? 1 : 0; }
|
||||
inline int esp_ble_get_bond_device_list(int*, esp_ble_bond_dev_t* dest) { *dest=esp_ble_bond_dev_t{}; return ESP_OK; }
|
||||
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; }
|
||||
class BLESecurityCallbacks {
|
||||
public:
|
||||
virtual ~BLESecurityCallbacks()=default;
|
||||
virtual uint32_t onPassKeyRequest() { return 0; }
|
||||
virtual void onPassKeyNotify(uint32_t) {}
|
||||
virtual bool onConfirmPIN(uint32_t) { return false; }
|
||||
virtual bool onSecurityRequest() { return false; }
|
||||
virtual void onAuthenticationComplete(esp_ble_auth_cmpl_t) {}
|
||||
};
|
||||
class BLEServerCallbacks {
|
||||
public:
|
||||
virtual ~BLEServerCallbacks()=default;
|
||||
virtual void onConnect(BLEServer*) {}
|
||||
virtual void onConnect(BLEServer*, esp_ble_gatts_cb_param_t*) {}
|
||||
virtual void onDisconnect(BLEServer*) {}
|
||||
virtual void onMtuChanged(BLEServer*, esp_ble_gatts_cb_param_t*) {}
|
||||
};
|
||||
class BLECharacteristicCallbacks {
|
||||
public:
|
||||
enum Status { SUCCESS_NOTIFY, ERROR_GATT };
|
||||
virtual ~BLECharacteristicCallbacks()=default;
|
||||
virtual void onWrite(BLECharacteristic*, esp_ble_gatts_cb_param_t*) {}
|
||||
virtual void onStatus(BLECharacteristic*, Status, uint32_t) {}
|
||||
};
|
||||
class BLESecurity {
|
||||
public:
|
||||
void setStaticPIN(uint32_t pin) { fake_bt::pin=pin; }
|
||||
void setCapability(int) {}
|
||||
void setAuthenticationMode(int) {}
|
||||
};
|
||||
class BLE2902 {
|
||||
bool subscribed=false;
|
||||
public:
|
||||
BLE2902() { ++fake_bt::descriptors; }
|
||||
~BLE2902() { --fake_bt::descriptors; }
|
||||
void setNotifications(bool v) { subscribed=v; }
|
||||
bool getNotifications() const { return subscribed; }
|
||||
void setAccessPermissions(int) {}
|
||||
};
|
||||
class BLEAdvertisementData { public: void setFlags(int) {} };
|
||||
class BLEAdvertising {
|
||||
public:
|
||||
bool active=false, filtered=false;
|
||||
void start() { active=true; }
|
||||
void stop() { active=false; }
|
||||
void addServiceUUID(const char*) { ++fake_bt::uuid_adds; }
|
||||
void setDeviceAddress(esp_bd_addr_t addr, int) { memcpy(fake_bt::address,addr,6); }
|
||||
void setScanResponse(bool) {}
|
||||
void setScanFilter(bool a,bool b) { filtered=a&&b; }
|
||||
void setAdvertisementData(BLEAdvertisementData&) {}
|
||||
};
|
||||
class BLECharacteristic {
|
||||
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; }
|
||||
~BLECharacteristic() { --fake_bt::characteristics; }
|
||||
void setCallbacks(BLECharacteristicCallbacks* cb) { callbacks=cb; }
|
||||
void setAccessPermissions(int) {}
|
||||
void addDescriptor(BLE2902*) {}
|
||||
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); }
|
||||
};
|
||||
class BLEService {
|
||||
public:
|
||||
BLEService() { ++fake_bt::services; }
|
||||
~BLEService() { --fake_bt::services; }
|
||||
unsigned made=0;
|
||||
BLECharacteristic* createCharacteristic(const char*,uint32_t) {
|
||||
const char* phase = made++ ? "rx" : "tx";
|
||||
return fake_bt::failure==phase ? nullptr : new BLECharacteristic;
|
||||
}
|
||||
void start() {}
|
||||
};
|
||||
inline BLEAdvertising* fake_advertising=nullptr;
|
||||
class BLEServer {
|
||||
public:
|
||||
bool connected=false;
|
||||
BLEServerCallbacks* callbacks=nullptr;
|
||||
BLEServer() { ++fake_bt::servers; }
|
||||
~BLEServer() {
|
||||
assert(fake_bt::host!=ESP_BLUEDROID_STATUS_ENABLED || fake_bt::server!=this);
|
||||
--fake_bt::servers;
|
||||
}
|
||||
BLEService* createService(const char*) { return fake_bt::failure=="service" ? nullptr : new BLEService; }
|
||||
BLEAdvertising* getAdvertising() { if(!fake_advertising) fake_advertising=new BLEAdvertising; return fake_advertising; }
|
||||
void setCallbacks(BLEServerCallbacks* cb) { callbacks=cb; }
|
||||
unsigned getConnectedCount() const { return connected; }
|
||||
void disconnect(uint16_t) { ++fake_bt::disconnects; }
|
||||
uint16_t getConnId() { return 7; }
|
||||
uint16_t getPeerMTU(uint16_t) { return 179; }
|
||||
};
|
||||
class BLEDevice {
|
||||
public:
|
||||
inline static BLESecurityCallbacks* security=nullptr;
|
||||
static void init(const std::string& name) {
|
||||
fake_bt::name=name;
|
||||
if(fake_bt::failure=="init") return;
|
||||
fake_bt::initialized=true;
|
||||
fake_bt::host=ESP_BLUEDROID_STATUS_ENABLED;
|
||||
fake_bt::controller=ESP_BT_CONTROLLER_STATUS_ENABLED;
|
||||
}
|
||||
static BLEServer* createServer() {
|
||||
fake_bt::server=fake_bt::failure=="server" ? nullptr : new BLEServer;
|
||||
return fake_bt::server;
|
||||
}
|
||||
#ifdef TEST_BLE_OWNED_SERVER
|
||||
static BLEServer* getServer() { return fake_bt::server; }
|
||||
#endif
|
||||
static void deinit(bool release) {
|
||||
assert(!release); // BLE memory release is irreversible until reboot.
|
||||
if(!fake_bt::initialized) return;
|
||||
#ifdef TEST_BLE_OWNED_SERVER
|
||||
assert(fake_bt::host!=ESP_BLUEDROID_STATUS_ENABLED);
|
||||
delete fake_bt::server; fake_bt::server=nullptr;
|
||||
delete fake_advertising; fake_advertising=nullptr;
|
||||
#endif
|
||||
fake_bt::host=ESP_BLUEDROID_STATUS_UNINITIALIZED;
|
||||
fake_bt::controller=ESP_BT_CONTROLLER_STATUS_IDLE;
|
||||
fake_bt::initialized=false;
|
||||
}
|
||||
static void setSecurityCallbacks(BLESecurityCallbacks* cb) { security=cb; }
|
||||
static void setMTU(uint16_t) {}
|
||||
};
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "BLEDevice.h"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "BLEDevice.h"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "BLEDevice.h"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "BLEDevice.h"
|
||||
@@ -0,0 +1,2 @@
|
||||
#pragma once
|
||||
#include "BLEDevice.h"
|
||||
@@ -0,0 +1,81 @@
|
||||
#include <helpers/esp32/SerialBLEInterface.h>
|
||||
#include <cassert>
|
||||
using namespace fake_bt;
|
||||
static const uint8_t custom[]={0xc2,1,2,3,4,5};
|
||||
static unsigned suites;
|
||||
static void retire_global() {
|
||||
delete server; server=nullptr;
|
||||
delete fake_advertising; fake_advertising=nullptr;
|
||||
uuid_adds=0;
|
||||
}
|
||||
static void stopped(SerialBLEInterface& a) {
|
||||
a.disable(); test_millis+=1000; a.servicePowerState();
|
||||
assert(!a.isEnabled() && !a.isPowerTransitionPending());
|
||||
assert(controller==ESP_BT_CONTROLLER_STATUS_IDLE);
|
||||
assert(host==ESP_BLUEDROID_STATUS_UNINITIALIZED);
|
||||
assert(services==0 && characteristics==0 && descriptors==0);
|
||||
#ifdef TEST_BLE_OWNED_SERVER
|
||||
assert(servers==0);
|
||||
#else
|
||||
assert(servers==1); // Valid static library callback target, retired on next init.
|
||||
#endif
|
||||
}
|
||||
static void good(SerialBLEInterface& a) {
|
||||
assert(a.isEnabled() && !a.isPowerTransitionPending());
|
||||
assert(servers==1 && services==1 && characteristics==2 && descriptors==1);
|
||||
assert(name=="MeshCore-legacy" && pin==246810);
|
||||
assert(memcmp(address,custom,6)==0 && fake_advertising->active);
|
||||
}
|
||||
static void repeated_cycles() {
|
||||
SerialBLEInterface a; assert(a.begin("MeshCore-","legacy",246810,custom));
|
||||
// Test initial saved-off as well as all subsequent live toggles.
|
||||
stopped(a);
|
||||
for(unsigned i=0;i<100;++i) {
|
||||
a.enable(); good(a); assert(bond_deletions==0); stopped(a);
|
||||
#ifndef TEST_BLE_OWNED_SERVER
|
||||
assert(uuid_adds==1); // Reused static advertiser must not grow forever.
|
||||
#endif
|
||||
}
|
||||
retire_global(); ++suites;
|
||||
}
|
||||
static void partial_stop_failures_and_late_callbacks() {
|
||||
for(const char* fault : {"stop-host","deinit-host","stop-controller","deinit-controller"}) {
|
||||
SerialBLEInterface a; assert(a.begin("MeshCore-","legacy",246810,custom)); a.enable(); good(a);
|
||||
auto* original=server; original->connected=true;
|
||||
esp_ble_gatts_cb_param_t connection;
|
||||
original->callbacks->onConnect(original,&connection);
|
||||
BLEDevice::security->onAuthenticationComplete(esp_ble_auth_cmpl_t{});
|
||||
assert(a.isConnected());
|
||||
a.disable(); assert(!a.isEnabled() && a.isPowerTransitionPending());
|
||||
assert(server==original && services==1 && characteristics==2);
|
||||
original->connected=false;
|
||||
failure=fault; test_millis+=1000; a.servicePowerState();
|
||||
assert(a.isPowerTransitionPending() && server==original && services==1);
|
||||
esp_ble_auth_cmpl_t failed; failed.success=false;
|
||||
BLEDevice::security->onAuthenticationComplete(failed);
|
||||
assert(bond_deletions==0);
|
||||
a.enable(); assert(!a.isEnabled() && a.isPowerTransitionPending());
|
||||
failure.clear(); test_millis+=1000; a.servicePowerState(); good(a); stopped(a); retire_global();
|
||||
}
|
||||
++suites;
|
||||
}
|
||||
static void startup_failures() {
|
||||
for(const char* fault : {"init","server","service","tx","rx"}) {
|
||||
SerialBLEInterface a; assert(a.begin("MeshCore-","legacy",246810,custom)); a.enable(); stopped(a);
|
||||
failure=fault; a.enable();
|
||||
assert(!a.isEnabled() && !a.isConnected()); a.servicePowerState();
|
||||
assert(!a.isPowerTransitionPending());
|
||||
assert(services==0 && characteristics==0 && descriptors==0);
|
||||
failure.clear(); a.enable(); good(a); stopped(a); retire_global();
|
||||
}
|
||||
++suites;
|
||||
}
|
||||
int main() {
|
||||
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.
|
||||
delete server; server=nullptr;
|
||||
delete fake_advertising; fake_advertising=nullptr;
|
||||
assert(servers==0 && services==0 && characteristics==0 && descriptors==0);
|
||||
printf("%u lifecycle suites passed\n",suites);
|
||||
}
|
||||
+176
@@ -0,0 +1,176 @@
|
||||
#include <helpers/esp32/SerialBLEInterface.h>
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
using namespace fake_nimble;
|
||||
using mesh::companion::BluetoothPeerIdentity;
|
||||
static const uint8_t custom[] = {0xc2, 1, 2, 3, 4, 5};
|
||||
static unsigned suites;
|
||||
|
||||
static void reset() {
|
||||
failure.clear();
|
||||
NimBLEDevice::deinit(true);
|
||||
calls.clear(); transmitted.clear(); connection_updates.clear();
|
||||
stops = deleted_servers = bond_deletions = disconnects = 0;
|
||||
persisted = accept_notification = true;
|
||||
test_millis = 100;
|
||||
}
|
||||
static void stop(SerialBLEInterface& adapter) {
|
||||
adapter.disable();
|
||||
test_millis += 1000;
|
||||
adapter.servicePowerState();
|
||||
assert(!adapter.isEnabled() && !adapter.isPowerTransitionPending());
|
||||
assert(!initialized && !NimBLEDevice::server);
|
||||
}
|
||||
static void connect(SerialBLEInterface& adapter, NimBLEConnInfo& info) {
|
||||
auto* server = NimBLEDevice::server.get();
|
||||
server->connected = true;
|
||||
server->callbacks->onConnect(server, info);
|
||||
server->callbacks->onAuthenticationComplete(info);
|
||||
assert(adapter.isConnected());
|
||||
uint8_t rx[MAX_FRAME_SIZE]; adapter.checkRecvFrame(rx);
|
||||
}
|
||||
static void verify(SerialBLEInterface& adapter) {
|
||||
auto* server = NimBLEDevice::server.get();
|
||||
assert(adapter.isEnabled() && !adapter.isPowerTransitionPending());
|
||||
assert(initialized_name == "MeshCore-fixed");
|
||||
assert(server && server->advertising.active);
|
||||
assert(server->advertising.name == "MeshCore-fixed");
|
||||
assert(server->advertising.service == "6E400001-B5A3-F393-E0A9-E50E24DCCA9E");
|
||||
assert(server->displayPairingPasskey() == 246810);
|
||||
assert(bonding && mitm && sc && mtu == MAX_FRAME_SIZE + 3);
|
||||
assert(own_type == BLE_OWN_ADDR_RANDOM);
|
||||
for (unsigned i=0; i<6; ++i) assert(address[i] == custom[5-i]);
|
||||
assert(server->service->characteristics.size() == 2);
|
||||
assert(server->service->characteristics[0]->properties & NIMBLE_PROPERTY::READ_AUTHEN);
|
||||
assert(server->service->characteristics[1]->properties & NIMBLE_PROPERTY::WRITE_AUTHEN);
|
||||
}
|
||||
static void initial_saved_off() {
|
||||
reset(); SerialBLEInterface a;
|
||||
char prefix[] = "MeshCore-", name[] = "fixed";
|
||||
uint8_t address_copy[6]; memcpy(address_copy, custom, 6);
|
||||
assert(a.begin(prefix, name, 246810, address_copy));
|
||||
assert(!a.isEnabled() && a.isPowerTransitionPending());
|
||||
a.servicePowerState();
|
||||
assert(!initialized && !a.isPowerTransitionPending());
|
||||
memset(prefix, 'x', sizeof(prefix)-1); memset(name, 'z', sizeof(name)-1);
|
||||
memset(address_copy, 0, sizeof(address_copy));
|
||||
a.enable(); verify(a); stop(a); ++suites;
|
||||
}
|
||||
static void repeated_cycles_preserve_identity_bonds_and_gatt() {
|
||||
reset(); SerialBLEInterface a;
|
||||
assert(a.begin("MeshCore-", "fixed", 246810, custom, true));
|
||||
for (unsigned i=0; i<100; ++i) {
|
||||
a.enable(); verify(a); stop(a);
|
||||
assert(bond_deletions == 1 && persisted);
|
||||
assert(deleted_servers == i+1);
|
||||
}
|
||||
++suites;
|
||||
}
|
||||
static void deferred_disconnect_and_quick_on() {
|
||||
reset(); SerialBLEInterface a;
|
||||
assert(a.begin("MeshCore-", "fixed", 246810, custom)); a.enable();
|
||||
NimBLEConnInfo info; connect(a, info);
|
||||
auto* original = NimBLEDevice::server.get();
|
||||
auto* tx = original->service->characteristics[0].get();
|
||||
auto* rx = original->service->characteristics[1].get();
|
||||
tx->callbacks->onSubscribe(tx, info, 1);
|
||||
const uint8_t reply[] = {29, 'O', 'K'};
|
||||
assert(a.writeFrame(reply, sizeof(reply)) == sizeof(reply));
|
||||
test_millis += 61; uint8_t frame[MAX_FRAME_SIZE];
|
||||
a.checkRecvFrame(frame); // Existing main drains its off-command reply first.
|
||||
assert(transmitted.size() == 1 && !a.hasPendingIO());
|
||||
a.disable();
|
||||
assert(!a.isEnabled() && a.isPowerTransitionPending());
|
||||
assert(initialized && NimBLEDevice::server.get() == original && stops == 0);
|
||||
assert(disconnects == 1 && !original->advertising.active);
|
||||
// Late authentication failure during intentional disconnect must not erase
|
||||
// a good saved bond. Late writes/subscriptions cannot repopulate the queue.
|
||||
info.authenticated = false;
|
||||
original->callbacks->onAuthenticationComplete(info);
|
||||
rx->setValue(reply, sizeof(reply)); rx->callbacks->onWrite(rx, info);
|
||||
tx->callbacks->onSubscribe(tx, info, 1);
|
||||
original->callbacks->onDisconnect(original, info, 0);
|
||||
assert(bond_deletions == 0 && !a.hasPendingIO());
|
||||
a.enable();
|
||||
assert(!a.isEnabled() && a.isPowerTransitionPending());
|
||||
original->connected = false;
|
||||
test_millis += 999; a.servicePowerState();
|
||||
assert(NimBLEDevice::server.get() == original && stops == 0);
|
||||
++test_millis; a.servicePowerState(); verify(a);
|
||||
assert(deleted_servers == 1 && bond_deletions == 0);
|
||||
stop(a); ++suites;
|
||||
}
|
||||
static void failed_stop_keeps_callback_objects_alive() {
|
||||
reset(); SerialBLEInterface a;
|
||||
assert(a.begin("MeshCore-", "fixed", 246810, custom)); a.enable();
|
||||
auto* original = NimBLEDevice::server.get();
|
||||
failure = "stop-host"; a.disable();
|
||||
assert(!a.isEnabled() && a.isPowerTransitionPending());
|
||||
assert(initialized && NimBLEDevice::server.get() == original && deleted_servers == 0);
|
||||
unsigned attempts = stops;
|
||||
for (unsigned i=0; i<100; ++i) a.servicePowerState();
|
||||
assert(stops == attempts); // No tight retry loop or premature deletion.
|
||||
a.enable(); assert(!a.isEnabled() && a.isPowerTransitionPending());
|
||||
failure.clear(); test_millis += 1000; a.servicePowerState(); verify(a);
|
||||
assert(bond_deletions == 0); stop(a); ++suites;
|
||||
}
|
||||
static void ignored_controller_teardown_failure_is_retried() {
|
||||
for (const char* fault : {"stop-controller", "deinit-controller"}) {
|
||||
reset(); SerialBLEInterface a;
|
||||
assert(a.begin("MeshCore-", "fixed", 246810, custom)); a.enable();
|
||||
auto* original = NimBLEDevice::server.get();
|
||||
failure = fault; a.disable();
|
||||
// Host says it stopped, but its wrapper silently ignored a live
|
||||
// controller. Objects and the sleep gate must remain intact.
|
||||
assert(!initialized && controller_state != 0);
|
||||
assert(!a.isEnabled() && a.isPowerTransitionPending());
|
||||
assert(NimBLEDevice::server.get() == original && deleted_servers == 0);
|
||||
a.enable(); assert(!a.isEnabled() && a.isPowerTransitionPending());
|
||||
failure.clear(); test_millis += 1000; a.servicePowerState(); verify(a);
|
||||
assert(deleted_servers == 1 && bond_deletions == 0);
|
||||
stop(a);
|
||||
}
|
||||
++suites;
|
||||
}
|
||||
static void startup_failures_remain_off_and_recover() {
|
||||
for (const char* fault : {"init", "address", "address-type", "server", "service", "tx", "rx", "start-service", "advertise"}) {
|
||||
reset(); SerialBLEInterface a;
|
||||
assert(a.begin("MeshCore-", "fixed", 246810, custom)); a.enable(); stop(a);
|
||||
failure = fault; a.enable();
|
||||
assert(!a.isEnabled() && !a.isConnected());
|
||||
a.servicePowerState();
|
||||
assert(!initialized && !a.isPowerTransitionPending());
|
||||
assert(bond_deletions == 0);
|
||||
failure.clear(); a.enable(); verify(a); stop(a);
|
||||
}
|
||||
++suites;
|
||||
}
|
||||
static void stealth_and_recovery_never_reopen_general_advertising() {
|
||||
reset(); SerialBLEInterface a;
|
||||
BluetoothPeerIdentity peer; peer.type = mesh::companion::BLUETOOTH_PEER_ADDRESS_PUBLIC;
|
||||
const uint8_t known[] = {1, 2, 3, 4, 5, 6}; memcpy(peer.address, known, 6);
|
||||
assert(a.begin("MeshCore-", "fixed", 246810, custom, false, false, &peer));
|
||||
for (unsigned i=0; i<3; ++i) {
|
||||
a.enable(); auto& adv = NimBLEDevice::server->advertising;
|
||||
assert(adv.active && adv.name.empty() && adv.service.empty());
|
||||
assert(adv.scan_filter && adv.connect_filter && !adv.scan_response);
|
||||
for (unsigned j=0; j<6; ++j) assert(allowlisted.val[j] == known[5-j]);
|
||||
stop(a);
|
||||
}
|
||||
// A missing stored bond must remain suppressed, including after the
|
||||
// application consumes the recovery event and toggles Bluetooth again.
|
||||
persisted = false; a.enable();
|
||||
assert(a.takeBondedOnlyRecovery());
|
||||
assert(!NimBLEDevice::server->advertising.active); stop(a);
|
||||
persisted = true; a.enable();
|
||||
assert(!NimBLEDevice::server->advertising.active); stop(a); ++suites;
|
||||
}
|
||||
int main() {
|
||||
initial_saved_off(); repeated_cycles_preserve_identity_bonds_and_gatt();
|
||||
deferred_disconnect_and_quick_on(); failed_stop_keeps_callback_objects_alive();
|
||||
ignored_controller_teardown_failure_is_retried();
|
||||
startup_failures_remain_off_and_recover();
|
||||
stealth_and_recovery_never_reopen_general_advertising();
|
||||
printf("%u lifecycle suites passed\n", suites);
|
||||
}
|
||||
+26
-8
@@ -51,8 +51,10 @@ struct NimBLEAdvertisementData {
|
||||
bool setFlags(uint8_t value) { flags = value; return true; }
|
||||
};
|
||||
namespace fake_nimble {
|
||||
inline int controller_state = 0;
|
||||
inline bool initialized = false, persisted = true, accept_notification = true;
|
||||
inline std::string failure;
|
||||
inline std::string failure, initialized_name;
|
||||
inline unsigned stops = 0, deleted_servers = 0, bond_deletions = 0;
|
||||
inline std::vector<std::string> calls;
|
||||
inline uint8_t address[6] = {}, own_type = BLE_OWN_ADDR_PUBLIC, io_cap = 255;
|
||||
inline ble_addr_t allowlisted;
|
||||
@@ -76,7 +78,8 @@ struct NimBLEAdvertising {
|
||||
bool active = false, scan_response = false, scan_filter = false, connect_filter = false;
|
||||
std::string name, service;
|
||||
bool start(uint32_t = 0, const NimBLEAddress* = nullptr) {
|
||||
fake_nimble::calls.push_back("advertise"); active = true; return true;
|
||||
fake_nimble::calls.push_back("advertise");
|
||||
active = fake_nimble::failure != "advertise"; return active;
|
||||
}
|
||||
bool stop() { active = false; return true; }
|
||||
void enableScanResponse(bool value) { scan_response = value; }
|
||||
@@ -115,7 +118,11 @@ struct NimBLEServer {
|
||||
std::unique_ptr<NimBLEService> service;
|
||||
NimBLEServerCallbacks* callbacks = nullptr;
|
||||
bool owns_callbacks = true, auto_advertise = true, connected = false;
|
||||
~NimBLEServer() { if (owns_callbacks) delete callbacks; }
|
||||
~NimBLEServer() {
|
||||
++fake_nimble::deleted_servers;
|
||||
assert(!fake_nimble::initialized); // Stop must precede destruction.
|
||||
if (owns_callbacks) delete callbacks;
|
||||
}
|
||||
void setCallbacks(NimBLEServerCallbacks* cb, bool take_ownership = true) {
|
||||
callbacks = cb; owns_callbacks = take_ownership;
|
||||
}
|
||||
@@ -126,7 +133,7 @@ struct NimBLEServer {
|
||||
return fake_nimble::pin == 123456
|
||||
? callbacks->onPassKeyDisplay() : fake_nimble::pin;
|
||||
}
|
||||
bool start() { return true; }
|
||||
bool start() { return fake_nimble::failure != "start-service"; }
|
||||
uint8_t getConnectedCount() const { return connected; }
|
||||
uint16_t getPeerMTU(uint16_t) const { return fake_nimble::mtu; }
|
||||
void updateConnParams(uint16_t handle, uint16_t minimum, uint16_t maximum,
|
||||
@@ -142,17 +149,28 @@ struct NimBLEServer {
|
||||
};
|
||||
struct NimBLEDevice {
|
||||
inline static std::unique_ptr<NimBLEServer> server;
|
||||
static bool init(const std::string&) {
|
||||
static bool init(const std::string& name) {
|
||||
fake_nimble::initialized_name = name;
|
||||
fake_nimble::calls.push_back("init");
|
||||
return fake_nimble::initialized = fake_nimble::failure != "init";
|
||||
fake_nimble::initialized = fake_nimble::failure != "init";
|
||||
fake_nimble::controller_state = fake_nimble::initialized ? 2 : 0;
|
||||
return fake_nimble::initialized;
|
||||
}
|
||||
static bool deinit(bool clear = false) {
|
||||
++fake_nimble::stops;
|
||||
if (fake_nimble::failure == "stop-host") return false;
|
||||
if (fake_nimble::initialized) {
|
||||
// The real NimBLE wrapper ignores its port's controller teardown error.
|
||||
if (fake_nimble::failure == "stop-controller") fake_nimble::controller_state = 2;
|
||||
else if (fake_nimble::failure == "deinit-controller") fake_nimble::controller_state = 1;
|
||||
else fake_nimble::controller_state = 0;
|
||||
}
|
||||
fake_nimble::initialized = false;
|
||||
if (clear) server.reset();
|
||||
return true;
|
||||
}
|
||||
static bool deleteAllBonds() { fake_nimble::calls.push_back("clear-bonds"); return fake_nimble::failure != "clear-bonds"; }
|
||||
static bool deleteBond(const NimBLEAddress&) { return true; }
|
||||
static bool deleteAllBonds() { ++fake_nimble::bond_deletions; fake_nimble::calls.push_back("clear-bonds"); return fake_nimble::failure != "clear-bonds"; }
|
||||
static bool deleteBond(const NimBLEAddress&) { ++fake_nimble::bond_deletions; return true; }
|
||||
static bool isBonded(const NimBLEAddress&) { return fake_nimble::persisted; }
|
||||
static bool setOwnAddr(const uint8_t* address) {
|
||||
fake_nimble::calls.push_back("address");
|
||||
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include <NimBLEDevice.h>
|
||||
using esp_err_t = int;
|
||||
constexpr esp_err_t ESP_OK = 0;
|
||||
enum esp_bt_controller_status_t {
|
||||
ESP_BT_CONTROLLER_STATUS_IDLE,
|
||||
ESP_BT_CONTROLLER_STATUS_INITED,
|
||||
ESP_BT_CONTROLLER_STATUS_ENABLED
|
||||
};
|
||||
inline esp_bt_controller_status_t esp_bt_controller_get_status() {
|
||||
return static_cast<esp_bt_controller_status_t>(fake_nimble::controller_state);
|
||||
}
|
||||
inline esp_err_t esp_bt_controller_disable() {
|
||||
if (fake_nimble::failure == "stop-controller") return 1;
|
||||
fake_nimble::controller_state = ESP_BT_CONTROLLER_STATUS_INITED;
|
||||
return ESP_OK;
|
||||
}
|
||||
inline esp_err_t esp_bt_controller_deinit() {
|
||||
if (fake_nimble::failure == "deinit-controller") return 1;
|
||||
fake_nimble::controller_state = ESP_BT_CONTROLLER_STATUS_IDLE;
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Run the actual ESP32 BLE adapter through reversible stack lifecycles."""
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
class Esp32BlePowerTests(unittest.TestCase):
|
||||
def test_nimble_reversible_power_faults_identity_bonds_and_callbacks(self):
|
||||
self.run_adapter("test_nimble_power.cpp", ["-DMESH_USE_NIMBLE_ARDUINO=1"],
|
||||
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"],
|
||||
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"],
|
||||
ROOT / "test/fixtures/esp32_ble_power/mocks")
|
||||
|
||||
def run_adapter(self, fixture, flags, mocks):
|
||||
with tempfile.TemporaryDirectory(prefix="meshcore-ble-power-") as temp:
|
||||
binary = Path(temp) / "ble-power"
|
||||
result = subprocess.run([
|
||||
"c++", "-std=c++17", "-g", "-fsanitize=address,undefined", *flags,
|
||||
"-I", str(mocks),
|
||||
"-I", str(ROOT / "test/fixtures/nimble_companion/mocks"),
|
||||
"-I", str(ROOT / "src"),
|
||||
str(ROOT / "src/helpers/esp32/SerialBLEInterface.cpp"),
|
||||
str(ROOT / "test/fixtures/esp32_ble_power" / fixture),
|
||||
"-o", str(binary),
|
||||
], text=True, capture_output=True, timeout=60)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
result = subprocess.run([str(binary)], text=True, capture_output=True, timeout=30)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
self.assertIn("lifecycle suites passed", result.stdout)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -27,6 +27,9 @@ def run_cpp(source, defines=()):
|
||||
cpp = Path(directory) / "test.cpp"
|
||||
exe = Path(directory) / "test"
|
||||
(Path(directory) / "Dispatcher.h").write_text(MORSE_RADIO, encoding="ascii")
|
||||
(Path(directory) / "esp_timer.h").write_text("#pragma once\n#include <stdint.h>\nint64_t esp_timer_get_time();\n")
|
||||
for header in ("esp_wifi.h", "esp_bt.h", "esp_bt_main.h"):
|
||||
(Path(directory) / header).write_text("#pragma once\n")
|
||||
cpp.write_text(source)
|
||||
built = subprocess.run(
|
||||
[os.environ.get("CXX", "c++"), "-std=c++17", "-Wall", "-Wextra",
|
||||
@@ -105,18 +108,25 @@ int main() {
|
||||
static uint32_t now = 1000;
|
||||
uint32_t millis() { return now; }
|
||||
uint32_t micros() { return now * 1000U; }
|
||||
int64_t esp_timer_get_time() { return int64_t(now) * 1000; }
|
||||
using esp_err_t = int;
|
||||
enum wifi_mode_t { WIFI_MODE_NULL, WIFI_MODE_STA, WIFI_MODE_AP, WIFI_MODE_APSTA };
|
||||
constexpr int ESP_OK = 0, ESP_ERR_WIFI_NOT_INIT = 1, ESP_ERR_WIFI_NOT_STARTED = 2;
|
||||
static wifi_mode_t sdk_mode = WIFI_MODE_NULL;
|
||||
static int sdk_result = ESP_OK, sleeps = 0, yields = 0;
|
||||
constexpr int ESP_BT_CONTROLLER_STATUS_ENABLED = 1;
|
||||
using esp_bt_controller_status_t = int;
|
||||
constexpr int ESP_BT_CONTROLLER_STATUS_IDLE = 0, ESP_BT_CONTROLLER_STATUS_INITED = 1,
|
||||
ESP_BT_CONTROLLER_STATUS_ENABLED = 2;
|
||||
static bool bt_controller_active = false;
|
||||
int esp_bt_controller_get_status() { return bt_controller_active ? 1 : 0; }
|
||||
constexpr int ESP_BLUEDROID_STATUS_ENABLED = 1;
|
||||
int esp_bt_controller_get_status() { return bt_controller_active ? 2 : 0; }
|
||||
using esp_bluedroid_status_t = int;
|
||||
constexpr int ESP_BLUEDROID_STATUS_UNINITIALIZED = 0, ESP_BLUEDROID_STATUS_INITIALIZED = 1,
|
||||
ESP_BLUEDROID_STATUS_ENABLED = 2;
|
||||
static bool bt_host_active = false;
|
||||
int esp_bluedroid_get_status() { return bt_host_active ? 1 : 0; }
|
||||
int esp_bluedroid_get_status() { return bt_host_active ? 2 : 0; }
|
||||
int esp_wifi_get_mode(wifi_mode_t* mode) { *mode = sdk_mode; return sdk_result; }
|
||||
#include <helpers/esp32/LightSleepPolicy.h>
|
||||
#include <helpers/esp32/ProfileIdleSleep.h>
|
||||
int esp_sleep_enable_timer_wakeup(uint64_t us) { assert(us == 10000); return ESP_OK; }
|
||||
int esp_light_sleep_start() { ++sleeps; return ESP_OK; }
|
||||
int pdMS_TO_TICKS(int ms) { return ms; }
|
||||
@@ -129,6 +139,7 @@ struct Board {
|
||||
@USB_HELD@
|
||||
bool isOTAUpdateRunning() const { return ota; }
|
||||
bool isRadioTestActive() const { return test; }
|
||||
void sleepUntilMicroseconds(uint32_t) { ++sleeps; }
|
||||
} board;
|
||||
struct Prefs { bool powersaving_enabled = true; } prefs;
|
||||
struct TestRadio : mesh::Radio {
|
||||
@@ -146,10 +157,12 @@ struct Mesh {
|
||||
}
|
||||
bool isDualRadioActive() const { return dual; }
|
||||
bool canIdleDualRadio() const { return !dual; }
|
||||
bool getDualRadioIdleDelay(uint32_t& out) const { out = 0; return false; }
|
||||
bool getNextNoiseFloorWakeDelay(uint32_t&) const { return false; }
|
||||
mesh::Radio* getProfileRadio() { return &timing_radio; }
|
||||
} the_mesh;
|
||||
struct Interfaces { bool pending = false; bool hasPendingIO() const { return pending; } } interface_manager;
|
||||
struct Bluetooth { bool pending = false; bool isPowerTransitionPending() const { return pending; } } bluetooth_interface;
|
||||
struct Sensors { bool gps_uart = false; bool gpsUsesSerialUart(uint8_t uart) const { return uart == 1 && gps_uart; } } sensors;
|
||||
struct Button { bool polling = false; bool needsPolling() const { return polling; } } user_btn;
|
||||
struct Wireless { unsigned services = 0; unsigned enabled() const { return services; } } companion_wireless;
|
||||
@@ -202,7 +215,7 @@ int main() {
|
||||
blocks(board.ota); blocks(board.test); blocks(the_mesh.pending);
|
||||
blocks(interface_manager.pending); blocks(user_btn.polling); blocks(mesh::logging);
|
||||
blocks(bt_controller_active);
|
||||
blocks(bt_host_active); blocks(sensors.gps_uart);
|
||||
blocks(bt_host_active); blocks(sensors.gps_uart); blocks(bluetooth_interface.pending);
|
||||
blocks(mesh::watchdog); blocks(mesh::wireless::controller.transition);
|
||||
prefs.powersaving_enabled = false; before = sleeps; idle(); assert(sleeps == before);
|
||||
prefs.powersaving_enabled = true; idle(); assert(sleeps == before + 1);
|
||||
@@ -263,7 +276,7 @@ int main() {
|
||||
source = "#include <initializer_list>\n" + source
|
||||
flags = ("ESP32_PLATFORM", "ESP32", "ENABLE_USB_INTERFACE", "ARDUINO_USB_CDC_ON_BOOT=1",
|
||||
'WIFI_SSID=""', "BLE_PIN_CODE=123456", "MESH_USB_LOGGING_AVAILABLE=1",
|
||||
"CONFIG_BLUEDROID_ENABLED=1",
|
||||
"CONFIG_BLUEDROID_ENABLED=1", "CONFIG_BT_ENABLED=1",
|
||||
"MESH_ESP32_USB_CONSOLE_COOPERATIVE=1", "MESH_ESP32_USB_HOST_LOSS_SLEEP_GRACE_MS=120000UL",
|
||||
"MOMENTARY_BUTTON_WAKE_FROM_SLEEP=1", "PIN_USER_BTN=0", "DISPLAY_CLASS=Mock")
|
||||
for pm in (0, 1):
|
||||
|
||||
@@ -301,7 +301,9 @@ int main(){FEMBoard board; board.enterDeepSleep(5); assert(slept);}
|
||||
light='\n'.join(body(header, signature) for signature in (
|
||||
'void sleep(uint32_t secs) override',
|
||||
'void sleepMilliseconds(uint32_t millisecs) override',
|
||||
'void sleepMicroseconds(uint64_t wake_us, bool require_timer = false)'))
|
||||
'void sleepMicroseconds(uint64_t wake_us, bool require_timer = false)',
|
||||
'void sleepUntilMicroseconds(uint32_t deadline_us)',
|
||||
'void sleepMicrosecondsImpl(uint64_t wake_us, bool require_timer,'))
|
||||
self.assertNotIn('commitTerminalDeepSleepHolds',light)
|
||||
for folder in (ROOT/'src',ROOT/'variants'):
|
||||
for path in folder.rglob('*'):
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Compile the real manual-sleep driver guard for WiFi/BT SDK configurations."""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
WIFI = r'''
|
||||
#pragma once
|
||||
using esp_err_t = int;
|
||||
constexpr int ESP_OK = 0, ESP_ERR_WIFI_NOT_INIT = 1, ESP_ERR_WIFI_NOT_STARTED = 2;
|
||||
enum wifi_mode_t { WIFI_MODE_NULL, WIFI_MODE_STA, WIFI_MODE_AP, WIFI_MODE_APSTA };
|
||||
static wifi_mode_t mode = WIFI_MODE_NULL;
|
||||
static int wifi_error = ESP_OK;
|
||||
inline int esp_wifi_get_mode(wifi_mode_t* out) { *out = mode; return wifi_error; }
|
||||
'''
|
||||
CONTROLLER = r'''
|
||||
#pragma once
|
||||
enum esp_bt_controller_status_t { ESP_BT_CONTROLLER_STATUS_IDLE,
|
||||
ESP_BT_CONTROLLER_STATUS_INITED, ESP_BT_CONTROLLER_STATUS_ENABLED };
|
||||
static auto controller = ESP_BT_CONTROLLER_STATUS_IDLE;
|
||||
inline esp_bt_controller_status_t esp_bt_controller_get_status() { return controller; }
|
||||
'''
|
||||
HOST = r'''
|
||||
#pragma once
|
||||
enum esp_bluedroid_status_t { ESP_BLUEDROID_STATUS_UNINITIALIZED,
|
||||
ESP_BLUEDROID_STATUS_INITIALIZED, ESP_BLUEDROID_STATUS_ENABLED };
|
||||
static auto host = ESP_BLUEDROID_STATUS_UNINITIALIZED;
|
||||
inline esp_bluedroid_status_t esp_bluedroid_get_status() { return host; }
|
||||
'''
|
||||
HARNESS = r'''
|
||||
#include <helpers/esp32/LightSleepPolicy.h>
|
||||
#include <cassert>
|
||||
using mesh::powerdiag::Reason;
|
||||
using mesh::esp32::manualLightSleepBlocker;
|
||||
using mesh::esp32::manualLightSleepReady;
|
||||
int main() {
|
||||
assert(manualLightSleepReady());
|
||||
for (auto value : {WIFI_MODE_STA, WIFI_MODE_AP, WIFI_MODE_APSTA}) {
|
||||
mode = value;
|
||||
assert(manualLightSleepBlocker() == Reason::Network);
|
||||
}
|
||||
mode = WIFI_MODE_NULL;
|
||||
for (int error : {ESP_ERR_WIFI_NOT_INIT, ESP_ERR_WIFI_NOT_STARTED}) {
|
||||
wifi_error = error;
|
||||
assert(manualLightSleepReady());
|
||||
}
|
||||
wifi_error = -1;
|
||||
assert(manualLightSleepBlocker() == Reason::Network);
|
||||
wifi_error = ESP_OK;
|
||||
#if defined(CONFIG_BT_ENABLED) && CONFIG_BT_ENABLED
|
||||
controller = ESP_BT_CONTROLLER_STATUS_INITED;
|
||||
assert(manualLightSleepReady());
|
||||
controller = ESP_BT_CONTROLLER_STATUS_ENABLED;
|
||||
assert(manualLightSleepBlocker() == Reason::Wireless);
|
||||
controller = static_cast<esp_bt_controller_status_t>(99);
|
||||
assert(manualLightSleepBlocker() == Reason::Wireless);
|
||||
controller = ESP_BT_CONTROLLER_STATUS_IDLE;
|
||||
#if defined(CONFIG_BLUEDROID_ENABLED) && CONFIG_BLUEDROID_ENABLED \
|
||||
&& !defined(MESH_USE_NIMBLE_ARDUINO)
|
||||
host = ESP_BLUEDROID_STATUS_INITIALIZED;
|
||||
assert(manualLightSleepReady());
|
||||
host = ESP_BLUEDROID_STATUS_ENABLED;
|
||||
assert(manualLightSleepBlocker() == Reason::Wireless);
|
||||
host = static_cast<esp_bluedroid_status_t>(99);
|
||||
assert(manualLightSleepBlocker() == Reason::Wireless);
|
||||
host = ESP_BLUEDROID_STATUS_UNINITIALIZED;
|
||||
#endif
|
||||
#endif
|
||||
assert(manualLightSleepReady());
|
||||
}
|
||||
'''
|
||||
|
||||
|
||||
class LightSleepPolicyTest(unittest.TestCase):
|
||||
def test_real_guard_across_sdk_and_host_modes(self):
|
||||
for flags in (
|
||||
(), ("CONFIG_BT_ENABLED=0", "CONFIG_IDF_TARGET_ESP32S2=1"),
|
||||
("CONFIG_BT_ENABLED=1", "CONFIG_BLUEDROID_ENABLED=1", "CONFIG_IDF_TARGET_ESP32=1"),
|
||||
("CONFIG_BT_ENABLED=1", "CONFIG_BLUEDROID_ENABLED=1", "CONFIG_IDF_TARGET_ESP32S3=1"),
|
||||
("CONFIG_BT_ENABLED=1", "CONFIG_BLUEDROID_ENABLED=0", "CONFIG_IDF_TARGET_ESP32C3=1"),
|
||||
("CONFIG_BT_ENABLED=1", "CONFIG_BLUEDROID_ENABLED=1", "MESH_USE_NIMBLE_ARDUINO=1", "CONFIG_IDF_TARGET_ESP32C6=1"),
|
||||
):
|
||||
with self.subTest(flags=flags), tempfile.TemporaryDirectory(prefix="mesh-esp32-sleep-driver-") as directory:
|
||||
root = Path(directory)
|
||||
(root / "esp_wifi.h").write_text(WIFI)
|
||||
bt = "CONFIG_BT_ENABLED=1" in flags
|
||||
bluedroid = bt and "CONFIG_BLUEDROID_ENABLED=1" in flags and "MESH_USE_NIMBLE_ARDUINO=1" not in flags
|
||||
(root / "esp_bt.h").write_text(CONTROLLER if bt else '#error "Bluetooth absent on this target"\n')
|
||||
(root / "esp_bt_main.h").write_text(HOST if bluedroid else '#error "Bluedroid absent from this host build"\n')
|
||||
cpp, exe = root / "guard.cpp", root / "guard"
|
||||
cpp.write_text("#include <initializer_list>\n" + HARNESS)
|
||||
result = subprocess.run([
|
||||
os.environ.get("CXX", "c++"), "-std=c++17", "-Wall", "-Wextra", "-Werror",
|
||||
*("-D" + flag for flag in flags), "-I", str(root), "-I", str(ROOT / "src"),
|
||||
str(cpp), "-o", str(exe)], text=True, capture_output=True, timeout=30)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
result = subprocess.run([str(exe)], text=True, capture_output=True, timeout=10)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,97 @@
|
||||
"""Execute the production profile sleep handoff with elapsed and wrapping clocks."""
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
class Esp32ProfileIdleSleepTest(unittest.TestCase):
|
||||
def test_absolute_role_and_radio_deadlines_survive_preemption(self):
|
||||
source = r'''
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <helpers/esp32/ProfileIdleSleep.h>
|
||||
static uint64_t now_us = 0;
|
||||
int64_t esp_timer_get_time() { return static_cast<int64_t>(now_us); }
|
||||
struct Mesh {
|
||||
bool ready = true;
|
||||
uint32_t budget_ms = 10, query_cost_us = 0;
|
||||
unsigned reads = 0;
|
||||
bool getDualRadioIdleDelay(uint32_t& out) {
|
||||
++reads;
|
||||
out = budget_ms;
|
||||
now_us += query_cost_us;
|
||||
return ready;
|
||||
}
|
||||
} mesh_node;
|
||||
struct Board {
|
||||
unsigned calls = 0;
|
||||
uint32_t deadline = 0;
|
||||
void sleepUntilMicroseconds(uint32_t value) { ++calls; deadline = value; }
|
||||
} board;
|
||||
int main() {
|
||||
for (uint64_t start : {100000ULL, 0xfffffc00ULL, 0x1fffffc00ULL}) {
|
||||
now_us = start;
|
||||
mesh_node = {};
|
||||
board = {};
|
||||
assert(mesh::esp32::tryProfileIdleSleep(mesh_node, board, uint32_t(start), 30));
|
||||
assert(board.calls == 1 && board.deadline == uint32_t(start + 10000));
|
||||
|
||||
// A role deadline calculated before other loop work is never restarted.
|
||||
now_us = start + 4000;
|
||||
assert(mesh::esp32::tryProfileIdleSleep(mesh_node, board, uint32_t(start), 7));
|
||||
assert(board.deadline == uint32_t(start + 7000));
|
||||
now_us = start + 5001;
|
||||
const unsigned before = board.calls;
|
||||
assert(!mesh::esp32::tryProfileIdleSleep(mesh_node, board, uint32_t(start), 7));
|
||||
assert(board.calls == before);
|
||||
now_us = start + 8000;
|
||||
assert(!mesh::esp32::tryProfileIdleSleep(mesh_node, board, uint32_t(start), 7));
|
||||
|
||||
// An IRQ/task delay in the scanner query reduces the board's remaining
|
||||
// time instead of moving the absolute deadline forward by that delay.
|
||||
now_us = start;
|
||||
mesh_node.query_cost_us = 6000;
|
||||
assert(mesh::esp32::tryProfileIdleSleep(mesh_node, board, uint32_t(start), 30));
|
||||
assert(now_us == start + 6000 && board.deadline == uint32_t(start + 10000));
|
||||
mesh_node.query_cost_us = 0;
|
||||
mesh_node.budget_ms = 2;
|
||||
now_us = start;
|
||||
assert(mesh::esp32::tryProfileIdleSleep(mesh_node, board, uint32_t(start), 30));
|
||||
assert(board.deadline == uint32_t(start + 2000));
|
||||
}
|
||||
now_us = 100000;
|
||||
for (uint32_t budget : {0U, 1U, 11U, 0xffffffffU}) {
|
||||
mesh_node = {};
|
||||
mesh_node.budget_ms = budget;
|
||||
const unsigned before = board.calls;
|
||||
assert(!mesh::esp32::tryProfileIdleSleep(mesh_node, board, 100000, 30));
|
||||
assert(board.calls == before);
|
||||
}
|
||||
mesh_node = {};
|
||||
mesh_node.ready = false;
|
||||
assert(!mesh::esp32::tryProfileIdleSleep(mesh_node, board, 100000, 30));
|
||||
mesh_node = {};
|
||||
const unsigned reads = mesh_node.reads;
|
||||
assert(!mesh::esp32::tryProfileIdleSleep(mesh_node, board, 100000, 0));
|
||||
assert(!mesh::esp32::tryProfileIdleSleep(mesh_node, board, 100000, 0xffffffffU));
|
||||
assert(mesh_node.reads == reads);
|
||||
}
|
||||
'''
|
||||
with tempfile.TemporaryDirectory(prefix="mesh-esp32-profile-handoff-") as directory:
|
||||
path = Path(directory)
|
||||
(path / "esp_timer.h").write_text("#pragma once\n#include <stdint.h>\nint64_t esp_timer_get_time();\n")
|
||||
(path / "test.cpp").write_text("#include <initializer_list>\n" + source)
|
||||
built = subprocess.run([os.environ.get("CXX", "c++"), "-std=c++17", "-Wall", "-Wextra", "-Werror",
|
||||
"-I", str(path), "-I", str(ROOT / "src"), str(path / "test.cpp"),
|
||||
"-o", str(path / "test")], capture_output=True, text=True)
|
||||
self.assertEqual(built.returncode, 0, built.stdout + built.stderr)
|
||||
ran = subprocess.run([str(path / "test")], capture_output=True, text=True)
|
||||
self.assertEqual(ran.returncode, 0, ran.stdout + ran.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -58,6 +58,10 @@ static void attachHost(bool attached) {
|
||||
namespace mesh {
|
||||
static bool logging_enabled = false;
|
||||
bool isUsbLoggingEnabled() { return logging_enabled; }
|
||||
namespace esp32 {
|
||||
static powerdiag::Reason wireless_blocker = powerdiag::Reason::Count;
|
||||
powerdiag::Reason manualLightSleepBlocker() { return wireless_blocker; }
|
||||
}
|
||||
}
|
||||
|
||||
using gpio_num_t = int;
|
||||
@@ -71,6 +75,9 @@ constexpr int GPIO_INTR_DISABLE = 0;
|
||||
static unsigned sleep_calls = 0;
|
||||
static uint64_t timer_us = 0;
|
||||
static int timer_error = ESP_OK;
|
||||
static int radio_wake_error = ESP_OK, button_wake_error = ESP_OK, gpio_wake_error = ESP_OK;
|
||||
static unsigned gpio_setup_us = 0;
|
||||
static bool irq_during_setup = false;
|
||||
static bool radio_irq_high = false;
|
||||
static int critical_depth = 0;
|
||||
static int wake_pin = -1;
|
||||
@@ -98,11 +105,17 @@ static int esp_light_sleep_start() {
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
static int esp_sleep_enable_gpio_wakeup() { return ESP_OK; }
|
||||
static int esp_sleep_enable_gpio_wakeup() {
|
||||
mock_millis += gpio_setup_us / 1000U;
|
||||
if (irq_during_setup) radio_irq_high = true;
|
||||
return gpio_wake_error;
|
||||
}
|
||||
static int gpio_get_level(gpio_num_t pin) {
|
||||
return pin == 38 ? (button_pressed ? LOW : HIGH) : (radio_irq_high ? HIGH : LOW);
|
||||
}
|
||||
static int gpio_wakeup_enable(gpio_num_t pin, int level) {
|
||||
if (pin == 48 && radio_wake_error != ESP_OK) return radio_wake_error;
|
||||
if (pin == 38 && button_wake_error != ESP_OK) return button_wake_error;
|
||||
wake_pin = pin;
|
||||
wake_levels.at(pin) = level;
|
||||
return ESP_OK;
|
||||
@@ -267,7 +280,9 @@ def board_sleep_methods() -> str:
|
||||
return "\n".join(board_method(signature) for signature in (
|
||||
"void sleep(uint32_t secs) override",
|
||||
"void sleepMilliseconds(uint32_t millisecs) override",
|
||||
"void sleepMicroseconds(uint64_t wake_us, bool require_timer = false)"))
|
||||
"void sleepMicroseconds(uint64_t wake_us, bool require_timer = false)",
|
||||
"void sleepUntilMicroseconds(uint32_t deadline_us)",
|
||||
"void sleepMicrosecondsImpl(uint64_t wake_us, bool require_timer,"))
|
||||
|
||||
|
||||
def v4_profile_config():
|
||||
@@ -345,6 +360,9 @@ struct SerialPort {
|
||||
} Serial;
|
||||
namespace mesh {
|
||||
bool isUsbLoggingEnabled() { return false; }
|
||||
namespace esp32 {
|
||||
powerdiag::Reason manualLightSleepBlocker() { return powerdiag::Reason::Count; }
|
||||
}
|
||||
struct Console { void printf(const char*) {} };
|
||||
static Console console;
|
||||
Console& usbConsolePort() { return console; }
|
||||
@@ -502,6 +520,101 @@ int main() {
|
||||
|
||||
|
||||
class Esp32UsbSleepTest(unittest.TestCase):
|
||||
def test_physical_wireless_vetoes_and_deadline_gpio_failures(self):
|
||||
methods = board_sleep_methods() + "\n" + "\n".join(board_method(signature) for signature in (
|
||||
"bool isUsbDataConnected() override", "bool isUsbHostConnected() override",
|
||||
"bool isUsbSleepHeld()"))
|
||||
harness = HARNESS.split("int main()", 1)[0] + r'''
|
||||
int main() {
|
||||
try {
|
||||
ESP32Board board;
|
||||
attachHost(false);
|
||||
Serial.end();
|
||||
for (auto reason : {mesh::powerdiag::Reason::Network,
|
||||
mesh::powerdiag::Reason::Wireless}) {
|
||||
mesh::esp32::wireless_blocker = reason;
|
||||
mesh::powerdiag::setEnabled(true, millis());
|
||||
board.sleepMilliseconds(10);
|
||||
require(sleep_calls == 0 && timer_us == 0 && critical_depth == 0,
|
||||
"physical wireless veto configured wake or slept");
|
||||
require(mesh::powerdiag::snapshot(millis()).current & mesh::powerdiag::mask(reason),
|
||||
"physical wireless veto lost its actual diagnostic reason");
|
||||
const unsigned before_yield = yield_ms;
|
||||
board.sleepUntilMicroseconds(micros() + 10000U);
|
||||
require(yield_ms == before_yield && sleep_calls == 0,
|
||||
"blocked profile sleep yielded beyond the scanner's safe visit");
|
||||
mesh::powerdiag::setEnabled(false, millis());
|
||||
}
|
||||
mesh::esp32::wireless_blocker = mesh::powerdiag::Reason::Count;
|
||||
for (bool diagnostics : {false, true}) {
|
||||
mesh::powerdiag::setEnabled(diagnostics, millis());
|
||||
for (int source : {0, 1, 2, 3}) {
|
||||
radio_wake_error = source == 0 ? 31 : ESP_OK;
|
||||
button_wake_error = source == 1 ? 32 : ESP_OK;
|
||||
gpio_wake_error = source == 2 ? 33 : ESP_OK;
|
||||
timer_error = source == 3 ? 34 : ESP_OK;
|
||||
const unsigned before = sleep_calls;
|
||||
const uint64_t before_timer = timer_us;
|
||||
board.sleepUntilMicroseconds(micros() + 10000U);
|
||||
require(sleep_calls == before && timer_us == before_timer && critical_depth == 0,
|
||||
"failed timed wake setup slept or reused an old timer");
|
||||
require(wake_levels[48] == 0 && wake_levels[38] == 0
|
||||
&& restored_interrupts[48] == GPIO_INTR_POSEDGE
|
||||
&& restored_interrupts[38] == GPIO_INTR_DISABLE,
|
||||
"failed timed wake setup left level IRQ or wake pin armed");
|
||||
}
|
||||
radio_wake_error = button_wake_error = gpio_wake_error = timer_error = ESP_OK;
|
||||
gpio_setup_us = 3000;
|
||||
const unsigned before = sleep_calls;
|
||||
board.sleepUntilMicroseconds(micros() + 10000U);
|
||||
require(sleep_calls == before + 1 && timer_us == 7000U,
|
||||
"absolute timer restarted the full visit after wake setup work");
|
||||
gpio_setup_us = 0;
|
||||
const uint64_t last_timer = timer_us;
|
||||
for (uint32_t delta : {0U, 1U, 1999U, UINT32_MAX}) {
|
||||
board.sleepUntilMicroseconds(micros() + delta);
|
||||
require(sleep_calls == before + 1 && timer_us == last_timer && critical_depth == 0,
|
||||
"expired or subminimum deadline slept or inherited an old timer");
|
||||
}
|
||||
board.sleepUntilMicroseconds(micros() + 2000U);
|
||||
require(sleep_calls == before + 2 && timer_us == 2000U,
|
||||
"minimum useful absolute deadline did not sleep");
|
||||
mock_millis = 4294967U;
|
||||
board.sleepUntilMicroseconds(micros() + 5000U);
|
||||
require(sleep_calls == before + 3 && timer_us == 5000U,
|
||||
"microsecond clock wrap corrupted a valid absolute deadline");
|
||||
board.irq = UINT32_MAX;
|
||||
board.sleepUntilMicroseconds(micros() + 10000U);
|
||||
require(sleep_calls == before + 3 && timer_us == 5000U,
|
||||
"profile deadline accepted a timer without a radio IRQ wake");
|
||||
board.irq = 48;
|
||||
irq_during_setup = true;
|
||||
board.sleepUntilMicroseconds(micros() + 10000U);
|
||||
require(sleep_calls == before + 3 && timer_us == 5000U && critical_depth == 0,
|
||||
"profile wait slept over an IRQ arriving during wake setup");
|
||||
irq_during_setup = radio_irq_high = false;
|
||||
mesh::powerdiag::setEnabled(false, millis());
|
||||
}
|
||||
} catch (const std::exception& error) {
|
||||
std::cerr << error.what() << '\n'; return 1;
|
||||
}
|
||||
}
|
||||
'''
|
||||
with tempfile.TemporaryDirectory(prefix="meshcore-esp32-profile-deadline-") as directory:
|
||||
cpp = Path(directory) / "sleep.cpp"
|
||||
exe = Path(directory) / "sleep"
|
||||
cpp.write_text(harness.replace("@METHODS@", methods), encoding="ascii")
|
||||
result = subprocess.run([
|
||||
os.environ.get("CXX", "c++"), "-std=c++17", "-Wall", "-Wextra",
|
||||
"-DARDUINO_USB_MODE=1", "-DARDUINO_USB_CDC_ON_BOOT=1",
|
||||
"-DMESH_ESP32_USB_CONSOLE_COOPERATIVE=0", "-DMESH_USB_LOGGING_AVAILABLE=0",
|
||||
"-DMOMENTARY_BUTTON_WAKE_FROM_SLEEP=1", "-DPIN_USER_BTN=38",
|
||||
"-I", str(ROOT / "src"), str(cpp), "-o", str(exe)],
|
||||
capture_output=True, text=True, timeout=60)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
result = subprocess.run([str(exe)], capture_output=True, text=True, timeout=10)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
|
||||
def test_v4_profiles_inherit_button_wake_with_the_actual_gpio_pins(self):
|
||||
config = v4_profile_config()
|
||||
# These standalone partition-migration utilities deliberately bypass
|
||||
|
||||
@@ -101,8 +101,15 @@ static mesh::Radio radio_driver;
|
||||
sleep_getter = " unsigned getPowerSaveSleepMilliseconds(unsigned limit) { return limit; }"
|
||||
assert fixture.count(sleep_getter) == 1, "Missing sleep-deadline peripheral boundary"
|
||||
fixture = fixture.replace(sleep_getter, sleep_getter + "\n" +
|
||||
" bool getNextNoiseFloorWakeDelay(uint32_t&) const { return false; }")
|
||||
" bool getNextNoiseFloorWakeDelay(uint32_t&) const { return false; }\n" +
|
||||
# This RF boundary has no settled scanner proof, so a live radio2
|
||||
# must keep polling. Execute the production deadline helper below.
|
||||
" bool getDualRadioIdleDelay(uint32_t& ms) const { ms = 0; return false; }")
|
||||
source += "\n#include <helpers/PowerManagementUtils.h>\n"
|
||||
profile_helper = body((ROOT / "src/helpers/esp32/ProfileIdleSleep.h").read_text(),
|
||||
"template <typename Mesh, typename Board>")
|
||||
source += ("\nstatic int64_t esp_timer_get_time() { return micros(); }\n"
|
||||
"namespace mesh { namespace esp32 {\n" + profile_helper + "\n} }\n")
|
||||
return source + morse_radio + (fixture.replace("@SETTER@", setter).replace("@GETTER@", getter)
|
||||
.replace("@COMMAND_PUMP@", pump).replace("@POWER_LOOP@", power_loop))
|
||||
|
||||
|
||||
@@ -356,6 +356,8 @@ int main() {
|
||||
"void sleep(uint32_t secs) override",
|
||||
"void sleepMilliseconds(uint32_t millisecs) override",
|
||||
"void sleepMicroseconds(uint64_t wake_us, bool require_timer = false)",
|
||||
"void sleepUntilMicroseconds(uint32_t deadline_us)",
|
||||
"void sleepMicrosecondsImpl(uint64_t wake_us, bool require_timer,",
|
||||
))
|
||||
code = r'''
|
||||
#include <cassert>
|
||||
@@ -388,6 +390,9 @@ static esp_err_t esp_light_sleep_start() { ++sdk_calls; now_us += sleep_duration
|
||||
namespace mesh {
|
||||
static bool logging = false;
|
||||
static bool isUsbLoggingEnabled() { return logging; }
|
||||
namespace esp32 {
|
||||
static powerdiag::Reason manualLightSleepBlocker() { return powerdiag::Reason::Count; }
|
||||
}
|
||||
}
|
||||
struct MainBoard {
|
||||
virtual void sleep(uint32_t) = 0;
|
||||
@@ -412,8 +417,8 @@ int main() {
|
||||
board.sleepMilliseconds(0);
|
||||
assert(sdk_calls == 2 && timer_us == 50000ULL);
|
||||
setEnabled(true, millis()); reset(millis()); decision(0, millis());
|
||||
timer_error = 29; gpio_error = 31; pin_error = 33;
|
||||
board.sleep(30); // Diagnostics may observe failures, never repair/change policy.
|
||||
timer_error = 29;
|
||||
board.sleep(30); // Keep the legacy seconds API's timer-error policy.
|
||||
auto value = snapshot(millis());
|
||||
assert(sdk_calls == 3 && value.calls == 1 && value.returns == 1 && value.errors == 1);
|
||||
assert(value.detail == 29); // First setup failure, without changing later SDK calls.
|
||||
@@ -426,6 +431,18 @@ int main() {
|
||||
value = snapshot(millis());
|
||||
assert(sdk_calls == before_failed_slice && value.calls == 0);
|
||||
assert(value.errors == 1 && value.detail == 29 && critical_depth == 0);
|
||||
const int gpio_errors[] = {31, 33};
|
||||
for (int error : gpio_errors) {
|
||||
reset(millis()); decision(0, millis());
|
||||
timer_error = 0;
|
||||
gpio_error = error == 31 ? error : 0;
|
||||
pin_error = error == 33 ? error : 0;
|
||||
board.sleep(30);
|
||||
value = snapshot(millis());
|
||||
assert(sdk_calls == before_failed_slice && value.calls == 0 && value.returns == 0);
|
||||
assert(value.errors == 1 && value.detail == error && critical_depth == 0);
|
||||
assert(value.eligible_ms == 0 && value.call_us == 0);
|
||||
}
|
||||
reset(millis()); decision(0, millis());
|
||||
timer_error = gpio_error = pin_error = 0; sleep_error = 41;
|
||||
board.sleep(30);
|
||||
|
||||
@@ -0,0 +1,249 @@
|
||||
"""Execute ESP32 scanner publication and its late, integer-only idle query."""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from cpp_source import body
|
||||
from test_radio_profile_scan import HARNESS as SCAN_HARNESS
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
ESP_MEMBERS = r'''
|
||||
uint64_t _esp32_profile_visit_started_us=0, _esp32_profile_idle_stamp_us=0;
|
||||
uint32_t _esp32_profile_idle_visit_us=0, _esp32_profile_idle_switch_us=0;
|
||||
uint32_t _esp32_profile_idle_generation[2]={};
|
||||
uint16_t _esp32_profile_idle_preamble=0;
|
||||
uint8_t _esp32_profile_idle_profile=0;
|
||||
uint32_t _wd_observe_until=0;
|
||||
bool _rx_retry_pending=false, _morse_owned=false;
|
||||
bool _beacon_owned=false, _beacon_restoring=false, _morse_rx_owned=false;
|
||||
bool getProfileIdleDelay(uint32_t&) const;
|
||||
'''
|
||||
|
||||
COMMON = r'''
|
||||
// Complete both independent baselines after startup without faking the idle
|
||||
// token: its RX stamp, generation, preamble and visit come from the scanner.
|
||||
static void ready(RadioLibWrapper& w, uint8_t sf=11, float bw=125) {
|
||||
w._profiles.primary.sf=w._cur_sf=sf;
|
||||
w._profiles.primary.bw=w._cur_bw=bw;
|
||||
w.enable();
|
||||
assert(w._profiles.switchTestReady());
|
||||
w._nf_refresh_requested=w._nf_calib_active=false;
|
||||
w._noise_floor_valid=w._noise_floor_secondary_valid=true;
|
||||
w.serviceProfileScan();
|
||||
assert(w._esp32_profile_idle_visit_us>0);
|
||||
}
|
||||
static bool idle(RadioLibWrapper& w, uint32_t expected) {
|
||||
uint32_t delay=999;
|
||||
const bool allowed=w.getProfileIdleDelay(delay);
|
||||
assert(delay==expected);
|
||||
return allowed;
|
||||
}
|
||||
'''
|
||||
|
||||
ADMISSION = r'''
|
||||
int main() {
|
||||
RadioLibWrapper w; ready(w);
|
||||
const uint64_t start=w._esp32_profile_visit_started_us;
|
||||
const uint32_t visit=w._esp32_profile_idle_visit_us;
|
||||
assert(visit>30000);
|
||||
// Frontend settling keeps polling even when the whole visit is long.
|
||||
elapsed_us=start+RadioLibWrapper::NoiseFloorSettleMillis*1000-1;
|
||||
assert(!idle(w,0));
|
||||
elapsed_us=start+RadioLibWrapper::NoiseFloorSettleMillis*1000;
|
||||
assert(idle(w,10));
|
||||
// The original RX stamp survives repeated loop passes on the same tuple.
|
||||
w.serviceProfileScan();
|
||||
assert(w._esp32_profile_visit_started_us==start && idle(w,10));
|
||||
|
||||
// Visit end is strict: require 4 ms guard plus at least 2 ms of useful idle.
|
||||
for (uint32_t remaining=0; remaining<=17000; ++remaining) {
|
||||
elapsed_us=start+visit-remaining;
|
||||
const auto usable=remaining>=6000 ? (remaining-4000)/1000 : 0;
|
||||
const auto expected=usable>10 ? 10 : usable;
|
||||
assert(idle(w,expected)==(expected!=0));
|
||||
}
|
||||
elapsed_us=start+visit+1;
|
||||
assert(!idle(w,0));
|
||||
// Stale low-32-bit clock equality after a full wrap is never a new visit.
|
||||
elapsed_us=start+0x100000000ULL+10000;
|
||||
assert(!idle(w,0));
|
||||
elapsed_us=start-1;
|
||||
assert(!idle(w,0));
|
||||
|
||||
// A late IRQ, RX restart, owner or changed tuning proof revokes admission.
|
||||
auto denied=[&](auto mutate) {
|
||||
RadioLibWrapper fresh; ready(fresh);
|
||||
elapsed_us=fresh._esp32_profile_visit_started_us+10000;
|
||||
assert(idle(fresh,10));
|
||||
mutate(fresh);
|
||||
assert(!idle(fresh,0));
|
||||
};
|
||||
denied([](auto& x){x._profiles.secondary.mode=mesh::RadioProfileMode::Off;});
|
||||
denied([](auto& x){x._esp32_profile_idle_visit_us=0;});
|
||||
denied([](auto& x){x._params_valid=false;});
|
||||
denied([](auto& x){x.state=STATE_TX_WAIT;});
|
||||
denied([](auto& x){x._profile_rxps_suspended=false;});
|
||||
denied([](auto& x){x._rx_ps_enabled=true;});
|
||||
denied([](auto& x){x._rx_ps_armed=true;});
|
||||
denied([](auto& x){x._rx_hold_continuous=true;});
|
||||
denied([](auto& x){x._rx_retry_pending=true;});
|
||||
denied([](auto& x){x._profile_retry_at=1;});
|
||||
denied([](auto& x){x._profile_refresh_required=true;});
|
||||
denied([](auto& x){x._profile_repair_required=true;});
|
||||
denied([](auto& x){x._nf_refresh_requested=true;});
|
||||
denied([](auto& x){x._nf_calib_active=true;});
|
||||
denied([](auto& x){x._noise_floor_valid=false;});
|
||||
denied([](auto& x){x._noise_floor_secondary_valid=false;});
|
||||
denied([](auto& x){x._wd_observe_until=1;});
|
||||
denied([](auto& x){x._cw_active=true;});
|
||||
denied([](auto& x){x._morse_owned=true;});
|
||||
denied([](auto& x){x._beacon_owned=true;});
|
||||
denied([](auto& x){x._morse_rx_owned=true;});
|
||||
denied([](auto& x){x.events.record();});
|
||||
denied([](auto& x){x._profiles.resetSwitchTest();});
|
||||
denied([](auto& x){x._active_profile=2;});
|
||||
denied([](auto& x){x._active_profile^=1;});
|
||||
denied([](auto& x){++x._profile_generation;});
|
||||
denied([](auto& x){++x._profiles.generation[0];});
|
||||
denied([](auto& x){++x._profiles.generation[1];});
|
||||
denied([](auto& x){++x._profile_scan_generation[0];});
|
||||
denied([](auto& x){++x._profile_scan_generation[1];});
|
||||
denied([](auto& x){++x._esp32_profile_idle_generation[0];});
|
||||
denied([](auto& x){++x._esp32_profile_idle_generation[1];});
|
||||
denied([](auto& x){++x._esp32_profile_visit_started_us;});
|
||||
denied([](auto& x){++x._physical_preamble;});
|
||||
denied([](auto& x){++x._profiles.switch_test_max_us[1];});
|
||||
|
||||
// Both scanner generations matter even if a replacement copied the same
|
||||
// active tuple and its retune sample budget happened to be unchanged.
|
||||
denied([](auto& x){
|
||||
++x._profiles.generation[1]; ++x._profile_scan_generation[1];
|
||||
});
|
||||
}
|
||||
'''
|
||||
|
||||
SCANNER = r'''
|
||||
int main() {
|
||||
RadioLibWrapper w; ready(w);
|
||||
auto start=w._esp32_profile_visit_started_us;
|
||||
elapsed_us=start+10000;
|
||||
assert(idle(w,10));
|
||||
w._cw_active=true; w.serviceProfileScan();
|
||||
assert(w._esp32_profile_idle_visit_us==0 && !idle(w,0));
|
||||
w._cw_active=false; w.serviceProfileScan(); assert(idle(w,10));
|
||||
w._profile_retry_at=millis()+1000; w.serviceProfileScan();
|
||||
assert(w._esp32_profile_idle_visit_us==0);
|
||||
w._profile_retry_at=0; w.serviceProfileScan(); assert(idle(w,10));
|
||||
w.busy=true; w.serviceProfileScan(); assert(!idle(w,0));
|
||||
w.busy=false; w.packet=true; w.serviceProfileScan(); assert(!idle(w,0));
|
||||
w.packet=false; w.events.record(); w.serviceProfileScan(); assert(!idle(w,0));
|
||||
w.events.claim(); w.serviceProfileScan(); assert(idle(w,10));
|
||||
|
||||
// A timer wake advances the same clock the scanner uses. It cannot let a
|
||||
// due scan spend an extra visit on the old profile or reuse its old budget.
|
||||
auto previous=w._active_profile;
|
||||
elapsed_us=start+w._esp32_profile_idle_visit_us;
|
||||
w.serviceProfileScan();
|
||||
assert(w._active_profile!=previous && !idle(w,0));
|
||||
start=w._esp32_profile_visit_started_us;
|
||||
w.serviceProfileScan();
|
||||
assert(w._esp32_profile_idle_stamp_us==start && !idle(w,0));
|
||||
|
||||
// The fast visit is sub-ms (SF5/BW500) and must keep polling throughout.
|
||||
RadioLibWrapper fast; ready(fast,5,500);
|
||||
assert(fast._esp32_profile_idle_visit_us<1000 && !idle(fast,0));
|
||||
elapsed_us+=fast._esp32_profile_idle_visit_us;
|
||||
fast.serviceProfileScan();
|
||||
assert(!idle(fast,0));
|
||||
|
||||
// A long hold spanning micros() wrap still causes a real hop on release.
|
||||
RadioLibWrapper wrap; ready(wrap);
|
||||
const auto old_profile=wrap._active_profile;
|
||||
elapsed_us=wrap._esp32_profile_visit_started_us+0x100000000ULL+10000;
|
||||
wrap.serviceProfileScan();
|
||||
assert(wrap._active_profile!=old_profile && !idle(wrap,0));
|
||||
assert(wrap._esp32_profile_visit_started_us==elapsed_us);
|
||||
assert(wrap._profile_visit_stamp==uint32_t(elapsed_us));
|
||||
|
||||
RadioLibWrapper changed; ready(changed);
|
||||
elapsed_us=changed._esp32_profile_visit_started_us+10000;
|
||||
assert(idle(changed,10));
|
||||
auto replacement=changed._profiles.secondary;
|
||||
replacement.params.freq+=1;
|
||||
changed._profiles.setSecondary(replacement,false);
|
||||
assert(!idle(changed,0));
|
||||
changed.serviceProfileScan();
|
||||
assert(changed._esp32_profile_idle_visit_us==0 && !idle(changed,0));
|
||||
assert(changed._nf_refresh_requested && !changed._noise_floor_secondary_valid);
|
||||
changed._profiles.setSecondary({},false);
|
||||
changed.serviceProfileScan();
|
||||
assert(changed._esp32_profile_idle_visit_us==0 && !idle(changed,0));
|
||||
assert(!changed._profile_rxps_suspended && changed._rx_ps_enabled);
|
||||
}
|
||||
'''
|
||||
|
||||
|
||||
class Esp32ProfileIdleTest(unittest.TestCase):
|
||||
@staticmethod
|
||||
def fixture():
|
||||
source = (ROOT / "src/helpers/radiolib/RadioLibWrappers.cpp").read_text()
|
||||
names = [("uint8_t", "beginReconfigure"), ("void", "endReconfigure"),
|
||||
("void", "serviceProfileScan")] + [("mesh::RadioParamApplyResult", name) for name in
|
||||
("tuneProfile", "prepareTransmitProfile", "tryRestoreCodingRate",
|
||||
"trySetParams", "trySetPrimaryParams")]
|
||||
methods = "\n".join(body(source, f"{kind} RadioLibWrapper::{name}(") for kind, name in names)
|
||||
methods += "\n" + body(source,
|
||||
"mesh::RadioParamApplyResult RadioLibWrapper::prepareTransmitProfile(uint8_t profile, bool")
|
||||
query = body(source, "bool RadioLibWrapper::getProfileIdleDelay(")
|
||||
# This final admission must never perform SPI, derive float timing or
|
||||
# run the scanner. Every such operation precedes the published proof.
|
||||
for forbidden in ("profilePreamble(", "listenUs(", "profileTimestamp(",
|
||||
"isChipBusy(", "isPacketPendingOrReceiving(", "tuneProfile("):
|
||||
assert forbidden not in query, forbidden
|
||||
methods += "\n" + query
|
||||
wrapper = (ROOT / "src/helpers/radiolib/CustomSX1262Wrapper.h").read_text()
|
||||
methods += "\n" + body(wrapper, "void setProfileStandbyWarm(").replace(
|
||||
"void setProfileStandbyWarm(bool enabled) override",
|
||||
"void RadioLibWrapper::setProfileStandbyWarm(bool enabled)")
|
||||
fixture = SCAN_HARNESS.split("int main()", 1)[0]
|
||||
fixture = fixture.replace(" bool _cw_active=false;", ESP_MEMBERS + "\n bool _cw_active=false;")
|
||||
fixture = fixture.replace("static uint64_t elapsed_us;", """
|
||||
#define MESH_ENABLE_TX_BEACONS 1
|
||||
#define MESH_ENABLE_MORSE_RX 1
|
||||
static uint64_t elapsed_us;
|
||||
int64_t esp_timer_get_time() { return static_cast<int64_t>(elapsed_us); }
|
||||
""")
|
||||
fixture = fixture.replace("state=STATE_RX; _profile_visit_stamp=profileTimestamp(_profiles.enabled());",
|
||||
"state=STATE_RX; _esp32_profile_visit_started_us=elapsed_us; "
|
||||
"_profile_visit_stamp=profileTimestamp(_profiles.enabled());")
|
||||
return fixture.replace("@METHODS@", methods) + COMMON
|
||||
|
||||
def run_fixture(self, main):
|
||||
with tempfile.TemporaryDirectory(prefix="meshcore-esp32-profile-idle-") as directory:
|
||||
folder = Path(directory)
|
||||
(folder / "Arduino.h").write_text("#pragma once\n#include <cstdint>\n#include <cmath>\n")
|
||||
(folder / "test.cpp").write_text(self.fixture() + main)
|
||||
flags = ["-std=c++17", "-Wall", "-Wextra", "-Werror", "-DESP32_PLATFORM=1"]
|
||||
if os.name != "nt":
|
||||
flags += ["-fsanitize=address,undefined", "-fno-sanitize-recover=all", "-fno-pie", "-no-pie"]
|
||||
result = subprocess.run([os.environ.get("CXX", "c++"), *flags,
|
||||
"-I", directory, "-I", str(ROOT / "src"), str(folder / "test.cpp"),
|
||||
"-o", str(folder / "test")], capture_output=True, text=True, timeout=60)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
result = subprocess.run([str(folder / "test")], capture_output=True, text=True, timeout=20)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
|
||||
def test_late_integer_admission_boundaries_and_revocation(self):
|
||||
self.run_fixture(ADMISSION)
|
||||
|
||||
def test_real_scanner_clock_hops_and_failed_publication(self):
|
||||
self.run_fixture(SCANNER)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -121,6 +121,7 @@ int main() {
|
||||
#define ESP32_PLATFORM
|
||||
uint32_t clockUs=0;
|
||||
uint32_t micros() { return clockUs; }
|
||||
int64_t esp_timer_get_time() { return clockUs; }
|
||||
@CLOCK@
|
||||
struct BenchWrapper { uint32_t _profile_visit_stamp=0; @GETTER@ };
|
||||
int main() {
|
||||
|
||||
@@ -20,6 +20,8 @@ struct FakeRtc {
|
||||
} rtc;
|
||||
#define NRF_RTC2 (&rtc)
|
||||
uint32_t micros() { return 1234; }
|
||||
static uint64_t esp_elapsed_us=1234;
|
||||
int64_t esp_timer_get_time() { return static_cast<int64_t>(esp_elapsed_us); }
|
||||
@CLOCK@
|
||||
int main() {
|
||||
assert(profileTimestamp(false) == 1234);
|
||||
@@ -61,6 +63,17 @@ int main() {
|
||||
rtc.COUNTER=(rtc.COUNTER+32768)&0x00ffffffUL;
|
||||
const auto wrapped=profileTimestamp(true);assert(wrapped==0);
|
||||
assert(profileElapsedUs(before,wrapped,true)==1000000UL);
|
||||
#elif defined(ESP32_PLATFORM)
|
||||
assert(profileTimestamp(true) == 1234);
|
||||
// The ESP Timer is the light-sleep-adjusted clock; a frozen cycle/micros
|
||||
// source must not extend a radio visit while the MCU was asleep.
|
||||
esp_elapsed_us+=10000;
|
||||
assert(micros()==1234 && profileTimestamp(true)==11234);
|
||||
assert(profileTimestamp(false)==11234);
|
||||
esp_elapsed_us=0x100000010ULL;
|
||||
assert(profileTimestamp(true)==0x10);
|
||||
assert(profileElapsedUs(0xfffffff0,profileTimestamp(true),true)==32);
|
||||
assert(rtc.TASKS_START == 0 && rtc.TASKS_STOP == 0);
|
||||
#else
|
||||
assert(profileTimestamp(true) == 1234);
|
||||
assert(profileElapsedUs(12, 42, true) == 30);
|
||||
@@ -76,7 +89,7 @@ class ProfileClockTest(unittest.TestCase):
|
||||
cpp = Path(folder) / "test.cpp"
|
||||
exe = Path(folder) / "test.exe"
|
||||
cpp.write_text(HARNESS.replace("@CLOCK@", CLOCK))
|
||||
for defines in ([], ["-DNRF52_PLATFORM=1"]):
|
||||
for defines in ([], ["-DNRF52_PLATFORM=1"], ["-DESP32_PLATFORM=1"]):
|
||||
result = subprocess.run(
|
||||
[os.environ.get("CXX", "g++"), "-std=c++17", "-Wall", "-Wextra",
|
||||
"-Werror", *defines, str(cpp), "-o", str(exe)],
|
||||
|
||||
@@ -76,8 +76,10 @@ int main() {
|
||||
Manager manager;
|
||||
Dispatcher legacy{&unknown, &clock, &manager};
|
||||
assert(legacy.canIdleDualRadio());
|
||||
assert(!legacy.getDualRadioIdleDelay(unchanged) && unchanged == 0);
|
||||
unknown.profile.active = true;
|
||||
assert(!legacy.canIdleDualRadio());
|
||||
assert(!legacy.getDualRadioIdleDelay(unchanged) && unchanged == 0);
|
||||
assert(legacy.getNextQueueWakeDelay(unchanged) && unchanged == 0);
|
||||
|
||||
for (uint32_t now : {1000U, UINT32_MAX - 15U}) {
|
||||
@@ -87,20 +89,25 @@ int main() {
|
||||
Dispatcher node{&radio, &clock, &manager};
|
||||
uint32_t delay = 99;
|
||||
assert(node.canIdleDualRadio() && radio.reads == 0);
|
||||
assert(!node.getDualRadioIdleDelay(delay) && delay == 0 && radio.reads == 0);
|
||||
assert(!node.getNextQueueWakeDelay(delay) && radio.reads == 0);
|
||||
radio.profile.active = true;
|
||||
radio.delay = 37;
|
||||
assert(!node.canIdleDualRadio()); // Unknown ports keep their polling contract.
|
||||
assert(!node.getDualRadioIdleDelay(delay) && delay == 0); // Ignore a false writer's output.
|
||||
assert(node.getNextQueueWakeDelay(delay) && delay == 0);
|
||||
radio.supported = true;
|
||||
radio.delay = 0;
|
||||
assert(!node.canIdleDualRadio()); // A due/too-short visit is not an idle budget.
|
||||
assert(!node.getDualRadioIdleDelay(delay) && delay == 0);
|
||||
assert(node.getNextQueueWakeDelay(delay) && delay == 0);
|
||||
radio.delay = 37;
|
||||
assert(node.canIdleDualRadio());
|
||||
assert(node.getDualRadioIdleDelay(delay) && delay == 37);
|
||||
assert(node.getNextQueueWakeDelay(delay) && delay == 37);
|
||||
node.radio_available = false;
|
||||
assert(!node.canIdleDualRadio());
|
||||
assert(!node.getDualRadioIdleDelay(delay) && delay == 0);
|
||||
assert(node.getNextQueueWakeDelay(delay) && delay == 0);
|
||||
node.radio_available = true;
|
||||
|
||||
@@ -173,7 +180,8 @@ class RadioProfileIdleDispatcherTest(unittest.TestCase):
|
||||
header = (ROOT / 'src/Dispatcher.h').read_text(encoding='ascii')
|
||||
source = (ROOT / 'src/Dispatcher.cpp').read_text(encoding='ascii')
|
||||
code = HARNESS.replace('@DEFAULT@', method(header, 'virtual bool getProfileIdleDelay('))
|
||||
code = code.replace('@ADMISSION@', method(header, 'bool canIdleDualRadio() const'))
|
||||
code = code.replace('@ADMISSION@', method(header, 'bool canIdleDualRadio() const') + '\n'
|
||||
+ method(header, 'bool getDualRadioIdleDelay('))
|
||||
code = code.replace('@DEADLINES@', method(source, 'bool Dispatcher::getNextQueueWakeDelay('))
|
||||
with tempfile.TemporaryDirectory(prefix='meshcore-profile-idle-dispatcher-') as directory:
|
||||
cpp, binary = Path(directory) / 'test.cpp', Path(directory) / 'test'
|
||||
|
||||
Reference in new issue
Block a user