Discussion:
FTDI user space on Windows
Dirk Hohndel
2018-09-10 05:05:01 UTC
Permalink
This should be so simple. libtfdi is available in MXE, so just build against it, right?
Except this:

static dc_status_t serial_ftdi_sleep (void *io, unsigned int timeout)
{
ftdi_serial_t *device = io;

if (device == NULL)
return DC_STATUS_INVALIDARGS;

INFO (device->context, "Sleep: value=%u", timeout);

struct timespec ts;
ts.tv_sec = (timeout / 1000);
ts.tv_nsec = (timeout % 1000) * 1000000;

while (nanosleep (&ts, &ts) != 0) {
if (errno != EINTR ) {
SYSERROR (device->context, errno);
return DC_STATUS_IO;
}
}

return DC_STATUS_SUCCESS;
}

Turns out Windows doesn't have nanosleep. And I can't seem to find a function with comparable semantic, unless I use the one in the pthread library that is provided by MinGW which is not what we want to do.
So the question becomes how to represent this with something that gives us the same semantic / functionality.

I am anything but a Windows expert. Suggestions welcome.

Thanks

/D
Lubomir I. Ivanov
2018-09-11 21:00:50 UTC
Permalink
Post by Dirk Hohndel
This should be so simple. libtfdi is available in MXE, so just build against it, right?
static dc_status_t serial_ftdi_sleep (void *io, unsigned int timeout)
{
ftdi_serial_t *device = io;
if (device == NULL)
return DC_STATUS_INVALIDARGS;
INFO (device->context, "Sleep: value=%u", timeout);
struct timespec ts;
ts.tv_sec = (timeout / 1000);
ts.tv_nsec = (timeout % 1000) * 1000000;
while (nanosleep (&ts, &ts) != 0) {
if (errno != EINTR ) {
SYSERROR (device->context, errno);
return DC_STATUS_IO;
}
}
return DC_STATUS_SUCCESS;
}
Turns out Windows doesn't have nanosleep. And I can't seem to find a
function with comparable semantic, unless I use the one in the pthread
library that is provided by MinGW which is not what we want to do.
So the question becomes how to represent this with something that gives us
the same semantic / functionality.
I am anything but a Windows expert. Suggestions welcome.
i can have a look a bit later today.

lubomir
--
Lubomir I. Ivanov
2018-09-12 00:46:46 UTC
Permalink
Post by Dirk Hohndel
This should be so simple. libtfdi is available in MXE, so just build against it, right?
static dc_status_t serial_ftdi_sleep (void *io, unsigned int timeout)
{
ftdi_serial_t *device = io;
if (device == NULL)
return DC_STATUS_INVALIDARGS;
INFO (device->context, "Sleep: value=%u", timeout);
struct timespec ts;
ts.tv_sec = (timeout / 1000);
ts.tv_nsec = (timeout % 1000) * 1000000;
while (nanosleep (&ts, &ts) != 0) {
if (errno != EINTR ) {
SYSERROR (device->context, errno);
return DC_STATUS_IO;
}
}
return DC_STATUS_SUCCESS;
}
Turns out Windows doesn't have nanosleep. And I can't seem to find a
function with comparable semantic, unless I use the one in the pthread
library that is provided by MinGW which is not what we want to do.
So the question becomes how to represent this with something that gives us
the same semantic / functionality.
proposed patch:
https://github.com/Subsurface-divelog/subsurface/pull/1668

lubomir
--
Dirk Hohndel
2018-09-12 01:17:14 UTC
Permalink
https://github.com/Subsurface-divelog/subsurface/pull/1668 <https://github.com/Subsurface-divelog/subsurface/pull/1668>
Oh how simple. I am traveling and don't have a Windows system and FTDI device with me, but I'll try this on Friday.

Thanks

/D
Bill Perry
2018-09-13 18:44:14 UTC
Permalink
Just a reminder that last summer I saw that the code in serial_ftdi_read() doesn't guarantee that a timeout occurs
in the desired timeout period.
Due to the way it is written, you can get MUCH longer delays when the bytes don't stream in right away or
This was an issue I was fighting last year when I was trying to get retries to work on the Pelagic data cable with Android.
It actually affects any ftdi device.
This is likely an issue on Windows as well.

