Commit 9c3483113de1204118a1b252bf4c81b899dd12b4
Committed by
Albert ARIBAUD
1 parent
2627c8c0a1
Exists in
master
and in
54 other branches
common: add possibility for readline_into_buffer timeout
add possibility to add a timeout when reading a line into a buffer. Signed-off-by: Heiko Schocher <hs@denx.de> Cc: Mike Frysinger <vapier@gentoo.org> Acked-by: Mike Frysinger <vapier@gentoo.org>
Showing 4 changed files with 21 additions and 7 deletions Side-by-side Diff
common/cmd_nvedit.c
common/main.c
... | ... | @@ -685,7 +685,8 @@ |
685 | 685 | } |
686 | 686 | } |
687 | 687 | |
688 | -static int cread_line(const char *const prompt, char *buf, unsigned int *len) | |
688 | +static int cread_line(const char *const prompt, char *buf, unsigned int *len, | |
689 | + int timeout) | |
689 | 690 | { |
690 | 691 | unsigned long num = 0; |
691 | 692 | unsigned long eol_num = 0; |
... | ... | @@ -695,6 +696,7 @@ |
695 | 696 | int esc_len = 0; |
696 | 697 | char esc_save[8]; |
697 | 698 | int init_len = strlen(buf); |
699 | + int first = 1; | |
698 | 700 | |
699 | 701 | if (init_len) |
700 | 702 | cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len); |
701 | 703 | |
... | ... | @@ -707,7 +709,17 @@ |
707 | 709 | WATCHDOG_RESET(); |
708 | 710 | } |
709 | 711 | #endif |
712 | + if (first && timeout) { | |
713 | + uint64_t etime = endtick(timeout); | |
710 | 714 | |
715 | + while (!tstc()) { /* while no incoming data */ | |
716 | + if (get_ticks() >= etime) | |
717 | + return -2; /* timed out */ | |
718 | + WATCHDOG_RESET(); | |
719 | + } | |
720 | + first = 0; | |
721 | + } | |
722 | + | |
711 | 723 | ichar = getcmd_getch(); |
712 | 724 | |
713 | 725 | if ((ichar == '\n') || (ichar == '\r')) { |
714 | 726 | |
... | ... | @@ -922,11 +934,11 @@ |
922 | 934 | */ |
923 | 935 | console_buffer[0] = '\0'; |
924 | 936 | |
925 | - return readline_into_buffer(prompt, console_buffer); | |
937 | + return readline_into_buffer(prompt, console_buffer, 0); | |
926 | 938 | } |
927 | 939 | |
928 | 940 | |
929 | -int readline_into_buffer (const char *const prompt, char * buffer) | |
941 | +int readline_into_buffer(const char *const prompt, char *buffer, int timeout) | |
930 | 942 | { |
931 | 943 | char *p = buffer; |
932 | 944 | #ifdef CONFIG_CMDLINE_EDITING |
... | ... | @@ -949,7 +961,7 @@ |
949 | 961 | if (prompt) |
950 | 962 | puts (prompt); |
951 | 963 | |
952 | - rc = cread_line(prompt, p, &len); | |
964 | + rc = cread_line(prompt, p, &len, timeout); | |
953 | 965 | return rc < 0 ? rc : len; |
954 | 966 | |
955 | 967 | } else { |
common/menu.c
include/common.h
... | ... | @@ -265,7 +265,8 @@ |
265 | 265 | int run_command2(const char *cmd, int flag); |
266 | 266 | #endif |
267 | 267 | int readline (const char *const prompt); |
268 | -int readline_into_buffer (const char *const prompt, char * buffer); | |
268 | +int readline_into_buffer(const char *const prompt, char *buffer, | |
269 | + int timeout); | |
269 | 270 | int parse_line (char *, char *[]); |
270 | 271 | void init_cmd_timeout(void); |
271 | 272 | void reset_cmd_timeout(void); |