-#ifdef __linux__
- struct udev *ud;
- struct udev_enumerate *ud_enumerate;
- struct udev_list_entry *ud_list;
- struct udev_list_entry *ud_entry;
- const char *path;
- struct udev_device *ud_dev, *ud_parent;
- const char *name;
- const char *driver;
- int fd, ioctl_result;
- struct serial_struct serial_info;
-
- ud = udev_new();
- ud_enumerate = udev_enumerate_new(ud);
- udev_enumerate_add_match_subsystem(ud_enumerate, "tty");
- udev_enumerate_scan_devices(ud_enumerate);
- ud_list = udev_enumerate_get_list_entry(ud_enumerate);
- udev_list_entry_foreach(ud_entry, ud_list)
+}
+
+SP_API enum sp_return sp_blocking_read(struct sp_port *port, 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_FMT("Reading %d bytes from port %s, timeout %d ms",
+ count, port->name, timeout);
+ else
+ DEBUG_FMT("Reading %d bytes from port %s, no timeout",
+ count, port->name);
+
+ if (count == 0)
+ RETURN_INT(0);
+
+#ifdef _WIN32
+ DWORD bytes_read = 0;
+
+ /* Set timeout. */
+ port->timeouts.ReadIntervalTimeout = 0;
+ port->timeouts.ReadTotalTimeoutConstant = timeout;
+ 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 {
+ DEBUG("Read completed immediately");
+ bytes_read = count;
+ }
+
+ /* Start background operation for subsequent events. */
+ if (WaitCommEvent(port->hdl, &port->events, &port->wait_ovl) == 0) {
+ if (GetLastError() != ERROR_IO_PENDING)
+ RETURN_FAIL("WaitCommEvent() failed");
+ }
+
+ RETURN_INT(bytes_read);
+
+#else
+ size_t bytes_read = 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) * 1000;
+ /* Calculate time at which we should give up. */
+ timeradd(&start, &delta, &end);
+ }
+
+ /* Loop until we have the requested number of bytes. */
+ while (bytes_read < count)