summaryrefslogtreecommitdiff
path: root/serprog.c
diff options
context:
space:
mode:
authorstefanct <stefanct@2b7e53f0-3cfb-0310-b3e9-8179ed1497e1>2013-04-01 00:45:38 +0000
committerstefanct <stefanct@2b7e53f0-3cfb-0310-b3e9-8179ed1497e1>2013-04-01 00:45:38 +0000
commita13273eb81098883928cbe25234ff5779563b71d (patch)
tree42c1c49a92dcdfe9a406f31f9adc8ea50ac8c666 /serprog.c
parenta6d41be08f676de48e12eaefb987ee7f0e3a786f (diff)
downloadflashrom-a13273eb81098883928cbe25234ff5779563b71d.tar.gz
Replace sp_sync_read_timeout() with serialport_read_nonblock().
It seems useful to have a generic and platform-independent method to read and write to a serial port without blocking. This is the read part. It stores the current blocking properties before disabling blocking and restores them after reading. The timeout is implemented as previously by retrying every millisecond until the timeout is reached or enough characters are available. Signed-off-by: Stefan Tauner <stefan.tauner@student.tuwien.ac.at> Acked-by: Stefan Tauner <stefan.tauner@student.tuwien.ac.at> git-svn-id: https://code.coreboot.org/svn/flashrom/trunk@1661 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
Diffstat (limited to 'serprog.c')
-rw-r--r--serprog.c28
1 files changed, 4 insertions, 24 deletions
diff --git a/serprog.c b/serprog.c
index c36c93d..15d1d1b 100644
--- a/serprog.c
+++ b/serprog.c
@@ -115,26 +115,6 @@ static int sp_opensocket(char *ip, unsigned int port)
return sock;
}
-/* Returns 0 on success and places the character read into the location pointed to by c.
- * Returns positive values on temporary errors and negative ones on permanent errors.
- * An iteration of one loop takes up to 1ms. */
-static int sp_sync_read_timeout(unsigned int loops, unsigned char *c)
-{
- int i;
- for (i = 0; i < loops; i++) {
- ssize_t rv;
- rv = read(sp_fd, c, 1);
- if (rv == 1)
- return 0;
- if ((rv == -1) && (errno != EAGAIN)) {
- msg_perr("read: %s\n", strerror(errno));
- return -1;
- }
- usleep(1000); /* 1ms units */
- }
- return 1;
-}
-
/* Synchronize: a bit tricky algorithm that tries to (and in my tests has *
* always succeeded in) bring the serial protocol to known waiting-for- *
* command state - uses nonblocking read - rest of the driver uses *
@@ -174,12 +154,12 @@ static int sp_synchronize(void)
msg_pdbg(".");
fflush(stdout);
for (n = 0; n < 10; n++) {
- int ret = sp_sync_read_timeout(50, &c);
+ int ret = serialport_read_nonblock(&c, 1, 50, NULL);
if (ret < 0)
goto err_out;
if (ret > 0 || c != S_NAK)
continue;
- ret = sp_sync_read_timeout(20, &c);
+ ret = serialport_read_nonblock(&c, 1, 20, NULL);
if (ret < 0)
goto err_out;
if (ret > 0 || c != S_ACK)
@@ -189,12 +169,12 @@ static int sp_synchronize(void)
msg_perr("sync write: %s\n", strerror(errno));
return 1;
}
- ret = sp_sync_read_timeout(500, &c);
+ ret = serialport_read_nonblock(&c, 1, 500, NULL);
if (ret < 0)
goto err_out;
if (ret > 0 || c != S_NAK)
break; /* fail */
- ret = sp_sync_read_timeout(100, &c);
+ ret = serialport_read_nonblock(&c, 1, 100, NULL);
if (ret > 0 || ret < 0)
goto err_out;
if (c != S_ACK)