Implement crash handler and diagnostics app (#119)

This commit is contained in:
Ken Van Hoeylandt
2024-12-12 22:38:48 +01:00
committed by GitHub
parent 74a53b2735
commit 51c6aaa428
38 changed files with 3717 additions and 24 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ if (DEFINED ENV{ESP_IDF_VERSION})
SRCS ${SOURCE_FILES}
INCLUDE_DIRS "Source/"
PRIV_INCLUDE_DIRS "Private/"
REQUIRES TactilityHeadless lvgl driver elf_loader lv_screenshot
REQUIRES TactilityHeadless lvgl driver elf_loader lv_screenshot QRCode
)
add_definitions(-DESP_PLATFORM)
+11 -6
View File
@@ -17,7 +17,9 @@ static const Configuration* config_instance = nullptr;
namespace service {
namespace gui { extern const ServiceManifest manifest; }
namespace loader { extern const ServiceManifest manifest; }
#ifndef ESP_PLATFORM // Screenshots don't work yet on ESP32
namespace screenshot { extern const ServiceManifest manifest; }
#endif
namespace statusbar { extern const ServiceManifest manifest; }
}
@@ -54,13 +56,15 @@ namespace app {
namespace wifiapsettings { extern const AppManifest manifest; }
namespace wificonnect { extern const AppManifest manifest; }
namespace wifimanage { extern const AppManifest manifest; }
#ifdef ESP_PLATFORM
extern const AppManifest elfWrapperManifest;
namespace crashdiagnostics { extern const AppManifest manifest; }
#else
namespace app::screenshot { extern const AppManifest manifest; }
#endif
}
#ifndef ESP_PLATFORM
extern const app::AppManifest screenshot_app;
#endif
static const std::vector<const app::AppManifest*> system_apps = {
@@ -81,10 +85,11 @@ static const std::vector<const app::AppManifest*> system_apps = {
&app::wifiapsettings::manifest,
&app::wificonnect::manifest,
&app::wifimanage::manifest,
#ifndef ESP_PLATFORM
&app::screenshot::manifest, // Screenshots don't work yet on ESP32
#else
#ifdef ESP_PLATFORM
&app::crashdiagnostics::manifest,
&app::elfWrapperManifest, // For hot-loading ELF apps
#else
&app::screenshot::manifest, // Screenshots don't work yet on ESP32
#endif
};
+12
View File
@@ -4,6 +4,7 @@
#include "app/AppContext.h"
#include "app/display/DisplaySettings.h"
#include "hal/Display.h"
#include "kernel/PanicHandler.h"
#include "service/loader/Loader.h"
#include "lvgl/Style.h"
@@ -43,8 +44,19 @@ static int32_t threadCallback(TT_UNUSED void* context) {
if (minimum_ticks > ticks_passed) {
tt::kernel::delayTicks(minimum_ticks - ticks_passed);
}
tt::service::loader::stopApp();
#ifdef ESP_PLATFORM
esp_reset_reason_t reason = esp_reset_reason();
if (reason == ESP_RST_PANIC) {
tt::service::loader::startApp("CrashDiagnostics");
} else {
tt::service::loader::startApp("Desktop");
}
#else
tt::service::loader::startApp("Desktop");
#endif
return 0;
}
@@ -0,0 +1,121 @@
#ifdef ESP_PLATFORM
#include <esp_private/panic_internal.h>
#include "lvgl.h"
#include "lvgl/Statusbar.h"
#include "service/loader/Loader.h"
#include "qrcode.h"
#include "QrHelpers.h"
#include "QrUrl.h"
#define TAG "crash_diagnostics"
namespace tt::app::crashdiagnostics {
void onContinuePressed(TT_UNUSED lv_event_t* event) {
tt::service::loader::stopApp();
tt::service::loader::startApp("Desktop");
}
static void onShow(TT_UNUSED AppContext& app, lv_obj_t* parent) {
auto* display = lv_obj_get_display(parent);
int32_t parent_height = lv_display_get_vertical_resolution(display) - STATUSBAR_HEIGHT;
lv_obj_add_event_cb(parent, onContinuePressed, LV_EVENT_CLICKED, nullptr);
auto* top_label = lv_label_create(parent);
lv_label_set_text(top_label, "Oops! We've crashed ..."); // TODO: Funny messages
lv_obj_align(top_label, LV_ALIGN_TOP_MID, 0, 2);
auto* bottom_label = lv_label_create(parent);
lv_label_set_text(bottom_label, "Tap screen to continue");
lv_obj_align(bottom_label, LV_ALIGN_BOTTOM_MID, 0, -2);
std::string url = getUrlFromCrashData();
TT_LOG_I(TAG, "%s", url.c_str());
size_t url_length = url.length();
int qr_version;
if (!getQrVersionForBinaryDataLength(url_length, qr_version)) {
TT_LOG_E(TAG, "QR is too large");
service::loader::stopApp();
return;
}
TT_LOG_I(TAG, "QR version %d (length: %d)", qr_version, url_length);
auto qrcodeData = std::make_shared<uint8_t[]>(qrcode_getBufferSize(qr_version));
if (qrcodeData == nullptr) {
TT_LOG_E(TAG, "Failed to allocate QR buffer");
service::loader::stopApp();
return;
}
QRCode qrcode;
TT_LOG_I(TAG, "QR init text");
if (qrcode_initText(&qrcode, qrcodeData.get(), qr_version, ECC_LOW, url.c_str()) != 0) {
TT_LOG_E(TAG, "QR init text failed");
service::loader::stopApp();
return;
}
TT_LOG_I(TAG, "QR size: %d", qrcode.size);
// Calculate QR dot size
int32_t top_label_height = lv_obj_get_height(top_label) + 2;
int32_t bottom_label_height = lv_obj_get_height(bottom_label) + 2;
TT_LOG_I(TAG, "Create canvas");
int32_t available_height = parent_height - top_label_height - bottom_label_height;
int32_t available_width = lv_display_get_horizontal_resolution(display);
int32_t smallest_size = TT_MIN(available_height, available_width);
int32_t pixel_size;
if (qrcode.size * 2 <= smallest_size) {
pixel_size = 2;
} else if (qrcode.size <= smallest_size) {
pixel_size = 1;
} else {
TT_LOG_E(TAG, "QR code won't fit screen");
service::loader::stopApp();
return;
}
auto* canvas = lv_canvas_create(parent);
lv_obj_set_size(canvas, pixel_size * qrcode.size, pixel_size * qrcode.size);
lv_obj_align(canvas, LV_ALIGN_CENTER, 0, 0);
lv_canvas_fill_bg(canvas, lv_color_black(), LV_OPA_COVER);
lv_obj_set_content_height(canvas, qrcode.size * pixel_size);
lv_obj_set_content_width(canvas, qrcode.size * pixel_size);
TT_LOG_I(TAG, "Create draw buffer");
auto* draw_buf = lv_draw_buf_create(pixel_size * qrcode.size, pixel_size * qrcode.size, LV_COLOR_FORMAT_RGB565, LV_STRIDE_AUTO);
if (draw_buf == nullptr) {
TT_LOG_E(TAG, "Draw buffer alloc");
service::loader::stopApp();
return;
}
lv_canvas_set_draw_buf(canvas, draw_buf);
for (uint8_t y = 0; y < qrcode.size; y++) {
for (uint8_t x = 0; x < qrcode.size; x++) {
bool colored = qrcode_getModule(&qrcode, x, y);
auto color = colored ? lv_color_white() : lv_color_black();
int32_t pos_x = x * pixel_size;
int32_t pos_y = y * pixel_size;
for (int px = 0; px < pixel_size; px++) {
for (int py = 0; py < pixel_size; py++) {
lv_canvas_set_px(canvas, pos_x + px, pos_y + py, color, LV_OPA_COVER);
}
}
}
}
}
extern const AppManifest manifest = {
.id = "CrashDiagnostics",
.name = "Crash Diagnostics",
.type = TypeHidden,
.onShow = onShow
};
} // namespace
#endif
@@ -0,0 +1,27 @@
#include <cstdint>
#include <cassert>
#include "QrHelpers.h"
/**
* Maps QR version (starting at a fictitious 0) to the usable byte size for L(ow) CRC checking QR.
*
* See https://www.qrcode.com/en/about/version.html
*/
uint16_t qr_version_binary_sizes[] = {
0, 17, 32, 53, 78, 106, 134, 154, 192, 230, // 0 - 9
271, 321, 367, 425, 458, 520, 586, 644, 718, 792, // 10-19
858, 929, 1003, 1091, 1171, 1273, 1367, 1465, 1528, 1682, // 20-29
1732, 1840, 1952, 2068, 2188, 2303, 2431, 2563, 2699, 2809, 2953 // 30-40
};
bool getQrVersionForBinaryDataLength(size_t inLength, int& outVersion) {
static_assert(sizeof(qr_version_binary_sizes) == 41 * sizeof(int16_t));
for (int version = 1; version < 41; version++) {
if (inLength <= qr_version_binary_sizes[version]) {
outVersion = version;
return true;
}
}
return false;
}
@@ -0,0 +1,5 @@
#pragma once
#include <cstdio>
bool getQrVersionForBinaryDataLength(size_t inLength, int& outVersion);
@@ -0,0 +1,43 @@
#ifdef ESP_TARGET
#include "QrUrl.h"
#include "kernel/PanicHandler.h"
#include "Log.h"
#include <sstream>
#include <esp_cpu_utils.h>
std::string getUrlFromCrashData() {
auto* crash_data = getRtcCrashData();
auto* stack_buffer = (uint32_t*) malloc(crash_data->callstackLength * 2 * sizeof(uint32_t));
for (int i = 0; i < crash_data->callstackLength; ++i) {
const CallstackFrame&frame = crash_data->callstack[i];
uint32_t pc = esp_cpu_process_stack_pc(frame.pc);
uint32_t sp = frame.sp;
stack_buffer[i * 2] = pc;
stack_buffer[(i * 2) + 1] = sp;
}
assert(sizeof(CallstackFrame) == 8);
std::stringstream stream;
stream << "https://oops.bytewelder.com?";
stream << "i=1"; // Application id
stream << "&v=" << TT_VERSION; // Version
stream << "&a=" << CONFIG_IDF_TARGET; // Architecture
stream << "&s="; // Stacktrace
for (int i = crash_data->callstackLength - 1; i >= 0; --i) {
uint32_t pc = stack_buffer[(i * 2)];
uint32_t sp = stack_buffer[(i * 2) + 1];
stream << std::hex << pc << std::hex << sp;
}
free(stack_buffer);
return stream.str();
}
#endif
@@ -0,0 +1,5 @@
#pragma once
#include <string>
std::string getUrlFromCrashData();
+2
View File
@@ -233,6 +233,8 @@ static void onShow(AppContext& app, lv_obj_t* parent) {
}
static void onStart(AppContext& app) {
auto* test = new uint32_t;
delete test;
auto data = std::make_shared<Data>();
// PC platform is bound to current work directory because of the LVGL file system mapping
if (kernel::getPlatform() == kernel::PlatformSimulator) {