Skip to content

Fix WiFiClient #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 10, 2025
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
16 changes: 11 additions & 5 deletions src/OpenStreetMap-esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,16 +429,17 @@ void OpenStreetMap::PNGDraw(PNGDRAW *pDraw)
getPNGCurrentCore()->getLineAsRGB565(pDraw, destRow, PNG_RGB565_BIG_ENDIAN, 0xffffffff);
}

bool OpenStreetMap::fetchTile(CachedTile &tile, uint32_t x, uint32_t y, uint8_t zoom, String &result)
bool OpenStreetMap::fetchTile(ReusableTileFetcher &fetcher, CachedTile &tile, uint32_t x, uint32_t y, uint8_t zoom, String &result)
{

String url = currentProvider->urlTemplate;
url.replace("{x}", String(x));
url.replace("{y}", String(y));
url.replace("{z}", String(zoom));
if (currentProvider->requiresApiKey && strstr(url.c_str(), "{apiKey}"))
url.replace("{apiKey}", currentProvider->apiKey);

const std::unique_ptr<MemoryBuffer> buffer = urlToBuffer(url.c_str(), result);
const std::unique_ptr<MemoryBuffer> buffer = fetcher.fetchToBuffer(url, result);
if (!buffer)
return false;

Expand All @@ -462,19 +463,18 @@ bool OpenStreetMap::fetchTile(CachedTile &tile, uint32_t x, uint32_t y, uint8_t
if (decodeResult != PNG_SUCCESS)
{
result = "Decoding " + url + " failed with code: " + String(decodeResult);
tile.valid = false;
return false;
}

tile.x = x;
tile.y = y;
tile.z = zoom;
tile.valid = true;
return true;
}

void OpenStreetMap::tileFetcherTask(void *param)
{
ReusableTileFetcher fetcher;
OpenStreetMap *osm = static_cast<OpenStreetMap *>(param);
while (true)
{
Expand All @@ -486,17 +486,23 @@ void OpenStreetMap::tileFetcherTask(void *param)
break;

String result;
if (!osm->fetchTile(*job.tile, job.x, job.y, job.z, result))
if (!osm->fetchTile(fetcher, *job.tile, job.x, job.y, job.z, result))
{
const size_t tileByteCount = osm->currentProvider->tileSize * osm->currentProvider->tileSize * 2;
memset(job.tile->buffer, 0, tileByteCount);
job.tile->valid = false;
log_e("Tile fetch failed: %s", result.c_str());
}
else
{
job.tile->valid = true;
log_d("core %i fetched tile z=%u x=%lu, y=%lu in %lu ms", xPortGetCoreID(), job.z, job.x, job.y, millis() - startMS);
}

job.tile->busy = false;
--osm->pendingJobs;
if (!uxQueueMessagesWaiting(osm->jobQueue))
fetcher.close();
}
log_d("task on core %i exiting", xPortGetCoreID());
xTaskNotifyGive(osm->ownerTask);
Expand Down
3 changes: 2 additions & 1 deletion src/OpenStreetMap-esp32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "TileJob.hpp"
#include "MemoryBuffer.hpp"
#include "HTTPClientRAII.hpp"
#include "ReusableTileFetcher.hpp"
#include "fonts/DejaVu9-modded.h"

constexpr uint16_t OSM_BGCOLOR = lgfx::color565(32, 32, 128);
Expand Down Expand Up @@ -109,7 +110,7 @@ class OpenStreetMap
CachedTile *findUnusedTile(const tileList &requiredTiles, uint8_t zoom);
CachedTile *isTileCached(uint32_t x, uint32_t y, uint8_t z);
std::unique_ptr<MemoryBuffer> urlToBuffer(const char *url, String &result);
bool fetchTile(CachedTile &tile, uint32_t x, uint32_t y, uint8_t zoom, String &result);
bool fetchTile(ReusableTileFetcher &fetcher, CachedTile &tile, uint32_t x, uint32_t y, uint8_t zoom, String &result);
bool fillBuffer(WiFiClient *stream, MemoryBuffer &buffer, size_t contentSize, String &result);
bool composeMap(LGFX_Sprite &mapSprite, TileBufferList &tilePointers);
static void tileFetcherTask(void *param);
Expand Down
16 changes: 15 additions & 1 deletion src/ReusableTileFetcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,18 @@ bool ReusableTileFetcher::readHttpHeaders(size_t &contentLength, String &result)
break; // End of headers

if (line.startsWith("Content-Length:"))
contentLength = line.substring(15).toInt();
{
String val = line.substring(15);
val.trim();
contentLength = val.toInt();
}

else if (line.startsWith("HTTP/1.1"))
{
if (!line.startsWith("HTTP/1.1 200"))
{
result = "HTTP error: " + line;
client.stop();
return false;
}
}
Expand All @@ -128,6 +133,7 @@ bool ReusableTileFetcher::readHttpHeaders(size_t &contentLength, String &result)
if (contentLength == 0)
{
result = "Missing or invalid Content-Length";
client.stop();
return false;
}

Expand All @@ -152,6 +158,7 @@ bool ReusableTileFetcher::readBody(MemoryBuffer &buffer, size_t contentLength, S
else if (len < 0)
{
result = "Read error";
client.stop();
return false;
}
else
Expand All @@ -161,8 +168,15 @@ bool ReusableTileFetcher::readBody(MemoryBuffer &buffer, size_t contentLength, S
if (remaining > 0)
{
result = "Incomplete read";
client.stop();
return false;
}

return true;
}

void ReusableTileFetcher::close()
{
if (client)
client.stop();
}
1 change: 1 addition & 0 deletions src/ReusableTileFetcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ReusableTileFetcher
ReusableTileFetcher &operator=(const ReusableTileFetcher &) = delete;

std::unique_ptr<MemoryBuffer> fetchToBuffer(const String &url, String &result);
void close();

private:
WiFiClient client;
Expand Down