Prusa Slicer 2.6.0
Loading...
Searching...
No Matches
Slic3r::Utils Namespace Reference

Namespaces

namespace  __get_put_time_emulation
 
namespace  anonymous_namespace{Time.cpp}
 

Classes

struct  HexFile
 
class  Serial
 
struct  SerialPortInfo
 
class  TCPConsole
 

Enumerations

enum class  TimeZone { local , utc }
 
enum class  TimeFormat { gcode , iso8601Z }
 

Functions

static const char * get_fmtstr (TimeFormat fmt)
 
time_t get_current_time_utc ()
 
static std::string tm2str (const std::tm *tms, const char *fmt)
 
std::string time2str (const time_t &t, TimeZone zone, TimeFormat fmt)
 
static time_t str2time (std::istream &stream, TimeZone zone, const char *fmt)
 
time_t str2time (const std::string &str, TimeZone zone, TimeFormat fmt)
 
std::string time2str (TimeZone zone, TimeFormat fmt)
 
std::string utc_timestamp (time_t t)
 
std::string utc_timestamp ()
 
std::string iso_utc_timestamp (time_t t)
 
std::string iso_utc_timestamp ()
 
time_t parse_iso_utc_timestamp (const std::string &str)
 
static HexFile::DeviceKind parse_device_kind (const std::string &str)
 
static size_t hex_num_sections (fs::ifstream &file)
 
static bool looks_like_printer (const std::string &friendly_name)
 
std::vector< SerialPortInfoscan_serial_ports_extended ()
 
std::vector< std::string > scan_serial_ports ()
 
bool operator== (const SerialPortInfo &sp1, const SerialPortInfo &sp2)
 

Variables

static const constexpr char *const SLICER_UTC_TIME_FMT = "%Y-%m-%d at %T"
 
static const constexpr char *const ISO8601Z_TIME_FMT = "%Y%m%dT%H%M%SZ"
 

Enumeration Type Documentation

◆ TimeFormat

enum class Slic3r::Utils::TimeFormat
strong
Enumerator
gcode 
iso8601Z 

◆ TimeZone

enum class Slic3r::Utils::TimeZone
strong
Enumerator
local 
utc 

Function Documentation

◆ get_current_time_utc()

time_t Slic3r::Utils::get_current_time_utc ( )
176{
177 using clk = std::chrono::system_clock;
178 return clk::to_time_t(clk::now());
179}

Referenced by Slic3r::_3MF_Exporter::_add_model_file_to_archive(), iso_utc_timestamp(), Slic3r::GUI::Config::SnapshotDB::take_snapshot(), time2str(), and utc_timestamp().

+ Here is the caller graph for this function:

◆ get_fmtstr()

static const char * Slic3r::Utils::get_fmtstr ( TimeFormat  fmt)
static
28{
29 switch (fmt) {
30 case TimeFormat::gcode: return SLICER_UTC_TIME_FMT;
31 case TimeFormat::iso8601Z: return ISO8601Z_TIME_FMT;
32 }
33
34 return "";
35}
static const constexpr char *const SLICER_UTC_TIME_FMT
Definition Time.cpp:22
static const constexpr char *const ISO8601Z_TIME_FMT
Definition Time.cpp:25

References gcode, iso8601Z, ISO8601Z_TIME_FMT, and SLICER_UTC_TIME_FMT.

Referenced by str2time(), and time2str().

+ Here is the caller graph for this function:

◆ hex_num_sections()

static size_t Slic3r::Utils::hex_num_sections ( fs::ifstream &  file)
static
27{
28 file.seekg(0);
29 if (! file.good()) {
30 return 0;
31 }
32
33 static const char *hex_terminator = ":00000001FF\r";
34 size_t res = 0;
35 std::string line;
36 while (getline(file, line, '\n').good()) {
37 // Account for LF vs CRLF
38 if (!line.empty() && line.back() != '\r') {
39 line.push_back('\r');
40 }
41
42 if (line == hex_terminator) {
43 res++;
44 }
45 }
46
47 return res;
48}

◆ iso_utc_timestamp() [1/2]

std::string Slic3r::Utils::iso_utc_timestamp ( )
inline
53{
55}
std::string iso_utc_timestamp(time_t t)
Definition Time.hpp:47
time_t get_current_time_utc()
Definition Time.cpp:175

References get_current_time_utc(), and iso_utc_timestamp().

