OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::anonymous_namespace{Flashforge.cpp} Namespace Reference

Functions

std::string trim_null_terminated_ascii (const char *data, size_t len)
bool parse_discovery_response (const std::vector< unsigned char > &response, const std::string &ip_address, FlashforgeDiscoveredPrinter &printer)
std::vector< std::string > get_discovery_broadcast_addresses ()
std::string safe_config_string (DynamicPrintConfig *config, const char *key)
bool try_parse_json_int (const json &value, int &out)
bool validate_local_api_response (const std::string &response_body, wxString &error_msg)
std::string sanitize_flashforge_filename (const std::string &filename, const std::string &fallback_extension={})

Variables

constexpr unsigned short FLASHFORGE_DISCOVERY_PORT = 48899
constexpr unsigned short FLASHFORGE_DISCOVERY_LISTEN_PORT = 18007
const std::array< unsigned char, 20 > FLASHFORGE_DISCOVERY_MESSAGE

Function Documentation

◆ get_discovery_broadcast_addresses()

std::vector< std::string > Slic3r::anonymous_namespace{Flashforge.cpp}::get_discovery_broadcast_addresses ( )
93{
94 std::set<std::string> addresses = {"255.255.255.255", "192.168.0.255", "192.168.1.255"};
95
96 try {
97 boost::asio::io_context io_context;
98 boost::asio::ip::tcp::resolver resolver(io_context);
99 boost::system::error_code ec;
100 const auto host_name = boost::asio::ip::host_name(ec);
101 if (!ec) {
102 const auto results = resolver.resolve(boost::asio::ip::tcp::v4(), host_name, "", ec);
103 if (!ec) {
104 for (const auto& entry : results) {
105 const auto addr = entry.endpoint().address();
106 if (!addr.is_v4())
107 continue;
108
109 const auto bytes = addr.to_v4().to_bytes();
110 if (bytes[0] == 127)
111 continue;
112
113 addresses.insert((boost::format("%1%.%2%.%3%.255") % static_cast<int>(bytes[0]) % static_cast<int>(bytes[1]) % static_cast<int>(bytes[2])).str());
114 }
115 }
116 }
117 } catch (...) {
118 }
119
120 return {addresses.begin(), addresses.end()};
121}

References get_discovery_broadcast_addresses().

Referenced by get_discovery_broadcast_addresses().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_discovery_response()

bool Slic3r::anonymous_namespace{Flashforge.cpp}::parse_discovery_response ( const std::vector< unsigned char > & response,
const std::string & ip_address,
FlashforgeDiscoveredPrinter & printer )
82{
83 if (response.size() < 0xC4)
84 return false;
85
86 printer.name = trim_null_terminated_ascii(reinterpret_cast<const char*>(response.data()), 32);
87 printer.serial_number = trim_null_terminated_ascii(reinterpret_cast<const char*>(response.data() + 0x92), 32);
88 printer.ip_address = ip_address;
89 return !(printer.name.empty() && printer.serial_number.empty());
90}
std::string trim_null_terminated_ascii(const char *data, size_t len)
Definition Flashforge.cpp:71
std::string serial_number
Definition Flashforge.hpp:28
std::string ip_address
Definition Flashforge.hpp:29
std::string name
Definition Flashforge.hpp:27

References Slic3r::FlashforgeDiscoveredPrinter::ip_address, Slic3r::FlashforgeDiscoveredPrinter::name, parse_discovery_response(), Slic3r::FlashforgeDiscoveredPrinter::serial_number, and trim_null_terminated_ascii().

Referenced by parse_discovery_response().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ safe_config_string()

std::string Slic3r::anonymous_namespace{Flashforge.cpp}::safe_config_string ( DynamicPrintConfig * config,
const char * key )
124{
125 if (config == nullptr)
126 return {};
127
128 if (const auto* opt = config->option<ConfigOptionString>(key); opt != nullptr)
129 return opt->value;
130
131 return {};
132}
ConfigOption * option(const t_config_option_key &opt_key, bool create=false)
Definition Config.hpp:2768
Definition Config.hpp:1158
Definition BruteforceOptimizer.hpp:11

References Slic3r::ConfigBase::option(), and safe_config_string().

Referenced by safe_config_string().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sanitize_flashforge_filename()

