mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-09-16 21:42:43 +00:00
930 lines
32 KiB
C++
930 lines
32 KiB
C++
#include "BBLPrinterAgent.hpp"
|
|
#include "BBLNetworkPlugin.hpp"
|
|
#include "FileTransferUtils.hpp"
|
|
#include "IPrinterAgent.hpp"
|
|
#include "NetworkAgentFactory.hpp"
|
|
#include "NetworkAgent.hpp"
|
|
|
|
#include <boost/format.hpp>
|
|
#include <boost/log/trivial.hpp>
|
|
#include <memory>
|
|
#include <nlohmann/json.hpp>
|
|
#include <cmath>
|
|
#include <slic3r/GUI/DeviceManager.hpp>
|
|
#include <libslic3r/Utils.hpp>
|
|
|
|
namespace Slic3r {
|
|
|
|
// ============================================================================
|
|
// File Transfer (Bambu eMMC tunnel ABI)
|
|
// ============================================================================
|
|
|
|
BBLFileTransferTunnel::BBLFileTransferTunnel(const std::string &url) : IFileTransferTunnel(url)
|
|
{
|
|
FileTransferModule &m = module();
|
|
m_ = &m;
|
|
// Guard against missing symbols in older Bambu networking plugins.
|
|
// These symbols were added in a newer plugin ABI; if the installed
|
|
// plugin predates them, ft_tunnel_create/ft_tunnel_set_status_cb
|
|
// will be null and calling them crashes.
|
|
if (!m_->ft_tunnel_create || !m_->ft_tunnel_set_status_cb) {
|
|
throw std::runtime_error("Bambu networking plugin is too old: missing ft_tunnel_* symbols. "
|
|
"Please update the networking plugin.");
|
|
}
|
|
FT_TunnelHandle *h{};
|
|
if (m_->ft_tunnel_create(url.c_str(), &h) != 0 || !h) {
|
|
throw std::runtime_error("ft_tunnel_create failed");
|
|
}
|
|
h_ = h;
|
|
|
|
// C API: ft_status_cb(void* user, int old_status, int new_status, int err, const char* msg)
|
|
auto tramp = [](void *user, int old_status, int new_status, int err_code, const char *msg) noexcept {
|
|
auto *self = reinterpret_cast<BBLFileTransferTunnel *>(user);
|
|
self->status_ = new_status;
|
|
if (!self->status_cb_) return;
|
|
try {
|
|
self->status_cb_(old_status, new_status, err_code, std::string(msg ? msg : ""));
|
|
} catch (...) {}
|
|
};
|
|
if (m_->ft_tunnel_set_status_cb(h_, tramp, this) == ft_err::FT_EXCEPTION) { throw std::runtime_error("ft_tunnel_set_status_cb failed"); }
|
|
}
|
|
|
|
void BBLFileTransferTunnel::start_connect()
|
|
{
|
|
// C API: ft_conn_cb(void* user, int ok, int err, const char* msg)
|
|
auto tramp = [](void *user, int ok, int ec, const char *msg) noexcept {
|
|
auto *pcb = reinterpret_cast<ConnectionCb *>(user);
|
|
if (!pcb) return;
|
|
try {
|
|
(*pcb)(ok == 0, ec, std::string(msg ? msg : ""));
|
|
} catch (...) {}
|
|
};
|
|
if (m_->ft_tunnel_start_connect(h_, tramp, &conn_cb_) == ft_err::FT_EXCEPTION) { throw std::runtime_error("ft_tunnel_start_connect failed"); }
|
|
}
|
|
|
|
bool BBLFileTransferTunnel::sync_start_connect()
|
|
{
|
|
return m_->ft_tunnel_sync_connect(h_) == FT_OK;
|
|
}
|
|
|
|
void BBLFileTransferTunnel::shutdown()
|
|
{
|
|
if (m_->ft_tunnel_shutdown) (void) m_->ft_tunnel_shutdown(h_);
|
|
}
|
|
|
|
BBLFileTransferJob::BBLFileTransferJob(const std::string ¶ms_json) : IFileTransferJob(params_json)
|
|
{
|
|
m_ = &module();
|
|
FT_JobHandle *h{};
|
|
if (m_->ft_job_create(params_json.c_str(), &h) != 0 || !h) {
|
|
throw std::runtime_error("ft_job_create failed");
|
|
}
|
|
h_ = h;
|
|
|
|
// C API: ft_job_result_cb(void* user, int tunnel_err, ft_job_result result)
|
|
auto tramp = [](void *user, ft_job_result r) noexcept {
|
|
auto *self = reinterpret_cast<BBLFileTransferJob *>(user);
|
|
if (!self) return;
|
|
|
|
try {
|
|
self->finished_ = true;
|
|
self->solve_result(r);
|
|
if (self->result_cb_) self->result_cb_(self->res_, self->resp_ec_, self->res_json_, self->res_bin_);
|
|
} catch (...) {
|
|
// swallow
|
|
}
|
|
|
|
try {
|
|
if (auto *mod = self ? self->m_ : nullptr) {
|
|
if (mod->ft_job_result_destroy)
|
|
mod->ft_job_result_destroy(&r);
|
|
else if (mod->ft_free) {
|
|
if (r.json) mod->ft_free((void *) r.json);
|
|
if (r.bin) mod->ft_free((void *) r.bin);
|
|
}
|
|
}
|
|
} catch (...) {}
|
|
};
|
|
|
|
if (m_->ft_job_set_result_cb(h_, tramp, this) == ft_err::FT_EXCEPTION) { throw std::runtime_error("ft_job_set_result_cb failed"); }
|
|
}
|
|
|
|
bool BBLFileTransferJob::get_result(int &ec, int &resp_ec, std::string &json, std::vector<std::byte> &bin, uint32_t timeout_ms)
|
|
{
|
|
if (!h_) throw std::runtime_error("job handle invalid");
|
|
ft_job_result result;
|
|
if (m_->ft_job_get_result(h_, timeout_ms, &result) == ft_err::FT_EXCEPTION) return false;
|
|
solve_result(result);
|
|
m_->ft_job_result_destroy(&result);
|
|
ec = res_;
|
|
resp_ec = res_;
|
|
json = res_json_;
|
|
bin = res_bin_;
|
|
return true;
|
|
}
|
|
|
|
void BBLFileTransferJob::start_on(IFileTransferTunnel &t)
|
|
{
|
|
if (!h_) throw std::runtime_error("job handle invalid");
|
|
auto *handle = reinterpret_cast<FT_TunnelHandle *>(t.native());
|
|
if (m_->ft_tunnel_start_job(handle, h_) == ft_err::FT_EXCEPTION) { throw std::runtime_error("ft_tunnel_start_job failed"); }
|
|
}
|
|
|
|
void BBLFileTransferJob::on_msg(MsgCb cb)
|
|
{
|
|
IFileTransferJob::on_msg(std::move(cb));
|
|
if (!h_) return;
|
|
|
|
// C API: ft_job_msg_cb(void* user, ft_job_msg msg)
|
|
auto tramp = [](void *user, ft_job_msg m) noexcept {
|
|
auto *self = reinterpret_cast<BBLFileTransferJob *>(user);
|
|
if (!self) return;
|
|
try {
|
|
if (self->msg_cb_) { self->msg_cb_(m.kind, std::string(m.json ? m.json : "")); }
|
|
} catch (...) {}
|
|
|
|
try {
|
|
if (auto *mod = self->m_) {
|
|
if (mod->ft_job_msg_destroy)
|
|
mod->ft_job_msg_destroy(&m);
|
|
else if (mod->ft_free && m.json)
|
|
mod->ft_free((void *) m.json);
|
|
}
|
|
} catch (...) {}
|
|
};
|
|
|
|
if (m_->ft_job_set_msg_cb(h_, tramp, this) == ft_err::FT_EXCEPTION) { throw std::runtime_error("ft_job_set_msg_cb failed"); }
|
|
}
|
|
|
|
bool BBLFileTransferJob::try_get_msg(int &kind, std::string &json)
|
|
{
|
|
if (!h_) return false;
|
|
ft_job_msg m{};
|
|
int rc = m_->ft_job_try_get_msg(h_, &m);
|
|
if (rc != 0) return false;
|
|
|
|
kind = m.kind;
|
|
json.assign(m.json ? m.json : "");
|
|
|
|
if (m_->ft_job_msg_destroy)
|
|
m_->ft_job_msg_destroy(&m);
|
|
else if (m_->ft_free && m.json)
|
|
m_->ft_free((void *) m.json);
|
|
|
|
return true;
|
|
}
|
|
|
|
bool BBLFileTransferJob::get_msg(uint32_t timeout_ms, int &kind, std::string &json)
|
|
{
|
|
if (!h_) return false;
|
|
ft_job_msg m{};
|
|
int rc = m_->ft_job_get_msg(h_, timeout_ms, &m);
|
|
if (rc != 0) return false;
|
|
|
|
kind = m.kind;
|
|
json.assign(m.json ? m.json : "");
|
|
|
|
if (m_->ft_job_msg_destroy)
|
|
m_->ft_job_msg_destroy(&m);
|
|
else if (m_->ft_free && m.json)
|
|
m_->ft_free((void *) m.json);
|
|
|
|
return true;
|
|
}
|
|
|
|
void BBLFileTransferJob::solve_result(ft_job_result result)
|
|
{
|
|
res_ = result.ec;
|
|
resp_ec_ = result.resp_ec;
|
|
|
|
res_bin_.clear();
|
|
if (result.bin && result.bin_size) res_bin_.assign(reinterpret_cast<const std::byte *>(result.bin),
|
|
reinterpret_cast<const std::byte *>(result.bin) + result.bin_size);
|
|
res_json_.assign(result.json ? result.json : "");
|
|
}
|
|
|
|
BBLPrinterAgent::BBLPrinterAgent() = default;
|
|
|
|
BBLPrinterAgent::~BBLPrinterAgent() = default;
|
|
|
|
void BBLPrinterAgent::set_cloud_agent(std::shared_ptr<ICloudServiceAgent> cloud)
|
|
{
|
|
m_cloud_agent = cloud;
|
|
// BBL DLL manages tokens internally, so this is just for interface compliance
|
|
}
|
|
|
|
// ============================================================================
|
|
// Communication
|
|
// ============================================================================
|
|
|
|
std::string BBLPrinterAgent::ams_refresh_rfid_gcode(const std::string& tray_id)
|
|
{
|
|
return (boost::format("M620 R%1% \n") % tray_id).str();
|
|
}
|
|
|
|
std::string BBLPrinterAgent::ams_calibrate_gcode(int ams_id)
|
|
{
|
|
return (boost::format("M620 C%1% \n") % ams_id).str();
|
|
}
|
|
|
|
std::string BBLPrinterAgent::ams_select_tray_gcode(const std::string& tray_id)
|
|
{
|
|
return (boost::format("M620 P%1% \n") % tray_id).str();
|
|
}
|
|
|
|
int BBLPrinterAgent::command_ams_refresh_rfid(std::string dev_id, std::string tray_id, int sequence_id, bool lan_mode)
|
|
{
|
|
const std::string gcode = ams_refresh_rfid_gcode(tray_id);
|
|
BOOST_LOG_TRIVIAL(trace) << "ams_debug: gcode_cmd" << gcode;
|
|
nlohmann::json j;
|
|
j["print"]["command"] = "gcode_line";
|
|
j["print"]["param"] = gcode;
|
|
j["print"]["sequence_id"] = std::to_string(sequence_id);
|
|
return publish(dev_id, j, lan_mode);
|
|
}
|
|
|
|
int BBLPrinterAgent::command_ams_calibrate(std::string dev_id, int ams_id, int sequence_id, bool lan_mode)
|
|
{
|
|
const std::string gcode = ams_calibrate_gcode(ams_id);
|
|
BOOST_LOG_TRIVIAL(trace) << "ams_debug: gcode_cmd" << gcode;
|
|
nlohmann::json j;
|
|
j["print"]["command"] = "gcode_line";
|
|
j["print"]["param"] = gcode;
|
|
j["print"]["sequence_id"] = std::to_string(sequence_id);
|
|
return publish(dev_id, j, lan_mode);
|
|
}
|
|
|
|
int BBLPrinterAgent::command_ams_select_tray(std::string dev_id, std::string tray_id, int sequence_id, bool lan_mode)
|
|
{
|
|
const std::string gcode = ams_select_tray_gcode(tray_id);
|
|
BOOST_LOG_TRIVIAL(trace) << "ams_debug: gcode_cmd" << gcode;
|
|
nlohmann::json j;
|
|
j["print"]["command"] = "gcode_line";
|
|
j["print"]["param"] = gcode;
|
|
j["print"]["sequence_id"] = std::to_string(sequence_id);
|
|
return publish(dev_id, j, lan_mode);
|
|
}
|
|
|
|
int BBLPrinterAgent::command_xyz_abs(std::string dev_id, int sequence_id, bool lan_mode)
|
|
{
|
|
nlohmann::json j;
|
|
j["print"]["command"] = "gcode_line";
|
|
j["print"]["param"] = "G90 \n";
|
|
j["print"]["sequence_id"] = std::to_string(sequence_id);
|
|
return publish(dev_id, j, lan_mode);
|
|
}
|
|
|
|
int BBLPrinterAgent::command_auto_leveling(std::string dev_id, int sequence_id, bool lan_mode)
|
|
{
|
|
nlohmann::json j;
|
|
j["print"]["command"] = "gcode_line";
|
|
j["print"]["param"] = "G29 \n";
|
|
j["print"]["sequence_id"] = std::to_string(sequence_id);
|
|
return publish(dev_id, j, lan_mode);
|
|
}
|
|
|
|
int BBLPrinterAgent::command_go_home(std::string dev_id, bool is_printing, bool supports_mqtt_homing, int sequence_id, bool lan_mode)
|
|
{
|
|
nlohmann::json j;
|
|
j["print"]["sequence_id"] = std::to_string(sequence_id);
|
|
if (supports_mqtt_homing) {
|
|
j["print"]["command"] = "back_to_center";
|
|
return publish(dev_id, j, lan_mode);
|
|
}
|
|
|
|
j["print"]["command"] = "gcode_line";
|
|
j["print"]["param"] = is_printing ? "G28 X\n" : "G28 \n";
|
|
return publish(dev_id, j, lan_mode);
|
|
}
|
|
|
|
int BBLPrinterAgent::command_set_bed(std::string dev_id, int temp, bool supports_mqtt_bed_ctrl, int sequence_id, bool lan_mode)
|
|
{
|
|
nlohmann::json j;
|
|
j["print"]["sequence_id"] = std::to_string(sequence_id);
|
|
if (supports_mqtt_bed_ctrl) {
|
|
j["print"]["command"] = "set_bed_temp";
|
|
j["print"]["temp"] = temp;
|
|
return publish(dev_id, j, lan_mode);
|
|
}
|
|
|
|
j["print"]["command"] = "gcode_line";
|
|
j["print"]["param"] = (boost::format("M140 S%1%\n") % temp).str();
|
|
return publish(dev_id, j, lan_mode);
|
|
}
|
|
|
|
int BBLPrinterAgent::command_set_nozzle(std::string dev_id, int temp, int sequence_id, bool lan_mode)
|
|
{
|
|
nlohmann::json j;
|
|
j["print"]["command"] = "gcode_line";
|
|
j["print"]["param"] = (boost::format("M104 S%1%\n") % temp).str();
|
|
j["print"]["sequence_id"] = std::to_string(sequence_id);
|
|
return publish(dev_id, j, lan_mode);
|
|
}
|
|
|
|
int BBLPrinterAgent::command_axis_control(std::string dev_id, std::string axis, double unit, double input_val, int speed,
|
|
bool is_core_xy, bool supports_mqtt_axis_control, int sequence_id, bool lan_mode)
|
|
{
|
|
nlohmann::json j;
|
|
j["print"]["sequence_id"] = std::to_string(sequence_id);
|
|
|
|
if (supports_mqtt_axis_control) {
|
|
int dir = input_val > 0 ? 1 : -1;
|
|
// i3-arch printers move the bed for Y/Z, so the on-screen direction is
|
|
// reversed -- same negation the g-code fallback below applies.
|
|
if (!is_core_xy && (axis == "Y" || axis == "Z")) {
|
|
dir = -dir;
|
|
}
|
|
|
|
j["print"]["command"] = "xyz_ctrl";
|
|
j["print"]["axis"] = axis;
|
|
j["print"]["dir"] = dir;
|
|
j["print"]["mode"] = (std::abs(input_val) >= 10) ? 1 : 0;
|
|
return publish(dev_id, j, lan_mode);
|
|
}
|
|
|
|
double value = input_val;
|
|
if (!is_core_xy && (axis == "Y" || axis == "Z")) {
|
|
value = -1.0 * input_val;
|
|
}
|
|
|
|
std::string value_str = (boost::format("%.1f") % (value * unit)).str();
|
|
std::string gcode;
|
|
if (axis == "X" || axis == "Y" || axis == "Z") {
|
|
gcode = (boost::format("M211 S \nM211 X1 Y1 Z1\nM1002 push_ref_mode\nG91 \nG1 %1%%2% F%3%\nM1002 pop_ref_mode\nM211 R\n")
|
|
% axis % value_str % speed).str();
|
|
} else if (axis == "E") {
|
|
gcode = (boost::format("M83 \nG0 %1%%2% F%3%\n") % axis % value_str % speed).str();
|
|
} else {
|
|
return -1;
|
|
}
|
|
|
|
j["print"]["command"] = "gcode_line";
|
|
j["print"]["param"] = gcode;
|
|
return publish(dev_id, j, lan_mode);
|
|
}
|
|
|
|
int BBLPrinterAgent::publish(const std::string& dev_id, const nlohmann::json& j, bool lan_mode)
|
|
{
|
|
const int rtn = lan_mode ? send_message_to_printer(dev_id, j.dump(), 0, 0) : send_message(dev_id, j.dump(), 0, 0);
|
|
if (rtn == 0) {
|
|
BOOST_LOG_TRIVIAL(info) << "publish_json: " << j.dump() << " code: " << rtn;
|
|
} else {
|
|
BOOST_LOG_TRIVIAL(error) << "publish_json: " << j.dump() << " code: " << rtn;
|
|
}
|
|
return rtn;
|
|
}
|
|
|
|
int BBLPrinterAgent::send_message(std::string dev_id, std::string json_str, int qos, int flag)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_send_message();
|
|
if (func && agent) {
|
|
// Only the legacy plug-in lacks `flag`; 02.03.00 already takes it, and routing that
|
|
// series through the legacy form would silently drop MessageFlag sign/encrypt.
|
|
switch (plugin.network_abi()) {
|
|
case NetworkAbi::Legacy: {
|
|
auto legacy_func = reinterpret_cast<func_send_message_legacy>(func);
|
|
return legacy_func(agent, std::move(dev_id), std::move(json_str), qos);
|
|
}
|
|
case NetworkAbi::V0203:
|
|
case NetworkAbi::Current:
|
|
return func(agent, std::move(dev_id), std::move(json_str), qos, flag);
|
|
default:
|
|
return -1;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int BBLPrinterAgent::connect_printer(std::string dev_id, std::string dev_ip, std::string username, std::string password, bool use_ssl)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_connect_printer();
|
|
if (func && agent) {
|
|
return func(agent, dev_id, dev_ip, username, password, use_ssl);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int BBLPrinterAgent::disconnect_printer()
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_disconnect_printer();
|
|
if (func && agent) {
|
|
return func(agent);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int BBLPrinterAgent::send_message_to_printer(std::string dev_id, std::string json_str, int qos, int flag)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_send_message_to_printer();
|
|
if (func && agent) {
|
|
switch (plugin.network_abi()) {
|
|
case NetworkAbi::Legacy: {
|
|
auto legacy_func = reinterpret_cast<func_send_message_to_printer_legacy>(func);
|
|
return legacy_func(agent, std::move(dev_id), std::move(json_str), qos);
|
|
}
|
|
case NetworkAbi::V0203:
|
|
case NetworkAbi::Current:
|
|
return func(agent, std::move(dev_id), std::move(json_str), qos, flag);
|
|
default:
|
|
return -1;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
std::string BBLPrinterAgent::get_local_camera_url(CameraURLParams params)
|
|
{
|
|
std::string url;
|
|
if (params.protocol == LVL_Local)
|
|
url = "bambu:///local/" + params.ip_address + ".?port=6000&user=" + params.user + "&passwd=" + params.password;
|
|
else if (params.protocol == LVL_Rtsps)
|
|
url = "bambu:///rtsps___" + params.user + ":" + params.password + "@" + params.ip_address + "/streaming/live/1?proto=rtsps";
|
|
else if (params.protocol == LVL_Rtsp)
|
|
url = "bambu:///rtsp___" + params.user + ":" + params.password + "@" + params.ip_address + "/streaming/live/1?proto=rtsp";
|
|
else
|
|
url = "bambu:///local/" + params.ip_address + ".?port=6000&user=" + params.user + "&passwd=" + params.password;
|
|
|
|
url += "&device=" + params.device;
|
|
url += "&net_ver=" + params.network_version;
|
|
url += "&dev_ver=" + params.device_version;
|
|
url += "&cli_id=" + params.client_id;
|
|
url += "&cli_ver=" + params.client_version;
|
|
return url;
|
|
}
|
|
|
|
std::string BBLPrinterAgent::get_local_file_transfer_url(const FileTransferURLParams& params)
|
|
{
|
|
// Keep the historical PartSkipDialog URL unchanged. It is a file-transfer
|
|
// tunnel URL, not a camera URL, so it intentionally has no camera metadata
|
|
// suffix and no dot before the query string.
|
|
return "bambu:///local/" + params.ip_address + "?port=6000&user=" + params.username + "&passwd=" + params.password;
|
|
}
|
|
|
|
int BBLPrinterAgent::get_file_transfer_url(std::string dev_id, std::function<void(FileTransferURLResult)> callback,
|
|
FileTransferURLParams params)
|
|
{
|
|
if (params.url_state == URL_TCP) {
|
|
FileTransferURLResult result;
|
|
result.url = get_local_file_transfer_url(params);
|
|
result.is_success = !result.url.empty();
|
|
result.error_code = result.is_success ? 0 : -1;
|
|
if (callback)
|
|
callback(std::move(result));
|
|
return result.is_success ? 0 : -1;
|
|
}
|
|
|
|
if (!m_cloud_agent) {
|
|
if (callback)
|
|
callback({});
|
|
return -1;
|
|
}
|
|
|
|
const std::string protocols = "\"tutk\",\"agora\"";
|
|
return m_cloud_agent->get_camera_url(
|
|
std::move(dev_id) + "|" + params.device_version + "|" + protocols,
|
|
std::move(callback),
|
|
CameraURLParams{
|
|
"",
|
|
"",
|
|
"",
|
|
LVL_None,
|
|
params.device_id,
|
|
params.network_version,
|
|
params.device_version,
|
|
params.refresh_url,
|
|
params.client_id,
|
|
params.client_version,
|
|
true
|
|
});
|
|
}
|
|
|
|
// ============================================================================
|
|
// Certificates
|
|
// ============================================================================
|
|
|
|
int BBLPrinterAgent::check_cert()
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_check_cert();
|
|
if (func && agent) {
|
|
return func(agent);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
void BBLPrinterAgent::install_device_cert(std::string dev_id, bool lan_only)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_install_device_cert();
|
|
if (func && agent) {
|
|
func(agent, dev_id, lan_only);
|
|
}
|
|
}
|
|
|
|
// ============================================================================
|
|
// Discovery
|
|
// ============================================================================
|
|
|
|
bool BBLPrinterAgent::start_discovery(bool start, bool sending)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_start_discovery();
|
|
if (func && agent) {
|
|
return func(agent, start, sending);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// ============================================================================
|
|
// Binding
|
|
// ============================================================================
|
|
|
|
int BBLPrinterAgent::ping_bind(std::string ping_code)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_ping_bind();
|
|
if (func && agent) {
|
|
return func(agent, ping_code);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int BBLPrinterAgent::bind_detect(std::string dev_ip, std::string sec_link, detectResult& detect)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_bind_detect();
|
|
if (func && agent) {
|
|
return func(agent, dev_ip, sec_link, detect);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int BBLPrinterAgent::bind(std::string dev_ip, std::string dev_id, std::string dev_model, std::string sec_link, std::string timezone, bool improved, OnUpdateStatusFn update_fn)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_bind();
|
|
if (func && agent) {
|
|
// dev_model was added in 02.08.01. Passing it to a plug-in that takes the 7-argument
|
|
// form shifts every following argument, so the older generations get the older call.
|
|
switch (plugin.network_abi()) {
|
|
case NetworkAbi::Legacy:
|
|
case NetworkAbi::V0203: {
|
|
auto older_func = reinterpret_cast<func_bind_pre0208>(func);
|
|
return older_func(agent, dev_ip, dev_id, sec_link, timezone, improved, update_fn);
|
|
}
|
|
case NetworkAbi::Current:
|
|
return func(agent, dev_ip, dev_id, dev_model, sec_link, timezone, improved, update_fn);
|
|
default:
|
|
return -1;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int BBLPrinterAgent::unbind(std::string dev_id)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_unbind();
|
|
if (func && agent) {
|
|
return func(agent, dev_id);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int BBLPrinterAgent::request_bind_ticket(std::string* ticket)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_request_bind_ticket();
|
|
if (func && agent) {
|
|
return func(agent, ticket);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int BBLPrinterAgent::get_hms_snapshot(std::string dev_id, std::string file_name, std::function<void(std::string, int)> callback)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_get_hms_snapshot();
|
|
// dev_id/file_name are passed as lvalues to bind the plugin's std::string& params.
|
|
// A null func (older plugin without this symbol) falls through to -1 so callers degrade gracefully.
|
|
if (func && agent) {
|
|
return func(agent, dev_id, file_name, callback);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int BBLPrinterAgent::set_server_callback(OnServerErrFn fn)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_set_server_callback();
|
|
if (func && agent) {
|
|
return func(agent, fn);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
// ============================================================================
|
|
// Machine Selection
|
|
// ============================================================================
|
|
|
|
std::string BBLPrinterAgent::get_user_selected_machine()
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_get_user_selected_machine();
|
|
if (func && agent) {
|
|
return func(agent);
|
|
}
|
|
return "";
|
|
}
|
|
|
|
int BBLPrinterAgent::set_user_selected_machine(std::string dev_id)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_set_user_selected_machine();
|
|
if (func && agent) {
|
|
return func(agent, dev_id);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
// ============================================================================
|
|
// Subscriptions
|
|
// ============================================================================
|
|
|
|
int BBLPrinterAgent::start_subscribe(std::string module)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_start_subscribe();
|
|
if (func && agent) {
|
|
return func(agent, module);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int BBLPrinterAgent::stop_subscribe(std::string module)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_stop_subscribe();
|
|
if (func && agent) {
|
|
return func(agent, module);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int BBLPrinterAgent::add_subscribe(std::vector<std::string> dev_list)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_add_subscribe();
|
|
if (func && agent) {
|
|
return func(agent, dev_list);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int BBLPrinterAgent::del_subscribe(std::vector<std::string> dev_list)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_del_subscribe();
|
|
if (func && agent) {
|
|
return func(agent, dev_list);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
// ============================================================================
|
|
// Agent Information
|
|
// ============================================================================
|
|
AgentInfo BBLPrinterAgent::get_agent_info_static()
|
|
{
|
|
return AgentInfo{BBL_PRINTER_AGENT_ID, "Bambu Lab", "", "Bambu Lab printer agent"};
|
|
}
|
|
|
|
// ============================================================================
|
|
// Print Job Operations
|
|
// ============================================================================
|
|
|
|
namespace {
|
|
|
|
// Shared dispatcher for the start_* operations, whose params layout differs per generation.
|
|
// The per-generation typedefs are template arguments so a swapped pair fails to compile
|
|
// (each arm's converted params must match the casted signature). Each arm converts and calls
|
|
// in one step: as_legacy()/as_0203() move out of `params` and their prvalue result lands in
|
|
// the by-value ABI argument without another copy; the Current arm moves `params` outright.
|
|
template <typename LegacyFn, typename Fn0203, typename CurrentFn, typename... CallbackFns>
|
|
int dispatch_start(CurrentFn func, PrintParams& params, const CallbackFns&... callbacks)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
if (!func || !agent)
|
|
return -1;
|
|
switch (plugin.network_abi()) {
|
|
case NetworkAbi::Legacy:
|
|
return reinterpret_cast<LegacyFn>(func)(agent, BBLNetworkPlugin::as_legacy(params), callbacks...);
|
|
case NetworkAbi::V0203:
|
|
return reinterpret_cast<Fn0203>(func)(agent, BBLNetworkPlugin::as_0203(params), callbacks...);
|
|
case NetworkAbi::Current:
|
|
return func(agent, std::move(params), callbacks...);
|
|
default:
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int BBLPrinterAgent::start_print(PrintParams params, OnUpdateStatusFn update_fn, WasCancelledFn cancel_fn, OnWaitFn wait_fn)
|
|
{
|
|
return dispatch_start<func_start_print_legacy, func_start_print_0203>(
|
|
BBLNetworkPlugin::instance().get_start_print(), params, update_fn, cancel_fn, wait_fn);
|
|
}
|
|
|
|
int BBLPrinterAgent::start_local_print_with_record(PrintParams params, OnUpdateStatusFn update_fn, WasCancelledFn cancel_fn, OnWaitFn wait_fn)
|
|
{
|
|
return dispatch_start<func_start_local_print_with_record_legacy, func_start_local_print_with_record_0203>(
|
|
BBLNetworkPlugin::instance().get_start_local_print_with_record(), params, update_fn, cancel_fn, wait_fn);
|
|
}
|
|
|
|
int BBLPrinterAgent::verify_local_print_access(PrintParams params)
|
|
{
|
|
params.project_name = "verify_job";
|
|
params.filename = Slic3r::resources_dir() + "/check_access_code.txt";
|
|
params.try_emmc_print = false;
|
|
|
|
return start_send_gcode_to_sdcard(params, nullptr, nullptr, nullptr);
|
|
}
|
|
|
|
int BBLPrinterAgent::start_send_gcode_to_sdcard(PrintParams params, OnUpdateStatusFn update_fn, WasCancelledFn cancel_fn, OnWaitFn wait_fn)
|
|
{
|
|
int result = dispatch_start<func_start_send_gcode_to_sdcard_legacy, func_start_send_gcode_to_sdcard_0203>(
|
|
BBLNetworkPlugin::instance().get_start_send_gcode_to_sdcard(), params, update_fn, cancel_fn, wait_fn);
|
|
if (result != 0) {
|
|
BOOST_LOG_TRIVIAL(error) << "start_send_gcode_to_sdcard failed: result=" << result
|
|
<< ", try_emmc_print=" << params.try_emmc_print
|
|
<< ", legacy_mode=" << BBLNetworkPlugin::instance().use_legacy_network()
|
|
<< ", dev_ip=" << params.dev_ip << ", dev_id=" << params.dev_id;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
int BBLPrinterAgent::start_local_print(PrintParams params, OnUpdateStatusFn update_fn, WasCancelledFn cancel_fn)
|
|
{
|
|
if (params.connection_type == "lan" && params.print_type == "from_normal") {
|
|
const int verify_result = verify_local_print_access(params);
|
|
if (verify_result != 0) {
|
|
BOOST_LOG_TRIVIAL(error) << "LAN connection verification failed: result=" << verify_result
|
|
<< ", dev_ip=" << params.dev_ip << ", dev_id=" << params.dev_id
|
|
<< ", password_length=" << params.password.size();
|
|
return ORCA_NETWORK_ERR_ACCESS_VERIFICATION_FAILED;
|
|
}
|
|
}
|
|
|
|
return dispatch_start<func_start_local_print_legacy, func_start_local_print_0203>(
|
|
BBLNetworkPlugin::instance().get_start_local_print(), params, update_fn, cancel_fn);
|
|
}
|
|
|
|
int BBLPrinterAgent::start_sdcard_print(PrintParams params, OnUpdateStatusFn update_fn, WasCancelledFn cancel_fn)
|
|
{
|
|
return dispatch_start<func_start_sdcard_print_legacy, func_start_sdcard_print_0203>(
|
|
BBLNetworkPlugin::instance().get_start_sdcard_print(), params, update_fn, cancel_fn);
|
|
}
|
|
|
|
// ============================================================================
|
|
// Callbacks
|
|
// ============================================================================
|
|
|
|
int BBLPrinterAgent::set_on_ssdp_msg_fn(OnMsgArrivedFn fn)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_set_on_ssdp_msg_fn();
|
|
if (func && agent) {
|
|
return func(agent, fn);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int BBLPrinterAgent::set_on_printer_connected_fn(OnPrinterConnectedFn fn)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_set_on_printer_connected_fn();
|
|
if (func && agent) {
|
|
return func(agent, fn);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int BBLPrinterAgent::set_on_subscribe_failure_fn(GetSubscribeFailureFn fn)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_set_on_subscribe_failure_fn();
|
|
if (func && agent) {
|
|
return func(agent, fn);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int BBLPrinterAgent::set_on_message_fn(OnMessageFn fn)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_set_on_message_fn();
|
|
if (func && agent) {
|
|
return func(agent, fn);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int BBLPrinterAgent::set_on_user_message_fn(OnMessageFn fn)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_set_on_user_message_fn();
|
|
if (func && agent) {
|
|
return func(agent, fn);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int BBLPrinterAgent::set_on_local_connect_fn(OnLocalConnectedFn fn)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_set_on_local_connect_fn();
|
|
if (func && agent) {
|
|
return func(agent, fn);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int BBLPrinterAgent::set_on_local_message_fn(OnMessageFn fn)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_set_on_local_message_fn();
|
|
if (func && agent) {
|
|
return func(agent, fn);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int BBLPrinterAgent::set_queue_on_main_fn(QueueOnMainFn fn)
|
|
{
|
|
auto& plugin = BBLNetworkPlugin::instance();
|
|
auto agent = plugin.get_agent();
|
|
auto func = plugin.get_set_queue_on_main_fn();
|
|
if (func && agent) {
|
|
return func(agent, fn);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
// ============================================================================
|
|
// Filament Operations
|
|
// ============================================================================
|
|
|
|
FilamentSyncMode BBLPrinterAgent::get_filament_sync_mode() const
|
|
{
|
|
// BBL uses MQTT subscription for real-time filament updates
|
|
return FilamentSyncMode::subscription;
|
|
}
|
|
|
|
std::unique_ptr<IFileTransferTunnel> BBLPrinterAgent::create_file_transfer_tunnel(std::string& dev_ip, std::string& access_code) {
|
|
std::string url = "bambu:///local/" + dev_ip + "?port=6000&user=" + default_lan_username() + "&passwd=" + access_code;
|
|
return std::make_unique<BBLFileTransferTunnel>(url);
|
|
}
|
|
|
|
std::unique_ptr<IFileTransferTunnel> BBLPrinterAgent::create_file_transfer_tunnel_from_url(std::string url) {
|
|
return std::make_unique<BBLFileTransferTunnel>(url);
|
|
}
|
|
|
|
std::unique_ptr<IFileTransferJob> BBLPrinterAgent::create_file_transfer_job(std::string params_json) {
|
|
return std::make_unique<BBLFileTransferJob>(params_json);
|
|
}
|
|
|
|
} // namespace Slic3r
|