1
0
mirror of git://sigrok.org/libserialport synced 2023-08-10 21:13:24 +03:00

windows: Refactor duplicated code into a static function.

This commit is contained in:
Martin Ling 2015-05-06 20:11:17 +01:00 committed by Uwe Hermann
parent 27a58c0521
commit 94a314a806

View File

@ -917,6 +917,26 @@ SP_API enum sp_return sp_nonblocking_write(struct sp_port *port,
#endif
}
#ifdef _WIN32
/* Restart wait operation if buffer was emptied. */
static enum sp_return restart_wait_if_needed(struct sp_port *port, unsigned int bytes_read)
{
int ret, bytes_remaining;
ret = sp_input_waiting(port);
if (ret < 0)
RETURN_CODEVAL(ret);
bytes_remaining = ret;
if (bytes_read > 0 && bytes_remaining == 0)
TRY(restart_wait(port));
RETURN_OK();
}
#endif
SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf,
size_t count, unsigned int timeout_ms)
{
@ -939,8 +959,6 @@ SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf,
#ifdef _WIN32
DWORD bytes_read = 0;
DWORD bytes_remaining;
int ret;
/* Set timeout. */
if (port->timeouts.ReadIntervalTimeout != 0 ||
@ -965,16 +983,7 @@ SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf,
bytes_read = count;
}
ret = sp_input_waiting(port);
if (ret < 0)
RETURN_CODEVAL(ret);
bytes_remaining = ret;
/* Restart wait operation if buffer was emptied. */
if (bytes_read > 0 && bytes_remaining == 0)
TRY(restart_wait(port));
TRY(restart_wait_if_needed(port, bytes_read));
RETURN_INT(bytes_read);
@ -1054,8 +1063,6 @@ SP_API enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf,
#ifdef _WIN32
DWORD bytes_read;
DWORD bytes_remaining;
int ret;
/* Set timeout. */
if (port->timeouts.ReadIntervalTimeout != MAXDWORD ||
@ -1074,16 +1081,7 @@ SP_API enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf,
if (GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE) == 0)
RETURN_FAIL("GetOverlappedResult() failed");
ret = sp_input_waiting(port);
if (ret < 0)
RETURN_CODEVAL(ret);
bytes_remaining = ret;
/* Restart wait operation if buffer was emptied. */
if (bytes_read > 0 && bytes_remaining == 0)
TRY(restart_wait(port));
TRY(restart_wait_if_needed(port, bytes_read));
RETURN_INT(bytes_read);
#else