std::string Slic3r::anonymous_namespace{Flashforge.cpp}::sanitize_flashforge_filename ( const std::string & filename,
const std::string & fallback_extension = {} )
200 {})
201{
202 std::string basename = fs::path(filename).filename().string();
203 if (basename.empty()) {
204 basename = "print";
205 if (!fallback_extension.empty())
206 basename += fallback_extension;
207 }
208
209 for (char& ch : basename) {
210 const bool is_ascii_alnum = (ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
211 if (!is_ascii_alnum && ch != '.' && ch != '_' && ch != '-') {
212 ch = '_';
213 }
214 }
215
216 return basename;
217}

References sanitize_flashforge_filename().

Referenced by sanitize_flashforge_filename().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ trim_null_terminated_ascii()

std::string Slic3r::anonymous_namespace{Flashforge.cpp}::trim_null_terminated_ascii ( const char * data,
size_t len )
72{
73 std::string out(data, data + len);
74 const auto pos = out.find('\0');
75 if (pos != std::string::npos)
76 out.resize(pos);
77 boost::trim(out);
78 return out;
79}

References trim_null_terminated_ascii().

Referenced by parse_discovery_response(), and trim_null_terminated_ascii().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_parse_json_int()

bool Slic3r::anonymous_namespace{Flashforge.cpp}::try_parse_json_int ( const json & value,
int & out )
135{
136 try {
137 if (value.is_number_integer() || value.is_number_unsigned()) {
138 out = value.get<int>();
139 return true;
140 }
141
142 if (value.is_boolean()) {
143 out = value.get<bool>() ? 1 : 0;
144 return true;
145 }
146
147 if (value.is_string()) {
148 std::string text = value.get<std::string>();
149 boost::trim(text);
150 if (text.empty())
151 return false;
152
153 size_t pos = 0;
154 const long parsed = std::stol(text, &pos, 10);
155 if (pos == text.size()) {
156 out = static_cast<int>(parsed);
157 return true;
158 }
159 }
160 } catch (...) {
161 }
162
163 return false;
164}

References try_parse_json_int().

Referenced by try_parse_json_int(), and validate_local_api_response().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ validate_local_api_response()

bool Slic3r::anonymous_namespace{Flashforge.cpp}::validate_local_api_response ( const std::string & response_body,
wxString & error_msg )
167{
168 const auto parsed = json::parse(response_body, nullptr, false, true);
169 if (parsed.is_discarded() || !parsed.is_object()) {
170 error_msg = _(L("Flashforge returned an invalid JSON response."));
171 return false;
172 }
173
174 int result_code = 0;
175 bool has_code = false;
176
177 if (parsed.contains("code"))
178 has_code = try_parse_json_int(parsed["code"], result_code);
179 if (!has_code && parsed.contains("err"))
180 has_code = try_parse_json_int(parsed["err"], result_code);
181
182 if (has_code && result_code != 0) {
183 std::string message;
184 if (parsed.contains("message") && parsed["message"].is_string())
185 message = parsed["message"].get<std::string>();
186 else if (parsed.contains("msg") && parsed["msg"].is_string())
187 message = parsed["msg"].get<std::string>();
188
189 if (message.empty())
190 message = _u8L("Request failed");
191
192 // TRN %1% is the error code returned by the printer, %2% its error message.
193 error_msg = GUI::from_u8((boost::format(_u8L("Flashforge local API error %1%: %2%")) % result_code % message).str());
194 return false;
195 }
196
197 return true;
198}
#define _(s)
Definition 3mf.cpp:306
#define L(s)
Definition ExtrusionEntity.cpp:25
wxString from_u8(const std::string &str)
Definition GUI.cpp:544
bool try_parse_json_int(const json &value, int &out)
Definition Flashforge.cpp:134
#define _u8L(s)
Definition I18N.hpp:11

References _, _u8L, Slic3r::GUI::from_u8(), L, try_parse_json_int(), and validate_local_api_response().

Referenced by validate_local_api_response().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ FLASHFORGE_DISCOVERY_LISTEN_PORT

unsigned short Slic3r::anonymous_namespace{Flashforge.cpp}::FLASHFORGE_DISCOVERY_LISTEN_PORT = 18007
constexpr

◆ FLASHFORGE_DISCOVERY_MESSAGE

const std::array<unsigned char, 20> Slic3r::anonymous_namespace{Flashforge.cpp}::FLASHFORGE_DISCOVERY_MESSAGE
Initial value:
= {
0x77, 0x77, 0x77, 0x2e, 0x75, 0x73, 0x72, 0x22,
0x65, 0x36, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
}
65 {
66 0x77, 0x77, 0x77, 0x2e, 0x75, 0x73, 0x72, 0x22,
67 0x65, 0x36, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
68 0x00, 0x00, 0x00, 0x00
69};

◆ FLASHFORGE_DISCOVERY_PORT

unsigned short Slic3r::anonymous_namespace{Flashforge.cpp}::FLASHFORGE_DISCOVERY_PORT = 48899
constexpr