Implement http-module (#637)

Create http-module and implement it in App Hub and App Hub Details apps.
This commit is contained in:
Ken Van Hoeylandt
2026-08-28 23:09:59 +02:00
committed by GitHub
parent d2442bedb4
commit bcbf18e363
27 changed files with 1265 additions and 77 deletions
+1
View File
@@ -13,6 +13,7 @@ list(APPEND REQUIRES_LIST
app-module
crypt-module
gps-module
http-module
gps-generic-module
gps-meshtastic-module
service-module
+1
View File
@@ -2,6 +2,7 @@
#include <Tactility/TactilityCore.h>
#include <cstdint>
#include <cstdio>
#include <dirent.h>
#include <functional>
+2
View File
@@ -41,6 +41,7 @@
#include <gps/module.h>
#include <gps_generic/module.h>
#include <gps_meshtastic/module.h>
#include <http/module.h>
#include <crypt/module.h>
#include <lvgl/devices/keyboard.h>
@@ -475,6 +476,7 @@ void run(Module* const dtsModules[], const DtsDevice dtsDevices[]) {
check(module_ensure_started(&gps_module) == ERROR_NONE);
check(module_ensure_started(&gps_generic_module) == ERROR_NONE);
check(module_ensure_started(&gps_meshtastic_module) == ERROR_NONE);
check(module_ensure_started(&http_module) == ERROR_NONE);
// Registers the APP_LOCATION_MEMORY app loader (boot/launcher need it below).
check(module_ensure_started(&app_module) == ERROR_NONE);
#ifdef ESP_PLATFORM
+121 -28
View File
@@ -4,7 +4,6 @@
#include <Tactility/app/apphub/AppHubEntry.h>
#include <Tactility/app/apphubdetails/AppHubDetailsApp.h>
#include <Tactility/file/File.h>
#include <Tactility/network/Http.h>
#include <Tactility/service/wifi/Wifi.h>
#include <app/event.h>
@@ -12,6 +11,8 @@
#include <app/manifest.h>
#include <app/scheduler.h>
#include <http/download.h>
#include <lvgl_window_manager/window_manager.h>
#include <tactility/check.h>
@@ -22,6 +23,7 @@
#include <lvgl/widgets/toolbar.h>
#include <algorithm>
#include <atomic>
#include <format>
namespace tt::app::apphub {
@@ -43,6 +45,20 @@ struct Context {
// Survives across a bury/resurface cycle (e.g. opening AppHubDetailsApp and returning),
int32_t scrollY = 0;
// Set by createWidgets(), consumed by the first showApps() after resurfacing. The refresh
// itself runs async (see requestRefresh() below), so when showApps() first populates the
// list, contentWrapper is still an empty spinner; scrollY must come from here instead of a
// live read off it.
bool restoreScrollOnNextShow = false;
// Only the first createWidgets() call triggers a network refresh. The rest uses the cached file.
bool needsInitialRefresh = true;
TaskEventGroup* eventGroup = nullptr;
uint32_t refreshRequestedBit = 0;
std::atomic<bool> refreshRequested {false};
HttpDownloadSubscription downloadSub {};
bool downloadInProgress = false;
};
@@ -66,9 +82,14 @@ void onAppPressed(lv_event_t* e) {
ctx->mutex.unlock();
}
void requestRefresh(Context* ctx) {
ctx->refreshRequested = true;
task_event_group_signal(ctx->eventGroup, ctx->refreshRequestedBit);
}
void onRefreshPressed(lv_event_t* e) {
auto* ctx = static_cast<Context*>(lv_event_get_user_data(e));
refresh(ctx);
requestRefresh(ctx);
}
void showRefreshFailedError(Context* ctx, const char* message) {
@@ -88,7 +109,13 @@ void showNoInternet(Context* ctx) {
void showApps(Context* ctx) {
// Refresh rebuilds the list from scratch (cached copy, then again once the network fetch
// lands), which would otherwise reset the user's scroll position each time.
auto scrollY = lv_obj_get_scroll_y(ctx->contentWrapper);
int32_t scrollY;
if (ctx->restoreScrollOnNextShow) {
scrollY = ctx->scrollY;
ctx->restoreScrollOnNextShow = false;
} else {
scrollY = lv_obj_get_scroll_y(ctx->contentWrapper);
}
lv_obj_clean(ctx->contentWrapper);
ctx->mutex.lock();
if (parseJson(ctx->cachedAppsJsonFile, ctx->entries)) {
@@ -130,42 +157,82 @@ void showApps(Context* ctx) {
ctx->mutex.unlock();
}
// Runs on appMain()'s own task (triggered via requestRefresh()), never directly from the LVGL task.
void refresh(Context* ctx) {
// Buried (e.g. AppHubDetailsApp is open): destroyWidgets() already released these. A refresh
// request queued just before burying could still land here, so re-check rather than assume
// requestRefresh() and refresh() always run against a live window.
if (ctx->downloadInProgress || ctx->contentWrapper == nullptr) {
return;
}
lvgl_lock();
lv_obj_clean(ctx->contentWrapper);
auto* spinner = lvgl_spinner_create(ctx->contentWrapper);
lv_obj_align(spinner, LV_ALIGN_CENTER, 0, 0);
lv_obj_add_flag(ctx->refreshButton, LV_OBJ_FLAG_HIDDEN);
lvgl_unlock();
if (service::wifi::getRadioState() != service::wifi::RadioState::ConnectionActive) {
lvgl_lock();
showNoInternet(ctx);
lvgl_unlock();
return;
}
if (file::isFile(ctx->cachedAppsJsonFile)) {
lvgl_lock();
showApps(ctx);
lvgl_unlock();
}
// These callbacks run on a background network thread and reach back into this app's
// widgets via the captured ctx pointer - same convention as AppHubDetailsApp.cpp's
// download callback for the sibling "install/update" flow.
network::http::download(
getAppsJsonUrl(),
CERTIFICATE_PATH,
ctx->cachedAppsJsonFile,
[ctx] {
LOG_I(TAG, "Request success");
lvgl_lock();
showApps(ctx);
lvgl_unlock();
},
[ctx](const char* error) {
LOG_E(TAG, "Request failed: %s", error);
lvgl_lock();
showRefreshFailedError(ctx, "Cannot reach server");
lvgl_unlock();
}
);
if (http_download_subscribe(&ctx->downloadSub, ctx->eventGroup) != ERROR_NONE) {
LOG_E(TAG, "Failed to subscribe to download events");
lvgl_lock();
showRefreshFailedError(ctx, "Cannot reach server");
lvgl_unlock();
return;
}
auto url = getAppsJsonUrl();
if (http_download_start(url.c_str(), CERTIFICATE_PATH, ctx->cachedAppsJsonFile.c_str(), &ctx->downloadSub) != ERROR_NONE) {
LOG_E(TAG, "Failed to start download");
http_download_unsubscribe(&ctx->downloadSub);
lvgl_lock();
showRefreshFailedError(ctx, "Cannot reach server");
lvgl_unlock();
return;
}
ctx->downloadInProgress = true;
}
// Called from appMain()'s loop once http_download_poll() reports the download's terminal event.
void onDownloadFinished(Context* ctx, const HttpDownloadEvent& event) {
ctx->downloadInProgress = false;
http_download_unsubscribe(&ctx->downloadSub);
bool succeeded = event.type == HTTP_DOWNLOAD_EVENT_SUCCESS;
ctx->needsInitialRefresh = !succeeded;
if (succeeded) {
LOG_I(TAG, "Request success (status %d)", event.status_code);
} else {
LOG_E(TAG, "Request failed (status %d): %s", event.status_code, event.error.message);
}
if (ctx->contentWrapper == nullptr) {
// Buried (e.g. AppHubDetailsApp is open): destroyWidgets() already released the widgets
// above. createWidgets() picks this up via needsInitialRefresh on resurface instead.
return;
}
lvgl_lock();
if (succeeded) {
showApps(ctx);
} else {
showRefreshFailedError(ctx, "Cannot reach server");
}
lvgl_unlock();
}
void createWidgets(lv_obj_t* parent, void* userData) {
@@ -186,9 +253,15 @@ void createWidgets(lv_obj_t* parent, void* userData) {
lv_obj_set_style_pad_all(ctx->contentWrapper, 0, LV_STATE_DEFAULT);
lv_obj_set_style_pad_ver(ctx->contentWrapper, 0, LV_STATE_DEFAULT);
refresh(ctx);
lv_obj_scroll_to_y(ctx->contentWrapper, ctx->scrollY, LV_ANIM_OFF);
ctx->restoreScrollOnNextShow = true;
if (ctx->needsInitialRefresh) {
requestRefresh(ctx);
} else {
// Resurfacing (e.g. returning from AppHubDetailsApp): redisplay the cache already
// loaded this session instead of hitting the network again. window_manager calls
// createWidgets() with the LVGL lock already held, so this can touch widgets directly.
showApps(ctx);
}
}
void destroyWidgets(void* userData) {
@@ -205,6 +278,10 @@ int32_t appMain(int argc, char* argv[]) {
TaskEventGroup event_group {};
task_event_group_construct(&event_group);
ctx.eventGroup = &event_group;
if (task_event_group_claim_bit(&event_group, &ctx.refreshRequestedBit) != ERROR_NONE) {
LOG_W(TAG, "Failed to claim a refresh-requested bit; refresh button won't work");
}
AppEventSubscription sub {};
check(app_event_subscribe(&sub, &event_group) == ERROR_NONE);
@@ -224,8 +301,24 @@ int32_t appMain(int argc, char* argv[]) {
default:
break;
}
if (shouldClose) break;
}
if (ctx.downloadInProgress) {
HttpDownloadEvent download_event {};
if (http_download_poll(&ctx.downloadSub, &download_event) == ERROR_NONE) {
onDownloadFinished(&ctx, download_event);
}
}
if (!shouldClose && ctx.refreshRequested.exchange(false)) {
refresh(&ctx);
}
}
if (ctx.downloadInProgress) {
http_download_cancel(&ctx.downloadSub);
http_download_unsubscribe(&ctx.downloadSub);
ctx.downloadInProgress = false;
}
window_manager_remove(window);
@@ -4,7 +4,6 @@
#include <Tactility/app/apphub/AppHub.h>
#include <Tactility/app/apphub/AppHubEntry.h>
#include <Tactility/file/File.h>
#include <Tactility/network/Http.h>
#include <app/event.h>
#include <app/install.h>
@@ -13,6 +12,8 @@
#include <app/manifest.h>
#include <app/scheduler.h>
#include <http/download.h>
#include <lvgl_window_manager/window_manager.h>
#include <lvgl/lvgl.h>
@@ -51,6 +52,13 @@ struct Context {
std::atomic<uint32_t> installDialogId = 0;
std::atomic<uint32_t> uninstallDialogId = 0;
std::atomic<uint32_t> updateDialogId = 0;
// doInstall()/onDownloadFinished() and the poll for them both run on appMain()'s own task
// (triggered via confirm-dialog APP_EVENT_RESULTs), so HttpDownloadSubscription's
// subscribe/start/poll are naturally all on one consistent task already.
TaskEventGroup* eventGroup = nullptr;
HttpDownloadSubscription downloadSub {};
bool downloadInProgress = false;
};
@@ -95,36 +103,68 @@ void uninstallApp(Context* ctx) {
lvgl_unlock();
}
void doInstall(Context* ctx) {
auto url = apphub::getDownloadUrl(ctx->entry.file);
// Path doInstall() downloads to and onDownloadFinished() installs from - deterministic from ctx->entry,
// which doesn't change once this app instance is running, so it's recomputed at each use instead of stored.
std::string getTempFilePath(Context* ctx) {
auto file_name = file::getLastPathSegment(ctx->entry.file);
auto temp_file_path = std::format("{}/{}", getTempPath(), file_name);
network::http::download(
url,
apphub::CERTIFICATE_PATH,
temp_file_path,
[ctx, temp_file_path] {
app_install(temp_file_path.c_str());
return std::format("{}/{}", getTempPath(), file_name);
}
if (!file::deleteFile(temp_file_path)) {
LOG_W(TAG, "Failed to remove %s", temp_file_path.c_str());
} else {
LOG_I(TAG, "Deleted temporary file %s", temp_file_path.c_str());
}
void doInstall(Context* ctx) {
if (ctx->downloadInProgress) {
return;
}
lvgl_lock();
updateViews(ctx);
lvgl_unlock();
},
[ctx, temp_file_path](const char* errorMessage) {
LOG_E(TAG, "Download failed: %s", errorMessage);
if (http_download_subscribe(&ctx->downloadSub, ctx->eventGroup) != ERROR_NONE) {
LOG_E(TAG, "Failed to subscribe to download events");
alertdialog::start(ctx->appInstanceId, "Error", "Failed to install app");
return;
}
auto url = apphub::getDownloadUrl(ctx->entry.file);
auto temp_file_path = getTempFilePath(ctx);
if (http_download_start(url.c_str(), apphub::CERTIFICATE_PATH, temp_file_path.c_str(), &ctx->downloadSub) != ERROR_NONE) {
LOG_E(TAG, "Failed to start download");
http_download_unsubscribe(&ctx->downloadSub);
alertdialog::start(ctx->appInstanceId, "Error", "Failed to install app");
return;
}
ctx->downloadInProgress = true;
}
// Called from appMain()'s loop once http_download_poll() reports the download's terminal event.
void onDownloadFinished(Context* ctx, const HttpDownloadEvent& event) {
ctx->downloadInProgress = false;
http_download_unsubscribe(&ctx->downloadSub);
auto temp_file_path = getTempFilePath(ctx);
if (event.type == HTTP_DOWNLOAD_EVENT_SUCCESS) {
error_t install_result = app_install(temp_file_path.c_str());
if (install_result != ERROR_NONE) {
LOG_E(TAG, "Install of %s failed", temp_file_path.c_str());
alertdialog::start(ctx->appInstanceId, "Error", "Failed to install app");
if (file::isFile(temp_file_path) && !file::deleteFile(temp_file_path.c_str())) {
LOG_W(TAG, "Failed to remove %s", temp_file_path.c_str());
}
}
);
if (!file::deleteFile(temp_file_path)) {
LOG_W(TAG, "Failed to remove %s", temp_file_path.c_str());
} else {
LOG_I(TAG, "Deleted temporary file %s", temp_file_path.c_str());
}
lvgl_lock();
updateViews(ctx);
lvgl_unlock();
} else {
LOG_E(TAG, "Download failed (status %d): %s", event.status_code,
event.type == HTTP_DOWNLOAD_EVENT_ERROR ? event.error.message : "Cancelled");
alertdialog::start(ctx->appInstanceId, "Error", "Failed to install app");
if (file::isFile(temp_file_path) && !file::deleteFile(temp_file_path.c_str())) {
LOG_W(TAG, "Failed to remove %s", temp_file_path.c_str());
}
}
}
void installApp(Context* ctx) {
@@ -234,6 +274,7 @@ int32_t appMain(int argc, char* argv[]) {
TaskEventGroup event_group {};
task_event_group_construct(&event_group);
ctx.eventGroup = &event_group;
AppEventSubscription sub {};
check(app_event_subscribe(&sub, &event_group) == ERROR_NONE);
@@ -267,6 +308,21 @@ int32_t appMain(int argc, char* argv[]) {
}
if (shouldClose) break;
}
if (ctx.downloadInProgress) {
HttpDownloadEvent download_event {};
if (http_download_poll(&ctx.downloadSub, &download_event) == ERROR_NONE) {
onDownloadFinished(&ctx, download_event);
}
}
}
if (ctx.downloadInProgress) {
// Safe to call immediately, even mid-download - no need to wait for the terminal event
// first, so app close doesn't block on the network.
http_download_cancel(&ctx.downloadSub);
http_download_unsubscribe(&ctx.downloadSub);
ctx.downloadInProgress = false;
}
window_manager_remove(window);
+1
View File
@@ -19,6 +19,7 @@ target_link_libraries(TactilityTests PRIVATE
app-module
crypt-module
gps-module
http-module
gps-generic-module
gps-meshtastic-module
service-module