X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=serialport.c;h=c77682f98566fcf903e8aa76689949cdc4b8523c;hb=c3cee38c3b27071347811d742fec3a95ab8746a5;hp=272bdf6013a525b96a30cd869ea3e99ca0ea0499;hpb=ad036cc8efcdcae5d2b8727996108c38c4915d2b;p=libserialport.git diff --git a/serialport.c b/serialport.c index 272bdf6..c77682f 100644 --- a/serialport.c +++ b/serialport.c @@ -126,7 +126,7 @@ SP_API char *sp_get_port_name(const struct sp_port *port) RETURN_STRING(port->name); } -SP_API char *sp_get_port_description(struct sp_port *port) +SP_API char *sp_get_port_description(const struct sp_port *port) { TRACE("%p", port); @@ -234,6 +234,8 @@ SP_API enum sp_return sp_get_port_handle(const struct sp_port *port, if (!port) RETURN_ERROR(SP_ERR_ARG, "Null port"); + if (!result_ptr) + RETURN_ERROR(SP_ERR_ARG, "Null result pointer"); #ifdef _WIN32 HANDLE *handle_ptr = result_ptr; @@ -332,6 +334,8 @@ SP_API enum sp_return sp_list_ports(struct sp_port ***list_ptr) if (!list_ptr) RETURN_ERROR(SP_ERR_ARG, "Null result pointer"); + *list_ptr = NULL; + #ifdef NO_ENUMERATION RETURN_ERROR(SP_ERR_SUPP, "Enumeration not supported on this platform"); #else @@ -713,18 +717,18 @@ SP_API enum sp_return sp_drain(struct sp_port *port) } SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, - size_t count, unsigned int timeout) + size_t count, unsigned int timeout_ms) { - TRACE("%p, %p, %d, %d", port, buf, count, timeout); + TRACE("%p, %p, %d, %d", port, buf, count, timeout_ms); CHECK_OPEN_PORT(); if (!buf) RETURN_ERROR(SP_ERR_ARG, "Null buffer"); - if (timeout) + if (timeout_ms) DEBUG_FMT("Writing %d bytes to port %s, timeout %d ms", - count, port->name, timeout); + count, port->name, timeout_ms); else DEBUG_FMT("Writing %d bytes to port %s, no timeout", count, port->name); @@ -747,23 +751,24 @@ SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, } /* Set timeout. */ - port->timeouts.WriteTotalTimeoutConstant = timeout; - if (SetCommTimeouts(port->hdl, &port->timeouts) == 0) - RETURN_FAIL("SetCommTimeouts() failed"); + if (port->timeouts.WriteTotalTimeoutConstant != timeout_ms) { + port->timeouts.WriteTotalTimeoutConstant = timeout_ms; + if (SetCommTimeouts(port->hdl, &port->timeouts) == 0) + RETURN_FAIL("SetCommTimeouts() failed"); + } /* Start write. */ - if (WriteFile(port->hdl, buf, count, NULL, &port->write_ovl) == 0) { - if (GetLastError() == ERROR_IO_PENDING) { - DEBUG("Waiting for write to complete"); - GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE); - DEBUG_FMT("Write completed, %d/%d bytes written", bytes_written, count); - RETURN_INT(bytes_written); - } else { - RETURN_FAIL("WriteFile() failed"); - } - } else { + if (WriteFile(port->hdl, buf, count, NULL, &port->write_ovl)) { DEBUG("Write completed immediately"); RETURN_INT(count); + } else if (GetLastError() == ERROR_IO_PENDING) { + DEBUG("Waiting for write to complete"); + if (GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE) == 0) + RETURN_FAIL("GetOverlappedResult() failed"); + DEBUG_FMT("Write completed, %d/%d bytes written", bytes_written, count); + RETURN_INT(bytes_written); + } else { + RETURN_FAIL("WriteFile() failed"); } #else size_t bytes_written = 0; @@ -772,30 +777,30 @@ SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, fd_set fds; int result; - if (timeout) { + if (timeout_ms) { /* Get time at start of operation. */ gettimeofday(&start, NULL); /* Define duration of timeout. */ - delta.tv_sec = timeout / 1000; - delta.tv_usec = (timeout % 1000) * 1000; + delta.tv_sec = timeout_ms / 1000; + delta.tv_usec = (timeout_ms % 1000) * 1000; /* Calculate time at which we should give up. */ timeradd(&start, &delta, &end); } + FD_ZERO(&fds); + FD_SET(port->fd, &fds); + /* Loop until we have written the requested number of bytes. */ while (bytes_written < count) { /* Wait until space is available. */ - FD_ZERO(&fds); - FD_SET(port->fd, &fds); - if (timeout) { + if (timeout_ms) { gettimeofday(&now, NULL); - if (timercmp(&now, &end, >)) { - DEBUG("Write timed out"); - RETURN_INT(bytes_written); - } + if (timercmp(&now, &end, >)) + /* Timeout has expired. */ + break; timersub(&end, &now, &delta); } - result = select(port->fd + 1, NULL, &fds, NULL, timeout ? &delta : NULL); + result = select(port->fd + 1, NULL, &fds, NULL, timeout_ms ? &delta : NULL); if (result < 0) { if (errno == EINTR) { DEBUG("select() call was interrupted, repeating"); @@ -804,8 +809,8 @@ SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, RETURN_FAIL("select() failed"); } } else if (result == 0) { - DEBUG("Write timed out"); - RETURN_INT(bytes_written); + /* Timeout has expired. */ + break; } /* Do write. */ @@ -824,6 +829,9 @@ SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, ptr += result; } + if (bytes_written < count) + DEBUG("Write timed out"); + RETURN_INT(bytes_written); #endif } @@ -860,9 +868,11 @@ SP_API enum sp_return sp_nonblocking_write(struct sp_port *port, } /* Set timeout. */ - port->timeouts.WriteTotalTimeoutConstant = 0; - if (SetCommTimeouts(port->hdl, &port->timeouts) == 0) - RETURN_FAIL("SetCommTimeouts() failed"); + if (port->timeouts.WriteTotalTimeoutConstant != 0) { + port->timeouts.WriteTotalTimeoutConstant = 0; + if (SetCommTimeouts(port->hdl, &port->timeouts) == 0) + RETURN_FAIL("SetCommTimeouts() failed"); + } /* * Keep writing data until the OS has to actually start an async IO @@ -909,19 +919,39 @@ 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) +{ + DWORD errors; + COMSTAT comstat; + + if (bytes_read == 0) + RETURN_OK(); + + if (ClearCommError(port->hdl, &errors, &comstat) == 0) + RETURN_FAIL("ClearCommError() failed"); + + if (comstat.cbInQue == 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) + size_t count, unsigned int timeout_ms) { - TRACE("%p, %p, %d, %d", port, buf, count, timeout); + TRACE("%p, %p, %d, %d", port, buf, count, timeout_ms); CHECK_OPEN_PORT(); if (!buf) RETURN_ERROR(SP_ERR_ARG, "Null buffer"); - if (timeout) + if (timeout_ms) DEBUG_FMT("Reading %d bytes from port %s, timeout %d ms", - count, port->name, timeout); + count, port->name, timeout_ms); else DEBUG_FMT("Reading %d bytes from port %s, no timeout", count, port->name); @@ -931,39 +961,30 @@ 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. */ - port->timeouts.ReadIntervalTimeout = 0; - port->timeouts.ReadTotalTimeoutConstant = timeout; - if (SetCommTimeouts(port->hdl, &port->timeouts) == 0) - RETURN_FAIL("SetCommTimeouts() failed"); + if (port->timeouts.ReadIntervalTimeout != 0 || + port->timeouts.ReadTotalTimeoutConstant != timeout_ms) { + port->timeouts.ReadIntervalTimeout = 0; + port->timeouts.ReadTotalTimeoutConstant = timeout_ms; + if (SetCommTimeouts(port->hdl, &port->timeouts) == 0) + RETURN_FAIL("SetCommTimeouts() failed"); + } /* Start read. */ - if (ReadFile(port->hdl, buf, count, NULL, &port->read_ovl) == 0) { - if (GetLastError() == ERROR_IO_PENDING) { - DEBUG("Waiting for read to complete"); - GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE); - DEBUG_FMT("Read completed, %d/%d bytes read", bytes_read, count); - } else { - RETURN_FAIL("ReadFile() failed"); - } - } else { + if (ReadFile(port->hdl, buf, count, NULL, &port->read_ovl)) { DEBUG("Read completed immediately"); bytes_read = count; + } else if (GetLastError() == ERROR_IO_PENDING) { + DEBUG("Waiting for read to complete"); + if (GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE) == 0) + RETURN_FAIL("GetOverlappedResult() failed"); + DEBUG_FMT("Read completed, %d/%d bytes read", bytes_read, count); + } else { + RETURN_FAIL("ReadFile() 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); @@ -974,29 +995,30 @@ SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf, fd_set fds; int result; - if (timeout) { + if (timeout_ms) { /* Get time at start of operation. */ gettimeofday(&start, NULL); /* Define duration of timeout. */ - delta.tv_sec = timeout / 1000; - delta.tv_usec = (timeout % 1000) * 1000; + delta.tv_sec = timeout_ms / 1000; + delta.tv_usec = (timeout_ms % 1000) * 1000; /* Calculate time at which we should give up. */ timeradd(&start, &delta, &end); } + FD_ZERO(&fds); + FD_SET(port->fd, &fds); + /* Loop until we have the requested number of bytes. */ while (bytes_read < count) { /* Wait until data is available. */ - FD_ZERO(&fds); - FD_SET(port->fd, &fds); - if (timeout) { + if (timeout_ms) { gettimeofday(&now, NULL); if (timercmp(&now, &end, >)) /* Timeout has expired. */ - RETURN_INT(bytes_read); + break; timersub(&end, &now, &delta); } - result = select(port->fd + 1, &fds, NULL, NULL, timeout ? &delta : NULL); + result = select(port->fd + 1, &fds, NULL, NULL, timeout_ms ? &delta : NULL); if (result < 0) { if (errno == EINTR) { DEBUG("select() call was interrupted, repeating"); @@ -1005,8 +1027,8 @@ SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf, RETURN_FAIL("select() failed"); } } else if (result == 0) { - DEBUG("Read timed out"); - RETURN_INT(bytes_read); + /* Timeout has expired. */ + break; } /* Do read. */ @@ -1025,6 +1047,9 @@ SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf, ptr += result; } + if (bytes_read < count) + DEBUG("Read timed out"); + RETURN_INT(bytes_read); #endif } @@ -1043,14 +1068,15 @@ 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. */ - port->timeouts.ReadIntervalTimeout = MAXDWORD; - port->timeouts.ReadTotalTimeoutConstant = 0; - if (SetCommTimeouts(port->hdl, &port->timeouts) == 0) - RETURN_FAIL("SetCommTimeouts() failed"); + if (port->timeouts.ReadIntervalTimeout != MAXDWORD || + port->timeouts.ReadTotalTimeoutConstant != 0) { + port->timeouts.ReadIntervalTimeout = MAXDWORD; + port->timeouts.ReadTotalTimeoutConstant = 0; + if (SetCommTimeouts(port->hdl, &port->timeouts) == 0) + RETURN_FAIL("SetCommTimeouts() failed"); + } /* Do read. */ if (ReadFile(port->hdl, buf, count, NULL, &port->read_ovl) == 0) @@ -1060,16 +1086,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 @@ -1235,22 +1252,22 @@ SP_API void sp_free_event_set(struct sp_event_set *event_set) } SP_API enum sp_return sp_wait(struct sp_event_set *event_set, - unsigned int timeout) + unsigned int timeout_ms) { - TRACE("%p, %d", event_set, timeout); + TRACE("%p, %d", event_set, timeout_ms); if (!event_set) RETURN_ERROR(SP_ERR_ARG, "Null event set"); #ifdef _WIN32 if (WaitForMultipleObjects(event_set->count, event_set->handles, FALSE, - timeout ? timeout : INFINITE) == WAIT_FAILED) + timeout_ms ? timeout_ms : INFINITE) == WAIT_FAILED) RETURN_FAIL("WaitForMultipleObjects() failed"); RETURN_OK(); #else struct timeval start, delta, now, end = {0, 0}; - int result, timeout_remaining; + int result, timeout_remaining_ms; struct pollfd *pollfds; unsigned int i; @@ -1269,29 +1286,29 @@ SP_API enum sp_return sp_wait(struct sp_event_set *event_set, pollfds[i].events |= POLLERR; } - if (timeout) { + if (timeout_ms) { /* Get time at start of operation. */ gettimeofday(&start, NULL); /* Define duration of timeout. */ - delta.tv_sec = timeout / 1000; - delta.tv_usec = (timeout % 1000) * 1000; + delta.tv_sec = timeout_ms / 1000; + delta.tv_usec = (timeout_ms % 1000) * 1000; /* Calculate time at which we should give up. */ timeradd(&start, &delta, &end); } /* Loop until an event occurs. */ while (1) { - if (timeout) { + if (timeout_ms) { gettimeofday(&now, NULL); if (timercmp(&now, &end, >)) { DEBUG("Wait timed out"); break; } timersub(&end, &now, &delta); - timeout_remaining = delta.tv_sec * 1000 + delta.tv_usec / 1000; + timeout_remaining_ms = delta.tv_sec * 1000 + delta.tv_usec / 1000; } - result = poll(pollfds, event_set->count, timeout ? timeout_remaining : -1); + result = poll(pollfds, event_set->count, timeout_ms ? timeout_remaining_ms : -1); if (result < 0) { if (errno == EINTR) { @@ -2115,6 +2132,8 @@ SP_API enum sp_return sp_set_##x(struct sp_port *port, type x) { \ SP_API enum sp_return sp_get_config_##x(const struct sp_port_config *config, \ type *x) { \ TRACE("%p, %p", config, x); \ + if (!x) \ + RETURN_ERROR(SP_ERR_ARG, "Null result pointer"); \ if (!config) \ RETURN_ERROR(SP_ERR_ARG, "Null config"); \ *x = config->x; \ @@ -2281,10 +2300,10 @@ SP_API char *sp_last_error_message(void) TRACE_VOID(); #ifdef _WIN32 - LPVOID message; + TCHAR *message; DWORD error = GetLastError(); - FormatMessage( + DWORD length = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, @@ -2294,6 +2313,9 @@ SP_API char *sp_last_error_message(void) (LPTSTR) &message, 0, NULL ); + if (length >= 2 && message[length - 2] == '\r') + message[length - 2] = '\0'; + RETURN_STRING(message); #else RETURN_STRING(strerror(errno));