From 2b55ff6a621584f661e9d6d3b3f3ab48fb3639de Mon Sep 17 00:00:00 2001 From: mikecarper Date: Tue, 27 Oct 2026 08:17:23 -0700 Subject: [PATCH] Recover RAK RAM without reducing OTA or sensor capacities Cache each validated OTA descriptor field once and size manifest reassembly to the admitted wire format. Compile reviewed read-only VL53L0X tuning data in flash through a private dependency copy, retaining the original implementation for unknown revisions. --- .github/workflows/run-unit-tests.yml | 1 + scripts/nrf52_flash_trim.py | 5 + scripts/nrf52_vl53l0x_const.py | 81 ++++++++ src/helpers/ota/OtaManager.cpp | 8 +- src/helpers/ota/OtaManager.h | 54 +++-- test/fixtures/vl53_const/README.md | 11 + test/fixtures/vl53_const/tuning_loader.inc | 109 ++++++++++ .../vl53l0x_interrupt_threshold_settings.h | 81 ++++++++ test/fixtures/vl53_const/vl53l0x_tuning.h | 87 ++++++++ test/test_nrf52_flash_trim.py | 7 + test/test_nrf52_vl53l0x_const.py | 190 ++++++++++++++++++ test/test_ota/test_ota_core.cpp | 43 ++++ test/test_ota_memory_layout.py | 44 +++- 13 files changed, 696 insertions(+), 25 deletions(-) create mode 100644 scripts/nrf52_vl53l0x_const.py create mode 100644 test/fixtures/vl53_const/README.md create mode 100644 test/fixtures/vl53_const/tuning_loader.inc create mode 100644 test/fixtures/vl53_const/vl53l0x_interrupt_threshold_settings.h create mode 100644 test/fixtures/vl53_const/vl53l0x_tuning.h create mode 100644 test/test_nrf52_vl53l0x_const.py diff --git a/.github/workflows/run-unit-tests.yml b/.github/workflows/run-unit-tests.yml index ca217bc92..d89db099e 100644 --- a/.github/workflows/run-unit-tests.yml +++ b/.github/workflows/run-unit-tests.yml @@ -1143,6 +1143,7 @@ jobs: - name: Verify nRF52 infrastructure trims and unchanged Companion scope run: | python3 -B test/test_nrf52_flash_trim.py -v + python3 -B test/test_nrf52_vl53l0x_const.py -v python3 -B test/test_nrf52_gnss_version_trim.py -v python3 -B test/test_nrf52_program_lifetime.py -v python3 -B test/test_nrf52_tft_font_trim.py -v diff --git a/scripts/nrf52_flash_trim.py b/scripts/nrf52_flash_trim.py index 4d1a19f4b..a7116487f 100644 --- a/scripts/nrf52_flash_trim.py +++ b/scripts/nrf52_flash_trim.py @@ -243,6 +243,11 @@ def install(build_env): return build_env["MESH_NRF52_FLASH_TRIM_ACTIVE"] = True build_env.AppendUnique(CPPDEFINES=list(definitions.items())) + hook_path = build_env.subst("$PROJECT_DIR/scripts/nrf52_vl53l0x_const.py") + hook_spec = importlib.util.spec_from_file_location("mesh_nrf52_vl53_const", hook_path) + hook = importlib.util.module_from_spec(hook_spec) + hook_spec.loader.exec_module(hook) + hook.install(build_env) if infrastructure: print("nRF52 infrastructure flash trim: compact crypto, unused USB device classes/GPS cache/vendor splash, compact OTA target names") else: diff --git a/scripts/nrf52_vl53l0x_const.py b/scripts/nrf52_vl53l0x_const.py new file mode 100644 index 000000000..0d52ae433 --- /dev/null +++ b/scripts/nrf52_vl53l0x_const.py @@ -0,0 +1,81 @@ +"""Keep the reviewed VL53L0X tuning tables in nRF52 flash. + +The vendor tuning loader only reads these bytes and copies register writes +through a local buffer. Compile a private dependency copy with const inputs; +leave its public caller-owned tuning-buffer setter/getter unchanged. Unknown +library versions retain their ordinary implementation and RAM requirements. +""" + +import hashlib +from pathlib import Path +import shutil + + +FILES = { + "vl53l0x_tuning.h": "0e0331b7df1084d183248355f75d835b3446200b22f71765b5c63a470ff01c63", + "vl53l0x_interrupt_threshold_settings.h": "9e5e6c00af66497d03d6c6a13a269521c12b33c90455efcd3b8494487e1780c2", + "vl53l0x_api_core.h": "948de4a0363339facc58c4e1bc5781b954b99cca1c13a4a105eb3969aeba8653", + "core/src/vl53l0x_api_core.cpp": "d31da82edbad0f7fd89106340f7a006b61cd2e5b7c353460fb394978ce55953b", + "core/src/vl53l0x_api.cpp": "6cfa0784600bd797e372b511761f4be10e97264b55d5c7269dd41ba607b8edb7", +} +REPLACEMENTS = { + "vl53l0x_tuning.h": ("uint8_t DefaultTuningSettings[]", "const uint8_t DefaultTuningSettings[]"), + "vl53l0x_interrupt_threshold_settings.h": ( + "uint8_t InterruptThresholdSettings[]", "const uint8_t InterruptThresholdSettings[]"), + "vl53l0x_api_core.h": ("uint8_t* pTuningSettingBuffer);", "const uint8_t* pTuningSettingBuffer);"), + "core/src/vl53l0x_api_core.cpp": ( + "uint8_t* pTuningSettingBuffer) {", "const uint8_t* pTuningSettingBuffer) {"), + "core/src/vl53l0x_api.cpp": ("uint8_t* pTuningSettingBuffer;", "const uint8_t* pTuningSettingBuffer;"), +} + + +def digest(text): + return hashlib.sha256(text.encode("utf-8")).hexdigest() + + +def patched_files(source_root): + result = {} + for name, expected in FILES.items(): + text = (source_root / name).read_text(encoding="utf-8").replace("\r\n", "\n") + old, new = REPLACEMENTS[name] + if digest(text) != expected or text.count(old) != 1: + raise RuntimeError("changed reviewed VL53L0X dependency: " + name) + result[name] = text.replace(old, new, 1) + return result + + +def write_if_changed(path, content): + if not path.exists() or path.read_text(encoding="utf-8") != content: + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(content, encoding="utf-8") + + +def replace_vl53_source(build_env, node): + source = Path(node.srcnode().get_abspath()) + source_root = next((parent for parent in source.parents + if parent.name == "src" and parent.parent.name == "Adafruit_VL53L0X"), None) + if source_root is None or source.suffix != ".cpp": + return node + try: + patches = patched_files(source_root) + except (OSError, UnicodeError, RuntimeError) as error: + print("nRF52 VL53L0X const tables: ordinary source retained (" + str(error) + ")") + return node + destination = Path(build_env.subst("$BUILD_DIR")) / "readonly-nrf52-vl53l0x" / "src" + # Keep every quoted sibling/relative include inside the same private copy. + # All compilation units therefore see the same const loader declaration. + for path in source_root.rglob("*"): + if not path.is_file(): + continue + relative = path.relative_to(source_root) + output = destination / relative + if relative.as_posix() in patches: + write_if_changed(output, patches[relative.as_posix()]) + elif not output.exists() or output.read_bytes() != path.read_bytes(): + output.parent.mkdir(parents=True, exist_ok=True) + shutil.copyfile(path, output) + return build_env.File(str(destination / source.relative_to(source_root))) + + +def install(build_env): + build_env.AddBuildMiddleware(replace_vl53_source, "*Adafruit_VL53L0X*/*.cpp") diff --git a/src/helpers/ota/OtaManager.cpp b/src/helpers/ota/OtaManager.cpp index e853d2691..5ef6f0295 100644 --- a/src/helpers/ota/OtaManager.cpp +++ b/src/helpers/ota/OtaManager.cpp @@ -334,10 +334,8 @@ void OtaManager::refresh_sources() { (uint64_t)d.payload_off + d.payload_size + 5 != d.total_size) continue; if (serveEntryIndex(d.mid) >= 0) continue; // already offered (e.g. our own fw in the folder) ServeEntry& e = _serve[_n_serve++]; - memcpy(e.mid, d.mid, 4); - e.target_id = d.target_id; e.fw_version = d.fw_version; - e.codec_id = d.codec_id; e.flags = d.flags; e.have_count = d.block_count; // a folder mota is fully held - e.is_self = false; e.src = src; e.src_idx = i; e.desc = d; + e.cacheDescriptor(d); + e.is_self = false; e.src = src; e.src_idx = i; _src_advertised[s]++; } } @@ -409,7 +407,7 @@ OtaManager::ServeView* OtaManager::resolve(const uint8_t* mid) { // builds also cache leaves here; bounded builds acquire proof metadata through // cooperative egress steps, without delaying request admission on a full table. bool OtaManager::loadSource(const ServeEntry& e) { - const MotaDesc& d = e.desc; + const MotaDesc d = e.descriptor(); if (d.leaves_off < 8) return false; if (!e.src) return false; uint16_t mfl = (uint16_t)(d.leaves_off - 8); diff --git a/src/helpers/ota/OtaManager.h b/src/helpers/ota/OtaManager.h index d7a32c5f1..1ec321ed8 100644 --- a/src/helpers/ota/OtaManager.h +++ b/src/helpers/ota/OtaManager.h @@ -2,6 +2,7 @@ #include #include +#include #include "OtaFormat.h" #include "OtaMemoryPolicy.h" #include "OtaTiming.h" @@ -327,19 +328,44 @@ public: }; // A lightweight catalog entry: what we advertise per mota + how to load its ServeView on demand. struct ServeEntry { - // Named fields only: this is not a wire/persistent layout. Group aligned - // values ahead of byte flags instead of paying three padding gaps per row. - MotaSource* src; // load from this external source ... - MotaDesc desc; // cached region offsets for the source entry - uint32_t target_id, fw_version; - uint32_t have_count; // blocks we currently hold (== block_count when complete) - uint8_t mid[4]; - uint8_t codec_id, flags; - bool is_self; // true => entry is view0 (our own fw / RAM mota) - uint8_t src_idx; // ... at this index + // This is not a wire/persistent layout. Source admission proves the fixed + // header/leaves/payload geometry, so cache each field once and reconstruct + // the three derived offsets/sizes instead of retaining another MotaDesc. + MotaSource* src = nullptr; // load from this external source ... + uint32_t target_id = 0, fw_version = 0; + uint32_t have_count = 0; // complete sources hold every block + uint32_t payload_size = 0; + uint8_t mid[4] = {}; + uint8_t codec_id = 0, flags = 0; + bool is_self = false; // true => entry is view0 (our own fw / RAM mota) + uint8_t src_idx = 0; // ... at this index + uint8_t block_size_log2 = 0, source_caps = 0; + + void cacheDescriptor(const MotaDesc& d) { + memcpy(mid, d.mid, sizeof(mid)); + target_id = d.target_id; fw_version = d.fw_version; + codec_id = d.codec_id; flags = d.flags; have_count = d.block_count; + payload_size = d.payload_size; + block_size_log2 = d.block_size_log2; source_caps = d.source_caps; + } + MotaDesc descriptor() const { + MotaDesc d; + if (!have_count) return d; + memcpy(d.mid, mid, sizeof(mid)); + d.target_id = target_id; d.fw_version = fw_version; + d.codec_id = codec_id; d.flags = flags; d.block_count = have_count; + d.payload_size = payload_size; + d.block_size_log2 = block_size_log2; d.source_caps = source_caps; + d.leaves_off = 8u + MOTA_MFL; + d.payload_off = d.leaves_off + have_count * 4u; + d.total_size = d.payload_off + payload_size + 5u; + return d; + } }; - static_assert(sizeof(ServeEntry) <= sizeof(MotaDesc) + sizeof(MotaSource*) + 20u, - "OTA source catalog must not add per-entry alignment padding"); + static_assert(sizeof(ServeEntry) == + ((sizeof(MotaSource*) + 28u + alignof(MotaSource*) - 1u) + / alignof(MotaSource*)) * alignof(MotaSource*), + "OTA source catalog must not duplicate validated descriptor fields"); // Context for the source-payload reader trampoline. struct SrcReadCtx { MotaSource* src; uint8_t idx; uint32_t payload_off; }; @@ -903,7 +929,9 @@ private: uint8_t _observed_path_transmissions = 0; // source + relays; 0 falls back to configured max_hops+1 uint32_t _packets_sent = 0; // OTA packets accepted by the radio adapter (wrap-safe) // multi-fragment manifest reassembly (a signed v2 manifest exceeds one packet) - uint8_t _mf_buf[OTA_MF_MAXFRAG * OTA_MF_FRAG]; // sized to the fragment cap so no valid manifest is silently dropped + // handleManifest admits only the fixed MOTA_MFL bytes and exact fragment + // shapes. The transport's larger fragment-count ceiling adds no valid data. + uint8_t _mf_buf[MOTA_MFL]; uint16_t _mf_retries = 0; // GET_MANIFEST retries while WANT_MANIFEST (give up after a cap) uint8_t _mf_total = 0; // frag_total of the manifest being reassembled (0 = none) uint16_t _mf_mask = 0; // received manifest-fragment bitmap diff --git a/test/fixtures/vl53_const/README.md b/test/fixtures/vl53_const/README.md new file mode 100644 index 000000000..3c281cb6f --- /dev/null +++ b/test/fixtures/vl53_const/README.md @@ -0,0 +1,11 @@ +These fixtures preserve Adafruit_VL53L0X 1.2.5's two tuning tables and the +complete `VL53L0X_load_tuning_settings` function, with their ST license notices. +The loader's hardware write function is replaced only in the native harness. + +Source: https://github.com/adafruit/Adafruit_VL53L0X/tree/1.2.5/src + +`test_nrf52_vl53l0x_const.py` compares every register write and internal tuning +parameter before and after const qualification, including hardware failures. +The production middleware fingerprints the five complete affected files and +compiles a private library copy. Unknown dependency revisions use their ordinary +source; the downloaded library is never modified. diff --git a/test/fixtures/vl53_const/tuning_loader.inc b/test/fixtures/vl53_const/tuning_loader.inc new file mode 100644 index 000000000..67a4b0a9e --- /dev/null +++ b/test/fixtures/vl53_const/tuning_loader.inc @@ -0,0 +1,109 @@ +/******************************************************************************* + Copyright 2016, STMicroelectronics International N.V. + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of STMicroelectronics nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND + NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED. + IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +VL53L0X_Error VL53L0X_load_tuning_settings(VL53L0X_DEV Dev, + uint8_t* pTuningSettingBuffer) { + VL53L0X_Error Status = VL53L0X_ERROR_NONE; + int i; + int Index; + uint8_t msb; + uint8_t lsb; + uint8_t SelectParam; + uint8_t NumberOfWrites; + uint8_t Address; + uint8_t localBuffer[4]; /* max */ + uint16_t Temp16; + + LOG_FUNCTION_START(""); + + Index = 0; + + while ((*(pTuningSettingBuffer + Index) != 0) && + (Status == VL53L0X_ERROR_NONE)) { + NumberOfWrites = *(pTuningSettingBuffer + Index); + Index++; + if (NumberOfWrites == 0xFF) { + /* internal parameters */ + SelectParam = *(pTuningSettingBuffer + Index); + Index++; + switch (SelectParam) { + case 0: /* uint16_t SigmaEstRefArray -> 2 bytes */ + msb = *(pTuningSettingBuffer + Index); + Index++; + lsb = *(pTuningSettingBuffer + Index); + Index++; + Temp16 = VL53L0X_MAKEUINT16(lsb, msb); + PALDevDataSet(Dev, SigmaEstRefArray, Temp16); + break; + case 1: /* uint16_t SigmaEstEffPulseWidth -> 2 bytes */ + msb = *(pTuningSettingBuffer + Index); + Index++; + lsb = *(pTuningSettingBuffer + Index); + Index++; + Temp16 = VL53L0X_MAKEUINT16(lsb, msb); + PALDevDataSet(Dev, SigmaEstEffPulseWidth, Temp16); + break; + case 2: /* uint16_t SigmaEstEffAmbWidth -> 2 bytes */ + msb = *(pTuningSettingBuffer + Index); + Index++; + lsb = *(pTuningSettingBuffer + Index); + Index++; + Temp16 = VL53L0X_MAKEUINT16(lsb, msb); + PALDevDataSet(Dev, SigmaEstEffAmbWidth, Temp16); + break; + case 3: /* uint16_t targetRefRate -> 2 bytes */ + msb = *(pTuningSettingBuffer + Index); + Index++; + lsb = *(pTuningSettingBuffer + Index); + Index++; + Temp16 = VL53L0X_MAKEUINT16(lsb, msb); + PALDevDataSet(Dev, targetRefRate, Temp16); + break; + default: /* invalid parameter */ + Status = VL53L0X_ERROR_INVALID_PARAMS; + } + + } else if (NumberOfWrites <= 4) { + Address = *(pTuningSettingBuffer + Index); + Index++; + + for (i = 0; i < NumberOfWrites; i++) { + localBuffer[i] = *(pTuningSettingBuffer + Index); + Index++; + } + + Status = VL53L0X_WriteMulti(Dev, Address, localBuffer, NumberOfWrites); + + } else { + Status = VL53L0X_ERROR_INVALID_PARAMS; + } + } + + LOG_FUNCTION_END(Status); + return Status; +} diff --git a/test/fixtures/vl53_const/vl53l0x_interrupt_threshold_settings.h b/test/fixtures/vl53_const/vl53l0x_interrupt_threshold_settings.h new file mode 100644 index 000000000..64aba5ecf --- /dev/null +++ b/test/fixtures/vl53_const/vl53l0x_interrupt_threshold_settings.h @@ -0,0 +1,81 @@ +/******************************************************************************* +Copyright 2016, STMicroelectronics International N.V. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of STMicroelectronics nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND +NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED. +IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef _VL53L0X_INTERRUPT_THRESHOLD_SETTINGS_H_ +#define _VL53L0X_INTERRUPT_THRESHOLD_SETTINGS_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +uint8_t InterruptThresholdSettings[] = { + + /* Start of Interrupt Threshold Settings */ + 0x1, 0xff, 0x00, 0x1, 0x80, 0x01, 0x1, 0xff, 0x01, 0x1, 0x00, 0x00, + 0x1, 0xff, 0x01, 0x1, 0x4f, 0x02, 0x1, 0xFF, 0x0E, 0x1, 0x00, 0x03, + 0x1, 0x01, 0x84, 0x1, 0x02, 0x0A, 0x1, 0x03, 0x03, 0x1, 0x04, 0x08, + 0x1, 0x05, 0xC8, 0x1, 0x06, 0x03, 0x1, 0x07, 0x8D, 0x1, 0x08, 0x08, + 0x1, 0x09, 0xC6, 0x1, 0x0A, 0x01, 0x1, 0x0B, 0x02, 0x1, 0x0C, 0x00, + 0x1, 0x0D, 0xD5, 0x1, 0x0E, 0x18, 0x1, 0x0F, 0x12, 0x1, 0x10, 0x01, + 0x1, 0x11, 0x82, 0x1, 0x12, 0x00, 0x1, 0x13, 0xD5, 0x1, 0x14, 0x18, + 0x1, 0x15, 0x13, 0x1, 0x16, 0x03, 0x1, 0x17, 0x86, 0x1, 0x18, 0x0A, + 0x1, 0x19, 0x09, 0x1, 0x1A, 0x08, 0x1, 0x1B, 0xC2, 0x1, 0x1C, 0x03, + 0x1, 0x1D, 0x8F, 0x1, 0x1E, 0x0A, 0x1, 0x1F, 0x06, 0x1, 0x20, 0x01, + 0x1, 0x21, 0x02, 0x1, 0x22, 0x00, 0x1, 0x23, 0xD5, 0x1, 0x24, 0x18, + 0x1, 0x25, 0x22, 0x1, 0x26, 0x01, 0x1, 0x27, 0x82, 0x1, 0x28, 0x00, + 0x1, 0x29, 0xD5, 0x1, 0x2A, 0x18, 0x1, 0x2B, 0x0B, 0x1, 0x2C, 0x28, + 0x1, 0x2D, 0x78, 0x1, 0x2E, 0x28, 0x1, 0x2F, 0x91, 0x1, 0x30, 0x00, + 0x1, 0x31, 0x0B, 0x1, 0x32, 0x00, 0x1, 0x33, 0x0B, 0x1, 0x34, 0x00, + 0x1, 0x35, 0xA1, 0x1, 0x36, 0x00, 0x1, 0x37, 0xA0, 0x1, 0x38, 0x00, + 0x1, 0x39, 0x04, 0x1, 0x3A, 0x28, 0x1, 0x3B, 0x30, 0x1, 0x3C, 0x0C, + 0x1, 0x3D, 0x04, 0x1, 0x3E, 0x0F, 0x1, 0x3F, 0x79, 0x1, 0x40, 0x28, + 0x1, 0x41, 0x1E, 0x1, 0x42, 0x2F, 0x1, 0x43, 0x87, 0x1, 0x44, 0x00, + 0x1, 0x45, 0x0B, 0x1, 0x46, 0x00, 0x1, 0x47, 0x0B, 0x1, 0x48, 0x00, + 0x1, 0x49, 0xA7, 0x1, 0x4A, 0x00, 0x1, 0x4B, 0xA6, 0x1, 0x4C, 0x00, + 0x1, 0x4D, 0x04, 0x1, 0x4E, 0x01, 0x1, 0x4F, 0x00, 0x1, 0x50, 0x00, + 0x1, 0x51, 0x80, 0x1, 0x52, 0x09, 0x1, 0x53, 0x08, 0x1, 0x54, 0x01, + 0x1, 0x55, 0x00, 0x1, 0x56, 0x0F, 0x1, 0x57, 0x79, 0x1, 0x58, 0x09, + 0x1, 0x59, 0x05, 0x1, 0x5A, 0x00, 0x1, 0x5B, 0x60, 0x1, 0x5C, 0x05, + 0x1, 0x5D, 0xD1, 0x1, 0x5E, 0x0C, 0x1, 0x5F, 0x3C, 0x1, 0x60, 0x00, + 0x1, 0x61, 0xD0, 0x1, 0x62, 0x0B, 0x1, 0x63, 0x03, 0x1, 0x64, 0x28, + 0x1, 0x65, 0x10, 0x1, 0x66, 0x2A, 0x1, 0x67, 0x39, 0x1, 0x68, 0x0B, + 0x1, 0x69, 0x02, 0x1, 0x6A, 0x28, 0x1, 0x6B, 0x10, 0x1, 0x6C, 0x2A, + 0x1, 0x6D, 0x61, 0x1, 0x6E, 0x0C, 0x1, 0x6F, 0x00, 0x1, 0x70, 0x0F, + 0x1, 0x71, 0x79, 0x1, 0x72, 0x00, 0x1, 0x73, 0x0B, 0x1, 0x74, 0x00, + 0x1, 0x75, 0x0B, 0x1, 0x76, 0x00, 0x1, 0x77, 0xA1, 0x1, 0x78, 0x00, + 0x1, 0x79, 0xA0, 0x1, 0x7A, 0x00, 0x1, 0x7B, 0x04, 0x1, 0xFF, 0x04, + 0x1, 0x79, 0x1D, 0x1, 0x7B, 0x27, 0x1, 0x96, 0x0E, 0x1, 0x97, 0xFE, + 0x1, 0x98, 0x03, 0x1, 0x99, 0xEF, 0x1, 0x9A, 0x02, 0x1, 0x9B, 0x44, + 0x1, 0x73, 0x07, 0x1, 0x70, 0x01, 0x1, 0xff, 0x01, 0x1, 0x00, 0x01, + 0x1, 0xff, 0x00, 0x00, 0x00, 0x00}; + +#ifdef __cplusplus +} +#endif + +#endif /* _VL53L0X_INTERRUPT_THRESHOLD_SETTINGS_H_ */ diff --git a/test/fixtures/vl53_const/vl53l0x_tuning.h b/test/fixtures/vl53_const/vl53l0x_tuning.h new file mode 100644 index 000000000..94cbb7507 --- /dev/null +++ b/test/fixtures/vl53_const/vl53l0x_tuning.h @@ -0,0 +1,87 @@ +/******************************************************************************* +Copyright 2016, STMicroelectronics International N.V. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of STMicroelectronics nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND +NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED. +IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef _VL53L0X_TUNING_H_ +#define _VL53L0X_TUNING_H_ + +#include "vl53l0x_def.h" + +#ifdef __cplusplus +extern "C" { +#endif + +uint8_t DefaultTuningSettings[] = { + + /* update 02/11/2015_v36 */ + 0x01, 0xFF, 0x01, 0x01, 0x00, 0x00, + + 0x01, 0xFF, 0x00, 0x01, 0x09, 0x00, 0x01, 0x10, 0x00, 0x01, 0x11, 0x00, + + 0x01, 0x24, 0x01, 0x01, 0x25, 0xff, 0x01, 0x75, 0x00, + + 0x01, 0xFF, 0x01, 0x01, 0x4e, 0x2c, 0x01, 0x48, 0x00, 0x01, 0x30, 0x20, + + 0x01, 0xFF, 0x00, 0x01, 0x30, 0x09, /* mja changed from 0x64. */ + 0x01, 0x54, 0x00, 0x01, 0x31, 0x04, 0x01, 0x32, 0x03, 0x01, 0x40, 0x83, + 0x01, 0x46, 0x25, 0x01, 0x60, 0x00, 0x01, 0x27, 0x00, 0x01, 0x50, 0x06, + 0x01, 0x51, 0x00, 0x01, 0x52, 0x96, 0x01, 0x56, 0x08, 0x01, 0x57, 0x30, + 0x01, 0x61, 0x00, 0x01, 0x62, 0x00, 0x01, 0x64, 0x00, 0x01, 0x65, 0x00, + 0x01, 0x66, 0xa0, + + 0x01, 0xFF, 0x01, 0x01, 0x22, 0x32, 0x01, 0x47, 0x14, 0x01, 0x49, 0xff, + 0x01, 0x4a, 0x00, + + 0x01, 0xFF, 0x00, 0x01, 0x7a, 0x0a, 0x01, 0x7b, 0x00, 0x01, 0x78, 0x21, + + 0x01, 0xFF, 0x01, 0x01, 0x23, 0x34, 0x01, 0x42, 0x00, 0x01, 0x44, 0xff, + 0x01, 0x45, 0x26, 0x01, 0x46, 0x05, 0x01, 0x40, 0x40, 0x01, 0x0E, 0x06, + 0x01, 0x20, 0x1a, 0x01, 0x43, 0x40, + + 0x01, 0xFF, 0x00, 0x01, 0x34, 0x03, 0x01, 0x35, 0x44, + + 0x01, 0xFF, 0x01, 0x01, 0x31, 0x04, 0x01, 0x4b, 0x09, 0x01, 0x4c, 0x05, + 0x01, 0x4d, 0x04, + + 0x01, 0xFF, 0x00, 0x01, 0x44, 0x00, 0x01, 0x45, 0x20, 0x01, 0x47, 0x08, + 0x01, 0x48, 0x28, 0x01, 0x67, 0x00, 0x01, 0x70, 0x04, 0x01, 0x71, 0x01, + 0x01, 0x72, 0xfe, 0x01, 0x76, 0x00, 0x01, 0x77, 0x00, + + 0x01, 0xFF, 0x01, 0x01, 0x0d, 0x01, + + 0x01, 0xFF, 0x00, 0x01, 0x80, 0x01, 0x01, 0x01, 0xF8, + + 0x01, 0xFF, 0x01, 0x01, 0x8e, 0x01, 0x01, 0x00, 0x01, 0x01, 0xFF, 0x00, + 0x01, 0x80, 0x00, + + 0x00, 0x00, 0x00}; + +#ifdef __cplusplus +} +#endif + +#endif /* _VL53L0X_TUNING_H_ */ diff --git a/test/test_nrf52_flash_trim.py b/test/test_nrf52_flash_trim.py index 402f3e5dd..09e8ae260 100644 --- a/test/test_nrf52_flash_trim.py +++ b/test/test_nrf52_flash_trim.py @@ -134,6 +134,12 @@ class Nrf52FlashTrimTest(unittest.TestCase): def BuildSources(self, *args): self.setdefault("BUILT_SOURCES", []).append(args) + def subst(self, value): + return value.replace("$PROJECT_DIR", str(ROOT)) + + def AddBuildMiddleware(self, callback, pattern): + self.setdefault("MIDDLEWARE", []).append((callback, pattern)) + base = dict(BUILD_FLAGS=["-DUSE_CC310_HW_CRYPTO=1"], SRC_FILTER=["+<../examples/simple_repeater>"], CPPDEFINES=[]) excluded = BuildEnvironment(**base, PIOENV="RAK_4631_companion_radio_full") @@ -148,6 +154,7 @@ class Nrf52FlashTrimTest(unittest.TestCase): self.assertTrue(active["MESH_NRF52_FLASH_TRIM_ACTIVE"]) self.assertIn("-Wl,--wrap=CRYS_ECPKI_GetEcDomain", active["LINKFLAGS"]) self.assertEqual(active["BUILT_SOURCES"][0][-1], "+") + self.assertEqual(active["MIDDLEWARE"][0][1], "*Adafruit_VL53L0X*/*.cpp") @unittest.skipUnless(shutil.which("cc"), "a native C compiler is required") def test_wrapped_domain_preserves_p256_and_rejects_unused_curves(self): diff --git a/test/test_nrf52_vl53l0x_const.py b/test/test_nrf52_vl53l0x_const.py new file mode 100644 index 000000000..cacc1f095 --- /dev/null +++ b/test/test_nrf52_vl53l0x_const.py @@ -0,0 +1,190 @@ +"""Qualify flash-resident VL53L0X tables against the reviewed vendor loader.""" + +import contextlib +import importlib.util +import io +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest +from unittest.mock import patch + + +ROOT = Path(__file__).resolve().parents[1] +FIXTURE = ROOT / "test/fixtures/vl53_const" +SPEC = importlib.util.spec_from_file_location("vl53_const", ROOT / "scripts/nrf52_vl53l0x_const.py") +TRIM = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(TRIM) + + +class Node: + def __init__(self, path): + self.path = Path(path) + + def srcnode(self): + return self + + def get_abspath(self): + return str(self.path) + + +class Environment: + def __init__(self, build): + self.build = Path(build) + + def subst(self, value): + assert value == "$BUILD_DIR" + return str(self.build) + + def File(self, path): + return Path(path) + + +class Vl53ConstTests(unittest.TestCase): + def test_private_copy_is_complete_idempotent_and_downloads_are_unchanged(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + source = root / "libdeps/Adafruit_VL53L0X/src" + texts = {} + for name, (old, _) in TRIM.REPLACEMENTS.items(): + texts[name] = "// synthetic reviewed fixture\n" + old + "\n" + texts["vl53l0x_def.h"] = "// ordinary sibling header\n" + texts["core/src/vl53l0x_api_calibration.cpp"] = '#include "../../vl53l0x_api_core.h"\n' + for name, text in texts.items(): + path = source / name + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(text.replace("\n", "\r\n").encode()) + before = {name: (source / name).read_bytes() for name in texts} + node = Node(source / "core/src/vl53l0x_api_calibration.cpp") + env = Environment(root / "build") + hashes = {name: TRIM.digest(texts[name]) for name in TRIM.FILES} + with patch.object(TRIM, "FILES", hashes): + output = TRIM.replace_vl53_source(env, node) + repeated = TRIM.replace_vl53_source(env, node) + self.assertEqual(output, repeated) + copied = root / "build/readonly-nrf52-vl53l0x/src" + self.assertEqual(output, copied / "core/src/vl53l0x_api_calibration.cpp") + self.assertEqual({name: (source / name).read_bytes() for name in texts}, before) + for name, text in texts.items(): + if name in TRIM.REPLACEMENTS: + old, new = TRIM.REPLACEMENTS[name] + self.assertEqual((copied / name).read_text(), text.replace(old, new)) + else: + self.assertEqual((copied / name).read_bytes(), before[name]) + # One changed table, loader, or prototype keeps every original TU. + for changed in TRIM.FILES: + path = source / changed + saved = path.read_bytes() + path.write_bytes(saved + b"// custom revision\n") + with patch.object(TRIM, "FILES", hashes), contextlib.redirect_stdout(io.StringIO()): + self.assertIs(TRIM.replace_vl53_source(env, node), node) + path.write_bytes(saved) + + def test_real_vendor_tables_produce_identical_register_writes_and_parameters(self): + original = (FIXTURE / "tuning_loader.inc").read_text() + mutable = original.replace("VL53L0X_load_tuning_settings", "load_original") + readonly = original.replace("VL53L0X_load_tuning_settings", "load_readonly").replace( + *TRIM.REPLACEMENTS["core/src/vl53l0x_api_core.cpp"]) + tables = "\n".join((FIXTURE / name).read_text().replace(*TRIM.REPLACEMENTS[name]) + .replace('#include "vl53l0x_def.h"', '') + for name in ("vl53l0x_tuning.h", "vl53l0x_interrupt_threshold_settings.h")) + source = r''' +#include +#include +#include +#include +#include +struct Device { + unsigned SigmaEstRefArray=0, SigmaEstEffPulseWidth=0, SigmaEstEffAmbWidth=0, targetRefRate=0; + unsigned calls=0, fail=0; + std::vector writes; +}; +using VL53L0X_Error = int; +using VL53L0X_DEV = Device*; +#define VL53L0X_ERROR_NONE 0 +#define VL53L0X_ERROR_INVALID_PARAMS 1 +#define LOG_FUNCTION_START(...) +#define LOG_FUNCTION_END(...) +#define PALDevDataSet(device, member, value) ((device)->member = (value)) +#define VL53L0X_MAKEUINT16(low, high) (uint16_t(low) | (uint16_t(high) << 8)) +int VL53L0X_WriteMulti(Device* device, uint8_t address, uint8_t* bytes, unsigned count) { + device->writes.push_back(address); device->writes.push_back(uint8_t(count)); + device->writes.insert(device->writes.end(),bytes,bytes+count); + return ++device->calls == device->fail ? 2 : 0; +} +@TABLES@ +@MUTABLE@ +@READONLY@ +static_assert(std::is_const::type>::value, + "default tuning must live in readonly storage"); +int main() { + const struct { const uint8_t* bytes; unsigned size; } inputs[] = { + {DefaultTuningSettings,sizeof(DefaultTuningSettings)}, + {InterruptThresholdSettings,sizeof(InterruptThresholdSettings)}, + }; + assert(inputs[0].size == 243 && inputs[1].size == 438); + for (const auto& input:inputs) { + for(unsigned fail=0;fail copy(input.bytes,input.bytes+input.size); + Device before, after; before.fail=after.fail=fail; + assert(load_original(&before,copy.data()) == load_readonly(&after,input.bytes)); + assert(before.calls==after.calls && before.writes==after.writes); + assert(before.SigmaEstRefArray==after.SigmaEstRefArray); + assert(before.SigmaEstEffPulseWidth==after.SigmaEstEffPulseWidth); + assert(before.SigmaEstEffAmbWidth==after.SigmaEstEffAmbWidth); + assert(before.targetRefRate==after.targetRefRate); + assert(memcmp(copy.data(),input.bytes,input.size)==0); + } + } + // The caller-owned custom tuning API keeps accepting ordinary mutable data. + uint8_t custom[] = {0xFF,0,0x01,0x23,2,0x10,0xAA,0xBB,0}; + uint8_t snapshot[sizeof(custom)]; memcpy(snapshot,custom,sizeof(custom)); + Device before,after; + assert(load_original(&before,custom)==load_readonly(&after,custom)); + assert(before.SigmaEstRefArray==after.SigmaEstRefArray && before.writes==after.writes); + assert(memcmp(custom,snapshot,sizeof(custom))==0); +} +'''.replace("@TABLES@", tables).replace("@MUTABLE@", mutable).replace("@READONLY@", readonly) + with tempfile.TemporaryDirectory() as directory: + path, binary = Path(directory) / "loader.cpp", Path(directory) / "loader" + path.write_text(source, encoding="ascii") + built = subprocess.run(["g++", "-std=c++17", "-O1", "-Wall", "-Wextra", "-Werror", + "-fsanitize=address,undefined", "-fno-pie", "-no-pie", + str(path), "-o", str(binary)], capture_output=True, text=True, timeout=30) + self.assertEqual(built.returncode, 0, built.stdout + built.stderr) + subprocess.run([str(binary)], check=True, timeout=10) + + def test_arm_table_bytes_move_to_readonly_without_changing_their_size(self): + compiler = shutil.which("arm-none-eabi-g++") + nm = shutil.which("arm-none-eabi-nm") + if not compiler or not nm: + self.skipTest("ARM toolchain required") + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + for readonly in (False, True): + text = "#include \n" + for name in ("vl53l0x_tuning.h", "vl53l0x_interrupt_threshold_settings.h"): + table = (FIXTURE / name).read_text().replace('#include "vl53l0x_def.h"', '') + if readonly: + table = table.replace(*TRIM.REPLACEMENTS[name]) + text += table + "\n" + text += ''' +extern "C" const uint8_t* default_table() { return DefaultTuningSettings; } +extern "C" const uint8_t* threshold_table() { return InterruptThresholdSettings; } +''' + path, obj = root / "tables.cpp", root / "tables.o" + path.write_text(text, encoding="ascii") + built = subprocess.run([compiler,"-std=c++17","-mcpu=cortex-m4","-mthumb","-Os", + "-c",str(path),"-o",str(obj)],capture_output=True,text=True,timeout=30) + self.assertEqual(built.returncode,0,built.stdout+built.stderr) + symbols = subprocess.check_output([nm,"-S","-C",str(obj)],text=True) + for name,size in (("DefaultTuningSettings",243),("InterruptThresholdSettings",438)): + line = next(line for line in symbols.splitlines() if line.endswith(" "+name)) + _, length, kind, _ = line.split() + self.assertEqual(int(length,16),size) + self.assertEqual(kind.lower(),"r" if readonly else "d") + + +if __name__ == "__main__": + unittest.main() diff --git a/test/test_ota/test_ota_core.cpp b/test/test_ota/test_ota_core.cpp index 390a4b657..1d7cb28ed 100644 --- a/test/test_ota/test_ota_core.cpp +++ b/test/test_ota/test_ota_core.cpp @@ -4319,6 +4319,49 @@ TEST(OtaTransfer, RejectsShortNonFinalManifestFragment) { EXPECT_EQ(client.fetchState(), OtaManager::FETCHING); } +TEST(OtaTransfer, FixedManifestStoragePreservesEveryValidShapeAndRejectsOtherShapes) { + MotaManifest manifest; + ASSERT_TRUE(mota_parse(SIM_MOTA, SIM_MOTA_LEN, manifest)); + const uint8_t totals[] = {0,1,2,3,4,5,16,255}; + const uint8_t indexes[] = {0,1,2,3,4,15,254,255}; + for (uint8_t total : totals) for (uint8_t index : indexes) { + for (unsigned length = 0; length <= MAX_PACKET_PAYLOAD - 7; ++length) { + SCOPED_TRACE(::testing::Message() << "total=" << unsigned(total) + << " index=" << unsigned(index) << " length=" << length); + g_q.clear(); + OtaManager client; + OtaStoreRam<4096> store; + SendTo to_none{&client}; + client.begin(SIM_TARGET_ID, sim_send, &to_none); + client.set_fetch_store(&store); + ASSERT_EQ(client.pull(manifest.merkle_root, manifest.target_id), OtaManager::PULL_STARTED); + g_q.clear(); + const uint16_t final_length = MOTA_MFL - OTA_MF_FRAG; + const uint8_t other = index == 0 ? 1 : 0; + deliver_manifest_fragment(client, manifest.merkle_root, other, + manifest.manifest_start + (other ? OTA_MF_FRAG : 0), + other ? final_length : OTA_MF_FRAG); + ASSERT_EQ(client.fetchState(), OtaManager::WANT_MANIFEST); + uint8_t bytes[MAX_PACKET_PAYLOAD] = {}; + if (index == 0) memcpy(bytes, manifest.manifest_start, OTA_MF_FRAG); + else if (index == 1) memcpy(bytes, manifest.manifest_start + OTA_MF_FRAG, final_length); + ManifestMsg fragment{}; + memcpy(fragment.manifest_id, manifest.merkle_root, 4); + fragment.frag_idx = index; fragment.frag_total = total; + fragment.bytes = bytes; fragment.len = uint16_t(length); + uint8_t wire[MAX_PACKET_PAYLOAD]; + const auto wire_length = encode_manifest(wire, sizeof(wire), fragment); + ASSERT_GT(wire_length, 0); + client.on_message(wire, wire_length); + const bool valid = total == 2 && ((index == 0 && length == OTA_MF_FRAG) + || (index == 1 && length == final_length)); + EXPECT_EQ(client.fetchState(), valid ? OtaManager::FETCHING : OtaManager::WANT_MANIFEST); + if (!valid) EXPECT_EQ(store.staged_size(), 0u); + } + } + g_q.clear(); +} + TEST(OtaTransfer, RejectsManifestBlockExponentBeforeShift) { g_q.clear(); MotaManifest manifest; diff --git a/test/test_ota_memory_layout.py b/test/test_ota_memory_layout.py index b05c12895..f0643d3b3 100644 --- a/test/test_ota_memory_layout.py +++ b/test/test_ota_memory_layout.py @@ -21,25 +21,55 @@ class OtaMemoryLayoutTest(unittest.TestCase): #include #include using namespace mesh::ota; -static_assert(sizeof(OtaManager::ServeEntry) == (sizeof(void*) == 4 ? 60u : 64u), - "source entries must retain every field without old padding"); +static_assert(sizeof(OtaManager::ServeEntry) == (sizeof(void*) == 4 ? 32u : 40u), + "source entries must retain all independent descriptor fields once"); static_assert(alignof(OtaManager::ServeEntry) >= alignof(MotaSource*), "unaligned source pointer"); -static_assert(offsetof(OtaManager::ServeEntry, desc) % alignof(MotaDesc) == 0, "unaligned descriptor"); static_assert(offsetof(OtaManager::ServeEntry, target_id) % alignof(uint32_t) == 0, "unaligned word"); static_assert(sizeof(OtaStoreQspiNrf52) > 8192u, "both staging page buffers must remain"); int main() { OtaManager::ServeEntry row; - assert(row.desc.target_id == 0 && row.desc.fw_version == 0 && row.desc.source_caps == 0); - assert(row.desc.mid[0] == 0 && row.desc.mid[3] == 0 && row.desc.total_size == 0); + const auto empty = row.descriptor(); + assert(empty.target_id == 0 && empty.fw_version == 0 && empty.source_caps == 0); + assert(empty.mid[0] == 0 && empty.mid[3] == 0 && empty.total_size == 0); row.mid[0] = 17; row.target_id = 123; row.fw_version = 456; row.have_count = 789; row.src = nullptr; row.src_idx = 255; row.codec_id = 2; row.flags = 3; row.is_self = true; - row.desc.target_id = 987; row.desc.payload_size = 654; + row.payload_size = 654; row.block_size_log2 = 11; row.source_caps = 3; const OtaManager::ServeEntry copy = row; assert(copy.mid[0] == 17 && copy.target_id == 123 && copy.fw_version == 456); assert(copy.have_count == 789 && copy.src == nullptr && copy.src_idx == 255); assert(copy.codec_id == 2 && copy.flags == 3 && copy.is_self); - assert(copy.desc.target_id == 987 && copy.desc.payload_size == 654); + const auto descriptor = copy.descriptor(); + assert(descriptor.target_id == 123 && descriptor.payload_size == 654); + assert(descriptor.block_count == 789 && descriptor.block_size_log2 == 11 && descriptor.source_caps == 3); + assert(descriptor.leaves_off == 8 + MOTA_MFL); + assert(descriptor.payload_off == descriptor.leaves_off + 789 * 4); + assert(descriptor.total_size == descriptor.payload_off + 654 + 5); + // Every descriptor admitted by refresh_sources() has this canonical layout. + // Keep arbitrary metadata/caps/large payload values, not just current images. + uint32_t random = 0x6d76a345; + for (unsigned trial = 0; trial != 10000; ++trial) { + auto next = [&]() { random ^= random << 13; random ^= random >> 17; random ^= random << 5; return random; }; + MotaDesc original; + for (auto& byte : original.mid) byte = uint8_t(next()); + original.target_id = next(); original.fw_version = next(); + original.codec_id = uint8_t(next()); original.flags = uint8_t(next()); + original.block_size_log2 = uint8_t(next()); original.source_caps = uint8_t(next()); + original.block_count = 1 + next() % (UINT32_MAX / 8); + original.leaves_off = 8 + MOTA_MFL; + original.payload_off = original.leaves_off + original.block_count * 4; + original.payload_size = next() % (UINT32_MAX - original.payload_off - 5); + original.total_size = original.payload_off + original.payload_size + 5; + row.cacheDescriptor(original); + const auto roundtrip = row.descriptor(); + assert(memcmp(original.mid, roundtrip.mid, 4) == 0); + assert(original.target_id == roundtrip.target_id && original.fw_version == roundtrip.fw_version); + assert(original.codec_id == roundtrip.codec_id && original.flags == roundtrip.flags); + assert(original.block_size_log2 == roundtrip.block_size_log2 && original.source_caps == roundtrip.source_caps); + assert(original.block_count == roundtrip.block_count && original.payload_size == roundtrip.payload_size); + assert(original.leaves_off == roundtrip.leaves_off && original.payload_off == roundtrip.payload_off); + assert(original.total_size == roundtrip.total_size); + } } ''' with tempfile.TemporaryDirectory(prefix="ota-layout-") as directory: