]> sigrok.org Git - libsigrok.git/commitdiff
serial: Use OS-independent flags for serial port opening.
authorUwe Hermann <redacted>
Sun, 2 Dec 2012 11:45:54 +0000 (12:45 +0100)
committerUwe Hermann <redacted>
Sun, 2 Dec 2012 11:45:54 +0000 (12:45 +0100)
Add SERIAL_RDWR, SERIAL_RDONLY, and SERIAL_NONBLOCK (for now), which are
mapped to the respective OS-specific mechanism in serial_open().

hardware/agilent-dmm/api.c
hardware/colead-slm/api.c
hardware/common/serial.c
hardware/fluke-dmm/api.c
hardware/link-mso19/link-mso19.c
hardware/openbench-logic-sniffer/ols.c
hardware/radioshack-dmm/api.c
hardware/tekpower-dmm/api.c
hardware/tondaj-sl-814/api.c
libsigrok-internal.h

index c7783bca399fb7387f3f825972b3eb178c21c13b..fcd51d54130ffeedb7dc5908fd77a981e441db95 100644 (file)
@@ -145,7 +145,7 @@ static GSList *hw_scan(GSList *options)
        if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
                return NULL;
 
-       if (serial_open(serial, O_RDWR|O_NONBLOCK) != SR_OK)
+       if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
                return NULL;
 
        serial_flush(serial);
@@ -218,7 +218,7 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
                return SR_ERR_BUG;
        }
 
-       if (serial_open(devc->serial, O_RDWR|O_NONBLOCK) != SR_OK)
+       if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
                return SR_ERR;
 
        sdi->status = SR_ST_ACTIVE;
index def7cf04ffb5ec83c4dea1facf8ca7f58ea58dd7..45e19ba670116f82fd8e6db7f688dd3c6ea349e3 100644 (file)
@@ -163,7 +163,7 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
                return SR_ERR_BUG;
        }
 
-       if (serial_open(devc->serial, O_RDWR) != SR_OK)
+       if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK)
                return SR_ERR;
 
        sdi->status = SR_ST_ACTIVE;
index 8ab2d401402ce7ed03dd20784f138f1305b894c4..b9ef46d4cda23960e2035a571d2296a687810db9 100644 (file)
@@ -55,8 +55,8 @@ static HANDLE hdl;
  * Open the specified serial port.
  *
  * @param serial Previously initialized serial port structure.
- * @param flags Flags to use when opening the serial port.
- * TODO: Abstract 'flags', currently they're OS-specific!
+ * @param flags Flags to use when opening the serial port. Possible flags
+ *              include SERIAL_RDWR, SERIAL_RDONLY, SERIAL_NONBLOCK.
  *
  * If the serial structure contains a serialcomm string, it will be
  * passed to serial_set_paramstr() after the port is opened.