ftdi_read_data() took a few milliseconds of time before returning and that time is not accounted for.
And on android the the LIBUSB_ERROR_INTERRUPTED was also happening.
This caused the while loop to loop for several minutes before timing out.
My solution (which I posted a patch for to the list back in Nov 2017) was to get rid of slept and to always sleep for 1ms.
Then rather than comparing slept >= timeout - which doesn't work, was to use gettimofday()
to check for the elapsed time to check for the desired timeout period.

--- bill
Lubomir I. Ivanov
2018-09-14 21:39:50 UTC
Permalink
Post by Bill Perry
Just a reminder that last summer I saw that the code in serial_ftdi_read() doesn't guarantee that a timeout occurs
in the desired timeout period.
Due to the way it is written, you can get MUCH longer delays when the bytes don't stream in right away or
This was an issue I was fighting last year when I was trying to get retries to work on the Pelagic data cable with Android.
It actually affects any ftdi device.
This is likely an issue on Windows as well.
ftdi_read_data() took a few milliseconds of time before returning and that time is not accounted for.
And on android the the LIBUSB_ERROR_INTERRUPTED was also happening.
This caused the while loop to loop for several minutes before timing out.
My solution (which I posted a patch for to the list back in Nov 2017) was to get rid of slept and to always sleep for 1ms.
Then rather than comparing slept >= timeout - which doesn't work, was to use gettimofday()
to check for the elapsed time to check for the desired timeout period.
sleeping for small chunks of time can end up being better.
would you like to submit a github PR to address this improvement as
per your experiments?

lubomir
--
Bill Perry
2018-09-14 22:48:29 UTC
Permalink
Yes, I can do PR. (once I figure out how to do signing)
Post by Lubomir I. Ivanov
sleeping for small chunks of time can end up being better.
The modification I did used the exact same constant 1ms sleep after each ftdi_read_data() call that the current code does but it checked elapsed time prior each 1ms sleep
rather than counting the number of 1ms sleeps until they equaled the desired millisecond timeout.
Counting the number of 1ms sleeps done does not work since it does account for
the time inside the loop (which can be significant like 100+ ms) since sometimes the ftdi_read_data() doesn't return immediately
when there is no data.
That is why looking at elapsed time is required.

Are you wanting something smarter than changing the way the loop exits by checking elapsed time instead of blindly looping {timout} times?


--- bill
Post by Lubomir I. Ivanov
Post by Bill Perry
Just a reminder that last summer I saw that the code in serial_ftdi_read() doesn't guarantee that a timeout occurs
in the desired timeout period.
Due to the way it is written, you can get MUCH longer delays when the bytes don't stream in right away or
This was an issue I was fighting last year when I was trying to get retries to work on the Pelagic data cable with Android.
It actually affects any ftdi device.
This is likely an issue on Windows as well.
ftdi_read_data() took a few milliseconds of time before returning and that time is not accounted for.
And on android the the LIBUSB_ERROR_INTERRUPTED was also happening.
This caused the while loop to loop for several minutes before timing out.
My solution (which I posted a patch for to the list back in Nov 2017) was to get rid of slept and to always sleep for 1ms.
Then rather than comparing slept >= timeout - which doesn't work, was to use gettimofday()
to check for the elapsed time to check for the desired timeout period.
sleeping for small chunks of time can end up being better.
would you like to submit a github PR to address this improvement as
per your experiments?
lubomir
--
Lubomir I. Ivanov
2018-09-14 23:38:51 UTC
Permalink
Post by Bill Perry
Yes, I can do PR. (once I figure out how to do signing)
feel free to ask any github related questions, if any.
Post by Bill Perry
Post by Lubomir I. Ivanov
sleeping for small chunks of time can end up being better.
The modification I did used the exact same constant 1ms sleep after each ftdi_read_data() call that the current code does but it checked elapsed time prior each 1ms sleep
rather than counting the number of 1ms sleeps until they equaled the desired millisecond timeout.
Counting the number of 1ms sleeps done does not work since it does account for
the time inside the loop (which can be significant like 100+ ms) since sometimes the ftdi_read_data() doesn't return immediately
when there is no data.
That is why looking at elapsed time is required.
Are you wanting something smarter than changing the way the loop exits by checking elapsed time instead of blindly looping {timout} times?
ah, i misunderstood the initial comment, sorry.
i'd encourage you to send the PR so that we can have a look at the
code and discuss it.

thanks
lubomir
--

Loading...