Referenced by iso_utc_timestamp(), Slic3r::GUI::Config::Snapshot::save_ini(), and Slic3r::GUI::Config::SnapshotDB::take_snapshot().

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

◆ iso_utc_timestamp() [2/2]

std::string Slic3r::Utils::iso_utc_timestamp ( time_t  t)
inline
48{
49 return time2str(t, TimeZone::utc, TimeFormat::iso8601Z);
50}
std::string time2str(const time_t &t, TimeZone zone, TimeFormat fmt)
Definition Time.cpp:189

References iso8601Z, time2str(), and utc.

+ Here is the call graph for this function:

◆ looks_like_printer()

static bool Slic3r::Utils::looks_like_printer ( const std::string &  friendly_name)
static
66{
67 return friendly_name.find("Original Prusa") != std::string::npos;
68}

Referenced by scan_serial_ports_extended().

+ Here is the caller graph for this function:

◆ operator==()

bool Slic3r::Utils::operator== ( const SerialPortInfo sp1,
const SerialPortInfo sp2 
)
inline
27{
28 return
29 sp1.port == sp2.port &&
30 sp1.id_vendor == sp2.id_vendor &&
31 sp1.id_product == sp2.id_product &&
32 sp1.is_printer == sp2.is_printer;
33}
std::string port
Definition Serial.hpp:14
unsigned id_product
Definition Serial.hpp:16
bool is_printer
Definition Serial.hpp:18
unsigned id_vendor
Definition Serial.hpp:15

References Slic3r::Utils::SerialPortInfo::id_product, Slic3r::Utils::SerialPortInfo::id_vendor, Slic3r::Utils::SerialPortInfo::is_printer, and Slic3r::Utils::SerialPortInfo::port.

◆ parse_device_kind()

static HexFile::DeviceKind Slic3r::Utils::parse_device_kind ( const std::string &  str)
static
17{
18 if (str == "mk2") { return HexFile::DEV_MK2; }
19 else if (str == "mk3") { return HexFile::DEV_MK3; }
20 else if (str == "mm-control") { return HexFile::DEV_MM_CONTROL; }
21 else if (str == "cw1") { return HexFile::DEV_CW1; }
22 else if (str == "cw1s") { return HexFile::DEV_CW1S; }
23 else { return HexFile::DEV_GENERIC; }
24}

References Slic3r::Utils::HexFile::DEV_CW1, Slic3r::Utils::HexFile::DEV_CW1S, Slic3r::Utils::HexFile::DEV_GENERIC, Slic3r::Utils::HexFile::DEV_MK2, Slic3r::Utils::HexFile::DEV_MK3, and Slic3r::Utils::HexFile::DEV_MM_CONTROL.

◆ parse_iso_utc_timestamp()

time_t Slic3r::Utils::parse_iso_utc_timestamp ( const std::string &  str)
inline
58{
59 return str2time(str, TimeZone::utc, TimeFormat::iso8601Z);
60}
time_t str2time(const std::string &str, TimeZone zone, TimeFormat fmt)
Definition Time.cpp:224

References iso8601Z, str2time(), and utc.

Referenced by Slic3r::GUI::Config::Snapshot::load_ini().

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

◆ scan_serial_ports()

std::vector< std::string > Slic3r::Utils::scan_serial_ports ( )
265{
266 std::vector<SerialPortInfo> ports = scan_serial_ports_extended();
267 std::vector<std::string> output;
268 output.reserve(ports.size());
269 for (const SerialPortInfo &spi : ports)
270 output.emplace_back(std::move(spi.port));
271 return output;
272}
std::vector< SerialPortInfo > scan_serial_ports_extended()
Definition Serial.cpp:107
STL namespace.
Definition Serial.hpp:13

References Slic3r::Utils::SerialPortInfo::port, and scan_serial_ports_extended().

+ Here is the call graph for this function:

◆ scan_serial_ports_extended()

