1
0
Fork 0

Adds timeout

This commit is contained in:
Shawn Nock 2019-05-08 15:53:08 +00:00
parent a76382c2bb
commit 28a0622aa3
4 changed files with 48 additions and 3 deletions

View File

@ -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)

View File

@ -9,11 +9,13 @@
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <math.h>
#include "base64.h"
#include "crc.h"
#include "image.h"
#include "nmgr.h"
#include "timer.h"
#include <tinycbor/cbor.h>
#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){

18
src/timer.c Normal file
View File

@ -0,0 +1,18 @@
//
// Created by nock on 2019-05-08.
//
#include <time.h>
#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;
}

13
src/timer.h Normal file
View File

@ -0,0 +1,13 @@
//
// Created by nock on 2019-05-08.
//
#ifndef UMCUMGR_TIMER_H
#define UMCUMGR_TIMER_H
#include <stdbool.h>
void timer_start(int sec);
bool timer_expired(void);
#endif //UMCUMGR_TIMER_H