]> rtime.felk.cvut.cz Git - sojka/sterm.git/commitdiff
Refactor pulse to a function
authorMichal Sojka <sojkam1@fel.cvut.cz>
Wed, 19 Aug 2015 20:43:36 +0000 (22:43 +0200)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Wed, 19 Aug 2015 20:43:36 +0000 (22:43 +0200)
sterm.c

diff --git a/sterm.c b/sterm.c
index c075f8b73bcfe913624fd13a1f7ca32d5cd021aa..34f484554683990d7e84a78a33af1b046831df2e 100644 (file)
--- a/sterm.c
+++ b/sterm.c
@@ -137,6 +137,27 @@ void usage(const char* argv0)
                );
 }
 
+void pulse(int fd, int dtr, int rts)
+{
+       int status, ms = 0;
+       /* tio.c_cflag &= ~HUPCL; */ /* Don't lower DTR/RTS on close */
+
+       CHECK(ioctl(fd, TIOCMGET, &status));
+       if (dtr > 0) { status &= ~TIOCM_DTR; ms = +dtr; }
+       if (dtr < 0) { status |=  TIOCM_DTR; ms = -dtr; }
+       if (rts > 0) { status &= ~TIOCM_RTS; ms = +rts; }
+       if (rts < 0) { status |=  TIOCM_RTS; ms = -rts; }
+       CHECK(ioctl(fd, TIOCMSET, &status));
+
+       usleep(ms*1000);
+
+       if (dtr < 0) { status &= ~TIOCM_DTR; }
+       if (dtr > 0) { status |=  TIOCM_DTR; }
+       if (rts < 0) { status &= ~TIOCM_RTS; }
+       if (rts > 0) { status |=  TIOCM_RTS; }
+       CHECK(ioctl(fd, TIOCMSET, &status));
+}
+
 int main(int argc, char *argv[])
 {
        int fd;
@@ -241,26 +262,8 @@ int main(int argc, char *argv[])
                        CHECK(cfsetispeed(&tio, speed));
                }
 
-               if (dtr || rts) {
-                       int status, ms = 0;
-                       /* tio.c_cflag &= ~HUPCL; */ /* Don't lower DTR/RTS on close */
-
-                       CHECK(ioctl(fd, TIOCMGET, &status));
-                       if (dtr > 0) { status &= ~TIOCM_DTR; ms = +dtr; }
-                       if (dtr < 0) { status |=  TIOCM_DTR; ms = -dtr; }
-                       if (rts > 0) { status &= ~TIOCM_RTS; ms = +rts; }
-                       if (rts < 0) { status |=  TIOCM_RTS; ms = -rts; }
-                       CHECK(ioctl(fd, TIOCMSET, &status));
-
-                       usleep(ms*1000);
-
-                       if (dtr < 0) { status &= ~TIOCM_DTR; }
-                       if (dtr > 0) { status |=  TIOCM_DTR; }
-                       if (rts < 0) { status &= ~TIOCM_RTS; }
-                       if (rts > 0) { status |=  TIOCM_RTS; }
-                       CHECK(ioctl(fd, TIOCMSET, &status));
-
-               }
+               if (dtr || rts)
+                       pulse(fd, dtr, rts);
 
                 /* Disable flow control */
                tio.c_cflag &= ~(CRTSCTS);