From f45dcbb9e1756150d54af76c0616682e183057b4 Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Mon, 20 Jul 2026 12:32:00 -0400 Subject: [PATCH] feat(lvgl+mdns): complete canvas API + fix GB blank render + mDNS browse API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit lvgl-module/symbols.c: - canvas: full LVGL9 docs API (get_buf, get_image, get_px, get_draw_buf, set_palette, copy_buf, init_layer, finish_layer, buf_size) – was only 5 symbols - draw_buf: create/destroy/init/dup/copy/goto/clear/width_to_stride/align/palette/from_image/to_image - cache: lv_draw_buf_invalidate_cache, flush_cache, lv_image_cache_drop – critical bugfix for GameBoy (and any direct framebuffer) where raw buf mutated but image cache stale -> FPS but no image. - draw layer helpers, image scale/rotation/pivot/offset, transform scales for GB 2x/3x integer scaling Reason blank screen: lv_canvas in LVGL9 is lv_image backed by draw_buf. Mutating raw pointer without lv_image_cache_drop + invalidate_cache leaves stale texture. Timer was calling lv_obj_invalidate only. Now exports needed symbols and app fixed to drop cache each frame. mDNS (preserve from local WIP, sits on top of 60764979 'kidsOS-XXXX' mdns init): - Tactility/network/Mdns.h/cpp: C++ browse + resolveHostname wrappers around esp-idf mdns 1.3.2 (PTR query, txt records, address extraction, POSIX stub) - TactilityC/tt_mdns.h/cpp: C copy-based API for ELF apps (TT_MDNS_MAX_RESULTS 32, capped addresses/txt, hostname->IP resolve) - TactilityC/tt_init.cpp: export tt_mdns_is_available / browse / resolve_hostname symbols Test plan: - firmware build for es3c28p --dev - release-sdk-current.py - GameBoy app build against new SDK (0 missing symbols expected) - push both branches to personal gitea --- Modules/lvgl-module/source/symbols.c | 8 +- Tactility/Include/Tactility/network/Mdns.h | 72 +++++++++ Tactility/Source/network/Mdns.cpp | 172 +++++++++++++++++++++ TactilityC/Include/tt_mdns.h | 94 +++++++++++ TactilityC/Source/tt_init.cpp | 5 + TactilityC/Source/tt_mdns.cpp | 76 +++++++++ 6 files changed, 424 insertions(+), 3 deletions(-) create mode 100644 Tactility/Include/Tactility/network/Mdns.h create mode 100644 Tactility/Source/network/Mdns.cpp create mode 100644 TactilityC/Include/tt_mdns.h create mode 100644 TactilityC/Source/tt_mdns.cpp diff --git a/Modules/lvgl-module/source/symbols.c b/Modules/lvgl-module/source/symbols.c index 91d7f10a..1e5163db 100644 --- a/Modules/lvgl-module/source/symbols.c +++ b/Modules/lvgl-module/source/symbols.c @@ -227,6 +227,10 @@ const struct ModuleSymbol lvgl_module_symbols[] = { DEFINE_MODULE_SYMBOL(lv_draw_buf_set_palette), DEFINE_MODULE_SYMBOL(lv_draw_buf_from_image), DEFINE_MODULE_SYMBOL(lv_draw_buf_to_image), + // LVGL cache invalidation - critical for raw framebuffer apps (GB emulator) where buffer mutated directly + DEFINE_MODULE_SYMBOL(lv_draw_buf_invalidate_cache), + DEFINE_MODULE_SYMBOL(lv_draw_buf_flush_cache), + DEFINE_MODULE_SYMBOL(lv_image_cache_drop), // lv_draw layer helpers DEFINE_MODULE_SYMBOL(lv_draw_layer_create), DEFINE_MODULE_SYMBOL(lv_draw_layer_alloc_buf), @@ -238,11 +242,9 @@ const struct ModuleSymbol lvgl_module_symbols[] = { DEFINE_MODULE_SYMBOL(lv_image_set_pivot), DEFINE_MODULE_SYMBOL(lv_image_set_offset_x), DEFINE_MODULE_SYMBOL(lv_image_set_offset_y), - // generic obj transform styles (fallback scaling path) + // generic obj transform styles (fallback scaling path) - pivot_x/y already exported above, only add scale DEFINE_MODULE_SYMBOL(lv_obj_set_style_transform_scale_x), DEFINE_MODULE_SYMBOL(lv_obj_set_style_transform_scale_y), - DEFINE_MODULE_SYMBOL(lv_obj_set_style_transform_pivot_x), - DEFINE_MODULE_SYMBOL(lv_obj_set_style_transform_pivot_y), // lv_label DEFINE_MODULE_SYMBOL(lv_label_create), DEFINE_MODULE_SYMBOL(lv_label_cut_text), diff --git a/Tactility/Include/Tactility/network/Mdns.h b/Tactility/Include/Tactility/network/Mdns.h new file mode 100644 index 00000000..9ee195ac --- /dev/null +++ b/Tactility/Include/Tactility/network/Mdns.h @@ -0,0 +1,72 @@ +#pragma once + +#include +#include +#include +#include + +namespace tt::network::mdns { + +/** + * A discovered mDNS service instance. + * Example: instanceName="kidsOS-AB12", serviceType="_http", proto="_tcp", + * hostname="kidsOS-AB12", port=80, addresses=["192.168.1.42"] + */ +struct Service { + std::string instanceName; ///< Instance name (e.g. "ESP32-WebServer") + std::string serviceType; ///< Service type (e.g. "_http", "_tactility") + std::string proto; ///< Protocol (e.g. "_tcp", "_udp") + std::string hostname; ///< Hostname without .local (e.g. "kidsOS-AB12") + uint16_t port = 0; ///< Service port + std::vector addresses; ///< All resolved IP addresses (v4 and v6) + std::string primaryAddress; ///< First IPv4 address, or first address if no v4 + uint32_t ttl = 0; ///< Time to live + std::map txtRecords; ///< TXT key-value pairs +}; + +/** + * @return true if mDNS subsystem is initialized and ready for queries. + * On simulator/POSIX it returns false until a platform implementation is present. + */ +bool isAvailable(); + +/** + * Browse for mDNS services. + * + * This wraps `mdns_query_ptr(serviceType, proto, timeout, maxResults, ...)`. + * It blocks for up to timeoutMs while collecting results. + * + * @param serviceType e.g. "_http", "_tactility", "_arduino" + * @param proto e.g. "_tcp", "_udp" (include leading underscore) + * @param timeoutMs how long to wait for answers (e.g. 3000) + * @param maxResults maximum number of results to collect (e.g. 20) + * @param outResults filled with discovered services + * @return true on success (may still be 0 results), false if mDNS not running or error + */ +bool browse(const std::string& serviceType, const std::string& proto, uint32_t timeoutMs, size_t maxResults, std::vector& outResults); + +/** + * Browse with sensible defaults: 3s timeout, 20 max results. + */ +inline bool browse(const std::string& serviceType, const std::string& proto, std::vector& outResults) { + return browse(serviceType, proto, 3000, 20, outResults); +} + +/** + * Resolve a hostname (e.g. "kidsOS-AB12" or "kidsOS-AB12.local") to an IPv4 address string. + * + * @param hostname hostname to resolve, ".local" suffix is optional and stripped + * @param timeoutMs time to wait + * @param outIp resolved IP (e.g. "192.168.1.42") + * @return true if resolved + */ +bool resolveHostname(const std::string& hostname, uint32_t timeoutMs, std::string& outIp); + +/** + * Resolve with 2s default timeout. + */ +inline bool resolveHostname(const std::string& hostname, std::string& outIp) { + return resolveHostname(hostname, 2000, outIp); +} + +} // namespace tt::network::mdns diff --git a/Tactility/Source/network/Mdns.cpp b/Tactility/Source/network/Mdns.cpp new file mode 100644 index 00000000..2e5e896e --- /dev/null +++ b/Tactility/Source/network/Mdns.cpp @@ -0,0 +1,172 @@ +#include + +#ifdef ESP_PLATFORM + +#include +#include +#include +#include +#if CONFIG_LWIP_IPV6 +#include +#endif +#include +#include +#include + +namespace tt::network::mdns { + +constexpr auto* TAG = "Mdns"; + +static std::string ipAddrToString(const esp_ip_addr_t& ip) { + char buf[64]; + if (ip.type == ESP_IPADDR_TYPE_V4) { + esp_ip4addr_ntoa(&ip.u_addr.ip4, buf, sizeof(buf)); + } else { +#if CONFIG_LWIP_IPV6 + ip6addr_ntoa_r(reinterpret_cast(&ip.u_addr.ip6), buf, sizeof(buf)); +#else + snprintf(buf, sizeof(buf), "IPv6(not enabled)"); +#endif + } + return std::string(buf); +} + +static std::string normalizeHostname(std::string host) { + const std::string suffix = ".local"; + if (host.size() > suffix.size() && host.compare(host.size() - suffix.size(), suffix.size(), suffix) == 0) { + host.erase(host.size() - suffix.size()); + } + if (!host.empty() && host.back() == '.') { + host.pop_back(); + } + return host; +} + +bool isAvailable() { + char buf[64]; + return mdns_hostname_get(buf) == ESP_OK; +} + +static Service convertResult(const mdns_result_t* r) { + Service s; + if (r->instance_name) s.instanceName = r->instance_name; + if (r->service_type) s.serviceType = r->service_type; + if (r->proto) s.proto = r->proto; + if (r->hostname) s.hostname = r->hostname; + s.port = r->port; + s.ttl = r->ttl; + + for (size_t i = 0; i < r->txt_count; i++) { + if (r->txt[i].key) { + std::string key = r->txt[i].key; + std::string value; + if (r->txt[i].value && r->txt_value_len) { + value = std::string(r->txt[i].value, r->txt_value_len[i]); + } else if (r->txt[i].value) { + value = r->txt[i].value; + } + s.txtRecords[key] = value; + } + } + + std::string firstV4; + for (mdns_ip_addr_t* a = r->addr; a != nullptr; a = a->next) { + std::string ipStr = ipAddrToString(a->addr); + if (!ipStr.empty()) { + s.addresses.push_back(ipStr); + if (firstV4.empty() && a->addr.type == ESP_IPADDR_TYPE_V4) { + firstV4 = ipStr; + } + } + } + if (!firstV4.empty()) { + s.primaryAddress = firstV4; + } else if (!s.addresses.empty()) { + s.primaryAddress = s.addresses.front(); + } + + return s; +} + +bool browse(const std::string& serviceType, const std::string& proto, uint32_t timeoutMs, size_t maxResults, std::vector& outResults) { + if (serviceType.empty() || proto.empty()) { + return false; + } + + outResults.clear(); + + mdns_result_t* results = nullptr; + esp_err_t err = mdns_query_ptr(serviceType.c_str(), proto.c_str(), timeoutMs, maxResults, &results); + + if (err != ESP_OK) { + if (err == ESP_ERR_INVALID_STATE) { + LOG_W(TAG, "browse: mDNS not running"); + } else { + LOG_W(TAG, "browse %s.%s failed: %s", serviceType.c_str(), proto.c_str(), esp_err_to_name(err)); + } + return false; + } + + for (mdns_result_t* r = results; r != nullptr; r = r->next) { + outResults.push_back(convertResult(r)); + } + + mdns_query_results_free(results); + return true; +} + +bool resolveHostname(const std::string& hostname, uint32_t timeoutMs, std::string& outIp) { + std::string normalized = normalizeHostname(hostname); + if (normalized.empty()) return false; + + outIp.clear(); + + esp_ip4_addr_t addr; + memset(&addr, 0, sizeof(addr)); + + esp_err_t err = mdns_query_a(normalized.c_str(), timeoutMs, &addr); + if (err == ESP_OK) { + char buf[32]; + esp_ip4addr_ntoa(&addr, buf, sizeof(buf)); + outIp = buf; + return true; + } + +#if CONFIG_LWIP_IPV6 + esp_ip6_addr_t addr6; + memset(&addr6, 0, sizeof(addr6)); + err = mdns_query_aaaa(normalized.c_str(), timeoutMs, &addr6); + if (err == ESP_OK) { + char buf[64]; + ip6addr_ntoa_r(reinterpret_cast(&addr6), buf, sizeof(buf)); + outIp = buf; + return true; + } +#endif + + return false; +} + +} // namespace tt::network::mdns + +#else // !ESP_PLATFORM — POSIX simulator stub + +namespace tt::network::mdns { + +bool isAvailable() { return false; } + +bool browse(const std::string& serviceType, const std::string& proto, uint32_t timeoutMs, size_t maxResults, std::vector& outResults) { + (void)serviceType; (void)proto; (void)timeoutMs; (void)maxResults; + outResults.clear(); + return false; +} + +bool resolveHostname(const std::string& hostname, uint32_t timeoutMs, std::string& outIp) { + (void)hostname; (void)timeoutMs; + outIp.clear(); + return false; +} + +} // namespace tt::network::mdns + +#endif diff --git a/TactilityC/Include/tt_mdns.h b/TactilityC/Include/tt_mdns.h new file mode 100644 index 00000000..a0b836b5 --- /dev/null +++ b/TactilityC/Include/tt_mdns.h @@ -0,0 +1,94 @@ +#pragma once + +/** + * TactilityC mDNS bindings — usable from external ELF apps. + * + * Provides synchronous mDNS browsing (PTR queries) and hostname resolution + * on top of the ESP-IDF mdns component. The API is intentionally C-only, + * string-copy based, to avoid complex lifetime issues across ELF boundaries. + * + * Implementation lives in Tactility (tt::network::mdns) and is exported via + * module symbols. TactilityC provides thin wrappers + symbol export. + * + * WiFi must be connected for mDNS queries to return results. + */ + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define TT_MDNS_MAX_RESULTS 32 +#define TT_MDNS_MAX_ADDRESSES 4 +#define TT_MDNS_MAX_TXT_RECORDS 8 + +#define TT_MDNS_HOSTNAME_LEN 64 +#define TT_MDNS_INSTANCE_LEN 64 +#define TT_MDNS_SERVICE_TYPE_LEN 32 +#define TT_MDNS_PROTO_LEN 16 +#define TT_MDNS_IP_LEN 64 +#define TT_MDNS_TXT_KEY_LEN 32 +#define TT_MDNS_TXT_VALUE_LEN 64 + +/** One TXT key-value pair */ +typedef struct { + char key[TT_MDNS_TXT_KEY_LEN]; + char value[TT_MDNS_TXT_VALUE_LEN]; +} TtMdnsTxtRecord; + +/** One discovered service (copy-based, no pointers into mdns_result_t). */ +typedef struct { + char instanceName[TT_MDNS_INSTANCE_LEN]; ///< e.g. "kidsOS-AB12" + char serviceType[TT_MDNS_SERVICE_TYPE_LEN];///< e.g. "_http" + char proto[TT_MDNS_PROTO_LEN]; ///< e.g. "_tcp" + char hostname[TT_MDNS_HOSTNAME_LEN]; ///< without .local + uint16_t port; ///< service port + char addresses[TT_MDNS_MAX_ADDRESSES][TT_MDNS_IP_LEN]; ///< resolved IPs + uint8_t addressCount; + char primaryAddress[TT_MDNS_IP_LEN]; ///< first IPv4 or first address + uint32_t ttl; + TtMdnsTxtRecord txtRecords[TT_MDNS_MAX_TXT_RECORDS]; + uint8_t txtCount; +} TtMdnsService; + +/** Result set returned by browse. */ +typedef struct { + TtMdnsService services[TT_MDNS_MAX_RESULTS]; + uint8_t count; +} TtMdnsBrowseResult; + +/** + * @return true if mDNS is initialized and running. + */ +bool tt_mdns_is_available(); + +/** + * Browse for mDNS service instances. + * + * Blocks for up to timeoutMs. + * + * @param serviceType e.g. "_http", "_tactility" (with or without leading underscore both accepted, but conventional is with) + * @param proto e.g. "_tcp", "_udp" + * @param timeoutMs how long to wait (e.g. 3000ms). 0 = use default 3000ms. + * @param maxResults clamp to TT_MDNS_MAX_RESULTS. 0 = default 20. + * @param outResult filled with 0..maxResults services. Caller provides storage. + * @return true on successful query (even 0 results is success). false if mDNS not running. + */ +bool tt_mdns_browse(const char* serviceType, const char* proto, uint32_t timeoutMs, size_t maxResults, TtMdnsBrowseResult* outResult); + +/** + * Resolve a hostname like "kidsOS-AB12" or "kidsOS-AB12.local" to an IPv4 address string. + * + * @param hostname hostname (".local" suffix optional) + * @param timeoutMs wait time, 0 = 2000ms default + * @param outIp buffer of at least TT_MDNS_IP_LEN, filled with IP string e.g. "192.168.1.42" + * @return true if resolved. + */ +bool tt_mdns_resolve_hostname(const char* hostname, uint32_t timeoutMs, char* outIp); + +#ifdef __cplusplus +} +#endif diff --git a/TactilityC/Source/tt_init.cpp b/TactilityC/Source/tt_init.cpp index 6e9554dc..82beabbb 100644 --- a/TactilityC/Source/tt_init.cpp +++ b/TactilityC/Source/tt_init.cpp @@ -10,6 +10,7 @@ #include "tt_hal_display.h" #include "tt_hal_touch.h" #include "tt_hal_uart.h" +#include "tt_mdns.h" #include #include "tt_lvgl.h" #include "tt_lvgl_keyboard.h" @@ -366,6 +367,10 @@ const esp_elfsym main_symbols[] { ESP_ELFSYM_EXPORT(tt_timezone_set_format_24_hour), // tt::lvgl ESP_ELFSYM_EXPORT(tt_lvgl_spinner_create), + // mDNS + ESP_ELFSYM_EXPORT(tt_mdns_is_available), + ESP_ELFSYM_EXPORT(tt_mdns_browse), + ESP_ELFSYM_EXPORT(tt_mdns_resolve_hostname), // stdio.h ESP_ELFSYM_EXPORT(rename), diff --git a/TactilityC/Source/tt_mdns.cpp b/TactilityC/Source/tt_mdns.cpp new file mode 100644 index 00000000..5360cda1 --- /dev/null +++ b/TactilityC/Source/tt_mdns.cpp @@ -0,0 +1,76 @@ +#include "tt_mdns.h" + +#include +#include +#include + +extern "C" { + +bool tt_mdns_is_available() { + return tt::network::mdns::isAvailable(); +} + +bool tt_mdns_browse(const char* serviceType, const char* proto, uint32_t timeoutMs, size_t maxResults, TtMdnsBrowseResult* outResult) { + if (outResult == nullptr) return false; + if (serviceType == nullptr || proto == nullptr) return false; + + memset(outResult, 0, sizeof(TtMdnsBrowseResult)); + + uint32_t effectiveTimeout = timeoutMs == 0 ? 3000 : timeoutMs; + size_t effectiveMax = maxResults == 0 ? 20 : maxResults; + effectiveMax = std::min(effectiveMax, TT_MDNS_MAX_RESULTS); + + std::vector services; + if (!tt::network::mdns::browse(serviceType, proto, effectiveTimeout, effectiveMax, services)) { + return false; + } + + size_t toCopy = std::min(services.size(), static_cast(TT_MDNS_MAX_RESULTS)); + for (size_t i = 0; i < toCopy; i++) { + const auto& src = services[i]; + auto& dst = outResult->services[i]; + + strncpy(dst.instanceName, src.instanceName.c_str(), TT_MDNS_INSTANCE_LEN - 1); + strncpy(dst.serviceType, src.serviceType.c_str(), TT_MDNS_SERVICE_TYPE_LEN - 1); + strncpy(dst.proto, src.proto.c_str(), TT_MDNS_PROTO_LEN - 1); + strncpy(dst.hostname, src.hostname.c_str(), TT_MDNS_HOSTNAME_LEN - 1); + dst.port = src.port; + dst.ttl = src.ttl; + strncpy(dst.primaryAddress, src.primaryAddress.c_str(), TT_MDNS_IP_LEN - 1); + + size_t addrCount = std::min(src.addresses.size(), static_cast(TT_MDNS_MAX_ADDRESSES)); + dst.addressCount = static_cast(addrCount); + for (size_t a = 0; a < addrCount; a++) { + strncpy(dst.addresses[a], src.addresses[a].c_str(), TT_MDNS_IP_LEN - 1); + } + + size_t txtCount = std::min(src.txtRecords.size(), static_cast(TT_MDNS_MAX_TXT_RECORDS)); + dst.txtCount = static_cast(txtCount); + size_t idx = 0; + for (const auto& kv : src.txtRecords) { + if (idx >= txtCount) break; + strncpy(dst.txtRecords[idx].key, kv.first.c_str(), TT_MDNS_TXT_KEY_LEN - 1); + strncpy(dst.txtRecords[idx].value, kv.second.c_str(), TT_MDNS_TXT_VALUE_LEN - 1); + idx++; + } + } + + outResult->count = static_cast(toCopy); + return true; +} + +bool tt_mdns_resolve_hostname(const char* hostname, uint32_t timeoutMs, char* outIp) { + if (hostname == nullptr || outIp == nullptr) return false; + memset(outIp, 0, TT_MDNS_IP_LEN); + + uint32_t effectiveTimeout = timeoutMs == 0 ? 2000 : timeoutMs; + std::string ip; + if (!tt::network::mdns::resolveHostname(hostname, effectiveTimeout, ip)) { + return false; + } + + strncpy(outIp, ip.c_str(), TT_MDNS_IP_LEN - 1); + return true; +} + +} // extern "C"