From dc54f28ff376b0ace021b04002752f32b49f7c5f Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Sun, 27 Jan 2019 07:09:31 -0600 Subject: [PATCH] apps/system/zmodem: Switch to the raw mode before transfer and restore to the original setting after finishing the transfer. --- system/zmodem/rz_main.c | 19 +++++++++++++++++++ system/zmodem/sz_main.c | 21 ++++++++++++++++++++- system/zmodem/zm.h | 15 ++++++++++++--- system/zmodem/zm_utils.c | 22 +++++++++++++++++++--- 4 files changed, 70 insertions(+), 7 deletions(-) diff --git a/system/zmodem/rz_main.c b/system/zmodem/rz_main.c index 422c8d438..6b1bd4098 100644 --- a/system/zmodem/rz_main.c +++ b/system/zmodem/rz_main.c @@ -84,6 +84,9 @@ int rz_main(int argc, FAR char **argv) FAR const char *devname = CONFIG_SYSTEM_ZMODEM_DEVNAME; FAR const char *pathname = CONFIG_SYSTEM_ZMODEM_MOUNTPOINT; int exitcode = EXIT_FAILURE; +#ifdef CONFIG_SERIAL_TERMIOS + struct termios saveterm; +#endif int option; int ret; int fd; @@ -136,10 +139,20 @@ int rz_main(int argc, FAR char **argv) goto errout; } +#ifdef CONFIG_SERIAL_TERMIOS + /* Save the current terminal setting */ + + tcgetattr(fd, &saveterm); + + /* Enable the raw mode */ + + zm_rawmode(fd); + #ifdef CONFIG_SYSTEM_ZMODEM_FLOWC /* Enable hardware Rx/Tx flow control */ zm_flowc(fd); +#endif #endif /* Get the Zmodem handle */ @@ -166,12 +179,18 @@ errout_with_zmodem: (void)zmr_release(handle); errout_with_device: +#ifdef CONFIG_SERIAL_TERMIOS #ifdef CONFIG_SYSTEM_ZMODEM_FLOWC /* Flush the serial output to assure do not hang trying to drain it */ tcflush(fd, TCIOFLUSH); #endif + /* Restore the saved terminal setting */ + + tcsetattr(fd, TCSANOW, &saveterm); +#endif + (void)close(fd); errout: diff --git a/system/zmodem/sz_main.c b/system/zmodem/sz_main.c index ca16a3445..c9752cdca 100644 --- a/system/zmodem/sz_main.c +++ b/system/zmodem/sz_main.c @@ -106,6 +106,9 @@ int sz_main(int argc, FAR char **argv) bool skip = false; long tmp; int exitcode = EXIT_FAILURE; +#ifdef CONFIG_SERIAL_TERMIOS + struct termios saveterm; +#endif int option; int ret; int fd; @@ -188,10 +191,20 @@ int sz_main(int argc, FAR char **argv) goto errout; } +#ifdef CONFIG_SERIAL_TERMIOS + /* Save the current terminal setting */ + + tcgetattr(fd, &saveterm); + + /* Enable the raw mode */ + + zm_rawmode(fd); + #ifdef CONFIG_SYSTEM_ZMODEM_FLOWC /* Enable hardware Rx/Tx flow control */ zm_flowc(fd); +#endif #endif /* Get the Zmodem handle */ @@ -265,12 +278,18 @@ errout_with_zmodem: (void)zms_release(handle); errout_with_device: -#ifdef CONFIG_SYSTEM_ZMODEM_FLOWC +#ifdef CONFIG_SERIAL_TERMIOS +# ifdef CONFIG_SYSTEM_ZMODEM_FLOWC /* Flush the serial output to assure do not hang trying to drain it */ tcflush(fd, TCIOFLUSH); #endif + /* Restore the saved terminal setting */ + + tcsetattr(fd, TCSANOW, &saveterm); +#endif + (void)close(fd); errout: diff --git a/system/zmodem/zm.h b/system/zmodem/zm.h index 2537bd93a..ed553835f 100644 --- a/system/zmodem/zm.h +++ b/system/zmodem/zm.h @@ -578,15 +578,24 @@ int zm_writefile(int fd, FAR const uint8_t *buffer, size_t buflen, bool zcnl); uint32_t zm_filecrc(FAR struct zm_state_s *pzm, FAR const char *filename); +/**************************************************************************** + * Name: zm_rawmode + * + * Description: + * Set the terminal to the raw mode + * + ****************************************************************************/ + +#ifdef CONFIG_SERIAL_TERMIOS +void zm_rawmode(int fd); +#endif + /**************************************************************************** * Name: zm_flowc * * Description: * Enable hardware Rx/Tx flow control. * - * REVISIT: Consider returning the original termios settings so that they - * could be restored with rx/sz exits. - * ****************************************************************************/ #ifdef CONFIG_SYSTEM_ZMODEM_FLOWC diff --git a/system/zmodem/zm_utils.c b/system/zmodem/zm_utils.c index c466a05d8..cc628ce74 100644 --- a/system/zmodem/zm_utils.c +++ b/system/zmodem/zm_utils.c @@ -473,15 +473,31 @@ uint32_t zm_filecrc(FAR struct zm_state_s *pzm, FAR const char *filename) return ~crc; } +/**************************************************************************** + * Name: zm_rawmode + * + * Description: + * Set the terminal to the raw mode + * + ****************************************************************************/ + +#ifdef CONFIG_SERIAL_TERMIOS +void zm_rawmode(int fd) +{ + struct termios term; + + tcgetattr(fd, &term); + cfmakeraw(&term); + tcsetattr(fd, TCSANOW, &term); +} +#endif + /**************************************************************************** * Name: zm_flowc * * Description: * Enable hardware Rx/Tx flow control. * - * REVISIT: Consider returning the original termios settings so that they - * could be restored with rx/sz exits. - * ****************************************************************************/ #ifdef CONFIG_SYSTEM_ZMODEM_FLOWC