Files
OrcaSlicer-bambulab/src/avrdude/avrdude-slic3r.cpp
T

251 lines
4.8 KiB
C++
Raw Normal View History

2018-04-19 10:35:47 +02:00
#include "avrdude-slic3r.hpp"
#include <deque>
#include <thread>
2018-08-07 11:28:39 +02:00
#include <cstring>
#include <cstdlib>
#include <new>
#include <exception>
2018-04-19 10:35:47 +02:00
extern "C" {
#include "ac_cfg.h"
#include "avrdude.h"
}
2018-05-04 16:04:43 +02:00
2018-04-19 10:35:47 +02:00
namespace Slic3r {
// C callbacks
2018-05-04 16:04:43 +02:00
// Used by our custom code in avrdude to receive messages that avrdude normally outputs on stdout (see avrdude_message())
static void avrdude_message_handler_closure(const char *msg, unsigned size, void *user_p)
2018-04-19 10:35:47 +02:00
{
2018-05-18 15:38:33 +02:00
auto *message_fn = reinterpret_cast<AvrDude::MessageFn*>(user_p);
2018-05-04 16:04:43 +02:00
(*message_fn)(msg, size);
2018-04-19 10:35:47 +02:00
}
2018-05-18 15:38:33 +02:00
// Used by our custom code in avrdude to report progress in the GUI
2018-05-21 15:24:24 +02:00
static void avrdude_progress_handler_closure(const char *task, unsigned progress, void *user_p)
2018-05-18 15:38:33 +02:00
{
auto *progress_fn = reinterpret_cast<AvrDude::ProgressFn*>(user_p);
2018-05-21 15:24:24 +02:00
(*progress_fn)(task, progress);
2018-05-18 15:38:33 +02:00
}
2018-08-07 11:28:39 +02:00
static void avrdude_oom_handler(const char *context, void *user_p)
{
throw std::bad_alloc();
}
2018-05-18 15:38:33 +02:00
// Private
2018-05-18 15:38:33 +02:00
struct AvrDude::priv
2018-05-18 15:38:33 +02:00
{
std::deque<std::vector<std::string>> args;
bool cancelled = false;
2018-07-27 11:55:11 +02:00
int exit_code = 0;
size_t current_args_set = 0;
2018-06-05 11:55:23 +02:00
RunFn run_fn;
MessageFn message_fn;
ProgressFn progress_fn;
CompleteFn complete_fn;
2018-05-18 15:38:33 +02:00
std::thread avrdude_thread;
2018-05-18 15:38:33 +02:00
2018-08-07 11:28:39 +02:00
void set_handlers();
void unset_handlers();
int run_one(const std::vector<std::string> &args);
int run();
2018-08-07 11:28:39 +02:00
struct HandlerGuard
{
priv &p;
HandlerGuard(priv &p) : p(p) { p.set_handlers(); }
~HandlerGuard() { p.unset_handlers(); }
};
};
2018-05-18 15:38:33 +02:00
2018-08-07 11:28:39 +02:00
void AvrDude::priv::set_handlers()
{
if (message_fn) {
::avrdude_message_handler_set(avrdude_message_handler_closure, reinterpret_cast<void*>(&message_fn));
2018-05-18 15:38:33 +02:00
} else {
::avrdude_message_handler_set(nullptr, nullptr);
}
if (progress_fn) {
::avrdude_progress_handler_set(avrdude_progress_handler_closure, reinterpret_cast<void*>(&progress_fn));
2018-05-18 15:38:33 +02:00
} else {
::avrdude_progress_handler_set(nullptr, nullptr);
}
2018-08-07 11:28:39 +02:00
::avrdude_oom_handler_set(avrdude_oom_handler, nullptr);
}
2018-08-07 11:28:39 +02:00
void AvrDude::priv::unset_handlers()
{
2018-04-19 10:35:47 +02:00
::avrdude_message_handler_set(nullptr, nullptr);
2018-05-18 15:38:33 +02:00
::avrdude_progress_handler_set(nullptr, nullptr);
2018-08-07 11:28:39 +02:00
::avrdude_oom_handler_set(nullptr, nullptr);
}
int AvrDude::priv::run_one(const std::vector<std::string> &args) {
2020-01-03 16:32:56 +01:00
std::vector<const char*> c_args { PACKAGE };
2019-02-22 10:12:38 +01:00
std::string command_line { PACKAGE };
2018-08-07 11:28:39 +02:00
for (const auto &arg : args) {
2020-01-03 16:32:56 +01:00
c_args.push_back(arg.c_str());
2019-02-22 10:12:38 +01:00
command_line.push_back(' ');
command_line.append(arg);
2018-08-07 11:28:39 +02:00
}
2019-02-22 10:12:38 +01:00
command_line.push_back('\n');
2018-08-07 11:28:39 +02:00
HandlerGuard guard(*this);
2019-06-25 14:50:27 +02:00
message_fn(command_line.c_str(), (unsigned)command_line.size());
2019-02-22 10:12:38 +01:00
2020-01-03 16:32:56 +01:00
const auto res = ::avrdude_main(static_cast<int>(c_args.size()), const_cast<char**>(c_args.data()));
2018-08-07 11:28:39 +02:00
2018-04-19 10:35:47 +02:00
return res;
}
int AvrDude::priv::run() {
for (; args.size() > 0; current_args_set++) {
int res = run_one(args.front());
args.pop_front();
if (res != 0) {
return res;
}
}
return 0;
}
2018-04-19 10:35:47 +02:00
// Public
AvrDude::AvrDude() : p(new priv()) {}
2018-05-18 18:37:16 +02:00
AvrDude::AvrDude(AvrDude &&other) : p(std::move(other.p)) {}
AvrDude::~AvrDude()
{
if (p && p->avrdude_thread.joinable()) {
p->avrdude_thread.detach();
}
}
AvrDude& AvrDude::push_args(std::vector<std::string> args)
{
if (p) { p->args.push_back(std::move(args)); }
return *this;
}
2018-06-05 11:55:23 +02:00
AvrDude& AvrDude::on_run(RunFn fn)
{
if (p) { p->run_fn = std::move(fn); }
return *this;
}
AvrDude& AvrDude::on_message(MessageFn fn)
{
if (p) { p->message_fn = std::move(fn); }
return *this;
}
2018-05-18 18:37:16 +02:00
AvrDude& AvrDude::on_progress(ProgressFn fn)
{
if (p) { p->progress_fn = std::move(fn); }
return *this;
}
AvrDude& AvrDude::on_complete(CompleteFn fn)
{
if (p) { p->complete_fn = std::move(fn); }
return *this;
}
int AvrDude::run_sync()
{
2018-08-07 11:28:39 +02:00
return p ? p->run() : -1;
}
AvrDude::Ptr AvrDude::run()
{
auto self = std::make_shared<AvrDude>(std::move(*this));
if (self->p) {
auto avrdude_thread = std::thread([self]() {
2018-08-07 11:28:39 +02:00
try {
if (self->p->run_fn) {
self->p->run_fn(self);
}
2018-08-07 11:28:39 +02:00
if (! self->p->cancelled) {
self->p->exit_code = self->p->run();
}
2018-06-05 11:55:23 +02:00
2018-08-07 11:28:39 +02:00
if (self->p->complete_fn) {
self->p->complete_fn();
}
} catch (const std::exception &ex) {
self->p->exit_code = EXIT_EXCEPTION;
2018-06-05 11:55:23 +02:00
2018-08-07 11:28:39 +02:00
static const char *msg = "An exception was thrown in the background thread:\n";
const char *what = ex.what();
auto &message_fn = self->p->message_fn;
if (message_fn) {
message_fn(msg, sizeof(msg));
2019-06-25 14:50:27 +02:00
message_fn(what, (unsigned)std::strlen(what));
2018-08-07 11:28:39 +02:00
message_fn("\n", 1);
}
if (self->p->complete_fn) {
self->p->complete_fn();
}
2018-06-05 11:55:23 +02:00
}
});
self->p->avrdude_thread = std::move(avrdude_thread);
}
return self;
}
2018-05-21 15:24:24 +02:00
void AvrDude::cancel()
{
if (p) {
p->cancelled = true;
::avrdude_cancel();
}
2018-05-21 15:24:24 +02:00
}
void AvrDude::join()
{
if (p && p->avrdude_thread.joinable()) {
p->avrdude_thread.join();
}
}
2018-07-27 11:55:11 +02:00
bool AvrDude::cancelled()
{
return p ? p->cancelled : false;
}
int AvrDude::exit_code()
{
return p ? p->exit_code : 0;
}
size_t AvrDude::last_args_set()
{
return p ? p->current_args_set : 0;
}
2018-04-19 10:35:47 +02:00
}