Firmware updater: Perform work in a background thread

This commit is contained in:
Vojtech Kral 2018-05-18 17:35:24 +02:00
parent a54672fb54
commit 98ae20c3df
3 changed files with 216 additions and 85 deletions

View file

@ -1,5 +1,7 @@
#include "avrdude-slic3r.hpp"
#include <thread>
extern "C" {
#include "ac_cfg.h"
#include "avrdude.h"
@ -8,6 +10,9 @@ extern "C" {
namespace Slic3r {
// C callbacks
// 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)
{
@ -23,52 +28,116 @@ static void avrdude_progress_handler_closure(const char *task, unsigned progress
}
AvrDude::AvrDude() {}
AvrDude::~AvrDude() {}
// Private
AvrDude& AvrDude::sys_config(std::string sys_config)
struct AvrDude::priv
{
m_sys_config = std::move(sys_config);
return *this;
}
std::string sys_config;
std::vector<std::string> args;
MessageFn message_fn;
ProgressFn progress_fn;
CompleteFn complete_fn;
AvrDude& AvrDude::on_message(MessageFn fn)
{
m_message_fn = std::move(fn);
return *this;
}
std::thread avrdude_thread;
AvrDude& AvrDude::on_progress(MessageFn fn)
{
m_progress_fn = std::move(fn);
return *this;
}
int run();
};
int AvrDude::run(std::vector<std::string> args)
{
std::vector<char *> c_args {{ const_cast<char*>(PACKAGE_NAME) }};
int AvrDude::priv::run() {
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 (m_message_fn) {
::avrdude_message_handler_set(avrdude_message_handler_closure, reinterpret_cast<void*>(&m_message_fn));
if (message_fn) {
::avrdude_message_handler_set(avrdude_message_handler_closure, reinterpret_cast<void*>(&message_fn));
} else {
::avrdude_message_handler_set(nullptr, nullptr);
}
if (m_progress_fn) {
::avrdude_progress_handler_set(avrdude_progress_handler_closure, reinterpret_cast<void*>(&m_progress_fn));
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(), m_sys_config.c_str());
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;
}
// Public
AvrDude::AvrDude() : p(new priv()) {}
AvrDude::~AvrDude()
{
if (p && p->avrdude_thread.joinable()) {
p->avrdude_thread.detach();
}
}
AvrDude& AvrDude::sys_config(std::string sys_config)
{
if (p) { p->sys_config = std::move(sys_config); }
return *this;
}
AvrDude& AvrDude::args(std::vector<std::string> args)
{
if (p) { p->args = std::move(args); }
return *this;
}
AvrDude& AvrDude::on_message(MessageFn fn)
{
if (p) { p->message_fn = std::move(fn); }
return *this;
}
AvrDude& AvrDude::on_progress(MessageFn 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()
{
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]() {
auto res = self->p->run();
if (self->p->complete_fn) {
self->p->complete_fn(res);
}
});
self->p->avrdude_thread = std::move(avrdude_thread);
}
return self;
}
void AvrDude::join()
{
if (p && p->avrdude_thread.joinable()) {
p->avrdude_thread.join();
}
}
}