]> sigrok.org Git - libsigrok.git/blobdiff - hardware/common/serial.c
sr: always turn off ICRNL on serial ports
[libsigrok.git] / hardware / common / serial.c
index 1e03d4dfff1d0896f1d60c365d4f520929e95c75..ad88958a0d0feb5c6601651687acdb1dd593c4cd 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the sigrok project.
  *
- * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
+ * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #endif
 #include <stdlib.h>
 #include <glib.h>
-#include <sigrok.h>
-#include <sigrok-internal.h>
+#include "libsigrok.h"
+#include "libsigrok-internal.h"
 
 // FIXME: Must be moved, or rather passed as function argument.
 #ifdef _WIN32
 static HANDLE hdl;
 #endif
 
-const char *serial_port_glob[] = {
+static const char *serial_port_glob[] = {
        /* Linux */
        "/dev/ttyS*",
        "/dev/ttyUSB*",
@@ -50,14 +50,14 @@ const char *serial_port_glob[] = {
        NULL,
 };
 
-GSList *list_serial_ports(void)
+SR_PRIV GSList *list_serial_ports(void)
 {
        GSList *ports;
 
 #ifdef _WIN32
        /* TODO */
        ports = NULL;
-       ports = g_slist_append(ports, strdup("COM1"));
+       ports = g_slist_append(ports, g_strdup("COM1"));
 #else
        glob_t g;
        unsigned int i, j;
@@ -67,7 +67,7 @@ GSList *list_serial_ports(void)
                if (glob(serial_port_glob[i], 0, NULL, &g))
                        continue;
                for (j = 0; j < g.gl_pathc; j++)
-                       ports = g_slist_append(ports, strdup(g.gl_pathv[j]));
+                       ports = g_slist_append(ports, g_strdup(g.gl_pathv[j]));
                globfree(&g);
        }
 #endif
@@ -75,7 +75,7 @@ GSList *list_serial_ports(void)
        return ports;
 }
 
-int serial_open(const char *pathname, int flags)
+SR_PRIV int serial_open(const char *pathname, int flags)
 {
 #ifdef _WIN32
        /* FIXME: Don't hardcode COM1. */
@@ -94,7 +94,7 @@ int serial_open(const char *pathname, int flags)
  * Close the serial port.
  * Returns 0 upon success, -1 upon failure.
  */
-int serial_close(int fd)
+SR_PRIV int serial_close(int fd)
 {
 #ifdef _WIN32
        /* Returns non-zero upon success, 0 upon failure. */
@@ -109,7 +109,7 @@ int serial_close(int fd)
  * Flush serial port buffers (if any).
  * Returns 0 upon success, -1 upon failure.
  */
-int serial_flush(int fd)
+SR_PRIV int serial_flush(int fd)
 {
 #ifdef _WIN32
        /* Returns non-zero upon success, 0 upon failure. */
@@ -124,7 +124,7 @@ int serial_flush(int fd)
  * Write a number of bytes to the specified serial port.
  * Returns the number of bytes written, or -1 upon failure.
  */
-int serial_write(int fd, const void *buf, size_t count)
+SR_PRIV int serial_write(int fd, const void *buf, size_t count)
 {
 #ifdef _WIN32
        DWORD tmp = 0;
@@ -142,7 +142,7 @@ int serial_write(int fd, const void *buf, size_t count)
  * Read a number of bytes from the specified serial port.
  * Returns the number of bytes read, or -1 upon failure.
  */
-int serial_read(int fd, void *buf, size_t count)
+SR_PRIV int serial_read(int fd, void *buf, size_t count)
 {
 #ifdef _WIN32
        DWORD tmp = 0;
@@ -156,7 +156,7 @@ int serial_read(int fd, void *buf, size_t count)
 #endif
 }
 
-void *serial_backup_params(int fd)
+SR_PRIV void *serial_backup_params(int fd)
 {
 #ifdef _WIN32
        /* TODO */
@@ -175,7 +175,7 @@ void *serial_backup_params(int fd)
 #endif
 }
 
-void serial_restore_params(int fd, void *backup)
+SR_PRIV void serial_restore_params(int fd, void *backup)
 {
 #ifdef _WIN32
        /* TODO */
@@ -190,8 +190,8 @@ void serial_restore_params(int fd, void *backup)
  * flowcontrol: 1 = rts/cts, 2 = xon/xoff
  * parity: 0 = none, 1 = even, 2 = odd
  */
-int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
-                     int flowcontrol)
+SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity,
+                             int stopbits, int flowcontrol)
 {
 #ifdef _WIN32
        DCB dcb;
@@ -201,8 +201,7 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
                return SR_ERR;
        }
 
-       /* TODO: Rename 'speed' to 'baudrate'. */
-       switch(speed) {
+       switch (baudrate) {
        /* TODO: Support for higher baud rates. */
        case 115200:
                dcb.BaudRate = CBR_115200;
@@ -235,7 +234,10 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
        struct termios term;
        speed_t baud;
 
-       switch (speed) {
+       if (tcgetattr(fd, &term) < 0)
+               return SR_ERR;
+
+       switch (baudrate) {
        case 9600:
                baud = B9600;
                break;
@@ -256,8 +258,7 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
        default:
                return SR_ERR;
        }
-
-       if (tcgetattr(fd, &term) < 0)
+       if (cfsetospeed(&term, baud) < 0)
                return SR_ERR;
        if (cfsetispeed(&term, baud) < 0)
                return SR_ERR;
@@ -284,13 +285,17 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
                return SR_ERR;
        }
 
-       term.c_cflag &= ~(IXON | IXOFF | CRTSCTS);
+       term.c_iflag &= ~(IXON | IXOFF);
+       term.c_cflag &= ~CRTSCTS;
        switch (flowcontrol) {
-       case 2:
-               term.c_cflag |= IXON | IXOFF;
+       case 0:
+               /* No flow control. */
                break;
        case 1:
                term.c_cflag |= CRTSCTS;
+       case 2:
+               term.c_iflag |= IXON | IXOFF;
+               break;
        default:
                return SR_ERR;
        }
@@ -298,22 +303,71 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
        term.c_iflag &= ~IGNPAR;
        term.c_cflag &= ~(PARODD | PARENB);
        switch (parity) {
-       case 0:
+       case SERIAL_PARITY_NONE:
                term.c_iflag |= IGNPAR;
                break;
-       case 1:
+       case SERIAL_PARITY_EVEN:
                term.c_cflag |= PARENB;
                break;
-       case 2:
+       case SERIAL_PARITY_ODD:
                term.c_cflag |= PARENB | PARODD;
                break;
        default:
                return SR_ERR;
        }
 
+       /* Some default parameters */
+       term.c_iflag &= ~(ICRNL);
+       term.c_lflag &= ~(ICANON | ECHO);
+
        if (tcsetattr(fd, TCSADRAIN, &term) < 0)
                return SR_ERR;
 #endif
 
        return SR_OK;
 }
+
+#define SERIAL_COMM_SPEC "^(\\d+)/([78])([neo])([12])$"
+SR_PRIV int serial_set_paramstr(int fd, const char *paramstr)
+{
+       GRegex *reg;
+       GMatchInfo *match;
+       int speed, databits, parity, stopbits;
+       char *mstr;
+
+       speed = databits = parity = stopbits = 0;
+       reg = g_regex_new(SERIAL_COMM_SPEC, 0, 0, NULL);
+       if (g_regex_match(reg, paramstr, 0, &match)) {
+               if ((mstr = g_match_info_fetch(match, 1)))
+                       speed = strtoul(mstr, NULL, 10);
+               g_free(mstr);
+               if ((mstr = g_match_info_fetch(match, 2)))
+                       databits = strtoul(mstr, NULL, 10);
+               g_free(mstr);
+               if ((mstr = g_match_info_fetch(match, 3))) {
+                       switch (mstr[0]) {
+                       case 'n':
+                               parity = SERIAL_PARITY_NONE;
+                               break;
+                       case 'e':
+                               parity = SERIAL_PARITY_EVEN;
+                               break;
+                       case 'o':
+                               parity = SERIAL_PARITY_ODD;
+                               break;
+                       }
+               }
+               g_free(mstr);
+               if ((mstr = g_match_info_fetch(match, 4)))
+                       stopbits = strtoul(mstr, NULL, 10);
+               g_free(mstr);
+       }
+       g_match_info_unref(match);
+       g_regex_unref(reg);
+
+       if (speed)
+               return serial_set_params(fd, speed, databits, parity, stopbits, 0);
+       else
+               return SR_ERR_ARG;
+}
+