X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=serialport.c;h=c233b28564c799b66cd3e903cd92dd45a9ea6ded;hb=d9cc984fe7acc487d39366a98f1d3759f6129361;hp=74d46eede7953d7ac0c761c18001cd0a3a102ffe;hpb=b457865b8f42b68953bf2910dcc02d3fe27fc2a8;p=libserialport.git diff --git a/serialport.c b/serialport.c index 74d46ee..c233b28 100644 --- a/serialport.c +++ b/serialport.c @@ -55,6 +55,28 @@ static enum sp_return get_config(struct sp_port *port, struct port_data *data, static enum sp_return set_config(struct sp_port *port, struct port_data *data, const struct sp_port_config *config); +#ifndef _WIN32 +static void get_time(struct timeval *time) +{ +#ifdef HAVE_CLOCK_GETTIME + struct timespec ts; + if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1) + clock_gettime(CLOCK_REALTIME, &ts); + time->tv_sec = ts.tv_sec; + time->tv_usec = ts.tv_nsec / 1000; +#elif defined(__APPLE__) + mach_timebase_info_data_t info; + mach_timebase_info(&info); + uint64_t ticks = mach_absolute_time(); + uint64_t ns = (ticks * info.numer) / info.denom; + time->tv_sec = ns / 1000000000; + time->tv_usec = (ns % 1000000000) / 1000; +#else + gettimeofday(time, NULL); +#endif +} +#endif + SP_API enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr) { struct sp_port *port; @@ -541,7 +563,7 @@ SP_API enum sp_return sp_open(struct sp_port *port, enum sp_mode flags) RETURN_CODEVAL(ret); } #else - int flags_local = O_NONBLOCK | O_NOCTTY; + int flags_local = O_NONBLOCK | O_NOCTTY | O_CLOEXEC; /* Map 'flags' to the OS-specific settings. */ if ((flags & SP_MODE_READ_WRITE) == SP_MODE_READ_WRITE) @@ -720,7 +742,9 @@ SP_API enum sp_return sp_drain(struct sp_port *port) #else int result; while (1) { -#ifdef __ANDROID__ +#if defined(__ANDROID__) && (__ANDROID_API__ < 21) + /* Android only has tcdrain from platform 21 onwards. + * On previous API versions, use the ioctl directly. */ int arg = 1; result = ioctl(port->fd, TCSBRK, &arg); #else @@ -826,7 +850,7 @@ SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, if (timeout_ms) { /* Get time at start of operation. */ - gettimeofday(&start, NULL); + get_time(&start); /* Define duration of timeout. */ delta.tv_sec = timeout_ms / 1000; delta.tv_usec = (timeout_ms % 1000) * 1000; @@ -845,7 +869,7 @@ SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, * select() is even run. */ if (timeout_ms && started) { - gettimeofday(&now, NULL); + get_time(&now); if (timercmp(&now, &end, >)) /* Timeout has expired. */ break; @@ -1046,7 +1070,7 @@ SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf, if (timeout_ms) { /* Get time at start of operation. */ - gettimeofday(&start, NULL); + get_time(&start); /* Define duration of timeout. */ delta.tv_sec = timeout_ms / 1000; delta.tv_usec = (timeout_ms % 1000) * 1000; @@ -1065,7 +1089,7 @@ SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf, * select() is even run. */ if (timeout_ms && started) { - gettimeofday(&now, NULL); + get_time(&now); if (timercmp(&now, &end, >)) /* Timeout has expired. */ break; @@ -1183,7 +1207,7 @@ SP_API enum sp_return sp_blocking_read_next(struct sp_port *port, void *buf, if (timeout_ms) { /* Get time at start of operation. */ - gettimeofday(&start, NULL); + get_time(&start); /* Define duration of timeout. */ delta.tv_sec = timeout_ms / 1000; delta.tv_usec = (timeout_ms % 1000) * 1000; @@ -1202,7 +1226,7 @@ SP_API enum sp_return sp_blocking_read_next(struct sp_port *port, void *buf, * select() is even run. */ if (timeout_ms && started) { - gettimeofday(&now, NULL); + get_time(&now); if (timercmp(&now, &end, >)) /* Timeout has expired. */ break; @@ -1485,7 +1509,7 @@ SP_API enum sp_return sp_wait(struct sp_event_set *event_set, if (timeout_ms) { /* Get time at start of operation. */ - gettimeofday(&start, NULL); + get_time(&start); /* Define duration of timeout. */ delta.tv_sec = timeout_ms / 1000; delta.tv_usec = (timeout_ms % 1000) * 1000; @@ -1506,7 +1530,7 @@ SP_API enum sp_return sp_wait(struct sp_event_set *event_set, timeout_overflow = (timeout_ms > INT_MAX); timeout_remaining_ms = timeout_overflow ? INT_MAX : timeout_ms; } else { - gettimeofday(&now, NULL); + get_time(&now); if (timercmp(&now, &end, >)) { DEBUG("Wait timed out"); break;