]> sigrok.org Git - libserialport.git/blobdiff - serialport.c
windows: Fix a build error.
[libserialport.git] / serialport.c
index d0618bb91df7f13b225085948122c7efed7a18f0..2c1b68217eaf0747d93761fc5af61a6accaa45c4 100644 (file)
@@ -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;
@@ -793,6 +815,10 @@ SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf,
                        RETURN_FAIL("SetCommTimeouts() failed");
        }
 
+       /* Reduce count if it exceeds the WriteFile limit. */
+       if (count > WRITEFILE_MAX_SIZE)
+               count = WRITEFILE_MAX_SIZE;
+
        /* Start write. */
        if (WriteFile(port->hdl, buf, count, NULL, &port->write_ovl)) {
                DEBUG("Write completed immediately");
@@ -822,7 +848,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;
@@ -841,7 +867,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;
@@ -921,6 +947,10 @@ SP_API enum sp_return sp_nonblocking_write(struct sp_port *port,
                        RETURN_FAIL("SetCommTimeouts() failed");
        }
 
+       /* Reduce count if it exceeds the WriteFile limit. */
+       if (count > WRITEFILE_MAX_SIZE)
+               count = WRITEFILE_MAX_SIZE;
+
        /* Copy data to our write buffer. */
        buf_bytes = min(port->write_buf_size, count);
        memcpy(port->write_buf, buf, buf_bytes);
@@ -1038,7 +1068,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;
@@ -1057,7 +1087,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;
@@ -1175,7 +1205,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;
@@ -1194,7 +1224,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;
@@ -1477,7 +1507,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;
@@ -1498,7 +1528,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;