Firmware updater: Add cancelation

This commit is contained in:
Vojtech Kral 2018-05-18 18:37:16 +02:00
parent 98ae20c3df
commit fd00ea0ca7
7 changed files with 96 additions and 44 deletions

View file

@ -358,7 +358,7 @@ int avr_read(PROGRAMMER * pgm, AVRPART * p, char * memtype,
return -1;
}
}
report_progress(i, mem->size, NULL);
if (!report_progress(i, mem->size, NULL)) return -99;
}
return avr_mem_hiaddr(mem);
}
@ -415,7 +415,7 @@ int avr_read(PROGRAMMER * pgm, AVRPART * p, char * memtype,
progname, pageaddr / mem->page_size);
}
nread++;
report_progress(nread, npages, NULL);
if (!report_progress(nread, npages, NULL)) return -99;
}
if (!failure) {
if (strcasecmp(mem->desc, "flash") == 0 ||
@ -448,7 +448,7 @@ int avr_read(PROGRAMMER * pgm, AVRPART * p, char * memtype,
return -2;
}
}
report_progress(i, mem->size, NULL);
if (!report_progress(i, mem->size, NULL)) return -99;
}
if (strcasecmp(mem->desc, "flash") == 0 ||
@ -896,7 +896,7 @@ int avr_write(PROGRAMMER * pgm, AVRPART * p, char * memtype, int size,
while (avr_tpi_poll_nvmbsy(pgm));
}
report_progress(i, wsize, NULL);
if (!report_progress(i, wsize, NULL)) return -99;
}
return i;
}
@ -948,7 +948,7 @@ int avr_write(PROGRAMMER * pgm, AVRPART * p, char * memtype, int size,
progname, pageaddr / m->page_size);
}
nwritten++;
report_progress(nwritten, npages, NULL);
if (!report_progress(nwritten, npages, NULL)) return -99;
}
if (!failure)
return wsize;
@ -965,7 +965,7 @@ int avr_write(PROGRAMMER * pgm, AVRPART * p, char * memtype, int size,
for (i=0; i<wsize; i++) {
data = m->buf[i];
report_progress(i, wsize, NULL);
if (!report_progress(i, wsize, NULL)) return -99;
/*
* Find out whether the write action must be invoked for this
@ -1050,14 +1050,14 @@ int avr_signature(PROGRAMMER * pgm, AVRPART * p)
{
int rc;
report_progress (0,1,"Reading");
if (!report_progress(0,1,"Reading")) return -99;
rc = avr_read(pgm, p, "signature", 0);
if (rc < 0) {
avrdude_message(MSG_INFO, "%s: error reading signature data for part \"%s\", rc=%d\n",
progname, p->desc, rc);
return -1;
}
report_progress (1,1,NULL);
if (!report_progress(1,1,NULL)) return -99;
return 0;
}
@ -1214,16 +1214,19 @@ int avr_chip_erase(PROGRAMMER * pgm, AVRPART * p)
* call for each of start, during and end cases. As things stand now,
* that is not possible and makes maintenance a bit more work.
*/
void report_progress (int completed, int total, char *hdr)
// Prusa version modification: report_progress() returns bool to faciliate cancelation
// the bool has "continue" semantics, ie. true = continue, false = interrupt
bool report_progress (int completed, int total, char *hdr)
{
static int last = 0;
static double start_time;
int percent = (total > 0) ? ((completed * 100) / total) : 100;
struct timeval tv;
double t;
bool res = true;
if (update_progress == NULL)
return;
return true;
gettimeofday(&tv, NULL);
t = tv.tv_sec + ((double)tv.tv_usec)/1000000;
@ -1231,7 +1234,7 @@ void report_progress (int completed, int total, char *hdr)
if (hdr) {
last = 0;
start_time = t;
update_progress (percent, t - start_time, hdr);
res = update_progress (percent, t - start_time, hdr);
}
if (percent > 100)
@ -1239,9 +1242,11 @@ void report_progress (int completed, int total, char *hdr)
if (percent > last) {
last = percent;
update_progress (percent, t - start_time, hdr);
res = update_progress (percent, t - start_time, hdr);
}
if (percent == 100)
last = 0; /* Get ready for next time. */
return res;
}

View file

@ -21,10 +21,10 @@ static void avrdude_message_handler_closure(const char *msg, unsigned size, void
}
// Used by our custom code in avrdude to report progress in the GUI
static void avrdude_progress_handler_closure(const char *task, unsigned progress, void *user_p)
static bool avrdude_progress_handler_closure(const char *task, unsigned progress, void *user_p)
{
auto *progress_fn = reinterpret_cast<AvrDude::ProgressFn*>(user_p);
(*progress_fn)(task, progress);
return (*progress_fn)(task, progress);
}
@ -73,6 +73,8 @@ int AvrDude::priv::run() {
AvrDude::AvrDude() : p(new priv()) {}
AvrDude::AvrDude(AvrDude &&other) : p(std::move(other.p)) {}
AvrDude::~AvrDude()
{
if (p && p->avrdude_thread.joinable()) {
@ -98,7 +100,7 @@ AvrDude& AvrDude::on_message(MessageFn fn)
return *this;
}
AvrDude& AvrDude::on_progress(MessageFn fn)
AvrDude& AvrDude::on_progress(ProgressFn fn)
{
if (p) { p->progress_fn = std::move(fn); }
return *this;

View file

@ -13,11 +13,11 @@ class AvrDude
public:
typedef std::shared_ptr<AvrDude> Ptr;
typedef std::function<void(const char * /* msg */, unsigned /* size */)> MessageFn;
typedef std::function<void(const char * /* task */, unsigned /* progress */)> ProgressFn;
typedef std::function<bool(const char * /* task */, unsigned /* progress */)> ProgressFn;
typedef std::function<void(int /* exit status */)> CompleteFn;
AvrDude();
AvrDude(AvrDude &&) = default;
AvrDude(AvrDude &&);
AvrDude(const AvrDude &) = delete;
AvrDude &operator=(AvrDude &&) = delete;
AvrDude &operator=(const AvrDude &) = delete;
@ -34,7 +34,8 @@ public:
// Set progress report callback
// Progress is reported per each task (reading / writing), progress is reported in percents.
AvrDude& on_progress(MessageFn fn);
// The callback's return value indicates whether to continue flashing (true) or cancel (false).
AvrDude& on_progress(ProgressFn fn);
// Called when avrdude's main function finishes
AvrDude& on_complete(CompleteFn fn);

View file

@ -21,6 +21,8 @@
#ifndef avrdude_h
#define avrdude_h
#include <stdbool.h>
extern char * progname; /* name of program, for messages */
extern char progbuf[]; /* spaces same length as progname */
@ -34,9 +36,9 @@ int avrdude_message(const int msglvl, const char *format, ...);
// Progress reporting callback
// `progress` is in range 0 ~ 100 percent
typedef void (*avrdude_progress_handler_t)(const char *task, unsigned progress, void *user_p);
typedef bool (*avrdude_progress_handler_t)(const char *task, unsigned progress, void *user_p);
void avrdude_progress_handler_set(avrdude_progress_handler_t newhandler, void *user_p);
void avrdude_progress_external(const char *task, unsigned progress);
bool avrdude_progress_external(const char *task, unsigned progress);
#define MSG_INFO (0) /* no -v option, can be supressed with -qq */
#define MSG_NOTICE (1) /* displayed with -v */

View file

@ -727,7 +727,7 @@ void sort_programmers(LISTID programmers);
/* formerly avr.h */
typedef void (*FP_UpdateProgress)(int percent, double etime, char *hdr);
typedef bool (*FP_UpdateProgress)(int percent, double etime, char *hdr);
extern struct avrpart parts[];
@ -769,7 +769,7 @@ int avr_mem_hiaddr(AVRMEM * mem);
int avr_chip_erase(PROGRAMMER * pgm, AVRPART * p);
void report_progress (int completed, int total, char *hdr);
bool report_progress (int completed, int total, char *hdr);
#ifdef __cplusplus
}

View file

@ -115,12 +115,13 @@ int avrdude_message(const int msglvl, const char *format, ...)
}
static void avrdude_progress_handler_null(const char *task, unsigned progress, void *user_p)
static bool avrdude_progress_handler_null(const char *task, unsigned progress, void *user_p)
{
// By default do nothing
(void)task;
(void)progress;
(void)user_p;
return true;
}
static void *avrdude_progress_handler_user_p = NULL;
@ -137,9 +138,9 @@ void avrdude_progress_handler_set(avrdude_progress_handler_t newhandler, void *u
}
}
void avrdude_progress_external(const char *task, unsigned progress)
bool avrdude_progress_external(const char *task, unsigned progress)
{
avrdude_progress_handler(task, progress, avrdude_progress_handler_user_p);
return avrdude_progress_handler(task, progress, avrdude_progress_handler_user_p);
}
@ -244,12 +245,13 @@ static void usage(void)
// setvbuf(stderr, (char*)NULL, _IOLBF, 0);
// }
static void update_progress_no_tty (int percent, double etime, char *hdr)
static bool update_progress_no_tty (int percent, double etime, char *hdr)
{
static int done = 0;
static int last = 0;
static char *header = NULL;
int cnt = (percent>>1)*2;
bool res = true;
// setvbuf(stderr, (char*)NULL, _IONBF, 0);
@ -258,7 +260,7 @@ static void update_progress_no_tty (int percent, double etime, char *hdr)
last = 0;
done = 0;
header = hdr;
avrdude_progress_external(header, 0);
res = avrdude_progress_external(header, 0);
}
else {
while ((cnt > last) && (done == 0)) {
@ -267,7 +269,7 @@ static void update_progress_no_tty (int percent, double etime, char *hdr)
}
if (done == 0) {
avrdude_progress_external(header, percent > 99 ? 99 : percent);
res = avrdude_progress_external(header, percent > 99 ? 99 : percent);
}
}
@ -281,6 +283,8 @@ static void update_progress_no_tty (int percent, double etime, char *hdr)
last = (percent>>1)*2; /* Make last a multiple of 2. */
// setvbuf(stderr, (char*)NULL, _IOLBF, 0);
return res;
}
static void list_programmers_callback(const char *name, const char *desc,