+enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t count, unsigned int timeout)
+{
+ TRACE("%p, %p, %d, %d", port, buf, count, timeout);
+
+ CHECK_OPEN_PORT();
+
+ if (!buf)
+ RETURN_ERROR(SP_ERR_ARG, "Null buffer");
+
+ if (timeout)
+ DEBUG("Writing %d bytes to port %s, timeout %d ms", count, port->name, timeout);
+ else
+ DEBUG("Writing %d bytes to port %s, no timeout", count, port->name);
+
+ if (count == 0)
+ RETURN_VALUE("0", 0);
+
+#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");
+ }
+
+ /* Set timeout. */
+ port->timeouts.WriteTotalTimeoutConstant = timeout;
+ 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("Write completed, %d/%d bytes written", bytes_written, count);
+ RETURN_VALUE("%d", bytes_written);
+ } else {
+ RETURN_FAIL("WriteFile() failed");
+ }
+ } else {
+ DEBUG("Write completed immediately");
+ RETURN_VALUE("%d", count);
+ }
+#else
+ size_t bytes_written = 0;
+ unsigned char *ptr = (unsigned char *) buf;
+ struct timeval start, delta, now, end = {0, 0};
+ fd_set fds;
+ int result;
+
+ if (timeout) {
+ /* Get time at start of operation. */
+ gettimeofday(&start, NULL);
+ /* Define duration of timeout. */
+ delta.tv_sec = timeout / 1000;
+ delta.tv_usec = timeout % 1000;
+ /* Calculate time at which we should give up. */
+ timeradd(&start, &delta, &end);
+ }
+
+ /* 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) {
+ gettimeofday(&now, NULL);
+ if (timercmp(&now, &end, >)) {
+ DEBUG("write timed out");
+ RETURN_VALUE("%d", bytes_written);
+ }
+ timersub(&end, &now, &delta);
+ }
+ result = select(port->fd + 1, NULL, &fds, NULL, timeout ? &delta : NULL);
+ if (result < 0)
+ RETURN_FAIL("select() failed");
+ if (result == 0) {
+ DEBUG("write timed out");
+ RETURN_VALUE("%d", bytes_written);
+ }
+
+ /* Do write. */
+ result = write(port->fd, ptr, count - bytes_written);
+
+ if (result < 0) {
+ if (errno == EAGAIN)
+ /* This shouldn't happen because we did a select() first, but handle anyway. */
+ continue;
+ else
+ /* This is an actual failure. */
+ RETURN_FAIL("write() failed");
+ }
+
+ bytes_written += result;
+ ptr += result;
+ }
+
+ RETURN_VALUE("%d", bytes_written);
+#endif
+}
+
+enum sp_return sp_nonblocking_write(struct sp_port *port, const void *buf, size_t count)