From 28a0622aa31be593c4b047727c79cc9af5651db1 Mon Sep 17 00:00:00 2001 From: Shawn Nock Date: Wed, 8 May 2019 15:53:08 +0000 Subject: [PATCH] Adds timeout --- CMakeLists.txt | 5 +++-- src/image.c | 15 ++++++++++++++- src/timer.c | 18 ++++++++++++++++++ src/timer.h | 13 +++++++++++++ 4 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 src/timer.c create mode 100644 src/timer.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a8d938..f92e09c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,8 @@ add_executable(umcumgr src/image.c src/main.c src/crc16_sw.c - src/termios_util.c) + src/termios_util.c + src/timer.c) if(SANITIZE) message(STATUS "SANITIZE Enabled") @@ -29,6 +30,6 @@ find_package(PkgConfig) PKG_CHECK_MODULES(CBOR REQUIRED tinycbor) target_include_directories(umcumgr PRIVATE ${CBOR_INCLUDE_DIRS}) -target_link_libraries(umcumgr PRIVATE ${CBOR_LIBRARIES}) +target_link_libraries(umcumgr PRIVATE ${CBOR_LIBRARIES} m) install(TARGETS umcumgr DESTINATION bin) \ No newline at end of file diff --git a/src/image.c b/src/image.c index aee1d88..d890150 100644 --- a/src/image.c +++ b/src/image.c @@ -9,11 +9,13 @@ #include #include #include +#include #include "base64.h" #include "crc.h" #include "image.h" #include "nmgr.h" +#include "timer.h" #include #ifdef DO_UPLOAD_SHA @@ -48,10 +50,15 @@ static char *hex(unsigned char in) { static void read_response(uint8_t *dest, size_t *len) { static uint8_t buf[BOOT_SERIAL_IN_MAX*2] = {0}; static uint8_t *p = buf; + timer_start(3); while (true) { int bytes_read = read(serial_fd, p, BOOT_SERIAL_IN_MAX); if (bytes_read < 0) { if (errno == EAGAIN) { + if (timer_expired()) { + fprintf(stderr, "Timeout waiting for bootloader\n"); + exit(12); + } continue; } fprintf(stderr, "Failed to read from serial port: %s\n", strerror(errno)); @@ -238,6 +245,8 @@ void do_upload(char *filename){ fstat(fd, &st); size_t pos = 0, len = st.st_size; + printf("Writing %s into slot 1\n", filename); + while (pos < len){ size_t data_chunk_len; if (pos == 0) { @@ -336,11 +345,15 @@ void do_upload(char *filename){ fprintf(stderr, "Failed to find an offset in the response %s:%d\n", __FILE__, __LINE__); } else { cbor_value_get_int(&rc, &r); - printf("Wrote %d\n", r); + uint8_t percent_complete = round(((double)r/len)*100); + printf("\033[80D%d/%d Written (%d%%)", r, len, percent_complete); + fflush(stdout); } pos += actual_size; free(out); } + printf("\nSuccess.\n"); + exit(0); } void image_main(int fd, int argc, char **argv){ diff --git a/src/timer.c b/src/timer.c new file mode 100644 index 0000000..686c964 --- /dev/null +++ b/src/timer.c @@ -0,0 +1,18 @@ +// +// Created by nock on 2019-05-08. +// + +#include + +#include "timer.h" + +static time_t timer; + +void timer_start(int sec) { + timer = time(NULL); + timer += sec; +} + +bool timer_expired(void) { + return time(NULL) > timer; +} \ No newline at end of file diff --git a/src/timer.h b/src/timer.h new file mode 100644 index 0000000..5d6bd4b --- /dev/null +++ b/src/timer.h @@ -0,0 +1,13 @@ +// +// Created by nock on 2019-05-08. +// + +#ifndef UMCUMGR_TIMER_H +#define UMCUMGR_TIMER_H + +#include + +void timer_start(int sec); +bool timer_expired(void); + +#endif //UMCUMGR_TIMER_H