summaryrefslogtreecommitdiffstats
path: root/serprog.c
diff options
context:
space:
mode:
authorStefan Tauner <stefan.tauner@alumni.tuwien.ac.at>2013-04-01 00:45:38 +0000
committerStefan Tauner <stefan.tauner@alumni.tuwien.ac.at>2013-04-01 00:45:38 +0000
commit00e1608501a0eb270df80a31f0dd0b327b202059 (patch)
tree42c1c49a92dcdfe9a406f31f9adc8ea50ac8c666 /serprog.c
parentda5b17c0a2c648c60272f92c9ecbcd10f4af8348 (diff)
downloadflashrom-00e1608501a0eb270df80a31f0dd0b327b202059.tar.gz
flashrom-00e1608501a0eb270df80a31f0dd0b327b202059.tar.bz2
flashrom-00e1608501a0eb270df80a31f0dd0b327b202059.zip
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. Corresponding to flashrom svn r1661. Signed-off-by: Stefan Tauner <stefan.tauner@alumni.tuwien.ac.at> Acked-by: Stefan Tauner <stefan.tauner@alumni.tuwien.ac.at>
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 c36c93d88..15d1d1b5d 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)