Consolidate companion components

This commit is contained in:
mikecarper committed 2026-09-17 13:41:39 -07:00
1 parent aab8f5038a
commit ebee99af81
32 files changed
+286 -287

No files matched your search

@@ -1,5 +1,5 @@
#pragma once
#include "JohnLookup.h"
#include "ReaderLookup.h"
namespace mesh { namespace bible {
@@ -92,7 +92,7 @@ inline uint32_t bookmarkChecksum(const uint8_t* data) {
return hash;
}
inline void encodeBookmark(Position pos, uint8_t (&out)[kBookmarkBytes]) {
out[0] = 'J'; out[1] = 'N'; out[2] = 1; out[3] = 0;
out[0] = 'R'; out[1] = 'D'; out[2] = 1; out[3] = 0;
out[4] = pos.verse; out[5] = pos.verse >> 8;
out[6] = pos.offset; out[7] = pos.offset >> 8;
const uint32_t hash = bookmarkChecksum(out);
@@ -100,7 +100,7 @@ inline void encodeBookmark(Position pos, uint8_t (&out)[kBookmarkBytes]) {
}
inline bool decodeBookmark(const uint8_t (&in)[kBookmarkBytes], Position& pos) {
pos = Position{};
if (in[0] != 'J' || in[1] != 'N' || in[2] != 1 || in[3] != 0) return false;
if (in[0] != 'R' || in[1] != 'D' || in[2] != 1 || in[3] != 0) return false;
const uint32_t hash = bookmarkChecksum(in);
for (unsigned i = 0; i < 4; ++i)
if (in[8 + i] != static_cast<uint8_t>(hash >> (8 * i))) return false;
@@ -112,7 +112,7 @@ inline bool decodeBookmark(const uint8_t (&in)[kBookmarkBytes], Position& pos) {
return true;
}
// Debounced checkpointing: no writes just for opening John 1:1, no rewrite
// Debounced checkpointing: no writes just for opening reader 1:1, no rewrite
// of unchanged positions, and a failed save remains pending for retry.
class ReaderBookmark {
Position _position, _saved;
@@ -1,5 +1,5 @@
#pragma once
#include "JohnReader.h"
#include "Reader.h"
namespace mesh { namespace bible {
// A verified replacement plus one previous record also works on SPIFFS,
@@ -9,8 +9,8 @@ template <typename Files>
bool loadReaderBookmark(Files& files, Position& pos) {
uint8_t data[kBookmarkBytes];
pos = Position{};
if (files.read("/john.pos", data) && decodeBookmark(data, pos)) return true;
return files.read("/john.pos.bak", data) && decodeBookmark(data, pos);
if (files.read("/reader.pos", data) && decodeBookmark(data, pos)) return true;
return files.read("/reader.pos.bak", data) && decodeBookmark(data, pos);
}
template <typename Files>
@@ -19,26 +19,26 @@ bool saveReaderBookmark(Files& files, Position pos) {
if (pos.atStart()) {
// Remove the fallback before the live record so a completed clear cannot
// resurrect an earlier place on reboot. Do not create a start marker.
return files.remove("/john.pos.tmp") && files.remove("/john.pos.bak")
&& files.remove("/john.pos");
return files.remove("/reader.pos.tmp") && files.remove("/reader.pos.bak")
&& files.remove("/reader.pos");
}
uint8_t data[kBookmarkBytes], verify[kBookmarkBytes];
encodeBookmark(pos, data);
if (!files.remove("/john.pos.tmp") || !files.write("/john.pos.tmp", data)) return false;
if (!files.read("/john.pos.tmp", verify) || memcmp(data, verify, sizeof(data)) != 0)
if (!files.remove("/reader.pos.tmp") || !files.write("/reader.pos.tmp", data)) return false;
if (!files.read("/reader.pos.tmp", verify) || memcmp(data, verify, sizeof(data)) != 0)
return false;
if (files.exists("/john.pos")) {
if (files.exists("/reader.pos")) {
// Keep a valid fallback if the live record was interrupted/corrupt.
Position old;
// A failed read is not proof of corruption. Preserve the live record on
// transient I/O failure rather than deleting it before publication.
if (!files.read("/john.pos", verify)) return false;
if (!files.read("/reader.pos", verify)) return false;
if (decodeBookmark(verify, old)) {
if (!files.remove("/john.pos.bak") || !files.rename("/john.pos", "/john.pos.bak"))
if (!files.remove("/reader.pos.bak") || !files.rename("/reader.pos", "/reader.pos.bak"))
return false;
} else if (!files.remove("/john.pos")) return false;
} else if (!files.remove("/reader.pos")) return false;
}
if (!files.rename("/john.pos.tmp", "/john.pos")) return false;
if (!files.rename("/reader.pos.tmp", "/reader.pos")) return false;
// Retain the last valid fallback until the next successful replacement.
return true;
}
@@ -1,13 +1,13 @@
// Generated by tools/bible/pack_john.py; do not edit.
// World English Bible (engwebp), John. Public domain; see tools/bible/README.md.
// Generated by tools/bible/pack_reader.py; do not edit.
// Offline Reader corpus. Public domain; see tools/bible/README.md.
// Firmware typography: ASCII quotes, apostrophes, dashes and spaces; wording unchanged.
// Canonical source SHA-256: 916bf9d1feae0ad410d69f61e6c5f328ac6acba10651d751b5bb29719732c49e
#pragma once
#include "JohnLookup.h"
#include "ReaderLookup.h"
namespace mesh { namespace bible { namespace generated {
static_assert(kBlockSize == 2048, "Regenerate John data after changing block size");
static_assert(sizeof(BlockIndex) == 12, "John flash-size accounting expects a 12-byte index");
static const uint8_t johnData[] = {
static_assert(kBlockSize == 2048, "Regenerate Reader data after changing block size");
static_assert(sizeof(BlockIndex) == 12, "Reader flash-size accounting expects a 12-byte index");
static const uint8_t readerData[] = {
0x8d,0x54,0xc1,0x6e,0xdb,0x30,0x0c,0xcd,0xa7,0x10,0xbe,0xf4,0x12,0xf8,0x03,0x76,0x29,0xb6,0x61,0xed,
0x5a,0x6c,0xbb,0xac,0x40,0xcf,0x72,0x4c,0x5b,0x5a,0x64,0x29,0x90,0xec,0x06,0xd9,0xd7,0xef,0x91,0x92,
0xdd,0xb4,0xdb,0x61,0x97,0xc4,0xa2,0x28,0xf2,0xf1,0xf1,0x91,0x0f,0x81,0x66,0xcb,0xd4,0xf1,0xe8,0x42,
@@ -2175,7 +2175,7 @@ static const uint8_t johnData[] = {
0xc6,0x51,0x3c,0x7b,0xbd,0x92,0xc0,0xdb,0xfa,0x70,0xd9,0x94,0x44,0xf6,0xab,0x34,0x0b,0xc1,0x09,0x7c,
0x7b,0xf3,0x17,
};
static const BlockIndex johnBlocks[] = {
static const BlockIndex readerBlocks[] = {
{0, 841, 1930, 0},
{841, 910, 1946, 22},
{1751, 884, 1881, 41},
@@ -2226,10 +2226,10 @@ static const BlockIndex johnBlocks[] = {
{41668, 923, 2027, 854},
{42591, 712, 1669, 869},
};
static const Corpus johnCorpus = {
static const Corpus readerCorpus = {
"WEB",
"World English Bible - public domain (eBible.org).",
johnData, sizeof(johnData), johnBlocks,
sizeof(johnBlocks) / sizeof(johnBlocks[0]), kVerseCount
readerData, sizeof(readerData), readerBlocks,
sizeof(readerBlocks) / sizeof(readerBlocks[0]), kVerseCount
};
}}}
@@ -71,7 +71,7 @@ inline ParseResult parse(const char* command, Reference& ref) {
skipSpace(command);
if (!token(command, "get")) return ParseResult::NoMatch;
skipSpace(command);
if (!token(command, "john")) return ParseResult::NoMatch;
if (!token(command, "reader")) return ParseResult::NoMatch;
skipSpace(command);
if (!number(command, ref.chapter) || *command != ':') return ParseResult::Invalid;
++command;