darconeous / libnyoci

A flexible CoAP stack for embedded devices and computers. RFC7252 compatible.
Other
27 stars 10 forks source link

nyoci_plat_wait() returns wrong value when timer fires #18

Open snej opened 6 years ago

snej commented 6 years ago

The header doc for nyoci_plat_wait() says "it returns 0 if nyoci_plat_process() should be executed". The POSIX implementation doesn't quite agree with this: it does return 0 if a file descriptor has input, but it returns NYOCI_STATUS_TIMEOUT if a timer is ready to fire. Thus, a loop like this one won't work because it never handles timers until a packet arrives:

            while (true) {
                if (nyoci_plat_wait(_nyoci, MSEC_PER_SEC) == 0)
                    nyoci_plat_process(_nyoci);
            }

This happens because the function doesn't distinguish whether it's the timeout given by the caller that's elapsed, or the timeout till the next timer. In the latter case it should return 0.

Alternatively, the docs could be amended to match the implementation, by saying that if the function returns NYOCI_STATUS_TIMEOUT, then nyoci_handle_timers() should be called.

darconeous commented 6 years ago

Good catch!

NYOCI_STATUS_TIMEOUT should only be returned if the actual call timed out, not if a timer fired—that should just return NYOCI_STATUS_OK (zero).

darconeous commented 6 years ago

Happy to take a patch, otherwise it may be a week or two before I can get to this.