## Summary The GitHub recent release API returns lots of data, including the full release notes. For 1.2.0, that data is 30,530 bytes. The existing OtaUpdater HTTP handler and single-buffer JSON parsing can't reliably handle that much data in the constrained ESP32 environment. This change does a few things: 1. Adds a very simple lib/JsonParser/StreamingJsonParser.cpp with SAX-style callbacks to read JSON data incrementally. 2. Adds a very simple lib/JsonParser/ReleaseJsonParser.cpp building on StreamingJsonParser, which specifically parses the GitHub release JSON for the release version, URL, and size. 3. Updates OtaUpdater.cpp to use an instance of ReleaseJsonParser to incrementally parse the large response it may receive from GitHub. Building from this commit while overriding my local version to 1.1.9, I was able to run the OTA update process ~5 times in a row successfully. Fixes #1561 (second part, after #1805). --- ### AI Usage While CrossPoint doesn't have restrictions on AI tools in contributing, please be transparent about their usage as it helps set the right context for reviewers. Did you use AI tools to help write this code? _**YES**_
209 lines
6.1 KiB
C++
209 lines
6.1 KiB
C++
#include "ReleaseJsonParser.h"
|
|
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
|
|
namespace {
|
|
|
|
void safeCopy(char* dst, size_t dstSize, const char* src, size_t srcLen) {
|
|
size_t n = srcLen < dstSize - 1 ? srcLen : dstSize - 1;
|
|
memcpy(dst, src, n);
|
|
dst[n] = '\0';
|
|
}
|
|
|
|
} // namespace
|
|
|
|
ReleaseJsonParser::ReleaseJsonParser()
|
|
: parser(JsonCallbacks{this, sOnKey, sOnString, sOnNumber, sOnBool, sOnNull, sOnObjectStart, sOnObjectEnd,
|
|
sOnArrayStart, sOnArrayEnd}) {
|
|
reset();
|
|
}
|
|
|
|
void ReleaseJsonParser::reset() {
|
|
parser.reset();
|
|
position = Position::TOP_LEVEL;
|
|
lastKey = LastKey::NONE;
|
|
depth = 0;
|
|
assetDepth = 0;
|
|
tagName[0] = '\0';
|
|
firmwareUrl[0] = '\0';
|
|
firmwareSize = 0;
|
|
tagFound = false;
|
|
firmwareFound = false;
|
|
currentAssetName[0] = '\0';
|
|
currentAssetUrl[0] = '\0';
|
|
currentAssetSize = 0;
|
|
}
|
|
|
|
void ReleaseJsonParser::feed(const char* data, size_t len) { parser.feed(data, len); }
|
|
|
|
bool ReleaseJsonParser::foundTag() const { return tagFound; }
|
|
bool ReleaseJsonParser::foundFirmware() const { return firmwareFound; }
|
|
const char* ReleaseJsonParser::getTagName() const { return tagName; }
|
|
const char* ReleaseJsonParser::getFirmwareUrl() const { return firmwareUrl; }
|
|
size_t ReleaseJsonParser::getFirmwareSize() const { return firmwareSize; }
|
|
|
|
void ReleaseJsonParser::commitAsset() {
|
|
if (strcmp(currentAssetName, "firmware.bin") == 0) {
|
|
memcpy(firmwareUrl, currentAssetUrl, sizeof(firmwareUrl));
|
|
firmwareSize = currentAssetSize;
|
|
firmwareFound = true;
|
|
}
|
|
currentAssetName[0] = '\0';
|
|
currentAssetUrl[0] = '\0';
|
|
currentAssetSize = 0;
|
|
}
|
|
|
|
// -- SAX callbacks (static trampolines) -------------------------------------
|
|
|
|
void ReleaseJsonParser::sOnKey(void* ctx, const char* key, size_t len) {
|
|
auto* self = static_cast<ReleaseJsonParser*>(ctx);
|
|
|
|
switch (self->position) {
|
|
case Position::TOP_LEVEL:
|
|
if (self->depth == 1) {
|
|
if (len == 8 && memcmp(key, "tag_name", 8) == 0)
|
|
self->lastKey = LastKey::TAG_NAME;
|
|
else if (len == 6 && memcmp(key, "assets", 6) == 0)
|
|
self->lastKey = LastKey::ASSETS;
|
|
else
|
|
self->lastKey = LastKey::NONE;
|
|
}
|
|
break;
|
|
case Position::IN_ASSET_OBJECT:
|
|
if (self->assetDepth == 1) {
|
|
if (len == 4 && memcmp(key, "name", 4) == 0)
|
|
self->lastKey = LastKey::ASSET_NAME;
|
|
else if (len == 20 && memcmp(key, "browser_download_url", 20) == 0)
|
|
self->lastKey = LastKey::ASSET_URL;
|
|
else if (len == 4 && memcmp(key, "size", 4) == 0)
|
|
self->lastKey = LastKey::ASSET_SIZE;
|
|
else
|
|
self->lastKey = LastKey::NONE;
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void ReleaseJsonParser::sOnString(void* ctx, const char* value, size_t len) {
|
|
auto* self = static_cast<ReleaseJsonParser*>(ctx);
|
|
|
|
switch (self->lastKey) {
|
|
case LastKey::TAG_NAME:
|
|
if (self->position == Position::TOP_LEVEL && self->depth == 1) {
|
|
safeCopy(self->tagName, sizeof(self->tagName), value, len);
|
|
self->tagFound = true;
|
|
}
|
|
break;
|
|
case LastKey::ASSET_NAME:
|
|
if (self->position == Position::IN_ASSET_OBJECT && self->assetDepth == 1)
|
|
safeCopy(self->currentAssetName, sizeof(self->currentAssetName), value, len);
|
|
break;
|
|
case LastKey::ASSET_URL:
|
|
if (self->position == Position::IN_ASSET_OBJECT && self->assetDepth == 1)
|
|
safeCopy(self->currentAssetUrl, sizeof(self->currentAssetUrl), value, len);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
self->lastKey = LastKey::NONE;
|
|
}
|
|
|
|
void ReleaseJsonParser::sOnNumber(void* ctx, const char* value, size_t /*len*/) {
|
|
auto* self = static_cast<ReleaseJsonParser*>(ctx);
|
|
|
|
if (self->lastKey == LastKey::ASSET_SIZE && self->position == Position::IN_ASSET_OBJECT && self->assetDepth == 1) {
|
|
self->currentAssetSize = static_cast<size_t>(strtoul(value, nullptr, 10));
|
|
}
|
|
self->lastKey = LastKey::NONE;
|
|
}
|
|
|
|
void ReleaseJsonParser::sOnBool(void* ctx, bool /*value*/) {
|
|
static_cast<ReleaseJsonParser*>(ctx)->lastKey = LastKey::NONE;
|
|
}
|
|
|
|
void ReleaseJsonParser::sOnNull(void* ctx) { static_cast<ReleaseJsonParser*>(ctx)->lastKey = LastKey::NONE; }
|
|
|
|
void ReleaseJsonParser::sOnObjectStart(void* ctx) {
|
|
auto* self = static_cast<ReleaseJsonParser*>(ctx);
|
|
|
|
switch (self->position) {
|
|
case Position::TOP_LEVEL:
|
|
self->depth++;
|
|
self->lastKey = LastKey::NONE;
|
|
break;
|
|
case Position::IN_ASSETS_ARRAY:
|
|
self->position = Position::IN_ASSET_OBJECT;
|
|
self->assetDepth = 1;
|
|
self->currentAssetName[0] = '\0';
|
|
self->currentAssetUrl[0] = '\0';
|
|
self->currentAssetSize = 0;
|
|
self->lastKey = LastKey::NONE;
|
|
break;
|
|
case Position::IN_ASSET_OBJECT:
|
|
self->assetDepth++;
|
|
self->lastKey = LastKey::NONE;
|
|
break;
|
|
}
|
|
}
|
|
|
|
void ReleaseJsonParser::sOnObjectEnd(void* ctx) {
|
|
auto* self = static_cast<ReleaseJsonParser*>(ctx);
|
|
|
|
switch (self->position) {
|
|
case Position::TOP_LEVEL:
|
|
if (self->depth > 0) self->depth--;
|
|
break;
|
|
case Position::IN_ASSET_OBJECT:
|
|
self->assetDepth--;
|
|
if (self->assetDepth == 0) {
|
|
self->commitAsset();
|
|
self->position = Position::IN_ASSETS_ARRAY;
|
|
}
|
|
self->lastKey = LastKey::NONE;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void ReleaseJsonParser::sOnArrayStart(void* ctx) {
|
|
auto* self = static_cast<ReleaseJsonParser*>(ctx);
|
|
|
|
switch (self->position) {
|
|
case Position::TOP_LEVEL:
|
|
if (self->lastKey == LastKey::ASSETS && self->depth == 1) {
|
|
self->position = Position::IN_ASSETS_ARRAY;
|
|
} else {
|
|
self->depth++;
|
|
}
|
|
self->lastKey = LastKey::NONE;
|
|
break;
|
|
case Position::IN_ASSET_OBJECT:
|
|
self->assetDepth++;
|
|
self->lastKey = LastKey::NONE;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void ReleaseJsonParser::sOnArrayEnd(void* ctx) {
|
|
auto* self = static_cast<ReleaseJsonParser*>(ctx);
|
|
|
|
switch (self->position) {
|
|
case Position::TOP_LEVEL:
|
|
if (self->depth > 0) self->depth--;
|
|
break;
|
|
case Position::IN_ASSETS_ARRAY:
|
|
self->position = Position::TOP_LEVEL;
|
|
break;
|
|
case Position::IN_ASSET_OBJECT:
|
|
self->assetDepth--;
|
|
self->lastKey = LastKey::NONE;
|
|
break;
|
|
}
|
|
}
|