mirror of
https://github.com/MarekZegare4/MeshCore-Solo.git
synced 2026-07-26 14:58:12 +00:00
fix(screenshot): drop C-style cast UB, fix ERR check order
handleScreenshotRequest() reinterpret-cast every DisplayDriver* first to SH1106Display* then to SSD1306Display*; both casts always produce a non-null pointer, so the SSD1306 fallback was unreachable and on any SSD1306 build we called SH1106::getBuffer() on a SSD1306 object — UB that only happened to work because both backing classes share an Adafruit_GFX layout at offset zero. Replace the cast hack with virtual getBuffer()/getBufferSize() on DisplayDriver, overridden in SH1106Display and SSD1306Display. MyMesh no longer needs to know about either concrete type. const-correct the buffer pointer and widen bufferSize to uint16_t while passing through. Also fix the matching Python decoder: it sanity-checked length before dispatching on resp_code, so a 2-byte RESP_CODE_ERR frame was reported as "Frame too short" instead of the actual device error. Move the length check after the resp_code dispatch and log the error code byte. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -7,8 +7,6 @@
|
||||
|
||||
#ifdef DISPLAY_CLASS
|
||||
#include "helpers/ui/DisplayDriver.h"
|
||||
#include "helpers/ui/SH1106Display.h"
|
||||
#include "helpers/ui/SSD1306Display.h"
|
||||
#include "UITask.h"
|
||||
#endif
|
||||
|
||||
@@ -2083,29 +2081,13 @@ void MyMesh::handleScreenshotRequest() {
|
||||
writeErrFrame(ERR_CODE_UNSUPPORTED_CMD);
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t* buffer = nullptr;
|
||||
int bufferSize = 0;
|
||||
|
||||
// Try SH1106Display first (used by Wio L1)
|
||||
// Use C-style cast since RTTI is disabled (-fno-rtti)
|
||||
SH1106Display* sh1106 = (SH1106Display*)display;
|
||||
if (sh1106) {
|
||||
buffer = sh1106->getBuffer();
|
||||
bufferSize = (display->width() * display->height()) / 8;
|
||||
} else {
|
||||
// Fallback: try SSD1306Display
|
||||
SSD1306Display* ssd1306 = (SSD1306Display*)display;
|
||||
if (ssd1306) {
|
||||
buffer = ssd1306->getBuffer();
|
||||
bufferSize = (display->width() * display->height()) / 8;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const uint8_t* buffer = display->getBuffer();
|
||||
uint16_t bufferSize = display->getBufferSize();
|
||||
|
||||
if (buffer && bufferSize > 0) {
|
||||
sendScreenshotResponse(display, buffer, bufferSize);
|
||||
} else {
|
||||
// No supported display type
|
||||
writeErrFrame(ERR_CODE_UNSUPPORTED_CMD);
|
||||
}
|
||||
#else
|
||||
@@ -2113,26 +2095,28 @@ void MyMesh::handleScreenshotRequest() {
|
||||
#endif
|
||||
}
|
||||
|
||||
void MyMesh::sendScreenshotResponse(DisplayDriver* display, uint8_t* buffer, int bufferSize) {
|
||||
// MAX_FRAME_SIZE is 172 bytes, but we need to send 1026 bytes (3 header + 1024 buffer)
|
||||
// We need to split into multiple frames
|
||||
// Frame format: RESP_CODE_SCREENSHOT, width, height, chunk_index, total_chunks, [chunk_data...]
|
||||
|
||||
const int MAX_DATA_PER_FRAME = MAX_FRAME_SIZE - 5; // 5 bytes header
|
||||
void MyMesh::sendScreenshotResponse(DisplayDriver* display, const uint8_t* buffer, uint16_t bufferSize) {
|
||||
// Frame format (5-byte header): RESP_CODE_SCREENSHOT, width, height,
|
||||
// chunk_idx, total_chunks, [chunk_data...]
|
||||
// width/height fit in uint8_t for all displays this firmware targets
|
||||
// (SH1106 128×64, SSD1306 128×64); the framebuffer is page-based 1bpp
|
||||
// column-major, byte_idx = page*width + col.
|
||||
const int HEADER_SIZE = 5;
|
||||
const int MAX_DATA_PER_FRAME = MAX_FRAME_SIZE - HEADER_SIZE;
|
||||
int totalChunks = (bufferSize + MAX_DATA_PER_FRAME - 1) / MAX_DATA_PER_FRAME;
|
||||
|
||||
|
||||
for (int chunkIdx = 0; chunkIdx < totalChunks; chunkIdx++) {
|
||||
int i = 0;
|
||||
out_frame[i++] = RESP_CODE_SCREENSHOT;
|
||||
out_frame[i++] = display->width();
|
||||
out_frame[i++] = display->height();
|
||||
out_frame[i++] = chunkIdx;
|
||||
out_frame[i++] = totalChunks;
|
||||
|
||||
int chunkSize = min(MAX_DATA_PER_FRAME, bufferSize - chunkIdx * MAX_DATA_PER_FRAME);
|
||||
out_frame[i++] = (uint8_t)display->width();
|
||||
out_frame[i++] = (uint8_t)display->height();
|
||||
out_frame[i++] = (uint8_t)chunkIdx;
|
||||
out_frame[i++] = (uint8_t)totalChunks;
|
||||
|
||||
int chunkSize = min(MAX_DATA_PER_FRAME, (int)bufferSize - chunkIdx * MAX_DATA_PER_FRAME);
|
||||
memcpy(&out_frame[i], buffer + chunkIdx * MAX_DATA_PER_FRAME, chunkSize);
|
||||
i += chunkSize;
|
||||
|
||||
|
||||
_serial->writeFrame(out_frame, i);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,7 +229,7 @@ private:
|
||||
bool isValidClientRepeatFreq(uint32_t f) const;
|
||||
#ifdef ENABLE_SCREENSHOT
|
||||
void handleScreenshotRequest();
|
||||
void sendScreenshotResponse(DisplayDriver* display, uint8_t* buffer, int bufferSize);
|
||||
void sendScreenshotResponse(DisplayDriver* display, const uint8_t* buffer, uint16_t bufferSize);
|
||||
#endif
|
||||
|
||||
UITask* getUITask() { return (UITask*)_ui; }
|
||||
|
||||
Reference in New Issue
Block a user