std::vector< SerialPortInfo > Slic3r::Utils::scan_serial_ports_extended ( )
108{
109 std::vector<SerialPortInfo> output;
110
111#ifdef _WIN32
112 SP_DEVINFO_DATA devInfoData = { 0 };
113 devInfoData.cbSize = sizeof(devInfoData);
114 // Get the tree containing the info for the ports.
115 HDEVINFO hDeviceInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_PORTS, 0, nullptr, DIGCF_PRESENT);
116 if (hDeviceInfo != INVALID_HANDLE_VALUE) {
117 // Iterate over all the devices in the tree.
118 for (int nDevice = 0; SetupDiEnumDeviceInfo(hDeviceInfo, nDevice, &devInfoData); ++ nDevice) {
119 SerialPortInfo port_info;
120 // Get the registry key which stores the ports settings.
121 HKEY hDeviceKey = SetupDiOpenDevRegKey(hDeviceInfo, &devInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE);
122 if (hDeviceKey) {
123 // Read in the name of the port.
124 wchar_t pszPortName[4096];
125 DWORD dwSize = sizeof(pszPortName);
126 DWORD dwType = 0;
127 if (RegQueryValueEx(hDeviceKey, L"PortName", NULL, &dwType, (LPBYTE)pszPortName, &dwSize) == ERROR_SUCCESS)
128 port_info.port = boost::nowide::narrow(pszPortName);
129 RegCloseKey(hDeviceKey);
130 if (port_info.port.empty())
131 continue;
132 }
133
134 // Find the size required to hold the device info.
135 DWORD regDataType;
136 DWORD reqSize = 0;
137 SetupDiGetDeviceRegistryProperty(hDeviceInfo, &devInfoData, SPDRP_HARDWAREID, nullptr, nullptr, 0, &reqSize);
138 std::vector<wchar_t> hardware_id(reqSize > 1 ? reqSize : 1);
139 // Now store it in a buffer.
140 if (! SetupDiGetDeviceRegistryProperty(hDeviceInfo, &devInfoData, SPDRP_HARDWAREID, &regDataType, (BYTE*)hardware_id.data(), reqSize, nullptr))
141 continue;
142 parse_hardware_id(boost::nowide::narrow(hardware_id.data()), port_info);
143
144 // Find the size required to hold the friendly name.
145 reqSize = 0;
146 SetupDiGetDeviceRegistryProperty(hDeviceInfo, &devInfoData, SPDRP_FRIENDLYNAME, nullptr, nullptr, 0, &reqSize);
147 std::vector<wchar_t> friendly_name;
148 friendly_name.reserve(reqSize > 1 ? reqSize : 1);
149 // Now store it in a buffer.
150 if (! SetupDiGetDeviceRegistryProperty(hDeviceInfo, &devInfoData, SPDRP_FRIENDLYNAME, nullptr, (BYTE*)friendly_name.data(), reqSize, nullptr)) {
151 port_info.friendly_name = port_info.port;
152 } else {
153 port_info.friendly_name = boost::nowide::narrow(friendly_name.data());
154 port_info.is_printer = looks_like_printer(port_info.friendly_name);
155 }
156 output.emplace_back(std::move(port_info));
157 }
158 }
159#elif __APPLE__
160 // inspired by https://sigrok.org/wiki/Libserialport
161 CFMutableDictionaryRef classes = IOServiceMatching(kIOSerialBSDServiceValue);
162 if (classes != 0) {
163 io_iterator_t iter;
164 if (IOServiceGetMatchingServices(kIOMasterPortDefault, classes, &iter) == KERN_SUCCESS) {
165 io_object_t port;
166 while ((port = IOIteratorNext(iter)) != 0) {
167 CFTypeRef cf_property = IORegistryEntryCreateCFProperty(port, CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
168 if (cf_property) {
169 char path[PATH_MAX];
170 Boolean result = CFStringGetCString((CFStringRef)cf_property, path, sizeof(path), kCFStringEncodingUTF8);
171 CFRelease(cf_property);
172 if (result) {
173 SerialPortInfo port_info;
174 port_info.port = path;
175
176 // Attempt to read out the device friendly name
177 if ((cf_property = IORegistryEntrySearchCFProperty(port, kIOServicePlane,
178 CFSTR("USB Interface Name"), kCFAllocatorDefault,
179 kIORegistryIterateRecursively | kIORegistryIterateParents)) ||
180 (cf_property = IORegistryEntrySearchCFProperty(port, kIOServicePlane,
181 CFSTR("USB Product Name"), kCFAllocatorDefault,
182 kIORegistryIterateRecursively | kIORegistryIterateParents)) ||
183 (cf_property = IORegistryEntrySearchCFProperty(port, kIOServicePlane,
184 CFSTR("Product Name"), kCFAllocatorDefault,
185 kIORegistryIterateRecursively | kIORegistryIterateParents)) ||
186 (cf_property = IORegistryEntryCreateCFProperty(port,
187 CFSTR(kIOTTYDeviceKey), kCFAllocatorDefault, 0))) {
188 // Description limited to 127 char, anything longer would not be user friendly anyway.
189 char description[128];
190 if (CFStringGetCString((CFStringRef)cf_property, description, sizeof(description), kCFStringEncodingUTF8)) {
191 port_info.friendly_name = std::string(description) + " (" + port_info.port + ")";
192 port_info.is_printer = looks_like_printer(port_info.friendly_name);
193 }
194 CFRelease(cf_property);
195 }
196 if (port_info.friendly_name.empty())
197 port_info.friendly_name = port_info.port;
198
199 // Attempt to read out the VID & PID
200 int vid, pid;
201 auto cf_vendor = IORegistryEntrySearchCFProperty(port, kIOServicePlane, CFSTR("idVendor"),
202 kCFAllocatorDefault, kIORegistryIterateRecursively | kIORegistryIterateParents);
203 auto cf_product = IORegistryEntrySearchCFProperty(port, kIOServicePlane, CFSTR("idProduct"),
204 kCFAllocatorDefault, kIORegistryIterateRecursively | kIORegistryIterateParents);
205 if (cf_vendor && cf_product) {
206 if (CFNumberGetValue((CFNumberRef)cf_vendor, kCFNumberIntType, &vid) &&
207 CFNumberGetValue((CFNumberRef)cf_product, kCFNumberIntType, &pid)) {
208 port_info.id_vendor = vid;
209 port_info.id_product = pid;
210 }
211 }
212 if (cf_vendor) { CFRelease(cf_vendor); }
213 if (cf_product) { CFRelease(cf_product); }
214
215 output.emplace_back(std::move(port_info));
216 }
217 }
218 IOObjectRelease(port);
219 }
220 }
221 }
222#else
223 // UNIX / Linux
224 std::initializer_list<const char*> prefixes { "ttyUSB" , "ttyACM", "tty.", "cu.", "rfcomm" };
225 for (auto &dir_entry : boost::filesystem::directory_iterator(boost::filesystem::path("/dev"))) {
226 std::string name = dir_entry.path().filename().string();
227 for (const char *prefix : prefixes) {
228 if (boost::starts_with(name, prefix)) {
229 const auto path = dir_entry.path().string();
230 SerialPortInfo spi;
231 spi.port = path;
232#ifdef __linux__
233 auto friendly_name = sysfs_tty_prop(name, "product");
234 if (friendly_name) {
235 spi.is_printer = looks_like_printer(*friendly_name);
236 spi.friendly_name = (boost::format("%1% (%2%)") % *friendly_name % path).str();
237 } else {
238 spi.friendly_name = path;
239 }
240 auto vid = sysfs_tty_prop_hex(name, "idVendor");
241 auto pid = sysfs_tty_prop_hex(name, "idProduct");
242 if (vid && pid) {
243 spi.id_vendor = *vid;
244 spi.id_product = *pid;
245 }
246#else
247 spi.friendly_name = path;
248#endif
249 output.emplace_back(std::move(spi));
250 break;
251 }
252 }
253 }
254#endif
255
256 output.erase(std::remove_if(output.begin(), output.end(),
257 [](const SerialPortInfo &info) {
258 return boost::starts_with(info.port, "Bluetooth") || boost::starts_with(info.port, "FireFly");
259 }),
260 output.end());
261 return output;
262}
#define PATH_MAX
Definition libavrdude.h:51
static bool looks_like_printer(const std::string &friendly_name)
Definition Serial.cpp:65
char * narrow(char *output, size_t output_size, wchar_t const *source)
Convert NULL terminated UTF source string to NULL terminated output string of size at most output_siz...
Definition convert.hpp:73
Definition args.hpp:18
#define L(s)
Definition I18N.hpp:18
std::string friendly_name
Definition Serial.hpp:17

