2018-04-19 08:35:47 +00:00
|
|
|
#include "avrdude-slic3r.hpp"
|
|
|
|
|
2018-06-19 09:16:56 +00:00
|
|
|
#include <deque>
|
2018-05-18 15:35:24 +00:00
|
|
|
#include <thread>
|
|
|
|
|
2018-04-19 08:35:47 +00:00
|
|
|
extern "C" {
|
|
|
|
#include "ac_cfg.h"
|
|
|
|
#include "avrdude.h"
|
|
|
|
}
|
|
|
|
|
2018-05-04 14:04:43 +00:00
|
|
|
|
2018-04-19 08:35:47 +00:00
|
|
|
namespace Slic3r {
|
|
|
|
|
2018-05-18 15:35:24 +00:00
|
|
|
|
|
|
|
// C callbacks
|
|
|
|
|
2018-05-04 14:04:43 +00: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 08:35:47 +00:00
|
|
|
{
|
2018-05-18 13:38:33 +00:00
|
|
|
auto *message_fn = reinterpret_cast<AvrDude::MessageFn*>(user_p);
|
2018-05-04 14:04:43 +00:00
|
|
|
(*message_fn)(msg, size);
|
2018-04-19 08:35:47 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 13:38:33 +00:00
|
|
|
// Used by our custom code in avrdude to report progress in the GUI
|
2018-05-21 13:24:24 +00:00
|
|
|
static void avrdude_progress_handler_closure(const char *task, unsigned progress, void *user_p)
|
2018-05-18 13:38:33 +00:00
|
|
|
{
|
|
|
|
auto *progress_fn = reinterpret_cast<AvrDude::ProgressFn*>(user_p);
|
2018-05-21 13:24:24 +00:00
|
|
|
(*progress_fn)(task, progress);
|
2018-05-18 13:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-18 15:35:24 +00:00
|
|
|
// Private
|
|
|
|
|
|
|
|
struct AvrDude::priv
|
|
|
|
{
|
|
|
|
std::string sys_config;
|
2018-06-19 09:16:56 +00:00
|
|
|
std::deque<std::vector<std::string>> args;
|
|
|
|
size_t current_args_set = 0;
|
2018-06-05 09:55:23 +00:00
|
|
|
RunFn run_fn;
|
2018-05-18 15:35:24 +00:00
|
|
|
MessageFn message_fn;
|
|
|
|
ProgressFn progress_fn;
|
|
|
|
CompleteFn complete_fn;
|
|
|
|
|
|
|
|
std::thread avrdude_thread;
|
|
|
|
|
2018-06-19 09:16:56 +00:00
|
|
|
priv(std::string &&sys_config) : sys_config(sys_config) {}
|
|
|
|
|
|
|
|
int run_one(const std::vector<std::string> &args);
|
2018-05-18 15:35:24 +00:00
|
|
|
int run();
|
|
|
|
};
|
|
|
|
|
2018-06-19 09:16:56 +00:00
|
|
|
int AvrDude::priv::run_one(const std::vector<std::string> &args) {
|
2018-05-18 15:35:24 +00:00
|
|
|
std::vector<char*> c_args {{ const_cast<char*>(PACKAGE_NAME) }};
|
|
|
|
for (const auto &arg : args) {
|
|
|
|
c_args.push_back(const_cast<char*>(arg.data()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message_fn) {
|
|
|
|
::avrdude_message_handler_set(avrdude_message_handler_closure, reinterpret_cast<void*>(&message_fn));
|
|
|
|
} else {
|
|
|
|
::avrdude_message_handler_set(nullptr, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (progress_fn) {
|
|
|
|
::avrdude_progress_handler_set(avrdude_progress_handler_closure, reinterpret_cast<void*>(&progress_fn));
|
|
|
|
} else {
|
|
|
|
::avrdude_progress_handler_set(nullptr, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto res = ::avrdude_main(static_cast<int>(c_args.size()), c_args.data(), sys_config.c_str());
|
|
|
|
|
|
|
|
::avrdude_message_handler_set(nullptr, nullptr);
|
|
|
|
::avrdude_progress_handler_set(nullptr, nullptr);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-06-19 09:16:56 +00:00
|
|
|
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-05-18 15:35:24 +00:00
|
|
|
|
|
|
|
// Public
|
|
|
|
|
2018-06-19 09:16:56 +00:00
|
|
|
AvrDude::AvrDude(std::string sys_config) : p(new priv(std::move(sys_config))) {}
|
2018-05-18 15:35:24 +00:00
|
|
|
|
2018-05-18 16:37:16 +00:00
|
|
|
AvrDude::AvrDude(AvrDude &&other) : p(std::move(other.p)) {}
|
|
|
|
|
2018-05-18 15:35:24 +00:00
|
|
|
AvrDude::~AvrDude()
|
|
|
|
{
|
|
|
|
if (p && p->avrdude_thread.joinable()) {
|
|
|
|
p->avrdude_thread.detach();
|
|
|
|
}
|
|
|
|
}
|
2018-05-18 13:38:33 +00:00
|
|
|
|
2018-06-19 09:16:56 +00:00
|
|
|
AvrDude& AvrDude::push_args(std::vector<std::string> args)
|
2018-05-18 15:35:24 +00:00
|
|
|
{
|
2018-06-19 09:16:56 +00:00
|
|
|
if (p) { p->args.push_back(std::move(args)); }
|
2018-05-18 13:38:33 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-06-05 09:55:23 +00:00
|
|
|
AvrDude& AvrDude::on_run(RunFn fn)
|
|
|
|
{
|
|
|
|
if (p) { p->run_fn = std::move(fn); }
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-05-18 13:38:33 +00:00
|
|
|
AvrDude& AvrDude::on_message(MessageFn fn)
|
|
|
|
{
|
2018-05-18 15:35:24 +00:00
|
|
|
if (p) { p->message_fn = std::move(fn); }
|
2018-05-18 13:38:33 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-05-18 16:37:16 +00:00
|
|
|
AvrDude& AvrDude::on_progress(ProgressFn fn)
|
2018-05-18 13:38:33 +00:00
|
|
|
{
|
2018-05-18 15:35:24 +00:00
|
|
|
if (p) { p->progress_fn = std::move(fn); }
|
2018-05-18 13:38:33 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-05-18 15:35:24 +00:00
|
|
|
AvrDude& AvrDude::on_complete(CompleteFn fn)
|
2018-04-19 08:35:47 +00:00
|
|
|
{
|
2018-05-18 15:35:24 +00:00
|
|
|
if (p) { p->complete_fn = std::move(fn); }
|
|
|
|
return *this;
|
|
|
|
}
|
2018-04-19 08:35:47 +00:00
|
|
|
|
2018-05-18 15:35:24 +00:00
|
|
|
int AvrDude::run_sync()
|
|
|
|
{
|
|
|
|
return p->run();
|
|
|
|
}
|
|
|
|
|
|
|
|
AvrDude::Ptr AvrDude::run()
|
|
|
|
{
|
|
|
|
auto self = std::make_shared<AvrDude>(std::move(*this));
|
|
|
|
|
|
|
|
if (self->p) {
|
|
|
|
auto avrdude_thread = std::thread([self]() {
|
2018-06-05 09:55:23 +00:00
|
|
|
if (self->p->run_fn) {
|
|
|
|
self->p->run_fn();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto res = self->p->run();
|
|
|
|
|
|
|
|
if (self->p->complete_fn) {
|
2018-06-19 09:16:56 +00:00
|
|
|
self->p->complete_fn(res, self->p->current_args_set);
|
2018-06-05 09:55:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-05-18 15:35:24 +00:00
|
|
|
self->p->avrdude_thread = std::move(avrdude_thread);
|
2018-05-18 13:38:33 +00:00
|
|
|
}
|
2018-05-18 15:35:24 +00:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-05-21 13:24:24 +00:00
|
|
|
void AvrDude::cancel()
|
|
|
|
{
|
|
|
|
::avrdude_cancel();
|
|
|
|
}
|
|
|
|
|
2018-05-18 15:35:24 +00:00
|
|
|
void AvrDude::join()
|
|
|
|
{
|
|
|
|
if (p && p->avrdude_thread.joinable()) {
|
|
|
|
p->avrdude_thread.join();
|
2018-05-18 13:38:33 +00:00
|
|
|
}
|
2018-04-19 08:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|