diff options
author | Stefan Tauner <stefan.tauner@alumni.tuwien.ac.at> | 2013-04-01 00:45:45 +0000 |
---|---|---|
committer | Stefan Tauner <stefan.tauner@alumni.tuwien.ac.at> | 2013-04-01 00:45:45 +0000 |
commit | ae3d83765cbb306696b3e0caa92e535e522b32d2 (patch) | |
tree | 2777c097b70e61f9a39ca0bca327974038c2ee4a /serprog.c | |
parent | 00e1608501a0eb270df80a31f0dd0b327b202059 (diff) | |
download | flashrom-ae3d83765cbb306696b3e0caa92e535e522b32d2.tar.gz flashrom-ae3d83765cbb306696b3e0caa92e535e522b32d2.tar.bz2 flashrom-ae3d83765cbb306696b3e0caa92e535e522b32d2.zip |
Introduce serialport_write_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 write part.
This allows to get rid of the explicit temporary disabling of blocking I/O in
serprog's sp_synchronize().
Corresponding to flashrom svn r1662.
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.c | 19 |
1 files changed, 5 insertions, 14 deletions
@@ -117,23 +117,19 @@ static int sp_opensocket(char *ip, unsigned int port) /* 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 * + * command state - uses nonblocking I/O - rest of the driver uses * * blocking read - TODO: add an alarm() timer for the rest of the app on * * serial operations, though not such a big issue as the first thing to * * do is synchronize (eg. check that device is alive). */ static int sp_synchronize(void) { int i; - int flags = fcntl(sp_fd, F_GETFL); unsigned char buf[8]; - flags |= O_NONBLOCK; - fcntl(sp_fd, F_SETFL, flags); /* First sends 8 NOPs, then flushes the return data - should cause * * the device serial parser to get to a sane state, unless if it * * is waiting for a real long write-n. */ memset(buf, S_CMD_NOP, 8); - if (write(sp_fd, buf, 8) != 8) { - msg_perr("flush write: %s\n", strerror(errno)); + if (serialport_write_nonblock(buf, 8, 1, NULL) != 0) { goto err_out; } /* A second should be enough to get all the answers to the buffer */ @@ -147,8 +143,7 @@ static int sp_synchronize(void) for (i = 0; i < 8; i++) { int n; unsigned char c = S_CMD_SYNCNOP; - if (write(sp_fd, &c, 1) != 1) { - msg_perr("sync write: %s\n", strerror(errno)); + if (serialport_write_nonblock(&c, 1, 1, NULL) != 0) { goto err_out; } msg_pdbg("."); @@ -165,9 +160,8 @@ static int sp_synchronize(void) if (ret > 0 || c != S_ACK) continue; c = S_CMD_SYNCNOP; - if (write(sp_fd, &c, 1) != 1) { - msg_perr("sync write: %s\n", strerror(errno)); - return 1; + if (serialport_write_nonblock(&c, 1, 1, NULL) != 0) { + goto err_out; } ret = serialport_read_nonblock(&c, 1, 500, NULL); if (ret < 0) @@ -179,9 +173,6 @@ static int sp_synchronize(void) goto err_out; if (c != S_ACK) break; /* fail */ - /* Ok, synchronized; back to blocking reads and return. */ - flags &= ~O_NONBLOCK; - fcntl(sp_fd, F_SETFL, flags); msg_pdbg("\n"); return 0; } |