]> sigrok.org Git - libsigrok.git/blobdiff - hardware/common/serial.c
sr: adjust copyright year
[libsigrok.git] / hardware / common / serial.c
index cebca29e418dd833007c37572a747d2c84d85cbc..de97496a8748d698feed02f9eafe8ce47693a099 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
 #include <fcntl.h>
 #include <unistd.h>
 #ifdef _WIN32
-#include <conio.h>
+#include <windows.h>
 #else
 #include <glob.h>
 #include <termios.h>
 #endif
 #include <stdlib.h>
 #include <glib.h>
-#include <sigrok.h>
+#include "sigrok.h"
+#include "sigrok-internal.h"
 
 // FIXME: Must be moved, or rather passed as function argument.
 #ifdef _WIN32
-HANDLE hdl;
+static HANDLE hdl;
 #endif
 
-char *serial_port_glob[] = {
+static const char *serial_port_glob[] = {
        /* Linux */
        "/dev/ttyS*",
        "/dev/ttyUSB*",
@@ -49,13 +50,16 @@ 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, g_strdup("COM1"));
 #else
        glob_t g;
-       GSList *ports;
        unsigned int i, j;
 
        ports = NULL;
@@ -63,15 +67,15 @@ 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
 
        return ports;
-#endif
 }
 
-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. */
@@ -86,36 +90,92 @@ int serial_open(const char *pathname, int flags)
 #endif
 }
 
-int serial_close(int fd)
+/*
+ * Close the serial port.
+ * Returns 0 upon success, -1 upon failure.
+ */
+SR_PRIV int serial_close(int fd)
 {
 #ifdef _WIN32
-       CloseHandle(hdl);
+       /* Returns non-zero upon success, 0 upon failure. */
+       return (CloseHandle(hdl) == 0) ? -1 : 0;
 #else
+       /* Returns 0 upon success, -1 upon failure. */
        return close(fd);
 #endif
 }
 
-int serial_flush(int fd)
+/*
+ * Flush serial port buffers (if any).
+ * Returns 0 upon success, -1 upon failure.
+ */
+SR_PRIV int serial_flush(int fd)
 {
-
+#ifdef _WIN32
+       /* Returns non-zero upon success, 0 upon failure. */
+       return (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0) ? -1 : 0;
+#else
+       /* Returns 0 upon success, -1 upon failure. */
        return tcflush(fd, TCIOFLUSH);
+#endif
+}
+
+/*
+ * Write a number of bytes to the specified serial port.
+ * Returns the number of bytes written, or -1 upon failure.
+ */
+SR_PRIV int serial_write(int fd, const void *buf, size_t count)
+{
+#ifdef _WIN32
+       DWORD tmp = 0;
+
+       /* FIXME */
+       /* Returns non-zero upon success, 0 upon failure. */
+       WriteFile(hdl, buf, count, &tmp, NULL);
+#else
+       /* Returns the number of bytes written, or -1 upon failure. */
+       return write(fd, buf, count);
+#endif
+}
+
+/*
+ * Read a number of bytes from the specified serial port.
+ * Returns the number of bytes read, or -1 upon failure.
+ */
+SR_PRIV int serial_read(int fd, void *buf, size_t count)
+{
+#ifdef _WIN32
+       DWORD tmp = 0;
+
+       /* FIXME */
+       /* Returns non-zero upon success, 0 upon failure. */
+       return ReadFile(hdl, buf, count, &tmp, NULL);
+#else
+       /* Returns the number of bytes read, or -1 upon failure. */
+       return read(fd, buf, count);
+#endif
 }
 
-void *serial_backup_params(int fd)
+SR_PRIV void *serial_backup_params(int fd)
 {
 #ifdef _WIN32
        /* TODO */
 #else
        struct termios *term;
 
-       term = malloc(sizeof(struct termios));
+       /* TODO: 'term' is never g_free()'d? */
+       if (!(term = g_try_malloc(sizeof(struct termios)))) {
+               sr_err("serial: %s: term malloc failed", __func__);
+               return NULL;
+       }
+
        tcgetattr(fd, term);
 
        return term;
 #endif
 }
 
-void serial_restore_params(int fd, void *backup)
+SR_PRIV void serial_restore_params(int fd, void *backup)
 {
 #ifdef _WIN32
        /* TODO */
@@ -125,21 +185,25 @@ void serial_restore_params(int fd, void *backup)
 }
 
 /*
- * flowcontrol 1 = rts/cts  2 = xon/xoff
- * parity 0 = none, 1 = even, 2 = odd
+ * Set serial parameters.
+ *
+ * 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 speed, int bits, int parity,
+                             int stopbits, int flowcontrol)
 {
 #ifdef _WIN32
        DCB dcb;
 
        if (!GetCommState(hdl, &dcb)) {
                /* TODO: Error handling. */
+               return SR_ERR;
        }
 
        /* TODO: Rename 'speed' to 'baudrate'. */
        switch(speed) {
+       /* TODO: Support for higher baud rates. */
        case 115200:
                dcb.BaudRate = CBR_115200;
                break;
@@ -165,6 +229,7 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
 
        if (!SetCommState(hdl, &dcb)) {
                /* TODO: Error handling. */
+               return SR_ERR;
        }
 #else
        struct termios term;
@@ -183,17 +248,19 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
        case 115200:
                baud = B115200;
                break;
+#ifndef __APPLE__
        case 460800:
                baud = B460800;
                break;
+#endif
        default:
-               return SIGROK_ERR;
+               return SR_ERR;
        }
 
        if (tcgetattr(fd, &term) < 0)
-               return SIGROK_ERR;
+               return SR_ERR;
        if (cfsetispeed(&term, baud) < 0)
-               return SIGROK_ERR;
+               return SR_ERR;
 
        term.c_cflag &= ~CSIZE;
        switch (bits) {
@@ -204,7 +271,7 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
                term.c_cflag |= CS7;
                break;
        default:
-               return SIGROK_ERR;
+               return SR_ERR;
        }
 
        term.c_cflag &= ~CSTOPB;
@@ -214,7 +281,7 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
        case 2:
                term.c_cflag |= CSTOPB;
        default:
-               return SIGROK_ERR;
+               return SR_ERR;
        }
 
        term.c_cflag &= ~(IXON | IXOFF | CRTSCTS);
@@ -225,27 +292,28 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
        case 1:
                term.c_cflag |= CRTSCTS;
        default:
-               return SIGROK_ERR;
+               return SR_ERR;
        }
 
-       term.c_iflag &= ~(IGNPAR | PARODD | PARENB);
+       term.c_iflag &= ~IGNPAR;
+       term.c_cflag &= ~(PARODD | PARENB);
        switch (parity) {
        case 0:
                term.c_iflag |= IGNPAR;
                break;
        case 1:
-               term.c_iflag |= PARENB;
+               term.c_cflag |= PARENB;
                break;
        case 2:
-               term.c_iflag |= PARENB | PARODD;
+               term.c_cflag |= PARENB | PARODD;
                break;
        default:
-               return SIGROK_ERR;
+               return SR_ERR;
        }
 
        if (tcsetattr(fd, TCSADRAIN, &term) < 0)
-               return SIGROK_ERR;
+               return SR_ERR;
 #endif
 
-       return SIGROK_OK;
+       return SR_OK;
 }