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);
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;
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;
* 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.
*/
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.");
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)
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;
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;
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;
#include "libsigrok-internal.h"
#include "ols.h"
-#ifdef _WIN32
-#define O_NONBLOCK FIONBIO
-#endif
-
#define SERIALCOMM "115200/8n1"
static const int hwcaps[] = {
* 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;
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;
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);
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;
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);
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;
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;
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;
/*--- 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);