@@ -65,6 +65,10 @@ static HANDLE hdl;
  */
 SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags)
 {
+       int flags_local = 0;
+#ifdef _WIN32
+       DWORD desired_access = 0, flags_and_attributes = 0;
+#endif
 
        if (!serial) {
                sr_dbg("Invalid serial port.");
@@ -74,19 +78,36 @@ SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags)
        sr_spew("Opening serial port '%s' (flags %d).", serial->port, flags);
 
 #ifdef _WIN32
-       hdl = CreateFile(serial->port, GENERIC_READ | GENERIC_WRITE, 0, 0,
-                        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
+       /* Map 'flags' to the OS-specific settings. */
+       desired_access |= GENERIC_READ;
+       flags_and_attributes = FILE_ATTRIBUTE_NORMAL;
+       if (flags & SERIAL_RDWR)
+               desired_access |= GENERIC_WRITE;
+       if (flags & SERIAL_NONBLOCK)
+               flags_and_attributes |= FILE_FLAG_OVERLAPPED;
+
+       hdl = CreateFile(serial->port, desired_access, 0, 0,
+                        OPEN_EXISTING, flags_and_attributes, 0);
        if (hdl == INVALID_HANDLE_VALUE) {
                sr_err("Error opening serial port '%s'.", serial->port);
                return SR_ERR;
        }
 #else
-       if ((serial->fd = open(serial->port, flags)) < 0) {
+       /* Map 'flags' to the OS-specific settings. */
+       if (flags & SERIAL_RDWR)
+               flags_local |= O_RDWR;
+       if (flags & SERIAL_RDONLY)
+               flags_local |= O_RDONLY;
+       if (flags & SERIAL_NONBLOCK)
+               flags_local |= O_NONBLOCK;
+
+       if ((serial->fd = open(serial->port, flags_local)) < 0) {
                sr_err("Error opening serial port '%s': %s.", serial->port,
                       strerror(errno));
                return SR_ERR;
-       } else
-               sr_spew("Opened serial port '%s' (fd %d).", serial->port, serial->fd);
+       }
+
+       sr_spew("Opened serial port '%s' (fd %d).", serial->port, serial->fd);
 #endif
 
        if (serial->serialcomm)
index 2d476419307ff79cab7a62691e84930fd1606821..d6391fa08d71f4c14949ab20594d4818ead7c123 100644 (file)
@@ -109,7 +109,7 @@ static GSList *fluke_scan(const char *conn, const char *serialcomm)
        if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
                return NULL;
 
-       if (serial_open(serial, O_RDWR|O_NONBLOCK) != SR_OK)
+       if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
                return NULL;
 
        drvc = di->priv;
@@ -230,7 +230,7 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
                return SR_ERR_BUG;
        }
 
-       if (serial_open(devc->serial, O_RDWR|O_NONBLOCK) != SR_OK)
+       if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
                return SR_ERR;
 
        sdi->status = SR_ST_ACTIVE;
index d1e2fb003cf55ae2c099efb8dfb14fd7f55b88e8..521c8440acb08ab5591eabe88f9beedee9272bb7 100644 (file)
@@ -560,7 +560,7 @@ static int hw_dev_open(int dev_index)
                return ret;
 
        ctx = sdi->priv;
-       sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
+       sdi->serial->fd = serial_open(sdi->serial->port, SERIAL_RDWR);
        if (sdi->serial->fd == -1)
                return ret;
 
index 995fdccf4bdc41a2554752a83f79f5e9d49754ba..a8d9dbea908d11996c341c9183412e90c5cf1902 100644 (file)
 #include "libsigrok-internal.h"
 #include "ols.h"
 
-#ifdef _WIN32
-#define O_NONBLOCK FIONBIO
-#endif
-
 #define SERIALCOMM "115200/8n1"
 
 static const int hwcaps[] = {
@@ -425,7 +421,7 @@ static GSList *hw_scan(GSList *options)
         * have a match.
         */
        sr_info("ols: probing %s .", conn);
-       if (serial_open(serial, O_RDWR | O_NONBLOCK) != SR_OK)
+       if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
                return NULL;
 
        ret = SR_OK;
@@ -503,7 +499,7 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
 
        devc = sdi->priv;
 
-       if (serial_open(devc->serial, O_RDWR) != SR_OK)
+       if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK)
                return SR_ERR;
 
        sdi->status = SR_ST_ACTIVE;
index c51c6f08689c519dbf1d5859e3e9d907e4dac504..47620e3c05fc7b1588a73c8d59c77dc38dfda037 100644 (file)
@@ -112,7 +112,7 @@ static GSList *rs_22_812_scan(const char *conn, const char *serialcomm)
        if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
                return NULL;
 
-       if (serial_open(serial, O_RDONLY|O_NONBLOCK) != SR_OK)
+       if (serial_open(serial, SERIAL_RDONLY | SERIAL_NONBLOCK) != SR_OK)
                return NULL;
 
        sr_info("Probing port '%s' readonly.", conn);
@@ -217,7 +217,7 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
                return SR_ERR_BUG;
        }
 
-       if (serial_open(devc->serial, O_RDONLY) != SR_OK)
+       if (serial_open(devc->serial, SERIAL_RDONLY) != SR_OK)
                return SR_ERR;
 
        sdi->status = SR_ST_ACTIVE;
index 97c705a0c8088481905afdd9fea659f570feaaf5..dd53f4dd3d64b017fc4ac823f66c1799521ea0eb 100644 (file)
@@ -106,7 +106,7 @@ static GSList *lcd14_scan(const char *conn, const char *serialcomm)
        if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
                return NULL;
 
-       if (serial_open(serial, O_RDONLY | O_NONBLOCK) != SR_OK)
+       if (serial_open(serial, SERIAL_RDONLY | SERIAL_NONBLOCK) != SR_OK)
                return NULL;
 
        sr_info("Probing port %s readonly.", conn);
@@ -217,7 +217,7 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
                return SR_ERR_BUG;
        }
 
-       if (serial_open(devc->serial, O_RDONLY) != SR_OK)
+       if (serial_open(devc->serial, SERIAL_RDONLY) != SR_OK)
                return SR_ERR;
 
        sdi->status = SR_ST_ACTIVE;
index c1d8abf8bc2206600ce10a197446470b092253ca..beb8c3378724859c1e67a07530c7877f15dd0afd 100644 (file)
@@ -140,7 +140,7 @@ static GSList *hw_scan(GSList *options)
        if (!(devc->serial = sr_serial_dev_inst_new(conn, serialcomm)))
                return NULL;
 
-       if (serial_open(devc->serial, O_RDWR|O_NONBLOCK) != SR_OK)
+       if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
                return NULL;
 
        sdi->priv = devc;
@@ -172,7 +172,7 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
 
        devc = sdi->priv;
 
-       if (serial_open(devc->serial, O_RDWR|O_NONBLOCK) != SR_OK)
+       if (serial_open(devc->serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
                return SR_ERR;
 
        sdi->status = SR_ST_ACTIVE;
index 5991bc71cde270955418348993be81f28d1fde56..af13e43273ea531580d72156f3e2803bbd49ec17 100644 (file)
@@ -125,6 +125,12 @@ SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
 
 /*--- hardware/common/serial.c ----------------------------------------------*/
 
+enum {
+       SERIAL_RDWR = 1,
+       SERIAL_RDONLY = 2,
+       SERIAL_NONBLOCK = 4,
+};
+
 typedef gboolean (*packet_valid_t)(const uint8_t *buf);
 
 SR_PRIV int serial_open(struct sr_serial_dev_inst *serial, int flags);