Expose actual BLE identity in nRF52 trace diagnostics
This commit is contained in:
1 parent
c1c0c3178c
commit
042d5b7f0d
3 files changed
+122
No files matched your search
@@ -24,6 +24,9 @@ extern "C" { MeshBleTraceBuffer mesh_ble_trace; }
|
||||
void meshBleTraceState(char* reply, size_t capacity) {
|
||||
snprintf(reply,capacity,"> state");
|
||||
}
|
||||
void meshBleTraceIdentity(char* reply, size_t capacity) {
|
||||
snprintf(reply,capacity,"> identity");
|
||||
}
|
||||
int main() {
|
||||
char reply[160];
|
||||
assert(!meshBleTraceCommand("get radio",reply,sizeof(reply)));
|
||||
@@ -42,6 +45,10 @@ int main() {
|
||||
assert(!strcmp(reply,"> 257,1234,0x0010,65535,0,0"));
|
||||
assert(meshBleTraceCommand("get bluetooth.trace.state",reply,sizeof(reply)));
|
||||
assert(!strcmp(reply,"> state"));
|
||||
for (const char* command : {"get bluetooth.trace.identity", "get ble.trace.identity"}) {
|
||||
assert(meshBleTraceCommand(command,reply,sizeof(reply)));
|
||||
assert(!strcmp(reply,"> identity"));
|
||||
}
|
||||
meshBleTraceDispatchEnter(0x10,0);
|
||||
assert(mesh_ble_trace.dispatch_event.load()==0x10);
|
||||
meshBleTraceDispatchExit(0x10,0);
|
||||
@@ -76,6 +83,73 @@ int main() {
|
||||
check=True, capture_output=True)
|
||||
subprocess.run([str(binary)],check=True,capture_output=True)
|
||||
|
||||
def test_real_identity_query_bounds_sdk_outputs_and_reports_failures(self):
|
||||
source = (ROOT / "src/helpers/nrf52/SerialBLEInterface.cpp").read_text()
|
||||
start = source.index("void meshBleTraceIdentity(")
|
||||
implementation = source[start:source.index("\n}\n", start) + 3]
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
work = Path(directory)
|
||||
cpp = work / "identity.cpp"
|
||||
cpp.write_text(r'''
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
static constexpr uint32_t NRF_SUCCESS = 0;
|
||||
struct ble_gap_addr_t { uint8_t addr_type; uint8_t addr[6]; };
|
||||
static bool initialized;
|
||||
static void* instance;
|
||||
static unsigned calls;
|
||||
static uint32_t address_result, name_result;
|
||||
static uint16_t actual_length;
|
||||
struct Data { uint8_t count() { return 255; } };
|
||||
static struct { Data Advertising, ScanResponse; } Bluefruit;
|
||||
static uint32_t sd_ble_gap_addr_get(ble_gap_addr_t* address) {
|
||||
assert(initialized); ++calls;
|
||||
*address = {1, {0xc6, 0xbb, 0xcd, 0x86, 0xa0, 0xd1}};
|
||||
return address_result;
|
||||
}
|
||||
static uint32_t sd_ble_gap_device_name_get(uint8_t* name, uint16_t* length) {
|
||||
assert(initialized && *length == 32); ++calls;
|
||||
const uint8_t bytes[] = {0x41, 0x00, 0xc3, 0xa9};
|
||||
const size_t copied = actual_length < *length ? actual_length : *length;
|
||||
for (size_t i = 0; i < copied; ++i) name[i] = bytes[i % sizeof(bytes)];
|
||||
*length = actual_length;
|
||||
return name_result;
|
||||
}
|
||||
@IMPLEMENTATION@
|
||||
int main() {
|
||||
char reply[172];
|
||||
meshBleTraceIdentity(reply, sizeof(reply));
|
||||
assert(!strcmp(reply, "> Bluetooth not initialized") && calls == 0);
|
||||
initialized = true; instance = &initialized; actual_length = 4;
|
||||
meshBleTraceIdentity(reply, sizeof(reply));
|
||||
assert(!strcmp(reply, "> a=0,t=1,mac=d1a086cdbbc6,n=0,len=4,hex=4100c3a9,adv=255,scan=255"));
|
||||
actual_length = 65535;
|
||||
meshBleTraceIdentity(reply, sizeof(reply));
|
||||
assert(strstr(reply, ",len=65535,hex="));
|
||||
assert(strstr(reply, ",adv=255,scan=255"));
|
||||
assert(strlen(reply) < sizeof(reply) - 1);
|
||||
const char* begin = strstr(reply, "hex=") + 4;
|
||||
assert(strchr(begin, ',') - begin == 64);
|
||||
address_result = name_result = UINT32_MAX;
|
||||
meshBleTraceIdentity(reply, sizeof(reply));
|
||||
assert(!strcmp(reply, "> a=4294967295,t=255,mac=,n=4294967295,len=65535,hex=,adv=255,scan=255"));
|
||||
char tiny[5] = {};
|
||||
meshBleTraceIdentity(tiny, sizeof(tiny));
|
||||
assert(tiny[4] == 0);
|
||||
}
|
||||
'''.replace("@IMPLEMENTATION@", implementation))
|
||||
binary = work / "identity"
|
||||
compiled = subprocess.run([
|
||||
"c++", "-std=c++17", "-O2", "-flto", "-Wall", "-Wextra", "-Werror",
|
||||
"-fsanitize=address,undefined", "-fno-omit-frame-pointer",
|
||||
"-fno-pie", "-no-pie", str(cpp), "-o", str(binary),
|
||||
], capture_output=True, text=True)
|
||||
self.assertEqual(compiled.returncode, 0, compiled.stderr)
|
||||
result = subprocess.run([str(binary)], capture_output=True, text=True)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
|
||||
def test_concurrent_reader_never_accepts_mixed_records(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
work = Path(directory)
|
||||
|
||||
Reference in new issue
Block a user