References Slic3r::Utils::SerialPortInfo::friendly_name, Slic3r::Utils::SerialPortInfo::id_product, Slic3r::Utils::SerialPortInfo::id_vendor, Slic3r::Utils::SerialPortInfo::is_printer, L, looks_like_printer(), boost::nowide::narrow(), PATH_MAX, and Slic3r::Utils::SerialPortInfo::port.

Referenced by Slic3r::FirmwareDialog::priv::avr109_lookup_port(), Slic3r::FirmwareDialog::priv::avr109_wait_for_bootloader(), Slic3r::FirmwareDialog::priv::find_serial_ports(), and scan_serial_ports().

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

◆ str2time() [1/2]

time_t Slic3r::Utils::str2time ( const std::string &  str,
TimeZone  zone,
TimeFormat  fmt 
)
225{
226 std::string fmtstr = process_format(get_fmtstr(fmt), zone).c_str();
227 std::stringstream ss(str);
228
229 ss.imbue(std::locale("C"));
230 return str2time(ss, zone, fmtstr.c_str());
231}
char * fmtstr(FILEFMT format)
Definition fileio.c:166
static const char * get_fmtstr(TimeFormat fmt)
Definition Time.cpp:27
static time_t str2time(std::istream &stream, TimeZone zone, const char *fmt)
Definition Time.cpp:206

