Files
tactility/Tactility/Source/network/Mdns.cpp
Adolfo Reyna 75178652e2 feat(lvgl+mdns): complete canvas API + fix GB blank render + mDNS browse API
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
2026-07-20 12:32:00 -04:00

173 lines
4.7 KiB
C++

#include <Tactility/network/Mdns.h>
#ifdef ESP_PLATFORM
#include <tactility/log.h>
#include <esp_wifi.h>
#include <esp_netif.h>
#include <lwip/ip4_addr.h>
#if CONFIG_LWIP_IPV6
#include <lwip/ip6_addr.h>
#endif
#include <mdns.h>
#include <cstring>
#include <algorithm>
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<const ip6_addr_t*>(&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<Service>& 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<const ip6_addr_t*>(&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<Service>& 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