Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions include/graphics/map/URLService.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <functional>

#ifdef ARDUINO_ARCH_ESP32
#include "HTTPClient.h" // not available on Linux/Portduino

class URLService : public ITileService
{
Expand All @@ -16,7 +15,6 @@ class URLService : public ITileService
virtual ~URLService();

private:
HTTPClient http;
Callback saveCB = nullptr;
};

Expand Down
51 changes: 42 additions & 9 deletions source/graphics/map/URLService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#ifdef ARDUINO_ARCH_ESP32

#include "HTTPClient.h" // not available on Linux/Portduino
#include "WiFi.h"

// from ConvertPNG.c
Expand All @@ -20,10 +21,7 @@ URLService::~URLService() {}

bool URLService::load(const char *name, void *img)
{
struct HttpEndGuard {
decltype(http) &client;
~HttpEndGuard() { client.end(); }
} httpGuard{http};
HTTPClient http;

if (WiFi.status() != WL_CONNECTED) {
ILOG_DEBUG("URLService::load skipped (WiFi not connected)");
Expand All @@ -42,33 +40,68 @@ bool URLService::load(const char *name, void *img)
return false;
}

http.begin(url.c_str());
http.setReuse(false);
if (!http.begin(url.c_str())) {
ILOG_ERROR("ERROR begin %s", url.c_str());
return false;
}

int httpCode = http.GET();
if (httpCode != HTTP_CODE_OK) {
ILOG_ERROR("ERROR GET %s : %d", url.c_str(), httpCode);
return false;
}

WiFiClient *stream = http.getStreamPtr();
size_t len = http.getSize();
if (len == 0) {
int contentLen = http.getSize();
if (contentLen <= 0) {
ILOG_WARN("GET %s : empty", url.c_str());
return false;
}

size_t len = (size_t)contentLen;

uint8_t *pngImage = (uint8_t *)lv_malloc(len);
LvFreeGuard pngGuard{pngImage};
if (!pngImage) {
ILOG_ERROR("lv_malloc failed for %s (%u bytes)", url.c_str(), (unsigned int)len);
return false;
}

size_t bytesRead = stream->readBytes(pngImage, len);
// read .png file in chunks to increase reliability (avoid readBytes())
size_t bytesRead = 0;
uint8_t idleSpins = 0;
const uint8_t maxIdleSpins = 3;
while (bytesRead < len) {
size_t available = stream->available();
if (available == 0) {
if (++idleSpins > maxIdleSpins) {
break;
}
delay(5);
continue;
}

idleSpins = 0;
size_t toRead = available;
size_t remaining = len - bytesRead;
if (toRead > remaining) {
toRead = remaining;
}

int got = stream->read(pngImage + bytesRead, toRead);
if (got <= 0) {
break;
}
bytesRead += (size_t)got;
}

if (bytesRead != len) {
ILOG_ERROR("http read error %s : %u != %u", url.c_str(), (unsigned int)bytesRead, (unsigned int)len);
return false;
}
ILOG_DEBUG("SUCCESS: GET %s (%u bytes)", url.c_str(), (unsigned int)bytesRead);

ILOG_DEBUG("SUCCESS(%d): GET %s (%u bytes)", (int)idleSpins, url.c_str(), (unsigned int)len);

// save png tile to SD card
if (saveCB && MapTileSettings::saveOK()) {
Expand Down
Loading