]> sigrok.org Git - libsigrok.git/blobdiff - hardware/common/serial.c
libsigrok: Fix #includes.
[libsigrok.git] / hardware / common / serial.c
index 853c26afb06c957e45fd0a3c2c715a75a49cb273..80b6f22cd2a37e2be1edae8e1344956343397863 100644 (file)
 #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[] = {
+const char *serial_port_glob[] = {
        /* Linux */
        "/dev/ttyS*",
        "/dev/ttyUSB*",
@@ -162,7 +163,12 @@ void *serial_backup_params(int fd)
 #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;
@@ -192,7 +198,7 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
 
        if (!GetCommState(hdl, &dcb)) {
                /* TODO: Error handling. */
-               return SIGROK_ERR;
+               return SR_ERR;
        }
 
        /* TODO: Rename 'speed' to 'baudrate'. */
@@ -223,7 +229,7 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
 
        if (!SetCommState(hdl, &dcb)) {
                /* TODO: Error handling. */
-               return SIGROK_ERR;
+               return SR_ERR;
        }
 #else
        struct termios term;
@@ -242,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) {
@@ -263,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;
@@ -273,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);
@@ -284,7 +292,7 @@ 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;
@@ -300,12 +308,12 @@ int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
                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;
 }