SDK for TESAIoT Dev Kit
API reference & tutorials (ModusToolbox)
Loading...
Searching...
No Matches
The two implementations, one on-disk contract

The same six names exist on mtb-mpy, where they come from lfs_wifi_creds.c inside libbento_mpy.a — an archive, not shipped as source, whose file I/O runs by executing Python open() through the MicroPython VFS. That is why the mtb-mpy version needs a VM and cannot exist on this variant. The shipped C store reaches the same file through bento_storage instead, and is byte-compatible on purpose.

mtb-mpy (archive, not readable here) mtb-only (lfs_wifi_creds_c.c, shipped)
File I/O Python open() through the VFS bridge bento_storage_read_file() / bento_storage_write_file() over plain lfs2
Calling context MicroPython task only, after the VFS mount any task once bento_storage_init() has mounted
lfs_wifi_creds_init() real bring-up; gc_collect() first (~16 KB GC heap) no-op — bento_storage_init() owns bring-up
lfs_wifi_creds_deinit() invalidates Python handles before soft reset no-op
lfs_wifi_creds_ready() store initialised and VFS mounted forwards to bento_storage_ready()
lfs_wifi_creds_needs_resave() true after a legacy XOR-32 file was read always false — writes only the current format
Write path whole write inside wifi_creds_lock(), deferred to a REPL-idle flusher snapshot under the lock, write outside it, immediately in the WiFi worker

Shared contract — the byte-compatible on-disk format (a board moved between variants keeps its saved networks):

Offset Size Field
0 4 magic 0x57494649 ("WIFI", QSPI_WIFI_CREDS_MAGIC)
4 2 version 1 (QSPI_WIFI_CREDS_VERSION)
6 2 count, 0 to 6
8 600 entries, 6 × sizeof(qspi_wifi_entry_t) = 100
608 4 CRC32 (poly 0xEDB88320) of bytes 0 to 607

Both readers accept the legacy XOR-32 checksum on read so a pre-migration file still boots with its networks; both writers emit CRC32 only. The shipped implementation, verbatim — it is short enough to be its own specification:

void lfs_wifi_creds_init(void) { /* bento_storage_init() owns bring-up */ }
void lfs_wifi_creds_deinit(void) { }
bool lfs_wifi_creds_ready(void) { return bento_storage_ready(); }
/* The mtb-mpy store can inherit a pre-LFS sector image that wants rewriting;
* this store writes only the current format, so never. */
bool lfs_wifi_creds_needs_resave(void) { return false; }
int lfs_wifi_creds_read(qspi_wifi_entry_t *entries, int max_entries) {
if (entries == NULL || max_entries <= 0) return 0;
if (!bento_storage_ready()) return 0;
int n = bento_storage_read_file(CREDS_FILE, s_buf, sizeof(s_buf));
if (n < CREDS_TOTAL_SIZE) return 0;
uint32_t magic; memcpy(&magic, &s_buf[0], 4);
uint16_t version; memcpy(&version, &s_buf[4], 2);
uint16_t count; memcpy(&count, &s_buf[6], 2);
if (magic != QSPI_WIFI_CREDS_MAGIC) return 0;
if (version != QSPI_WIFI_CREDS_VERSION) return 0;
if (count > QSPI_WIFI_CREDS_MAX) return 0;
uint32_t stored; memcpy(&stored, &s_buf[CREDS_HEADER_SIZE + CREDS_ENTRIES_SIZE], 4);
if (stored != calc_crc32(s_buf, CREDS_HEADER_SIZE + CREDS_ENTRIES_SIZE)) {
/* Legacy XOR-32 fallback, exactly as the mtb-mpy reader does. */
if (stored != calc_xor32(s_buf, CREDS_HEADER_SIZE + CREDS_ENTRIES_SIZE)) return 0;
}
int out = (count < (uint16_t)max_entries) ? count : max_entries;
memcpy(entries, &s_buf[CREDS_HEADER_SIZE], (size_t)out * sizeof(qspi_wifi_entry_t));
/* Defensive termination, same as the mtb-mpy reader. */
for (int i = 0; i < out; i++) {
entries[i].ssid[32] = '\0';
entries[i].password[64] = '\0';
}
return out;
}
bool lfs_wifi_creds_write(const qspi_wifi_entry_t *entries, int count) {
if (count < 0 || count > QSPI_WIFI_CREDS_MAX) return false;
if (count > 0 && entries == NULL) return false;
if (!bento_storage_ready()) return false;
memset(s_buf, 0, sizeof(s_buf));
uint32_t magic = QSPI_WIFI_CREDS_MAGIC;
uint16_t version = QSPI_WIFI_CREDS_VERSION;
uint16_t cnt = (uint16_t)count;
memcpy(&s_buf[0], &magic, 4);
memcpy(&s_buf[4], &version, 2);
memcpy(&s_buf[6], &cnt, 2);
if (count > 0) {
memcpy(&s_buf[CREDS_HEADER_SIZE], entries,
(size_t)count * sizeof(qspi_wifi_entry_t));
}
uint32_t crc = calc_crc32(s_buf, CREDS_HEADER_SIZE + CREDS_ENTRIES_SIZE);
memcpy(&s_buf[CREDS_HEADER_SIZE + CREDS_ENTRIES_SIZE], &crc, 4);
return bento_storage_write_file(CREDS_FILE, s_buf, sizeof(s_buf));
}

Locking. wifi_creds_lock() / wifi_creds_unlock() protect the shared g_boot_wifi_creds globals, not the file. wifi_init.c also read-modify-writes /.wifi_creds directly without touching the globals; the only lock over the file itself is the bento_storage layer's own.