DEBUG_FMT("Building structure for port %s", portname);
+#if !defined(_WIN32) && defined(HAVE_REALPATH)
+ /*
+ * get_port_details() below tries to be too smart and figure out
+ * some transport properties from the port name which breaks with
+ * symlinks. Therefore we canonicalize the portname first.
+ */
+ char pathbuf[PATH_MAX + 1];
+ char *res = realpath(portname, pathbuf);
+ if (!res)
+ RETURN_ERROR(SP_ERR_ARG, "Could not retrieve realpath behind port name");
+
+ portname = pathbuf;
+#endif
+
if (!(port = malloc(sizeof(struct sp_port))))
RETURN_ERROR(SP_ERR_MEM, "Port structure malloc failed");
void *tmp;
unsigned int count;
- for (count = 0; list[count]; count++);
+ for (count = 0; list[count]; count++)
+ ;
if (!(tmp = realloc(list, sizeof(struct sp_port *) * (count + 2))))
goto fail;
list = tmp;
#endif
}
+#ifdef _WIN32
+static enum sp_return await_write_completion(struct sp_port *port)
+{
+ TRACE("%p", port);
+ DWORD bytes_written;
+ BOOL result;
+
+ /* Wait for previous non-blocking write to complete, if any. */
+ if (port->writing) {
+ DEBUG("Waiting for previous write to complete");
+ result = GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE);
+ port->writing = 0;
+ if (!result)
+ RETURN_FAIL("Previous write failed to complete");
+ DEBUG("Previous write completed");
+ }
+
+ RETURN_OK();
+}
+#endif
+
SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf,
size_t count, unsigned int timeout_ms)
{
#ifdef _WIN32
DWORD bytes_written = 0;
- BOOL result;
- /* Wait for previous non-blocking write to complete, if any. */
- if (port->writing) {
- DEBUG("Waiting for previous write to complete");
- result = GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE);
- port->writing = 0;
- if (!result)
- RETURN_FAIL("Previous write failed to complete");
- DEBUG("Previous write completed");
- }
+ TRY(await_write_completion(port));
/* Set timeout. */
if (port->timeouts.WriteTotalTimeoutConstant != timeout_ms) {
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");
+ if (GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE) == 0) {
+ if (GetLastError() == ERROR_SEM_TIMEOUT) {
+ DEBUG("Write timed out");
+ RETURN_INT(0);
+ } else {
+ RETURN_FAIL("GetOverlappedResult() failed");
+ }
+ }
DEBUG_FMT("Write completed, %d/%d bytes written", bytes_written, count);
RETURN_INT(bytes_written);
} else {
/* Returns the number of bytes written, or -1 upon failure. */
ssize_t written = write(port->fd, buf, count);
- if (written < 0)
- RETURN_FAIL("write() failed");
- else
+ if (written < 0) {
+ if (errno == EAGAIN)
+ // Buffer is full, no bytes written.
+ RETURN_INT(0);
+ else
+ RETURN_FAIL("write() failed");
+ } else {
RETURN_INT(written);
+ }
#endif
}
#else
size_t bytes_read = 0;
- unsigned char *ptr = (unsigned char *) buf;
+ unsigned char *ptr = (unsigned char *)buf;
struct timeval start, delta, now, end = {0, 0};
int started = 0;
fd_set fds;
#else
struct timeval start, delta, now, end = {0, 0};
const struct timeval max_delta = {
- (INT_MAX / 1000), (INT_MAX % 1000) * 1000};
+ (INT_MAX / 1000), (INT_MAX % 1000) * 1000
+ };
int started = 0, timeout_overflow = 0;
int result, timeout_remaining_ms;
struct pollfd *pollfds;
RETURN_ERROR(SP_ERR_MEM, "pollfds malloc() failed");
for (i = 0; i < event_set->count; i++) {
- pollfds[i].fd = ((int *) event_set->handles)[i];
+ pollfds[i].fd = ((int *)event_set->handles)[i];
pollfds[i].events = 0;
pollfds[i].revents = 0;
if (event_set->masks[i] & SP_EVENT_RX_READY)
DEBUG_FMT("Setting configuration for port %s", port->name);
#ifdef _WIN32
+
+ TRY(await_write_completion(port));
+
if (config->baudrate >= 0) {
for (i = 0; i < NUM_STD_BAUDRATES; i++) {
if (config->baudrate == std_baudrates[i].value) {