Add Creality K-series LAN discovery via DNS-SD mDNS

When a user adds a Creality K-series printer (host_type=crealityprint)
and clicks the existing "Browse" button in the Physical Printer dialog,
dispatch to a new CrealityDiscoveryDialog that finds K2 / K2 Plus /
K2 Pro printers on the LAN automatically. For other host types the
button keeps its existing BonjourDialog behaviour.

  CrealityHostDiscovery (src/slic3r/Utils/CrealityHostDiscovery.{hpp,cpp})

    Wraps the vendored cxmdns wrapper from the previous commit:

      static std::vector<CrealityHost> scan(bool probe_info = true);

    Calls cxnet::syncDiscoveryService({"Creality", "creality"}) to find
    K-series printers via DNS-SD, dedupes by IP, then optionally HTTP
    GETs http://<ip>/info on each match to fetch the model code
    (F008 = K2 Plus, F012 = K2 Pro, F021 = K2) and MAC. Returns enriched
    {ip, hostname, model_code, model_name, mac, cfs_capable} entries.

  CrealityDiscoveryDialog (src/slic3r/GUI/CrealityDiscoveryDialog.{hpp,cpp})

    Modal dialog with a wxListView showing Model / Hostname / IP per
    discovered host. Runs CrealityHostDiscovery::scan() synchronously
    with wxBusyCursor + wxWindowDisabler (5-10s total wait). User picks
    one, dialog returns the IP via selected_ip().

  PhysicalPrinterDialog (src/slic3r/GUI/PhysicalPrinterDialog.cpp)

    The "Browse" button's click handler now reads host_type from the
    edited config. If htCrealityPrint, opens CrealityDiscoveryDialog
    and writes "http://<ip>" into the print_host field. Otherwise the
    existing BonjourDialog path runs unchanged -- no behaviour change
    for OctoPrint / Moonraker / Klipper users.

No new UI surface: one existing button now does the right thing per
host_type, mirroring how Creality Print discovers its own printers.
This commit is contained in:
grant0013
2026-05-19 16:28:41 +00:00
parent d6cffbba26
commit 598df9ff0b
6 changed files with 372 additions and 1 deletions

View File

@@ -0,0 +1,128 @@
#include "CrealityHostDiscovery.hpp"
#include "mdns/cxmdns.h"
#include "Http.hpp"
#include <boost/log/trivial.hpp>
#include <nlohmann/json.hpp>
#include <algorithm>
namespace Slic3r {
namespace {
struct ModelEntry { const char* code; const char* name; };
constexpr ModelEntry kCfsCapableModels[] = {
{"F008", "K2 Plus"},
{"F012", "K2 Pro"},
{"F021", "K2"},
};
bool is_cfs_capable(const std::string& code)
{
for (const auto& m : kCfsCapableModels)
if (code == m.code) return true;
return false;
}
std::string model_name_for(const std::string& code)
{
for (const auto& m : kCfsCapableModels)
if (code == m.code) return m.name;
return {};
}
// Extract the device suffix from a service name like
// "_Creality-543324280CDB19._udp.local." and synthesise a hostname-ish label
// (e.g. "K2-DB19" using the last 4 hex of the MAC-derived suffix).
std::string hostname_from_service(const std::string& service_name)
{
auto dash = service_name.find_last_of('-');
if (dash == std::string::npos) return {};
auto dot = service_name.find('.', dash);
if (dot == std::string::npos) return {};
std::string suffix = service_name.substr(dash + 1, dot - dash - 1);
if (suffix.size() >= 4) {
return "K2-" + suffix.substr(suffix.size() - 4);
}
return suffix.empty() ? std::string{} : "K2-" + suffix;
}
// Synchronously probe http://<ip>/info for {model, mac}. Short timeout --
// we don't want one slow host to drag down discovery.
void probe_info(CrealityHost& host)
{
const std::string url = "http://" + host.ip + "/info";
auto http = Http::get(url);
http.timeout_connect(2)
.timeout_max(4)
.on_complete([&host](std::string body, unsigned /*status*/) {
try {
auto j = nlohmann::json::parse(body);
if (j.contains("model") && j["model"].is_string())
host.model_code = j["model"].get<std::string>();
if (j.contains("mac") && j["mac"].is_string())
host.mac = j["mac"].get<std::string>();
if (is_cfs_capable(host.model_code)) {
host.cfs_capable = true;
host.model_name = model_name_for(host.model_code);
}
} catch (const std::exception& e) {
BOOST_LOG_TRIVIAL(warning)
<< "CrealityHostDiscovery: /info parse failed for "
<< host.ip << ": " << e.what();
}
})
.on_error([&host](std::string /*body*/, std::string error, unsigned status) {
BOOST_LOG_TRIVIAL(info)
<< "CrealityHostDiscovery: /info GET failed for "
<< host.ip << ": " << error << " (HTTP " << status << ")";
})
.perform_sync();
}
} // namespace
std::vector<CrealityHost> CrealityHostDiscovery::scan(bool probe)
{
const std::vector<std::string> prefixes{ "Creality", "creality" };
BOOST_LOG_TRIVIAL(info)
<< "CrealityHostDiscovery: starting DNS-SD discovery (prefixes: Creality, creality)";
auto raw = cxnet::syncDiscoveryService(prefixes);
BOOST_LOG_TRIVIAL(info)
<< "CrealityHostDiscovery: mDNS returned " << raw.size() << " match(es)";
std::vector<CrealityHost> hosts;
hosts.reserve(raw.size());
// Dedupe by IP -- one printer may announce twice if multi-homed or if
// we capture both IPv4/IPv6 replies.
std::vector<std::string> seen_ips;
for (const auto& m : raw) {
if (m.machineIp.empty()) continue;
if (std::find(seen_ips.begin(), seen_ips.end(), m.machineIp) != seen_ips.end())
continue;
seen_ips.push_back(m.machineIp);
CrealityHost h;
h.ip = m.machineIp;
h.service_name = m.answer;
h.hostname = hostname_from_service(m.answer);
if (probe) {
probe_info(h);
}
hosts.push_back(std::move(h));
}
BOOST_LOG_TRIVIAL(info)
<< "CrealityHostDiscovery: " << hosts.size() << " unique host(s) after dedup";
return hosts;
}
} // namespace Slic3r