]> sigrok.org Git - libsigrok.git/blobdiff - src/serial.c
Fix log varargs bugs indicated by -Wformat
[libsigrok.git] / src / serial.c
index 0a60d3f14a496761516a4d627baaa97ad52c44db..d9c611bd1164e401a16fc554fc3e29d7304f3eda 100644 (file)
 #include <glib.h>
 #include <glib/gstdio.h>
 #include <libserialport.h>
-#include "libsigrok.h"
+#include <libsigrok/libsigrok.h>
 #include "libsigrok-internal.h"
+#ifdef G_OS_WIN32
+#include <windows.h> /* for HANDLE */
+#endif
 
 /** @cond PRIVATE */
 #define LOG_PREFIX "serial"
@@ -264,7 +267,7 @@ static int _serial_write(struct sr_serial_dev_inst *serial,
                return SR_ERR;
        }
 
-       sr_spew("Wrote %d/%d bytes.", ret, count);
+       sr_spew("Wrote %zd/%zu bytes.", ret, count);
 
        return ret;
 }
@@ -342,7 +345,7 @@ static int _serial_read(struct sr_serial_dev_inst *serial, void *buf,
        }
 
        if (ret > 0)
-               sr_spew("Read %d/%d bytes.", ret, count);
+               sr_spew("Read %zd/%zu bytes.", ret, count);
 
        return ret;
 }
@@ -624,7 +627,7 @@ SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf,
 
        maxlen = *buflen;
        *buflen = len = 0;
-       while(1) {
+       while (1) {
                len = maxlen - *buflen - 1;
                if (len < 1)
                        break;
@@ -680,7 +683,7 @@ SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
 {
        uint64_t start, time, byte_delay_us;
        size_t ibuf, i, maxlen;
-       int len;
+       ssize_t len;
 
        maxlen = *buflen;
 
@@ -693,7 +696,7 @@ SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
        }
 
        /* Assume 8n1 transmission. That is 10 bits for every byte. */
-       byte_delay_us = 10 * (1000000 / baudrate);
+       byte_delay_us = 10 * ((1000 * 1000) / baudrate);
        start = g_get_monotonic_time();
 
        i = ibuf = len = 0;
@@ -713,12 +716,12 @@ SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
                if ((ibuf - i) >= packet_size) {
                        /* We have at least a packet's worth of data. */
                        if (is_valid(&buf[i])) {
-                               sr_spew("Found valid %d-byte packet after "
+                               sr_spew("Found valid %zu-byte packet after "
                                        "%" PRIu64 "ms.", (ibuf - i), time);
                                *buflen = ibuf;
                                return SR_OK;
                        } else {
-                               sr_spew("Got %d bytes, but not a valid "
+                               sr_spew("Got %zu bytes, but not a valid "
                                        "packet.", (ibuf - i));
                        }
                        /* Not a valid packet. Continue searching. */
@@ -726,7 +729,7 @@ SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
                }
                if (time >= timeout_ms) {
                        /* Timeout */
-                       sr_dbg("Detection timed out after %dms.", time);
+                       sr_dbg("Detection timed out after %" PRIu64 "ms.", time);
                        break;
                }
                if (len < 1)
@@ -735,7 +738,7 @@ SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
 
        *buflen = ibuf;
 
-       sr_err("Didn't find a valid packet (read %d bytes).", *buflen);
+       sr_err("Didn't find a valid packet (read %zu bytes).", *buflen);
 
        return SR_ERR;
 }
@@ -744,7 +747,7 @@ SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial,
  * Extract the serial device and options from the options linked list.
  *
  * @param options List of options passed from the command line.
- * @param serial_device Pointer where to store the exctracted serial device.
+ * @param serial_device Pointer where to store the extracted serial device.
  * @param serial_options Pointer where to store the optional extracted serial
  * options.
  *
@@ -784,7 +787,7 @@ SR_PRIV int sr_serial_extract_options(GSList *options, const char **serial_devic
 }
 
 /** @cond PRIVATE */
-#ifdef _WIN32
+#ifdef G_OS_WIN32
 typedef HANDLE event_handle;
 #else
 typedef int event_handle;
@@ -814,12 +817,12 @@ SR_PRIV int serial_source_add(struct sr_session *session,
                return SR_ERR;
        }
 
-       serial->pollfds = (GPollFD *) g_malloc0(sizeof(GPollFD) * serial->event_set->count);
+       serial->pollfds = g_new0(GPollFD, serial->event_set->count);
 
        for (i = 0; i < serial->event_set->count; i++) {
 
-               serial->pollfds[i].fd = ((event_handle *) serial->event_set->handles)[i];
-
+               serial->pollfds[i].fd = (gintptr)
+                       ((event_handle *)serial->event_set->handles)[i];
                mask = serial->event_set->masks[i];
 
                if (mask & SP_EVENT_RX_READY)
@@ -843,6 +846,9 @@ SR_PRIV int serial_source_remove(struct sr_session *session,
 {
        unsigned int i;
 
+       if (!serial->event_set)
+               return SR_OK;
+
        for (i = 0; i < serial->event_set->count; i++)
                if (sr_session_source_remove_pollfd(session, &serial->pollfds[i]) != SR_OK)
                        return SR_ERR;