From patchwork Tue Jan 17 07:13:05 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heiko Schocher X-Patchwork-Id: 136415 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 5FCD2B6F65 for ; Tue, 17 Jan 2012 18:14:42 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id A5AAA2833E; Tue, 17 Jan 2012 08:14:17 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id V9NSu7+I8GRn; Tue, 17 Jan 2012 08:14:17 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 402D628305; Tue, 17 Jan 2012 08:13:42 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 5921F282B4 for ; Tue, 17 Jan 2012 08:13:20 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id OxUcSohwGzZT for ; Tue, 17 Jan 2012 08:13:16 +0100 (CET) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from pollux.denx.de (host-82-135-33-74.customer.m-online.net [82.135.33.74]) by theia.denx.de (Postfix) with ESMTP id 1D3C5282D9 for ; Tue, 17 Jan 2012 08:13:07 +0100 (CET) Received: by pollux.denx.de (Postfix, from userid 515) id 0B12732257A; Tue, 17 Jan 2012 08:13:06 +0100 (CET) From: Heiko Schocher To: u-boot@lists.denx.de Date: Tue, 17 Jan 2012 08:13:05 +0100 Message-Id: <1326784385-4759-1-git-send-email-hs@denx.de> X-Mailer: git-send-email 1.7.7.4 In-Reply-To: <1326614022-24014-2-git-send-email-hs@denx.de> References: <1326614022-24014-2-git-send-email-hs@denx.de> Cc: Heiko Schocher Subject: [U-Boot] [PATCH 1/3 v2] common: add possibility for readline_into_buffer timeout X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de add possibility to add a timeout when reading a line into a buffer. Signed-off-by: Heiko Schocher Cc: Mike Frysinger Acked-by: Mike Frysinger --- - changes for v2: - add comments from Mike Frysinger : - remove useless inner parens - rework timeout handling in readline_into_buffer(): use endtick(), drop CONIG_SYS_HZ usage common/cmd_nvedit.c | 2 +- common/main.c | 20 ++++++++++++++++---- common/menu.c | 3 ++- include/common.h | 3 ++- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c index 63afc82..20080dc 100644 --- a/common/cmd_nvedit.c +++ b/common/cmd_nvedit.c @@ -502,7 +502,7 @@ int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) else buffer[0] = '\0'; - readline_into_buffer("edit: ", buffer); + readline_into_buffer("edit: ", buffer, 0); return setenv(argv[1], buffer); } diff --git a/common/main.c b/common/main.c index e96c95a..248744b 100644 --- a/common/main.c +++ b/common/main.c @@ -685,7 +685,8 @@ static void cread_add_str(char *str, int strsize, int insert, unsigned long *num } } -static int cread_line(const char *const prompt, char *buf, unsigned int *len) +static int cread_line(const char *const prompt, char *buf, unsigned int *len, + int timeout) { unsigned long num = 0; unsigned long eol_num = 0; @@ -695,6 +696,7 @@ static int cread_line(const char *const prompt, char *buf, unsigned int *len) int esc_len = 0; char esc_save[8]; int init_len = strlen(buf); + int first = 1; if (init_len) cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len); @@ -707,6 +709,16 @@ static int cread_line(const char *const prompt, char *buf, unsigned int *len) WATCHDOG_RESET(); } #endif + if (first && timeout) { + uint64_t etime = endtick(timeout); + + while (!tstc()) { /* while no incoming data */ + if (get_ticks() >= etime) + return -2; /* timed out */ + WATCHDOG_RESET(); + } + first = 0; + } ichar = getcmd_getch(); @@ -922,11 +934,11 @@ int readline (const char *const prompt) */ console_buffer[0] = '\0'; - return readline_into_buffer(prompt, console_buffer); + return readline_into_buffer(prompt, console_buffer, 0); } -int readline_into_buffer (const char *const prompt, char * buffer) +int readline_into_buffer(const char *const prompt, char *buffer, int timeout) { char *p = buffer; #ifdef CONFIG_CMDLINE_EDITING @@ -949,7 +961,7 @@ int readline_into_buffer (const char *const prompt, char * buffer) if (prompt) puts (prompt); - rc = cread_line(prompt, p, &len); + rc = cread_line(prompt, p, &len, timeout); return rc < 0 ? rc : len; } else { diff --git a/common/menu.c b/common/menu.c index 5e0817c..3b1e0d0 100644 --- a/common/menu.c +++ b/common/menu.c @@ -222,7 +222,8 @@ static inline int menu_interactive_choice(struct menu *m, void **choice) menu_display(m); - readret = readline_into_buffer("Enter choice: ", cbuf); + readret = readline_into_buffer("Enter choice: ", cbuf, + m->timeout); if (readret >= 0) { choice_item = menu_item_by_key(m, cbuf); diff --git a/include/common.h b/include/common.h index 3df1def..7a9b3a2 100644 --- a/include/common.h +++ b/include/common.h @@ -265,7 +265,8 @@ int run_command (const char *cmd, int flag); int run_command2(const char *cmd, int flag); #endif int readline (const char *const prompt); -int readline_into_buffer (const char *const prompt, char * buffer); +int readline_into_buffer(const char *const prompt, char *buffer, + int timeout); int parse_line (char *, char *[]); void init_cmd_timeout(void); void reset_cmd_timeout(void);