References fmtstr(), get_fmtstr(), and str2time().

+ Here is the call graph for this function:

◆ str2time() [2/2]

static time_t Slic3r::Utils::str2time ( std::istream &  stream,
TimeZone  zone,
const char *  fmt 
)
static
207{
208 std::tm tms = {};
209 tms.tm_isdst = -1;
210
211 stream >> __get_put_time_emulation::get_time(&tms, fmt);
212 time_t ret = time_t(-1);
213
214 switch (zone) {
215 case TimeZone::local: ret = _mktime(&tms); break;
216 case TimeZone::utc: ret = _timegm(&tms); break;
217 }
218
219 if (stream.fail() || ret < time_t(0)) ret = time_t(-1);
220
221 return ret;
222}

References Slic3r::Utils::__get_put_time_emulation::get_time(), local, and utc.

Referenced by parse_iso_utc_timestamp(), and str2time().

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

◆ time2str() [1/2]

std::string Slic3r::Utils::time2str ( const time_t &  t,
TimeZone  zone,
TimeFormat  fmt 
)
190{
191 std::string ret;
192 std::tm tms = {};
193 tms.tm_isdst = -1;
194 std::string fmtstr = process_format(get_fmtstr(fmt), zone);
195
196 switch (zone) {
197 case TimeZone::local:
198 ret = tm2str(_localtime_r(&t, &tms), fmtstr.c_str()); break;
199 case TimeZone::utc:
200 ret = tm2str(_gmtime_r(&t, &tms), fmtstr.c_str()); break;
201 }
202
203 return ret;
204}

References fmtstr(), get_fmtstr(), local, tm2str(), and utc.

Referenced by iso_utc_timestamp(), time2str(), and utc_timestamp().

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

◆ time2str() [2/2]

std::string Slic3r::Utils::time2str ( TimeZone  zone,
TimeFormat  fmt 
)
inline
21{
22 return time2str(get_current_time_utc(), zone, fmt);
23}

References get_current_time_utc(), and time2str().

+ Here is the call graph for this function:

◆ tm2str()

static std::string Slic3r::Utils::tm2str ( const std::tm *  tms,
const char *  fmt 
)
static
182{
183 std::stringstream ss;
184 ss.imbue(std::locale("C"));
185 ss << __get_put_time_emulation::put_time(tms, fmt);
186 return ss.str();
187}

References Slic3r::Utils::__get_put_time_emulation::put_time().

Referenced by time2str().

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

◆ utc_timestamp() [1/2]

std::string Slic3r::Utils::utc_timestamp ( )
inline
31{
33}
std::string utc_timestamp(time_t t)
Definition Time.hpp:25

References get_current_time_utc(), and utc_timestamp().

Referenced by Slic3r::_3MF_Exporter::_add_model_file_to_archive(), Slic3r::anonymous_namespace{SL1.cpp}::fill_iniconf(), Slic3r::header_gcodeviewer_generated(), Slic3r::header_slic3r_generated(), and utc_timestamp().

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

◆ utc_timestamp() [2/2]

std::string Slic3r::Utils::utc_timestamp ( time_t  t)
inline
26{
27 return time2str(t, TimeZone::utc, TimeFormat::gcode);
28}

References gcode, time2str(), and utc.

+ Here is the call graph for this function:

Variable Documentation

◆ ISO8601Z_TIME_FMT

const constexpr char* const Slic3r::Utils::ISO8601Z_TIME_FMT = "%Y%m%dT%H%M%SZ"
staticconstexpr

Referenced by get_fmtstr().

◆ SLICER_UTC_TIME_FMT

const constexpr char* const Slic3r::Utils::SLICER_UTC_TIME_FMT = "%Y-%m-%d at %T"
staticconstexpr