2010-04-02 18:18:27 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the sigrok project.
|
|
|
|
*
|
2012-02-13 13:31:51 +00:00
|
|
|
* Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
|
2010-04-02 18:18:27 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-04-08 14:44:13 +00:00
|
|
|
#include <string.h>
|
2010-04-09 03:15:27 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2010-04-22 01:39:02 +00:00
|
|
|
#ifdef _WIN32
|
2011-02-02 12:13:13 +00:00
|
|
|
#include <windows.h>
|
2010-04-22 01:39:02 +00:00
|
|
|
#else
|
|
|
|
#include <glob.h>
|
2010-04-09 03:15:27 +00:00
|
|
|
#include <termios.h>
|
2010-04-22 01:39:02 +00:00
|
|
|
#endif
|
2010-04-09 03:15:27 +00:00
|
|
|
#include <stdlib.h>
|
2010-04-02 18:18:27 +00:00
|
|
|
#include <glib.h>
|
2011-12-28 22:07:08 +00:00
|
|
|
#include "sigrok.h"
|
|
|
|
#include "sigrok-internal.h"
|
2010-04-02 18:18:27 +00:00
|
|
|
|
2010-04-22 01:39:02 +00:00
|
|
|
// FIXME: Must be moved, or rather passed as function argument.
|
|
|
|
#ifdef _WIN32
|
2011-12-28 21:55:21 +00:00
|
|
|
static HANDLE hdl;
|
2010-04-22 01:39:02 +00:00
|
|
|
#endif
|
|
|
|
|
2012-02-01 22:40:35 +00:00
|
|
|
static const char *serial_port_glob[] = {
|
2010-04-02 18:18:27 +00:00
|
|
|
/* Linux */
|
|
|
|
"/dev/ttyS*",
|
|
|
|
"/dev/ttyUSB*",
|
|
|
|
"/dev/ttyACM*",
|
|
|
|
/* MacOS X */
|
|
|
|
"/dev/ttys*",
|
|
|
|
"/dev/tty.USB-*",
|
|
|
|
"/dev/tty.Modem-*",
|
2010-04-15 18:55:57 +00:00
|
|
|
NULL,
|
2010-04-02 18:18:27 +00:00
|
|
|
};
|
|
|
|
|
2012-02-01 22:40:35 +00:00
|
|
|
SR_PRIV GSList *list_serial_ports(void)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
2011-01-11 23:43:00 +00:00
|
|
|
GSList *ports;
|
|
|
|
|
2010-04-22 01:39:02 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
/* TODO */
|
2011-01-11 23:43:00 +00:00
|
|
|
ports = NULL;
|
2012-02-11 19:06:46 +00:00
|
|
|
ports = g_slist_append(ports, g_strdup("COM1"));
|
2010-04-22 01:39:02 +00:00
|
|
|
#else
|
2010-04-02 18:18:27 +00:00
|
|
|
glob_t g;
|
2010-04-09 20:18:46 +00:00
|
|
|
unsigned int i, j;
|
2010-04-02 18:18:27 +00:00
|
|
|
|
|
|
|
ports = NULL;
|
2010-04-15 18:55:57 +00:00
|
|
|
for (i = 0; serial_port_glob[i]; i++) {
|
2010-05-09 21:04:24 +00:00
|
|
|
if (glob(serial_port_glob[i], 0, NULL, &g))
|
|
|
|
continue;
|
|
|
|
for (j = 0; j < g.gl_pathc; j++)
|
2012-02-11 19:06:46 +00:00
|
|
|
ports = g_slist_append(ports, g_strdup(g.gl_pathv[j]));
|
2010-05-09 21:04:24 +00:00
|
|
|
globfree(&g);
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
2011-01-11 23:43:00 +00:00
|
|
|
#endif
|
2010-04-02 18:18:27 +00:00
|
|
|
|
|
|
|
return ports;
|
|
|
|
}
|
|
|
|
|
2012-02-01 22:40:35 +00:00
|
|
|
SR_PRIV int serial_open(const char *pathname, int flags)
|
2010-04-09 03:15:27 +00:00
|
|
|
{
|
2010-04-22 01:39:02 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
/* FIXME: Don't hardcode COM1. */
|
|
|
|
hdl = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0,
|
|
|
|
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
|
|
|
|
if (hdl == INVALID_HANDLE_VALUE) {
|
|
|
|
/* TODO: Error handling. */
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#else
|
2010-04-09 03:15:27 +00:00
|
|
|
return open(pathname, flags);
|
2010-04-22 01:39:02 +00:00
|
|
|
#endif
|
2010-04-09 03:15:27 +00:00
|
|
|
}
|
|
|
|
|
2011-01-11 23:43:00 +00:00
|
|
|
/*
|
|
|
|
* Close the serial port.
|
|
|
|
* Returns 0 upon success, -1 upon failure.
|
|
|
|
*/
|
2012-02-01 22:40:35 +00:00
|
|
|
SR_PRIV int serial_close(int fd)
|
2010-04-09 03:15:27 +00:00
|
|
|
{
|
2010-04-22 01:39:02 +00:00
|
|
|
#ifdef _WIN32
|
2011-01-11 23:43:00 +00:00
|
|
|
/* Returns non-zero upon success, 0 upon failure. */
|
|
|
|
return (CloseHandle(hdl) == 0) ? -1 : 0;
|
2010-04-22 01:39:02 +00:00
|
|
|
#else
|
2011-01-11 23:43:00 +00:00
|
|
|
/* Returns 0 upon success, -1 upon failure. */
|
2010-04-09 03:15:27 +00:00
|
|
|
return close(fd);
|
2010-04-22 01:39:02 +00:00
|
|
|
#endif
|
2010-04-09 03:15:27 +00:00
|
|
|
}
|
|
|
|
|
2011-01-11 00:25:10 +00:00
|
|
|
/*
|
|
|
|
* Flush serial port buffers (if any).
|
|
|
|
* Returns 0 upon success, -1 upon failure.
|
|
|
|
*/
|
2012-02-01 22:40:35 +00:00
|
|
|
SR_PRIV int serial_flush(int fd)
|
2010-08-12 04:02:25 +00:00
|
|
|
{
|
2011-01-11 00:25:10 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
/* Returns non-zero upon success, 0 upon failure. */
|
2011-01-11 23:43:00 +00:00
|
|
|
return (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0) ? -1 : 0;
|
2011-01-11 00:25:10 +00:00
|
|
|
#else
|
|
|
|
/* Returns 0 upon success, -1 upon failure. */
|
2010-08-12 04:04:44 +00:00
|
|
|
return tcflush(fd, TCIOFLUSH);
|
2011-01-11 00:25:10 +00:00
|
|
|
#endif
|
2010-08-12 04:02:25 +00:00
|
|
|
}
|
|
|
|
|
2011-01-11 23:43:00 +00:00
|
|
|
/*
|
|
|
|
* Write a number of bytes to the specified serial port.
|
|
|
|
* Returns the number of bytes written, or -1 upon failure.
|
|
|
|
*/
|
2012-02-01 22:40:35 +00:00
|
|
|
SR_PRIV int serial_write(int fd, const void *buf, size_t count)
|
2011-01-11 23:43:00 +00:00
|
|
|
{
|
|
|
|
#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.
|
|
|
|
*/
|
2012-02-01 22:40:35 +00:00
|
|
|
SR_PRIV int serial_read(int fd, void *buf, size_t count)
|
2011-01-11 23:43:00 +00:00
|
|
|
{
|
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
2012-02-01 22:40:35 +00:00
|
|
|
SR_PRIV void *serial_backup_params(int fd)
|
2010-04-09 03:15:27 +00:00
|
|
|
{
|
2010-04-22 01:39:02 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
/* TODO */
|
|
|
|
#else
|
2010-04-09 03:15:27 +00:00
|
|
|
struct termios *term;
|
|
|
|
|
2011-04-16 14:07:28 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2010-04-09 03:15:27 +00:00
|
|
|
tcgetattr(fd, term);
|
|
|
|
|
|
|
|
return term;
|
2010-04-22 01:39:02 +00:00
|
|
|
#endif
|
2010-04-09 03:15:27 +00:00
|
|
|
}
|
|
|
|
|
2012-02-01 22:40:35 +00:00
|
|
|
SR_PRIV void serial_restore_params(int fd, void *backup)
|
2010-04-09 03:15:27 +00:00
|
|
|
{
|
2010-04-22 01:39:02 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
/* TODO */
|
|
|
|
#else
|
2010-04-15 18:55:57 +00:00
|
|
|
tcsetattr(fd, TCSADRAIN, (struct termios *)backup);
|
2010-04-22 01:39:02 +00:00
|
|
|
#endif
|
2010-04-09 03:15:27 +00:00
|
|
|
}
|
|
|
|
|
2011-01-10 04:21:07 +00:00
|
|
|
/*
|
2011-01-11 23:43:00 +00:00
|
|
|
* Set serial parameters.
|
|
|
|
*
|
|
|
|
* flowcontrol: 1 = rts/cts, 2 = xon/xoff
|
|
|
|
* parity: 0 = none, 1 = even, 2 = odd
|
2011-01-10 04:21:07 +00:00
|
|
|
*/
|
2012-03-04 14:08:11 +00:00
|
|
|
SR_PRIV int serial_set_params(int fd, int baudrate, int bits, int parity,
|
2012-02-01 22:40:35 +00:00
|
|
|
int stopbits, int flowcontrol)
|
2010-04-09 03:15:27 +00:00
|
|
|
{
|
2010-04-22 01:39:02 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
DCB dcb;
|
|
|
|
|
|
|
|
if (!GetCommState(hdl, &dcb)) {
|
|
|
|
/* TODO: Error handling. */
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_ERR;
|
2010-04-22 01:39:02 +00:00
|
|
|
}
|
|
|
|
|
2012-03-04 14:08:11 +00:00
|
|
|
switch (baudrate) {
|
2011-01-11 00:25:10 +00:00
|
|
|
/* TODO: Support for higher baud rates. */
|
2010-04-22 01:39:02 +00:00
|
|
|
case 115200:
|
|
|
|
dcb.BaudRate = CBR_115200;
|
|
|
|
break;
|
|
|
|
case 57600:
|
|
|
|
dcb.BaudRate = CBR_57600;
|
|
|
|
break;
|
|
|
|
case 38400:
|
|
|
|
dcb.BaudRate = CBR_38400;
|
|
|
|
break;
|
|
|
|
case 19200:
|
|
|
|
dcb.BaudRate = CBR_19200;
|
|
|
|
break;
|
|
|
|
case 9600:
|
|
|
|
dcb.BaudRate = CBR_9600;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* TODO: Error handling. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
dcb.ByteSize = bits;
|
|
|
|
dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */
|
|
|
|
dcb.StopBits = ONESTOPBIT; /* TODO: Don't hardcode. */
|
|
|
|
|
|
|
|
if (!SetCommState(hdl, &dcb)) {
|
|
|
|
/* TODO: Error handling. */
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_ERR;
|
2010-04-22 01:39:02 +00:00
|
|
|
}
|
|
|
|
#else
|
2010-04-09 03:15:27 +00:00
|
|
|
struct termios term;
|
2011-01-10 04:21:07 +00:00
|
|
|
speed_t baud;
|
2010-04-09 03:15:27 +00:00
|
|
|
|
2012-03-04 14:08:11 +00:00
|
|
|
switch (baudrate) {
|
2011-01-10 04:21:07 +00:00
|
|
|
case 9600:
|
|
|
|
baud = B9600;
|
|
|
|
break;
|
|
|
|
case 38400:
|
|
|
|
baud = B38400;
|
|
|
|
break;
|
|
|
|
case 57600:
|
|
|
|
baud = B57600;
|
|
|
|
break;
|
|
|
|
case 115200:
|
|
|
|
baud = B115200;
|
|
|
|
break;
|
2011-06-20 21:43:44 +00:00
|
|
|
#ifndef __APPLE__
|
2011-01-10 04:21:07 +00:00
|
|
|
case 460800:
|
|
|
|
baud = B460800;
|
|
|
|
break;
|
2011-06-20 21:43:44 +00:00
|
|
|
#endif
|
2011-01-10 04:21:07 +00:00
|
|
|
default:
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_ERR;
|
2011-01-10 04:21:07 +00:00
|
|
|
}
|
2010-04-09 03:15:27 +00:00
|
|
|
|
2010-04-15 18:55:57 +00:00
|
|
|
if (tcgetattr(fd, &term) < 0)
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_ERR;
|
2011-01-10 04:21:07 +00:00
|
|
|
if (cfsetispeed(&term, baud) < 0)
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_ERR;
|
2011-01-10 04:21:07 +00:00
|
|
|
|
2010-04-09 03:15:27 +00:00
|
|
|
term.c_cflag &= ~CSIZE;
|
2011-01-10 04:21:07 +00:00
|
|
|
switch (bits) {
|
|
|
|
case 8:
|
|
|
|
term.c_cflag |= CS8;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
term.c_cflag |= CS7;
|
|
|
|
break;
|
|
|
|
default:
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_ERR;
|
2011-01-10 04:21:07 +00:00
|
|
|
}
|
|
|
|
|
2010-04-09 03:15:27 +00:00
|
|
|
term.c_cflag &= ~CSTOPB;
|
2011-01-10 04:21:07 +00:00
|
|
|
switch (stopbits) {
|
|
|
|
case 1:
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
term.c_cflag |= CSTOPB;
|
|
|
|
default:
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_ERR;
|
2011-01-10 04:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
term.c_cflag &= ~(IXON | IXOFF | CRTSCTS);
|
|
|
|
switch (flowcontrol) {
|
|
|
|
case 2:
|
|
|
|
term.c_cflag |= IXON | IXOFF;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
term.c_cflag |= CRTSCTS;
|
|
|
|
default:
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_ERR;
|
2011-01-10 04:21:07 +00:00
|
|
|
}
|
|
|
|
|
2011-01-10 17:05:14 +00:00
|
|
|
term.c_iflag &= ~IGNPAR;
|
|
|
|
term.c_cflag &= ~(PARODD | PARENB);
|
2011-01-10 04:21:07 +00:00
|
|
|
switch (parity) {
|
|
|
|
case 0:
|
|
|
|
term.c_iflag |= IGNPAR;
|
|
|
|
break;
|
|
|
|
case 1:
|
2011-01-10 17:05:14 +00:00
|
|
|
term.c_cflag |= PARENB;
|
2011-01-10 04:21:07 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2011-01-10 17:05:14 +00:00
|
|
|
term.c_cflag |= PARENB | PARODD;
|
2011-01-10 04:21:07 +00:00
|
|
|
break;
|
|
|
|
default:
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_ERR;
|
2011-01-10 04:21:07 +00:00
|
|
|
}
|
|
|
|
|
2010-04-15 18:55:57 +00:00
|
|
|
if (tcsetattr(fd, TCSADRAIN, &term) < 0)
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_ERR;
|
2010-04-22 01:39:02 +00:00
|
|
|
#endif
|
2010-04-09 03:15:27 +00:00
|
|
|
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_OK;
|
2010-04-09 03:15:27 +00:00
|
|
|
}
|